12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2bfc36894SJoel Stanley /*
3bfc36894SJoel Stanley  * PowerNV OPAL in-memory console interface
4bfc36894SJoel Stanley  *
5bfc36894SJoel Stanley  * Copyright 2014 IBM Corp.
6bfc36894SJoel Stanley  */
7bfc36894SJoel Stanley 
8bfc36894SJoel Stanley #include <asm/io.h>
9bfc36894SJoel Stanley #include <asm/opal.h>
10bfc36894SJoel Stanley #include <linux/debugfs.h>
11bfc36894SJoel Stanley #include <linux/of.h>
12bfc36894SJoel Stanley #include <linux/types.h>
13bfc36894SJoel Stanley #include <asm/barrier.h>
14bfc36894SJoel Stanley 
158471c1ddSOliver O'Halloran #include "powernv.h"
168471c1ddSOliver O'Halloran 
17bfc36894SJoel Stanley /* OPAL in-memory console. Defined in OPAL source at core/console.c */
18bfc36894SJoel Stanley struct memcons {
19bfc36894SJoel Stanley 	__be64 magic;
20bfc36894SJoel Stanley #define MEMCONS_MAGIC	0x6630696567726173L
21bfc36894SJoel Stanley 	__be64 obuf_phys;
22bfc36894SJoel Stanley 	__be64 ibuf_phys;
23bfc36894SJoel Stanley 	__be32 obuf_size;
24bfc36894SJoel Stanley 	__be32 ibuf_size;
25bfc36894SJoel Stanley 	__be32 out_pos;
26bfc36894SJoel Stanley #define MEMCONS_OUT_POS_WRAP	0x80000000u
27bfc36894SJoel Stanley #define MEMCONS_OUT_POS_MASK	0x00ffffffu
28bfc36894SJoel Stanley 	__be32 in_prod;
29bfc36894SJoel Stanley 	__be32 in_cons;
30bfc36894SJoel Stanley };
31bfc36894SJoel Stanley 
329b4fffa1SAndrew Donnellan static struct memcons *opal_memcons = NULL;
339b4fffa1SAndrew Donnellan 
memcons_copy(struct memcons * mc,char * to,loff_t pos,size_t count)34dea45ea7SClaudio Carvalho ssize_t memcons_copy(struct memcons *mc, char *to, loff_t pos, size_t count)
35bfc36894SJoel Stanley {
36bfc36894SJoel Stanley 	const char *conbuf;
37caf69ba6SJoel Stanley 	ssize_t ret;
38caf69ba6SJoel Stanley 	size_t first_read = 0;
39bfc36894SJoel Stanley 	uint32_t out_pos, avail;
40bfc36894SJoel Stanley 
41dea45ea7SClaudio Carvalho 	if (!mc)
42bfc36894SJoel Stanley 		return -ENODEV;
43bfc36894SJoel Stanley 
44dea45ea7SClaudio Carvalho 	out_pos = be32_to_cpu(READ_ONCE(mc->out_pos));
45bfc36894SJoel Stanley 
46bfc36894SJoel Stanley 	/* Now we've read out_pos, put a barrier in before reading the new
47bfc36894SJoel Stanley 	 * data it points to in conbuf. */
48bfc36894SJoel Stanley 	smp_rmb();
49bfc36894SJoel Stanley 
50dea45ea7SClaudio Carvalho 	conbuf = phys_to_virt(be64_to_cpu(mc->obuf_phys));
51bfc36894SJoel Stanley 
52bfc36894SJoel Stanley 	/* When the buffer has wrapped, read from the out_pos marker to the end
53bfc36894SJoel Stanley 	 * of the buffer, and then read the remaining data as in the un-wrapped
54bfc36894SJoel Stanley 	 * case. */
55bfc36894SJoel Stanley 	if (out_pos & MEMCONS_OUT_POS_WRAP) {
56bfc36894SJoel Stanley 
57bfc36894SJoel Stanley 		out_pos &= MEMCONS_OUT_POS_MASK;
58dea45ea7SClaudio Carvalho 		avail = be32_to_cpu(mc->obuf_size) - out_pos;
59bfc36894SJoel Stanley 
60bfc36894SJoel Stanley 		ret = memory_read_from_buffer(to, count, &pos,
61bfc36894SJoel Stanley 				conbuf + out_pos, avail);
62bfc36894SJoel Stanley 
63bfc36894SJoel Stanley 		if (ret < 0)
64bfc36894SJoel Stanley 			goto out;
65bfc36894SJoel Stanley 
66bfc36894SJoel Stanley 		first_read = ret;
67bfc36894SJoel Stanley 		to += first_read;
68bfc36894SJoel Stanley 		count -= first_read;
69bfc36894SJoel Stanley 		pos -= avail;
70caf69ba6SJoel Stanley 
71caf69ba6SJoel Stanley 		if (count <= 0)
72caf69ba6SJoel Stanley 			goto out;
73bfc36894SJoel Stanley 	}
74bfc36894SJoel Stanley 
75bfc36894SJoel Stanley 	/* Sanity check. The firmware should not do this to us. */
76dea45ea7SClaudio Carvalho 	if (out_pos > be32_to_cpu(mc->obuf_size)) {
77bfc36894SJoel Stanley 		pr_err("OPAL: memory console corruption. Aborting read.\n");
78bfc36894SJoel Stanley 		return -EINVAL;
79bfc36894SJoel Stanley 	}
80bfc36894SJoel Stanley 
81bfc36894SJoel Stanley 	ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos);
82bfc36894SJoel Stanley 
83bfc36894SJoel Stanley 	if (ret < 0)
84bfc36894SJoel Stanley 		goto out;
85bfc36894SJoel Stanley 
86bfc36894SJoel Stanley 	ret += first_read;
87bfc36894SJoel Stanley out:
88bfc36894SJoel Stanley 	return ret;
89bfc36894SJoel Stanley }
90bfc36894SJoel Stanley 
opal_msglog_copy(char * to,loff_t pos,size_t count)91dea45ea7SClaudio Carvalho ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count)
92dea45ea7SClaudio Carvalho {
93dea45ea7SClaudio Carvalho 	return memcons_copy(opal_memcons, to, pos, count);
94dea45ea7SClaudio Carvalho }
95dea45ea7SClaudio Carvalho 
opal_msglog_read(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * to,loff_t pos,size_t count)969b4fffa1SAndrew Donnellan static ssize_t opal_msglog_read(struct file *file, struct kobject *kobj,
979b4fffa1SAndrew Donnellan 				struct bin_attribute *bin_attr, char *to,
989b4fffa1SAndrew Donnellan 				loff_t pos, size_t count)
999b4fffa1SAndrew Donnellan {
1009b4fffa1SAndrew Donnellan 	return opal_msglog_copy(to, pos, count);
1019b4fffa1SAndrew Donnellan }
1029b4fffa1SAndrew Donnellan 
103bfc36894SJoel Stanley static struct bin_attribute opal_msglog_attr = {
1047b62f9bdSJordan Niethe 	.attr = {.name = "msglog", .mode = 0400},
105bfc36894SJoel Stanley 	.read = opal_msglog_read
106bfc36894SJoel Stanley };
107bfc36894SJoel Stanley 
memcons_init(struct device_node * node,const char * mc_prop_name)108*e5913db1SNick Child struct memcons *__init memcons_init(struct device_node *node, const char *mc_prop_name)
109bfc36894SJoel Stanley {
110bfc36894SJoel Stanley 	u64 mcaddr;
111bfc36894SJoel Stanley 	struct memcons *mc;
112bfc36894SJoel Stanley 
113dea45ea7SClaudio Carvalho 	if (of_property_read_u64(node, mc_prop_name, &mcaddr)) {
114dea45ea7SClaudio Carvalho 		pr_warn("%s property not found, no message log\n",
115dea45ea7SClaudio Carvalho 			mc_prop_name);
116dea45ea7SClaudio Carvalho 		goto out_err;
117bfc36894SJoel Stanley 	}
118bfc36894SJoel Stanley 
119bfc36894SJoel Stanley 	mc = phys_to_virt(mcaddr);
120bfc36894SJoel Stanley 	if (!mc) {
121dea45ea7SClaudio Carvalho 		pr_warn("memory console address is invalid\n");
122dea45ea7SClaudio Carvalho 		goto out_err;
123bfc36894SJoel Stanley 	}
124bfc36894SJoel Stanley 
125bfc36894SJoel Stanley 	if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) {
126dea45ea7SClaudio Carvalho 		pr_warn("memory console version is invalid\n");
127dea45ea7SClaudio Carvalho 		goto out_err;
128dea45ea7SClaudio Carvalho 	}
129dea45ea7SClaudio Carvalho 
130dea45ea7SClaudio Carvalho 	return mc;
131dea45ea7SClaudio Carvalho 
132dea45ea7SClaudio Carvalho out_err:
133dea45ea7SClaudio Carvalho 	return NULL;
134dea45ea7SClaudio Carvalho }
135dea45ea7SClaudio Carvalho 
memcons_get_size(struct memcons * mc)136*e5913db1SNick Child u32 __init memcons_get_size(struct memcons *mc)
137dea45ea7SClaudio Carvalho {
138dea45ea7SClaudio Carvalho 	return be32_to_cpu(mc->ibuf_size) + be32_to_cpu(mc->obuf_size);
139dea45ea7SClaudio Carvalho }
140dea45ea7SClaudio Carvalho 
opal_msglog_init(void)141dea45ea7SClaudio Carvalho void __init opal_msglog_init(void)
142dea45ea7SClaudio Carvalho {
143dea45ea7SClaudio Carvalho 	opal_memcons = memcons_init(opal_node, "ibm,opal-memcons");
144dea45ea7SClaudio Carvalho 	if (!opal_memcons) {
145dea45ea7SClaudio Carvalho 		pr_warn("OPAL: memcons failed to load from ibm,opal-memcons\n");
146bfc36894SJoel Stanley 		return;
147bfc36894SJoel Stanley 	}
148bfc36894SJoel Stanley 
149dea45ea7SClaudio Carvalho 	opal_msglog_attr.size = memcons_get_size(opal_memcons);
1509b4fffa1SAndrew Donnellan }
151bfc36894SJoel Stanley 
opal_msglog_sysfs_init(void)1529b4fffa1SAndrew Donnellan void __init opal_msglog_sysfs_init(void)
1539b4fffa1SAndrew Donnellan {
15488409d0cSAndrew Donnellan 	if (!opal_memcons) {
15588409d0cSAndrew Donnellan 		pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n");
15688409d0cSAndrew Donnellan 		return;
15788409d0cSAndrew Donnellan 	}
15888409d0cSAndrew Donnellan 
159bfc36894SJoel Stanley 	if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0)
160bfc36894SJoel Stanley 		pr_warn("OPAL: sysfs file creation failed\n");
161bfc36894SJoel Stanley }
162