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