Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / ppc64 / kernel / rtas.c
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/module.h>
19 #include <linux/init.h>
20
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>
33
34 struct flash_block_list_header rtas_firmware_flash_list = {0, NULL};
35
36 struct rtas_t rtas = {
37 .lock = SPIN_LOCK_UNLOCKED
38 };
39
40 EXPORT_SYMBOL(rtas);
41
42 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
43
44 DEFINE_SPINLOCK(rtas_data_buf_lock);
45 char rtas_data_buf[RTAS_DATA_BUF_SIZE]__page_aligned;
46 unsigned long rtas_rmo_buf;
47
48 void
49 call_rtas_display_status(unsigned char c)
50 {
51 struct rtas_args *args = &rtas.args;
52 unsigned long s;
53
54 if (!rtas.base)
55 return;
56 spin_lock_irqsave(&rtas.lock, s);
57
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;
63
64 enter_rtas(__pa(args));
65
66 spin_unlock_irqrestore(&rtas.lock, s);
67 }
68
69 void
70 call_rtas_display_status_delay(unsigned char c)
71 {
72 static int pending_newline = 0; /* did last write end with unprinted newline? */
73 static int width = 16;
74
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');
85 }
86 pending_newline = 0;
87 if (width--) {
88 call_rtas_display_status(c);
89 udelay(10000);
90 }
91 }
92 }
93
94 int
95 rtas_token(const char *service)
96 {
97 int *tokp;
98 if (rtas.dev == NULL) {
99 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
100 return RTAS_UNKNOWN_SERVICE;
101 }
102 tokp = (int *) get_property(rtas.dev, service, NULL);
103 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
104 }
105
106 /*
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'.
110 */
111 int rtas_get_error_log_max(void)
112 {
113 static int rtas_error_log_max;
114 if (rtas_error_log_max)
115 return rtas_error_log_max;
116
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;
122 }
123 return rtas_error_log_max;
124 }
125
126
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).
132 */
133 static int
134 __fetch_rtas_last_error(void)
135 {
136 struct rtas_args err_args, save_args;
137 u32 bufsz;
138
139 bufsz = rtas_get_error_log_max();
140
141 err_args.token = rtas_token("rtas-last-error");
142 err_args.nargs = 2;
143 err_args.nret = 1;
144
145 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
146 err_args.args[1] = bufsz;
147 err_args.args[2] = 0;
148
149 save_args = rtas.args;
150 rtas.args = err_args;
151
152 enter_rtas(__pa(&rtas.args));
153
154 err_args = rtas.args;
155 rtas.args = save_args;
156
157 return err_args.args[2];
158 }
159
160 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
161 {
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;
168
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;
176
177 /* Gotta do something different here, use global lock for now... */
178 spin_lock_irqsave(&rtas.lock, s);
179 rtas_args = &rtas.args;
180
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]);
189 }
190 va_end(list);
191
192 for (i = 0; i < nret; ++i)
193 rtas_args->rets[i] = 0;
194
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");
199
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);
204
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]);
208 }
209
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;
214
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);
223 }
224 }
225
226 /* Gotta do something different here, use global lock for now... */
227 spin_unlock_irqrestore(&rtas.lock, s);
228
229 if (buff_copy) {
230 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
231 if (mem_init_done)
232 kfree(buff_copy);
233 }
234 return ret;
235 }
236
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).
239 */
240 unsigned int
241 rtas_extended_busy_delay_time(int status)
242 {
243 int order = status - 9900;
244 unsigned long ms;
245
246 if (order < 0)
247 order = 0; /* RTC depends on this for -2 clock busy */
248 else if (order > 5)
249 order = 5; /* bound */
250
251 /* Use microseconds for reasonable accuracy */
252 for (ms=1; order > 0; order--)
253 ms *= 10;
254
255 return ms;
256 }
257
258 int rtas_error_rc(int rtas_rc)
259 {
260 int rc;
261
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;
283 }
284 return rc;
285 }
286
287 int rtas_get_power_level(int powerdomain, int *level)
288 {
289 int token = rtas_token("get-power-level");
290 int rc;
291
292 if (token == RTAS_UNKNOWN_SERVICE)
293 return -ENOENT;
294
295 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
296 udelay(1);
297
298 if (rc < 0)
299 return rtas_error_rc(rc);
300 return rc;
301 }
302
303 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
304 {
305 int token = rtas_token("set-power-level");
306 unsigned int wait_time;
307 int rc;
308
309 if (token == RTAS_UNKNOWN_SERVICE)
310 return -ENOENT;
311
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;
321 }
322
323 if (rc < 0)
324 return rtas_error_rc(rc);
325 return rc;
326 }
327
328 int rtas_get_sensor(int sensor, int index, int *state)
329 {
330 int token = rtas_token("get-sensor-state");
331 unsigned int wait_time;
332 int rc;
333
334 if (token == RTAS_UNKNOWN_SERVICE)
335 return -ENOENT;
336
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;
346 }
347
348 if (rc < 0)
349 return rtas_error_rc(rc);
350 return rc;
351 }
352
353 int rtas_set_indicator(int indicator, int index, int new_value)
354 {
355 int token = rtas_token("set-indicator");
356 unsigned int wait_time;
357 int rc;
358
359 if (token == RTAS_UNKNOWN_SERVICE)
360 return -ENOENT;
361
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);
369 }
370 else
371 break;
372 }
373
374 if (rc < 0)
375 return rtas_error_rc(rc);
376 return rc;
377 }
378
379 #define FLASH_BLOCK_LIST_VERSION (1UL)
380 static void
381 rtas_flash_firmware(void)
382 {
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;
387
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;
393 }
394
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.
398 */
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;
405 }
406
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;
415 }
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);
424 }
425
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;
448 }
449 }
450
451 void rtas_flash_bypass_warning(void)
452 {
453 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
454 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
455 }
456
457
458 void
459 rtas_restart(char *cmd)
460 {
461 if (rtas_firmware_flash_list.next)
462 rtas_flash_firmware();
463
464 printk("RTAS system-reboot returned %d\n",
465 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
466 for (;;);
467 }
468
469 void
470 rtas_power_off(void)
471 {
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 (;;);
478 }
479
480 void
481 rtas_halt(void)
482 {
483 if (rtas_firmware_flash_list.next)
484 rtas_flash_bypass_warning();
485 rtas_power_off();
486 }
487
488 /* Must be in the RMO region, so we place it here */
489 static char rtas_os_term_buf[2048];
490
491 void rtas_os_term(char *str)
492 {
493 int status;
494
495 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
496 return;
497
498 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
499
500 do {
501 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
502 __pa(rtas_os_term_buf));
503
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);
510 }
511
512
513 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
514 {
515 struct rtas_args args;
516 unsigned long flags;
517 char * buff_copy;
518 int nargs;
519 int err_rc = 0;
520
521 if (!capable(CAP_SYS_ADMIN))
522 return -EPERM;
523
524 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
525 return -EFAULT;
526
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;
532
533 /* Copy in args. */
534 if (copy_from_user(args.args, uargs->args,
535 nargs * sizeof(rtas_arg_t)) != 0)
536 return -EFAULT;
537
538 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL);
539
540 spin_lock_irqsave(&rtas.lock, flags);
541
542 rtas.args = args;
543 enter_rtas(__pa(&rtas.args));
544 args = rtas.args;
545
546 args.rets = &args.args[nargs];
547
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);
554 }
555 }
556
557 spin_unlock_irqrestore(&rtas.lock, flags);
558
559 if (buff_copy) {
560 if ((args.rets[0] == -1) && (err_rc == 0)) {
561 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
562 }
563 kfree(buff_copy);
564 }
565
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;
571
572 return 0;
573 }
574
575 /* This version can't take the spinlock, because it never returns */
576
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],
583 };
584
585 void rtas_stop_self(void)
586 {
587 struct rtas_args *rtas_args = &rtas_stop_self_args;
588
589 local_irq_disable();
590
591 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
592
593 printk("cpu %u (hwid %u) Ready to die...\n",
594 smp_processor_id(), hard_smp_processor_id());
595 enter_rtas(__pa(rtas_args));
596
597 panic("Alas, I survived.\n");
598 }
599
600 /*
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.
604 */
605 void __init rtas_initialize(void)
606 {
607 /* Get RTAS dev node and fill up our "rtas" structure with infos
608 * about it.
609 */
610 rtas.dev = of_find_node_by_name(NULL, "rtas");
611 if (rtas.dev) {
612 u32 *basep, *entryp;
613 u32 *sizep;
614
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;
627 }
628 /* If RTAS was found, allocate the RMO buffer for it and look for
629 * the stop-self token if any
630 */
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);
635
636 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
637 rtas_region);
638
639 #ifdef CONFIG_HOTPLUG_CPU
640 rtas_stop_self_args.token = rtas_token("stop-self");
641 #endif /* CONFIG_HOTPLUG_CPU */
642 }
643
644 }
645
646
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);