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