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>
18 #include <linux/spinlock.h>
19 #include <linux/slab.h>
20 #include <linux/kmsg_dump.h>
21 #include <linux/pstore.h>
22 #include <linux/ctype.h>
23 #include <asm/uaccess.h>
24 #include <asm/nvram.h>
25 #include <asm/rtas.h>
26 #include <asm/prom.h>
27 #include <asm/machdep.h>
28
29 /* Max bytes to read/write in one go */
30 #define NVRW_CNT 0x20
31
32 static unsigned int nvram_size;
33 static int nvram_fetch, nvram_store;
34 static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
35 static DEFINE_SPINLOCK(nvram_lock);
36
37 /* See clobbering_unread_rtas_event() */
38 #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
39 static time64_t last_unread_rtas_event; /* timestamp */
40
41 #ifdef CONFIG_PSTORE
42 time64_t last_rtas_event;
43 #endif
44
pSeries_nvram_read(char * buf,size_t count,loff_t * index)45 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
46 {
47 unsigned int i;
48 unsigned long len;
49 int done;
50 unsigned long flags;
51 char *p = buf;
52
53
54 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
55 return -ENODEV;
56
57 if (*index >= nvram_size)
58 return 0;
59
60 i = *index;
61 if (i + count > nvram_size)
62 count = nvram_size - i;
63
64 spin_lock_irqsave(&nvram_lock, flags);
65
66 for (; count != 0; count -= len) {
67 len = count;
68 if (len > NVRW_CNT)
69 len = NVRW_CNT;
70
71 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
72 len) != 0) || len != done) {
73 spin_unlock_irqrestore(&nvram_lock, flags);
74 return -EIO;
75 }
76
77 memcpy(p, nvram_buf, len);
78
79 p += len;
80 i += len;
81 }
82
83 spin_unlock_irqrestore(&nvram_lock, flags);
84
85 *index = i;
86 return p - buf;
87 }
88
pSeries_nvram_write(char * buf,size_t count,loff_t * index)89 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
90 {
91 unsigned int i;
92 unsigned long len;
93 int done;
94 unsigned long flags;
95 const char *p = buf;
96
97 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
98 return -ENODEV;
99
100 if (*index >= nvram_size)
101 return 0;
102
103 i = *index;
104 if (i + count > nvram_size)
105 count = nvram_size - i;
106
107 spin_lock_irqsave(&nvram_lock, flags);
108
109 for (; count != 0; count -= len) {
110 len = count;
111 if (len > NVRW_CNT)
112 len = NVRW_CNT;
113
114 memcpy(nvram_buf, p, len);
115
116 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
117 len) != 0) || len != done) {
118 spin_unlock_irqrestore(&nvram_lock, flags);
119 return -EIO;
120 }
121
122 p += len;
123 i += len;
124 }
125 spin_unlock_irqrestore(&nvram_lock, flags);
126
127 *index = i;
128 return p - buf;
129 }
130
pSeries_nvram_get_size(void)131 static ssize_t pSeries_nvram_get_size(void)
132 {
133 return nvram_size ? nvram_size : -ENODEV;
134 }
135
136 /* nvram_write_error_log
137 *
138 * We need to buffer the error logs into nvram to ensure that we have
139 * the failure information to decode.
140 */
nvram_write_error_log(char * buff,int length,unsigned int err_type,unsigned int error_log_cnt)141 int nvram_write_error_log(char * buff, int length,
142 unsigned int err_type, unsigned int error_log_cnt)
143 {
144 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
145 err_type, error_log_cnt);
146 if (!rc) {
147 last_unread_rtas_event = ktime_get_real_seconds();
148 #ifdef CONFIG_PSTORE
149 last_rtas_event = ktime_get_real_seconds();
150 #endif
151 }
152
153 return rc;
154 }
155
156 /* nvram_read_error_log
157 *
158 * Reads nvram for error log for at most 'length'
159 */
nvram_read_error_log(char * buff,int length,unsigned int * err_type,unsigned int * error_log_cnt)160 int nvram_read_error_log(char *buff, int length,
161 unsigned int *err_type, unsigned int *error_log_cnt)
162 {
163 return nvram_read_partition(&rtas_log_partition, buff, length,
164 err_type, error_log_cnt);
165 }
166
167 /* This doesn't actually zero anything, but it sets the event_logged
168 * word to tell that this event is safely in syslog.
169 */
nvram_clear_error_log(void)170 int nvram_clear_error_log(void)
171 {
172 loff_t tmp_index;
173 int clear_word = ERR_FLAG_ALREADY_LOGGED;
174 int rc;
175
176 if (rtas_log_partition.index == -1)
177 return -1;
178
179 tmp_index = rtas_log_partition.index;
180
181 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
182 if (rc <= 0) {
183 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
184 return rc;
185 }
186 last_unread_rtas_event = 0;
187
188 return 0;
189 }
190
191 /*
192 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
193 * would logging this oops/panic overwrite an RTAS event that rtas_errd
194 * hasn't had a chance to read and process? Return 1 if so, else 0.
195 *
196 * We assume that if rtas_errd hasn't read the RTAS event in
197 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
198 */
clobbering_unread_rtas_event(void)199 int clobbering_unread_rtas_event(void)
200 {
201 return (oops_log_partition.index == rtas_log_partition.index
202 && last_unread_rtas_event
203 && ktime_get_real_seconds() - last_unread_rtas_event <=
204 NVRAM_RTAS_READ_TIMEOUT);
205 }
206
pseries_nvram_init_log_partitions(void)207 static int __init pseries_nvram_init_log_partitions(void)
208 {
209 int rc;
210
211 /* Scan nvram for partitions */
212 nvram_scan_partitions();
213
214 rc = nvram_init_os_partition(&rtas_log_partition);
215 nvram_init_oops_partition(rc == 0);
216 return 0;
217 }
218 machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
219
pSeries_nvram_init(void)220 int __init pSeries_nvram_init(void)
221 {
222 struct device_node *nvram;
223 const __be32 *nbytes_p;
224 unsigned int proplen;
225
226 nvram = of_find_node_by_type(NULL, "nvram");
227 if (nvram == NULL)
228 return -ENODEV;
229
230 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
231 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
232 of_node_put(nvram);
233 return -EIO;
234 }
235
236 nvram_size = be32_to_cpup(nbytes_p);
237
238 nvram_fetch = rtas_token("nvram-fetch");
239 nvram_store = rtas_token("nvram-store");
240 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
241 of_node_put(nvram);
242
243 ppc_md.nvram_read = pSeries_nvram_read;
244 ppc_md.nvram_write = pSeries_nvram_write;
245 ppc_md.nvram_size = pSeries_nvram_get_size;
246
247 return 0;
248 }
249
250