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