initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / ppc64 / kernel / rtas.c
blobbecb652e14efb158b7734f4589d268b6bc36929e
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>
33 struct flash_block_list_header rtas_firmware_flash_list = {0, NULL};
35 struct rtas_t rtas = {
36 .lock = SPIN_LOCK_UNLOCKED
39 EXPORT_SYMBOL(rtas);
41 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
43 spinlock_t rtas_data_buf_lock = SPIN_LOCK_UNLOCKED;
44 char rtas_data_buf[RTAS_DATA_BUF_SIZE]__page_aligned;
45 unsigned long rtas_rmo_buf;
47 void
48 call_rtas_display_status(unsigned char c)
50 struct rtas_args *args = &rtas.args;
51 unsigned long s;
53 if (!rtas.base)
54 return;
55 spin_lock_irqsave(&rtas.lock, s);
57 args->token = 10;
58 args->nargs = 1;
59 args->nret = 1;
60 args->rets = (rtas_arg_t *)&(args->args[1]);
61 args->args[0] = (int)c;
63 enter_rtas(__pa(args));
65 spin_unlock_irqrestore(&rtas.lock, s);
68 void
69 call_rtas_display_status_delay(unsigned char c)
71 static int pending_newline = 0; /* did last write end with unprinted newline? */
72 static int width = 16;
74 if (c == '\n') {
75 while (width-- > 0)
76 call_rtas_display_status(' ');
77 width = 16;
78 udelay(500000);
79 pending_newline = 1;
80 } else {
81 if (pending_newline) {
82 call_rtas_display_status('\r');
83 call_rtas_display_status('\n');
85 pending_newline = 0;
86 if (width--) {
87 call_rtas_display_status(c);
88 udelay(10000);
93 int
94 rtas_token(const char *service)
96 int *tokp;
97 if (rtas.dev == NULL) {
98 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
99 return RTAS_UNKNOWN_SERVICE;
101 tokp = (int *) get_property(rtas.dev, service, NULL);
102 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
105 /** Return a copy of the detailed error text associated with the
106 * most recent failed call to rtas. Because the error text
107 * might go stale if there are any other intervening rtas calls,
108 * this routine must be called atomically with whatever produced
109 * the error (i.e. with rtas.lock still held from the previous call).
111 static int
112 __fetch_rtas_last_error(void)
114 struct rtas_args err_args, save_args;
115 u32 bufsz;
117 bufsz = rtas_token ("rtas-error-log-max");
118 if ((bufsz == RTAS_UNKNOWN_SERVICE) ||
119 (bufsz > RTAS_ERROR_LOG_MAX)) {
120 printk (KERN_WARNING "RTAS: bad log buffer size %d\n", bufsz);
121 bufsz = RTAS_ERROR_LOG_MAX;
124 err_args.token = rtas_token("rtas-last-error");
125 err_args.nargs = 2;
126 err_args.nret = 1;
128 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
129 err_args.args[1] = bufsz;
130 err_args.args[2] = 0;
132 save_args = rtas.args;
133 rtas.args = err_args;
135 enter_rtas(__pa(&rtas.args));
137 err_args = rtas.args;
138 rtas.args = save_args;
140 return err_args.args[2];
143 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
145 va_list list;
146 int i, logit = 0;
147 unsigned long s;
148 struct rtas_args *rtas_args;
149 char * buff_copy = NULL;
150 int ret;
152 PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
153 PPCDBG(PPCDBG_RTAS, "\ttoken = 0x%x\n", token);
154 PPCDBG(PPCDBG_RTAS, "\tnargs = %d\n", nargs);
155 PPCDBG(PPCDBG_RTAS, "\tnret = %d\n", nret);
156 PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
157 if (token == RTAS_UNKNOWN_SERVICE)
158 return -1;
160 /* Gotta do something different here, use global lock for now... */
161 spin_lock_irqsave(&rtas.lock, s);
162 rtas_args = &rtas.args;
164 rtas_args->token = token;
165 rtas_args->nargs = nargs;
166 rtas_args->nret = nret;
167 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
168 va_start(list, outputs);
169 for (i = 0; i < nargs; ++i) {
170 rtas_args->args[i] = va_arg(list, rtas_arg_t);
171 PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%x\n", i, rtas_args->args[i]);
173 va_end(list);
175 for (i = 0; i < nret; ++i)
176 rtas_args->rets[i] = 0;
178 PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
179 __pa(rtas_args));
180 enter_rtas(__pa(rtas_args));
181 PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
183 /* A -1 return code indicates that the last command couldn't
184 be completed due to a hardware error. */
185 if (rtas_args->rets[0] == -1)
186 logit = (__fetch_rtas_last_error() == 0);
188 ifppcdebug(PPCDBG_RTAS) {
189 for(i=0; i < nret ;i++)
190 udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
193 if (nret > 1 && outputs != NULL)
194 for (i = 0; i < nret-1; ++i)
195 outputs[i] = rtas_args->rets[i+1];
196 ret = (nret > 0)? rtas_args->rets[0]: 0;
198 /* Log the error in the unlikely case that there was one. */
199 if (unlikely(logit)) {
200 buff_copy = rtas_err_buf;
201 if (mem_init_done) {
202 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
203 if (buff_copy)
204 memcpy(buff_copy, rtas_err_buf,
205 RTAS_ERROR_LOG_MAX);
209 /* Gotta do something different here, use global lock for now... */
210 spin_unlock_irqrestore(&rtas.lock, s);
212 if (buff_copy) {
213 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
214 if (mem_init_done)
215 kfree(buff_copy);
217 return ret;
220 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
221 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
223 unsigned int
224 rtas_extended_busy_delay_time(int status)
226 int order = status - 9900;
227 unsigned long ms;
229 if (order < 0)
230 order = 0; /* RTC depends on this for -2 clock busy */
231 else if (order > 5)
232 order = 5; /* bound */
234 /* Use microseconds for reasonable accuracy */
235 for (ms=1; order > 0; order--)
236 ms *= 10;
238 return ms;
242 rtas_get_power_level(int powerdomain, int *level)
244 int token = rtas_token("get-power-level");
245 int rc;
247 if (token == RTAS_UNKNOWN_SERVICE)
248 return RTAS_UNKNOWN_OP;
250 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
251 udelay(1);
252 return rc;
256 rtas_set_power_level(int powerdomain, int level, int *setlevel)
258 int token = rtas_token("set-power-level");
259 unsigned int wait_time;
260 int rc;
262 if (token == RTAS_UNKNOWN_SERVICE)
263 return RTAS_UNKNOWN_OP;
265 while (1) {
266 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
267 if (rc == RTAS_BUSY)
268 udelay(1);
269 else if (rtas_is_extended_busy(rc)) {
270 wait_time = rtas_extended_busy_delay_time(rc);
271 udelay(wait_time * 1000);
272 } else
273 break;
275 return rc;
279 rtas_get_sensor(int sensor, int index, int *state)
281 int token = rtas_token("get-sensor-state");
282 unsigned int wait_time;
283 int rc;
285 if (token == RTAS_UNKNOWN_SERVICE)
286 return RTAS_UNKNOWN_OP;
288 while (1) {
289 rc = rtas_call(token, 2, 2, state, sensor, index);
290 if (rc == RTAS_BUSY)
291 udelay(1);
292 else if (rtas_is_extended_busy(rc)) {
293 wait_time = rtas_extended_busy_delay_time(rc);
294 udelay(wait_time * 1000);
295 } else
296 break;
298 return rc;
302 rtas_set_indicator(int indicator, int index, int new_value)
304 int token = rtas_token("set-indicator");
305 unsigned int wait_time;
306 int rc;
308 if (token == RTAS_UNKNOWN_SERVICE)
309 return RTAS_UNKNOWN_OP;
311 while (1) {
312 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
313 if (rc == RTAS_BUSY)
314 udelay(1);
315 else if (rtas_is_extended_busy(rc)) {
316 wait_time = rtas_extended_busy_delay_time(rc);
317 udelay(wait_time * 1000);
319 else
320 break;
323 return rc;
326 #define FLASH_BLOCK_LIST_VERSION (1UL)
327 static void
328 rtas_flash_firmware(void)
330 unsigned long image_size;
331 struct flash_block_list *f, *next, *flist;
332 unsigned long rtas_block_list;
333 int i, status, update_token;
335 update_token = rtas_token("ibm,update-flash-64-and-reboot");
336 if (update_token == RTAS_UNKNOWN_SERVICE) {
337 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
338 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
339 return;
342 /* NOTE: the "first" block list is a global var with no data
343 * blocks in the kernel data segment. We do this because
344 * we want to ensure this block_list addr is under 4GB.
346 rtas_firmware_flash_list.num_blocks = 0;
347 flist = (struct flash_block_list *)&rtas_firmware_flash_list;
348 rtas_block_list = virt_to_abs(flist);
349 if (rtas_block_list >= 4UL*1024*1024*1024) {
350 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
351 return;
354 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
355 /* Update the block_list in place. */
356 image_size = 0;
357 for (f = flist; f; f = next) {
358 /* Translate data addrs to absolute */
359 for (i = 0; i < f->num_blocks; i++) {
360 f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
361 image_size += f->blocks[i].length;
363 next = f->next;
364 /* Don't translate NULL pointer for last entry */
365 if (f->next)
366 f->next = (struct flash_block_list *)virt_to_abs(f->next);
367 else
368 f->next = NULL;
369 /* make num_blocks into the version/length field */
370 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
373 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
374 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
375 ppc_md.progress("Flashing \n", 0x0);
376 ppc_md.progress("Please Wait... ", 0x0);
377 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
378 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
379 switch (status) { /* should only get "bad" status */
380 case 0:
381 printk(KERN_ALERT "FLASH: success\n");
382 break;
383 case -1:
384 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
385 break;
386 case -3:
387 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
388 break;
389 case -4:
390 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
391 break;
392 default:
393 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
394 break;
398 void rtas_flash_bypass_warning(void)
400 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
401 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
405 void
406 rtas_restart(char *cmd)
408 if (rtas_firmware_flash_list.next)
409 rtas_flash_firmware();
411 printk("RTAS system-reboot returned %d\n",
412 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
413 for (;;);
416 void
417 rtas_power_off(void)
419 if (rtas_firmware_flash_list.next)
420 rtas_flash_bypass_warning();
421 /* allow power on only with power button press */
422 printk("RTAS power-off returned %d\n",
423 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
424 for (;;);
427 void
428 rtas_halt(void)
430 if (rtas_firmware_flash_list.next)
431 rtas_flash_bypass_warning();
432 rtas_power_off();
435 /* Must be in the RMO region, so we place it here */
436 static char rtas_os_term_buf[2048];
438 void rtas_os_term(char *str)
440 int status;
442 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
444 do {
445 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
446 __pa(rtas_os_term_buf));
448 if (status == RTAS_BUSY)
449 udelay(1);
450 else if (status != 0)
451 printk(KERN_EMERG "ibm,os-term call failed %d\n",
452 status);
453 } while (status == RTAS_BUSY);
457 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
459 struct rtas_args args;
460 unsigned long flags;
461 char * buff_copy;
462 int nargs;
463 int err_rc = 0;
465 if (!capable(CAP_SYS_ADMIN))
466 return -EPERM;
468 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
469 return -EFAULT;
471 nargs = args.nargs;
472 if (nargs > ARRAY_SIZE(args.args)
473 || args.nret > ARRAY_SIZE(args.args)
474 || nargs + args.nret > ARRAY_SIZE(args.args))
475 return -EINVAL;
477 /* Copy in args. */
478 if (copy_from_user(args.args, uargs->args,
479 nargs * sizeof(rtas_arg_t)) != 0)
480 return -EFAULT;
482 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL);
484 spin_lock_irqsave(&rtas.lock, flags);
486 rtas.args = args;
487 enter_rtas(__pa(&rtas.args));
488 args = rtas.args;
490 args.rets = &args.args[nargs];
492 /* A -1 return code indicates that the last command couldn't
493 be completed due to a hardware error. */
494 if (args.rets[0] == -1) {
495 err_rc = __fetch_rtas_last_error();
496 if ((err_rc == 0) && buff_copy) {
497 memcpy(buff_copy, rtas_err_buf, RTAS_ERROR_LOG_MAX);
501 spin_unlock_irqrestore(&rtas.lock, flags);
503 if (buff_copy) {
504 if ((args.rets[0] == -1) && (err_rc == 0)) {
505 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
507 kfree(buff_copy);
510 /* Copy out args. */
511 if (copy_to_user(uargs->args + nargs,
512 args.args + nargs,
513 args.nret * sizeof(rtas_arg_t)) != 0)
514 return -EFAULT;
516 return 0;
519 #ifdef CONFIG_HOTPLUG_CPU
520 /* This version can't take the spinlock, because it never returns */
522 struct rtas_args rtas_stop_self_args = {
523 /* The token is initialized for real in setup_system() */
524 .token = RTAS_UNKNOWN_SERVICE,
525 .nargs = 0,
526 .nret = 1,
527 .rets = &rtas_stop_self_args.args[0],
530 void rtas_stop_self(void)
532 struct rtas_args *rtas_args = &rtas_stop_self_args;
534 local_irq_disable();
536 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
538 printk("cpu %u (hwid %u) Ready to die...\n",
539 smp_processor_id(), hard_smp_processor_id());
540 enter_rtas(__pa(rtas_args));
542 panic("Alas, I survived.\n");
544 #endif /* CONFIG_HOTPLUG_CPU */
547 * Return the firmware-specified size of the error log buffer
548 * for all rtas calls that require an error buffer argument.
549 * This includes 'check-exception' and 'rtas-last-error'.
551 int rtas_get_error_log_max(void)
553 static int rtas_error_log_max;
554 if (rtas_error_log_max)
555 return rtas_error_log_max;
557 rtas_error_log_max = rtas_token ("rtas-error-log-max");
558 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
559 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
560 printk (KERN_WARNING "RTAS: bad log buffer size %d\n", rtas_error_log_max);
561 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
563 return rtas_error_log_max;
567 * Call early during boot, before mem init or bootmem, to retreive the RTAS
568 * informations from the device-tree and allocate the RMO buffer for userland
569 * accesses.
571 void __init rtas_initialize(void)
573 /* Get RTAS dev node and fill up our "rtas" structure with infos
574 * about it.
576 rtas.dev = of_find_node_by_name(NULL, "rtas");
577 if (rtas.dev) {
578 u64 *basep, *entryp;
579 u32 *sizep;
581 basep = (u64 *)get_property(of_chosen, "linux,rtas-base", NULL);
582 sizep = (u32 *)get_property(of_chosen, "linux,rtas-size", NULL);
583 if (basep != NULL && sizep != NULL) {
584 rtas.base = *basep;
585 rtas.size = *sizep;
586 entryp = (u64 *)get_property(of_chosen, "linux,rtas-entry", NULL);
587 if (entryp == NULL) /* Ugh */
588 rtas.entry = rtas.base;
589 else
590 rtas.entry = *entryp;
591 } else
592 rtas.dev = NULL;
594 /* If RTAS was found, allocate the RMO buffer for it and look for
595 * the stop-self token if any
597 if (rtas.dev) {
598 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
599 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
600 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
602 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
603 rtas_region);
605 #ifdef CONFIG_HOTPLUG_CPU
606 rtas_stop_self_args.token = rtas_token("stop-self");
607 #endif /* CONFIG_HOTPLUG_CPU */
613 EXPORT_SYMBOL(rtas_firmware_flash_list);
614 EXPORT_SYMBOL(rtas_token);
615 EXPORT_SYMBOL(rtas_call);
616 EXPORT_SYMBOL(rtas_data_buf);
617 EXPORT_SYMBOL(rtas_data_buf_lock);
618 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
619 EXPORT_SYMBOL(rtas_get_sensor);
620 EXPORT_SYMBOL(rtas_get_power_level);
621 EXPORT_SYMBOL(rtas_set_power_level);
622 EXPORT_SYMBOL(rtas_set_indicator);
623 EXPORT_SYMBOL(rtas_get_error_log_max);