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.
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 #include <linux/delay.h>
24 #include <asm/semaphore.h>
25 #include <asm/machdep.h>
27 #include <asm/param.h>
28 #include <asm/system.h>
29 #include <asm/delay.h>
30 #include <asm/uaccess.h>
34 struct rtas_t rtas
= {
35 .lock
= SPIN_LOCK_UNLOCKED
40 DEFINE_SPINLOCK(rtas_data_buf_lock
);
41 char rtas_data_buf
[RTAS_DATA_BUF_SIZE
] __cacheline_aligned
;
42 unsigned long rtas_rmo_buf
;
45 * If non-NULL, this gets called when the kernel terminates.
46 * This is done like this so rtas_flash can be a module.
48 void (*rtas_flash_term_hook
)(int);
49 EXPORT_SYMBOL(rtas_flash_term_hook
);
52 * call_rtas_display_status and call_rtas_display_status_delay
53 * are designed only for very early low-level debugging, which
54 * is why the token is hard-coded to 10.
56 static void call_rtas_display_status(char c
)
58 struct rtas_args
*args
= &rtas
.args
;
63 spin_lock_irqsave(&rtas
.lock
, s
);
68 args
->rets
= (rtas_arg_t
*)&(args
->args
[1]);
69 args
->args
[0] = (unsigned char)c
;
71 enter_rtas(__pa(args
));
73 spin_unlock_irqrestore(&rtas
.lock
, s
);
76 static void call_rtas_display_status_delay(char c
)
78 static int pending_newline
= 0; /* did last write end with unprinted newline? */
79 static int width
= 16;
83 call_rtas_display_status(' ');
88 if (pending_newline
) {
89 call_rtas_display_status('\r');
90 call_rtas_display_status('\n');
94 call_rtas_display_status(c
);
100 void __init
udbg_init_rtas(void)
102 udbg_putc
= call_rtas_display_status_delay
;
105 void rtas_progress(char *s
, unsigned short hex
)
107 struct device_node
*root
;
110 static int display_character
, set_indicator
;
111 static int display_width
, display_lines
, *row_width
, form_feed
;
112 static DEFINE_SPINLOCK(progress_lock
);
113 static int current_line
;
114 static int pending_newline
= 0; /* did last write end with unprinted newline? */
119 if (display_width
== 0) {
120 display_width
= 0x10;
121 if ((root
= find_path_device("/rtas"))) {
122 if ((p
= (unsigned int *)get_property(root
,
123 "ibm,display-line-length", NULL
)))
125 if ((p
= (unsigned int *)get_property(root
,
126 "ibm,form-feed", NULL
)))
128 if ((p
= (unsigned int *)get_property(root
,
129 "ibm,display-number-of-lines", NULL
)))
131 row_width
= (unsigned int *)get_property(root
,
132 "ibm,display-truncation-length", NULL
);
134 display_character
= rtas_token("display-character");
135 set_indicator
= rtas_token("set-indicator");
138 if (display_character
== RTAS_UNKNOWN_SERVICE
) {
139 /* use hex display if available */
140 if (set_indicator
!= RTAS_UNKNOWN_SERVICE
)
141 rtas_call(set_indicator
, 3, 1, NULL
, 6, 0, hex
);
145 spin_lock(&progress_lock
);
148 * Last write ended with newline, but we didn't print it since
149 * it would just clear the bottom line of output. Print it now
152 * If no newline is pending and form feed is supported, clear the
153 * display with a form feed; otherwise, print a CR to start output
154 * at the beginning of the line.
156 if (pending_newline
) {
157 rtas_call(display_character
, 1, 1, NULL
, '\r');
158 rtas_call(display_character
, 1, 1, NULL
, '\n');
163 rtas_call(display_character
, 1, 1, NULL
,
166 rtas_call(display_character
, 1, 1, NULL
, '\r');
170 width
= row_width
[current_line
];
172 width
= display_width
;
175 if (*os
== '\n' || *os
== '\r') {
176 /* If newline is the last character, save it
177 * until next call to avoid bumping up the
180 if (*os
== '\n' && !os
[1]) {
183 if (current_line
> display_lines
-1)
184 current_line
= display_lines
-1;
185 spin_unlock(&progress_lock
);
189 /* RTAS wants CR-LF, not just LF */
192 rtas_call(display_character
, 1, 1, NULL
, '\r');
193 rtas_call(display_character
, 1, 1, NULL
, '\n');
195 /* CR might be used to re-draw a line, so we'll
196 * leave it alone and not add LF.
198 rtas_call(display_character
, 1, 1, NULL
, *os
);
202 width
= row_width
[current_line
];
204 width
= display_width
;
207 rtas_call(display_character
, 1, 1, NULL
, *os
);
212 /* if we overwrite the screen length */
214 while ((*os
!= 0) && (*os
!= '\n') && (*os
!= '\r'))
218 spin_unlock(&progress_lock
);
220 EXPORT_SYMBOL(rtas_progress
); /* needed by rtas_flash module */
222 int rtas_token(const char *service
)
225 if (rtas
.dev
== NULL
)
226 return RTAS_UNKNOWN_SERVICE
;
227 tokp
= (int *) get_property(rtas
.dev
, service
, NULL
);
228 return tokp
? *tokp
: RTAS_UNKNOWN_SERVICE
;
231 #ifdef CONFIG_RTAS_ERROR_LOGGING
233 * Return the firmware-specified size of the error log buffer
234 * for all rtas calls that require an error buffer argument.
235 * This includes 'check-exception' and 'rtas-last-error'.
237 int rtas_get_error_log_max(void)
239 static int rtas_error_log_max
;
240 if (rtas_error_log_max
)
241 return rtas_error_log_max
;
243 rtas_error_log_max
= rtas_token ("rtas-error-log-max");
244 if ((rtas_error_log_max
== RTAS_UNKNOWN_SERVICE
) ||
245 (rtas_error_log_max
> RTAS_ERROR_LOG_MAX
)) {
246 printk (KERN_WARNING
"RTAS: bad log buffer size %d\n",
248 rtas_error_log_max
= RTAS_ERROR_LOG_MAX
;
250 return rtas_error_log_max
;
252 EXPORT_SYMBOL(rtas_get_error_log_max
);
255 char rtas_err_buf
[RTAS_ERROR_LOG_MAX
];
256 int rtas_last_error_token
;
258 /** Return a copy of the detailed error text associated with the
259 * most recent failed call to rtas. Because the error text
260 * might go stale if there are any other intervening rtas calls,
261 * this routine must be called atomically with whatever produced
262 * the error (i.e. with rtas.lock still held from the previous call).
264 static char *__fetch_rtas_last_error(char *altbuf
)
266 struct rtas_args err_args
, save_args
;
270 if (rtas_last_error_token
== -1)
273 bufsz
= rtas_get_error_log_max();
275 err_args
.token
= rtas_last_error_token
;
278 err_args
.args
[0] = (rtas_arg_t
)__pa(rtas_err_buf
);
279 err_args
.args
[1] = bufsz
;
280 err_args
.args
[2] = 0;
282 save_args
= rtas
.args
;
283 rtas
.args
= err_args
;
285 enter_rtas(__pa(&rtas
.args
));
287 err_args
= rtas
.args
;
288 rtas
.args
= save_args
;
290 /* Log the error in the unlikely case that there was one. */
291 if (unlikely(err_args
.args
[2] == 0)) {
297 buf
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_ATOMIC
);
300 memcpy(buf
, rtas_err_buf
, RTAS_ERROR_LOG_MAX
);
306 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
308 #else /* CONFIG_RTAS_ERROR_LOGGING */
309 #define __fetch_rtas_last_error(x) NULL
310 #define get_errorlog_buffer() NULL
313 int rtas_call(int token
, int nargs
, int nret
, int *outputs
, ...)
318 struct rtas_args
*rtas_args
;
319 char *buff_copy
= NULL
;
322 if (token
== RTAS_UNKNOWN_SERVICE
)
325 /* Gotta do something different here, use global lock for now... */
326 spin_lock_irqsave(&rtas
.lock
, s
);
327 rtas_args
= &rtas
.args
;
329 rtas_args
->token
= token
;
330 rtas_args
->nargs
= nargs
;
331 rtas_args
->nret
= nret
;
332 rtas_args
->rets
= (rtas_arg_t
*)&(rtas_args
->args
[nargs
]);
333 va_start(list
, outputs
);
334 for (i
= 0; i
< nargs
; ++i
)
335 rtas_args
->args
[i
] = va_arg(list
, rtas_arg_t
);
338 for (i
= 0; i
< nret
; ++i
)
339 rtas_args
->rets
[i
] = 0;
341 enter_rtas(__pa(rtas_args
));
343 /* A -1 return code indicates that the last command couldn't
344 be completed due to a hardware error. */
345 if (rtas_args
->rets
[0] == -1)
346 buff_copy
= __fetch_rtas_last_error(NULL
);
348 if (nret
> 1 && outputs
!= NULL
)
349 for (i
= 0; i
< nret
-1; ++i
)
350 outputs
[i
] = rtas_args
->rets
[i
+1];
351 ret
= (nret
> 0)? rtas_args
->rets
[0]: 0;
353 /* Gotta do something different here, use global lock for now... */
354 spin_unlock_irqrestore(&rtas
.lock
, s
);
357 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
364 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
365 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
367 unsigned int rtas_extended_busy_delay_time(int status
)
369 int order
= status
- 9900;
373 order
= 0; /* RTC depends on this for -2 clock busy */
375 order
= 5; /* bound */
377 /* Use microseconds for reasonable accuracy */
378 for (ms
= 1; order
> 0; order
--)
384 int rtas_error_rc(int rtas_rc
)
389 case -1: /* Hardware Error */
392 case -3: /* Bad indicator/domain/etc */
395 case -9000: /* Isolation error */
398 case -9001: /* Outstanding TCE/PTE */
401 case -9002: /* No usable slot */
405 printk(KERN_ERR
"%s: unexpected RTAS error %d\n",
406 __FUNCTION__
, rtas_rc
);
413 int rtas_get_power_level(int powerdomain
, int *level
)
415 int token
= rtas_token("get-power-level");
418 if (token
== RTAS_UNKNOWN_SERVICE
)
421 while ((rc
= rtas_call(token
, 1, 2, level
, powerdomain
)) == RTAS_BUSY
)
425 return rtas_error_rc(rc
);
429 int rtas_set_power_level(int powerdomain
, int level
, int *setlevel
)
431 int token
= rtas_token("set-power-level");
432 unsigned int wait_time
;
435 if (token
== RTAS_UNKNOWN_SERVICE
)
439 rc
= rtas_call(token
, 2, 2, setlevel
, powerdomain
, level
);
442 else if (rtas_is_extended_busy(rc
)) {
443 wait_time
= rtas_extended_busy_delay_time(rc
);
444 udelay(wait_time
* 1000);
450 return rtas_error_rc(rc
);
454 int rtas_get_sensor(int sensor
, int index
, int *state
)
456 int token
= rtas_token("get-sensor-state");
457 unsigned int wait_time
;
460 if (token
== RTAS_UNKNOWN_SERVICE
)
464 rc
= rtas_call(token
, 2, 2, state
, sensor
, index
);
467 else if (rtas_is_extended_busy(rc
)) {
468 wait_time
= rtas_extended_busy_delay_time(rc
);
469 udelay(wait_time
* 1000);
475 return rtas_error_rc(rc
);
479 int rtas_set_indicator(int indicator
, int index
, int new_value
)
481 int token
= rtas_token("set-indicator");
482 unsigned int wait_time
;
485 if (token
== RTAS_UNKNOWN_SERVICE
)
489 rc
= rtas_call(token
, 3, 1, NULL
, indicator
, index
, new_value
);
492 else if (rtas_is_extended_busy(rc
)) {
493 wait_time
= rtas_extended_busy_delay_time(rc
);
494 udelay(wait_time
* 1000);
501 return rtas_error_rc(rc
);
505 void rtas_restart(char *cmd
)
507 if (rtas_flash_term_hook
)
508 rtas_flash_term_hook(SYS_RESTART
);
509 printk("RTAS system-reboot returned %d\n",
510 rtas_call(rtas_token("system-reboot"), 0, 1, NULL
));
514 void rtas_power_off(void)
516 if (rtas_flash_term_hook
)
517 rtas_flash_term_hook(SYS_POWER_OFF
);
518 /* allow power on only with power button press */
519 printk("RTAS power-off returned %d\n",
520 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
526 if (rtas_flash_term_hook
)
527 rtas_flash_term_hook(SYS_HALT
);
528 /* allow power on only with power button press */
529 printk("RTAS power-off returned %d\n",
530 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
534 /* Must be in the RMO region, so we place it here */
535 static char rtas_os_term_buf
[2048];
537 void rtas_os_term(char *str
)
541 if (RTAS_UNKNOWN_SERVICE
== rtas_token("ibm,os-term"))
544 snprintf(rtas_os_term_buf
, 2048, "OS panic: %s", str
);
547 status
= rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL
,
548 __pa(rtas_os_term_buf
));
550 if (status
== RTAS_BUSY
)
552 else if (status
!= 0)
553 printk(KERN_EMERG
"ibm,os-term call failed %d\n",
555 } while (status
== RTAS_BUSY
);
559 asmlinkage
int ppc_rtas(struct rtas_args __user
*uargs
)
561 struct rtas_args args
;
563 char *buff_copy
, *errbuf
= NULL
;
566 if (!capable(CAP_SYS_ADMIN
))
569 if (copy_from_user(&args
, uargs
, 3 * sizeof(u32
)) != 0)
573 if (nargs
> ARRAY_SIZE(args
.args
)
574 || args
.nret
> ARRAY_SIZE(args
.args
)
575 || nargs
+ args
.nret
> ARRAY_SIZE(args
.args
))
579 if (copy_from_user(args
.args
, uargs
->args
,
580 nargs
* sizeof(rtas_arg_t
)) != 0)
583 buff_copy
= get_errorlog_buffer();
585 spin_lock_irqsave(&rtas
.lock
, flags
);
588 enter_rtas(__pa(&rtas
.args
));
591 args
.rets
= &args
.args
[nargs
];
593 /* A -1 return code indicates that the last command couldn't
594 be completed due to a hardware error. */
595 if (args
.rets
[0] == -1)
596 errbuf
= __fetch_rtas_last_error(buff_copy
);
598 spin_unlock_irqrestore(&rtas
.lock
, flags
);
602 log_error(errbuf
, ERR_TYPE_RTAS_LOG
, 0);
607 if (copy_to_user(uargs
->args
+ nargs
,
609 args
.nret
* sizeof(rtas_arg_t
)) != 0)
615 /* This version can't take the spinlock, because it never returns */
617 struct rtas_args rtas_stop_self_args
= {
618 /* The token is initialized for real in setup_system() */
619 .token
= RTAS_UNKNOWN_SERVICE
,
622 .rets
= &rtas_stop_self_args
.args
[0],
625 void rtas_stop_self(void)
627 struct rtas_args
*rtas_args
= &rtas_stop_self_args
;
631 BUG_ON(rtas_args
->token
== RTAS_UNKNOWN_SERVICE
);
633 printk("cpu %u (hwid %u) Ready to die...\n",
634 smp_processor_id(), hard_smp_processor_id());
635 enter_rtas(__pa(rtas_args
));
637 panic("Alas, I survived.\n");
641 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
642 * informations from the device-tree and allocate the RMO buffer for userland
645 void __init
rtas_initialize(void)
647 unsigned long rtas_region
= RTAS_INSTANTIATE_MAX
;
649 /* Get RTAS dev node and fill up our "rtas" structure with infos
652 rtas
.dev
= of_find_node_by_name(NULL
, "rtas");
657 basep
= (u32
*)get_property(rtas
.dev
, "linux,rtas-base", NULL
);
658 sizep
= (u32
*)get_property(rtas
.dev
, "rtas-size", NULL
);
659 if (basep
!= NULL
&& sizep
!= NULL
) {
662 entryp
= (u32
*)get_property(rtas
.dev
, "linux,rtas-entry", NULL
);
663 if (entryp
== NULL
) /* Ugh */
664 rtas
.entry
= rtas
.base
;
666 rtas
.entry
= *entryp
;
673 /* If RTAS was found, allocate the RMO buffer for it and look for
674 * the stop-self token if any
677 if (_machine
== PLATFORM_PSERIES_LPAR
)
678 rtas_region
= min(lmb
.rmo_size
, RTAS_INSTANTIATE_MAX
);
680 rtas_rmo_buf
= lmb_alloc_base(RTAS_RMOBUF_MAX
, PAGE_SIZE
, rtas_region
);
682 #ifdef CONFIG_HOTPLUG_CPU
683 rtas_stop_self_args
.token
= rtas_token("stop-self");
684 #endif /* CONFIG_HOTPLUG_CPU */
685 #ifdef CONFIG_RTAS_ERROR_LOGGING
686 rtas_last_error_token
= rtas_token("rtas-last-error");
691 EXPORT_SYMBOL(rtas_token
);
692 EXPORT_SYMBOL(rtas_call
);
693 EXPORT_SYMBOL(rtas_data_buf
);
694 EXPORT_SYMBOL(rtas_data_buf_lock
);
695 EXPORT_SYMBOL(rtas_extended_busy_delay_time
);
696 EXPORT_SYMBOL(rtas_get_sensor
);
697 EXPORT_SYMBOL(rtas_get_power_level
);
698 EXPORT_SYMBOL(rtas_set_power_level
);
699 EXPORT_SYMBOL(rtas_set_indicator
);