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.
10 #include <linux/stdarg.h>
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/sched.h>
20 #include <linux/smp.h>
21 #include <linux/completion.h>
22 #include <linux/cpumask.h>
23 #include <linux/memblock.h>
24 #include <linux/slab.h>
25 #include <linux/reboot.h>
26 #include <linux/syscalls.h>
28 #include <linux/of_fdt.h>
30 #include <asm/interrupt.h>
32 #include <asm/hvcall.h>
33 #include <asm/machdep.h>
34 #include <asm/firmware.h>
36 #include <asm/param.h>
37 #include <asm/delay.h>
38 #include <linux/uaccess.h>
40 #include <asm/syscalls.h>
42 #include <linux/atomic.h>
45 #include <asm/topology.h>
48 /* This is here deliberately so it's only used in this file */
49 void enter_rtas(unsigned long);
51 static inline void do_enter_rtas(unsigned long args)
56 * Make sure MSR[RI] is currently enabled as it will be forced later
60 BUG_ON(!(msr & MSR_RI));
62 BUG_ON(!irqs_disabled());
64 hard_irq_disable(); /* Ensure MSR[EE] is disabled on PPC64 */
68 srr_regs_clobbered(); /* rtas uses SRRs, invalidate */
71 struct rtas_t rtas = {
72 .lock = __ARCH_SPIN_LOCK_UNLOCKED
76 DEFINE_SPINLOCK(rtas_data_buf_lock);
77 EXPORT_SYMBOL(rtas_data_buf_lock);
79 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
80 EXPORT_SYMBOL(rtas_data_buf);
82 unsigned long rtas_rmo_buf;
85 * If non-NULL, this gets called when the kernel terminates.
86 * This is done like this so rtas_flash can be a module.
88 void (*rtas_flash_term_hook)(int);
89 EXPORT_SYMBOL(rtas_flash_term_hook);
91 /* RTAS use home made raw locking instead of spin_lock_irqsave
92 * because those can be called from within really nasty contexts
93 * such as having the timebase stopped which would lockup with
94 * normal locks and spinlock debugging enabled
96 static unsigned long lock_rtas(void)
100 local_irq_save(flags);
102 arch_spin_lock(&rtas.lock);
106 static void unlock_rtas(unsigned long flags)
108 arch_spin_unlock(&rtas.lock);
109 local_irq_restore(flags);
114 * call_rtas_display_status and call_rtas_display_status_delay
115 * are designed only for very early low-level debugging, which
116 * is why the token is hard-coded to 10.
118 static void call_rtas_display_status(unsigned char c)
126 rtas_call_unlocked(&rtas.args, 10, 1, 1, NULL, c);
130 static void call_rtas_display_status_delay(char c)
132 static int pending_newline = 0; /* did last write end with unprinted newline? */
133 static int width = 16;
137 call_rtas_display_status(' ');
142 if (pending_newline) {
143 call_rtas_display_status('\r');
144 call_rtas_display_status('\n');
148 call_rtas_display_status(c);
154 void __init udbg_init_rtas_panel(void)
156 udbg_putc = call_rtas_display_status_delay;
159 #ifdef CONFIG_UDBG_RTAS_CONSOLE
161 /* If you think you're dying before early_init_dt_scan_rtas() does its
162 * work, you can hard code the token values for your firmware here and
163 * hardcode rtas.base/entry etc.
165 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
166 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
168 static void udbg_rtascon_putc(char c)
175 /* Add CRs before LFs */
177 udbg_rtascon_putc('\r');
179 /* if there is more than one character to be displayed, wait a bit */
180 for (tries = 0; tries < 16; tries++) {
181 if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
187 static int udbg_rtascon_getc_poll(void)
194 if (rtas_call(rtas_getchar_token, 0, 2, &c))
200 static int udbg_rtascon_getc(void)
204 while ((c = udbg_rtascon_getc_poll()) == -1)
211 void __init udbg_init_rtas_console(void)
213 udbg_putc = udbg_rtascon_putc;
214 udbg_getc = udbg_rtascon_getc;
215 udbg_getc_poll = udbg_rtascon_getc_poll;
217 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
219 void rtas_progress(char *s, unsigned short hex)
221 struct device_node *root;
225 static int display_character, set_indicator;
226 static int display_width, display_lines, form_feed;
227 static const int *row_width;
228 static DEFINE_SPINLOCK(progress_lock);
229 static int current_line;
230 static int pending_newline = 0; /* did last write end with unprinted newline? */
235 if (display_width == 0) {
236 display_width = 0x10;
237 if ((root = of_find_node_by_path("/rtas"))) {
238 if ((p = of_get_property(root,
239 "ibm,display-line-length", NULL)))
240 display_width = be32_to_cpu(*p);
241 if ((p = of_get_property(root,
242 "ibm,form-feed", NULL)))
243 form_feed = be32_to_cpu(*p);
244 if ((p = of_get_property(root,
245 "ibm,display-number-of-lines", NULL)))
246 display_lines = be32_to_cpu(*p);
247 row_width = of_get_property(root,
248 "ibm,display-truncation-length", NULL);
251 display_character = rtas_token("display-character");
252 set_indicator = rtas_token("set-indicator");
255 if (display_character == RTAS_UNKNOWN_SERVICE) {
256 /* use hex display if available */
257 if (set_indicator != RTAS_UNKNOWN_SERVICE)
258 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
262 spin_lock(&progress_lock);
265 * Last write ended with newline, but we didn't print it since
266 * it would just clear the bottom line of output. Print it now
269 * If no newline is pending and form feed is supported, clear the
270 * display with a form feed; otherwise, print a CR to start output
271 * at the beginning of the line.
273 if (pending_newline) {
274 rtas_call(display_character, 1, 1, NULL, '\r');
275 rtas_call(display_character, 1, 1, NULL, '\n');
280 rtas_call(display_character, 1, 1, NULL,
283 rtas_call(display_character, 1, 1, NULL, '\r');
287 width = row_width[current_line];
289 width = display_width;
292 if (*os == '\n' || *os == '\r') {
293 /* If newline is the last character, save it
294 * until next call to avoid bumping up the
297 if (*os == '\n' && !os[1]) {
300 if (current_line > display_lines-1)
301 current_line = display_lines-1;
302 spin_unlock(&progress_lock);
306 /* RTAS wants CR-LF, not just LF */
309 rtas_call(display_character, 1, 1, NULL, '\r');
310 rtas_call(display_character, 1, 1, NULL, '\n');
312 /* CR might be used to re-draw a line, so we'll
313 * leave it alone and not add LF.
315 rtas_call(display_character, 1, 1, NULL, *os);
319 width = row_width[current_line];
321 width = display_width;
324 rtas_call(display_character, 1, 1, NULL, *os);
329 /* if we overwrite the screen length */
331 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
335 spin_unlock(&progress_lock);
337 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
339 int rtas_token(const char *service)
342 if (rtas.dev == NULL)
343 return RTAS_UNKNOWN_SERVICE;
344 tokp = of_get_property(rtas.dev, service, NULL);
345 return tokp ? be32_to_cpu(*tokp) : RTAS_UNKNOWN_SERVICE;
347 EXPORT_SYMBOL(rtas_token);
349 int rtas_service_present(const char *service)
351 return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
353 EXPORT_SYMBOL(rtas_service_present);
355 #ifdef CONFIG_RTAS_ERROR_LOGGING
357 * Return the firmware-specified size of the error log buffer
358 * for all rtas calls that require an error buffer argument.
359 * This includes 'check-exception' and 'rtas-last-error'.
361 int rtas_get_error_log_max(void)
363 static int rtas_error_log_max;
364 if (rtas_error_log_max)
365 return rtas_error_log_max;
367 rtas_error_log_max = rtas_token ("rtas-error-log-max");
368 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
369 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
370 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
372 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
374 return rtas_error_log_max;
376 EXPORT_SYMBOL(rtas_get_error_log_max);
379 static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
380 static int rtas_last_error_token;
382 /** Return a copy of the detailed error text associated with the
383 * most recent failed call to rtas. Because the error text
384 * might go stale if there are any other intervening rtas calls,
385 * this routine must be called atomically with whatever produced
386 * the error (i.e. with rtas.lock still held from the previous call).
388 static char *__fetch_rtas_last_error(char *altbuf)
390 struct rtas_args err_args, save_args;
394 if (rtas_last_error_token == -1)
397 bufsz = rtas_get_error_log_max();
399 err_args.token = cpu_to_be32(rtas_last_error_token);
400 err_args.nargs = cpu_to_be32(2);
401 err_args.nret = cpu_to_be32(1);
402 err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf));
403 err_args.args[1] = cpu_to_be32(bufsz);
404 err_args.args[2] = 0;
406 save_args = rtas.args;
407 rtas.args = err_args;
409 do_enter_rtas(__pa(&rtas.args));
411 err_args = rtas.args;
412 rtas.args = save_args;
414 /* Log the error in the unlikely case that there was one. */
415 if (unlikely(err_args.args[2] == 0)) {
420 if (slab_is_available())
421 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
424 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
430 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
432 #else /* CONFIG_RTAS_ERROR_LOGGING */
433 #define __fetch_rtas_last_error(x) NULL
434 #define get_errorlog_buffer() NULL
439 va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret,
444 args->token = cpu_to_be32(token);
445 args->nargs = cpu_to_be32(nargs);
446 args->nret = cpu_to_be32(nret);
447 args->rets = &(args->args[nargs]);
449 for (i = 0; i < nargs; ++i)
450 args->args[i] = cpu_to_be32(va_arg(list, __u32));
452 for (i = 0; i < nret; ++i)
455 do_enter_rtas(__pa(args));
458 void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...)
462 va_start(list, nret);
463 va_rtas_call_unlocked(args, token, nargs, nret, list);
467 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
472 struct rtas_args *rtas_args;
473 char *buff_copy = NULL;
476 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
479 if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
486 /* We use the global rtas args buffer */
487 rtas_args = &rtas.args;
489 va_start(list, outputs);
490 va_rtas_call_unlocked(rtas_args, token, nargs, nret, list);
493 /* A -1 return code indicates that the last command couldn't
494 be completed due to a hardware error. */
495 if (be32_to_cpu(rtas_args->rets[0]) == -1)
496 buff_copy = __fetch_rtas_last_error(NULL);
498 if (nret > 1 && outputs != NULL)
499 for (i = 0; i < nret-1; ++i)
500 outputs[i] = be32_to_cpu(rtas_args->rets[i+1]);
501 ret = (nret > 0)? be32_to_cpu(rtas_args->rets[0]): 0;
506 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
507 if (slab_is_available())
512 EXPORT_SYMBOL(rtas_call);
515 * rtas_busy_delay_time() - From an RTAS status value, calculate the
516 * suggested delay time in milliseconds.
518 * @status: a value returned from rtas_call() or similar APIs which return
519 * the status of a RTAS function call.
521 * Context: Any context.
524 * * 100000 - If @status is 9905.
525 * * 10000 - If @status is 9904.
526 * * 1000 - If @status is 9903.
527 * * 100 - If @status is 9902.
528 * * 10 - If @status is 9901.
529 * * 1 - If @status is either 9900 or -2. This is "wrong" for -2, but
530 * some callers depend on this behavior, and the worst outcome
531 * is that they will delay for longer than necessary.
532 * * 0 - If @status is not a busy or extended delay value.
534 unsigned int rtas_busy_delay_time(int status)
539 if (status == RTAS_BUSY) {
541 } else if (status >= RTAS_EXTENDED_DELAY_MIN &&
542 status <= RTAS_EXTENDED_DELAY_MAX) {
543 order = status - RTAS_EXTENDED_DELAY_MIN;
544 for (ms = 1; order > 0; order--)
550 EXPORT_SYMBOL(rtas_busy_delay_time);
553 * rtas_busy_delay() - helper for RTAS busy and extended delay statuses
555 * @status: a value returned from rtas_call() or similar APIs which return
556 * the status of a RTAS function call.
558 * Context: Process context. May sleep or schedule.
561 * * true - @status is RTAS_BUSY or an extended delay hint. The
562 * caller may assume that the CPU has been yielded if necessary,
563 * and that an appropriate delay for @status has elapsed.
564 * Generally the caller should reattempt the RTAS call which
567 * * false - @status is not @RTAS_BUSY nor an extended delay hint. The
568 * caller is responsible for handling @status.
570 bool rtas_busy_delay(int status)
576 case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
578 ms = rtas_busy_delay_time(status);
580 * The extended delay hint can be as high as 100 seconds.
581 * Surely any function returning such a status is either
582 * buggy or isn't going to be significantly slowed by us
583 * polling at 1HZ. Clamp the sleep time to one second.
585 ms = clamp(ms, 1U, 1000U);
587 * The delay hint is an order-of-magnitude suggestion, not
588 * a minimum. It is fine, possibly even advantageous, for
589 * us to pause for less time than hinted. For small values,
590 * use usleep_range() to ensure we don't sleep much longer
591 * than actually needed.
593 * See Documentation/timers/timers-howto.rst for
594 * explanation of the threshold used here. In effect we use
595 * usleep_range() for 9900 and 9901, msleep() for
599 usleep_range(ms * 100, ms * 1000);
606 * We should call again immediately if there's no other
614 * Not a busy or extended delay status; the caller should
615 * handle @status itself. Ensure we warn on misuses in
616 * atomic context regardless.
624 EXPORT_SYMBOL(rtas_busy_delay);
626 static int rtas_error_rc(int rtas_rc)
631 case -1: /* Hardware Error */
634 case -3: /* Bad indicator/domain/etc */
637 case -9000: /* Isolation error */
640 case -9001: /* Outstanding TCE/PTE */
643 case -9002: /* No usable slot */
647 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
655 int rtas_get_power_level(int powerdomain, int *level)
657 int token = rtas_token("get-power-level");
660 if (token == RTAS_UNKNOWN_SERVICE)
663 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
667 return rtas_error_rc(rc);
670 EXPORT_SYMBOL(rtas_get_power_level);
672 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
674 int token = rtas_token("set-power-level");
677 if (token == RTAS_UNKNOWN_SERVICE)
681 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
682 } while (rtas_busy_delay(rc));
685 return rtas_error_rc(rc);
688 EXPORT_SYMBOL(rtas_set_power_level);
690 int rtas_get_sensor(int sensor, int index, int *state)
692 int token = rtas_token("get-sensor-state");
695 if (token == RTAS_UNKNOWN_SERVICE)
699 rc = rtas_call(token, 2, 2, state, sensor, index);
700 } while (rtas_busy_delay(rc));
703 return rtas_error_rc(rc);
706 EXPORT_SYMBOL(rtas_get_sensor);
708 int rtas_get_sensor_fast(int sensor, int index, int *state)
710 int token = rtas_token("get-sensor-state");
713 if (token == RTAS_UNKNOWN_SERVICE)
716 rc = rtas_call(token, 2, 2, state, sensor, index);
717 WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
718 rc <= RTAS_EXTENDED_DELAY_MAX));
721 return rtas_error_rc(rc);
725 bool rtas_indicator_present(int token, int *maxindex)
727 int proplen, count, i;
728 const struct indicator_elem {
733 indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
737 count = proplen / sizeof(struct indicator_elem);
739 for (i = 0; i < count; i++) {
740 if (__be32_to_cpu(indicators[i].token) != token)
743 *maxindex = __be32_to_cpu(indicators[i].maxindex);
749 EXPORT_SYMBOL(rtas_indicator_present);
751 int rtas_set_indicator(int indicator, int index, int new_value)
753 int token = rtas_token("set-indicator");
756 if (token == RTAS_UNKNOWN_SERVICE)
760 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
761 } while (rtas_busy_delay(rc));
764 return rtas_error_rc(rc);
767 EXPORT_SYMBOL(rtas_set_indicator);
770 * Ignoring RTAS extended delay
772 int rtas_set_indicator_fast(int indicator, int index, int new_value)
775 int token = rtas_token("set-indicator");
777 if (token == RTAS_UNKNOWN_SERVICE)
780 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
782 WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
783 rc <= RTAS_EXTENDED_DELAY_MAX));
786 return rtas_error_rc(rc);
792 * rtas_ibm_suspend_me() - Call ibm,suspend-me to suspend the LPAR.
794 * @fw_status: RTAS call status will be placed here if not NULL.
796 * rtas_ibm_suspend_me() should be called only on a CPU which has
797 * received H_CONTINUE from the H_JOIN hcall. All other active CPUs
798 * should be waiting to return from H_JOIN.
800 * rtas_ibm_suspend_me() may suspend execution of the OS
801 * indefinitely. Callers should take appropriate measures upon return, such as
802 * resetting watchdog facilities.
804 * Callers may choose to retry this call if @fw_status is
805 * %RTAS_THREADS_ACTIVE.
808 * 0 - The partition has resumed from suspend, possibly after
809 * migration to a different host.
810 * -ECANCELED - The operation was aborted.
811 * -EAGAIN - There were other CPUs not in H_JOIN at the time of the call.
812 * -EBUSY - Some other condition prevented the suspend from succeeding.
813 * -EIO - Hardware/platform error.
815 int rtas_ibm_suspend_me(int *fw_status)
820 fwrc = rtas_call(rtas_token("ibm,suspend-me"), 0, 1, NULL);
826 case RTAS_SUSPEND_ABORTED:
829 case RTAS_THREADS_ACTIVE:
832 case RTAS_NOT_SUSPENDABLE:
833 case RTAS_OUTSTANDING_COPROC:
848 void __noreturn rtas_restart(char *cmd)
850 if (rtas_flash_term_hook)
851 rtas_flash_term_hook(SYS_RESTART);
852 printk("RTAS system-reboot returned %d\n",
853 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
857 void rtas_power_off(void)
859 if (rtas_flash_term_hook)
860 rtas_flash_term_hook(SYS_POWER_OFF);
861 /* allow power on only with power button press */
862 printk("RTAS power-off returned %d\n",
863 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
867 void __noreturn rtas_halt(void)
869 if (rtas_flash_term_hook)
870 rtas_flash_term_hook(SYS_HALT);
871 /* allow power on only with power button press */
872 printk("RTAS power-off returned %d\n",
873 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
877 /* Must be in the RMO region, so we place it here */
878 static char rtas_os_term_buf[2048];
880 void rtas_os_term(char *str)
885 * Firmware with the ibm,extended-os-term property is guaranteed
886 * to always return from an ibm,os-term call. Earlier versions without
887 * this property may terminate the partition which we want to avoid
888 * since it interferes with panic_timeout.
890 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term") ||
891 RTAS_UNKNOWN_SERVICE == rtas_token("ibm,extended-os-term"))
894 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
897 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
898 __pa(rtas_os_term_buf));
899 } while (rtas_busy_delay(status));
902 printk(KERN_EMERG "ibm,os-term call failed %d\n", status);
906 * rtas_activate_firmware() - Activate a new version of firmware.
908 * Context: This function may sleep.
910 * Activate a new version of partition firmware. The OS must call this
911 * after resuming from a partition hibernation or migration in order
912 * to maintain the ability to perform live firmware updates. It's not
913 * catastrophic for this method to be absent or to fail; just log the
914 * condition in that case.
916 void rtas_activate_firmware(void)
921 token = rtas_token("ibm,activate-firmware");
922 if (token == RTAS_UNKNOWN_SERVICE) {
923 pr_notice("ibm,activate-firmware method unavailable\n");
928 fwrc = rtas_call(token, 0, 1, NULL);
929 } while (rtas_busy_delay(fwrc));
932 pr_err("ibm,activate-firmware failed (%i)\n", fwrc);
935 #ifdef CONFIG_PPC_PSERIES
937 * rtas_call_reentrant() - Used for reentrant rtas calls
938 * @token: Token for desired reentrant RTAS call
939 * @nargs: Number of Input Parameters
940 * @nret: Number of Output Parameters
941 * @outputs: Array of outputs
942 * @...: Inputs for desired RTAS call
944 * According to LoPAR documentation, only "ibm,int-on", "ibm,int-off",
945 * "ibm,get-xive" and "ibm,set-xive" are currently reentrant.
946 * Reentrant calls need their own rtas_args buffer, so not using rtas.args, but
949 * Return: -1 on error,
950 * First output value of RTAS call if (nret > 0),
953 int rtas_call_reentrant(int token, int nargs, int nret, int *outputs, ...)
956 struct rtas_args *args;
960 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
963 local_irq_save(flags);
966 /* We use the per-cpu (PACA) rtas args buffer */
967 args = local_paca->rtas_args_reentrant;
969 va_start(list, outputs);
970 va_rtas_call_unlocked(args, token, nargs, nret, list);
973 if (nret > 1 && outputs)
974 for (i = 0; i < nret - 1; ++i)
975 outputs[i] = be32_to_cpu(args->rets[i + 1]);
978 ret = be32_to_cpu(args->rets[0]);
980 local_irq_restore(flags);
986 #endif /* CONFIG_PPC_PSERIES */
989 * get_pseries_errorlog() - Find a specific pseries error log in an RTAS
990 * extended event log.
991 * @log: RTAS error/event log
992 * @section_id: two character section identifier
994 * Return: A pointer to the specified errorlog or NULL if not found.
996 noinstr struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
999 struct rtas_ext_event_log_v6 *ext_log =
1000 (struct rtas_ext_event_log_v6 *)log->buffer;
1001 struct pseries_errorlog *sect;
1002 unsigned char *p, *log_end;
1003 uint32_t ext_log_length = rtas_error_extended_log_length(log);
1004 uint8_t log_format = rtas_ext_event_log_format(ext_log);
1005 uint32_t company_id = rtas_ext_event_company_id(ext_log);
1007 /* Check that we understand the format */
1008 if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
1009 log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
1010 company_id != RTAS_V6EXT_COMPANY_ID_IBM)
1013 log_end = log->buffer + ext_log_length;
1014 p = ext_log->vendor_log;
1016 while (p < log_end) {
1017 sect = (struct pseries_errorlog *)p;
1018 if (pseries_errorlog_id(sect) == section_id)
1020 p += pseries_errorlog_length(sect);
1026 #ifdef CONFIG_PPC_RTAS_FILTER
1029 * The sys_rtas syscall, as originally designed, allows root to pass
1030 * arbitrary physical addresses to RTAS calls. A number of RTAS calls
1031 * can be abused to write to arbitrary memory and do other things that
1032 * are potentially harmful to system integrity, and thus should only
1033 * be used inside the kernel and not exposed to userspace.
1035 * All known legitimate users of the sys_rtas syscall will only ever
1036 * pass addresses that fall within the RMO buffer, and use a known
1037 * subset of RTAS calls.
1039 * Accordingly, we filter RTAS requests to check that the call is
1040 * permitted, and that provided pointers fall within the RMO buffer.
1041 * The rtas_filters list contains an entry for each permitted call,
1042 * with the indexes of the parameters which are expected to contain
1043 * addresses and sizes of buffers allocated inside the RMO buffer.
1045 struct rtas_filter {
1048 /* Indexes into the args buffer, -1 if not used */
1057 static struct rtas_filter rtas_filters[] __ro_after_init = {
1058 { "ibm,activate-firmware", -1, -1, -1, -1, -1 },
1059 { "ibm,configure-connector", -1, 0, -1, 1, -1, 4096 }, /* Special cased */
1060 { "display-character", -1, -1, -1, -1, -1 },
1061 { "ibm,display-message", -1, 0, -1, -1, -1 },
1062 { "ibm,errinjct", -1, 2, -1, -1, -1, 1024 },
1063 { "ibm,close-errinjct", -1, -1, -1, -1, -1 },
1064 { "ibm,open-errinjct", -1, -1, -1, -1, -1 },
1065 { "ibm,get-config-addr-info2", -1, -1, -1, -1, -1 },
1066 { "ibm,get-dynamic-sensor-state", -1, 1, -1, -1, -1 },
1067 { "ibm,get-indices", -1, 2, 3, -1, -1 },
1068 { "get-power-level", -1, -1, -1, -1, -1 },
1069 { "get-sensor-state", -1, -1, -1, -1, -1 },
1070 { "ibm,get-system-parameter", -1, 1, 2, -1, -1 },
1071 { "get-time-of-day", -1, -1, -1, -1, -1 },
1072 { "ibm,get-vpd", -1, 0, -1, 1, 2 },
1073 { "ibm,lpar-perftools", -1, 2, 3, -1, -1 },
1074 { "ibm,platform-dump", -1, 4, 5, -1, -1 },
1075 { "ibm,read-slot-reset-state", -1, -1, -1, -1, -1 },
1076 { "ibm,scan-log-dump", -1, 0, 1, -1, -1 },
1077 { "ibm,set-dynamic-indicator", -1, 2, -1, -1, -1 },
1078 { "ibm,set-eeh-option", -1, -1, -1, -1, -1 },
1079 { "set-indicator", -1, -1, -1, -1, -1 },
1080 { "set-power-level", -1, -1, -1, -1, -1 },
1081 { "set-time-for-power-on", -1, -1, -1, -1, -1 },
1082 { "ibm,set-system-parameter", -1, 1, -1, -1, -1 },
1083 { "set-time-of-day", -1, -1, -1, -1, -1 },
1084 #ifdef CONFIG_CPU_BIG_ENDIAN
1085 { "ibm,suspend-me", -1, -1, -1, -1, -1 },
1086 { "ibm,update-nodes", -1, 0, -1, -1, -1, 4096 },
1087 { "ibm,update-properties", -1, 0, -1, -1, -1, 4096 },
1089 { "ibm,physical-attestation", -1, 0, 1, -1, -1 },
1092 static bool in_rmo_buf(u32 base, u32 end)
1094 return base >= rtas_rmo_buf &&
1095 base < (rtas_rmo_buf + RTAS_USER_REGION_SIZE) &&
1097 end >= rtas_rmo_buf &&
1098 end < (rtas_rmo_buf + RTAS_USER_REGION_SIZE);
1101 static bool block_rtas_call(int token, int nargs,
1102 struct rtas_args *args)
1106 for (i = 0; i < ARRAY_SIZE(rtas_filters); i++) {
1107 struct rtas_filter *f = &rtas_filters[i];
1108 u32 base, size, end;
1110 if (token != f->token)
1113 if (f->buf_idx1 != -1) {
1114 base = be32_to_cpu(args->args[f->buf_idx1]);
1115 if (f->size_idx1 != -1)
1116 size = be32_to_cpu(args->args[f->size_idx1]);
1117 else if (f->fixed_size)
1118 size = f->fixed_size;
1122 end = base + size - 1;
1123 if (!in_rmo_buf(base, end))
1127 if (f->buf_idx2 != -1) {
1128 base = be32_to_cpu(args->args[f->buf_idx2]);
1129 if (f->size_idx2 != -1)
1130 size = be32_to_cpu(args->args[f->size_idx2]);
1131 else if (f->fixed_size)
1132 size = f->fixed_size;
1135 end = base + size - 1;
1138 * Special case for ibm,configure-connector where the
1141 if (!strcmp(f->name, "ibm,configure-connector") &&
1145 if (!in_rmo_buf(base, end))
1153 pr_err_ratelimited("sys_rtas: RTAS call blocked - exploit attempt?\n");
1154 pr_err_ratelimited("sys_rtas: token=0x%x, nargs=%d (called by %s)\n",
1155 token, nargs, current->comm);
1159 static void __init rtas_syscall_filter_init(void)
1163 for (i = 0; i < ARRAY_SIZE(rtas_filters); i++)
1164 rtas_filters[i].token = rtas_token(rtas_filters[i].name);
1169 static bool block_rtas_call(int token, int nargs,
1170 struct rtas_args *args)
1175 static void __init rtas_syscall_filter_init(void)
1179 #endif /* CONFIG_PPC_RTAS_FILTER */
1181 /* We assume to be passed big endian arguments */
1182 SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
1184 struct rtas_args args;
1185 unsigned long flags;
1186 char *buff_copy, *errbuf = NULL;
1187 int nargs, nret, token;
1189 if (!capable(CAP_SYS_ADMIN))
1195 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
1198 nargs = be32_to_cpu(args.nargs);
1199 nret = be32_to_cpu(args.nret);
1200 token = be32_to_cpu(args.token);
1202 if (nargs >= ARRAY_SIZE(args.args)
1203 || nret > ARRAY_SIZE(args.args)
1204 || nargs + nret > ARRAY_SIZE(args.args))
1208 if (copy_from_user(args.args, uargs->args,
1209 nargs * sizeof(rtas_arg_t)) != 0)
1212 if (token == RTAS_UNKNOWN_SERVICE)
1215 args.rets = &args.args[nargs];
1216 memset(args.rets, 0, nret * sizeof(rtas_arg_t));
1218 if (block_rtas_call(token, nargs, &args))
1221 /* Need to handle ibm,suspend_me call specially */
1222 if (token == rtas_token("ibm,suspend-me")) {
1225 * rtas_ibm_suspend_me assumes the streamid handle is in cpu
1226 * endian, or at least the hcall within it requires it.
1229 u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
1230 | be32_to_cpu(args.args[1]);
1231 rc = rtas_syscall_dispatch_ibm_suspend_me(handle);
1233 args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
1234 else if (rc == -EIO)
1235 args.rets[0] = cpu_to_be32(-1);
1241 buff_copy = get_errorlog_buffer();
1243 flags = lock_rtas();
1246 do_enter_rtas(__pa(&rtas.args));
1249 /* A -1 return code indicates that the last command couldn't
1250 be completed due to a hardware error. */
1251 if (be32_to_cpu(args.rets[0]) == -1)
1252 errbuf = __fetch_rtas_last_error(buff_copy);
1258 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
1263 /* Copy out args. */
1264 if (copy_to_user(uargs->args + nargs,
1266 nret * sizeof(rtas_arg_t)) != 0)
1273 * Call early during boot, before mem init, to retrieve the RTAS
1274 * information from the device-tree and allocate the RMO buffer for userland
1277 void __init rtas_initialize(void)
1279 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
1280 u32 base, size, entry;
1281 int no_base, no_size, no_entry;
1283 /* Get RTAS dev node and fill up our "rtas" structure with infos
1286 rtas.dev = of_find_node_by_name(NULL, "rtas");
1290 no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
1291 no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
1292 if (no_base || no_size) {
1293 of_node_put(rtas.dev);
1300 no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
1301 rtas.entry = no_entry ? rtas.base : entry;
1303 /* If RTAS was found, allocate the RMO buffer for it and look for
1304 * the stop-self token if any
1307 if (firmware_has_feature(FW_FEATURE_LPAR))
1308 rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
1310 rtas_rmo_buf = memblock_phys_alloc_range(RTAS_USER_REGION_SIZE, PAGE_SIZE,
1313 panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
1314 PAGE_SIZE, &rtas_region);
1316 #ifdef CONFIG_RTAS_ERROR_LOGGING
1317 rtas_last_error_token = rtas_token("rtas-last-error");
1320 rtas_syscall_filter_init();
1323 int __init early_init_dt_scan_rtas(unsigned long node,
1324 const char *uname, int depth, void *data)
1326 const u32 *basep, *entryp, *sizep;
1328 if (depth != 1 || strcmp(uname, "rtas") != 0)
1331 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1332 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1333 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
1336 /* need this feature to decide the crashkernel offset */
1337 if (of_get_flat_dt_prop(node, "ibm,hypertas-functions", NULL))
1338 powerpc_firmware_features |= FW_FEATURE_LPAR;
1341 if (basep && entryp && sizep) {
1343 rtas.entry = *entryp;
1347 #ifdef CONFIG_UDBG_RTAS_CONSOLE
1348 basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
1350 rtas_putchar_token = *basep;
1352 basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
1354 rtas_getchar_token = *basep;
1356 if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
1357 rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
1358 udbg_init_rtas_console();
1366 static arch_spinlock_t timebase_lock;
1367 static u64 timebase = 0;
1369 void rtas_give_timebase(void)
1371 unsigned long flags;
1373 local_irq_save(flags);
1375 arch_spin_lock(&timebase_lock);
1376 rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
1377 timebase = get_tb();
1378 arch_spin_unlock(&timebase_lock);
1382 rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
1383 local_irq_restore(flags);
1386 void rtas_take_timebase(void)
1390 arch_spin_lock(&timebase_lock);
1391 set_tb(timebase >> 32, timebase & 0xffffffff);
1393 arch_spin_unlock(&timebase_lock);