1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Procedures for interfacing to the RTAS on CHRP machines.
6 * Peter Bergner, IBM March 2001.
7 * Copyright (C) 2001 IBM.
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/spinlock.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/capability.h>
17 #include <linux/delay.h>
18 #include <linux/cpu.h>
19 #include <linux/smp.h>
20 #include <linux/completion.h>
21 #include <linux/cpumask.h>
22 #include <linux/memblock.h>
23 #include <linux/slab.h>
24 #include <linux/reboot.h>
25 #include <linux/syscalls.h>
29 #include <asm/hvcall.h>
30 #include <asm/machdep.h>
31 #include <asm/firmware.h>
33 #include <asm/param.h>
34 #include <asm/delay.h>
35 #include <linux/uaccess.h>
37 #include <asm/syscalls.h>
39 #include <linux/atomic.h>
42 #include <asm/topology.h>
44 /* This is here deliberately so it's only used in this file */
45 void enter_rtas(unsigned long);
47 struct rtas_t rtas = {
48 .lock = __ARCH_SPIN_LOCK_UNLOCKED
52 DEFINE_SPINLOCK(rtas_data_buf_lock);
53 EXPORT_SYMBOL(rtas_data_buf_lock);
55 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
56 EXPORT_SYMBOL(rtas_data_buf);
58 unsigned long rtas_rmo_buf;
61 * If non-NULL, this gets called when the kernel terminates.
62 * This is done like this so rtas_flash can be a module.
64 void (*rtas_flash_term_hook)(int);
65 EXPORT_SYMBOL(rtas_flash_term_hook);
67 /* RTAS use home made raw locking instead of spin_lock_irqsave
68 * because those can be called from within really nasty contexts
69 * such as having the timebase stopped which would lockup with
70 * normal locks and spinlock debugging enabled
72 static unsigned long lock_rtas(void)
76 local_irq_save(flags);
78 arch_spin_lock(&rtas.lock);
82 static void unlock_rtas(unsigned long flags)
84 arch_spin_unlock(&rtas.lock);
85 local_irq_restore(flags);
90 * call_rtas_display_status and call_rtas_display_status_delay
91 * are designed only for very early low-level debugging, which
92 * is why the token is hard-coded to 10.
94 static void call_rtas_display_status(unsigned char c)
102 rtas_call_unlocked(&rtas.args, 10, 1, 1, NULL, c);
106 static void call_rtas_display_status_delay(char c)
108 static int pending_newline = 0; /* did last write end with unprinted newline? */
109 static int width = 16;
113 call_rtas_display_status(' ');
118 if (pending_newline) {
119 call_rtas_display_status('\r');
120 call_rtas_display_status('\n');
124 call_rtas_display_status(c);
130 void __init udbg_init_rtas_panel(void)
132 udbg_putc = call_rtas_display_status_delay;
135 #ifdef CONFIG_UDBG_RTAS_CONSOLE
137 /* If you think you're dying before early_init_dt_scan_rtas() does its
138 * work, you can hard code the token values for your firmware here and
139 * hardcode rtas.base/entry etc.
141 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
142 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
144 static void udbg_rtascon_putc(char c)
151 /* Add CRs before LFs */
153 udbg_rtascon_putc('\r');
155 /* if there is more than one character to be displayed, wait a bit */
156 for (tries = 0; tries < 16; tries++) {
157 if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
163 static int udbg_rtascon_getc_poll(void)
170 if (rtas_call(rtas_getchar_token, 0, 2, &c))
176 static int udbg_rtascon_getc(void)
180 while ((c = udbg_rtascon_getc_poll()) == -1)
187 void __init udbg_init_rtas_console(void)
189 udbg_putc = udbg_rtascon_putc;
190 udbg_getc = udbg_rtascon_getc;
191 udbg_getc_poll = udbg_rtascon_getc_poll;
193 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
195 void rtas_progress(char *s, unsigned short hex)
197 struct device_node *root;
201 static int display_character, set_indicator;
202 static int display_width, display_lines, form_feed;
203 static const int *row_width;
204 static DEFINE_SPINLOCK(progress_lock);
205 static int current_line;
206 static int pending_newline = 0; /* did last write end with unprinted newline? */
211 if (display_width == 0) {
212 display_width = 0x10;
213 if ((root = of_find_node_by_path("/rtas"))) {
214 if ((p = of_get_property(root,
215 "ibm,display-line-length", NULL)))
216 display_width = be32_to_cpu(*p);
217 if ((p = of_get_property(root,
218 "ibm,form-feed", NULL)))
219 form_feed = be32_to_cpu(*p);
220 if ((p = of_get_property(root,
221 "ibm,display-number-of-lines", NULL)))
222 display_lines = be32_to_cpu(*p);
223 row_width = of_get_property(root,
224 "ibm,display-truncation-length", NULL);
227 display_character = rtas_token("display-character");
228 set_indicator = rtas_token("set-indicator");
231 if (display_character == RTAS_UNKNOWN_SERVICE) {
232 /* use hex display if available */
233 if (set_indicator != RTAS_UNKNOWN_SERVICE)
234 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
238 spin_lock(&progress_lock);
241 * Last write ended with newline, but we didn't print it since
242 * it would just clear the bottom line of output. Print it now
245 * If no newline is pending and form feed is supported, clear the
246 * display with a form feed; otherwise, print a CR to start output
247 * at the beginning of the line.
249 if (pending_newline) {
250 rtas_call(display_character, 1, 1, NULL, '\r');
251 rtas_call(display_character, 1, 1, NULL, '\n');
256 rtas_call(display_character, 1, 1, NULL,
259 rtas_call(display_character, 1, 1, NULL, '\r');
263 width = row_width[current_line];
265 width = display_width;
268 if (*os == '\n' || *os == '\r') {
269 /* If newline is the last character, save it
270 * until next call to avoid bumping up the
273 if (*os == '\n' && !os[1]) {
276 if (current_line > display_lines-1)
277 current_line = display_lines-1;
278 spin_unlock(&progress_lock);
282 /* RTAS wants CR-LF, not just LF */
285 rtas_call(display_character, 1, 1, NULL, '\r');
286 rtas_call(display_character, 1, 1, NULL, '\n');
288 /* CR might be used to re-draw a line, so we'll
289 * leave it alone and not add LF.
291 rtas_call(display_character, 1, 1, NULL, *os);
295 width = row_width[current_line];
297 width = display_width;
300 rtas_call(display_character, 1, 1, NULL, *os);
305 /* if we overwrite the screen length */
307 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
311 spin_unlock(&progress_lock);
313 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
315 int rtas_token(const char *service)
318 if (rtas.dev == NULL)
319 return RTAS_UNKNOWN_SERVICE;
320 tokp = of_get_property(rtas.dev, service, NULL);
321 return tokp ? be32_to_cpu(*tokp) : RTAS_UNKNOWN_SERVICE;
323 EXPORT_SYMBOL(rtas_token);
325 int rtas_service_present(const char *service)
327 return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
329 EXPORT_SYMBOL(rtas_service_present);
331 #ifdef CONFIG_RTAS_ERROR_LOGGING
333 * Return the firmware-specified size of the error log buffer
334 * for all rtas calls that require an error buffer argument.
335 * This includes 'check-exception' and 'rtas-last-error'.
337 int rtas_get_error_log_max(void)
339 static int rtas_error_log_max;
340 if (rtas_error_log_max)
341 return rtas_error_log_max;
343 rtas_error_log_max = rtas_token ("rtas-error-log-max");
344 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
345 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
346 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
348 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
350 return rtas_error_log_max;
352 EXPORT_SYMBOL(rtas_get_error_log_max);
355 static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
356 static int rtas_last_error_token;
358 /** Return a copy of the detailed error text associated with the
359 * most recent failed call to rtas. Because the error text
360 * might go stale if there are any other intervening rtas calls,
361 * this routine must be called atomically with whatever produced
362 * the error (i.e. with rtas.lock still held from the previous call).
364 static char *__fetch_rtas_last_error(char *altbuf)
366 struct rtas_args err_args, save_args;
370 if (rtas_last_error_token == -1)
373 bufsz = rtas_get_error_log_max();
375 err_args.token = cpu_to_be32(rtas_last_error_token);
376 err_args.nargs = cpu_to_be32(2);
377 err_args.nret = cpu_to_be32(1);
378 err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf));
379 err_args.args[1] = cpu_to_be32(bufsz);
380 err_args.args[2] = 0;
382 save_args = rtas.args;
383 rtas.args = err_args;
385 enter_rtas(__pa(&rtas.args));
387 err_args = rtas.args;
388 rtas.args = save_args;
390 /* Log the error in the unlikely case that there was one. */
391 if (unlikely(err_args.args[2] == 0)) {
396 if (slab_is_available())
397 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
400 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
406 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
408 #else /* CONFIG_RTAS_ERROR_LOGGING */
409 #define __fetch_rtas_last_error(x) NULL
410 #define get_errorlog_buffer() NULL
415 va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret,
420 args->token = cpu_to_be32(token);
421 args->nargs = cpu_to_be32(nargs);
422 args->nret = cpu_to_be32(nret);
423 args->rets = &(args->args[nargs]);
425 for (i = 0; i < nargs; ++i)
426 args->args[i] = cpu_to_be32(va_arg(list, __u32));
428 for (i = 0; i < nret; ++i)
431 enter_rtas(__pa(args));
434 void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...)
438 va_start(list, nret);
439 va_rtas_call_unlocked(args, token, nargs, nret, list);
443 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
448 struct rtas_args *rtas_args;
449 char *buff_copy = NULL;
452 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
457 /* We use the global rtas args buffer */
458 rtas_args = &rtas.args;
460 va_start(list, outputs);
461 va_rtas_call_unlocked(rtas_args, token, nargs, nret, list);
464 /* A -1 return code indicates that the last command couldn't
465 be completed due to a hardware error. */
466 if (be32_to_cpu(rtas_args->rets[0]) == -1)
467 buff_copy = __fetch_rtas_last_error(NULL);
469 if (nret > 1 && outputs != NULL)
470 for (i = 0; i < nret-1; ++i)
471 outputs[i] = be32_to_cpu(rtas_args->rets[i+1]);
472 ret = (nret > 0)? be32_to_cpu(rtas_args->rets[0]): 0;
477 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
478 if (slab_is_available())
483 EXPORT_SYMBOL(rtas_call);
485 /* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
486 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
488 unsigned int rtas_busy_delay_time(int status)
493 if (status == RTAS_BUSY) {
495 } else if (status >= RTAS_EXTENDED_DELAY_MIN &&
496 status <= RTAS_EXTENDED_DELAY_MAX) {
497 order = status - RTAS_EXTENDED_DELAY_MIN;
498 for (ms = 1; order > 0; order--)
504 EXPORT_SYMBOL(rtas_busy_delay_time);
506 /* For an RTAS busy status code, perform the hinted delay. */
507 unsigned int rtas_busy_delay(int status)
512 ms = rtas_busy_delay_time(status);
513 if (ms && need_resched())
518 EXPORT_SYMBOL(rtas_busy_delay);
520 static int rtas_error_rc(int rtas_rc)
525 case -1: /* Hardware Error */
528 case -3: /* Bad indicator/domain/etc */
531 case -9000: /* Isolation error */
534 case -9001: /* Outstanding TCE/PTE */
537 case -9002: /* No usable slot */
541 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
549 int rtas_get_power_level(int powerdomain, int *level)
551 int token = rtas_token("get-power-level");
554 if (token == RTAS_UNKNOWN_SERVICE)
557 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
561 return rtas_error_rc(rc);
564 EXPORT_SYMBOL(rtas_get_power_level);
566 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
568 int token = rtas_token("set-power-level");
571 if (token == RTAS_UNKNOWN_SERVICE)
575 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
576 } while (rtas_busy_delay(rc));
579 return rtas_error_rc(rc);
582 EXPORT_SYMBOL(rtas_set_power_level);
584 int rtas_get_sensor(int sensor, int index, int *state)
586 int token = rtas_token("get-sensor-state");
589 if (token == RTAS_UNKNOWN_SERVICE)
593 rc = rtas_call(token, 2, 2, state, sensor, index);
594 } while (rtas_busy_delay(rc));
597 return rtas_error_rc(rc);
600 EXPORT_SYMBOL(rtas_get_sensor);
602 int rtas_get_sensor_fast(int sensor, int index, int *state)
604 int token = rtas_token("get-sensor-state");
607 if (token == RTAS_UNKNOWN_SERVICE)
610 rc = rtas_call(token, 2, 2, state, sensor, index);
611 WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
612 rc <= RTAS_EXTENDED_DELAY_MAX));
615 return rtas_error_rc(rc);
619 bool rtas_indicator_present(int token, int *maxindex)
621 int proplen, count, i;
622 const struct indicator_elem {
627 indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
631 count = proplen / sizeof(struct indicator_elem);
633 for (i = 0; i < count; i++) {
634 if (__be32_to_cpu(indicators[i].token) != token)
637 *maxindex = __be32_to_cpu(indicators[i].maxindex);
643 EXPORT_SYMBOL(rtas_indicator_present);
645 int rtas_set_indicator(int indicator, int index, int new_value)
647 int token = rtas_token("set-indicator");
650 if (token == RTAS_UNKNOWN_SERVICE)
654 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
655 } while (rtas_busy_delay(rc));
658 return rtas_error_rc(rc);
661 EXPORT_SYMBOL(rtas_set_indicator);
664 * Ignoring RTAS extended delay
666 int rtas_set_indicator_fast(int indicator, int index, int new_value)
669 int token = rtas_token("set-indicator");
671 if (token == RTAS_UNKNOWN_SERVICE)
674 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
676 WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
677 rc <= RTAS_EXTENDED_DELAY_MAX));
680 return rtas_error_rc(rc);
685 void __noreturn rtas_restart(char *cmd)
687 if (rtas_flash_term_hook)
688 rtas_flash_term_hook(SYS_RESTART);
689 printk("RTAS system-reboot returned %d\n",
690 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
694 void rtas_power_off(void)
696 if (rtas_flash_term_hook)
697 rtas_flash_term_hook(SYS_POWER_OFF);
698 /* allow power on only with power button press */
699 printk("RTAS power-off returned %d\n",
700 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
704 void __noreturn rtas_halt(void)
706 if (rtas_flash_term_hook)
707 rtas_flash_term_hook(SYS_HALT);
708 /* allow power on only with power button press */
709 printk("RTAS power-off returned %d\n",
710 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
714 /* Must be in the RMO region, so we place it here */
715 static char rtas_os_term_buf[2048];
717 void rtas_os_term(char *str)
722 * Firmware with the ibm,extended-os-term property is guaranteed
723 * to always return from an ibm,os-term call. Earlier versions without
724 * this property may terminate the partition which we want to avoid
725 * since it interferes with panic_timeout.
727 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term") ||
728 RTAS_UNKNOWN_SERVICE == rtas_token("ibm,extended-os-term"))
731 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
734 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
735 __pa(rtas_os_term_buf));
736 } while (rtas_busy_delay(status));
739 printk(KERN_EMERG "ibm,os-term call failed %d\n", status);
742 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
743 #ifdef CONFIG_PPC_PSERIES
744 static int __rtas_suspend_last_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
746 u16 slb_size = mmu_slb_size;
747 int rc = H_MULTI_THREADS_ACTIVE;
750 slb_set_size(SLB_MIN_SIZE);
751 printk(KERN_DEBUG "calling ibm,suspend-me on cpu %i\n", smp_processor_id());
753 while (rc == H_MULTI_THREADS_ACTIVE && !atomic_read(&data->done) &&
754 !atomic_read(&data->error))
755 rc = rtas_call(data->token, 0, 1, NULL);
757 if (rc || atomic_read(&data->error)) {
758 printk(KERN_DEBUG "ibm,suspend-me returned %d\n", rc);
759 slb_set_size(slb_size);
762 if (atomic_read(&data->error))
763 rc = atomic_read(&data->error);
765 atomic_set(&data->error, rc);
766 pSeries_coalesce_init();
768 if (wake_when_done) {
769 atomic_set(&data->done, 1);
771 for_each_online_cpu(cpu)
772 plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
775 if (atomic_dec_return(&data->working) == 0)
776 complete(data->complete);
781 int rtas_suspend_last_cpu(struct rtas_suspend_me_data *data)
783 atomic_inc(&data->working);
784 return __rtas_suspend_last_cpu(data, 0);
787 static int __rtas_suspend_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
790 unsigned long msr_save;
793 atomic_inc(&data->working);
795 /* really need to ensure MSR.EE is off for H_JOIN */
797 mtmsr(msr_save & ~(MSR_EE));
799 while (rc == H_SUCCESS && !atomic_read(&data->done) && !atomic_read(&data->error))
800 rc = plpar_hcall_norets(H_JOIN);
804 if (rc == H_SUCCESS) {
805 /* This cpu was prodded and the suspend is complete. */
807 } else if (rc == H_CONTINUE) {
808 /* All other cpus are in H_JOIN, this cpu does
811 return __rtas_suspend_last_cpu(data, wake_when_done);
813 printk(KERN_ERR "H_JOIN on cpu %i failed with rc = %ld\n",
814 smp_processor_id(), rc);
815 atomic_set(&data->error, rc);
818 if (wake_when_done) {
819 atomic_set(&data->done, 1);
821 /* This cpu did the suspend or got an error; in either case,
822 * we need to prod all other other cpus out of join state.
823 * Extra prods are harmless.
825 for_each_online_cpu(cpu)
826 plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
829 if (atomic_dec_return(&data->working) == 0)
830 complete(data->complete);
834 int rtas_suspend_cpu(struct rtas_suspend_me_data *data)
836 return __rtas_suspend_cpu(data, 0);
839 static void rtas_percpu_suspend_me(void *info)
841 __rtas_suspend_cpu((struct rtas_suspend_me_data *)info, 1);
844 enum rtas_cpu_state {
850 static int rtas_cpu_state_change_mask(enum rtas_cpu_state state,
853 if (!cpumask_empty(cpus)) {
860 /* On return cpumask will be altered to indicate CPUs changed.
861 * CPUs with states changed will be set in the mask,
862 * CPUs with status unchanged will be unset in the mask. */
863 static int rtas_cpu_state_change_mask(enum rtas_cpu_state state,
870 if (cpumask_empty(cpus))
873 for_each_cpu(cpu, cpus) {
876 cpuret = cpu_down(cpu);
879 cpuret = cpu_up(cpu);
883 pr_debug("%s: cpu_%s for cpu#%d returned %d.\n",
885 ((state == UP) ? "up" : "down"),
890 /* clear bits for unchanged cpus, return */
891 cpumask_shift_right(cpus, cpus, cpu);
892 cpumask_shift_left(cpus, cpus, cpu);
895 /* clear bit for unchanged cpu, continue */
896 cpumask_clear_cpu(cpu, cpus);
905 int rtas_online_cpus_mask(cpumask_var_t cpus)
909 ret = rtas_cpu_state_change_mask(UP, cpus);
912 cpumask_var_t tmp_mask;
914 if (!alloc_cpumask_var(&tmp_mask, GFP_KERNEL))
917 /* Use tmp_mask to preserve cpus mask from first failure */
918 cpumask_copy(tmp_mask, cpus);
919 rtas_offline_cpus_mask(tmp_mask);
920 free_cpumask_var(tmp_mask);
925 EXPORT_SYMBOL(rtas_online_cpus_mask);
927 int rtas_offline_cpus_mask(cpumask_var_t cpus)
929 return rtas_cpu_state_change_mask(DOWN, cpus);
931 EXPORT_SYMBOL(rtas_offline_cpus_mask);
933 int rtas_ibm_suspend_me(u64 handle)
937 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
938 struct rtas_suspend_me_data data;
939 DECLARE_COMPLETION_ONSTACK(done);
940 cpumask_var_t offline_mask;
943 if (!rtas_service_present("ibm,suspend-me"))
946 /* Make sure the state is valid */
947 rc = plpar_hcall(H_VASI_STATE, retbuf, handle);
952 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
954 } else if (state == H_VASI_ENABLED) {
956 } else if (state != H_VASI_SUSPENDING) {
957 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
962 if (!alloc_cpumask_var(&offline_mask, GFP_KERNEL))
965 atomic_set(&data.working, 0);
966 atomic_set(&data.done, 0);
967 atomic_set(&data.error, 0);
968 data.token = rtas_token("ibm,suspend-me");
969 data.complete = &done;
971 /* All present CPUs must be online */
972 cpumask_andnot(offline_mask, cpu_present_mask, cpu_online_mask);
973 cpuret = rtas_online_cpus_mask(offline_mask);
975 pr_err("%s: Could not bring present CPUs online.\n", __func__);
976 atomic_set(&data.error, cpuret);
980 cpu_hotplug_disable();
982 /* Check if we raced with a CPU-Offline Operation */
983 if (!cpumask_equal(cpu_present_mask, cpu_online_mask)) {
984 pr_info("%s: Raced against a concurrent CPU-Offline\n", __func__);
985 atomic_set(&data.error, -EAGAIN);
986 goto out_hotplug_enable;
989 /* Call function on all CPUs. One of us will make the
992 on_each_cpu(rtas_percpu_suspend_me, &data, 0);
994 wait_for_completion(&done);
996 if (atomic_read(&data.error) != 0)
997 printk(KERN_ERR "Error doing global join\n");
1000 cpu_hotplug_enable();
1002 /* Take down CPUs not online prior to suspend */
1003 cpuret = rtas_offline_cpus_mask(offline_mask);
1005 pr_warn("%s: Could not restore CPUs to offline state.\n",
1009 free_cpumask_var(offline_mask);
1010 return atomic_read(&data.error);
1012 #else /* CONFIG_PPC_PSERIES */
1013 int rtas_ibm_suspend_me(u64 handle)
1020 * Find a specific pseries error log in an RTAS extended event log.
1021 * @log: RTAS error/event log
1022 * @section_id: two character section identifier
1024 * Returns a pointer to the specified errorlog or NULL if not found.
1026 struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
1027 uint16_t section_id)
1029 struct rtas_ext_event_log_v6 *ext_log =
1030 (struct rtas_ext_event_log_v6 *)log->buffer;
1031 struct pseries_errorlog *sect;
1032 unsigned char *p, *log_end;
1033 uint32_t ext_log_length = rtas_error_extended_log_length(log);
1034 uint8_t log_format = rtas_ext_event_log_format(ext_log);
1035 uint32_t company_id = rtas_ext_event_company_id(ext_log);
1037 /* Check that we understand the format */
1038 if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
1039 log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
1040 company_id != RTAS_V6EXT_COMPANY_ID_IBM)
1043 log_end = log->buffer + ext_log_length;
1044 p = ext_log->vendor_log;
1046 while (p < log_end) {
1047 sect = (struct pseries_errorlog *)p;
1048 if (pseries_errorlog_id(sect) == section_id)
1050 p += pseries_errorlog_length(sect);
1056 /* We assume to be passed big endian arguments */
1057 SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
1059 struct rtas_args args;
1060 unsigned long flags;
1061 char *buff_copy, *errbuf = NULL;
1062 int nargs, nret, token;
1064 if (!capable(CAP_SYS_ADMIN))
1070 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
1073 nargs = be32_to_cpu(args.nargs);
1074 nret = be32_to_cpu(args.nret);
1075 token = be32_to_cpu(args.token);
1077 if (nargs >= ARRAY_SIZE(args.args)
1078 || nret > ARRAY_SIZE(args.args)
1079 || nargs + nret > ARRAY_SIZE(args.args))
1083 if (copy_from_user(args.args, uargs->args,
1084 nargs * sizeof(rtas_arg_t)) != 0)
1087 if (token == RTAS_UNKNOWN_SERVICE)
1090 args.rets = &args.args[nargs];
1091 memset(args.rets, 0, nret * sizeof(rtas_arg_t));
1093 /* Need to handle ibm,suspend_me call specially */
1094 if (token == ibm_suspend_me_token) {
1097 * rtas_ibm_suspend_me assumes the streamid handle is in cpu
1098 * endian, or at least the hcall within it requires it.
1101 u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
1102 | be32_to_cpu(args.args[1]);
1103 rc = rtas_ibm_suspend_me(handle);
1105 args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
1106 else if (rc == -EIO)
1107 args.rets[0] = cpu_to_be32(-1);
1113 buff_copy = get_errorlog_buffer();
1115 flags = lock_rtas();
1118 enter_rtas(__pa(&rtas.args));
1121 /* A -1 return code indicates that the last command couldn't
1122 be completed due to a hardware error. */
1123 if (be32_to_cpu(args.rets[0]) == -1)
1124 errbuf = __fetch_rtas_last_error(buff_copy);
1130 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
1135 /* Copy out args. */
1136 if (copy_to_user(uargs->args + nargs,
1138 nret * sizeof(rtas_arg_t)) != 0)
1145 * Call early during boot, before mem init, to retrieve the RTAS
1146 * information from the device-tree and allocate the RMO buffer for userland
1149 void __init rtas_initialize(void)
1151 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
1152 u32 base, size, entry;
1153 int no_base, no_size, no_entry;
1155 /* Get RTAS dev node and fill up our "rtas" structure with infos
1158 rtas.dev = of_find_node_by_name(NULL, "rtas");
1162 no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
1163 no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
1164 if (no_base || no_size) {
1165 of_node_put(rtas.dev);
1172 no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
1173 rtas.entry = no_entry ? rtas.base : entry;
1175 /* If RTAS was found, allocate the RMO buffer for it and look for
1176 * the stop-self token if any
1179 if (firmware_has_feature(FW_FEATURE_LPAR)) {
1180 rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
1181 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
1184 rtas_rmo_buf = memblock_phys_alloc_range(RTAS_RMOBUF_MAX, PAGE_SIZE,
1187 panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
1188 PAGE_SIZE, &rtas_region);
1190 #ifdef CONFIG_RTAS_ERROR_LOGGING
1191 rtas_last_error_token = rtas_token("rtas-last-error");
1195 int __init early_init_dt_scan_rtas(unsigned long node,
1196 const char *uname, int depth, void *data)
1198 const u32 *basep, *entryp, *sizep;
1200 if (depth != 1 || strcmp(uname, "rtas") != 0)
1203 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1204 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1205 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
1207 if (basep && entryp && sizep) {
1209 rtas.entry = *entryp;
1213 #ifdef CONFIG_UDBG_RTAS_CONSOLE
1214 basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
1216 rtas_putchar_token = *basep;
1218 basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
1220 rtas_getchar_token = *basep;
1222 if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
1223 rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
1224 udbg_init_rtas_console();
1232 static arch_spinlock_t timebase_lock;
1233 static u64 timebase = 0;
1235 void rtas_give_timebase(void)
1237 unsigned long flags;
1239 local_irq_save(flags);
1241 arch_spin_lock(&timebase_lock);
1242 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
1243 timebase = get_tb();
1244 arch_spin_unlock(&timebase_lock);
1248 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
1249 local_irq_restore(flags);
1252 void rtas_take_timebase(void)
1256 arch_spin_lock(&timebase_lock);
1257 set_tb(timebase >> 32, timebase & 0xffffffff);
1259 arch_spin_unlock(&timebase_lock);