powerpc/rtas: Validate rtas.entry before calling enter_rtas()
[pandora-kernel.git] / arch / powerpc / kernel / rtas.c
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/export.h>
19 #include <linux/init.h>
20 #include <linux/capability.h>
21 #include <linux/delay.h>
22 #include <linux/cpu.h>
23 #include <linux/smp.h>
24 #include <linux/completion.h>
25 #include <linux/cpumask.h>
26 #include <linux/memblock.h>
27 #include <linux/slab.h>
28 #include <linux/reboot.h>
29
30 #include <asm/prom.h>
31 #include <asm/rtas.h>
32 #include <asm/hvcall.h>
33 #include <asm/machdep.h>
34 #include <asm/firmware.h>
35 #include <asm/page.h>
36 #include <asm/param.h>
37 #include <asm/system.h>
38 #include <asm/delay.h>
39 #include <asm/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 #include <asm/pSeries_reconfig.h>
48
49 struct rtas_t rtas = {
50         .lock = __ARCH_SPIN_LOCK_UNLOCKED
51 };
52 EXPORT_SYMBOL(rtas);
53
54 DEFINE_SPINLOCK(rtas_data_buf_lock);
55 EXPORT_SYMBOL(rtas_data_buf_lock);
56
57 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
58 EXPORT_SYMBOL(rtas_data_buf);
59
60 unsigned long rtas_rmo_buf;
61
62 /*
63  * If non-NULL, this gets called when the kernel terminates.
64  * This is done like this so rtas_flash can be a module.
65  */
66 void (*rtas_flash_term_hook)(int);
67 EXPORT_SYMBOL(rtas_flash_term_hook);
68
69 /* RTAS use home made raw locking instead of spin_lock_irqsave
70  * because those can be called from within really nasty contexts
71  * such as having the timebase stopped which would lockup with
72  * normal locks and spinlock debugging enabled
73  */
74 static unsigned long lock_rtas(void)
75 {
76         unsigned long flags;
77
78         local_irq_save(flags);
79         preempt_disable();
80         arch_spin_lock_flags(&rtas.lock, flags);
81         return flags;
82 }
83
84 static void unlock_rtas(unsigned long flags)
85 {
86         arch_spin_unlock(&rtas.lock);
87         local_irq_restore(flags);
88         preempt_enable();
89 }
90
91 /*
92  * call_rtas_display_status and call_rtas_display_status_delay
93  * are designed only for very early low-level debugging, which
94  * is why the token is hard-coded to 10.
95  */
96 static void call_rtas_display_status(char c)
97 {
98         struct rtas_args *args = &rtas.args;
99         unsigned long s;
100
101         if (!rtas.base)
102                 return;
103         s = lock_rtas();
104
105         args->token = 10;
106         args->nargs = 1;
107         args->nret  = 1;
108         args->rets  = (rtas_arg_t *)&(args->args[1]);
109         args->args[0] = (unsigned char)c;
110
111         enter_rtas(__pa(args));
112
113         unlock_rtas(s);
114 }
115
116 static void call_rtas_display_status_delay(char c)
117 {
118         static int pending_newline = 0;  /* did last write end with unprinted newline? */
119         static int width = 16;
120
121         if (c == '\n') {        
122                 while (width-- > 0)
123                         call_rtas_display_status(' ');
124                 width = 16;
125                 mdelay(500);
126                 pending_newline = 1;
127         } else {
128                 if (pending_newline) {
129                         call_rtas_display_status('\r');
130                         call_rtas_display_status('\n');
131                 } 
132                 pending_newline = 0;
133                 if (width--) {
134                         call_rtas_display_status(c);
135                         udelay(10000);
136                 }
137         }
138 }
139
140 void __init udbg_init_rtas_panel(void)
141 {
142         udbg_putc = call_rtas_display_status_delay;
143 }
144
145 #ifdef CONFIG_UDBG_RTAS_CONSOLE
146
147 /* If you think you're dying before early_init_dt_scan_rtas() does its
148  * work, you can hard code the token values for your firmware here and
149  * hardcode rtas.base/entry etc.
150  */
151 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
152 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
153
154 static void udbg_rtascon_putc(char c)
155 {
156         int tries;
157
158         if (!rtas.base)
159                 return;
160
161         /* Add CRs before LFs */
162         if (c == '\n')
163                 udbg_rtascon_putc('\r');
164
165         /* if there is more than one character to be displayed, wait a bit */
166         for (tries = 0; tries < 16; tries++) {
167                 if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
168                         break;
169                 udelay(1000);
170         }
171 }
172
173 static int udbg_rtascon_getc_poll(void)
174 {
175         int c;
176
177         if (!rtas.base)
178                 return -1;
179
180         if (rtas_call(rtas_getchar_token, 0, 2, &c))
181                 return -1;
182
183         return c;
184 }
185
186 static int udbg_rtascon_getc(void)
187 {
188         int c;
189
190         while ((c = udbg_rtascon_getc_poll()) == -1)
191                 ;
192
193         return c;
194 }
195
196
197 void __init udbg_init_rtas_console(void)
198 {
199         udbg_putc = udbg_rtascon_putc;
200         udbg_getc = udbg_rtascon_getc;
201         udbg_getc_poll = udbg_rtascon_getc_poll;
202 }
203 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
204
205 void rtas_progress(char *s, unsigned short hex)
206 {
207         struct device_node *root;
208         int width;
209         const int *p;
210         char *os;
211         static int display_character, set_indicator;
212         static int display_width, display_lines, form_feed;
213         static const int *row_width;
214         static DEFINE_SPINLOCK(progress_lock);
215         static int current_line;
216         static int pending_newline = 0;  /* did last write end with unprinted newline? */
217
218         if (!rtas.base)
219                 return;
220
221         if (display_width == 0) {
222                 display_width = 0x10;
223                 if ((root = of_find_node_by_path("/rtas"))) {
224                         if ((p = of_get_property(root,
225                                         "ibm,display-line-length", NULL)))
226                                 display_width = *p;
227                         if ((p = of_get_property(root,
228                                         "ibm,form-feed", NULL)))
229                                 form_feed = *p;
230                         if ((p = of_get_property(root,
231                                         "ibm,display-number-of-lines", NULL)))
232                                 display_lines = *p;
233                         row_width = of_get_property(root,
234                                         "ibm,display-truncation-length", NULL);
235                         of_node_put(root);
236                 }
237                 display_character = rtas_token("display-character");
238                 set_indicator = rtas_token("set-indicator");
239         }
240
241         if (display_character == RTAS_UNKNOWN_SERVICE) {
242                 /* use hex display if available */
243                 if (set_indicator != RTAS_UNKNOWN_SERVICE)
244                         rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
245                 return;
246         }
247
248         spin_lock(&progress_lock);
249
250         /*
251          * Last write ended with newline, but we didn't print it since
252          * it would just clear the bottom line of output. Print it now
253          * instead.
254          *
255          * If no newline is pending and form feed is supported, clear the
256          * display with a form feed; otherwise, print a CR to start output
257          * at the beginning of the line.
258          */
259         if (pending_newline) {
260                 rtas_call(display_character, 1, 1, NULL, '\r');
261                 rtas_call(display_character, 1, 1, NULL, '\n');
262                 pending_newline = 0;
263         } else {
264                 current_line = 0;
265                 if (form_feed)
266                         rtas_call(display_character, 1, 1, NULL,
267                                   (char)form_feed);
268                 else
269                         rtas_call(display_character, 1, 1, NULL, '\r');
270         }
271  
272         if (row_width)
273                 width = row_width[current_line];
274         else
275                 width = display_width;
276         os = s;
277         while (*os) {
278                 if (*os == '\n' || *os == '\r') {
279                         /* If newline is the last character, save it
280                          * until next call to avoid bumping up the
281                          * display output.
282                          */
283                         if (*os == '\n' && !os[1]) {
284                                 pending_newline = 1;
285                                 current_line++;
286                                 if (current_line > display_lines-1)
287                                         current_line = display_lines-1;
288                                 spin_unlock(&progress_lock);
289                                 return;
290                         }
291  
292                         /* RTAS wants CR-LF, not just LF */
293  
294                         if (*os == '\n') {
295                                 rtas_call(display_character, 1, 1, NULL, '\r');
296                                 rtas_call(display_character, 1, 1, NULL, '\n');
297                         } else {
298                                 /* CR might be used to re-draw a line, so we'll
299                                  * leave it alone and not add LF.
300                                  */
301                                 rtas_call(display_character, 1, 1, NULL, *os);
302                         }
303  
304                         if (row_width)
305                                 width = row_width[current_line];
306                         else
307                                 width = display_width;
308                 } else {
309                         width--;
310                         rtas_call(display_character, 1, 1, NULL, *os);
311                 }
312  
313                 os++;
314  
315                 /* if we overwrite the screen length */
316                 if (width <= 0)
317                         while ((*os != 0) && (*os != '\n') && (*os != '\r'))
318                                 os++;
319         }
320  
321         spin_unlock(&progress_lock);
322 }
323 EXPORT_SYMBOL(rtas_progress);           /* needed by rtas_flash module */
324
325 int rtas_token(const char *service)
326 {
327         const int *tokp;
328         if (rtas.dev == NULL)
329                 return RTAS_UNKNOWN_SERVICE;
330         tokp = of_get_property(rtas.dev, service, NULL);
331         return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
332 }
333 EXPORT_SYMBOL(rtas_token);
334
335 int rtas_service_present(const char *service)
336 {
337         return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
338 }
339 EXPORT_SYMBOL(rtas_service_present);
340
341 #ifdef CONFIG_RTAS_ERROR_LOGGING
342 /*
343  * Return the firmware-specified size of the error log buffer
344  *  for all rtas calls that require an error buffer argument.
345  *  This includes 'check-exception' and 'rtas-last-error'.
346  */
347 int rtas_get_error_log_max(void)
348 {
349         static int rtas_error_log_max;
350         if (rtas_error_log_max)
351                 return rtas_error_log_max;
352
353         rtas_error_log_max = rtas_token ("rtas-error-log-max");
354         if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
355             (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
356                 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
357                         rtas_error_log_max);
358                 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
359         }
360         return rtas_error_log_max;
361 }
362 EXPORT_SYMBOL(rtas_get_error_log_max);
363
364
365 static char rtas_err_buf[RTAS_ERROR_LOG_MAX];
366 static int rtas_last_error_token;
367
368 /** Return a copy of the detailed error text associated with the
369  *  most recent failed call to rtas.  Because the error text
370  *  might go stale if there are any other intervening rtas calls,
371  *  this routine must be called atomically with whatever produced
372  *  the error (i.e. with rtas.lock still held from the previous call).
373  */
374 static char *__fetch_rtas_last_error(char *altbuf)
375 {
376         struct rtas_args err_args, save_args;
377         u32 bufsz;
378         char *buf = NULL;
379
380         if (rtas_last_error_token == -1)
381                 return NULL;
382
383         bufsz = rtas_get_error_log_max();
384
385         err_args.token = rtas_last_error_token;
386         err_args.nargs = 2;
387         err_args.nret = 1;
388         err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
389         err_args.args[1] = bufsz;
390         err_args.args[2] = 0;
391
392         save_args = rtas.args;
393         rtas.args = err_args;
394
395         enter_rtas(__pa(&rtas.args));
396
397         err_args = rtas.args;
398         rtas.args = save_args;
399
400         /* Log the error in the unlikely case that there was one. */
401         if (unlikely(err_args.args[2] == 0)) {
402                 if (altbuf) {
403                         buf = altbuf;
404                 } else {
405                         buf = rtas_err_buf;
406                         if (mem_init_done)
407                                 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
408                 }
409                 if (buf)
410                         memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
411         }
412
413         return buf;
414 }
415
416 #define get_errorlog_buffer()   kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
417
418 #else /* CONFIG_RTAS_ERROR_LOGGING */
419 #define __fetch_rtas_last_error(x)      NULL
420 #define get_errorlog_buffer()           NULL
421 #endif
422
423 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
424 {
425         va_list list;
426         int i;
427         unsigned long s;
428         struct rtas_args *rtas_args;
429         char *buff_copy = NULL;
430         int ret;
431
432         if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
433                 return -1;
434
435         s = lock_rtas();
436         rtas_args = &rtas.args;
437
438         rtas_args->token = token;
439         rtas_args->nargs = nargs;
440         rtas_args->nret  = nret;
441         rtas_args->rets  = (rtas_arg_t *)&(rtas_args->args[nargs]);
442         va_start(list, outputs);
443         for (i = 0; i < nargs; ++i)
444                 rtas_args->args[i] = va_arg(list, rtas_arg_t);
445         va_end(list);
446
447         for (i = 0; i < nret; ++i)
448                 rtas_args->rets[i] = 0;
449
450         enter_rtas(__pa(rtas_args));
451
452         /* A -1 return code indicates that the last command couldn't
453            be completed due to a hardware error. */
454         if (rtas_args->rets[0] == -1)
455                 buff_copy = __fetch_rtas_last_error(NULL);
456
457         if (nret > 1 && outputs != NULL)
458                 for (i = 0; i < nret-1; ++i)
459                         outputs[i] = rtas_args->rets[i+1];
460         ret = (nret > 0)? rtas_args->rets[0]: 0;
461
462         unlock_rtas(s);
463
464         if (buff_copy) {
465                 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
466                 if (mem_init_done)
467                         kfree(buff_copy);
468         }
469         return ret;
470 }
471 EXPORT_SYMBOL(rtas_call);
472
473 /* For RTAS_BUSY (-2), delay for 1 millisecond.  For an extended busy status
474  * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
475  */
476 unsigned int rtas_busy_delay_time(int status)
477 {
478         int order;
479         unsigned int ms = 0;
480
481         if (status == RTAS_BUSY) {
482                 ms = 1;
483         } else if (status >= 9900 && status <= 9905) {
484                 order = status - 9900;
485                 for (ms = 1; order > 0; order--)
486                         ms *= 10;
487         }
488
489         return ms;
490 }
491 EXPORT_SYMBOL(rtas_busy_delay_time);
492
493 /* For an RTAS busy status code, perform the hinted delay. */
494 unsigned int rtas_busy_delay(int status)
495 {
496         unsigned int ms;
497
498         might_sleep();
499         ms = rtas_busy_delay_time(status);
500         if (ms && need_resched())
501                 msleep(ms);
502
503         return ms;
504 }
505 EXPORT_SYMBOL(rtas_busy_delay);
506
507 static int rtas_error_rc(int rtas_rc)
508 {
509         int rc;
510
511         switch (rtas_rc) {
512                 case -1:                /* Hardware Error */
513                         rc = -EIO;
514                         break;
515                 case -3:                /* Bad indicator/domain/etc */
516                         rc = -EINVAL;
517                         break;
518                 case -9000:             /* Isolation error */
519                         rc = -EFAULT;
520                         break;
521                 case -9001:             /* Outstanding TCE/PTE */
522                         rc = -EEXIST;
523                         break;
524                 case -9002:             /* No usable slot */
525                         rc = -ENODEV;
526                         break;
527                 default:
528                         printk(KERN_ERR "%s: unexpected RTAS error %d\n",
529                                         __func__, rtas_rc);
530                         rc = -ERANGE;
531                         break;
532         }
533         return rc;
534 }
535
536 int rtas_get_power_level(int powerdomain, int *level)
537 {
538         int token = rtas_token("get-power-level");
539         int rc;
540
541         if (token == RTAS_UNKNOWN_SERVICE)
542                 return -ENOENT;
543
544         while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
545                 udelay(1);
546
547         if (rc < 0)
548                 return rtas_error_rc(rc);
549         return rc;
550 }
551 EXPORT_SYMBOL(rtas_get_power_level);
552
553 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
554 {
555         int token = rtas_token("set-power-level");
556         int rc;
557
558         if (token == RTAS_UNKNOWN_SERVICE)
559                 return -ENOENT;
560
561         do {
562                 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
563         } while (rtas_busy_delay(rc));
564
565         if (rc < 0)
566                 return rtas_error_rc(rc);
567         return rc;
568 }
569 EXPORT_SYMBOL(rtas_set_power_level);
570
571 int rtas_get_sensor(int sensor, int index, int *state)
572 {
573         int token = rtas_token("get-sensor-state");
574         int rc;
575
576         if (token == RTAS_UNKNOWN_SERVICE)
577                 return -ENOENT;
578
579         do {
580                 rc = rtas_call(token, 2, 2, state, sensor, index);
581         } while (rtas_busy_delay(rc));
582
583         if (rc < 0)
584                 return rtas_error_rc(rc);
585         return rc;
586 }
587 EXPORT_SYMBOL(rtas_get_sensor);
588
589 bool rtas_indicator_present(int token, int *maxindex)
590 {
591         int proplen, count, i;
592         const struct indicator_elem {
593                 u32 token;
594                 u32 maxindex;
595         } *indicators;
596
597         indicators = of_get_property(rtas.dev, "rtas-indicators", &proplen);
598         if (!indicators)
599                 return false;
600
601         count = proplen / sizeof(struct indicator_elem);
602
603         for (i = 0; i < count; i++) {
604                 if (indicators[i].token != token)
605                         continue;
606                 if (maxindex)
607                         *maxindex = indicators[i].maxindex;
608                 return true;
609         }
610
611         return false;
612 }
613 EXPORT_SYMBOL(rtas_indicator_present);
614
615 int rtas_set_indicator(int indicator, int index, int new_value)
616 {
617         int token = rtas_token("set-indicator");
618         int rc;
619
620         if (token == RTAS_UNKNOWN_SERVICE)
621                 return -ENOENT;
622
623         do {
624                 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
625         } while (rtas_busy_delay(rc));
626
627         if (rc < 0)
628                 return rtas_error_rc(rc);
629         return rc;
630 }
631 EXPORT_SYMBOL(rtas_set_indicator);
632
633 /*
634  * Ignoring RTAS extended delay
635  */
636 int rtas_set_indicator_fast(int indicator, int index, int new_value)
637 {
638         int rc;
639         int token = rtas_token("set-indicator");
640
641         if (token == RTAS_UNKNOWN_SERVICE)
642                 return -ENOENT;
643
644         rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
645
646         WARN_ON(rc == -2 || (rc >= 9900 && rc <= 9905));
647
648         if (rc < 0)
649                 return rtas_error_rc(rc);
650
651         return rc;
652 }
653
654 void rtas_restart(char *cmd)
655 {
656         if (rtas_flash_term_hook)
657                 rtas_flash_term_hook(SYS_RESTART);
658         printk("RTAS system-reboot returned %d\n",
659                rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
660         for (;;);
661 }
662
663 void rtas_power_off(void)
664 {
665         if (rtas_flash_term_hook)
666                 rtas_flash_term_hook(SYS_POWER_OFF);
667         /* allow power on only with power button press */
668         printk("RTAS power-off returned %d\n",
669                rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
670         for (;;);
671 }
672
673 void rtas_halt(void)
674 {
675         if (rtas_flash_term_hook)
676                 rtas_flash_term_hook(SYS_HALT);
677         /* allow power on only with power button press */
678         printk("RTAS power-off returned %d\n",
679                rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
680         for (;;);
681 }
682
683 /* Must be in the RMO region, so we place it here */
684 static char rtas_os_term_buf[2048];
685
686 void rtas_os_term(char *str)
687 {
688         int status;
689
690         /*
691          * Firmware with the ibm,extended-os-term property is guaranteed
692          * to always return from an ibm,os-term call. Earlier versions without
693          * this property may terminate the partition which we want to avoid
694          * since it interferes with panic_timeout.
695          */
696         if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term") ||
697             RTAS_UNKNOWN_SERVICE == rtas_token("ibm,extended-os-term"))
698                 return;
699
700         snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
701
702         do {
703                 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
704                                    __pa(rtas_os_term_buf));
705         } while (rtas_busy_delay(status));
706
707         if (status != 0)
708                 printk(KERN_EMERG "ibm,os-term call failed %d\n", status);
709 }
710
711 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
712 #ifdef CONFIG_PPC_PSERIES
713 static int __rtas_suspend_last_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
714 {
715         u16 slb_size = mmu_slb_size;
716         int rc = H_MULTI_THREADS_ACTIVE;
717         int cpu;
718
719         slb_set_size(SLB_MIN_SIZE);
720         printk(KERN_DEBUG "calling ibm,suspend-me on cpu %i\n", smp_processor_id());
721
722         while (rc == H_MULTI_THREADS_ACTIVE && !atomic_read(&data->done) &&
723                !atomic_read(&data->error))
724                 rc = rtas_call(data->token, 0, 1, NULL);
725
726         if (rc || atomic_read(&data->error)) {
727                 printk(KERN_DEBUG "ibm,suspend-me returned %d\n", rc);
728                 slb_set_size(slb_size);
729         }
730
731         if (atomic_read(&data->error))
732                 rc = atomic_read(&data->error);
733
734         atomic_set(&data->error, rc);
735         pSeries_coalesce_init();
736
737         if (wake_when_done) {
738                 atomic_set(&data->done, 1);
739
740                 for_each_online_cpu(cpu)
741                         plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
742         }
743
744         if (atomic_dec_return(&data->working) == 0)
745                 complete(data->complete);
746
747         return rc;
748 }
749
750 int rtas_suspend_last_cpu(struct rtas_suspend_me_data *data)
751 {
752         atomic_inc(&data->working);
753         return __rtas_suspend_last_cpu(data, 0);
754 }
755
756 static int __rtas_suspend_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
757 {
758         long rc = H_SUCCESS;
759         unsigned long msr_save;
760         int cpu;
761
762         atomic_inc(&data->working);
763
764         /* really need to ensure MSR.EE is off for H_JOIN */
765         msr_save = mfmsr();
766         mtmsr(msr_save & ~(MSR_EE));
767
768         while (rc == H_SUCCESS && !atomic_read(&data->done) && !atomic_read(&data->error))
769                 rc = plpar_hcall_norets(H_JOIN);
770
771         mtmsr(msr_save);
772
773         if (rc == H_SUCCESS) {
774                 /* This cpu was prodded and the suspend is complete. */
775                 goto out;
776         } else if (rc == H_CONTINUE) {
777                 /* All other cpus are in H_JOIN, this cpu does
778                  * the suspend.
779                  */
780                 return __rtas_suspend_last_cpu(data, wake_when_done);
781         } else {
782                 printk(KERN_ERR "H_JOIN on cpu %i failed with rc = %ld\n",
783                        smp_processor_id(), rc);
784                 atomic_set(&data->error, rc);
785         }
786
787         if (wake_when_done) {
788                 atomic_set(&data->done, 1);
789
790                 /* This cpu did the suspend or got an error; in either case,
791                  * we need to prod all other other cpus out of join state.
792                  * Extra prods are harmless.
793                  */
794                 for_each_online_cpu(cpu)
795                         plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
796         }
797 out:
798         if (atomic_dec_return(&data->working) == 0)
799                 complete(data->complete);
800         return rc;
801 }
802
803 int rtas_suspend_cpu(struct rtas_suspend_me_data *data)
804 {
805         return __rtas_suspend_cpu(data, 0);
806 }
807
808 static void rtas_percpu_suspend_me(void *info)
809 {
810         __rtas_suspend_cpu((struct rtas_suspend_me_data *)info, 1);
811 }
812
813 enum rtas_cpu_state {
814         DOWN,
815         UP,
816 };
817
818 #ifndef CONFIG_SMP
819 static int rtas_cpu_state_change_mask(enum rtas_cpu_state state,
820                                 cpumask_var_t cpus)
821 {
822         if (!cpumask_empty(cpus)) {
823                 cpumask_clear(cpus);
824                 return -EINVAL;
825         } else
826                 return 0;
827 }
828 #else
829 /* On return cpumask will be altered to indicate CPUs changed.
830  * CPUs with states changed will be set in the mask,
831  * CPUs with status unchanged will be unset in the mask. */
832 static int rtas_cpu_state_change_mask(enum rtas_cpu_state state,
833                                 cpumask_var_t cpus)
834 {
835         int cpu;
836         int cpuret = 0;
837         int ret = 0;
838
839         if (cpumask_empty(cpus))
840                 return 0;
841
842         for_each_cpu(cpu, cpus) {
843                 switch (state) {
844                 case DOWN:
845                         cpuret = cpu_down(cpu);
846                         break;
847                 case UP:
848                         cpuret = cpu_up(cpu);
849                         break;
850                 }
851                 if (cpuret) {
852                         pr_debug("%s: cpu_%s for cpu#%d returned %d.\n",
853                                         __func__,
854                                         ((state == UP) ? "up" : "down"),
855                                         cpu, cpuret);
856                         if (!ret)
857                                 ret = cpuret;
858                         if (state == UP) {
859                                 /* clear bits for unchanged cpus, return */
860                                 cpumask_shift_right(cpus, cpus, cpu);
861                                 cpumask_shift_left(cpus, cpus, cpu);
862                                 break;
863                         } else {
864                                 /* clear bit for unchanged cpu, continue */
865                                 cpumask_clear_cpu(cpu, cpus);
866                         }
867                 }
868         }
869
870         return ret;
871 }
872 #endif
873
874 int rtas_online_cpus_mask(cpumask_var_t cpus)
875 {
876         int ret;
877
878         ret = rtas_cpu_state_change_mask(UP, cpus);
879
880         if (ret) {
881                 cpumask_var_t tmp_mask;
882
883                 if (!alloc_cpumask_var(&tmp_mask, GFP_TEMPORARY))
884                         return ret;
885
886                 /* Use tmp_mask to preserve cpus mask from first failure */
887                 cpumask_copy(tmp_mask, cpus);
888                 rtas_offline_cpus_mask(tmp_mask);
889                 free_cpumask_var(tmp_mask);
890         }
891
892         return ret;
893 }
894 EXPORT_SYMBOL(rtas_online_cpus_mask);
895
896 int rtas_offline_cpus_mask(cpumask_var_t cpus)
897 {
898         return rtas_cpu_state_change_mask(DOWN, cpus);
899 }
900 EXPORT_SYMBOL(rtas_offline_cpus_mask);
901
902 int rtas_ibm_suspend_me(struct rtas_args *args)
903 {
904         long state;
905         long rc;
906         unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
907         struct rtas_suspend_me_data data;
908         DECLARE_COMPLETION_ONSTACK(done);
909         cpumask_var_t offline_mask;
910         int cpuret;
911
912         if (!rtas_service_present("ibm,suspend-me"))
913                 return -ENOSYS;
914
915         /* Make sure the state is valid */
916         rc = plpar_hcall(H_VASI_STATE, retbuf,
917                          ((u64)args->args[0] << 32) | args->args[1]);
918
919         state = retbuf[0];
920
921         if (rc) {
922                 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
923                 return rc;
924         } else if (state == H_VASI_ENABLED) {
925                 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
926                 return 0;
927         } else if (state != H_VASI_SUSPENDING) {
928                 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
929                        state);
930                 args->args[args->nargs] = -1;
931                 return 0;
932         }
933
934         if (!alloc_cpumask_var(&offline_mask, GFP_TEMPORARY))
935                 return -ENOMEM;
936
937         atomic_set(&data.working, 0);
938         atomic_set(&data.done, 0);
939         atomic_set(&data.error, 0);
940         data.token = rtas_token("ibm,suspend-me");
941         data.complete = &done;
942
943         /* All present CPUs must be online */
944         cpumask_andnot(offline_mask, cpu_present_mask, cpu_online_mask);
945         cpuret = rtas_online_cpus_mask(offline_mask);
946         if (cpuret) {
947                 pr_err("%s: Could not bring present CPUs online.\n", __func__);
948                 atomic_set(&data.error, cpuret);
949                 goto out;
950         }
951
952         stop_topology_update();
953
954         /* Call function on all CPUs.  One of us will make the
955          * rtas call
956          */
957         if (on_each_cpu(rtas_percpu_suspend_me, &data, 0))
958                 atomic_set(&data.error, -EINVAL);
959
960         wait_for_completion(&done);
961
962         if (atomic_read(&data.error) != 0)
963                 printk(KERN_ERR "Error doing global join\n");
964
965         start_topology_update();
966
967         /* Take down CPUs not online prior to suspend */
968         cpuret = rtas_offline_cpus_mask(offline_mask);
969         if (cpuret)
970                 pr_warn("%s: Could not restore CPUs to offline state.\n",
971                                 __func__);
972
973 out:
974         free_cpumask_var(offline_mask);
975         return atomic_read(&data.error);
976 }
977 #else /* CONFIG_PPC_PSERIES */
978 int rtas_ibm_suspend_me(struct rtas_args *args)
979 {
980         return -ENOSYS;
981 }
982 #endif
983
984 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
985 {
986         struct rtas_args args;
987         unsigned long flags;
988         char *buff_copy, *errbuf = NULL;
989         int nargs;
990         int rc;
991
992         if (!capable(CAP_SYS_ADMIN))
993                 return -EPERM;
994
995         if (!rtas.entry)
996                 return -EINVAL;
997
998         if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
999                 return -EFAULT;
1000
1001         nargs = args.nargs;
1002         if (nargs > ARRAY_SIZE(args.args)
1003             || args.nret > ARRAY_SIZE(args.args)
1004             || nargs + args.nret > ARRAY_SIZE(args.args))
1005                 return -EINVAL;
1006
1007         /* Copy in args. */
1008         if (copy_from_user(args.args, uargs->args,
1009                            nargs * sizeof(rtas_arg_t)) != 0)
1010                 return -EFAULT;
1011
1012         if (args.token == RTAS_UNKNOWN_SERVICE)
1013                 return -EINVAL;
1014
1015         args.rets = &args.args[nargs];
1016         memset(args.rets, 0, args.nret * sizeof(rtas_arg_t));
1017
1018         /* Need to handle ibm,suspend_me call specially */
1019         if (args.token == ibm_suspend_me_token) {
1020                 rc = rtas_ibm_suspend_me(&args);
1021                 if (rc)
1022                         return rc;
1023                 goto copy_return;
1024         }
1025
1026         buff_copy = get_errorlog_buffer();
1027
1028         flags = lock_rtas();
1029
1030         rtas.args = args;
1031         enter_rtas(__pa(&rtas.args));
1032         args = rtas.args;
1033
1034         /* A -1 return code indicates that the last command couldn't
1035            be completed due to a hardware error. */
1036         if (args.rets[0] == -1)
1037                 errbuf = __fetch_rtas_last_error(buff_copy);
1038
1039         unlock_rtas(flags);
1040
1041         if (buff_copy) {
1042                 if (errbuf)
1043                         log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
1044                 kfree(buff_copy);
1045         }
1046
1047  copy_return:
1048         /* Copy out args. */
1049         if (copy_to_user(uargs->args + nargs,
1050                          args.args + nargs,
1051                          args.nret * sizeof(rtas_arg_t)) != 0)
1052                 return -EFAULT;
1053
1054         return 0;
1055 }
1056
1057 /*
1058  * Call early during boot, before mem init or bootmem, to retrieve the RTAS
1059  * informations from the device-tree and allocate the RMO buffer for userland
1060  * accesses.
1061  */
1062 void __init rtas_initialize(void)
1063 {
1064         unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
1065
1066         /* Get RTAS dev node and fill up our "rtas" structure with infos
1067          * about it.
1068          */
1069         rtas.dev = of_find_node_by_name(NULL, "rtas");
1070         if (rtas.dev) {
1071                 const u32 *basep, *entryp, *sizep;
1072
1073                 basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
1074                 sizep = of_get_property(rtas.dev, "rtas-size", NULL);
1075                 if (basep != NULL && sizep != NULL) {
1076                         rtas.base = *basep;
1077                         rtas.size = *sizep;
1078                         entryp = of_get_property(rtas.dev,
1079                                         "linux,rtas-entry", NULL);
1080                         if (entryp == NULL) /* Ugh */
1081                                 rtas.entry = rtas.base;
1082                         else
1083                                 rtas.entry = *entryp;
1084                 } else
1085                         rtas.dev = NULL;
1086         }
1087         if (!rtas.dev)
1088                 return;
1089
1090         /* If RTAS was found, allocate the RMO buffer for it and look for
1091          * the stop-self token if any
1092          */
1093 #ifdef CONFIG_PPC64
1094         if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
1095                 rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
1096                 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
1097         }
1098 #endif
1099         rtas_rmo_buf = memblock_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
1100
1101 #ifdef CONFIG_RTAS_ERROR_LOGGING
1102         rtas_last_error_token = rtas_token("rtas-last-error");
1103 #endif
1104 }
1105
1106 int __init early_init_dt_scan_rtas(unsigned long node,
1107                 const char *uname, int depth, void *data)
1108 {
1109         u32 *basep, *entryp, *sizep;
1110
1111         if (depth != 1 || strcmp(uname, "rtas") != 0)
1112                 return 0;
1113
1114         basep  = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1115         entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1116         sizep  = of_get_flat_dt_prop(node, "rtas-size", NULL);
1117
1118         if (basep && entryp && sizep) {
1119                 rtas.base = *basep;
1120                 rtas.entry = *entryp;
1121                 rtas.size = *sizep;
1122         }
1123
1124 #ifdef CONFIG_UDBG_RTAS_CONSOLE
1125         basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
1126         if (basep)
1127                 rtas_putchar_token = *basep;
1128
1129         basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
1130         if (basep)
1131                 rtas_getchar_token = *basep;
1132
1133         if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
1134             rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
1135                 udbg_init_rtas_console();
1136
1137 #endif
1138
1139         /* break now */
1140         return 1;
1141 }
1142
1143 static arch_spinlock_t timebase_lock;
1144 static u64 timebase = 0;
1145
1146 void __cpuinit rtas_give_timebase(void)
1147 {
1148         unsigned long flags;
1149
1150         local_irq_save(flags);
1151         hard_irq_disable();
1152         arch_spin_lock(&timebase_lock);
1153         rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
1154         timebase = get_tb();
1155         arch_spin_unlock(&timebase_lock);
1156
1157         while (timebase)
1158                 barrier();
1159         rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
1160         local_irq_restore(flags);
1161 }
1162
1163 void __cpuinit rtas_take_timebase(void)
1164 {
1165         while (!timebase)
1166                 barrier();
1167         arch_spin_lock(&timebase_lock);
1168         set_tb(timebase >> 32, timebase & 0xffffffff);
1169         timebase = 0;
1170         arch_spin_unlock(&timebase_lock);
1171 }