[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / arch / ppc64 / kernel / rtas.c
blob5575603def277d5ff3360b4c2c14115cb0c7b89c
1 /*
3 * Procedures for interfacing to the RTAS on CHRP machines.
5 * Peter Bergner, IBM March 2001.
6 * Copyright (C) 2001 IBM.
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.
14 #include <stdarg.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
21 #include <asm/prom.h>
22 #include <asm/rtas.h>
23 #include <asm/semaphore.h>
24 #include <asm/machdep.h>
25 #include <asm/page.h>
26 #include <asm/param.h>
27 #include <asm/system.h>
28 #include <asm/abs_addr.h>
29 #include <asm/udbg.h>
30 #include <asm/delay.h>
31 #include <asm/uaccess.h>
32 #include <asm/systemcfg.h>
34 struct flash_block_list_header rtas_firmware_flash_list = {0, NULL};
36 struct rtas_t rtas = {
37 .lock = SPIN_LOCK_UNLOCKED
40 EXPORT_SYMBOL(rtas);
42 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
44 DEFINE_SPINLOCK(rtas_data_buf_lock);
45 char rtas_data_buf[RTAS_DATA_BUF_SIZE]__page_aligned;
46 unsigned long rtas_rmo_buf;
48 void
49 call_rtas_display_status(unsigned char c)
51 struct rtas_args *args = &rtas.args;
52 unsigned long s;
54 if (!rtas.base)
55 return;
56 spin_lock_irqsave(&rtas.lock, s);
58 args->token = 10;
59 args->nargs = 1;
60 args->nret = 1;
61 args->rets = (rtas_arg_t *)&(args->args[1]);
62 args->args[0] = (int)c;
64 enter_rtas(__pa(args));
66 spin_unlock_irqrestore(&rtas.lock, s);
69 void
70 call_rtas_display_status_delay(unsigned char c)
72 static int pending_newline = 0; /* did last write end with unprinted newline? */
73 static int width = 16;
75 if (c == '\n') {
76 while (width-- > 0)
77 call_rtas_display_status(' ');
78 width = 16;
79 udelay(500000);
80 pending_newline = 1;
81 } else {
82 if (pending_newline) {
83 call_rtas_display_status('\r');
84 call_rtas_display_status('\n');
86 pending_newline = 0;
87 if (width--) {
88 call_rtas_display_status(c);
89 udelay(10000);
94 int
95 rtas_token(const char *service)
97 int *tokp;
98 if (rtas.dev == NULL) {
99 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
100 return RTAS_UNKNOWN_SERVICE;
102 tokp = (int *) get_property(rtas.dev, service, NULL);
103 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
107 * Return the firmware-specified size of the error log buffer
108 * for all rtas calls that require an error buffer argument.
109 * This includes 'check-exception' and 'rtas-last-error'.
111 int rtas_get_error_log_max(void)
113 static int rtas_error_log_max;
114 if (rtas_error_log_max)
115 return rtas_error_log_max;
117 rtas_error_log_max = rtas_token ("rtas-error-log-max");
118 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
119 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
120 printk (KERN_WARNING "RTAS: bad log buffer size %d\n", rtas_error_log_max);
121 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
123 return rtas_error_log_max;
127 /** Return a copy of the detailed error text associated with the
128 * most recent failed call to rtas. Because the error text
129 * might go stale if there are any other intervening rtas calls,
130 * this routine must be called atomically with whatever produced
131 * the error (i.e. with rtas.lock still held from the previous call).
133 static int
134 __fetch_rtas_last_error(void)
136 struct rtas_args err_args, save_args;
137 u32 bufsz;
139 bufsz = rtas_get_error_log_max();
141 err_args.token = rtas_token("rtas-last-error");
142 err_args.nargs = 2;
143 err_args.nret = 1;
145 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
146 err_args.args[1] = bufsz;
147 err_args.args[2] = 0;
149 save_args = rtas.args;
150 rtas.args = err_args;
152 enter_rtas(__pa(&rtas.args));
154 err_args = rtas.args;
155 rtas.args = save_args;
157 return err_args.args[2];
160 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
162 va_list list;
163 int i, logit = 0;
164 unsigned long s;
165 struct rtas_args *rtas_args;
166 char * buff_copy = NULL;
167 int ret;
169 PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
170 PPCDBG(PPCDBG_RTAS, "\ttoken = 0x%x\n", token);
171 PPCDBG(PPCDBG_RTAS, "\tnargs = %d\n", nargs);
172 PPCDBG(PPCDBG_RTAS, "\tnret = %d\n", nret);
173 PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
174 if (token == RTAS_UNKNOWN_SERVICE)
175 return -1;
177 /* Gotta do something different here, use global lock for now... */
178 spin_lock_irqsave(&rtas.lock, s);
179 rtas_args = &rtas.args;
181 rtas_args->token = token;
182 rtas_args->nargs = nargs;
183 rtas_args->nret = nret;
184 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
185 va_start(list, outputs);
186 for (i = 0; i < nargs; ++i) {
187 rtas_args->args[i] = va_arg(list, rtas_arg_t);
188 PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%x\n", i, rtas_args->args[i]);
190 va_end(list);
192 for (i = 0; i < nret; ++i)
193 rtas_args->rets[i] = 0;
195 PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
196 __pa(rtas_args));
197 enter_rtas(__pa(rtas_args));
198 PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
200 /* A -1 return code indicates that the last command couldn't
201 be completed due to a hardware error. */
202 if (rtas_args->rets[0] == -1)
203 logit = (__fetch_rtas_last_error() == 0);
205 ifppcdebug(PPCDBG_RTAS) {
206 for(i=0; i < nret ;i++)
207 udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
210 if (nret > 1 && outputs != NULL)
211 for (i = 0; i < nret-1; ++i)
212 outputs[i] = rtas_args->rets[i+1];
213 ret = (nret > 0)? rtas_args->rets[0]: 0;
215 /* Log the error in the unlikely case that there was one. */
216 if (unlikely(logit)) {
217 buff_copy = rtas_err_buf;
218 if (mem_init_done) {
219 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
220 if (buff_copy)
221 memcpy(buff_copy, rtas_err_buf,
222 RTAS_ERROR_LOG_MAX);
226 /* Gotta do something different here, use global lock for now... */
227 spin_unlock_irqrestore(&rtas.lock, s);
229 if (buff_copy) {
230 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
231 if (mem_init_done)
232 kfree(buff_copy);
234 return ret;
237 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
238 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
240 unsigned int
241 rtas_extended_busy_delay_time(int status)
243 int order = status - 9900;
244 unsigned long ms;
246 if (order < 0)
247 order = 0; /* RTC depends on this for -2 clock busy */
248 else if (order > 5)
249 order = 5; /* bound */
251 /* Use microseconds for reasonable accuracy */
252 for (ms=1; order > 0; order--)
253 ms *= 10;
255 return ms;
258 int rtas_error_rc(int rtas_rc)
260 int rc;
262 switch (rtas_rc) {
263 case -1: /* Hardware Error */
264 rc = -EIO;
265 break;
266 case -3: /* Bad indicator/domain/etc */
267 rc = -EINVAL;
268 break;
269 case -9000: /* Isolation error */
270 rc = -EFAULT;
271 break;
272 case -9001: /* Outstanding TCE/PTE */
273 rc = -EEXIST;
274 break;
275 case -9002: /* No usable slot */
276 rc = -ENODEV;
277 break;
278 default:
279 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
280 __FUNCTION__, rtas_rc);
281 rc = -ERANGE;
282 break;
284 return rc;
287 int rtas_get_power_level(int powerdomain, int *level)
289 int token = rtas_token("get-power-level");
290 int rc;
292 if (token == RTAS_UNKNOWN_SERVICE)
293 return -ENOENT;
295 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
296 udelay(1);
298 if (rc < 0)
299 return rtas_error_rc(rc);
300 return rc;
303 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
305 int token = rtas_token("set-power-level");
306 unsigned int wait_time;
307 int rc;
309 if (token == RTAS_UNKNOWN_SERVICE)
310 return -ENOENT;
312 while (1) {
313 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
314 if (rc == RTAS_BUSY)
315 udelay(1);
316 else if (rtas_is_extended_busy(rc)) {
317 wait_time = rtas_extended_busy_delay_time(rc);
318 udelay(wait_time * 1000);
319 } else
320 break;
323 if (rc < 0)
324 return rtas_error_rc(rc);
325 return rc;
328 int rtas_get_sensor(int sensor, int index, int *state)
330 int token = rtas_token("get-sensor-state");
331 unsigned int wait_time;
332 int rc;
334 if (token == RTAS_UNKNOWN_SERVICE)
335 return -ENOENT;
337 while (1) {
338 rc = rtas_call(token, 2, 2, state, sensor, index);
339 if (rc == RTAS_BUSY)
340 udelay(1);
341 else if (rtas_is_extended_busy(rc)) {
342 wait_time = rtas_extended_busy_delay_time(rc);
343 udelay(wait_time * 1000);
344 } else
345 break;
348 if (rc < 0)
349 return rtas_error_rc(rc);
350 return rc;
353 int rtas_set_indicator(int indicator, int index, int new_value)
355 int token = rtas_token("set-indicator");
356 unsigned int wait_time;
357 int rc;
359 if (token == RTAS_UNKNOWN_SERVICE)
360 return -ENOENT;
362 while (1) {
363 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
364 if (rc == RTAS_BUSY)
365 udelay(1);
366 else if (rtas_is_extended_busy(rc)) {
367 wait_time = rtas_extended_busy_delay_time(rc);
368 udelay(wait_time * 1000);
370 else
371 break;
374 if (rc < 0)
375 return rtas_error_rc(rc);
376 return rc;
379 #define FLASH_BLOCK_LIST_VERSION (1UL)
380 static void
381 rtas_flash_firmware(void)
383 unsigned long image_size;
384 struct flash_block_list *f, *next, *flist;
385 unsigned long rtas_block_list;
386 int i, status, update_token;
388 update_token = rtas_token("ibm,update-flash-64-and-reboot");
389 if (update_token == RTAS_UNKNOWN_SERVICE) {
390 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
391 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
392 return;
395 /* NOTE: the "first" block list is a global var with no data
396 * blocks in the kernel data segment. We do this because
397 * we want to ensure this block_list addr is under 4GB.
399 rtas_firmware_flash_list.num_blocks = 0;
400 flist = (struct flash_block_list *)&rtas_firmware_flash_list;
401 rtas_block_list = virt_to_abs(flist);
402 if (rtas_block_list >= 4UL*1024*1024*1024) {
403 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
404 return;
407 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
408 /* Update the block_list in place. */
409 image_size = 0;
410 for (f = flist; f; f = next) {
411 /* Translate data addrs to absolute */
412 for (i = 0; i < f->num_blocks; i++) {
413 f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
414 image_size += f->blocks[i].length;
416 next = f->next;
417 /* Don't translate NULL pointer for last entry */
418 if (f->next)
419 f->next = (struct flash_block_list *)virt_to_abs(f->next);
420 else
421 f->next = NULL;
422 /* make num_blocks into the version/length field */
423 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
426 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
427 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
428 ppc_md.progress("Flashing \n", 0x0);
429 ppc_md.progress("Please Wait... ", 0x0);
430 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
431 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
432 switch (status) { /* should only get "bad" status */
433 case 0:
434 printk(KERN_ALERT "FLASH: success\n");
435 break;
436 case -1:
437 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
438 break;
439 case -3:
440 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
441 break;
442 case -4:
443 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
444 break;
445 default:
446 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
447 break;
451 void rtas_flash_bypass_warning(void)
453 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
454 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
458 void
459 rtas_restart(char *cmd)
461 if (rtas_firmware_flash_list.next)
462 rtas_flash_firmware();
464 printk("RTAS system-reboot returned %d\n",
465 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
466 for (;;);
469 void
470 rtas_power_off(void)
472 if (rtas_firmware_flash_list.next)
473 rtas_flash_bypass_warning();
474 /* allow power on only with power button press */
475 printk("RTAS power-off returned %d\n",
476 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
477 for (;;);
480 void
481 rtas_halt(void)
483 if (rtas_firmware_flash_list.next)
484 rtas_flash_bypass_warning();
485 rtas_power_off();
488 /* Must be in the RMO region, so we place it here */
489 static char rtas_os_term_buf[2048];
491 void rtas_os_term(char *str)
493 int status;
495 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
496 return;
498 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
500 do {
501 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
502 __pa(rtas_os_term_buf));
504 if (status == RTAS_BUSY)
505 udelay(1);
506 else if (status != 0)
507 printk(KERN_EMERG "ibm,os-term call failed %d\n",
508 status);
509 } while (status == RTAS_BUSY);
513 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
515 struct rtas_args args;
516 unsigned long flags;
517 char * buff_copy;
518 int nargs;
519 int err_rc = 0;
521 if (!capable(CAP_SYS_ADMIN))
522 return -EPERM;
524 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
525 return -EFAULT;
527 nargs = args.nargs;
528 if (nargs > ARRAY_SIZE(args.args)
529 || args.nret > ARRAY_SIZE(args.args)
530 || nargs + args.nret > ARRAY_SIZE(args.args))
531 return -EINVAL;
533 /* Copy in args. */
534 if (copy_from_user(args.args, uargs->args,
535 nargs * sizeof(rtas_arg_t)) != 0)
536 return -EFAULT;
538 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL);
540 spin_lock_irqsave(&rtas.lock, flags);
542 rtas.args = args;
543 enter_rtas(__pa(&rtas.args));
544 args = rtas.args;
546 args.rets = &args.args[nargs];
548 /* A -1 return code indicates that the last command couldn't
549 be completed due to a hardware error. */
550 if (args.rets[0] == -1) {
551 err_rc = __fetch_rtas_last_error();
552 if ((err_rc == 0) && buff_copy) {
553 memcpy(buff_copy, rtas_err_buf, RTAS_ERROR_LOG_MAX);
557 spin_unlock_irqrestore(&rtas.lock, flags);
559 if (buff_copy) {
560 if ((args.rets[0] == -1) && (err_rc == 0)) {
561 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
563 kfree(buff_copy);
566 /* Copy out args. */
567 if (copy_to_user(uargs->args + nargs,
568 args.args + nargs,
569 args.nret * sizeof(rtas_arg_t)) != 0)
570 return -EFAULT;
572 return 0;
575 /* This version can't take the spinlock, because it never returns */
577 struct rtas_args rtas_stop_self_args = {
578 /* The token is initialized for real in setup_system() */
579 .token = RTAS_UNKNOWN_SERVICE,
580 .nargs = 0,
581 .nret = 1,
582 .rets = &rtas_stop_self_args.args[0],
585 void rtas_stop_self(void)
587 struct rtas_args *rtas_args = &rtas_stop_self_args;
589 local_irq_disable();
591 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
593 printk("cpu %u (hwid %u) Ready to die...\n",
594 smp_processor_id(), hard_smp_processor_id());
595 enter_rtas(__pa(rtas_args));
597 panic("Alas, I survived.\n");
601 * Call early during boot, before mem init or bootmem, to retreive the RTAS
602 * informations from the device-tree and allocate the RMO buffer for userland
603 * accesses.
605 void __init rtas_initialize(void)
607 /* Get RTAS dev node and fill up our "rtas" structure with infos
608 * about it.
610 rtas.dev = of_find_node_by_name(NULL, "rtas");
611 if (rtas.dev) {
612 u32 *basep, *entryp;
613 u32 *sizep;
615 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
616 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
617 if (basep != NULL && sizep != NULL) {
618 rtas.base = *basep;
619 rtas.size = *sizep;
620 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
621 if (entryp == NULL) /* Ugh */
622 rtas.entry = rtas.base;
623 else
624 rtas.entry = *entryp;
625 } else
626 rtas.dev = NULL;
628 /* If RTAS was found, allocate the RMO buffer for it and look for
629 * the stop-self token if any
631 if (rtas.dev) {
632 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
633 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
634 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
636 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
637 rtas_region);
639 #ifdef CONFIG_HOTPLUG_CPU
640 rtas_stop_self_args.token = rtas_token("stop-self");
641 #endif /* CONFIG_HOTPLUG_CPU */
647 EXPORT_SYMBOL(rtas_firmware_flash_list);
648 EXPORT_SYMBOL(rtas_token);
649 EXPORT_SYMBOL(rtas_call);
650 EXPORT_SYMBOL(rtas_data_buf);
651 EXPORT_SYMBOL(rtas_data_buf_lock);
652 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
653 EXPORT_SYMBOL(rtas_get_sensor);
654 EXPORT_SYMBOL(rtas_get_power_level);
655 EXPORT_SYMBOL(rtas_set_power_level);
656 EXPORT_SYMBOL(rtas_set_indicator);
657 EXPORT_SYMBOL(rtas_get_error_log_max);