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