xref: /openbmc/linux/arch/powerpc/kernel/rtas.c (revision c67a0e41)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * Procedures for interfacing to the RTAS on CHRP machines.
5  *
6  * Peter Bergner, IBM	March 2001.
7  * Copyright (C) 2001 IBM.
8  */
9 
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/security.h>
27 #include <linux/syscalls.h>
28 #include <linux/of.h>
29 #include <linux/of_fdt.h>
30 
31 #include <asm/interrupt.h>
32 #include <asm/rtas.h>
33 #include <asm/hvcall.h>
34 #include <asm/machdep.h>
35 #include <asm/firmware.h>
36 #include <asm/page.h>
37 #include <asm/param.h>
38 #include <asm/delay.h>
39 #include <linux/uaccess.h>
40 #include <asm/udbg.h>
41 #include <asm/syscalls.h>
42 #include <asm/smp.h>
43 #include <linux/atomic.h>
44 #include <asm/time.h>
45 #include <asm/mmu.h>
46 #include <asm/topology.h>
47 
48 /* This is here deliberately so it's only used in this file */
49 void enter_rtas(unsigned long);
50 
51 static inline void do_enter_rtas(unsigned long args)
52 {
53 	unsigned long msr;
54 
55 	/*
56 	 * Make sure MSR[RI] is currently enabled as it will be forced later
57 	 * in enter_rtas.
58 	 */
59 	msr = mfmsr();
60 	BUG_ON(!(msr & MSR_RI));
61 
62 	BUG_ON(!irqs_disabled());
63 
64 	hard_irq_disable(); /* Ensure MSR[EE] is disabled on PPC64 */
65 
66 	enter_rtas(args);
67 
68 	srr_regs_clobbered(); /* rtas uses SRRs, invalidate */
69 }
70 
71 struct rtas_t rtas = {
72 	.lock = __ARCH_SPIN_LOCK_UNLOCKED
73 };
74 EXPORT_SYMBOL(rtas);
75 
76 DEFINE_SPINLOCK(rtas_data_buf_lock);
77 EXPORT_SYMBOL(rtas_data_buf_lock);
78 
79 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
80 EXPORT_SYMBOL(rtas_data_buf);
81 
82 unsigned long rtas_rmo_buf;
83 
84 /*
85  * If non-NULL, this gets called when the kernel terminates.
86  * This is done like this so rtas_flash can be a module.
87  */
88 void (*rtas_flash_term_hook)(int);
89 EXPORT_SYMBOL(rtas_flash_term_hook);
90 
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
95  */
96 static unsigned long lock_rtas(void)
97 {
98 	unsigned long flags;
99 
100 	local_irq_save(flags);
101 	preempt_disable();
102 	arch_spin_lock(&rtas.lock);
103 	return flags;
104 }
105 
106 static void unlock_rtas(unsigned long flags)
107 {
108 	arch_spin_unlock(&rtas.lock);
109 	local_irq_restore(flags);
110 	preempt_enable();
111 }
112 
113 /*
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.
117  */
118 static void call_rtas_display_status(unsigned char c)
119 {
120 	unsigned long s;
121 
122 	if (!rtas.base)
123 		return;
124 
125 	s = lock_rtas();
126 	rtas_call_unlocked(&rtas.args, 10, 1, 1, NULL, c);
127 	unlock_rtas(s);
128 }
129 
130 static void call_rtas_display_status_delay(char c)
131 {
132 	static int pending_newline = 0;  /* did last write end with unprinted newline? */
133 	static int width = 16;
134 
135 	if (c == '\n') {
136 		while (width-- > 0)
137 			call_rtas_display_status(' ');
138 		width = 16;
139 		mdelay(500);
140 		pending_newline = 1;
141 	} else {
142 		if (pending_newline) {
143 			call_rtas_display_status('\r');
144 			call_rtas_display_status('\n');
145 		}
146 		pending_newline = 0;
147 		if (width--) {
148 			call_rtas_display_status(c);
149 			udelay(10000);
150 		}
151 	}
152 }
153 
154 void __init udbg_init_rtas_panel(void)
155 {
156 	udbg_putc = call_rtas_display_status_delay;
157 }
158 
159 #ifdef CONFIG_UDBG_RTAS_CONSOLE
160 
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.
164  */
165 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
166 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
167 
168 static void udbg_rtascon_putc(char c)
169 {
170 	int tries;
171 
172 	if (!rtas.base)
173 		return;
174 
175 	/* Add CRs before LFs */
176 	if (c == '\n')
177 		udbg_rtascon_putc('\r');
178 
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)
182 			break;
183 		udelay(1000);
184 	}
185 }
186 
187 static int udbg_rtascon_getc_poll(void)
188 {
189 	int c;
190 
191 	if (!rtas.base)
192 		return -1;
193 
194 	if (rtas_call(rtas_getchar_token, 0, 2, &c))
195 		return -1;
196 
197 	return c;
198 }
199 
200 static int udbg_rtascon_getc(void)
201 {
202 	int c;
203 
204 	while ((c = udbg_rtascon_getc_poll()) == -1)
205 		;
206 
207 	return c;
208 }
209 
210 
211 void __init udbg_init_rtas_console(void)
212 {
213 	udbg_putc = udbg_rtascon_putc;
214 	udbg_getc = udbg_rtascon_getc;
215 	udbg_getc_poll = udbg_rtascon_getc_poll;
216 }
217 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
218 
219 void rtas_progress(char *s, unsigned short hex)
220 {
221 	struct device_node *root;
222 	int width;
223 	const __be32 *p;
224 	char *os;
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? */
231 
232 	if (!rtas.base)
233 		return;
234 
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);
249 			of_node_put(root);
250 		}
251 		display_character = rtas_token("display-character");
252 		set_indicator = rtas_token("set-indicator");
253 	}
254 
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);
259 		return;
260 	}
261 
262 	spin_lock(&progress_lock);
263 
264 	/*
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
267 	 * instead.
268 	 *
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.
272 	 */
273 	if (pending_newline) {
274 		rtas_call(display_character, 1, 1, NULL, '\r');
275 		rtas_call(display_character, 1, 1, NULL, '\n');
276 		pending_newline = 0;
277 	} else {
278 		current_line = 0;
279 		if (form_feed)
280 			rtas_call(display_character, 1, 1, NULL,
281 				  (char)form_feed);
282 		else
283 			rtas_call(display_character, 1, 1, NULL, '\r');
284 	}
285 
286 	if (row_width)
287 		width = row_width[current_line];
288 	else
289 		width = display_width;
290 	os = s;
291 	while (*os) {
292 		if (*os == '\n' || *os == '\r') {
293 			/* If newline is the last character, save it
294 			 * until next call to avoid bumping up the
295 			 * display output.
296 			 */
297 			if (*os == '\n' && !os[1]) {
298 				pending_newline = 1;
299 				current_line++;
300 				if (current_line > display_lines-1)
301 					current_line = display_lines-1;
302 				spin_unlock(&progress_lock);
303 				return;
304 			}
305 
306 			/* RTAS wants CR-LF, not just LF */
307 
308 			if (*os == '\n') {
309 				rtas_call(display_character, 1, 1, NULL, '\r');
310 				rtas_call(display_character, 1, 1, NULL, '\n');
311 			} else {
312 				/* CR might be used to re-draw a line, so we'll
313 				 * leave it alone and not add LF.
314 				 */
315 				rtas_call(display_character, 1, 1, NULL, *os);
316 			}
317 
318 			if (row_width)
319 				width = row_width[current_line];
320 			else
321 				width = display_width;
322 		} else {
323 			width--;
324 			rtas_call(display_character, 1, 1, NULL, *os);
325 		}
326 
327 		os++;
328 
329 		/* if we overwrite the screen length */
330 		if (width <= 0)
331 			while ((*os != 0) && (*os != '\n') && (*os != '\r'))
332 				os++;
333 	}
334 
335 	spin_unlock(&progress_lock);
336 }
337 EXPORT_SYMBOL(rtas_progress);		/* needed by rtas_flash module */
338 
339 int rtas_token(const char *service)
340 {
341 	const __be32 *tokp;
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;
346 }
347 EXPORT_SYMBOL(rtas_token);
348 
349 int rtas_service_present(const char *service)
350 {
351 	return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
352 }
353 EXPORT_SYMBOL(rtas_service_present);
354 
355 #ifdef CONFIG_RTAS_ERROR_LOGGING
356 
357 static u32 rtas_error_log_max __ro_after_init = RTAS_ERROR_LOG_MAX;
358 
359 /*
360  * Return the firmware-specified size of the error log buffer
361  *  for all rtas calls that require an error buffer argument.
362  *  This includes 'check-exception' and 'rtas-last-error'.
363  */
364 int rtas_get_error_log_max(void)
365 {
366 	return rtas_error_log_max;
367 }
368 EXPORT_SYMBOL(rtas_get_error_log_max);
369 
370 static void __init init_error_log_max(void)
371 {
372 	static const char propname[] __initconst = "rtas-error-log-max";
373 	u32 max;
374 
375 	if (of_property_read_u32(rtas.dev, propname, &max)) {
376 		pr_warn("%s not found, using default of %u\n",
377 			propname, RTAS_ERROR_LOG_MAX);
378 		max = RTAS_ERROR_LOG_MAX;
379 	}
380 
381 	if (max > RTAS_ERROR_LOG_MAX) {
382 		pr_warn("%s = %u, clamping max error log size to %u\n",
383 			propname, max, RTAS_ERROR_LOG_MAX);
384 		max = RTAS_ERROR_LOG_MAX;
385 	}
386 
387 	rtas_error_log_max = max;
388 }
389 
390 
391 static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
392 static int rtas_last_error_token;
393 
394 /** Return a copy of the detailed error text associated with the
395  *  most recent failed call to rtas.  Because the error text
396  *  might go stale if there are any other intervening rtas calls,
397  *  this routine must be called atomically with whatever produced
398  *  the error (i.e. with rtas.lock still held from the previous call).
399  */
400 static char *__fetch_rtas_last_error(char *altbuf)
401 {
402 	struct rtas_args err_args, save_args;
403 	u32 bufsz;
404 	char *buf = NULL;
405 
406 	if (rtas_last_error_token == -1)
407 		return NULL;
408 
409 	bufsz = rtas_get_error_log_max();
410 
411 	err_args.token = cpu_to_be32(rtas_last_error_token);
412 	err_args.nargs = cpu_to_be32(2);
413 	err_args.nret = cpu_to_be32(1);
414 	err_args.args[0] = cpu_to_be32(__pa(rtas_err_buf));
415 	err_args.args[1] = cpu_to_be32(bufsz);
416 	err_args.args[2] = 0;
417 
418 	save_args = rtas.args;
419 	rtas.args = err_args;
420 
421 	do_enter_rtas(__pa(&rtas.args));
422 
423 	err_args = rtas.args;
424 	rtas.args = save_args;
425 
426 	/* Log the error in the unlikely case that there was one. */
427 	if (unlikely(err_args.args[2] == 0)) {
428 		if (altbuf) {
429 			buf = altbuf;
430 		} else {
431 			buf = rtas_err_buf;
432 			if (slab_is_available())
433 				buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
434 		}
435 		if (buf)
436 			memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
437 	}
438 
439 	return buf;
440 }
441 
442 #define get_errorlog_buffer()	kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
443 
444 #else /* CONFIG_RTAS_ERROR_LOGGING */
445 #define __fetch_rtas_last_error(x)	NULL
446 #define get_errorlog_buffer()		NULL
447 static void __init init_error_log_max(void) {}
448 #endif
449 
450 
451 static void
452 va_rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret,
453 		      va_list list)
454 {
455 	int i;
456 
457 	args->token = cpu_to_be32(token);
458 	args->nargs = cpu_to_be32(nargs);
459 	args->nret  = cpu_to_be32(nret);
460 	args->rets  = &(args->args[nargs]);
461 
462 	for (i = 0; i < nargs; ++i)
463 		args->args[i] = cpu_to_be32(va_arg(list, __u32));
464 
465 	for (i = 0; i < nret; ++i)
466 		args->rets[i] = 0;
467 
468 	do_enter_rtas(__pa(args));
469 }
470 
471 void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...)
472 {
473 	va_list list;
474 
475 	va_start(list, nret);
476 	va_rtas_call_unlocked(args, token, nargs, nret, list);
477 	va_end(list);
478 }
479 
480 static int ibm_open_errinjct_token;
481 static int ibm_errinjct_token;
482 
483 /**
484  * rtas_call() - Invoke an RTAS firmware function.
485  * @token: Identifies the function being invoked.
486  * @nargs: Number of input parameters. Does not include token.
487  * @nret: Number of output parameters, including the call status.
488  * @outputs: Array of @nret output words.
489  * @....: List of @nargs input parameters.
490  *
491  * Invokes the RTAS function indicated by @token, which the caller
492  * should obtain via rtas_token().
493  *
494  * The @nargs and @nret arguments must match the number of input and
495  * output parameters specified for the RTAS function.
496  *
497  * rtas_call() returns RTAS status codes, not conventional Linux errno
498  * values. Callers must translate any failure to an appropriate errno
499  * in syscall context. Most callers of RTAS functions that can return
500  * -2 or 990x should use rtas_busy_delay() to correctly handle those
501  * statuses before calling again.
502  *
503  * The return value descriptions are adapted from 7.2.8 [RTAS] Return
504  * Codes of the PAPR and CHRP specifications.
505  *
506  * Context: Process context preferably, interrupt context if
507  *          necessary.  Acquires an internal spinlock and may perform
508  *          GFP_ATOMIC slab allocation in error path. Unsafe for NMI
509  *          context.
510  * Return:
511  * *                          0 - RTAS function call succeeded.
512  * *                         -1 - RTAS function encountered a hardware or
513  *                                platform error, or the token is invalid,
514  *                                or the function is restricted by kernel policy.
515  * *                         -2 - Specs say "A necessary hardware device was busy,
516  *                                and the requested function could not be
517  *                                performed. The operation should be retried at
518  *                                a later time." This is misleading, at least with
519  *                                respect to current RTAS implementations. What it
520  *                                usually means in practice is that the function
521  *                                could not be completed while meeting RTAS's
522  *                                deadline for returning control to the OS (250us
523  *                                for PAPR/PowerVM, typically), but the call may be
524  *                                immediately reattempted to resume work on it.
525  * *                         -3 - Parameter error.
526  * *                         -7 - Unexpected state change.
527  * *                9000...9899 - Vendor-specific success codes.
528  * *                9900...9905 - Advisory extended delay. Caller should try
529  *                                again after ~10^x ms has elapsed, where x is
530  *                                the last digit of the status [0-5]. Again going
531  *                                beyond the PAPR text, 990x on PowerVM indicates
532  *                                contention for RTAS-internal resources. Other
533  *                                RTAS call sequences in progress should be
534  *                                allowed to complete before reattempting the
535  *                                call.
536  * *                      -9000 - Multi-level isolation error.
537  * *              -9999...-9004 - Vendor-specific error codes.
538  * * Additional negative values - Function-specific error.
539  * * Additional positive values - Function-specific success.
540  */
541 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
542 {
543 	va_list list;
544 	int i;
545 	unsigned long s;
546 	struct rtas_args *rtas_args;
547 	char *buff_copy = NULL;
548 	int ret;
549 
550 	if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
551 		return -1;
552 
553 	if (token == ibm_open_errinjct_token || token == ibm_errinjct_token) {
554 		/*
555 		 * It would be nicer to not discard the error value
556 		 * from security_locked_down(), but callers expect an
557 		 * RTAS status, not an errno.
558 		 */
559 		if (security_locked_down(LOCKDOWN_RTAS_ERROR_INJECTION))
560 			return -1;
561 	}
562 
563 	if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
564 		WARN_ON_ONCE(1);
565 		return -1;
566 	}
567 
568 	s = lock_rtas();
569 
570 	/* We use the global rtas args buffer */
571 	rtas_args = &rtas.args;
572 
573 	va_start(list, outputs);
574 	va_rtas_call_unlocked(rtas_args, token, nargs, nret, list);
575 	va_end(list);
576 
577 	/* A -1 return code indicates that the last command couldn't
578 	   be completed due to a hardware error. */
579 	if (be32_to_cpu(rtas_args->rets[0]) == -1)
580 		buff_copy = __fetch_rtas_last_error(NULL);
581 
582 	if (nret > 1 && outputs != NULL)
583 		for (i = 0; i < nret-1; ++i)
584 			outputs[i] = be32_to_cpu(rtas_args->rets[i+1]);
585 	ret = (nret > 0)? be32_to_cpu(rtas_args->rets[0]): 0;
586 
587 	unlock_rtas(s);
588 
589 	if (buff_copy) {
590 		log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
591 		if (slab_is_available())
592 			kfree(buff_copy);
593 	}
594 	return ret;
595 }
596 EXPORT_SYMBOL(rtas_call);
597 
598 /**
599  * rtas_busy_delay_time() - From an RTAS status value, calculate the
600  *                          suggested delay time in milliseconds.
601  *
602  * @status: a value returned from rtas_call() or similar APIs which return
603  *          the status of a RTAS function call.
604  *
605  * Context: Any context.
606  *
607  * Return:
608  * * 100000 - If @status is 9905.
609  * * 10000  - If @status is 9904.
610  * * 1000   - If @status is 9903.
611  * * 100    - If @status is 9902.
612  * * 10     - If @status is 9901.
613  * * 1      - If @status is either 9900 or -2. This is "wrong" for -2, but
614  *            some callers depend on this behavior, and the worst outcome
615  *            is that they will delay for longer than necessary.
616  * * 0      - If @status is not a busy or extended delay value.
617  */
618 unsigned int rtas_busy_delay_time(int status)
619 {
620 	int order;
621 	unsigned int ms = 0;
622 
623 	if (status == RTAS_BUSY) {
624 		ms = 1;
625 	} else if (status >= RTAS_EXTENDED_DELAY_MIN &&
626 		   status <= RTAS_EXTENDED_DELAY_MAX) {
627 		order = status - RTAS_EXTENDED_DELAY_MIN;
628 		for (ms = 1; order > 0; order--)
629 			ms *= 10;
630 	}
631 
632 	return ms;
633 }
634 EXPORT_SYMBOL(rtas_busy_delay_time);
635 
636 /**
637  * rtas_busy_delay() - helper for RTAS busy and extended delay statuses
638  *
639  * @status: a value returned from rtas_call() or similar APIs which return
640  *          the status of a RTAS function call.
641  *
642  * Context: Process context. May sleep or schedule.
643  *
644  * Return:
645  * * true  - @status is RTAS_BUSY or an extended delay hint. The
646  *           caller may assume that the CPU has been yielded if necessary,
647  *           and that an appropriate delay for @status has elapsed.
648  *           Generally the caller should reattempt the RTAS call which
649  *           yielded @status.
650  *
651  * * false - @status is not @RTAS_BUSY nor an extended delay hint. The
652  *           caller is responsible for handling @status.
653  */
654 bool rtas_busy_delay(int status)
655 {
656 	unsigned int ms;
657 	bool ret;
658 
659 	switch (status) {
660 	case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
661 		ret = true;
662 		ms = rtas_busy_delay_time(status);
663 		/*
664 		 * The extended delay hint can be as high as 100 seconds.
665 		 * Surely any function returning such a status is either
666 		 * buggy or isn't going to be significantly slowed by us
667 		 * polling at 1HZ. Clamp the sleep time to one second.
668 		 */
669 		ms = clamp(ms, 1U, 1000U);
670 		/*
671 		 * The delay hint is an order-of-magnitude suggestion, not
672 		 * a minimum. It is fine, possibly even advantageous, for
673 		 * us to pause for less time than hinted. For small values,
674 		 * use usleep_range() to ensure we don't sleep much longer
675 		 * than actually needed.
676 		 *
677 		 * See Documentation/timers/timers-howto.rst for
678 		 * explanation of the threshold used here. In effect we use
679 		 * usleep_range() for 9900 and 9901, msleep() for
680 		 * 9902-9905.
681 		 */
682 		if (ms <= 20)
683 			usleep_range(ms * 100, ms * 1000);
684 		else
685 			msleep(ms);
686 		break;
687 	case RTAS_BUSY:
688 		ret = true;
689 		/*
690 		 * We should call again immediately if there's no other
691 		 * work to do.
692 		 */
693 		cond_resched();
694 		break;
695 	default:
696 		ret = false;
697 		/*
698 		 * Not a busy or extended delay status; the caller should
699 		 * handle @status itself. Ensure we warn on misuses in
700 		 * atomic context regardless.
701 		 */
702 		might_sleep();
703 		break;
704 	}
705 
706 	return ret;
707 }
708 EXPORT_SYMBOL(rtas_busy_delay);
709 
710 static int rtas_error_rc(int rtas_rc)
711 {
712 	int rc;
713 
714 	switch (rtas_rc) {
715 		case -1: 		/* Hardware Error */
716 			rc = -EIO;
717 			break;
718 		case -3:		/* Bad indicator/domain/etc */
719 			rc = -EINVAL;
720 			break;
721 		case -9000:		/* Isolation error */
722 			rc = -EFAULT;
723 			break;
724 		case -9001:		/* Outstanding TCE/PTE */
725 			rc = -EEXIST;
726 			break;
727 		case -9002:		/* No usable slot */
728 			rc = -ENODEV;
729 			break;
730 		default:
731 			printk(KERN_ERR "%s: unexpected RTAS error %d\n",
732 					__func__, rtas_rc);
733 			rc = -ERANGE;
734 			break;
735 	}
736 	return rc;
737 }
738 
739 int rtas_get_power_level(int powerdomain, int *level)
740 {
741 	int token = rtas_token("get-power-level");
742 	int rc;
743 
744 	if (token == RTAS_UNKNOWN_SERVICE)
745 		return -ENOENT;
746 
747 	while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
748 		udelay(1);
749 
750 	if (rc < 0)
751 		return rtas_error_rc(rc);
752 	return rc;
753 }
754 EXPORT_SYMBOL(rtas_get_power_level);
755 
756 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
757 {
758 	int token = rtas_token("set-power-level");
759 	int rc;
760 
761 	if (token == RTAS_UNKNOWN_SERVICE)
762 		return -ENOENT;
763 
764 	do {
765 		rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
766 	} while (rtas_busy_delay(rc));
767 
768 	if (rc < 0)
769 		return rtas_error_rc(rc);
770 	return rc;
771 }
772 EXPORT_SYMBOL(rtas_set_power_level);
773 
774 int rtas_get_sensor(int sensor, int index, int *state)
775 {
776 	int token = rtas_token("get-sensor-state");
777 	int rc;
778 
779 	if (token == RTAS_UNKNOWN_SERVICE)
780 		return -ENOENT;
781 
782 	do {
783 		rc = rtas_call(token, 2, 2, state, sensor, index);
784 	} while (rtas_busy_delay(rc));
785 
786 	if (rc < 0)
787 		return rtas_error_rc(rc);
788 	return rc;
789 }
790 EXPORT_SYMBOL(rtas_get_sensor);
791 
792 int rtas_get_sensor_fast(int sensor, int index, int *state)
793 {
794 	int token = rtas_token("get-sensor-state");
795 	int rc;
796 
797 	if (token == RTAS_UNKNOWN_SERVICE)
798 		return -ENOENT;
799 
800 	rc = rtas_call(token, 2, 2, state, sensor, index);
801 	WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
802 				    rc <= RTAS_EXTENDED_DELAY_MAX));
803 
804 	if (rc < 0)
805 		return rtas_error_rc(rc);
806 	return rc;
807 }
808 
809 bool rtas_indicator_present(int token, int *maxindex)
810 {
811 	int proplen, count, i;
812 	const struct indicator_elem {
813 		__be32 token;
814 		__be32 maxindex;
815 	} *indicators;
816 
817 	indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
818 	if (!indicators)
819 		return false;
820 
821 	count = proplen / sizeof(struct indicator_elem);
822 
823 	for (i = 0; i < count; i++) {
824 		if (__be32_to_cpu(indicators[i].token) != token)
825 			continue;
826 		if (maxindex)
827 			*maxindex = __be32_to_cpu(indicators[i].maxindex);
828 		return true;
829 	}
830 
831 	return false;
832 }
833 EXPORT_SYMBOL(rtas_indicator_present);
834 
835 int rtas_set_indicator(int indicator, int index, int new_value)
836 {
837 	int token = rtas_token("set-indicator");
838 	int rc;
839 
840 	if (token == RTAS_UNKNOWN_SERVICE)
841 		return -ENOENT;
842 
843 	do {
844 		rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
845 	} while (rtas_busy_delay(rc));
846 
847 	if (rc < 0)
848 		return rtas_error_rc(rc);
849 	return rc;
850 }
851 EXPORT_SYMBOL(rtas_set_indicator);
852 
853 /*
854  * Ignoring RTAS extended delay
855  */
856 int rtas_set_indicator_fast(int indicator, int index, int new_value)
857 {
858 	int rc;
859 	int token = rtas_token("set-indicator");
860 
861 	if (token == RTAS_UNKNOWN_SERVICE)
862 		return -ENOENT;
863 
864 	rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
865 
866 	WARN_ON(rc == RTAS_BUSY || (rc >= RTAS_EXTENDED_DELAY_MIN &&
867 				    rc <= RTAS_EXTENDED_DELAY_MAX));
868 
869 	if (rc < 0)
870 		return rtas_error_rc(rc);
871 
872 	return rc;
873 }
874 
875 /**
876  * rtas_ibm_suspend_me() - Call ibm,suspend-me to suspend the LPAR.
877  *
878  * @fw_status: RTAS call status will be placed here if not NULL.
879  *
880  * rtas_ibm_suspend_me() should be called only on a CPU which has
881  * received H_CONTINUE from the H_JOIN hcall. All other active CPUs
882  * should be waiting to return from H_JOIN.
883  *
884  * rtas_ibm_suspend_me() may suspend execution of the OS
885  * indefinitely. Callers should take appropriate measures upon return, such as
886  * resetting watchdog facilities.
887  *
888  * Callers may choose to retry this call if @fw_status is
889  * %RTAS_THREADS_ACTIVE.
890  *
891  * Return:
892  * 0          - The partition has resumed from suspend, possibly after
893  *              migration to a different host.
894  * -ECANCELED - The operation was aborted.
895  * -EAGAIN    - There were other CPUs not in H_JOIN at the time of the call.
896  * -EBUSY     - Some other condition prevented the suspend from succeeding.
897  * -EIO       - Hardware/platform error.
898  */
899 int rtas_ibm_suspend_me(int *fw_status)
900 {
901 	int fwrc;
902 	int ret;
903 
904 	fwrc = rtas_call(rtas_token("ibm,suspend-me"), 0, 1, NULL);
905 
906 	switch (fwrc) {
907 	case 0:
908 		ret = 0;
909 		break;
910 	case RTAS_SUSPEND_ABORTED:
911 		ret = -ECANCELED;
912 		break;
913 	case RTAS_THREADS_ACTIVE:
914 		ret = -EAGAIN;
915 		break;
916 	case RTAS_NOT_SUSPENDABLE:
917 	case RTAS_OUTSTANDING_COPROC:
918 		ret = -EBUSY;
919 		break;
920 	case -1:
921 	default:
922 		ret = -EIO;
923 		break;
924 	}
925 
926 	if (fw_status)
927 		*fw_status = fwrc;
928 
929 	return ret;
930 }
931 
932 void __noreturn rtas_restart(char *cmd)
933 {
934 	if (rtas_flash_term_hook)
935 		rtas_flash_term_hook(SYS_RESTART);
936 	printk("RTAS system-reboot returned %d\n",
937 	       rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
938 	for (;;);
939 }
940 
941 void rtas_power_off(void)
942 {
943 	if (rtas_flash_term_hook)
944 		rtas_flash_term_hook(SYS_POWER_OFF);
945 	/* allow power on only with power button press */
946 	printk("RTAS power-off returned %d\n",
947 	       rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
948 	for (;;);
949 }
950 
951 void __noreturn rtas_halt(void)
952 {
953 	if (rtas_flash_term_hook)
954 		rtas_flash_term_hook(SYS_HALT);
955 	/* allow power on only with power button press */
956 	printk("RTAS power-off returned %d\n",
957 	       rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
958 	for (;;);
959 }
960 
961 /* Must be in the RMO region, so we place it here */
962 static char rtas_os_term_buf[2048];
963 static s32 ibm_os_term_token = RTAS_UNKNOWN_SERVICE;
964 
965 void rtas_os_term(char *str)
966 {
967 	int status;
968 
969 	/*
970 	 * Firmware with the ibm,extended-os-term property is guaranteed
971 	 * to always return from an ibm,os-term call. Earlier versions without
972 	 * this property may terminate the partition which we want to avoid
973 	 * since it interferes with panic_timeout.
974 	 */
975 	if (ibm_os_term_token == RTAS_UNKNOWN_SERVICE)
976 		return;
977 
978 	snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
979 
980 	/*
981 	 * Keep calling as long as RTAS returns a "try again" status,
982 	 * but don't use rtas_busy_delay(), which potentially
983 	 * schedules.
984 	 */
985 	do {
986 		status = rtas_call(ibm_os_term_token, 1, 1, NULL,
987 				   __pa(rtas_os_term_buf));
988 	} while (rtas_busy_delay_time(status));
989 
990 	if (status != 0)
991 		printk(KERN_EMERG "ibm,os-term call failed %d\n", status);
992 }
993 
994 /**
995  * rtas_activate_firmware() - Activate a new version of firmware.
996  *
997  * Context: This function may sleep.
998  *
999  * Activate a new version of partition firmware. The OS must call this
1000  * after resuming from a partition hibernation or migration in order
1001  * to maintain the ability to perform live firmware updates. It's not
1002  * catastrophic for this method to be absent or to fail; just log the
1003  * condition in that case.
1004  */
1005 void rtas_activate_firmware(void)
1006 {
1007 	int token;
1008 	int fwrc;
1009 
1010 	token = rtas_token("ibm,activate-firmware");
1011 	if (token == RTAS_UNKNOWN_SERVICE) {
1012 		pr_notice("ibm,activate-firmware method unavailable\n");
1013 		return;
1014 	}
1015 
1016 	do {
1017 		fwrc = rtas_call(token, 0, 1, NULL);
1018 	} while (rtas_busy_delay(fwrc));
1019 
1020 	if (fwrc)
1021 		pr_err("ibm,activate-firmware failed (%i)\n", fwrc);
1022 }
1023 
1024 /**
1025  * get_pseries_errorlog() - Find a specific pseries error log in an RTAS
1026  *                          extended event log.
1027  * @log: RTAS error/event log
1028  * @section_id: two character section identifier
1029  *
1030  * Return: A pointer to the specified errorlog or NULL if not found.
1031  */
1032 noinstr struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
1033 						      uint16_t section_id)
1034 {
1035 	struct rtas_ext_event_log_v6 *ext_log =
1036 		(struct rtas_ext_event_log_v6 *)log->buffer;
1037 	struct pseries_errorlog *sect;
1038 	unsigned char *p, *log_end;
1039 	uint32_t ext_log_length = rtas_error_extended_log_length(log);
1040 	uint8_t log_format = rtas_ext_event_log_format(ext_log);
1041 	uint32_t company_id = rtas_ext_event_company_id(ext_log);
1042 
1043 	/* Check that we understand the format */
1044 	if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
1045 	    log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
1046 	    company_id != RTAS_V6EXT_COMPANY_ID_IBM)
1047 		return NULL;
1048 
1049 	log_end = log->buffer + ext_log_length;
1050 	p = ext_log->vendor_log;
1051 
1052 	while (p < log_end) {
1053 		sect = (struct pseries_errorlog *)p;
1054 		if (pseries_errorlog_id(sect) == section_id)
1055 			return sect;
1056 		p += pseries_errorlog_length(sect);
1057 	}
1058 
1059 	return NULL;
1060 }
1061 
1062 #ifdef CONFIG_PPC_RTAS_FILTER
1063 
1064 /*
1065  * The sys_rtas syscall, as originally designed, allows root to pass
1066  * arbitrary physical addresses to RTAS calls. A number of RTAS calls
1067  * can be abused to write to arbitrary memory and do other things that
1068  * are potentially harmful to system integrity, and thus should only
1069  * be used inside the kernel and not exposed to userspace.
1070  *
1071  * All known legitimate users of the sys_rtas syscall will only ever
1072  * pass addresses that fall within the RMO buffer, and use a known
1073  * subset of RTAS calls.
1074  *
1075  * Accordingly, we filter RTAS requests to check that the call is
1076  * permitted, and that provided pointers fall within the RMO buffer.
1077  * The rtas_filters list contains an entry for each permitted call,
1078  * with the indexes of the parameters which are expected to contain
1079  * addresses and sizes of buffers allocated inside the RMO buffer.
1080  */
1081 struct rtas_filter {
1082 	const char *name;
1083 	int token;
1084 	/* Indexes into the args buffer, -1 if not used */
1085 	int buf_idx1;
1086 	int size_idx1;
1087 	int buf_idx2;
1088 	int size_idx2;
1089 
1090 	int fixed_size;
1091 };
1092 
1093 static struct rtas_filter rtas_filters[] __ro_after_init = {
1094 	{ "ibm,activate-firmware", -1, -1, -1, -1, -1 },
1095 	{ "ibm,configure-connector", -1, 0, -1, 1, -1, 4096 },	/* Special cased */
1096 	{ "display-character", -1, -1, -1, -1, -1 },
1097 	{ "ibm,display-message", -1, 0, -1, -1, -1 },
1098 	{ "ibm,errinjct", -1, 2, -1, -1, -1, 1024 },
1099 	{ "ibm,close-errinjct", -1, -1, -1, -1, -1 },
1100 	{ "ibm,open-errinjct", -1, -1, -1, -1, -1 },
1101 	{ "ibm,get-config-addr-info2", -1, -1, -1, -1, -1 },
1102 	{ "ibm,get-dynamic-sensor-state", -1, 1, -1, -1, -1 },
1103 	{ "ibm,get-indices", -1, 2, 3, -1, -1 },
1104 	{ "get-power-level", -1, -1, -1, -1, -1 },
1105 	{ "get-sensor-state", -1, -1, -1, -1, -1 },
1106 	{ "ibm,get-system-parameter", -1, 1, 2, -1, -1 },
1107 	{ "get-time-of-day", -1, -1, -1, -1, -1 },
1108 	{ "ibm,get-vpd", -1, 0, -1, 1, 2 },
1109 	{ "ibm,lpar-perftools", -1, 2, 3, -1, -1 },
1110 	{ "ibm,platform-dump", -1, 4, 5, -1, -1 },		/* Special cased */
1111 	{ "ibm,read-slot-reset-state", -1, -1, -1, -1, -1 },
1112 	{ "ibm,scan-log-dump", -1, 0, 1, -1, -1 },
1113 	{ "ibm,set-dynamic-indicator", -1, 2, -1, -1, -1 },
1114 	{ "ibm,set-eeh-option", -1, -1, -1, -1, -1 },
1115 	{ "set-indicator", -1, -1, -1, -1, -1 },
1116 	{ "set-power-level", -1, -1, -1, -1, -1 },
1117 	{ "set-time-for-power-on", -1, -1, -1, -1, -1 },
1118 	{ "ibm,set-system-parameter", -1, 1, -1, -1, -1 },
1119 	{ "set-time-of-day", -1, -1, -1, -1, -1 },
1120 #ifdef CONFIG_CPU_BIG_ENDIAN
1121 	{ "ibm,suspend-me", -1, -1, -1, -1, -1 },
1122 	{ "ibm,update-nodes", -1, 0, -1, -1, -1, 4096 },
1123 	{ "ibm,update-properties", -1, 0, -1, -1, -1, 4096 },
1124 #endif
1125 	{ "ibm,physical-attestation", -1, 0, 1, -1, -1 },
1126 };
1127 
1128 static bool in_rmo_buf(u32 base, u32 end)
1129 {
1130 	return base >= rtas_rmo_buf &&
1131 		base < (rtas_rmo_buf + RTAS_USER_REGION_SIZE) &&
1132 		base <= end &&
1133 		end >= rtas_rmo_buf &&
1134 		end < (rtas_rmo_buf + RTAS_USER_REGION_SIZE);
1135 }
1136 
1137 static bool block_rtas_call(int token, int nargs,
1138 			    struct rtas_args *args)
1139 {
1140 	int i;
1141 
1142 	for (i = 0; i < ARRAY_SIZE(rtas_filters); i++) {
1143 		struct rtas_filter *f = &rtas_filters[i];
1144 		u32 base, size, end;
1145 
1146 		if (token != f->token)
1147 			continue;
1148 
1149 		if (f->buf_idx1 != -1) {
1150 			base = be32_to_cpu(args->args[f->buf_idx1]);
1151 			if (f->size_idx1 != -1)
1152 				size = be32_to_cpu(args->args[f->size_idx1]);
1153 			else if (f->fixed_size)
1154 				size = f->fixed_size;
1155 			else
1156 				size = 1;
1157 
1158 			end = base + size - 1;
1159 
1160 			/*
1161 			 * Special case for ibm,platform-dump - NULL buffer
1162 			 * address is used to indicate end of dump processing
1163 			 */
1164 			if (!strcmp(f->name, "ibm,platform-dump") &&
1165 			    base == 0)
1166 				return false;
1167 
1168 			if (!in_rmo_buf(base, end))
1169 				goto err;
1170 		}
1171 
1172 		if (f->buf_idx2 != -1) {
1173 			base = be32_to_cpu(args->args[f->buf_idx2]);
1174 			if (f->size_idx2 != -1)
1175 				size = be32_to_cpu(args->args[f->size_idx2]);
1176 			else if (f->fixed_size)
1177 				size = f->fixed_size;
1178 			else
1179 				size = 1;
1180 			end = base + size - 1;
1181 
1182 			/*
1183 			 * Special case for ibm,configure-connector where the
1184 			 * address can be 0
1185 			 */
1186 			if (!strcmp(f->name, "ibm,configure-connector") &&
1187 			    base == 0)
1188 				return false;
1189 
1190 			if (!in_rmo_buf(base, end))
1191 				goto err;
1192 		}
1193 
1194 		return false;
1195 	}
1196 
1197 err:
1198 	pr_err_ratelimited("sys_rtas: RTAS call blocked - exploit attempt?\n");
1199 	pr_err_ratelimited("sys_rtas: token=0x%x, nargs=%d (called by %s)\n",
1200 			   token, nargs, current->comm);
1201 	return true;
1202 }
1203 
1204 static void __init rtas_syscall_filter_init(void)
1205 {
1206 	unsigned int i;
1207 
1208 	for (i = 0; i < ARRAY_SIZE(rtas_filters); i++)
1209 		rtas_filters[i].token = rtas_token(rtas_filters[i].name);
1210 }
1211 
1212 #else
1213 
1214 static bool block_rtas_call(int token, int nargs,
1215 			    struct rtas_args *args)
1216 {
1217 	return false;
1218 }
1219 
1220 static void __init rtas_syscall_filter_init(void)
1221 {
1222 }
1223 
1224 #endif /* CONFIG_PPC_RTAS_FILTER */
1225 
1226 /* We assume to be passed big endian arguments */
1227 SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
1228 {
1229 	struct rtas_args args;
1230 	unsigned long flags;
1231 	char *buff_copy, *errbuf = NULL;
1232 	int nargs, nret, token;
1233 
1234 	if (!capable(CAP_SYS_ADMIN))
1235 		return -EPERM;
1236 
1237 	if (!rtas.entry)
1238 		return -EINVAL;
1239 
1240 	if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
1241 		return -EFAULT;
1242 
1243 	nargs = be32_to_cpu(args.nargs);
1244 	nret  = be32_to_cpu(args.nret);
1245 	token = be32_to_cpu(args.token);
1246 
1247 	if (nargs >= ARRAY_SIZE(args.args)
1248 	    || nret > ARRAY_SIZE(args.args)
1249 	    || nargs + nret > ARRAY_SIZE(args.args))
1250 		return -EINVAL;
1251 
1252 	/* Copy in args. */
1253 	if (copy_from_user(args.args, uargs->args,
1254 			   nargs * sizeof(rtas_arg_t)) != 0)
1255 		return -EFAULT;
1256 
1257 	if (token == RTAS_UNKNOWN_SERVICE)
1258 		return -EINVAL;
1259 
1260 	args.rets = &args.args[nargs];
1261 	memset(args.rets, 0, nret * sizeof(rtas_arg_t));
1262 
1263 	if (block_rtas_call(token, nargs, &args))
1264 		return -EINVAL;
1265 
1266 	if (token == ibm_open_errinjct_token || token == ibm_errinjct_token) {
1267 		int err;
1268 
1269 		err = security_locked_down(LOCKDOWN_RTAS_ERROR_INJECTION);
1270 		if (err)
1271 			return err;
1272 	}
1273 
1274 	/* Need to handle ibm,suspend_me call specially */
1275 	if (token == rtas_token("ibm,suspend-me")) {
1276 
1277 		/*
1278 		 * rtas_ibm_suspend_me assumes the streamid handle is in cpu
1279 		 * endian, or at least the hcall within it requires it.
1280 		 */
1281 		int rc = 0;
1282 		u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
1283 		              | be32_to_cpu(args.args[1]);
1284 		rc = rtas_syscall_dispatch_ibm_suspend_me(handle);
1285 		if (rc == -EAGAIN)
1286 			args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
1287 		else if (rc == -EIO)
1288 			args.rets[0] = cpu_to_be32(-1);
1289 		else if (rc)
1290 			return rc;
1291 		goto copy_return;
1292 	}
1293 
1294 	buff_copy = get_errorlog_buffer();
1295 
1296 	flags = lock_rtas();
1297 
1298 	rtas.args = args;
1299 	do_enter_rtas(__pa(&rtas.args));
1300 	args = rtas.args;
1301 
1302 	/* A -1 return code indicates that the last command couldn't
1303 	   be completed due to a hardware error. */
1304 	if (be32_to_cpu(args.rets[0]) == -1)
1305 		errbuf = __fetch_rtas_last_error(buff_copy);
1306 
1307 	unlock_rtas(flags);
1308 
1309 	if (buff_copy) {
1310 		if (errbuf)
1311 			log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
1312 		kfree(buff_copy);
1313 	}
1314 
1315  copy_return:
1316 	/* Copy out args. */
1317 	if (copy_to_user(uargs->args + nargs,
1318 			 args.args + nargs,
1319 			 nret * sizeof(rtas_arg_t)) != 0)
1320 		return -EFAULT;
1321 
1322 	return 0;
1323 }
1324 
1325 /*
1326  * Call early during boot, before mem init, to retrieve the RTAS
1327  * information from the device-tree and allocate the RMO buffer for userland
1328  * accesses.
1329  */
1330 void __init rtas_initialize(void)
1331 {
1332 	unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
1333 	u32 base, size, entry;
1334 	int no_base, no_size, no_entry;
1335 
1336 	/* Get RTAS dev node and fill up our "rtas" structure with infos
1337 	 * about it.
1338 	 */
1339 	rtas.dev = of_find_node_by_name(NULL, "rtas");
1340 	if (!rtas.dev)
1341 		return;
1342 
1343 	no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
1344 	no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
1345 	if (no_base || no_size) {
1346 		of_node_put(rtas.dev);
1347 		rtas.dev = NULL;
1348 		return;
1349 	}
1350 
1351 	rtas.base = base;
1352 	rtas.size = size;
1353 	no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
1354 	rtas.entry = no_entry ? rtas.base : entry;
1355 
1356 	init_error_log_max();
1357 
1358 	/*
1359 	 * Discover these now to avoid device tree lookups in the
1360 	 * panic path.
1361 	 */
1362 	if (of_property_read_bool(rtas.dev, "ibm,extended-os-term"))
1363 		ibm_os_term_token = rtas_token("ibm,os-term");
1364 
1365 	/* If RTAS was found, allocate the RMO buffer for it and look for
1366 	 * the stop-self token if any
1367 	 */
1368 #ifdef CONFIG_PPC64
1369 	if (firmware_has_feature(FW_FEATURE_LPAR))
1370 		rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
1371 #endif
1372 	rtas_rmo_buf = memblock_phys_alloc_range(RTAS_USER_REGION_SIZE, PAGE_SIZE,
1373 						 0, rtas_region);
1374 	if (!rtas_rmo_buf)
1375 		panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
1376 		      PAGE_SIZE, &rtas_region);
1377 
1378 #ifdef CONFIG_RTAS_ERROR_LOGGING
1379 	rtas_last_error_token = rtas_token("rtas-last-error");
1380 #endif
1381 	ibm_open_errinjct_token = rtas_token("ibm,open-errinjct");
1382 	ibm_errinjct_token = rtas_token("ibm,errinjct");
1383 	rtas_syscall_filter_init();
1384 }
1385 
1386 int __init early_init_dt_scan_rtas(unsigned long node,
1387 		const char *uname, int depth, void *data)
1388 {
1389 	const u32 *basep, *entryp, *sizep;
1390 
1391 	if (depth != 1 || strcmp(uname, "rtas") != 0)
1392 		return 0;
1393 
1394 	basep  = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1395 	entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1396 	sizep  = of_get_flat_dt_prop(node, "rtas-size", NULL);
1397 
1398 #ifdef CONFIG_PPC64
1399 	/* need this feature to decide the crashkernel offset */
1400 	if (of_get_flat_dt_prop(node, "ibm,hypertas-functions", NULL))
1401 		powerpc_firmware_features |= FW_FEATURE_LPAR;
1402 #endif
1403 
1404 	if (basep && entryp && sizep) {
1405 		rtas.base = *basep;
1406 		rtas.entry = *entryp;
1407 		rtas.size = *sizep;
1408 	}
1409 
1410 #ifdef CONFIG_UDBG_RTAS_CONSOLE
1411 	basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
1412 	if (basep)
1413 		rtas_putchar_token = *basep;
1414 
1415 	basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
1416 	if (basep)
1417 		rtas_getchar_token = *basep;
1418 
1419 	if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
1420 	    rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
1421 		udbg_init_rtas_console();
1422 
1423 #endif
1424 
1425 	/* break now */
1426 	return 1;
1427 }
1428 
1429 static arch_spinlock_t timebase_lock;
1430 static u64 timebase = 0;
1431 
1432 void rtas_give_timebase(void)
1433 {
1434 	unsigned long flags;
1435 
1436 	local_irq_save(flags);
1437 	hard_irq_disable();
1438 	arch_spin_lock(&timebase_lock);
1439 	rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
1440 	timebase = get_tb();
1441 	arch_spin_unlock(&timebase_lock);
1442 
1443 	while (timebase)
1444 		barrier();
1445 	rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
1446 	local_irq_restore(flags);
1447 }
1448 
1449 void rtas_take_timebase(void)
1450 {
1451 	while (!timebase)
1452 		barrier();
1453 	arch_spin_lock(&timebase_lock);
1454 	set_tb(timebase >> 32, timebase & 0xffffffff);
1455 	timebase = 0;
1456 	arch_spin_unlock(&timebase_lock);
1457 }
1458