Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 2 | /* |
| 3 | * PowerNV OPAL in-memory console interface |
| 4 | * |
| 5 | * Copyright 2014 IBM Corp. |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 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 | |
Oliver O'Halloran | 8471c1d | 2020-08-04 10:54:06 +1000 | [diff] [blame] | 15 | #include "powernv.h" |
| 16 | |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 17 | /* OPAL in-memory console. Defined in OPAL source at core/console.c */ |
| 18 | struct memcons { |
| 19 | __be64 magic; |
| 20 | #define MEMCONS_MAGIC 0x6630696567726173L |
| 21 | __be64 obuf_phys; |
| 22 | __be64 ibuf_phys; |
| 23 | __be32 obuf_size; |
| 24 | __be32 ibuf_size; |
| 25 | __be32 out_pos; |
| 26 | #define MEMCONS_OUT_POS_WRAP 0x80000000u |
| 27 | #define MEMCONS_OUT_POS_MASK 0x00ffffffu |
| 28 | __be32 in_prod; |
| 29 | __be32 in_cons; |
| 30 | }; |
| 31 | |
Andrew Donnellan | 9b4fffa | 2016-02-09 18:17:48 +1100 | [diff] [blame] | 32 | static struct memcons *opal_memcons = NULL; |
| 33 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 34 | ssize_t memcons_copy(struct memcons *mc, char *to, loff_t pos, size_t count) |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 35 | { |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 36 | const char *conbuf; |
Joel Stanley | caf69ba | 2014-06-10 16:03:59 +1000 | [diff] [blame] | 37 | ssize_t ret; |
| 38 | size_t first_read = 0; |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 39 | uint32_t out_pos, avail; |
| 40 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 41 | if (!mc) |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 42 | return -ENODEV; |
| 43 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 44 | out_pos = be32_to_cpu(READ_ONCE(mc->out_pos)); |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 45 | |
| 46 | /* Now we've read out_pos, put a barrier in before reading the new |
| 47 | * data it points to in conbuf. */ |
| 48 | smp_rmb(); |
| 49 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 50 | conbuf = phys_to_virt(be64_to_cpu(mc->obuf_phys)); |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 51 | |
| 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 |
| 54 | * case. */ |
| 55 | if (out_pos & MEMCONS_OUT_POS_WRAP) { |
| 56 | |
| 57 | out_pos &= MEMCONS_OUT_POS_MASK; |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 58 | avail = be32_to_cpu(mc->obuf_size) - out_pos; |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 59 | |
| 60 | ret = memory_read_from_buffer(to, count, &pos, |
| 61 | conbuf + out_pos, avail); |
| 62 | |
| 63 | if (ret < 0) |
| 64 | goto out; |
| 65 | |
| 66 | first_read = ret; |
| 67 | to += first_read; |
| 68 | count -= first_read; |
| 69 | pos -= avail; |
Joel Stanley | caf69ba | 2014-06-10 16:03:59 +1000 | [diff] [blame] | 70 | |
| 71 | if (count <= 0) |
| 72 | goto out; |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* Sanity check. The firmware should not do this to us. */ |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 76 | if (out_pos > be32_to_cpu(mc->obuf_size)) { |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 77 | pr_err("OPAL: memory console corruption. Aborting read.\n"); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos); |
| 82 | |
| 83 | if (ret < 0) |
| 84 | goto out; |
| 85 | |
| 86 | ret += first_read; |
| 87 | out: |
| 88 | return ret; |
| 89 | } |
| 90 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 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 | |
Andrew Donnellan | 9b4fffa | 2016-02-09 18:17:48 +1100 | [diff] [blame] | 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) |
| 99 | { |
| 100 | return opal_msglog_copy(to, pos, count); |
| 101 | } |
| 102 | |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 103 | static struct bin_attribute opal_msglog_attr = { |
Jordan Niethe | 7b62f9b | 2019-02-27 14:02:29 +1100 | [diff] [blame] | 104 | .attr = {.name = "msglog", .mode = 0400}, |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 105 | .read = opal_msglog_read |
| 106 | }; |
| 107 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 108 | struct memcons *memcons_init(struct device_node *node, const char *mc_prop_name) |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 109 | { |
| 110 | u64 mcaddr; |
| 111 | struct memcons *mc; |
| 112 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 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; |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | mc = phys_to_virt(mcaddr); |
| 120 | if (!mc) { |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 121 | pr_warn("memory console address is invalid\n"); |
| 122 | goto out_err; |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) { |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 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"); |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
Claudio Carvalho | dea45ea | 2019-08-28 23:05:20 +1000 | [diff] [blame] | 149 | opal_msglog_attr.size = memcons_get_size(opal_memcons); |
Andrew Donnellan | 9b4fffa | 2016-02-09 18:17:48 +1100 | [diff] [blame] | 150 | } |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 151 | |
Andrew Donnellan | 9b4fffa | 2016-02-09 18:17:48 +1100 | [diff] [blame] | 152 | void __init opal_msglog_sysfs_init(void) |
| 153 | { |
Andrew Donnellan | 88409d0 | 2016-02-18 12:12:54 +1100 | [diff] [blame] | 154 | if (!opal_memcons) { |
| 155 | pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n"); |
| 156 | return; |
| 157 | } |
| 158 | |
Joel Stanley | bfc3689 | 2014-04-01 14:28:19 +1030 | [diff] [blame] | 159 | if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0) |
| 160 | pr_warn("OPAL: sysfs file creation failed\n"); |
| 161 | } |