1/*
2 * PowerNV OPAL in-memory console interface
3 *
4 * Copyright 2014 IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <asm/io.h>
13#include <asm/opal.h>
14#include <linux/debugfs.h>
15#include <linux/of.h>
16#include <linux/types.h>
17#include <asm/barrier.h>
18
19/* OPAL in-memory console. Defined in OPAL source at core/console.c */
20struct memcons {
21	__be64 magic;
22#define MEMCONS_MAGIC	0x6630696567726173L
23	__be64 obuf_phys;
24	__be64 ibuf_phys;
25	__be32 obuf_size;
26	__be32 ibuf_size;
27	__be32 out_pos;
28#define MEMCONS_OUT_POS_WRAP	0x80000000u
29#define MEMCONS_OUT_POS_MASK	0x00ffffffu
30	__be32 in_prod;
31	__be32 in_cons;
32};
33
34static ssize_t opal_msglog_read(struct file *file, struct kobject *kobj,
35				struct bin_attribute *bin_attr, char *to,
36				loff_t pos, size_t count)
37{
38	struct memcons *mc = bin_attr->private;
39	const char *conbuf;
40	ssize_t ret;
41	size_t first_read = 0;
42	uint32_t out_pos, avail;
43
44	if (!mc)
45		return -ENODEV;
46
47	out_pos = be32_to_cpu(ACCESS_ONCE(mc->out_pos));
48
49	/* Now we've read out_pos, put a barrier in before reading the new
50	 * data it points to in conbuf. */
51	smp_rmb();
52
53	conbuf = phys_to_virt(be64_to_cpu(mc->obuf_phys));
54
55	/* When the buffer has wrapped, read from the out_pos marker to the end
56	 * of the buffer, and then read the remaining data as in the un-wrapped
57	 * case. */
58	if (out_pos & MEMCONS_OUT_POS_WRAP) {
59
60		out_pos &= MEMCONS_OUT_POS_MASK;
61		avail = be32_to_cpu(mc->obuf_size) - out_pos;
62
63		ret = memory_read_from_buffer(to, count, &pos,
64				conbuf + out_pos, avail);
65
66		if (ret < 0)
67			goto out;
68
69		first_read = ret;
70		to += first_read;
71		count -= first_read;
72		pos -= avail;
73
74		if (count <= 0)
75			goto out;
76	}
77
78	/* Sanity check. The firmware should not do this to us. */
79	if (out_pos > be32_to_cpu(mc->obuf_size)) {
80		pr_err("OPAL: memory console corruption. Aborting read.\n");
81		return -EINVAL;
82	}
83
84	ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos);
85
86	if (ret < 0)
87		goto out;
88
89	ret += first_read;
90out:
91	return ret;
92}
93
94static struct bin_attribute opal_msglog_attr = {
95	.attr = {.name = "msglog", .mode = 0444},
96	.read = opal_msglog_read
97};
98
99void __init opal_msglog_init(void)
100{
101	u64 mcaddr;
102	struct memcons *mc;
103
104	if (of_property_read_u64(opal_node, "ibm,opal-memcons", &mcaddr)) {
105		pr_warn("OPAL: Property ibm,opal-memcons not found, no message log\n");
106		return;
107	}
108
109	mc = phys_to_virt(mcaddr);
110	if (!mc) {
111		pr_warn("OPAL: memory console address is invalid\n");
112		return;
113	}
114
115	if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) {
116		pr_warn("OPAL: memory console version is invalid\n");
117		return;
118	}
119
120	opal_msglog_attr.private = mc;
121
122	if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0)
123		pr_warn("OPAL: sysfs file creation failed\n");
124}
125