hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/arch/powerpc/platforms/powernv/opal-msglog.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * PowerNV OPAL in-memory console interface
34 *
45 * 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.
106 */
117
128 #include <asm/io.h>
....@@ -15,6 +11,8 @@
1511 #include <linux/of.h>
1612 #include <linux/types.h>
1713 #include <asm/barrier.h>
14
+
15
+#include "powernv.h"
1816
1917 /* OPAL in-memory console. Defined in OPAL source at core/console.c */
2018 struct memcons {
....@@ -33,23 +31,23 @@
3331
3432 static struct memcons *opal_memcons = NULL;
3533
36
-ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count)
34
+ssize_t memcons_copy(struct memcons *mc, char *to, loff_t pos, size_t count)
3735 {
3836 const char *conbuf;
3937 ssize_t ret;
4038 size_t first_read = 0;
4139 uint32_t out_pos, avail;
4240
43
- if (!opal_memcons)
41
+ if (!mc)
4442 return -ENODEV;
4543
46
- out_pos = be32_to_cpu(READ_ONCE(opal_memcons->out_pos));
44
+ out_pos = be32_to_cpu(READ_ONCE(mc->out_pos));
4745
4846 /* Now we've read out_pos, put a barrier in before reading the new
4947 * data it points to in conbuf. */
5048 smp_rmb();
5149
52
- conbuf = phys_to_virt(be64_to_cpu(opal_memcons->obuf_phys));
50
+ conbuf = phys_to_virt(be64_to_cpu(mc->obuf_phys));
5351
5452 /* When the buffer has wrapped, read from the out_pos marker to the end
5553 * of the buffer, and then read the remaining data as in the un-wrapped
....@@ -57,7 +55,7 @@
5755 if (out_pos & MEMCONS_OUT_POS_WRAP) {
5856
5957 out_pos &= MEMCONS_OUT_POS_MASK;
60
- avail = be32_to_cpu(opal_memcons->obuf_size) - out_pos;
58
+ avail = be32_to_cpu(mc->obuf_size) - out_pos;
6159
6260 ret = memory_read_from_buffer(to, count, &pos,
6361 conbuf + out_pos, avail);
....@@ -75,7 +73,7 @@
7573 }
7674
7775 /* Sanity check. The firmware should not do this to us. */
78
- if (out_pos > be32_to_cpu(opal_memcons->obuf_size)) {
76
+ if (out_pos > be32_to_cpu(mc->obuf_size)) {
7977 pr_err("OPAL: memory console corruption. Aborting read.\n");
8078 return -EINVAL;
8179 }
....@@ -90,6 +88,11 @@
9088 return ret;
9189 }
9290
91
+ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count)
92
+{
93
+ return memcons_copy(opal_memcons, to, pos, count);
94
+}
95
+
9396 static ssize_t opal_msglog_read(struct file *file, struct kobject *kobj,
9497 struct bin_attribute *bin_attr, char *to,
9598 loff_t pos, size_t count)
....@@ -102,32 +105,48 @@
102105 .read = opal_msglog_read
103106 };
104107
105
-void __init opal_msglog_init(void)
108
+struct memcons *memcons_init(struct device_node *node, const char *mc_prop_name)
106109 {
107110 u64 mcaddr;
108111 struct memcons *mc;
109112
110
- if (of_property_read_u64(opal_node, "ibm,opal-memcons", &mcaddr)) {
111
- pr_warn("OPAL: Property ibm,opal-memcons not found, no message log\n");
112
- return;
113
+ if (of_property_read_u64(node, mc_prop_name, &mcaddr)) {
114
+ pr_warn("%s property not found, no message log\n",
115
+ mc_prop_name);
116
+ goto out_err;
113117 }
114118
115119 mc = phys_to_virt(mcaddr);
116120 if (!mc) {
117
- pr_warn("OPAL: memory console address is invalid\n");
118
- return;
121
+ pr_warn("memory console address is invalid\n");
122
+ goto out_err;
119123 }
120124
121125 if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) {
122
- pr_warn("OPAL: memory console version is invalid\n");
126
+ pr_warn("memory console version is invalid\n");
127
+ goto out_err;
128
+ }
129
+
130
+ return mc;
131
+
132
+out_err:
133
+ return NULL;
134
+}
135
+
136
+u32 memcons_get_size(struct memcons *mc)
137
+{
138
+ return be32_to_cpu(mc->ibuf_size) + be32_to_cpu(mc->obuf_size);
139
+}
140
+
141
+void __init opal_msglog_init(void)
142
+{
143
+ opal_memcons = memcons_init(opal_node, "ibm,opal-memcons");
144
+ if (!opal_memcons) {
145
+ pr_warn("OPAL: memcons failed to load from ibm,opal-memcons\n");
123146 return;
124147 }
125148
126
- /* Report maximum size */
127
- opal_msglog_attr.size = be32_to_cpu(mc->ibuf_size) +
128
- be32_to_cpu(mc->obuf_size);
129
-
130
- opal_memcons = mc;
149
+ opal_msglog_attr.size = memcons_get_size(opal_memcons);
131150 }
132151
133152 void __init opal_msglog_sysfs_init(void)