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/capability.h>
21 #include <linux/delay.h>
25 #include <asm/hvcall.h>
26 #include <asm/semaphore.h>
27 #include <asm/machdep.h>
29 #include <asm/param.h>
30 #include <asm/system.h>
31 #include <asm/delay.h>
32 #include <asm/uaccess.h>
35 #include <asm/syscalls.h>
37 struct rtas_t rtas
= {
38 .lock
= SPIN_LOCK_UNLOCKED
41 struct rtas_suspend_me_data
{
43 struct rtas_args
*args
;
48 DEFINE_SPINLOCK(rtas_data_buf_lock
);
49 char rtas_data_buf
[RTAS_DATA_BUF_SIZE
] __cacheline_aligned
;
50 unsigned long rtas_rmo_buf
;
53 * If non-NULL, this gets called when the kernel terminates.
54 * This is done like this so rtas_flash can be a module.
56 void (*rtas_flash_term_hook
)(int);
57 EXPORT_SYMBOL(rtas_flash_term_hook
);
60 * call_rtas_display_status and call_rtas_display_status_delay
61 * are designed only for very early low-level debugging, which
62 * is why the token is hard-coded to 10.
64 static void call_rtas_display_status(char c
)
66 struct rtas_args
*args
= &rtas
.args
;
71 spin_lock_irqsave(&rtas
.lock
, s
);
76 args
->rets
= (rtas_arg_t
*)&(args
->args
[1]);
77 args
->args
[0] = (unsigned char)c
;
79 enter_rtas(__pa(args
));
81 spin_unlock_irqrestore(&rtas
.lock
, s
);
84 static void call_rtas_display_status_delay(char c
)
86 static int pending_newline
= 0; /* did last write end with unprinted newline? */
87 static int width
= 16;
91 call_rtas_display_status(' ');
96 if (pending_newline
) {
97 call_rtas_display_status('\r');
98 call_rtas_display_status('\n');
102 call_rtas_display_status(c
);
108 void __init
udbg_init_rtas(void)
110 udbg_putc
= call_rtas_display_status_delay
;
113 void rtas_progress(char *s
, unsigned short hex
)
115 struct device_node
*root
;
118 static int display_character
, set_indicator
;
119 static int display_width
, display_lines
, *row_width
, form_feed
;
120 static DEFINE_SPINLOCK(progress_lock
);
121 static int current_line
;
122 static int pending_newline
= 0; /* did last write end with unprinted newline? */
127 if (display_width
== 0) {
128 display_width
= 0x10;
129 if ((root
= find_path_device("/rtas"))) {
130 if ((p
= (unsigned int *)get_property(root
,
131 "ibm,display-line-length", NULL
)))
133 if ((p
= (unsigned int *)get_property(root
,
134 "ibm,form-feed", NULL
)))
136 if ((p
= (unsigned int *)get_property(root
,
137 "ibm,display-number-of-lines", NULL
)))
139 row_width
= (unsigned int *)get_property(root
,
140 "ibm,display-truncation-length", NULL
);
142 display_character
= rtas_token("display-character");
143 set_indicator
= rtas_token("set-indicator");
146 if (display_character
== RTAS_UNKNOWN_SERVICE
) {
147 /* use hex display if available */
148 if (set_indicator
!= RTAS_UNKNOWN_SERVICE
)
149 rtas_call(set_indicator
, 3, 1, NULL
, 6, 0, hex
);
153 spin_lock(&progress_lock
);
156 * Last write ended with newline, but we didn't print it since
157 * it would just clear the bottom line of output. Print it now
160 * If no newline is pending and form feed is supported, clear the
161 * display with a form feed; otherwise, print a CR to start output
162 * at the beginning of the line.
164 if (pending_newline
) {
165 rtas_call(display_character
, 1, 1, NULL
, '\r');
166 rtas_call(display_character
, 1, 1, NULL
, '\n');
171 rtas_call(display_character
, 1, 1, NULL
,
174 rtas_call(display_character
, 1, 1, NULL
, '\r');
178 width
= row_width
[current_line
];
180 width
= display_width
;
183 if (*os
== '\n' || *os
== '\r') {
184 /* If newline is the last character, save it
185 * until next call to avoid bumping up the
188 if (*os
== '\n' && !os
[1]) {
191 if (current_line
> display_lines
-1)
192 current_line
= display_lines
-1;
193 spin_unlock(&progress_lock
);
197 /* RTAS wants CR-LF, not just LF */
200 rtas_call(display_character
, 1, 1, NULL
, '\r');
201 rtas_call(display_character
, 1, 1, NULL
, '\n');
203 /* CR might be used to re-draw a line, so we'll
204 * leave it alone and not add LF.
206 rtas_call(display_character
, 1, 1, NULL
, *os
);
210 width
= row_width
[current_line
];
212 width
= display_width
;
215 rtas_call(display_character
, 1, 1, NULL
, *os
);
220 /* if we overwrite the screen length */
222 while ((*os
!= 0) && (*os
!= '\n') && (*os
!= '\r'))
226 spin_unlock(&progress_lock
);
228 EXPORT_SYMBOL(rtas_progress
); /* needed by rtas_flash module */
230 int rtas_token(const char *service
)
233 if (rtas
.dev
== NULL
)
234 return RTAS_UNKNOWN_SERVICE
;
235 tokp
= (int *) get_property(rtas
.dev
, service
, NULL
);
236 return tokp
? *tokp
: RTAS_UNKNOWN_SERVICE
;
239 #ifdef CONFIG_RTAS_ERROR_LOGGING
241 * Return the firmware-specified size of the error log buffer
242 * for all rtas calls that require an error buffer argument.
243 * This includes 'check-exception' and 'rtas-last-error'.
245 int rtas_get_error_log_max(void)
247 static int rtas_error_log_max
;
248 if (rtas_error_log_max
)
249 return rtas_error_log_max
;
251 rtas_error_log_max
= rtas_token ("rtas-error-log-max");
252 if ((rtas_error_log_max
== RTAS_UNKNOWN_SERVICE
) ||
253 (rtas_error_log_max
> RTAS_ERROR_LOG_MAX
)) {
254 printk (KERN_WARNING
"RTAS: bad log buffer size %d\n",
256 rtas_error_log_max
= RTAS_ERROR_LOG_MAX
;
258 return rtas_error_log_max
;
260 EXPORT_SYMBOL(rtas_get_error_log_max
);
263 char rtas_err_buf
[RTAS_ERROR_LOG_MAX
];
264 int rtas_last_error_token
;
266 /** Return a copy of the detailed error text associated with the
267 * most recent failed call to rtas. Because the error text
268 * might go stale if there are any other intervening rtas calls,
269 * this routine must be called atomically with whatever produced
270 * the error (i.e. with rtas.lock still held from the previous call).
272 static char *__fetch_rtas_last_error(char *altbuf
)
274 struct rtas_args err_args
, save_args
;
278 if (rtas_last_error_token
== -1)
281 bufsz
= rtas_get_error_log_max();
283 err_args
.token
= rtas_last_error_token
;
286 err_args
.args
[0] = (rtas_arg_t
)__pa(rtas_err_buf
);
287 err_args
.args
[1] = bufsz
;
288 err_args
.args
[2] = 0;
290 save_args
= rtas
.args
;
291 rtas
.args
= err_args
;
293 enter_rtas(__pa(&rtas
.args
));
295 err_args
= rtas
.args
;
296 rtas
.args
= save_args
;
298 /* Log the error in the unlikely case that there was one. */
299 if (unlikely(err_args
.args
[2] == 0)) {
305 buf
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_ATOMIC
);
308 memcpy(buf
, rtas_err_buf
, RTAS_ERROR_LOG_MAX
);
314 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
316 #else /* CONFIG_RTAS_ERROR_LOGGING */
317 #define __fetch_rtas_last_error(x) NULL
318 #define get_errorlog_buffer() NULL
321 int rtas_call(int token
, int nargs
, int nret
, int *outputs
, ...)
326 struct rtas_args
*rtas_args
;
327 char *buff_copy
= NULL
;
330 if (token
== RTAS_UNKNOWN_SERVICE
)
333 /* Gotta do something different here, use global lock for now... */
334 spin_lock_irqsave(&rtas
.lock
, s
);
335 rtas_args
= &rtas
.args
;
337 rtas_args
->token
= token
;
338 rtas_args
->nargs
= nargs
;
339 rtas_args
->nret
= nret
;
340 rtas_args
->rets
= (rtas_arg_t
*)&(rtas_args
->args
[nargs
]);
341 va_start(list
, outputs
);
342 for (i
= 0; i
< nargs
; ++i
)
343 rtas_args
->args
[i
] = va_arg(list
, rtas_arg_t
);
346 for (i
= 0; i
< nret
; ++i
)
347 rtas_args
->rets
[i
] = 0;
349 enter_rtas(__pa(rtas_args
));
351 /* A -1 return code indicates that the last command couldn't
352 be completed due to a hardware error. */
353 if (rtas_args
->rets
[0] == -1)
354 buff_copy
= __fetch_rtas_last_error(NULL
);
356 if (nret
> 1 && outputs
!= NULL
)
357 for (i
= 0; i
< nret
-1; ++i
)
358 outputs
[i
] = rtas_args
->rets
[i
+1];
359 ret
= (nret
> 0)? rtas_args
->rets
[0]: 0;
361 /* Gotta do something different here, use global lock for now... */
362 spin_unlock_irqrestore(&rtas
.lock
, s
);
365 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
372 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
373 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
375 unsigned int rtas_extended_busy_delay_time(int status
)
377 int order
= status
- 9900;
381 order
= 0; /* RTC depends on this for -2 clock busy */
383 order
= 5; /* bound */
385 /* Use microseconds for reasonable accuracy */
386 for (ms
= 1; order
> 0; order
--)
392 int rtas_error_rc(int rtas_rc
)
397 case -1: /* Hardware Error */
400 case -3: /* Bad indicator/domain/etc */
403 case -9000: /* Isolation error */
406 case -9001: /* Outstanding TCE/PTE */
409 case -9002: /* No usable slot */
413 printk(KERN_ERR
"%s: unexpected RTAS error %d\n",
414 __FUNCTION__
, rtas_rc
);
421 int rtas_get_power_level(int powerdomain
, int *level
)
423 int token
= rtas_token("get-power-level");
426 if (token
== RTAS_UNKNOWN_SERVICE
)
429 while ((rc
= rtas_call(token
, 1, 2, level
, powerdomain
)) == RTAS_BUSY
)
433 return rtas_error_rc(rc
);
437 int rtas_set_power_level(int powerdomain
, int level
, int *setlevel
)
439 int token
= rtas_token("set-power-level");
440 unsigned int wait_time
;
443 if (token
== RTAS_UNKNOWN_SERVICE
)
447 rc
= rtas_call(token
, 2, 2, setlevel
, powerdomain
, level
);
450 else if (rtas_is_extended_busy(rc
)) {
451 wait_time
= rtas_extended_busy_delay_time(rc
);
452 udelay(wait_time
* 1000);
458 return rtas_error_rc(rc
);
462 int rtas_get_sensor(int sensor
, int index
, int *state
)
464 int token
= rtas_token("get-sensor-state");
465 unsigned int wait_time
;
468 if (token
== RTAS_UNKNOWN_SERVICE
)
472 rc
= rtas_call(token
, 2, 2, state
, sensor
, index
);
475 else if (rtas_is_extended_busy(rc
)) {
476 wait_time
= rtas_extended_busy_delay_time(rc
);
477 udelay(wait_time
* 1000);
483 return rtas_error_rc(rc
);
487 int rtas_set_indicator(int indicator
, int index
, int new_value
)
489 int token
= rtas_token("set-indicator");
490 unsigned int wait_time
;
493 if (token
== RTAS_UNKNOWN_SERVICE
)
497 rc
= rtas_call(token
, 3, 1, NULL
, indicator
, index
, new_value
);
500 else if (rtas_is_extended_busy(rc
)) {
501 wait_time
= rtas_extended_busy_delay_time(rc
);
502 udelay(wait_time
* 1000);
509 return rtas_error_rc(rc
);
513 void rtas_restart(char *cmd
)
515 if (rtas_flash_term_hook
)
516 rtas_flash_term_hook(SYS_RESTART
);
517 printk("RTAS system-reboot returned %d\n",
518 rtas_call(rtas_token("system-reboot"), 0, 1, NULL
));
522 void rtas_power_off(void)
524 if (rtas_flash_term_hook
)
525 rtas_flash_term_hook(SYS_POWER_OFF
);
526 /* allow power on only with power button press */
527 printk("RTAS power-off returned %d\n",
528 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
534 if (rtas_flash_term_hook
)
535 rtas_flash_term_hook(SYS_HALT
);
536 /* allow power on only with power button press */
537 printk("RTAS power-off returned %d\n",
538 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
542 /* Must be in the RMO region, so we place it here */
543 static char rtas_os_term_buf
[2048];
545 void rtas_os_term(char *str
)
549 if (RTAS_UNKNOWN_SERVICE
== rtas_token("ibm,os-term"))
552 snprintf(rtas_os_term_buf
, 2048, "OS panic: %s", str
);
555 status
= rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL
,
556 __pa(rtas_os_term_buf
));
558 if (status
== RTAS_BUSY
)
560 else if (status
!= 0)
561 printk(KERN_EMERG
"ibm,os-term call failed %d\n",
563 } while (status
== RTAS_BUSY
);
566 static int ibm_suspend_me_token
= RTAS_UNKNOWN_SERVICE
;
567 #ifdef CONFIG_PPC_PSERIES
568 static void rtas_percpu_suspend_me(void *info
)
573 struct rtas_suspend_me_data
*data
=
574 (struct rtas_suspend_me_data
*)info
;
577 * We use "waiting" to indicate our state. As long
578 * as it is >0, we are still trying to all join up.
579 * If it goes to 0, we have successfully joined up and
580 * one thread got H_Continue. If any error happens,
583 local_irq_save(flags
);
585 rc
= plpar_hcall_norets(H_JOIN
);
587 } while (rc
== H_Success
&& data
->waiting
> 0);
591 if (rc
== H_Continue
) {
593 data
->args
->args
[data
->args
->nargs
] =
594 rtas_call(ibm_suspend_me_token
, 0, 1, NULL
);
596 plpar_hcall_norets(H_PROD
,i
);
598 data
->waiting
= -EBUSY
;
599 printk(KERN_ERR
"Error on H_Join hypervisor call\n");
603 local_irq_restore(flags
);
607 static int rtas_ibm_suspend_me(struct rtas_args
*args
)
611 struct rtas_suspend_me_data data
;
616 /* Call function on all CPUs. One of us will make the
619 if (on_each_cpu(rtas_percpu_suspend_me
, &data
, 1, 0))
620 data
.waiting
= -EINVAL
;
622 if (data
.waiting
!= 0)
623 printk(KERN_ERR
"Error doing global join\n");
625 /* Prod each CPU. This won't hurt, and will wake
626 * anyone we successfully put to sleep with H_Join
629 plpar_hcall_norets(H_PROD
, i
);
633 #else /* CONFIG_PPC_PSERIES */
634 static int rtas_ibm_suspend_me(struct rtas_args
*args
)
640 asmlinkage
int ppc_rtas(struct rtas_args __user
*uargs
)
642 struct rtas_args args
;
644 char *buff_copy
, *errbuf
= NULL
;
648 if (!capable(CAP_SYS_ADMIN
))
651 if (copy_from_user(&args
, uargs
, 3 * sizeof(u32
)) != 0)
655 if (nargs
> ARRAY_SIZE(args
.args
)
656 || args
.nret
> ARRAY_SIZE(args
.args
)
657 || nargs
+ args
.nret
> ARRAY_SIZE(args
.args
))
661 if (copy_from_user(args
.args
, uargs
->args
,
662 nargs
* sizeof(rtas_arg_t
)) != 0)
665 if (args
.token
== RTAS_UNKNOWN_SERVICE
)
668 /* Need to handle ibm,suspend_me call specially */
669 if (args
.token
== ibm_suspend_me_token
) {
670 rc
= rtas_ibm_suspend_me(&args
);
676 buff_copy
= get_errorlog_buffer();
678 spin_lock_irqsave(&rtas
.lock
, flags
);
681 enter_rtas(__pa(&rtas
.args
));
684 args
.rets
= &args
.args
[nargs
];
686 /* A -1 return code indicates that the last command couldn't
687 be completed due to a hardware error. */
688 if (args
.rets
[0] == -1)
689 errbuf
= __fetch_rtas_last_error(buff_copy
);
691 spin_unlock_irqrestore(&rtas
.lock
, flags
);
695 log_error(errbuf
, ERR_TYPE_RTAS_LOG
, 0);
701 if (copy_to_user(uargs
->args
+ nargs
,
703 args
.nret
* sizeof(rtas_arg_t
)) != 0)
709 /* This version can't take the spinlock, because it never returns */
711 struct rtas_args rtas_stop_self_args
= {
712 /* The token is initialized for real in setup_system() */
713 .token
= RTAS_UNKNOWN_SERVICE
,
716 .rets
= &rtas_stop_self_args
.args
[0],
719 void rtas_stop_self(void)
721 struct rtas_args
*rtas_args
= &rtas_stop_self_args
;
725 BUG_ON(rtas_args
->token
== RTAS_UNKNOWN_SERVICE
);
727 printk("cpu %u (hwid %u) Ready to die...\n",
728 smp_processor_id(), hard_smp_processor_id());
729 enter_rtas(__pa(rtas_args
));
731 panic("Alas, I survived.\n");
735 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
736 * informations from the device-tree and allocate the RMO buffer for userland
739 void __init
rtas_initialize(void)
741 unsigned long rtas_region
= RTAS_INSTANTIATE_MAX
;
743 /* Get RTAS dev node and fill up our "rtas" structure with infos
746 rtas
.dev
= of_find_node_by_name(NULL
, "rtas");
751 basep
= (u32
*)get_property(rtas
.dev
, "linux,rtas-base", NULL
);
752 sizep
= (u32
*)get_property(rtas
.dev
, "rtas-size", NULL
);
753 if (basep
!= NULL
&& sizep
!= NULL
) {
756 entryp
= (u32
*)get_property(rtas
.dev
, "linux,rtas-entry", NULL
);
757 if (entryp
== NULL
) /* Ugh */
758 rtas
.entry
= rtas
.base
;
760 rtas
.entry
= *entryp
;
767 /* If RTAS was found, allocate the RMO buffer for it and look for
768 * the stop-self token if any
771 if (_machine
== PLATFORM_PSERIES_LPAR
) {
772 rtas_region
= min(lmb
.rmo_size
, RTAS_INSTANTIATE_MAX
);
773 ibm_suspend_me_token
= rtas_token("ibm,suspend-me");
776 rtas_rmo_buf
= lmb_alloc_base(RTAS_RMOBUF_MAX
, PAGE_SIZE
, rtas_region
);
778 #ifdef CONFIG_HOTPLUG_CPU
779 rtas_stop_self_args
.token
= rtas_token("stop-self");
780 #endif /* CONFIG_HOTPLUG_CPU */
781 #ifdef CONFIG_RTAS_ERROR_LOGGING
782 rtas_last_error_token
= rtas_token("rtas-last-error");
787 EXPORT_SYMBOL(rtas_token
);
788 EXPORT_SYMBOL(rtas_call
);
789 EXPORT_SYMBOL(rtas_data_buf
);
790 EXPORT_SYMBOL(rtas_data_buf_lock
);
791 EXPORT_SYMBOL(rtas_extended_busy_delay_time
);
792 EXPORT_SYMBOL(rtas_get_sensor
);
793 EXPORT_SYMBOL(rtas_get_power_level
);
794 EXPORT_SYMBOL(rtas_set_power_level
);
795 EXPORT_SYMBOL(rtas_set_indicator
);