xref: /linux/arch/powerpc/kernel/rtas.c (revision 14b42963f64b98ab61fa9723c03d71aa5ef4f862)
1 /*
2  *
3  * Procedures for interfacing to the RTAS on CHRP machines.
4  *
5  * Peter Bergner, IBM	March 2001.
6  * Copyright (C) 2001 IBM.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13 
14 #include <stdarg.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/capability.h>
21 #include <linux/delay.h>
22 
23 #include <asm/prom.h>
24 #include <asm/rtas.h>
25 #include <asm/hvcall.h>
26 #include <asm/semaphore.h>
27 #include <asm/machdep.h>
28 #include <asm/firmware.h>
29 #include <asm/page.h>
30 #include <asm/param.h>
31 #include <asm/system.h>
32 #include <asm/delay.h>
33 #include <asm/uaccess.h>
34 #include <asm/lmb.h>
35 #include <asm/udbg.h>
36 #include <asm/syscalls.h>
37 
38 struct rtas_t rtas = {
39 	.lock = SPIN_LOCK_UNLOCKED
40 };
41 EXPORT_SYMBOL(rtas);
42 
43 struct rtas_suspend_me_data {
44 	long waiting;
45 	struct rtas_args *args;
46 };
47 
48 DEFINE_SPINLOCK(rtas_data_buf_lock);
49 EXPORT_SYMBOL(rtas_data_buf_lock);
50 
51 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
52 EXPORT_SYMBOL(rtas_data_buf);
53 
54 unsigned long rtas_rmo_buf;
55 
56 /*
57  * If non-NULL, this gets called when the kernel terminates.
58  * This is done like this so rtas_flash can be a module.
59  */
60 void (*rtas_flash_term_hook)(int);
61 EXPORT_SYMBOL(rtas_flash_term_hook);
62 
63 /*
64  * call_rtas_display_status and call_rtas_display_status_delay
65  * are designed only for very early low-level debugging, which
66  * is why the token is hard-coded to 10.
67  */
68 static void call_rtas_display_status(char c)
69 {
70 	struct rtas_args *args = &rtas.args;
71 	unsigned long s;
72 
73 	if (!rtas.base)
74 		return;
75 	spin_lock_irqsave(&rtas.lock, s);
76 
77 	args->token = 10;
78 	args->nargs = 1;
79 	args->nret  = 1;
80 	args->rets  = (rtas_arg_t *)&(args->args[1]);
81 	args->args[0] = (unsigned char)c;
82 
83 	enter_rtas(__pa(args));
84 
85 	spin_unlock_irqrestore(&rtas.lock, s);
86 }
87 
88 static void call_rtas_display_status_delay(char c)
89 {
90 	static int pending_newline = 0;  /* did last write end with unprinted newline? */
91 	static int width = 16;
92 
93 	if (c == '\n') {
94 		while (width-- > 0)
95 			call_rtas_display_status(' ');
96 		width = 16;
97 		mdelay(500);
98 		pending_newline = 1;
99 	} else {
100 		if (pending_newline) {
101 			call_rtas_display_status('\r');
102 			call_rtas_display_status('\n');
103 		}
104 		pending_newline = 0;
105 		if (width--) {
106 			call_rtas_display_status(c);
107 			udelay(10000);
108 		}
109 	}
110 }
111 
112 void __init udbg_init_rtas_panel(void)
113 {
114 	udbg_putc = call_rtas_display_status_delay;
115 }
116 
117 #ifdef CONFIG_UDBG_RTAS_CONSOLE
118 
119 /* If you think you're dying before early_init_dt_scan_rtas() does its
120  * work, you can hard code the token values for your firmware here and
121  * hardcode rtas.base/entry etc.
122  */
123 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
124 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
125 
126 static void udbg_rtascon_putc(char c)
127 {
128 	int tries;
129 
130 	if (!rtas.base)
131 		return;
132 
133 	/* Add CRs before LFs */
134 	if (c == '\n')
135 		udbg_rtascon_putc('\r');
136 
137 	/* if there is more than one character to be displayed, wait a bit */
138 	for (tries = 0; tries < 16; tries++) {
139 		if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
140 			break;
141 		udelay(1000);
142 	}
143 }
144 
145 static int udbg_rtascon_getc_poll(void)
146 {
147 	int c;
148 
149 	if (!rtas.base)
150 		return -1;
151 
152 	if (rtas_call(rtas_getchar_token, 0, 2, &c))
153 		return -1;
154 
155 	return c;
156 }
157 
158 static int udbg_rtascon_getc(void)
159 {
160 	int c;
161 
162 	while ((c = udbg_rtascon_getc_poll()) == -1)
163 		;
164 
165 	return c;
166 }
167 
168 
169 void __init udbg_init_rtas_console(void)
170 {
171 	udbg_putc = udbg_rtascon_putc;
172 	udbg_getc = udbg_rtascon_getc;
173 	udbg_getc_poll = udbg_rtascon_getc_poll;
174 }
175 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
176 
177 void rtas_progress(char *s, unsigned short hex)
178 {
179 	struct device_node *root;
180 	int width, *p;
181 	char *os;
182 	static int display_character, set_indicator;
183 	static int display_width, display_lines, *row_width, form_feed;
184 	static DEFINE_SPINLOCK(progress_lock);
185 	static int current_line;
186 	static int pending_newline = 0;  /* did last write end with unprinted newline? */
187 
188 	if (!rtas.base)
189 		return;
190 
191 	if (display_width == 0) {
192 		display_width = 0x10;
193 		if ((root = find_path_device("/rtas"))) {
194 			if ((p = (unsigned int *)get_property(root,
195 					"ibm,display-line-length", NULL)))
196 				display_width = *p;
197 			if ((p = (unsigned int *)get_property(root,
198 					"ibm,form-feed", NULL)))
199 				form_feed = *p;
200 			if ((p = (unsigned int *)get_property(root,
201 					"ibm,display-number-of-lines", NULL)))
202 				display_lines = *p;
203 			row_width = (unsigned int *)get_property(root,
204 					"ibm,display-truncation-length", NULL);
205 		}
206 		display_character = rtas_token("display-character");
207 		set_indicator = rtas_token("set-indicator");
208 	}
209 
210 	if (display_character == RTAS_UNKNOWN_SERVICE) {
211 		/* use hex display if available */
212 		if (set_indicator != RTAS_UNKNOWN_SERVICE)
213 			rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
214 		return;
215 	}
216 
217 	spin_lock(&progress_lock);
218 
219 	/*
220 	 * Last write ended with newline, but we didn't print it since
221 	 * it would just clear the bottom line of output. Print it now
222 	 * instead.
223 	 *
224 	 * If no newline is pending and form feed is supported, clear the
225 	 * display with a form feed; otherwise, print a CR to start output
226 	 * at the beginning of the line.
227 	 */
228 	if (pending_newline) {
229 		rtas_call(display_character, 1, 1, NULL, '\r');
230 		rtas_call(display_character, 1, 1, NULL, '\n');
231 		pending_newline = 0;
232 	} else {
233 		current_line = 0;
234 		if (form_feed)
235 			rtas_call(display_character, 1, 1, NULL,
236 				  (char)form_feed);
237 		else
238 			rtas_call(display_character, 1, 1, NULL, '\r');
239 	}
240 
241 	if (row_width)
242 		width = row_width[current_line];
243 	else
244 		width = display_width;
245 	os = s;
246 	while (*os) {
247 		if (*os == '\n' || *os == '\r') {
248 			/* If newline is the last character, save it
249 			 * until next call to avoid bumping up the
250 			 * display output.
251 			 */
252 			if (*os == '\n' && !os[1]) {
253 				pending_newline = 1;
254 				current_line++;
255 				if (current_line > display_lines-1)
256 					current_line = display_lines-1;
257 				spin_unlock(&progress_lock);
258 				return;
259 			}
260 
261 			/* RTAS wants CR-LF, not just LF */
262 
263 			if (*os == '\n') {
264 				rtas_call(display_character, 1, 1, NULL, '\r');
265 				rtas_call(display_character, 1, 1, NULL, '\n');
266 			} else {
267 				/* CR might be used to re-draw a line, so we'll
268 				 * leave it alone and not add LF.
269 				 */
270 				rtas_call(display_character, 1, 1, NULL, *os);
271 			}
272 
273 			if (row_width)
274 				width = row_width[current_line];
275 			else
276 				width = display_width;
277 		} else {
278 			width--;
279 			rtas_call(display_character, 1, 1, NULL, *os);
280 		}
281 
282 		os++;
283 
284 		/* if we overwrite the screen length */
285 		if (width <= 0)
286 			while ((*os != 0) && (*os != '\n') && (*os != '\r'))
287 				os++;
288 	}
289 
290 	spin_unlock(&progress_lock);
291 }
292 EXPORT_SYMBOL(rtas_progress);		/* needed by rtas_flash module */
293 
294 int rtas_token(const char *service)
295 {
296 	int *tokp;
297 	if (rtas.dev == NULL)
298 		return RTAS_UNKNOWN_SERVICE;
299 	tokp = (int *) get_property(rtas.dev, service, NULL);
300 	return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
301 }
302 EXPORT_SYMBOL(rtas_token);
303 
304 #ifdef CONFIG_RTAS_ERROR_LOGGING
305 /*
306  * Return the firmware-specified size of the error log buffer
307  *  for all rtas calls that require an error buffer argument.
308  *  This includes 'check-exception' and 'rtas-last-error'.
309  */
310 int rtas_get_error_log_max(void)
311 {
312 	static int rtas_error_log_max;
313 	if (rtas_error_log_max)
314 		return rtas_error_log_max;
315 
316 	rtas_error_log_max = rtas_token ("rtas-error-log-max");
317 	if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
318 	    (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
319 		printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
320 			rtas_error_log_max);
321 		rtas_error_log_max = RTAS_ERROR_LOG_MAX;
322 	}
323 	return rtas_error_log_max;
324 }
325 EXPORT_SYMBOL(rtas_get_error_log_max);
326 
327 
328 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
329 int rtas_last_error_token;
330 
331 /** Return a copy of the detailed error text associated with the
332  *  most recent failed call to rtas.  Because the error text
333  *  might go stale if there are any other intervening rtas calls,
334  *  this routine must be called atomically with whatever produced
335  *  the error (i.e. with rtas.lock still held from the previous call).
336  */
337 static char *__fetch_rtas_last_error(char *altbuf)
338 {
339 	struct rtas_args err_args, save_args;
340 	u32 bufsz;
341 	char *buf = NULL;
342 
343 	if (rtas_last_error_token == -1)
344 		return NULL;
345 
346 	bufsz = rtas_get_error_log_max();
347 
348 	err_args.token = rtas_last_error_token;
349 	err_args.nargs = 2;
350 	err_args.nret = 1;
351 	err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
352 	err_args.args[1] = bufsz;
353 	err_args.args[2] = 0;
354 
355 	save_args = rtas.args;
356 	rtas.args = err_args;
357 
358 	enter_rtas(__pa(&rtas.args));
359 
360 	err_args = rtas.args;
361 	rtas.args = save_args;
362 
363 	/* Log the error in the unlikely case that there was one. */
364 	if (unlikely(err_args.args[2] == 0)) {
365 		if (altbuf) {
366 			buf = altbuf;
367 		} else {
368 			buf = rtas_err_buf;
369 			if (mem_init_done)
370 				buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
371 		}
372 		if (buf)
373 			memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
374 	}
375 
376 	return buf;
377 }
378 
379 #define get_errorlog_buffer()	kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
380 
381 #else /* CONFIG_RTAS_ERROR_LOGGING */
382 #define __fetch_rtas_last_error(x)	NULL
383 #define get_errorlog_buffer()		NULL
384 #endif
385 
386 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
387 {
388 	va_list list;
389 	int i;
390 	unsigned long s;
391 	struct rtas_args *rtas_args;
392 	char *buff_copy = NULL;
393 	int ret;
394 
395 	if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
396 		return -1;
397 
398 	/* Gotta do something different here, use global lock for now... */
399 	spin_lock_irqsave(&rtas.lock, s);
400 	rtas_args = &rtas.args;
401 
402 	rtas_args->token = token;
403 	rtas_args->nargs = nargs;
404 	rtas_args->nret  = nret;
405 	rtas_args->rets  = (rtas_arg_t *)&(rtas_args->args[nargs]);
406 	va_start(list, outputs);
407 	for (i = 0; i < nargs; ++i)
408 		rtas_args->args[i] = va_arg(list, rtas_arg_t);
409 	va_end(list);
410 
411 	for (i = 0; i < nret; ++i)
412 		rtas_args->rets[i] = 0;
413 
414 	enter_rtas(__pa(rtas_args));
415 
416 	/* A -1 return code indicates that the last command couldn't
417 	   be completed due to a hardware error. */
418 	if (rtas_args->rets[0] == -1)
419 		buff_copy = __fetch_rtas_last_error(NULL);
420 
421 	if (nret > 1 && outputs != NULL)
422 		for (i = 0; i < nret-1; ++i)
423 			outputs[i] = rtas_args->rets[i+1];
424 	ret = (nret > 0)? rtas_args->rets[0]: 0;
425 
426 	/* Gotta do something different here, use global lock for now... */
427 	spin_unlock_irqrestore(&rtas.lock, s);
428 
429 	if (buff_copy) {
430 		log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
431 		if (mem_init_done)
432 			kfree(buff_copy);
433 	}
434 	return ret;
435 }
436 EXPORT_SYMBOL(rtas_call);
437 
438 /* For RTAS_BUSY (-2), delay for 1 millisecond.  For an extended busy status
439  * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
440  */
441 unsigned int rtas_busy_delay_time(int status)
442 {
443 	int order;
444 	unsigned int ms = 0;
445 
446 	if (status == RTAS_BUSY) {
447 		ms = 1;
448 	} else if (status >= 9900 && status <= 9905) {
449 		order = status - 9900;
450 		for (ms = 1; order > 0; order--)
451 			ms *= 10;
452 	}
453 
454 	return ms;
455 }
456 EXPORT_SYMBOL(rtas_busy_delay_time);
457 
458 /* For an RTAS busy status code, perform the hinted delay. */
459 unsigned int rtas_busy_delay(int status)
460 {
461 	unsigned int ms;
462 
463 	might_sleep();
464 	ms = rtas_busy_delay_time(status);
465 	if (ms)
466 		msleep(ms);
467 
468 	return ms;
469 }
470 EXPORT_SYMBOL(rtas_busy_delay);
471 
472 int rtas_error_rc(int rtas_rc)
473 {
474 	int rc;
475 
476 	switch (rtas_rc) {
477 		case -1: 		/* Hardware Error */
478 			rc = -EIO;
479 			break;
480 		case -3:		/* Bad indicator/domain/etc */
481 			rc = -EINVAL;
482 			break;
483 		case -9000:		/* Isolation error */
484 			rc = -EFAULT;
485 			break;
486 		case -9001:		/* Outstanding TCE/PTE */
487 			rc = -EEXIST;
488 			break;
489 		case -9002:		/* No usable slot */
490 			rc = -ENODEV;
491 			break;
492 		default:
493 			printk(KERN_ERR "%s: unexpected RTAS error %d\n",
494 					__FUNCTION__, rtas_rc);
495 			rc = -ERANGE;
496 			break;
497 	}
498 	return rc;
499 }
500 
501 int rtas_get_power_level(int powerdomain, int *level)
502 {
503 	int token = rtas_token("get-power-level");
504 	int rc;
505 
506 	if (token == RTAS_UNKNOWN_SERVICE)
507 		return -ENOENT;
508 
509 	while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
510 		udelay(1);
511 
512 	if (rc < 0)
513 		return rtas_error_rc(rc);
514 	return rc;
515 }
516 EXPORT_SYMBOL(rtas_get_power_level);
517 
518 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
519 {
520 	int token = rtas_token("set-power-level");
521 	int rc;
522 
523 	if (token == RTAS_UNKNOWN_SERVICE)
524 		return -ENOENT;
525 
526 	do {
527 		rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
528 	} while (rtas_busy_delay(rc));
529 
530 	if (rc < 0)
531 		return rtas_error_rc(rc);
532 	return rc;
533 }
534 EXPORT_SYMBOL(rtas_set_power_level);
535 
536 int rtas_get_sensor(int sensor, int index, int *state)
537 {
538 	int token = rtas_token("get-sensor-state");
539 	int rc;
540 
541 	if (token == RTAS_UNKNOWN_SERVICE)
542 		return -ENOENT;
543 
544 	do {
545 		rc = rtas_call(token, 2, 2, state, sensor, index);
546 	} while (rtas_busy_delay(rc));
547 
548 	if (rc < 0)
549 		return rtas_error_rc(rc);
550 	return rc;
551 }
552 EXPORT_SYMBOL(rtas_get_sensor);
553 
554 int rtas_set_indicator(int indicator, int index, int new_value)
555 {
556 	int token = rtas_token("set-indicator");
557 	int rc;
558 
559 	if (token == RTAS_UNKNOWN_SERVICE)
560 		return -ENOENT;
561 
562 	do {
563 		rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
564 	} while (rtas_busy_delay(rc));
565 
566 	if (rc < 0)
567 		return rtas_error_rc(rc);
568 	return rc;
569 }
570 EXPORT_SYMBOL(rtas_set_indicator);
571 
572 void rtas_restart(char *cmd)
573 {
574 	if (rtas_flash_term_hook)
575 		rtas_flash_term_hook(SYS_RESTART);
576 	printk("RTAS system-reboot returned %d\n",
577 	       rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
578 	for (;;);
579 }
580 
581 void rtas_power_off(void)
582 {
583 	if (rtas_flash_term_hook)
584 		rtas_flash_term_hook(SYS_POWER_OFF);
585 	/* allow power on only with power button press */
586 	printk("RTAS power-off returned %d\n",
587 	       rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
588 	for (;;);
589 }
590 
591 void rtas_halt(void)
592 {
593 	if (rtas_flash_term_hook)
594 		rtas_flash_term_hook(SYS_HALT);
595 	/* allow power on only with power button press */
596 	printk("RTAS power-off returned %d\n",
597 	       rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
598 	for (;;);
599 }
600 
601 /* Must be in the RMO region, so we place it here */
602 static char rtas_os_term_buf[2048];
603 
604 void rtas_os_term(char *str)
605 {
606 	int status;
607 
608 	if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
609 		return;
610 
611 	snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
612 
613 	do {
614 		status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
615 				   __pa(rtas_os_term_buf));
616 	} while (rtas_busy_delay(status));
617 
618 	if (status != 0)
619 		printk(KERN_EMERG "ibm,os-term call failed %d\n",
620 			       status);
621 }
622 
623 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
624 #ifdef CONFIG_PPC_PSERIES
625 static void rtas_percpu_suspend_me(void *info)
626 {
627 	int i;
628 	long rc;
629 	long flags;
630 	struct rtas_suspend_me_data *data =
631 		(struct rtas_suspend_me_data *)info;
632 
633 	/*
634 	 * We use "waiting" to indicate our state.  As long
635 	 * as it is >0, we are still trying to all join up.
636 	 * If it goes to 0, we have successfully joined up and
637 	 * one thread got H_CONTINUE.  If any error happens,
638 	 * we set it to <0.
639 	 */
640 	local_irq_save(flags);
641 	do {
642 		rc = plpar_hcall_norets(H_JOIN);
643 		smp_rmb();
644 	} while (rc == H_SUCCESS && data->waiting > 0);
645 	if (rc == H_SUCCESS)
646 		goto out;
647 
648 	if (rc == H_CONTINUE) {
649 		data->waiting = 0;
650 		data->args->args[data->args->nargs] =
651 			rtas_call(ibm_suspend_me_token, 0, 1, NULL);
652 		for_each_possible_cpu(i)
653 			plpar_hcall_norets(H_PROD,i);
654 	} else {
655 		data->waiting = -EBUSY;
656 		printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
657 	}
658 
659 out:
660 	local_irq_restore(flags);
661 	return;
662 }
663 
664 static int rtas_ibm_suspend_me(struct rtas_args *args)
665 {
666 	int i;
667 	long state;
668 	long rc;
669 	unsigned long dummy;
670 
671 	struct rtas_suspend_me_data data;
672 
673 	/* Make sure the state is valid */
674 	rc = plpar_hcall(H_VASI_STATE,
675 			 ((u64)args->args[0] << 32) | args->args[1],
676 			 0, 0, 0,
677 			 &state, &dummy, &dummy);
678 
679 	if (rc) {
680 		printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
681 		return rc;
682 	} else if (state == H_VASI_ENABLED) {
683 		args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
684 		return 0;
685 	} else if (state != H_VASI_SUSPENDING) {
686 		printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
687 		       state);
688 		args->args[args->nargs] = -1;
689 		return 0;
690 	}
691 
692 	data.waiting = 1;
693 	data.args = args;
694 
695 	/* Call function on all CPUs.  One of us will make the
696 	 * rtas call
697 	 */
698 	if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
699 		data.waiting = -EINVAL;
700 
701 	if (data.waiting != 0)
702 		printk(KERN_ERR "Error doing global join\n");
703 
704 	/* Prod each CPU.  This won't hurt, and will wake
705 	 * anyone we successfully put to sleep with H_JOIN.
706 	 */
707 	for_each_possible_cpu(i)
708 		plpar_hcall_norets(H_PROD, i);
709 
710 	return data.waiting;
711 }
712 #else /* CONFIG_PPC_PSERIES */
713 static int rtas_ibm_suspend_me(struct rtas_args *args)
714 {
715 	return -ENOSYS;
716 }
717 #endif
718 
719 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
720 {
721 	struct rtas_args args;
722 	unsigned long flags;
723 	char *buff_copy, *errbuf = NULL;
724 	int nargs;
725 	int rc;
726 
727 	if (!capable(CAP_SYS_ADMIN))
728 		return -EPERM;
729 
730 	if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
731 		return -EFAULT;
732 
733 	nargs = args.nargs;
734 	if (nargs > ARRAY_SIZE(args.args)
735 	    || args.nret > ARRAY_SIZE(args.args)
736 	    || nargs + args.nret > ARRAY_SIZE(args.args))
737 		return -EINVAL;
738 
739 	/* Copy in args. */
740 	if (copy_from_user(args.args, uargs->args,
741 			   nargs * sizeof(rtas_arg_t)) != 0)
742 		return -EFAULT;
743 
744 	if (args.token == RTAS_UNKNOWN_SERVICE)
745 		return -EINVAL;
746 
747 	/* Need to handle ibm,suspend_me call specially */
748 	if (args.token == ibm_suspend_me_token) {
749 		rc = rtas_ibm_suspend_me(&args);
750 		if (rc)
751 			return rc;
752 		goto copy_return;
753 	}
754 
755 	buff_copy = get_errorlog_buffer();
756 
757 	spin_lock_irqsave(&rtas.lock, flags);
758 
759 	rtas.args = args;
760 	enter_rtas(__pa(&rtas.args));
761 	args = rtas.args;
762 
763 	args.rets = &args.args[nargs];
764 
765 	/* A -1 return code indicates that the last command couldn't
766 	   be completed due to a hardware error. */
767 	if (args.rets[0] == -1)
768 		errbuf = __fetch_rtas_last_error(buff_copy);
769 
770 	spin_unlock_irqrestore(&rtas.lock, flags);
771 
772 	if (buff_copy) {
773 		if (errbuf)
774 			log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
775 		kfree(buff_copy);
776 	}
777 
778  copy_return:
779 	/* Copy out args. */
780 	if (copy_to_user(uargs->args + nargs,
781 			 args.args + nargs,
782 			 args.nret * sizeof(rtas_arg_t)) != 0)
783 		return -EFAULT;
784 
785 	return 0;
786 }
787 
788 /* This version can't take the spinlock, because it never returns */
789 
790 struct rtas_args rtas_stop_self_args = {
791 	/* The token is initialized for real in setup_system() */
792 	.token = RTAS_UNKNOWN_SERVICE,
793 	.nargs = 0,
794 	.nret = 1,
795 	.rets = &rtas_stop_self_args.args[0],
796 };
797 
798 void rtas_stop_self(void)
799 {
800 	struct rtas_args *rtas_args = &rtas_stop_self_args;
801 
802 	local_irq_disable();
803 
804 	BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
805 
806 	printk("cpu %u (hwid %u) Ready to die...\n",
807 	       smp_processor_id(), hard_smp_processor_id());
808 	enter_rtas(__pa(rtas_args));
809 
810 	panic("Alas, I survived.\n");
811 }
812 
813 /*
814  * Call early during boot, before mem init or bootmem, to retrieve the RTAS
815  * informations from the device-tree and allocate the RMO buffer for userland
816  * accesses.
817  */
818 void __init rtas_initialize(void)
819 {
820 	unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
821 
822 	/* Get RTAS dev node and fill up our "rtas" structure with infos
823 	 * about it.
824 	 */
825 	rtas.dev = of_find_node_by_name(NULL, "rtas");
826 	if (rtas.dev) {
827 		u32 *basep, *entryp;
828 		u32 *sizep;
829 
830 		basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
831 		sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
832 		if (basep != NULL && sizep != NULL) {
833 			rtas.base = *basep;
834 			rtas.size = *sizep;
835 			entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
836 			if (entryp == NULL) /* Ugh */
837 				rtas.entry = rtas.base;
838 			else
839 				rtas.entry = *entryp;
840 		} else
841 			rtas.dev = NULL;
842 	}
843 	if (!rtas.dev)
844 		return;
845 
846 	/* If RTAS was found, allocate the RMO buffer for it and look for
847 	 * the stop-self token if any
848 	 */
849 #ifdef CONFIG_PPC64
850 	if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
851 		rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
852 		ibm_suspend_me_token = rtas_token("ibm,suspend-me");
853 	}
854 #endif
855 	rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
856 
857 #ifdef CONFIG_HOTPLUG_CPU
858 	rtas_stop_self_args.token = rtas_token("stop-self");
859 #endif /* CONFIG_HOTPLUG_CPU */
860 #ifdef CONFIG_RTAS_ERROR_LOGGING
861 	rtas_last_error_token = rtas_token("rtas-last-error");
862 #endif
863 }
864 
865 int __init early_init_dt_scan_rtas(unsigned long node,
866 		const char *uname, int depth, void *data)
867 {
868 	u32 *basep, *entryp, *sizep;
869 
870 	if (depth != 1 || strcmp(uname, "rtas") != 0)
871 		return 0;
872 
873 	basep  = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
874 	entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
875 	sizep  = of_get_flat_dt_prop(node, "rtas-size", NULL);
876 
877 	if (basep && entryp && sizep) {
878 		rtas.base = *basep;
879 		rtas.entry = *entryp;
880 		rtas.size = *sizep;
881 	}
882 
883 #ifdef CONFIG_UDBG_RTAS_CONSOLE
884 	basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
885 	if (basep)
886 		rtas_putchar_token = *basep;
887 
888 	basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
889 	if (basep)
890 		rtas_getchar_token = *basep;
891 #endif
892 
893 	/* break now */
894 	return 1;
895 }
896