Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * c 2001 PPC 64 Team, IBM Corp |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * /dev/nvram driver for PPC64 |
| 10 | * |
| 11 | * This perhaps should live in drivers/char |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 19 | #include <linux/slab.h> |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 20 | #include <linux/ctype.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/nvram.h> |
| 23 | #include <asm/rtas.h> |
| 24 | #include <asm/prom.h> |
| 25 | #include <asm/machdep.h> |
| 26 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 27 | /* Max bytes to read/write in one go */ |
| 28 | #define NVRW_CNT 0x20 |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static unsigned int nvram_size; |
| 31 | static int nvram_fetch, nvram_store; |
| 32 | static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */ |
| 33 | static DEFINE_SPINLOCK(nvram_lock); |
| 34 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 35 | /* See clobbering_unread_rtas_event() */ |
| 36 | #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */ |
Hari Bathini | e4a9616 | 2015-02-06 01:07:17 +0530 | [diff] [blame] | 37 | static time64_t last_unread_rtas_event; /* timestamp */ |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 38 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 39 | #ifdef CONFIG_PSTORE |
Hari Bathini | e4a9616 | 2015-02-06 01:07:17 +0530 | [diff] [blame] | 40 | time64_t last_rtas_event; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 41 | #endif |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) |
| 44 | { |
| 45 | unsigned int i; |
| 46 | unsigned long len; |
| 47 | int done; |
| 48 | unsigned long flags; |
| 49 | char *p = buf; |
| 50 | |
| 51 | |
| 52 | if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE) |
| 53 | return -ENODEV; |
| 54 | |
| 55 | if (*index >= nvram_size) |
| 56 | return 0; |
| 57 | |
| 58 | i = *index; |
| 59 | if (i + count > nvram_size) |
| 60 | count = nvram_size - i; |
| 61 | |
| 62 | spin_lock_irqsave(&nvram_lock, flags); |
| 63 | |
| 64 | for (; count != 0; count -= len) { |
| 65 | len = count; |
| 66 | if (len > NVRW_CNT) |
| 67 | len = NVRW_CNT; |
| 68 | |
| 69 | if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf), |
| 70 | len) != 0) || len != done) { |
| 71 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 72 | return -EIO; |
| 73 | } |
| 74 | |
| 75 | memcpy(p, nvram_buf, len); |
| 76 | |
| 77 | p += len; |
| 78 | i += len; |
| 79 | } |
| 80 | |
| 81 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 82 | |
| 83 | *index = i; |
| 84 | return p - buf; |
| 85 | } |
| 86 | |
| 87 | static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index) |
| 88 | { |
| 89 | unsigned int i; |
| 90 | unsigned long len; |
| 91 | int done; |
| 92 | unsigned long flags; |
| 93 | const char *p = buf; |
| 94 | |
| 95 | if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE) |
| 96 | return -ENODEV; |
| 97 | |
| 98 | if (*index >= nvram_size) |
| 99 | return 0; |
| 100 | |
| 101 | i = *index; |
| 102 | if (i + count > nvram_size) |
| 103 | count = nvram_size - i; |
| 104 | |
| 105 | spin_lock_irqsave(&nvram_lock, flags); |
| 106 | |
| 107 | for (; count != 0; count -= len) { |
| 108 | len = count; |
| 109 | if (len > NVRW_CNT) |
| 110 | len = NVRW_CNT; |
| 111 | |
| 112 | memcpy(nvram_buf, p, len); |
| 113 | |
| 114 | if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf), |
| 115 | len) != 0) || len != done) { |
| 116 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 117 | return -EIO; |
| 118 | } |
| 119 | |
| 120 | p += len; |
| 121 | i += len; |
| 122 | } |
| 123 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 124 | |
| 125 | *index = i; |
| 126 | return p - buf; |
| 127 | } |
| 128 | |
| 129 | static ssize_t pSeries_nvram_get_size(void) |
| 130 | { |
| 131 | return nvram_size ? nvram_size : -ENODEV; |
| 132 | } |
| 133 | |
Hari Bathini | 78989f0 | 2015-02-06 01:06:04 +0530 | [diff] [blame] | 134 | /* nvram_write_error_log |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 135 | * |
| 136 | * We need to buffer the error logs into nvram to ensure that we have |
Hari Bathini | 78989f0 | 2015-02-06 01:06:04 +0530 | [diff] [blame] | 137 | * the failure information to decode. |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 138 | */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 139 | int nvram_write_error_log(char * buff, int length, |
| 140 | unsigned int err_type, unsigned int error_log_cnt) |
| 141 | { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 142 | int rc = nvram_write_os_partition(&rtas_log_partition, buff, length, |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 143 | err_type, error_log_cnt); |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 144 | if (!rc) { |
Hari Bathini | e4a9616 | 2015-02-06 01:07:17 +0530 | [diff] [blame] | 145 | last_unread_rtas_event = ktime_get_real_seconds(); |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 146 | #ifdef CONFIG_PSTORE |
Hari Bathini | e4a9616 | 2015-02-06 01:07:17 +0530 | [diff] [blame] | 147 | last_rtas_event = ktime_get_real_seconds(); |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 148 | #endif |
| 149 | } |
| 150 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 151 | return rc; |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 154 | /* nvram_read_error_log |
| 155 | * |
| 156 | * Reads nvram for error log for at most 'length' |
| 157 | */ |
| 158 | int nvram_read_error_log(char *buff, int length, |
| 159 | unsigned int *err_type, unsigned int *error_log_cnt) |
| 160 | { |
| 161 | return nvram_read_partition(&rtas_log_partition, buff, length, |
| 162 | err_type, error_log_cnt); |
| 163 | } |
| 164 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 165 | /* This doesn't actually zero anything, but it sets the event_logged |
| 166 | * word to tell that this event is safely in syslog. |
| 167 | */ |
| 168 | int nvram_clear_error_log(void) |
| 169 | { |
| 170 | loff_t tmp_index; |
| 171 | int clear_word = ERR_FLAG_ALREADY_LOGGED; |
| 172 | int rc; |
| 173 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 174 | if (rtas_log_partition.index == -1) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 175 | return -1; |
| 176 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 177 | tmp_index = rtas_log_partition.index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 178 | |
| 179 | rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); |
| 180 | if (rc <= 0) { |
| 181 | printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); |
| 182 | return rc; |
| 183 | } |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 184 | last_unread_rtas_event = 0; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 189 | /* |
| 190 | * Are we using the ibm,rtas-log for oops/panic reports? And if so, |
| 191 | * would logging this oops/panic overwrite an RTAS event that rtas_errd |
| 192 | * hasn't had a chance to read and process? Return 1 if so, else 0. |
| 193 | * |
| 194 | * We assume that if rtas_errd hasn't read the RTAS event in |
| 195 | * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to. |
| 196 | */ |
Hari Bathini | 78989f0 | 2015-02-06 01:06:04 +0530 | [diff] [blame] | 197 | int clobbering_unread_rtas_event(void) |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 198 | { |
| 199 | return (oops_log_partition.index == rtas_log_partition.index |
| 200 | && last_unread_rtas_event |
Hari Bathini | e4a9616 | 2015-02-06 01:07:17 +0530 | [diff] [blame] | 201 | && ktime_get_real_seconds() - last_unread_rtas_event <= |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 202 | NVRAM_RTAS_READ_TIMEOUT); |
| 203 | } |
| 204 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 205 | static int __init pseries_nvram_init_log_partitions(void) |
| 206 | { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 207 | int rc; |
| 208 | |
Cedric Le Goater | 65f36f4 | 2013-10-30 14:47:06 +0100 | [diff] [blame] | 209 | /* Scan nvram for partitions */ |
| 210 | nvram_scan_partitions(); |
| 211 | |
Hari Bathini | 78989f0 | 2015-02-06 01:06:04 +0530 | [diff] [blame] | 212 | rc = nvram_init_os_partition(&rtas_log_partition); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 213 | nvram_init_oops_partition(rc == 0); |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int __init pSeries_nvram_init(void) |
| 219 | { |
| 220 | struct device_node *nvram; |
Cedric Le Goater | 563c5d8 | 2013-10-30 14:47:07 +0100 | [diff] [blame] | 221 | const __be32 *nbytes_p; |
Jeremy Kerr | 954a46e | 2006-07-12 15:39:43 +1000 | [diff] [blame] | 222 | unsigned int proplen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | nvram = of_find_node_by_type(NULL, "nvram"); |
| 225 | if (nvram == NULL) |
| 226 | return -ENODEV; |
| 227 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 228 | nbytes_p = of_get_property(nvram, "#bytes", &proplen); |
Julia Lawall | bad5232 | 2008-06-09 22:20:04 +1000 | [diff] [blame] | 229 | if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { |
| 230 | of_node_put(nvram); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return -EIO; |
Julia Lawall | bad5232 | 2008-06-09 22:20:04 +1000 | [diff] [blame] | 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Cedric Le Goater | 563c5d8 | 2013-10-30 14:47:07 +0100 | [diff] [blame] | 234 | nvram_size = be32_to_cpup(nbytes_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | nvram_fetch = rtas_token("nvram-fetch"); |
| 237 | nvram_store = rtas_token("nvram-store"); |
| 238 | printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size); |
| 239 | of_node_put(nvram); |
| 240 | |
| 241 | ppc_md.nvram_read = pSeries_nvram_read; |
| 242 | ppc_md.nvram_write = pSeries_nvram_write; |
| 243 | ppc_md.nvram_size = pSeries_nvram_get_size; |
| 244 | |
| 245 | return 0; |
| 246 | } |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 247 | |