Pull release into acpica branch
[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/module.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21
22 #include <asm/prom.h>
23 #include <asm/rtas.h>
24 #include <asm/semaphore.h>
25 #include <asm/machdep.h>
26 #include <asm/page.h>
27 #include <asm/param.h>
28 #include <asm/system.h>
29 #include <asm/delay.h>
30 #include <asm/uaccess.h>
31 #include <asm/lmb.h>
32
33 struct rtas_t rtas = {
34         .lock = SPIN_LOCK_UNLOCKED
35 };
36
37 EXPORT_SYMBOL(rtas);
38
39 DEFINE_SPINLOCK(rtas_data_buf_lock);
40 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
41 unsigned long rtas_rmo_buf;
42
43 /*
44  * If non-NULL, this gets called when the kernel terminates.
45  * This is done like this so rtas_flash can be a module.
46  */
47 void (*rtas_flash_term_hook)(int);
48 EXPORT_SYMBOL(rtas_flash_term_hook);
49
50 /*
51  * call_rtas_display_status and call_rtas_display_status_delay
52  * are designed only for very early low-level debugging, which
53  * is why the token is hard-coded to 10.
54  */
55 void call_rtas_display_status(unsigned char c)
56 {
57         struct rtas_args *args = &rtas.args;
58         unsigned long s;
59
60         if (!rtas.base)
61                 return;
62         spin_lock_irqsave(&rtas.lock, s);
63
64         args->token = 10;
65         args->nargs = 1;
66         args->nret  = 1;
67         args->rets  = (rtas_arg_t *)&(args->args[1]);
68         args->args[0] = (int)c;
69
70         enter_rtas(__pa(args));
71
72         spin_unlock_irqrestore(&rtas.lock, s);
73 }
74
75 void call_rtas_display_status_delay(unsigned char c)
76 {
77         static int pending_newline = 0;  /* did last write end with unprinted newline? */
78         static int width = 16;
79
80         if (c == '\n') {        
81                 while (width-- > 0)
82                         call_rtas_display_status(' ');
83                 width = 16;
84                 mdelay(500);
85                 pending_newline = 1;
86         } else {
87                 if (pending_newline) {
88                         call_rtas_display_status('\r');
89                         call_rtas_display_status('\n');
90                 } 
91                 pending_newline = 0;
92                 if (width--) {
93                         call_rtas_display_status(c);
94                         udelay(10000);
95                 }
96         }
97 }
98
99 void rtas_progress(char *s, unsigned short hex)
100 {
101         struct device_node *root;
102         int width, *p;
103         char *os;
104         static int display_character, set_indicator;
105         static int display_width, display_lines, *row_width, form_feed;
106         static DEFINE_SPINLOCK(progress_lock);
107         static int current_line;
108         static int pending_newline = 0;  /* did last write end with unprinted newline? */
109
110         if (!rtas.base)
111                 return;
112
113         if (display_width == 0) {
114                 display_width = 0x10;
115                 if ((root = find_path_device("/rtas"))) {
116                         if ((p = (unsigned int *)get_property(root,
117                                         "ibm,display-line-length", NULL)))
118                                 display_width = *p;
119                         if ((p = (unsigned int *)get_property(root,
120                                         "ibm,form-feed", NULL)))
121                                 form_feed = *p;
122                         if ((p = (unsigned int *)get_property(root,
123                                         "ibm,display-number-of-lines", NULL)))
124                                 display_lines = *p;
125                         row_width = (unsigned int *)get_property(root,
126                                         "ibm,display-truncation-length", NULL);
127                 }
128                 display_character = rtas_token("display-character");
129                 set_indicator = rtas_token("set-indicator");
130         }
131
132         if (display_character == RTAS_UNKNOWN_SERVICE) {
133                 /* use hex display if available */
134                 if (set_indicator != RTAS_UNKNOWN_SERVICE)
135                         rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
136                 return;
137         }
138
139         spin_lock(&progress_lock);
140
141         /*
142          * Last write ended with newline, but we didn't print it since
143          * it would just clear the bottom line of output. Print it now
144          * instead.
145          *
146          * If no newline is pending and form feed is supported, clear the
147          * display with a form feed; otherwise, print a CR to start output
148          * at the beginning of the line.
149          */
150         if (pending_newline) {
151                 rtas_call(display_character, 1, 1, NULL, '\r');
152                 rtas_call(display_character, 1, 1, NULL, '\n');
153                 pending_newline = 0;
154         } else {
155                 current_line = 0;
156                 if (form_feed)
157                         rtas_call(display_character, 1, 1, NULL,
158                                   (char)form_feed);
159                 else
160                         rtas_call(display_character, 1, 1, NULL, '\r');
161         }
162  
163         if (row_width)
164                 width = row_width[current_line];
165         else
166                 width = display_width;
167         os = s;
168         while (*os) {
169                 if (*os == '\n' || *os == '\r') {
170                         /* If newline is the last character, save it
171                          * until next call to avoid bumping up the
172                          * display output.
173                          */
174                         if (*os == '\n' && !os[1]) {
175                                 pending_newline = 1;
176                                 current_line++;
177                                 if (current_line > display_lines-1)
178                                         current_line = display_lines-1;
179                                 spin_unlock(&progress_lock);
180                                 return;
181                         }
182  
183                         /* RTAS wants CR-LF, not just LF */
184  
185                         if (*os == '\n') {
186                                 rtas_call(display_character, 1, 1, NULL, '\r');
187                                 rtas_call(display_character, 1, 1, NULL, '\n');
188                         } else {
189                                 /* CR might be used to re-draw a line, so we'll
190                                  * leave it alone and not add LF.
191                                  */
192                                 rtas_call(display_character, 1, 1, NULL, *os);
193                         }
194  
195                         if (row_width)
196                                 width = row_width[current_line];
197                         else
198                                 width = display_width;
199                 } else {
200                         width--;
201                         rtas_call(display_character, 1, 1, NULL, *os);
202                 }
203  
204                 os++;
205  
206                 /* if we overwrite the screen length */
207                 if (width <= 0)
208                         while ((*os != 0) && (*os != '\n') && (*os != '\r'))
209                                 os++;
210         }
211  
212         spin_unlock(&progress_lock);
213 }
214 EXPORT_SYMBOL(rtas_progress);           /* needed by rtas_flash module */
215
216 int rtas_token(const char *service)
217 {
218         int *tokp;
219         if (rtas.dev == NULL)
220                 return RTAS_UNKNOWN_SERVICE;
221         tokp = (int *) get_property(rtas.dev, service, NULL);
222         return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
223 }
224
225 #ifdef CONFIG_RTAS_ERROR_LOGGING
226 /*
227  * Return the firmware-specified size of the error log buffer
228  *  for all rtas calls that require an error buffer argument.
229  *  This includes 'check-exception' and 'rtas-last-error'.
230  */
231 int rtas_get_error_log_max(void)
232 {
233         static int rtas_error_log_max;
234         if (rtas_error_log_max)
235                 return rtas_error_log_max;
236
237         rtas_error_log_max = rtas_token ("rtas-error-log-max");
238         if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
239             (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
240                 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
241                         rtas_error_log_max);
242                 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
243         }
244         return rtas_error_log_max;
245 }
246 EXPORT_SYMBOL(rtas_get_error_log_max);
247
248
249 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
250 int rtas_last_error_token;
251
252 /** Return a copy of the detailed error text associated with the
253  *  most recent failed call to rtas.  Because the error text
254  *  might go stale if there are any other intervening rtas calls,
255  *  this routine must be called atomically with whatever produced
256  *  the error (i.e. with rtas.lock still held from the previous call).
257  */
258 static char *__fetch_rtas_last_error(char *altbuf)
259 {
260         struct rtas_args err_args, save_args;
261         u32 bufsz;
262         char *buf = NULL;
263
264         if (rtas_last_error_token == -1)
265                 return NULL;
266
267         bufsz = rtas_get_error_log_max();
268
269         err_args.token = rtas_last_error_token;
270         err_args.nargs = 2;
271         err_args.nret = 1;
272         err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
273         err_args.args[1] = bufsz;
274         err_args.args[2] = 0;
275
276         save_args = rtas.args;
277         rtas.args = err_args;
278
279         enter_rtas(__pa(&rtas.args));
280
281         err_args = rtas.args;
282         rtas.args = save_args;
283
284         /* Log the error in the unlikely case that there was one. */
285         if (unlikely(err_args.args[2] == 0)) {
286                 if (altbuf) {
287                         buf = altbuf;
288                 } else {
289                         buf = rtas_err_buf;
290                         if (mem_init_done)
291                                 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
292                 }
293                 if (buf)
294                         memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
295         }
296
297         return buf;
298 }
299
300 #define get_errorlog_buffer()   kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
301
302 #else /* CONFIG_RTAS_ERROR_LOGGING */
303 #define __fetch_rtas_last_error(x)      NULL
304 #define get_errorlog_buffer()           NULL
305 #endif
306
307 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
308 {
309         va_list list;
310         int i;
311         unsigned long s;
312         struct rtas_args *rtas_args;
313         char *buff_copy = NULL;
314         int ret;
315
316         if (token == RTAS_UNKNOWN_SERVICE)
317                 return -1;
318
319         /* Gotta do something different here, use global lock for now... */
320         spin_lock_irqsave(&rtas.lock, s);
321         rtas_args = &rtas.args;
322
323         rtas_args->token = token;
324         rtas_args->nargs = nargs;
325         rtas_args->nret  = nret;
326         rtas_args->rets  = (rtas_arg_t *)&(rtas_args->args[nargs]);
327         va_start(list, outputs);
328         for (i = 0; i < nargs; ++i)
329                 rtas_args->args[i] = va_arg(list, rtas_arg_t);
330         va_end(list);
331
332         for (i = 0; i < nret; ++i)
333                 rtas_args->rets[i] = 0;
334
335         enter_rtas(__pa(rtas_args));
336
337         /* A -1 return code indicates that the last command couldn't
338            be completed due to a hardware error. */
339         if (rtas_args->rets[0] == -1)
340                 buff_copy = __fetch_rtas_last_error(NULL);
341
342         if (nret > 1 && outputs != NULL)
343                 for (i = 0; i < nret-1; ++i)
344                         outputs[i] = rtas_args->rets[i+1];
345         ret = (nret > 0)? rtas_args->rets[0]: 0;
346
347         /* Gotta do something different here, use global lock for now... */
348         spin_unlock_irqrestore(&rtas.lock, s);
349
350         if (buff_copy) {
351                 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
352                 if (mem_init_done)
353                         kfree(buff_copy);
354         }
355         return ret;
356 }
357
358 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
359  * (last digit) milliseconds.  For now we bound at n=5 (100 sec).
360  */
361 unsigned int rtas_extended_busy_delay_time(int status)
362 {
363         int order = status - 9900;
364         unsigned long ms;
365
366         if (order < 0)
367                 order = 0;      /* RTC depends on this for -2 clock busy */
368         else if (order > 5)
369                 order = 5;      /* bound */
370
371         /* Use microseconds for reasonable accuracy */
372         for (ms = 1; order > 0; order--)
373                 ms *= 10;
374
375         return ms; 
376 }
377
378 int rtas_error_rc(int rtas_rc)
379 {
380         int rc;
381
382         switch (rtas_rc) {
383                 case -1:                /* Hardware Error */
384                         rc = -EIO;
385                         break;
386                 case -3:                /* Bad indicator/domain/etc */
387                         rc = -EINVAL;
388                         break;
389                 case -9000:             /* Isolation error */
390                         rc = -EFAULT;
391                         break;
392                 case -9001:             /* Outstanding TCE/PTE */
393                         rc = -EEXIST;
394                         break;
395                 case -9002:             /* No usable slot */
396                         rc = -ENODEV;
397                         break;
398                 default:
399                         printk(KERN_ERR "%s: unexpected RTAS error %d\n",
400                                         __FUNCTION__, rtas_rc);
401                         rc = -ERANGE;
402                         break;
403         }
404         return rc;
405 }
406
407 int rtas_get_power_level(int powerdomain, int *level)
408 {
409         int token = rtas_token("get-power-level");
410         int rc;
411
412         if (token == RTAS_UNKNOWN_SERVICE)
413                 return -ENOENT;
414
415         while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
416                 udelay(1);
417
418         if (rc < 0)
419                 return rtas_error_rc(rc);
420         return rc;
421 }
422
423 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
424 {
425         int token = rtas_token("set-power-level");
426         unsigned int wait_time;
427         int rc;
428
429         if (token == RTAS_UNKNOWN_SERVICE)
430                 return -ENOENT;
431
432         while (1) {
433                 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
434                 if (rc == RTAS_BUSY)
435                         udelay(1);
436                 else if (rtas_is_extended_busy(rc)) {
437                         wait_time = rtas_extended_busy_delay_time(rc);
438                         udelay(wait_time * 1000);
439                 } else
440                         break;
441         }
442
443         if (rc < 0)
444                 return rtas_error_rc(rc);
445         return rc;
446 }
447
448 int rtas_get_sensor(int sensor, int index, int *state)
449 {
450         int token = rtas_token("get-sensor-state");
451         unsigned int wait_time;
452         int rc;
453
454         if (token == RTAS_UNKNOWN_SERVICE)
455                 return -ENOENT;
456
457         while (1) {
458                 rc = rtas_call(token, 2, 2, state, sensor, index);
459                 if (rc == RTAS_BUSY)
460                         udelay(1);
461                 else if (rtas_is_extended_busy(rc)) {
462                         wait_time = rtas_extended_busy_delay_time(rc);
463                         udelay(wait_time * 1000);
464                 } else
465                         break;
466         }
467
468         if (rc < 0)
469                 return rtas_error_rc(rc);
470         return rc;
471 }
472
473 int rtas_set_indicator(int indicator, int index, int new_value)
474 {
475         int token = rtas_token("set-indicator");
476         unsigned int wait_time;
477         int rc;
478
479         if (token == RTAS_UNKNOWN_SERVICE)
480                 return -ENOENT;
481
482         while (1) {
483                 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
484                 if (rc == RTAS_BUSY)
485                         udelay(1);
486                 else if (rtas_is_extended_busy(rc)) {
487                         wait_time = rtas_extended_busy_delay_time(rc);
488                         udelay(wait_time * 1000);
489                 }
490                 else
491                         break;
492         }
493
494         if (rc < 0)
495                 return rtas_error_rc(rc);
496         return rc;
497 }
498
499 void rtas_restart(char *cmd)
500 {
501         if (rtas_flash_term_hook)
502                 rtas_flash_term_hook(SYS_RESTART);
503         printk("RTAS system-reboot returned %d\n",
504                rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
505         for (;;);
506 }
507
508 void rtas_power_off(void)
509 {
510         if (rtas_flash_term_hook)
511                 rtas_flash_term_hook(SYS_POWER_OFF);
512         /* allow power on only with power button press */
513         printk("RTAS power-off returned %d\n",
514                rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
515         for (;;);
516 }
517
518 void rtas_halt(void)
519 {
520         if (rtas_flash_term_hook)
521                 rtas_flash_term_hook(SYS_HALT);
522         /* allow power on only with power button press */
523         printk("RTAS power-off returned %d\n",
524                rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
525         for (;;);
526 }
527
528 /* Must be in the RMO region, so we place it here */
529 static char rtas_os_term_buf[2048];
530
531 void rtas_os_term(char *str)
532 {
533         int status;
534
535         if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
536                 return;
537
538         snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
539
540         do {
541                 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
542                                    __pa(rtas_os_term_buf));
543
544                 if (status == RTAS_BUSY)
545                         udelay(1);
546                 else if (status != 0)
547                         printk(KERN_EMERG "ibm,os-term call failed %d\n",
548                                status);
549         } while (status == RTAS_BUSY);
550 }
551
552
553 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
554 {
555         struct rtas_args args;
556         unsigned long flags;
557         char *buff_copy, *errbuf = NULL;
558         int nargs;
559
560         if (!capable(CAP_SYS_ADMIN))
561                 return -EPERM;
562
563         if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
564                 return -EFAULT;
565
566         nargs = args.nargs;
567         if (nargs > ARRAY_SIZE(args.args)
568             || args.nret > ARRAY_SIZE(args.args)
569             || nargs + args.nret > ARRAY_SIZE(args.args))
570                 return -EINVAL;
571
572         /* Copy in args. */
573         if (copy_from_user(args.args, uargs->args,
574                            nargs * sizeof(rtas_arg_t)) != 0)
575                 return -EFAULT;
576
577         buff_copy = get_errorlog_buffer();
578
579         spin_lock_irqsave(&rtas.lock, flags);
580
581         rtas.args = args;
582         enter_rtas(__pa(&rtas.args));
583         args = rtas.args;
584
585         args.rets = &args.args[nargs];
586
587         /* A -1 return code indicates that the last command couldn't
588            be completed due to a hardware error. */
589         if (args.rets[0] == -1)
590                 errbuf = __fetch_rtas_last_error(buff_copy);
591
592         spin_unlock_irqrestore(&rtas.lock, flags);
593
594         if (buff_copy) {
595                 if (errbuf)
596                         log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
597                 kfree(buff_copy);
598         }
599
600         /* Copy out args. */
601         if (copy_to_user(uargs->args + nargs,
602                          args.args + nargs,
603                          args.nret * sizeof(rtas_arg_t)) != 0)
604                 return -EFAULT;
605
606         return 0;
607 }
608
609 /* This version can't take the spinlock, because it never returns */
610
611 struct rtas_args rtas_stop_self_args = {
612         /* The token is initialized for real in setup_system() */
613         .token = RTAS_UNKNOWN_SERVICE,
614         .nargs = 0,
615         .nret = 1,
616         .rets = &rtas_stop_self_args.args[0],
617 };
618
619 void rtas_stop_self(void)
620 {
621         struct rtas_args *rtas_args = &rtas_stop_self_args;
622
623         local_irq_disable();
624
625         BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
626
627         printk("cpu %u (hwid %u) Ready to die...\n",
628                smp_processor_id(), hard_smp_processor_id());
629         enter_rtas(__pa(rtas_args));
630
631         panic("Alas, I survived.\n");
632 }
633
634 /*
635  * Call early during boot, before mem init or bootmem, to retreive the RTAS
636  * informations from the device-tree and allocate the RMO buffer for userland
637  * accesses.
638  */
639 void __init rtas_initialize(void)
640 {
641         unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
642
643         /* Get RTAS dev node and fill up our "rtas" structure with infos
644          * about it.
645          */
646         rtas.dev = of_find_node_by_name(NULL, "rtas");
647         if (rtas.dev) {
648                 u32 *basep, *entryp;
649                 u32 *sizep;
650
651                 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
652                 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
653                 if (basep != NULL && sizep != NULL) {
654                         rtas.base = *basep;
655                         rtas.size = *sizep;
656                         entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
657                         if (entryp == NULL) /* Ugh */
658                                 rtas.entry = rtas.base;
659                         else
660                                 rtas.entry = *entryp;
661                 } else
662                         rtas.dev = NULL;
663         }
664         if (!rtas.dev)
665                 return;
666
667         /* If RTAS was found, allocate the RMO buffer for it and look for
668          * the stop-self token if any
669          */
670 #ifdef CONFIG_PPC64
671         if (_machine == PLATFORM_PSERIES_LPAR)
672                 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
673 #endif
674         rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
675
676 #ifdef CONFIG_HOTPLUG_CPU
677         rtas_stop_self_args.token = rtas_token("stop-self");
678 #endif /* CONFIG_HOTPLUG_CPU */
679 #ifdef CONFIG_RTAS_ERROR_LOGGING
680         rtas_last_error_token = rtas_token("rtas-last-error");
681 #endif
682 }
683
684
685 EXPORT_SYMBOL(rtas_token);
686 EXPORT_SYMBOL(rtas_call);
687 EXPORT_SYMBOL(rtas_data_buf);
688 EXPORT_SYMBOL(rtas_data_buf_lock);
689 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
690 EXPORT_SYMBOL(rtas_get_sensor);
691 EXPORT_SYMBOL(rtas_get_power_level);
692 EXPORT_SYMBOL(rtas_set_power_level);
693 EXPORT_SYMBOL(rtas_set_indicator);