1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerNV OPAL in-memory console interface
5 * Copyright 2014 IBM Corp.
10 #include <linux/debugfs.h>
12 #include <linux/types.h>
13 #include <asm/barrier.h>
17 /* OPAL in-memory console. Defined in OPAL source at core/console.c */
20 #define MEMCONS_MAGIC 0x6630696567726173L
26 #define MEMCONS_OUT_POS_WRAP 0x80000000u
27 #define MEMCONS_OUT_POS_MASK 0x00ffffffu
32 static struct memcons
*opal_memcons
= NULL
;
34 ssize_t
memcons_copy(struct memcons
*mc
, char *to
, loff_t pos
, size_t count
)
38 size_t first_read
= 0;
39 uint32_t out_pos
, avail
;
44 out_pos
= be32_to_cpu(READ_ONCE(mc
->out_pos
));
46 /* Now we've read out_pos, put a barrier in before reading the new
47 * data it points to in conbuf. */
50 conbuf
= phys_to_virt(be64_to_cpu(mc
->obuf_phys
));
52 /* When the buffer has wrapped, read from the out_pos marker to the end
53 * of the buffer, and then read the remaining data as in the un-wrapped
55 if (out_pos
& MEMCONS_OUT_POS_WRAP
) {
57 out_pos
&= MEMCONS_OUT_POS_MASK
;
58 avail
= be32_to_cpu(mc
->obuf_size
) - out_pos
;
60 ret
= memory_read_from_buffer(to
, count
, &pos
,
61 conbuf
+ out_pos
, avail
);
75 /* Sanity check. The firmware should not do this to us. */
76 if (out_pos
> be32_to_cpu(mc
->obuf_size
)) {
77 pr_err("OPAL: memory console corruption. Aborting read.\n");
81 ret
= memory_read_from_buffer(to
, count
, &pos
, conbuf
, out_pos
);
91 ssize_t
opal_msglog_copy(char *to
, loff_t pos
, size_t count
)
93 return memcons_copy(opal_memcons
, to
, pos
, count
);
96 static ssize_t
opal_msglog_read(struct file
*file
, struct kobject
*kobj
,
97 struct bin_attribute
*bin_attr
, char *to
,
98 loff_t pos
, size_t count
)
100 return opal_msglog_copy(to
, pos
, count
);
103 static struct bin_attribute opal_msglog_attr
= {
104 .attr
= {.name
= "msglog", .mode
= 0400},
105 .read
= opal_msglog_read
108 struct memcons
*memcons_init(struct device_node
*node
, const char *mc_prop_name
)
113 if (of_property_read_u64(node
, mc_prop_name
, &mcaddr
)) {
114 pr_warn("%s property not found, no message log\n",
119 mc
= phys_to_virt(mcaddr
);
121 pr_warn("memory console address is invalid\n");
125 if (be64_to_cpu(mc
->magic
) != MEMCONS_MAGIC
) {
126 pr_warn("memory console version is invalid\n");
136 u32
memcons_get_size(struct memcons
*mc
)
138 return be32_to_cpu(mc
->ibuf_size
) + be32_to_cpu(mc
->obuf_size
);
141 void __init
opal_msglog_init(void)
143 opal_memcons
= memcons_init(opal_node
, "ibm,opal-memcons");
145 pr_warn("OPAL: memcons failed to load from ibm,opal-memcons\n");
149 opal_msglog_attr
.size
= memcons_get_size(opal_memcons
);
152 void __init
opal_msglog_sysfs_init(void)
155 pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n");
159 if (sysfs_create_bin_file(opal_kobj
, &opal_msglog_attr
) != 0)
160 pr_warn("OPAL: sysfs file creation failed\n");