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