vserver 1.9.5.x5
[linux-2.6.git] / arch / ia64 / sn / kernel / setup.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1999,2001-2004 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/kdev_t.h>
15 #include <linux/string.h>
16 #include <linux/tty.h>
17 #include <linux/console.h>
18 #include <linux/timex.h>
19 #include <linux/sched.h>
20 #include <linux/ioport.h>
21 #include <linux/mm.h>
22 #include <linux/serial.h>
23 #include <linux/irq.h>
24 #include <linux/bootmem.h>
25 #include <linux/mmzone.h>
26 #include <linux/interrupt.h>
27 #include <linux/acpi.h>
28 #include <linux/compiler.h>
29 #include <linux/sched.h>
30 #include <linux/root_dev.h>
31 #include <linux/nodemask.h>
32
33 #include <asm/io.h>
34 #include <asm/sal.h>
35 #include <asm/machvec.h>
36 #include <asm/system.h>
37 #include <asm/processor.h>
38 #include <asm/sn/arch.h>
39 #include <asm/sn/addrs.h>
40 #include <asm/sn/pda.h>
41 #include <asm/sn/nodepda.h>
42 #include <asm/sn/sn_cpuid.h>
43 #include <asm/sn/simulator.h>
44 #include <asm/sn/leds.h>
45 #include <asm/sn/bte.h>
46 #include <asm/sn/shub_mmr.h>
47 #include <asm/sn/clksupport.h>
48 #include <asm/sn/sn_sal.h>
49 #include <asm/sn/geo.h>
50 #include "xtalk/xwidgetdev.h"
51 #include "xtalk/hubdev.h"
52 #include <asm/sn/klconfig.h>
53
54
55 DEFINE_PER_CPU(struct pda_s, pda_percpu);
56
57 #define MAX_PHYS_MEMORY         (1UL << 49)     /* 1 TB */
58
59 lboard_t *root_lboard[MAX_COMPACT_NODES];
60
61 extern void bte_init_node(nodepda_t *, cnodeid_t);
62
63 extern void sn_timer_init(void);
64 extern unsigned long last_time_offset;
65 extern void (*ia64_mark_idle) (int);
66 extern void snidle(int);
67 extern unsigned char acpi_kbd_controller_present;
68
69 unsigned long sn_rtc_cycles_per_second;
70
71 EXPORT_SYMBOL(sn_rtc_cycles_per_second);
72
73 partid_t sn_partid = -1;
74 EXPORT_SYMBOL(sn_partid);
75 char sn_system_serial_number_string[128];
76 EXPORT_SYMBOL(sn_system_serial_number_string);
77 u64 sn_partition_serial_number;
78 EXPORT_SYMBOL(sn_partition_serial_number);
79
80 short physical_node_map[MAX_PHYSNODE_ID];
81
82 EXPORT_SYMBOL(physical_node_map);
83
84 int numionodes;
85
86 static void sn_init_pdas(char **);
87 static void scan_for_ionodes(void);
88
89 static nodepda_t *nodepdaindr[MAX_COMPACT_NODES];
90
91 /*
92  * The format of "screen_info" is strange, and due to early i386-setup
93  * code. This is just enough to make the console code think we're on a
94  * VGA color display.
95  */
96 struct screen_info sn_screen_info = {
97         .orig_x = 0,
98         .orig_y = 0,
99         .orig_video_mode = 3,
100         .orig_video_cols = 80,
101         .orig_video_ega_bx = 3,
102         .orig_video_lines = 25,
103         .orig_video_isVGA = 1,
104         .orig_video_points = 16
105 };
106
107 /*
108  * This is here so we can use the CMOS detection in ide-probe.c to
109  * determine what drives are present.  In theory, we don't need this
110  * as the auto-detection could be done via ide-probe.c:do_probe() but
111  * in practice that would be much slower, which is painful when
112  * running in the simulator.  Note that passing zeroes in DRIVE_INFO
113  * is sufficient (the IDE driver will autodetect the drive geometry).
114  */
115 #ifdef CONFIG_IA64_GENERIC
116 extern char drive_info[4 * 16];
117 #else
118 char drive_info[4 * 16];
119 #endif
120
121 /*
122  * Get nasid of current cpu early in boot before nodepda is initialized
123  */
124 static int
125 boot_get_nasid(void)
126 {
127         int nasid;
128
129         if (ia64_sn_get_sapic_info(get_sapicid(), &nasid, NULL, NULL))
130                 BUG();
131         return nasid;
132 }
133
134 /*
135  * This routine can only be used during init, since
136  * smp_boot_data is an init data structure.
137  * We have to use smp_boot_data.cpu_phys_id to find
138  * the physical id of the processor because the normal
139  * cpu_physical_id() relies on data structures that
140  * may not be initialized yet.
141  */
142
143 static int __init pxm_to_nasid(int pxm)
144 {
145         int i;
146         int nid;
147
148         nid = pxm_to_nid_map[pxm];
149         for (i = 0; i < num_node_memblks; i++) {
150                 if (node_memblk[i].nid == nid) {
151                         return NASID_GET(node_memblk[i].start_paddr);
152                 }
153         }
154         return -1;
155 }
156
157 /**
158  * early_sn_setup - early setup routine for SN platforms
159  *
160  * Sets up an initial console to aid debugging.  Intended primarily
161  * for bringup.  See start_kernel() in init/main.c.
162  */
163
164 void __init early_sn_setup(void)
165 {
166         efi_system_table_t *efi_systab;
167         efi_config_table_t *config_tables;
168         struct ia64_sal_systab *sal_systab;
169         struct ia64_sal_desc_entry_point *ep;
170         char *p;
171         int i, j;
172
173         /*
174          * Parse enough of the SAL tables to locate the SAL entry point. Since, console
175          * IO on SN2 is done via SAL calls, early_printk won't work without this.
176          *
177          * This code duplicates some of the ACPI table parsing that is in efi.c & sal.c.
178          * Any changes to those file may have to be made hereas well.
179          */
180         efi_systab = (efi_system_table_t *) __va(ia64_boot_param->efi_systab);
181         config_tables = __va(efi_systab->tables);
182         for (i = 0; i < efi_systab->nr_tables; i++) {
183                 if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) ==
184                     0) {
185                         sal_systab = __va(config_tables[i].table);
186                         p = (char *)(sal_systab + 1);
187                         for (j = 0; j < sal_systab->entry_count; j++) {
188                                 if (*p == SAL_DESC_ENTRY_POINT) {
189                                         ep = (struct ia64_sal_desc_entry_point
190                                               *)p;
191                                         ia64_sal_handler_init(__va
192                                                               (ep->sal_proc),
193                                                               __va(ep->gp));
194                                         return;
195                                 }
196                                 p += SAL_DESC_SIZE(*p);
197                         }
198                 }
199         }
200         /* Uh-oh, SAL not available?? */
201         printk(KERN_ERR "failed to find SAL entry point\n");
202 }
203
204 extern int platform_intr_list[];
205 extern nasid_t master_nasid;
206 static int shub_1_1_found __initdata;
207
208 /*
209  * sn_check_for_wars
210  *
211  * Set flag for enabling shub specific wars
212  */
213
214 static inline int __init is_shub_1_1(int nasid)
215 {
216         unsigned long id;
217         int rev;
218
219         if (is_shub2())
220                 return 0;
221         id = REMOTE_HUB_L(nasid, SH1_SHUB_ID);
222         rev = (id & SH1_SHUB_ID_REVISION_MASK) >> SH1_SHUB_ID_REVISION_SHFT;
223         return rev <= 2;
224 }
225
226 static void __init sn_check_for_wars(void)
227 {
228         int cnode;
229
230         if (is_shub2()) {
231                 /* none yet */
232         } else {
233                 for_each_online_node(cnode) {
234                         if (is_shub_1_1(cnodeid_to_nasid(cnode)))
235                                 shub_1_1_found = 1;
236                 }
237         }
238 }
239
240 /**
241  * sn_setup - SN platform setup routine
242  * @cmdline_p: kernel command line
243  *
244  * Handles platform setup for SN machines.  This includes determining
245  * the RTC frequency (via a SAL call), initializing secondary CPUs, and
246  * setting up per-node data areas.  The console is also initialized here.
247  */
248 void __init sn_setup(char **cmdline_p)
249 {
250         long status, ticks_per_sec, drift;
251         int pxm;
252         int major = sn_sal_rev_major(), minor = sn_sal_rev_minor();
253         extern void sn_cpu_init(void);
254
255         /*
256          * If the generic code has enabled vga console support - lets
257          * get rid of it again. This is a kludge for the fact that ACPI
258          * currtently has no way of informing us if legacy VGA is available
259          * or not.
260          */
261 #if defined(CONFIG_VT) && defined(CONFIG_VGA_CONSOLE)
262         if (conswitchp == &vga_con) {
263                 printk(KERN_DEBUG "SGI: Disabling VGA console\n");
264 #ifdef CONFIG_DUMMY_CONSOLE
265                 conswitchp = &dummy_con;
266 #else
267                 conswitchp = NULL;
268 #endif                          /* CONFIG_DUMMY_CONSOLE */
269         }
270 #endif                          /* def(CONFIG_VT) && def(CONFIG_VGA_CONSOLE) */
271
272         MAX_DMA_ADDRESS = PAGE_OFFSET + MAX_PHYS_MEMORY;
273
274         memset(physical_node_map, -1, sizeof(physical_node_map));
275         for (pxm = 0; pxm < MAX_PXM_DOMAINS; pxm++)
276                 if (pxm_to_nid_map[pxm] != -1)
277                         physical_node_map[pxm_to_nasid(pxm)] =
278                             pxm_to_nid_map[pxm];
279
280         /*
281          * Old PROMs do not provide an ACPI FADT. Disable legacy keyboard
282          * support here so we don't have to listen to failed keyboard probe
283          * messages.
284          */
285         if ((major < 2 || (major == 2 && minor <= 9)) &&
286             acpi_kbd_controller_present) {
287                 printk(KERN_INFO "Disabling legacy keyboard support as prom "
288                        "is too old and doesn't provide FADT\n");
289                 acpi_kbd_controller_present = 0;
290         }
291
292         printk("SGI SAL version %x.%02x\n", major, minor);
293
294         /*
295          * Confirm the SAL we're running on is recent enough...
296          */
297         if ((major < SN_SAL_MIN_MAJOR) || (major == SN_SAL_MIN_MAJOR &&
298                                            minor < SN_SAL_MIN_MINOR)) {
299                 printk(KERN_ERR "This kernel needs SGI SAL version >= "
300                        "%x.%02x\n", SN_SAL_MIN_MAJOR, SN_SAL_MIN_MINOR);
301                 panic("PROM version too old\n");
302         }
303
304         master_nasid = boot_get_nasid();
305
306         status =
307             ia64_sal_freq_base(SAL_FREQ_BASE_REALTIME_CLOCK, &ticks_per_sec,
308                                &drift);
309         if (status != 0 || ticks_per_sec < 100000) {
310                 printk(KERN_WARNING
311                        "unable to determine platform RTC clock frequency, guessing.\n");
312                 /* PROM gives wrong value for clock freq. so guess */
313                 sn_rtc_cycles_per_second = 1000000000000UL / 30000UL;
314         } else
315                 sn_rtc_cycles_per_second = ticks_per_sec;
316
317         platform_intr_list[ACPI_INTERRUPT_CPEI] = IA64_CPE_VECTOR;
318
319         /*
320          * we set the default root device to /dev/hda
321          * to make simulation easy
322          */
323         ROOT_DEV = Root_HDA1;
324
325         /*
326          * Create the PDAs and NODEPDAs for all the cpus.
327          */
328         sn_init_pdas(cmdline_p);
329
330         ia64_mark_idle = &snidle;
331
332         /* 
333          * For the bootcpu, we do this here. All other cpus will make the
334          * call as part of cpu_init in slave cpu initialization.
335          */
336         sn_cpu_init();
337
338 #ifdef CONFIG_SMP
339         init_smp_config();
340 #endif
341         screen_info = sn_screen_info;
342
343         sn_timer_init();
344 }
345
346 /**
347  * sn_init_pdas - setup node data areas
348  *
349  * One time setup for Node Data Area.  Called by sn_setup().
350  */
351 static void __init sn_init_pdas(char **cmdline_p)
352 {
353         cnodeid_t cnode;
354
355         memset(pda->cnodeid_to_nasid_table, -1,
356                sizeof(pda->cnodeid_to_nasid_table));
357         for_each_online_node(cnode)
358                 pda->cnodeid_to_nasid_table[cnode] =
359                     pxm_to_nasid(nid_to_pxm_map[cnode]);
360
361         numionodes = num_online_nodes();
362         scan_for_ionodes();
363
364         /*
365          * Allocate & initalize the nodepda for each node.
366          */
367         for_each_online_node(cnode) {
368                 nodepdaindr[cnode] =
369                     alloc_bootmem_node(NODE_DATA(cnode), sizeof(nodepda_t));
370                 memset(nodepdaindr[cnode], 0, sizeof(nodepda_t));
371                 memset(nodepdaindr[cnode]->phys_cpuid, -1, 
372                     sizeof(nodepdaindr[cnode]->phys_cpuid));
373         }
374
375         /*
376          * Allocate & initialize nodepda for TIOs.  For now, put them on node 0.
377          */
378         for (cnode = num_online_nodes(); cnode < numionodes; cnode++) {
379                 nodepdaindr[cnode] =
380                     alloc_bootmem_node(NODE_DATA(0), sizeof(nodepda_t));
381                 memset(nodepdaindr[cnode], 0, sizeof(nodepda_t));
382         }
383
384         /*
385          * Now copy the array of nodepda pointers to each nodepda.
386          */
387         for (cnode = 0; cnode < numionodes; cnode++)
388                 memcpy(nodepdaindr[cnode]->pernode_pdaindr, nodepdaindr,
389                        sizeof(nodepdaindr));
390
391         /*
392          * Set up IO related platform-dependent nodepda fields.
393          * The following routine actually sets up the hubinfo struct
394          * in nodepda.
395          */
396         for_each_online_node(cnode) {
397                 bte_init_node(nodepdaindr[cnode], cnode);
398         }
399
400         /*
401          * Initialize the per node hubdev.  This includes IO Nodes and 
402          * headless/memless nodes.
403          */
404         for (cnode = 0; cnode < numionodes; cnode++) {
405                 hubdev_init_node(nodepdaindr[cnode], cnode);
406         }
407 }
408
409 /**
410  * sn_cpu_init - initialize per-cpu data areas
411  * @cpuid: cpuid of the caller
412  *
413  * Called during cpu initialization on each cpu as it starts.
414  * Currently, initializes the per-cpu data area for SNIA.
415  * Also sets up a few fields in the nodepda.  Also known as
416  * platform_cpu_init() by the ia64 machvec code.
417  */
418 void __init sn_cpu_init(void)
419 {
420         int cpuid;
421         int cpuphyid;
422         int nasid;
423         int subnode;
424         int slice;
425         int cnode;
426         int i;
427         u64 shubtype, nasid_bitmask, nasid_shift;
428         static int wars_have_been_checked;
429
430         memset(pda, 0, sizeof(pda));
431         if (ia64_sn_get_hub_info(0, &shubtype, &nasid_bitmask, &nasid_shift))
432                 BUG();
433         pda->shub2 = (u8)shubtype;
434         pda->nasid_bitmask = (u16)nasid_bitmask;
435         pda->nasid_shift = (u8)nasid_shift;
436         pda->as_shift = pda->nasid_shift - 2;
437
438         /*
439          * The boot cpu makes this call again after platform initialization is
440          * complete.
441          */
442         if (nodepdaindr[0] == NULL)
443                 return;
444
445         cpuid = smp_processor_id();
446         cpuphyid = get_sapicid();
447
448         if (ia64_sn_get_sapic_info(cpuphyid, &nasid, &subnode, &slice))
449                 BUG();
450
451         for (i=0; i < MAX_NUMNODES; i++) {
452                 if (nodepdaindr[i]) {
453                         nodepdaindr[i]->phys_cpuid[cpuid].nasid = nasid;
454                         nodepdaindr[i]->phys_cpuid[cpuid].slice = slice;
455                         nodepdaindr[i]->phys_cpuid[cpuid].subnode = subnode;
456                 }
457         }
458
459         cnode = nasid_to_cnodeid(nasid);
460
461         pda->p_nodepda = nodepdaindr[cnode];
462         pda->led_address =
463             (typeof(pda->led_address)) (LED0 + (slice << LED_CPU_SHIFT));
464         pda->led_state = LED_ALWAYS_SET;
465         pda->hb_count = HZ / 2;
466         pda->hb_state = 0;
467         pda->idle_flag = 0;
468
469         if (cpuid != 0) {
470                 memcpy(pda->cnodeid_to_nasid_table,
471                        pdacpu(0)->cnodeid_to_nasid_table,
472                        sizeof(pda->cnodeid_to_nasid_table));
473         }
474
475         /*
476          * Check for WARs.
477          * Only needs to be done once, on BSP.
478          * Has to be done after loop above, because it uses pda.cnodeid_to_nasid_table[i].
479          * Has to be done before assignment below.
480          */
481         if (!wars_have_been_checked) {
482                 sn_check_for_wars();
483                 wars_have_been_checked = 1;
484         }
485         pda->shub_1_1_found = shub_1_1_found;
486
487         /*
488          * Set up addresses of PIO/MEM write status registers.
489          */
490         {
491                 u64 pio1[] = {SH1_PIO_WRITE_STATUS_0, 0, SH1_PIO_WRITE_STATUS_1, 0};
492                 u64 pio2[] = {SH2_PIO_WRITE_STATUS_0, SH2_PIO_WRITE_STATUS_1, 
493                         SH2_PIO_WRITE_STATUS_2, SH2_PIO_WRITE_STATUS_3};
494                 u64 *pio;
495                 pio = is_shub1() ? pio1 : pio2;
496                 pda->pio_write_status_addr = (volatile unsigned long *) LOCAL_MMR_ADDR(pio[slice]);
497                 pda->pio_write_status_val = is_shub1() ? SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK : 0;
498         }
499
500         /*
501          * WAR addresses for SHUB 1.x.
502          */
503         if (local_node_data->active_cpu_count++ == 0 && is_shub1()) {
504                 int buddy_nasid;
505                 buddy_nasid =
506                     cnodeid_to_nasid(numa_node_id() ==
507                                      num_online_nodes() - 1 ? 0 : numa_node_id() + 1);
508                 pda->pio_shub_war_cam_addr =
509                     (volatile unsigned long *)GLOBAL_MMR_ADDR(nasid,
510                                                               SH1_PI_CAM_CONTROL);
511         }
512 }
513
514 /*
515  * Scan klconfig for ionodes.  Add the nasids to the
516  * physical_node_map and the pda and increment numionodes.
517  */
518
519 static void __init scan_for_ionodes(void)
520 {
521         int nasid = 0;
522         lboard_t *brd;
523
524         /* Setup ionodes with memory */
525         for (nasid = 0; nasid < MAX_PHYSNODE_ID; nasid += 2) {
526                 char *klgraph_header;
527                 cnodeid_t cnodeid;
528
529                 if (physical_node_map[nasid] == -1)
530                         continue;
531
532                 cnodeid = -1;
533                 klgraph_header = __va(ia64_sn_get_klconfig_addr(nasid));
534                 if (!klgraph_header) {
535                         if (IS_RUNNING_ON_SIMULATOR())
536                                 continue;
537                         BUG();  /* All nodes must have klconfig tables! */
538                 }
539                 cnodeid = nasid_to_cnodeid(nasid);
540                 root_lboard[cnodeid] = (lboard_t *)
541                     NODE_OFFSET_TO_LBOARD((nasid),
542                                           ((kl_config_hdr_t
543                                             *) (klgraph_header))->
544                                           ch_board_info);
545         }
546
547         /* Scan headless/memless IO Nodes. */
548         for (nasid = 0; nasid < MAX_PHYSNODE_ID; nasid += 2) {
549                 /* if there's no nasid, don't try to read the klconfig on the node */
550                 if (physical_node_map[nasid] == -1)
551                         continue;
552                 brd = find_lboard_any((lboard_t *)
553                                       root_lboard[nasid_to_cnodeid(nasid)],
554                                       KLTYPE_SNIA);
555                 if (brd) {
556                         brd = KLCF_NEXT_ANY(brd);       /* Skip this node's lboard */
557                         if (!brd)
558                                 continue;
559                 }
560
561                 brd = find_lboard_any(brd, KLTYPE_SNIA);
562
563                 while (brd) {
564                         pda->cnodeid_to_nasid_table[numionodes] =
565                             brd->brd_nasid;
566                         physical_node_map[brd->brd_nasid] = numionodes;
567                         root_lboard[numionodes] = brd;
568                         numionodes++;
569                         brd = KLCF_NEXT_ANY(brd);
570                         if (!brd)
571                                 break;
572
573                         brd = find_lboard_any(brd, KLTYPE_SNIA);
574                 }
575         }
576
577         /* Scan for TIO nodes. */
578         for (nasid = 0; nasid < MAX_PHYSNODE_ID; nasid += 2) {
579                 /* if there's no nasid, don't try to read the klconfig on the node */
580                 if (physical_node_map[nasid] == -1)
581                         continue;
582                 brd = find_lboard_any((lboard_t *)
583                                       root_lboard[nasid_to_cnodeid(nasid)],
584                                       KLTYPE_TIO);
585                 while (brd) {
586                         pda->cnodeid_to_nasid_table[numionodes] =
587                             brd->brd_nasid;
588                         physical_node_map[brd->brd_nasid] = numionodes;
589                         root_lboard[numionodes] = brd;
590                         numionodes++;
591                         brd = KLCF_NEXT_ANY(brd);
592                         if (!brd)
593                                 break;
594
595                         brd = find_lboard_any(brd, KLTYPE_TIO);
596                 }
597         }
598
599 }
600
601 int
602 nasid_slice_to_cpuid(int nasid, int slice)
603 {
604         long cpu;
605         
606         for (cpu=0; cpu < NR_CPUS; cpu++) 
607                 if (nodepda->phys_cpuid[cpu].nasid == nasid && nodepda->phys_cpuid[cpu].slice == slice)
608                         return cpu;
609
610         return -1;
611 }