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 15bfc36894SJoel Stanley /* OPAL in-memory console. Defined in OPAL source at core/console.c */ 16bfc36894SJoel Stanley struct memcons { 17bfc36894SJoel Stanley __be64 magic; 18bfc36894SJoel Stanley #define MEMCONS_MAGIC 0x6630696567726173L 19bfc36894SJoel Stanley __be64 obuf_phys; 20bfc36894SJoel Stanley __be64 ibuf_phys; 21bfc36894SJoel Stanley __be32 obuf_size; 22bfc36894SJoel Stanley __be32 ibuf_size; 23bfc36894SJoel Stanley __be32 out_pos; 24bfc36894SJoel Stanley #define MEMCONS_OUT_POS_WRAP 0x80000000u 25bfc36894SJoel Stanley #define MEMCONS_OUT_POS_MASK 0x00ffffffu 26bfc36894SJoel Stanley __be32 in_prod; 27bfc36894SJoel Stanley __be32 in_cons; 28bfc36894SJoel Stanley }; 29bfc36894SJoel Stanley 309b4fffa1SAndrew Donnellan static struct memcons *opal_memcons = NULL; 319b4fffa1SAndrew Donnellan 329b4fffa1SAndrew Donnellan ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count) 33bfc36894SJoel Stanley { 34bfc36894SJoel Stanley const char *conbuf; 35caf69ba6SJoel Stanley ssize_t ret; 36caf69ba6SJoel Stanley size_t first_read = 0; 37bfc36894SJoel Stanley uint32_t out_pos, avail; 38bfc36894SJoel Stanley 399b4fffa1SAndrew Donnellan if (!opal_memcons) 40bfc36894SJoel Stanley return -ENODEV; 41bfc36894SJoel Stanley 426aa7de05SMark Rutland out_pos = be32_to_cpu(READ_ONCE(opal_memcons->out_pos)); 43bfc36894SJoel Stanley 44bfc36894SJoel Stanley /* Now we've read out_pos, put a barrier in before reading the new 45bfc36894SJoel Stanley * data it points to in conbuf. */ 46bfc36894SJoel Stanley smp_rmb(); 47bfc36894SJoel Stanley 489b4fffa1SAndrew Donnellan conbuf = phys_to_virt(be64_to_cpu(opal_memcons->obuf_phys)); 49bfc36894SJoel Stanley 50bfc36894SJoel Stanley /* When the buffer has wrapped, read from the out_pos marker to the end 51bfc36894SJoel Stanley * of the buffer, and then read the remaining data as in the un-wrapped 52bfc36894SJoel Stanley * case. */ 53bfc36894SJoel Stanley if (out_pos & MEMCONS_OUT_POS_WRAP) { 54bfc36894SJoel Stanley 55bfc36894SJoel Stanley out_pos &= MEMCONS_OUT_POS_MASK; 569b4fffa1SAndrew Donnellan avail = be32_to_cpu(opal_memcons->obuf_size) - out_pos; 57bfc36894SJoel Stanley 58bfc36894SJoel Stanley ret = memory_read_from_buffer(to, count, &pos, 59bfc36894SJoel Stanley conbuf + out_pos, avail); 60bfc36894SJoel Stanley 61bfc36894SJoel Stanley if (ret < 0) 62bfc36894SJoel Stanley goto out; 63bfc36894SJoel Stanley 64bfc36894SJoel Stanley first_read = ret; 65bfc36894SJoel Stanley to += first_read; 66bfc36894SJoel Stanley count -= first_read; 67bfc36894SJoel Stanley pos -= avail; 68caf69ba6SJoel Stanley 69caf69ba6SJoel Stanley if (count <= 0) 70caf69ba6SJoel Stanley goto out; 71bfc36894SJoel Stanley } 72bfc36894SJoel Stanley 73bfc36894SJoel Stanley /* Sanity check. The firmware should not do this to us. */ 749b4fffa1SAndrew Donnellan if (out_pos > be32_to_cpu(opal_memcons->obuf_size)) { 75bfc36894SJoel Stanley pr_err("OPAL: memory console corruption. Aborting read.\n"); 76bfc36894SJoel Stanley return -EINVAL; 77bfc36894SJoel Stanley } 78bfc36894SJoel Stanley 79bfc36894SJoel Stanley ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos); 80bfc36894SJoel Stanley 81bfc36894SJoel Stanley if (ret < 0) 82bfc36894SJoel Stanley goto out; 83bfc36894SJoel Stanley 84bfc36894SJoel Stanley ret += first_read; 85bfc36894SJoel Stanley out: 86bfc36894SJoel Stanley return ret; 87bfc36894SJoel Stanley } 88bfc36894SJoel Stanley 899b4fffa1SAndrew Donnellan static ssize_t opal_msglog_read(struct file *file, struct kobject *kobj, 909b4fffa1SAndrew Donnellan struct bin_attribute *bin_attr, char *to, 919b4fffa1SAndrew Donnellan loff_t pos, size_t count) 929b4fffa1SAndrew Donnellan { 939b4fffa1SAndrew Donnellan return opal_msglog_copy(to, pos, count); 949b4fffa1SAndrew Donnellan } 959b4fffa1SAndrew Donnellan 96bfc36894SJoel Stanley static struct bin_attribute opal_msglog_attr = { 977b62f9bdSJordan Niethe .attr = {.name = "msglog", .mode = 0400}, 98bfc36894SJoel Stanley .read = opal_msglog_read 99bfc36894SJoel Stanley }; 100bfc36894SJoel Stanley 101bfc36894SJoel Stanley void __init opal_msglog_init(void) 102bfc36894SJoel Stanley { 103bfc36894SJoel Stanley u64 mcaddr; 104bfc36894SJoel Stanley struct memcons *mc; 105bfc36894SJoel Stanley 106bfc36894SJoel Stanley if (of_property_read_u64(opal_node, "ibm,opal-memcons", &mcaddr)) { 107bfc36894SJoel Stanley pr_warn("OPAL: Property ibm,opal-memcons not found, no message log\n"); 108bfc36894SJoel Stanley return; 109bfc36894SJoel Stanley } 110bfc36894SJoel Stanley 111bfc36894SJoel Stanley mc = phys_to_virt(mcaddr); 112bfc36894SJoel Stanley if (!mc) { 113bfc36894SJoel Stanley pr_warn("OPAL: memory console address is invalid\n"); 114bfc36894SJoel Stanley return; 115bfc36894SJoel Stanley } 116bfc36894SJoel Stanley 117bfc36894SJoel Stanley if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) { 118bfc36894SJoel Stanley pr_warn("OPAL: memory console version is invalid\n"); 119bfc36894SJoel Stanley return; 120bfc36894SJoel Stanley } 121bfc36894SJoel Stanley 12214a41d6bSJoel Stanley /* Report maximum size */ 12314a41d6bSJoel Stanley opal_msglog_attr.size = be32_to_cpu(mc->ibuf_size) + 12414a41d6bSJoel Stanley be32_to_cpu(mc->obuf_size); 12514a41d6bSJoel Stanley 1269b4fffa1SAndrew Donnellan opal_memcons = mc; 1279b4fffa1SAndrew Donnellan } 128bfc36894SJoel Stanley 1299b4fffa1SAndrew Donnellan void __init opal_msglog_sysfs_init(void) 1309b4fffa1SAndrew Donnellan { 13188409d0cSAndrew Donnellan if (!opal_memcons) { 13288409d0cSAndrew Donnellan pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n"); 13388409d0cSAndrew Donnellan return; 13488409d0cSAndrew Donnellan } 13588409d0cSAndrew Donnellan 136bfc36894SJoel Stanley if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0) 137bfc36894SJoel Stanley pr_warn("OPAL: sysfs file creation failed\n"); 138bfc36894SJoel Stanley } 139