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