This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_setup.c
1 /*
2  *  linux/arch/ppc/kernel/setup.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Adapted from 'alpha' version by Gary Thomas
6  *  Modified by Cort Dougan (cort@cs.nmt.edu)
7  *  Modified by PPC64 Team, IBM Corp
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version
12  * 2 of the License, or (at your option) any later version.
13  */
14
15 /*
16  * bootup setup stuff..
17  */
18
19 #undef DEBUG
20
21 #include <linux/config.h>
22 #include <linux/errno.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/slab.h>
29 #include <linux/user.h>
30 #include <linux/a.out.h>
31 #include <linux/tty.h>
32 #include <linux/major.h>
33 #include <linux/interrupt.h>
34 #include <linux/reboot.h>
35 #include <linux/init.h>
36 #include <linux/ioport.h>
37 #include <linux/console.h>
38 #include <linux/pci.h>
39 #include <linux/version.h>
40 #include <linux/adb.h>
41 #include <linux/module.h>
42 #include <linux/delay.h>
43
44 #include <linux/irq.h>
45 #include <linux/seq_file.h>
46 #include <linux/root_dev.h>
47
48 #include <asm/mmu.h>
49 #include <asm/processor.h>
50 #include <asm/io.h>
51 #include <asm/pgtable.h>
52 #include <asm/prom.h>
53 #include <asm/rtas.h>
54 #include <asm/pci-bridge.h>
55 #include <asm/iommu.h>
56 #include <asm/dma.h>
57 #include <asm/machdep.h>
58 #include <asm/irq.h>
59 #include <asm/naca.h>
60 #include <asm/time.h>
61 #include <asm/nvram.h>
62
63 #include "i8259.h"
64 #include "open_pic.h"
65 #include <asm/xics.h>
66 #include <asm/ppcdebug.h>
67 #include <asm/cputable.h>
68
69 #ifdef DEBUG
70 #define DBG(fmt...) udbg_printf(fmt)
71 #else
72 #define DBG(fmt...)
73 #endif
74
75 extern void pSeries_init_openpic(void);
76
77 extern void find_and_init_phbs(void);
78 extern void pSeries_final_fixup(void);
79
80 extern void pSeries_get_boot_time(struct rtc_time *rtc_time);
81 extern void pSeries_get_rtc_time(struct rtc_time *rtc_time);
82 extern int  pSeries_set_rtc_time(struct rtc_time *rtc_time);
83 extern void find_udbg_vterm(void);
84 extern void SystemReset_FWNMI(void), MachineCheck_FWNMI(void);  /* from head.S */
85 int fwnmi_active;  /* TRUE if an FWNMI handler is present */
86
87 unsigned long  virtPython0Facilities = 0;  // python0 facility area (memory mapped io) (64-bit format) VIRTUAL address.
88
89 extern unsigned long loops_per_jiffy;
90
91 extern unsigned long ppc_proc_freq;
92 extern unsigned long ppc_tb_freq;
93
94 void pSeries_get_cpuinfo(struct seq_file *m)
95 {
96         struct device_node *root;
97         const char *model = "";
98
99         root = of_find_node_by_path("/");
100         if (root)
101                 model = get_property(root, "model", NULL);
102         seq_printf(m, "machine\t\t: CHRP %s\n", model);
103         of_node_put(root);
104 }
105
106 /* Initialize firmware assisted non-maskable interrupts if
107  * the firmware supports this feature.
108  *
109  */
110 static void __init fwnmi_init(void)
111 {
112         int ret;
113         int ibm_nmi_register = rtas_token("ibm,nmi-register");
114         if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
115                 return;
116         ret = rtas_call(ibm_nmi_register, 2, 1, NULL,
117                         __pa((unsigned long)SystemReset_FWNMI),
118                         __pa((unsigned long)MachineCheck_FWNMI));
119         if (ret == 0)
120                 fwnmi_active = 1;
121 }
122
123 static void __init pSeries_setup_arch(void)
124 {
125         struct device_node *root;
126         unsigned int *opprop;
127
128         /* Fixup ppc_md depending on the type of interrupt controller */
129         if (naca->interrupt_controller == IC_OPEN_PIC) {
130                 ppc_md.init_IRQ       = pSeries_init_openpic; 
131                 ppc_md.get_irq        = openpic_get_irq;
132         } else {
133                 ppc_md.init_IRQ       = xics_init_IRQ;
134                 ppc_md.get_irq        = xics_get_irq;
135         }
136
137 #ifdef CONFIG_SMP
138         smp_init_pSeries();
139 #endif
140         /* openpic global configuration register (64-bit format). */
141         /* openpic Interrupt Source Unit pointer (64-bit format). */
142         /* python0 facility area (mmio) (64-bit format) REAL address. */
143
144         /* init to some ~sane value until calibrate_delay() runs */
145         loops_per_jiffy = 50000000;
146
147         if (ROOT_DEV == 0) {
148                 printk("No ramdisk, default root is /dev/sda2\n");
149                 ROOT_DEV = Root_SDA2;
150         }
151
152         fwnmi_init();
153
154         /* Find and initialize PCI host bridges */
155         /* iSeries needs to be done much later. */
156         eeh_init();
157         find_and_init_phbs();
158
159         /* Find the Open PIC if present */
160         root = of_find_node_by_path("/");
161         opprop = (unsigned int *) get_property(root,
162                                 "platform-open-pic", NULL);
163         if (opprop != 0) {
164                 int n = prom_n_addr_cells(root);
165                 unsigned long openpic;
166
167                 for (openpic = 0; n > 0; --n)
168                         openpic = (openpic << 32) + *opprop++;
169                 printk(KERN_DEBUG "OpenPIC addr: %lx\n", openpic);
170                 OpenPIC_Addr = __ioremap(openpic, 0x40000, _PAGE_NO_CACHE);
171         }
172         of_node_put(root);
173
174 #ifdef CONFIG_DUMMY_CONSOLE
175         conswitchp = &dummy_con;
176 #endif
177
178         pSeries_nvram_init();
179 }
180
181 static int __init pSeries_init_panel(void)
182 {
183         /* Manually leave the kernel version on the panel. */
184         ppc_md.progress("Linux ppc64\n", 0);
185         ppc_md.progress(UTS_RELEASE, 0);
186
187         return 0;
188 }
189 arch_initcall(pSeries_init_panel);
190
191
192
193 void __init pSeries_find_serial_port(void)
194 {
195         struct device_node *np;
196         unsigned long encode_phys_size = 32;
197         u32 *sizeprop;
198
199         struct isa_reg_property {
200                 u32 space;
201                 u32 address;
202                 u32 size;
203         };
204         struct pci_reg_property {
205                 struct pci_address addr;
206                 u32 size_hi;
207                 u32 size_lo;
208         };                                                                        
209
210         DBG(" -> pSeries_find_serial_port()\n");
211
212         naca->serialPortAddr = 0;
213
214         np = of_find_node_by_path("/");
215         if (!np)
216                 return;
217         sizeprop = (u32 *)get_property(np, "#size-cells", NULL);
218         if (sizeprop != NULL)
219                 encode_phys_size = (*sizeprop) << 5;
220         
221         for (np = NULL; (np = of_find_node_by_type(np, "serial"));) {
222                 struct device_node *isa, *pci;
223                 struct isa_reg_property *reg;
224                 union pci_range *rangesp;
225                 char *typep;
226
227                 typep = (char *)get_property(np, "ibm,aix-loc", NULL);
228                 if ((typep == NULL) || (typep && strcmp(typep, "S1")))
229                         continue;
230
231                 reg = (struct isa_reg_property *)get_property(np, "reg", NULL); 
232
233                 isa = of_get_parent(np);
234                 if (!isa) {
235                         DBG("no isa parent found\n");
236                         break;
237                 }
238                 pci = of_get_parent(isa);
239                 if (!pci) {
240                         DBG("no pci parent found\n");
241                         break;
242                 }
243
244                 rangesp = (union pci_range *)get_property(pci, "ranges", NULL);
245
246                 if ( encode_phys_size == 32 )
247                         naca->serialPortAddr = rangesp->pci32.phys+reg->address;
248                 else {
249                         naca->serialPortAddr =
250                                 ((((unsigned long)rangesp->pci64.phys_hi) << 32)
251                                 |
252                         (rangesp->pci64.phys_lo)) + reg->address;
253                 }
254                 break;
255         }
256
257         DBG(" <- pSeries_find_serial_port()\n");
258 }
259
260
261 /* Build up the firmware_features bitmask field
262  * using contents of device-tree/ibm,hypertas-functions.
263  * Ultimately this functionality may be moved into prom.c prom_init().
264  */
265 void __init fw_feature_init(void)
266 {
267         struct device_node * dn;
268         char * hypertas;
269         unsigned int len;
270
271         DBG(" -> fw_feature_init()\n");
272
273         cur_cpu_spec->firmware_features = 0;
274         dn = of_find_node_by_path("/rtas");
275         if (dn == NULL) {
276                 printk(KERN_ERR "WARNING ! Cannot find RTAS in device-tree !\n");
277                 goto no_rtas;
278         }
279
280         hypertas = get_property(dn, "ibm,hypertas-functions", &len);
281         if (hypertas) {
282                 while (len > 0){
283                         int i, hypertas_len;
284                         /* check value against table of strings */
285                         for(i=0; i < FIRMWARE_MAX_FEATURES ;i++) {
286                                 if ((firmware_features_table[i].name) &&
287                                     (strcmp(firmware_features_table[i].name,hypertas))==0) {
288                                         /* we have a match */
289                                         cur_cpu_spec->firmware_features |= 
290                                                 (firmware_features_table[i].val);
291                                         break;
292                                 } 
293                         }
294                         hypertas_len = strlen(hypertas);
295                         len -= hypertas_len +1;
296                         hypertas+= hypertas_len +1;
297                 }
298         }
299
300         of_node_put(dn);
301  no_rtas:
302         printk(KERN_INFO "firmware_features = 0x%lx\n", 
303                cur_cpu_spec->firmware_features);
304
305         DBG(" <- fw_feature_init()\n");
306 }
307
308
309 static  void __init pSeries_discover_pic(void)
310 {
311         struct device_node *np;
312         char *typep;
313
314         /*
315          * Setup interrupt mapping options that are needed for finish_device_tree
316          * to properly parse the OF interrupt tree & do the virtual irq mapping
317          */
318         __irq_offset_value = NUM_ISA_INTERRUPTS;
319         naca->interrupt_controller = IC_INVALID;
320         for (np = NULL; (np = of_find_node_by_name(np, "interrupt-controller"));) {
321                 typep = (char *)get_property(np, "compatible", NULL);
322                 if (strstr(typep, "open-pic"))
323                         naca->interrupt_controller = IC_OPEN_PIC;
324                 else if (strstr(typep, "ppc-xicp"))
325                         naca->interrupt_controller = IC_PPC_XIC;
326                 else
327                         printk("initialize_naca: failed to recognize"
328                                " interrupt-controller\n");
329                 break;
330         }
331 }
332
333 /*
334  * Early initialization.  Relocation is on but do not reference unbolted pages
335  */
336 static void __init pSeries_init_early(void)
337 {
338         void *comport;
339         int iommu_off = 0;
340
341         DBG(" -> pSeries_init_early()\n");
342
343         fw_feature_init();
344         
345         if (systemcfg->platform & PLATFORM_LPAR)
346                 hpte_init_lpar();
347         else {
348                 hpte_init_native();
349                 iommu_off = (of_chosen &&
350                              get_property(of_chosen, "linux,iommu-off", NULL));
351         }
352
353         pSeries_find_serial_port();
354
355         if (systemcfg->platform & PLATFORM_LPAR)
356                 find_udbg_vterm();
357         else if (naca->serialPortAddr) {
358                 /* Map the uart for udbg. */
359                 comport = (void *)__ioremap(naca->serialPortAddr, 16, _PAGE_NO_CACHE);
360                 udbg_init_uart(comport);
361
362                 ppc_md.udbg_putc = udbg_putc;
363                 ppc_md.udbg_getc = udbg_getc;
364                 ppc_md.udbg_getc_poll = udbg_getc_poll;
365                 DBG("Hello World !\n");
366         }
367
368
369         if (iommu_off)
370                 pci_dma_init_direct();
371         else
372                 tce_init_pSeries();
373
374         pSeries_discover_pic();
375
376         DBG(" <- pSeries_init_early()\n");
377 }
378
379
380 static void pSeries_progress(char *s, unsigned short hex)
381 {
382         struct device_node *root;
383         int width, *p;
384         char *os;
385         static int display_character, set_indicator;
386         static int max_width;
387         static spinlock_t progress_lock = SPIN_LOCK_UNLOCKED;
388         static int pending_newline = 0;  /* did last write end with unprinted newline? */
389
390         if (!rtas.base)
391                 return;
392
393         if (max_width == 0) {
394                 if ((root = find_path_device("/rtas")) &&
395                      (p = (unsigned int *)get_property(root,
396                                                        "ibm,display-line-length",
397                                                        NULL)))
398                         max_width = *p;
399                 else
400                         max_width = 0x10;
401                 display_character = rtas_token("display-character");
402                 set_indicator = rtas_token("set-indicator");
403         }
404
405         if (display_character == RTAS_UNKNOWN_SERVICE) {
406                 /* use hex display if available */
407                 if (set_indicator != RTAS_UNKNOWN_SERVICE)
408                         rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
409                 return;
410         }
411
412         spin_lock(&progress_lock);
413
414         /*
415          * Last write ended with newline, but we didn't print it since
416          * it would just clear the bottom line of output. Print it now
417          * instead.
418          *
419          * If no newline is pending, print a CR to start output at the
420          * beginning of the line.
421          */
422         if (pending_newline) {
423                 rtas_call(display_character, 1, 1, NULL, '\r');
424                 rtas_call(display_character, 1, 1, NULL, '\n');
425                 pending_newline = 0;
426         } else {
427                 rtas_call(display_character, 1, 1, NULL, '\r');
428         }
429  
430         width = max_width;
431         os = s;
432         while (*os) {
433                 if (*os == '\n' || *os == '\r') {
434                         /* Blank to end of line. */
435                         while (width-- > 0)
436                                 rtas_call(display_character, 1, 1, NULL, ' ');
437  
438                         /* If newline is the last character, save it
439                          * until next call to avoid bumping up the
440                          * display output.
441                          */
442                         if (*os == '\n' && !os[1]) {
443                                 pending_newline = 1;
444                                 spin_unlock(&progress_lock);
445                                 return;
446                         }
447  
448                         /* RTAS wants CR-LF, not just LF */
449  
450                         if (*os == '\n') {
451                                 rtas_call(display_character, 1, 1, NULL, '\r');
452                                 rtas_call(display_character, 1, 1, NULL, '\n');
453                         } else {
454                                 /* CR might be used to re-draw a line, so we'll
455                                  * leave it alone and not add LF.
456                                  */
457                                 rtas_call(display_character, 1, 1, NULL, *os);
458                         }
459  
460                         width = max_width;
461                 } else {
462                         width--;
463                         rtas_call(display_character, 1, 1, NULL, *os);
464                 }
465  
466                 os++;
467  
468                 /* if we overwrite the screen length */
469                 if (width <= 0)
470                         while ((*os != 0) && (*os != '\n') && (*os != '\r'))
471                                 os++;
472         }
473  
474         /* Blank to end of line. */
475         while (width-- > 0)
476                 rtas_call(display_character, 1, 1, NULL, ' ');
477
478         spin_unlock(&progress_lock);
479 }
480
481 extern void setup_default_decr(void);
482
483 /* Some sane defaults: 125 MHz timebase, 1GHz processor */
484 #define DEFAULT_TB_FREQ         125000000UL
485 #define DEFAULT_PROC_FREQ       (DEFAULT_TB_FREQ * 8)
486
487 static void __init pSeries_calibrate_decr(void)
488 {
489         struct device_node *cpu;
490         struct div_result divres;
491         unsigned int *fp;
492         int node_found;
493
494         /*
495          * The cpu node should have a timebase-frequency property
496          * to tell us the rate at which the decrementer counts.
497          */
498         cpu = of_find_node_by_type(NULL, "cpu");
499
500         ppc_tb_freq = DEFAULT_TB_FREQ;          /* hardcoded default */
501         node_found = 0;
502         if (cpu != 0) {
503                 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
504                                                   NULL);
505                 if (fp != 0) {
506                         node_found = 1;
507                         ppc_tb_freq = *fp;
508                 }
509         }
510         if (!node_found)
511                 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
512                                 "(not found)\n");
513
514         ppc_proc_freq = DEFAULT_PROC_FREQ;
515         node_found = 0;
516         if (cpu != 0) {
517                 fp = (unsigned int *)get_property(cpu, "clock-frequency",
518                                                   NULL);
519                 if (fp != 0) {
520                         node_found = 1;
521                         ppc_proc_freq = *fp;
522                 }
523         }
524         if (!node_found)
525                 printk(KERN_ERR "WARNING: Estimating processor frequency "
526                                 "(not found)\n");
527
528         of_node_put(cpu);
529
530         printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
531                ppc_tb_freq/1000000, ppc_tb_freq%1000000);
532         printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
533                ppc_proc_freq/1000000, ppc_proc_freq%1000000);
534
535         tb_ticks_per_jiffy = ppc_tb_freq / HZ;
536         tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
537         tb_ticks_per_usec = ppc_tb_freq / 1000000;
538         tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
539         div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
540         tb_to_xs = divres.result_low;
541
542         setup_default_decr();
543 }
544
545 /*
546  * Called very early, MMU is off, device-tree isn't unflattened
547  */
548 extern struct machdep_calls pSeries_md;
549
550 static int __init pSeries_probe(int platform)
551 {
552         if (platform != PLATFORM_PSERIES &&
553             platform != PLATFORM_PSERIES_LPAR)
554                 return 0;
555
556         /* if we have some ppc_md fixups for LPAR to do, do
557          * it here ...
558          */
559
560         return 1;
561 }
562
563 struct machdep_calls __initdata pSeries_md = {
564         .probe                  = pSeries_probe,
565         .setup_arch             = pSeries_setup_arch,
566         .init_early             = pSeries_init_early,
567         .get_cpuinfo            = pSeries_get_cpuinfo,
568         .log_error              = pSeries_log_error,
569         .pcibios_fixup          = pSeries_final_fixup,
570         .restart                = rtas_restart,
571         .power_off              = rtas_power_off,
572         .halt                   = rtas_halt,
573         .panic                  = rtas_os_term,
574         .get_boot_time          = pSeries_get_boot_time,
575         .get_rtc_time           = pSeries_get_rtc_time,
576         .set_rtc_time           = pSeries_set_rtc_time,
577         .calibrate_decr         = pSeries_calibrate_decr,
578         .progress               = pSeries_progress,
579 };