1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * c 2001 PPC 64 Team, IBM Corp
5 * /dev/nvram driver for PPC64
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/slab.h>
14 #include <linux/ctype.h>
15 #include <linux/uaccess.h>
17 #include <asm/nvram.h>
19 #include <asm/machdep.h>
21 /* Max bytes to read/write in one go */
24 static unsigned int nvram_size;
25 static int nvram_fetch, nvram_store;
26 static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
27 static DEFINE_SPINLOCK(nvram_lock);
29 /* See clobbering_unread_rtas_event() */
30 #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
31 static time64_t last_unread_rtas_event; /* timestamp */
34 time64_t last_rtas_event;
37 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
46 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
49 if (*index >= nvram_size)
53 if (i + count > nvram_size)
54 count = nvram_size - i;
56 spin_lock_irqsave(&nvram_lock, flags);
58 for (; count != 0; count -= len) {
63 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
64 len) != 0) || len != done) {
65 spin_unlock_irqrestore(&nvram_lock, flags);
69 memcpy(p, nvram_buf, len);
75 spin_unlock_irqrestore(&nvram_lock, flags);
81 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
89 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
92 if (*index >= nvram_size)
96 if (i + count > nvram_size)
97 count = nvram_size - i;
99 spin_lock_irqsave(&nvram_lock, flags);
101 for (; count != 0; count -= len) {
106 memcpy(nvram_buf, p, len);
108 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
109 len) != 0) || len != done) {
110 spin_unlock_irqrestore(&nvram_lock, flags);
117 spin_unlock_irqrestore(&nvram_lock, flags);
123 static ssize_t pSeries_nvram_get_size(void)
125 return nvram_size ? nvram_size : -ENODEV;
128 /* nvram_write_error_log
130 * We need to buffer the error logs into nvram to ensure that we have
131 * the failure information to decode.
133 int nvram_write_error_log(char * buff, int length,
134 unsigned int err_type, unsigned int error_log_cnt)
136 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
137 err_type, error_log_cnt);
139 last_unread_rtas_event = ktime_get_real_seconds();
141 last_rtas_event = ktime_get_real_seconds();
148 /* nvram_read_error_log
150 * Reads nvram for error log for at most 'length'
152 int nvram_read_error_log(char *buff, int length,
153 unsigned int *err_type, unsigned int *error_log_cnt)
155 return nvram_read_partition(&rtas_log_partition, buff, length,
156 err_type, error_log_cnt);
159 /* This doesn't actually zero anything, but it sets the event_logged
160 * word to tell that this event is safely in syslog.
162 int nvram_clear_error_log(void)
165 int clear_word = ERR_FLAG_ALREADY_LOGGED;
168 if (rtas_log_partition.index == -1)
171 tmp_index = rtas_log_partition.index;
173 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
175 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
178 last_unread_rtas_event = 0;
184 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
185 * would logging this oops/panic overwrite an RTAS event that rtas_errd
186 * hasn't had a chance to read and process? Return 1 if so, else 0.
188 * We assume that if rtas_errd hasn't read the RTAS event in
189 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
191 int clobbering_unread_rtas_event(void)
193 return (oops_log_partition.index == rtas_log_partition.index
194 && last_unread_rtas_event
195 && ktime_get_real_seconds() - last_unread_rtas_event <=
196 NVRAM_RTAS_READ_TIMEOUT);
199 static int __init pseries_nvram_init_log_partitions(void)
203 /* Scan nvram for partitions */
204 nvram_scan_partitions();
206 rc = nvram_init_os_partition(&rtas_log_partition);
207 nvram_init_oops_partition(rc == 0);
210 machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
212 int __init pSeries_nvram_init(void)
214 struct device_node *nvram;
215 const __be32 *nbytes_p;
216 unsigned int proplen;
218 nvram = of_find_node_by_type(NULL, "nvram");
222 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
223 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
228 nvram_size = be32_to_cpup(nbytes_p);
230 nvram_fetch = rtas_function_token(RTAS_FN_NVRAM_FETCH);
231 nvram_store = rtas_function_token(RTAS_FN_NVRAM_STORE);
232 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
235 ppc_md.nvram_read = pSeries_nvram_read;
236 ppc_md.nvram_write = pSeries_nvram_write;
237 ppc_md.nvram_size = pSeries_nvram_get_size;