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