1 /*
2  *
3  * Procedures for interfacing to the RTAS on CHRP machines.
4  *
5  * Peter Bergner, IBM	March 2001.
6  * Copyright (C) 2001 IBM.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13 
14 #include <stdarg.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/fs.h>
19 #include <linux/slab.h>
20 
21 #include <asm/init.h>
22 #include <asm/prom.h>
23 #include <asm/rtas.h>
24 #include <asm/semaphore.h>
25 #include <asm/machdep.h>
26 #include <asm/paca.h>
27 #include <asm/page.h>
28 #include <asm/param.h>
29 #include <asm/system.h>
30 #include <asm/abs_addr.h>
31 #include <asm/udbg.h>
32 #include <asm/uaccess.h>
33 
34 struct proc_dir_entry *rtas_proc_dir;	/* /proc/ppc64/rtas dir */
35 struct flash_block_list_header rtas_firmware_flash_list = {0, 0};
36 struct errinjct_token ei_token_list[MAX_ERRINJCT_TOKENS];
37 
38 /*
39  * prom_init() is called very early on, before the kernel text
40  * and data have been mapped to KERNELBASE.  At this point the code
41  * is running at whatever address it has been loaded at, so
42  * references to extern and static variables must be relocated
43  * explicitly.  The procedure reloc_offset() returns the address
44  * we're currently running at minus the address we were linked at.
45  * (Note that strings count as static variables.)
46  *
47  * Because OF may have mapped I/O devices into the area starting at
48  * KERNELBASE, particularly on CHRP machines, we can't safely call
49  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
50  * OF calls should be done within prom_init(), and prom_init()
51  * and all routines called within it must be careful to relocate
52  * references as necessary.
53  *
54  * Note that the bss is cleared *after* prom_init runs, so we have
55  * to make sure that any static or extern variables it accesses
56  * are put in the data segment.
57  */
58 
59 struct rtas_t rtas = {
60 	.lock = SPIN_LOCK_UNLOCKED
61 };
62 
63 extern unsigned long reloc_offset(void);
64 
65 spinlock_t rtas_data_buf_lock = SPIN_LOCK_UNLOCKED;
66 char rtas_data_buf[RTAS_DATA_BUF_SIZE]__page_aligned;
67 
68 void
phys_call_rtas(int token,int nargs,int nret,...)69 phys_call_rtas(int token, int nargs, int nret, ...)
70 {
71 	va_list list;
72 	unsigned long offset = reloc_offset();
73 	struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
74 	int i;
75 
76 	rtas->token = token;
77 	rtas->nargs = nargs;
78 	rtas->nret  = nret;
79 	rtas->rets  = (rtas_arg_t *)PTRRELOC(&(rtas->args[nargs]));
80 
81 	va_start(list, nret);
82 	for (i = 0; i < nargs; i++)
83 	  rtas->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
84 	va_end(list);
85 
86         enter_rtas(rtas);
87 }
88 
89 void
phys_call_rtas_display_status(char c)90 phys_call_rtas_display_status(char c)
91 {
92 	unsigned long offset = reloc_offset();
93 	struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
94 
95 	rtas->token = 10;
96 	rtas->nargs = 1;
97 	rtas->nret  = 1;
98 	rtas->rets  = (rtas_arg_t *)PTRRELOC(&(rtas->args[1]));
99 	rtas->args[0] = (int)c;
100 
101 	enter_rtas(rtas);
102 }
103 
104 void
call_rtas_display_status(char c)105 call_rtas_display_status(char c)
106 {
107 	struct rtas_args *rtas = &(get_paca()->xRtas);
108 
109 	rtas->token = 10;
110 	rtas->nargs = 1;
111 	rtas->nret  = 1;
112 	rtas->rets  = (rtas_arg_t *)&(rtas->args[1]);
113 	rtas->args[0] = (int)c;
114 
115 	enter_rtas((void *)__pa((unsigned long)rtas));
116 }
117 
118 __openfirmware
119 int
rtas_token(const char * service)120 rtas_token(const char *service)
121 {
122 	int *tokp;
123 	if (rtas.dev == NULL) {
124 		PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
125 		return RTAS_UNKNOWN_SERVICE;
126 	}
127 	tokp = (int *) get_property(rtas.dev, service, NULL);
128 	return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
129 }
130 
131 __openfirmware
132 long
rtas_call(int token,int nargs,int nret,unsigned long * outputs,...)133 rtas_call(int token, int nargs, int nret,
134 	  unsigned long *outputs, ...)
135 {
136 	va_list list;
137 	int i;
138 	unsigned long s;
139 	struct rtas_args *rtas_args = &(get_paca()->xRtas);
140 
141 	PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
142 	PPCDBG(PPCDBG_RTAS, "\ttoken    = 0x%x\n", token);
143 	PPCDBG(PPCDBG_RTAS, "\tnargs    = %d\n", nargs);
144 	PPCDBG(PPCDBG_RTAS, "\tnret     = %d\n", nret);
145 	PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
146 	if (token == RTAS_UNKNOWN_SERVICE)
147 		return -1;
148 
149 	rtas_args->token = token;
150 	rtas_args->nargs = nargs;
151 	rtas_args->nret  = nret;
152 	rtas_args->rets  = (rtas_arg_t *)&(rtas_args->args[nargs]);
153 	va_start(list, outputs);
154 	for (i = 0; i < nargs; ++i) {
155 		rtas_args->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
156 		PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%lx\n", i, rtas_args->args[i]);
157 	}
158 	va_end(list);
159 
160 	for (i = 0; i < nret; ++i)
161 	  rtas_args->rets[i] = 0;
162 
163 #if 0   /* Gotta do something different here, use global lock for now... */
164 	spin_lock_irqsave(&rtas_args->lock, s);
165 #else
166 	spin_lock_irqsave(&rtas.lock, s);
167 #endif
168 	PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
169 		(void *)__pa((unsigned long)rtas_args));
170 	enter_rtas((void *)__pa((unsigned long)rtas_args));
171 	PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
172 #if 0   /* Gotta do something different here, use global lock for now... */
173 	spin_unlock_irqrestore(&rtas_args->lock, s);
174 #else
175 	spin_unlock_irqrestore(&rtas.lock, s);
176 #endif
177 	ifppcdebug(PPCDBG_RTAS) {
178 		for(i=0; i < nret ;i++)
179 			udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
180 	}
181 
182 	if (nret > 1 && outputs != NULL)
183 		for (i = 0; i < nret-1; ++i)
184 			outputs[i] = rtas_args->rets[i+1];
185 	return (ulong)((nret > 0) ? rtas_args->rets[0] : 0);
186 }
187 
188 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
189  * (last digit) milliseconds.  For now we bound at n=3 (1 sec).
190  */
191 unsigned int
rtas_extended_busy_delay_time(int status)192 rtas_extended_busy_delay_time(int status)
193 {
194 	int order = status - 9900;
195 	unsigned int ms;
196 
197 	if (order < 0)
198 		order = 0;	/* RTC depends on this for -2 clock busy */
199 	else if (order > 3)
200 		order = 3;	/* bound */
201 
202 	/* Use microseconds for reasonable accuracy */
203 	for (ms = 1000; order > 0; order--)
204 		ms = ms * 10;
205 	return ms / (1000000/HZ); /* round down is fine */
206 }
207 
208 #define FLASH_BLOCK_LIST_VERSION (1UL)
209 static void
rtas_flash_firmware(void)210 rtas_flash_firmware(void)
211 {
212 	unsigned long image_size;
213 	struct flash_block_list *f, *next, *flist;
214 	unsigned long rtas_block_list;
215 	int i, status, update_token;
216 
217 	update_token = rtas_token("ibm,update-flash-64-and-reboot");
218 	if (update_token == RTAS_UNKNOWN_SERVICE) {
219 		printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
220 		printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
221 		return;
222 	}
223 
224 	/* NOTE: the "first" block list is a global var with no data
225 	 * blocks in the kernel data segment.  We do this because
226 	 * we want to ensure this block_list addr is under 4GB.
227 	 */
228 	rtas_firmware_flash_list.num_blocks = 0;
229 	flist = (struct flash_block_list *)&rtas_firmware_flash_list;
230 	rtas_block_list = virt_to_absolute((unsigned long)flist);
231 
232 	if (rtas_block_list >= 4UL*1024*1024*1024) {
233 		printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
234 		return;
235 	}
236 
237 	printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
238 	/* Update the block_list in place. */
239 	image_size = 0;
240 	for (f = flist; f; f = next) {
241 		/* Translate data addrs to absolute */
242 		for (i = 0; i < f->num_blocks; i++) {
243 			f->blocks[i].data = (char *)virt_to_absolute((unsigned long)f->blocks[i].data);
244 			image_size += f->blocks[i].length;
245 		}
246 		next = f->next;
247 		/* Don't translate final NULL pointer */
248 		if(f->next)
249 			f->next = (struct flash_block_list *)virt_to_absolute((unsigned long)f->next);
250 		else
251 			f->next = 0LL;
252 		/* make num_blocks into the version/length field */
253 		f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
254 	}
255 
256 	printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
257 	printk(KERN_ALERT "FLASH: performing flash and reboot\n");
258 	ppc_md.progress("Flashing        \n", 0x0);
259 	ppc_md.progress("Please Wait...  ", 0x0);
260 	printk(KERN_ALERT "FLASH: this will take several minutes.  Do not power off!\n");
261 	status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
262 	switch (status) {	/* should only get "bad" status */
263 	    case 0:
264 		printk(KERN_ALERT "FLASH: success\n");
265 		break;
266 	    case -1:
267 		printk(KERN_ALERT "FLASH: hardware error.  Firmware may not be not flashed\n");
268 		break;
269 	    case -3:
270 		printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform.  Firmware not flashed\n");
271 		break;
272 	    case -4:
273 		printk(KERN_ALERT "FLASH: flash failed when partially complete.  System may not reboot\n");
274 		break;
275 	    default:
276 		printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
277 		break;
278 	}
279 }
280 
rtas_flash_bypass_warning(void)281 void rtas_flash_bypass_warning(void)
282 {
283 	printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
284 	printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
285 }
286 
287 
288 void __chrp
rtas_restart(char * cmd)289 rtas_restart(char *cmd)
290 {
291 	if (rtas_firmware_flash_list.next)
292 		rtas_flash_firmware();
293 
294         printk("RTAS system-reboot returned %ld\n",
295 	       rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
296         for (;;);
297 }
298 
299 void __chrp
rtas_power_off(void)300 rtas_power_off(void)
301 {
302 	if (rtas_firmware_flash_list.next)
303 		rtas_flash_bypass_warning();
304         /* allow power on only with power button press */
305         printk("RTAS power-off returned %ld\n",
306                rtas_call(rtas_token("power-off"), 2, 1, NULL,0xffffffff,0xffffffff));
307         for (;;);
308 }
309 
310 void __chrp
rtas_halt(void)311 rtas_halt(void)
312 {
313 	if (rtas_firmware_flash_list.next)
314 		rtas_flash_bypass_warning();
315         rtas_power_off();
316 }
317 
318 int
rtas_errinjct_open(void)319 rtas_errinjct_open(void)
320 {
321 	u32 ret[2];
322 	int open_token;
323 	int rc;
324 
325 	/* The rc and open_token values are backwards due to a misprint in
326 	 * the RPA */
327 	open_token = rtas_call(rtas_token("ibm,open-errinjct"), 0, 2, (void *) &ret);
328 	rc = ret[0];
329 
330 	if (rc < 0) {
331 		printk(KERN_WARNING "error: ibm,open-errinjct failed (%d)\n", rc);
332 		return rc;
333 	}
334 
335 	return open_token;
336 }
337 
338 int
rtas_errinjct(unsigned int open_token,char * ei_token,char * in_workspace)339 rtas_errinjct(unsigned int open_token, char * ei_token, char * in_workspace)
340 {
341 	struct errinjct_token * ei;
342 	int rtas_ei_token = -1;
343 	int rc;
344 	int i;
345 
346 	ei = ei_token_list;
347 	for (i = 0; i < MAX_ERRINJCT_TOKENS && ei->name; i++) {
348 		if (strcmp(ei_token, ei->name) == 0) {
349 			rtas_ei_token = ei->value;
350 			break;
351 		}
352 		ei++;
353 	}
354 	if (rtas_ei_token == -1) {
355 		return -EINVAL;
356 	}
357 
358 	spin_lock(&rtas_data_buf_lock);
359 
360 	if (in_workspace)
361 		memcpy(rtas_data_buf, in_workspace, RTAS_DATA_BUF_SIZE);
362 
363 	rc = rtas_call(rtas_token("ibm,errinjct"), 3, 1, NULL, rtas_ei_token,
364 		       open_token, __pa(rtas_data_buf));
365 
366 	spin_unlock(&rtas_data_buf_lock);
367 
368 	return rc;
369 }
370 
371 int
rtas_errinjct_close(unsigned int open_token)372 rtas_errinjct_close(unsigned int open_token)
373 {
374 	int rc;
375 
376 	rc = rtas_call(rtas_token("ibm,close-errinjct"), 1, 1, NULL, open_token);
377 	if (rc != 0) {
378 		printk(KERN_WARNING "error: ibm,close-errinjct failed (%d)\n", rc);
379 		return rc;
380 	}
381 
382 	return 0;
383 }
384 
385 #ifndef CONFIG_PPC_ISERIES
rtas_errinjct_init(void)386 static int __init rtas_errinjct_init(void)
387 {
388 	char * token_array;
389 	char * end_array;
390 	int array_len = 0;
391 	int len;
392 	int i, j;
393 
394 	token_array = (char *) get_property(rtas.dev, "ibm,errinjct-tokens",
395 					    &array_len);
396 	/* if token is not found, then we fall through loop */
397 	end_array = token_array + array_len;
398 	for (i = 0, j = 0; i < MAX_ERRINJCT_TOKENS && token_array < end_array; i++) {
399 
400 		len = strnlen(token_array, ERRINJCT_TOKEN_LEN) + 1;
401 		ei_token_list[i].name = (char *) kmalloc(len, GFP_KERNEL);
402 		if (!ei_token_list[i].name) {
403 			printk(KERN_WARNING "error: kmalloc failed\n");
404 			return -ENOMEM;
405 		}
406 
407 		strcpy(ei_token_list[i].name, token_array);
408 		token_array += len;
409 
410 		ei_token_list[i].value = *(int *)token_array;
411 		token_array += sizeof(int);
412 	}
413 	for (; i < MAX_ERRINJCT_TOKENS; i++) {
414 		ei_token_list[i].name = 0;
415 		ei_token_list[i].value = 0;
416 	}
417 	return 0;
418 }
419 
420 __initcall(rtas_errinjct_init);
421 #endif
422