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