2 * PowerNV OPAL in-memory console interface
4 * Copyright 2014 IBM Corp.
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.
14 #include <linux/debugfs.h>
16 #include <linux/types.h>
17 #include <asm/barrier.h>
19 /* OPAL in-memory console. Defined in OPAL source at core/console.c */
22 #define MEMCONS_MAGIC 0x6630696567726173L
28 #define MEMCONS_OUT_POS_WRAP 0x80000000u
29 #define MEMCONS_OUT_POS_MASK 0x00ffffffu
34 static struct memcons
*opal_memcons
= NULL
;
36 ssize_t
opal_msglog_copy(char *to
, loff_t pos
, size_t count
)
40 size_t first_read
= 0;
41 uint32_t out_pos
, avail
;
46 out_pos
= be32_to_cpu(ACCESS_ONCE(opal_memcons
->out_pos
));
48 /* Now we've read out_pos, put a barrier in before reading the new
49 * data it points to in conbuf. */
52 conbuf
= phys_to_virt(be64_to_cpu(opal_memcons
->obuf_phys
));
54 /* When the buffer has wrapped, read from the out_pos marker to the end
55 * of the buffer, and then read the remaining data as in the un-wrapped
57 if (out_pos
& MEMCONS_OUT_POS_WRAP
) {
59 out_pos
&= MEMCONS_OUT_POS_MASK
;
60 avail
= be32_to_cpu(opal_memcons
->obuf_size
) - out_pos
;
62 ret
= memory_read_from_buffer(to
, count
, &pos
,
63 conbuf
+ out_pos
, avail
);
77 /* Sanity check. The firmware should not do this to us. */
78 if (out_pos
> be32_to_cpu(opal_memcons
->obuf_size
)) {
79 pr_err("OPAL: memory console corruption. Aborting read.\n");
83 ret
= memory_read_from_buffer(to
, count
, &pos
, conbuf
, out_pos
);
93 static ssize_t
opal_msglog_read(struct file
*file
, struct kobject
*kobj
,
94 struct bin_attribute
*bin_attr
, char *to
,
95 loff_t pos
, size_t count
)
97 return opal_msglog_copy(to
, pos
, count
);
100 static struct bin_attribute opal_msglog_attr
= {
101 .attr
= {.name
= "msglog", .mode
= 0444},
102 .read
= opal_msglog_read
105 void __init
opal_msglog_init(void)
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");
115 mc
= phys_to_virt(mcaddr
);
117 pr_warn("OPAL: memory console address is invalid\n");
121 if (be64_to_cpu(mc
->magic
) != MEMCONS_MAGIC
) {
122 pr_warn("OPAL: memory console version is invalid\n");
126 /* Report maximum size */
127 opal_msglog_attr
.size
= be32_to_cpu(mc
->ibuf_size
) +
128 be32_to_cpu(mc
->obuf_size
);
133 void __init
opal_msglog_sysfs_init(void)
136 pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n");
140 if (sysfs_create_bin_file(opal_kobj
, &opal_msglog_attr
) != 0)
141 pr_warn("OPAL: sysfs file creation failed\n");