ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / sgi-ip27 / ip27-init.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General
3  * Public License.  See the file "COPYING" in the main directory of this
4  * archive for more details.
5  *
6  * Copyright (C) 2000 - 2001 by Kanoj Sarcar (kanoj@sgi.com)
7  * Copyright (C) 2000 - 2001 by Silicon Graphics, Inc.
8  */
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/mmzone.h>       /* for numnodes */
14 #include <linux/mm.h>
15 #include <linux/cpumask.h>
16 #include <asm/cpu.h>
17 #include <asm/io.h>
18 #include <asm/pgtable.h>
19 #include <asm/time.h>
20 #include <asm/sn/types.h>
21 #include <asm/sn/sn0/addrs.h>
22 #include <asm/sn/sn0/hubni.h>
23 #include <asm/sn/sn0/hubio.h>
24 #include <asm/sn/klconfig.h>
25 #include <asm/sn/ioc3.h>
26 #include <asm/mipsregs.h>
27 #include <asm/sn/gda.h>
28 #include <asm/sn/hub.h>
29 #include <asm/sn/intr.h>
30 #include <asm/current.h>
31 #include <asm/smp.h>
32 #include <asm/processor.h>
33 #include <asm/mmu_context.h>
34 #include <asm/thread_info.h>
35 #include <asm/sn/launch.h>
36 #include <asm/sn/sn_private.h>
37 #include <asm/sn/sn0/ip27.h>
38 #include <asm/sn/mapped_kernel.h>
39
40 #define CPU_NONE                (cpuid_t)-1
41
42 static DECLARE_BITMAP(hub_init_mask, MAX_COMPACT_NODES);
43 static hubreg_t region_mask;
44 static int      fine_mode;
45 static int router_distance;
46 nasid_t master_nasid = INVALID_NASID;
47
48 cnodeid_t       nasid_to_compact_node[MAX_NASIDS];
49 nasid_t         compact_to_nasid_node[MAX_COMPACT_NODES];
50 cnodeid_t       cpuid_to_compact_node[MAXCPUS];
51 char            node_distances[MAX_COMPACT_NODES][MAX_COMPACT_NODES];
52
53 static hubreg_t get_region(cnodeid_t cnode)
54 {
55         if (fine_mode)
56                 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
57         else
58                 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_COARSEREG_SHFT;
59 }
60
61 static void gen_region_mask(hubreg_t *region_mask, int maxnodes)
62 {
63         cnodeid_t cnode;
64
65         (*region_mask) = 0;
66         for (cnode = 0; cnode < maxnodes; cnode++) {
67                 (*region_mask) |= 1ULL << get_region(cnode);
68         }
69 }
70
71 static int is_fine_dirmode(void)
72 {
73         return (((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK)
74                 >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE);
75 }
76
77 extern void pcibr_setup(cnodeid_t);
78
79 static __init void per_slice_init(cnodeid_t cnode, int slice)
80 {
81         struct slice_data *si = hub_data[cnode]->slice + slice;
82         int cpu = smp_processor_id();
83         int i;
84
85         for (i = 0; i < LEVELS_PER_SLICE; i++)
86                 si->level_to_irq[i] = -1;
87         /*
88          * Some interrupts are reserved by hardware or by software convention.
89          * Mark these as reserved right away so they won't be used accidently
90          * later.
91          */
92         for (i = 0; i <= BASE_PCI_IRQ; i++) {
93                 __set_bit(i, si->irq_alloc_mask);
94                 LOCAL_HUB_S(PI_INT_PEND_MOD, i);
95         }
96
97         __set_bit(IP_PEND0_6_63, si->irq_alloc_mask);
98         LOCAL_HUB_S(PI_INT_PEND_MOD, IP_PEND0_6_63);
99
100         for (i = NI_BRDCAST_ERR_A; i <= MSC_PANIC_INTR; i++) {
101                 __set_bit(i, si->irq_alloc_mask + 1);
102                 LOCAL_HUB_S(PI_INT_PEND_MOD, i);
103         }
104
105         LOCAL_HUB_L(PI_INT_PEND0);
106
107         /*
108          * We use this so we can find the local hub's data as fast as only
109          * possible.
110          */
111         cpu_data[cpu].data = si;
112 }
113
114 extern void xtalk_probe_node(cnodeid_t nid);
115
116 void __init per_hub_init(cnodeid_t cnode)
117 {
118         struct hub_data *hub = HUB_DATA(cnode);
119         nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
120         int slice = LOCAL_HUB_L(PI_CPU_NUM);
121
122         cpu_set(smp_processor_id(), hub->h_cpus);
123
124         if (!test_and_set_bit(slice, &hub->slice_map))
125                 per_slice_init(cnode, slice);
126
127         if (test_and_set_bit(cnode, hub_init_mask))
128                 return;
129
130         /*
131          * Set CRB timeout at 5ms, (< PI timeout of 10ms)
132          */
133         REMOTE_HUB_S(nasid, IIO_ICTP, 0x800);
134         REMOTE_HUB_S(nasid, IIO_ICTO, 0xff);
135
136         hub_rtc_init(cnode);
137         xtalk_probe_node(cnode);
138
139 #ifdef CONFIG_REPLICATE_EXHANDLERS
140         /*
141          * If this is not a headless node initialization,
142          * copy over the caliased exception handlers.
143          */
144         if (get_compact_nodeid() == cnode) {
145                 extern char except_vec0, except_vec1_r10k;
146                 extern char except_vec2_generic, except_vec3_generic;
147
148                 memcpy((void *)(KSEG0 + 0x100), &except_vec2_generic, 0x80);
149                 memcpy((void *)(KSEG0 + 0x180), &except_vec3_generic, 0x80);
150                 memcpy((void *)KSEG0, &except_vec0, 0x80);
151                 memcpy((void *)KSEG0 + 0x080, &except_vec1_r10k, 0x80);
152                 memcpy((void *)(KSEG0 + 0x100), (void *) KSEG0, 0x80);
153                 memcpy((void *)(KSEG0 + 0x180), &except_vec3_generic, 0x100);
154                 __flush_cache_all();
155         }
156 #endif
157 }
158
159 /*
160  * get_nasid() returns the physical node id number of the caller.
161  */
162 nasid_t
163 get_nasid(void)
164 {
165         return (nasid_t)((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_NODEID_MASK)
166                          >> NSRI_NODEID_SHFT);
167 }
168
169 /*
170  * Map the physical node id to a virtual node id (virtual node ids are contiguous).
171  */
172 cnodeid_t get_compact_nodeid(void)
173 {
174         return NASID_TO_COMPACT_NODEID(get_nasid());
175 }
176
177 #define rou_rflag       rou_flags
178
179 static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
180 {
181         klrou_t *router;
182         lboard_t *brd;
183         int     port;
184
185         if (router_a->rou_rflag == 1)
186                 return;
187
188         if (depth >= router_distance)
189                 return;
190
191         router_a->rou_rflag = 1;
192
193         for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
194                 if (router_a->rou_port[port].port_nasid == INVALID_NASID)
195                         continue;
196
197                 brd = (lboard_t *)NODE_OFFSET_TO_K0(
198                         router_a->rou_port[port].port_nasid,
199                         router_a->rou_port[port].port_offset);
200
201                 if (brd->brd_type == KLTYPE_ROUTER) {
202                         router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
203                         if (router == router_b) {
204                                 if (depth < router_distance)
205                                         router_distance = depth;
206                         }
207                         else
208                                 router_recurse(router, router_b, depth + 1);
209                 }
210         }
211
212         router_a->rou_rflag = 0;
213 }
214
215 int node_distance(nasid_t nasid_a, nasid_t nasid_b)
216 {
217         klrou_t *router, *router_a = NULL, *router_b = NULL;
218         lboard_t *brd, *dest_brd;
219         cnodeid_t cnode;
220         nasid_t nasid;
221         int port;
222
223         /* Figure out which routers nodes in question are connected to */
224         for (cnode = 0; cnode < numnodes; cnode++) {
225                 nasid = COMPACT_TO_NASID_NODEID(cnode);
226
227                 if (nasid == -1) continue;
228
229                 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
230                                         KLTYPE_ROUTER);
231
232                 if (!brd)
233                         continue;
234
235                 do {
236                         if (brd->brd_flags & DUPLICATE_BOARD)
237                                 continue;
238
239                         router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
240                         router->rou_rflag = 0;
241
242                         for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
243                                 if (router->rou_port[port].port_nasid == INVALID_NASID)
244                                         continue;
245
246                                 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
247                                         router->rou_port[port].port_nasid,
248                                         router->rou_port[port].port_offset);
249
250                                 if (dest_brd->brd_type == KLTYPE_IP27) {
251                                         if (dest_brd->brd_nasid == nasid_a)
252                                                 router_a = router;
253                                         if (dest_brd->brd_nasid == nasid_b)
254                                                 router_b = router;
255                                 }
256                         }
257
258                 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
259         }
260
261         if (router_a == NULL) {
262                 printk("node_distance: router_a NULL\n");
263                 return -1;
264         }
265         if (router_b == NULL) {
266                 printk("node_distance: router_b NULL\n");
267                 return -1;
268         }
269
270         if (nasid_a == nasid_b)
271                 return 0;
272
273         if (router_a == router_b)
274                 return 1;
275
276         router_distance = 100;
277         router_recurse(router_a, router_b, 2);
278
279         return router_distance;
280 }
281
282 static void init_topology_matrix(void)
283 {
284         nasid_t nasid, nasid2;
285         cnodeid_t row, col;
286
287         for (row = 0; row < MAX_COMPACT_NODES; row++)
288                 for (col = 0; col < MAX_COMPACT_NODES; col++)
289                         node_distances[row][col] = -1;
290
291         for (row = 0; row < numnodes; row++) {
292                 nasid = COMPACT_TO_NASID_NODEID(row);
293                 for (col = 0; col < numnodes; col++) {
294                         nasid2 = COMPACT_TO_NASID_NODEID(col);
295                         node_distances[row][col] = node_distance(nasid, nasid2);
296                 }
297         }
298 }
299
300 static void dump_topology(void)
301 {
302         nasid_t nasid;
303         cnodeid_t cnode;
304         lboard_t *brd, *dest_brd;
305         int port;
306         int router_num = 0;
307         klrou_t *router;
308         cnodeid_t row, col;
309
310         printk("************** Topology ********************\n");
311
312         printk("    ");
313         for (col = 0; col < numnodes; col++)
314                 printk("%02d ", col);
315         printk("\n");
316         for (row = 0; row < numnodes; row++) {
317                 printk("%02d  ", row);
318                 for (col = 0; col < numnodes; col++)
319                         printk("%2d ", node_distances[row][col]);
320                 printk("\n");
321         }
322
323         for (cnode = 0; cnode < numnodes; cnode++) {
324                 nasid = COMPACT_TO_NASID_NODEID(cnode);
325
326                 if (nasid == -1) continue;
327
328                 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
329                                         KLTYPE_ROUTER);
330
331                 if (!brd)
332                         continue;
333
334                 do {
335                         if (brd->brd_flags & DUPLICATE_BOARD)
336                                 continue;
337                         printk("Router %d:", router_num);
338                         router_num++;
339
340                         router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
341
342                         for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
343                                 if (router->rou_port[port].port_nasid == INVALID_NASID)
344                                         continue;
345
346                                 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
347                                         router->rou_port[port].port_nasid,
348                                         router->rou_port[port].port_offset);
349
350                                 if (dest_brd->brd_type == KLTYPE_IP27)
351                                         printk(" %d", dest_brd->brd_nasid);
352                                 if (dest_brd->brd_type == KLTYPE_ROUTER)
353                                         printk(" r");
354                         }
355                         printk("\n");
356
357                 } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
358         }
359 }
360
361 void mlreset(void)
362 {
363         int i;
364
365         master_nasid = get_nasid();
366         fine_mode = is_fine_dirmode();
367
368         /*
369          * Probe for all CPUs - this creates the cpumask and sets up the
370          * mapping tables.  We need to do this as early as possible.
371          */
372 #ifdef CONFIG_SMP
373         cpu_node_probe();
374 #endif
375
376         init_topology_matrix();
377         dump_topology();
378
379         gen_region_mask(&region_mask, numnodes);
380
381         setup_replication_mask(numnodes);
382
383         /*
384          * Set all nodes' calias sizes to 8k
385          */
386         for (i = 0; i < numnodes; i++) {
387                 nasid_t nasid;
388
389                 nasid = COMPACT_TO_NASID_NODEID(i);
390
391                 /*
392                  * Always have node 0 in the region mask, otherwise
393                  * CALIAS accesses get exceptions since the hub
394                  * thinks it is a node 0 address.
395                  */
396                 REMOTE_HUB_S(nasid, PI_REGION_PRESENT, (region_mask | 1));
397 #ifdef CONFIG_REPLICATE_EXHANDLERS
398                 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_8K);
399 #else
400                 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
401 #endif
402
403 #ifdef LATER
404                 /*
405                  * Set up all hubs to have a big window pointing at
406                  * widget 0. Memory mode, widget 0, offset 0
407                  */
408                 REMOTE_HUB_S(nasid, IIO_ITTE(SWIN0_BIGWIN),
409                         ((HUB_PIO_MAP_TO_MEM << IIO_ITTE_IOSP_SHIFT) |
410                         (0 << IIO_ITTE_WIDGET_SHIFT)));
411 #endif
412         }
413 }
414
415 /* Extracted from the IOC3 meta driver.  FIXME.  */
416 static inline void ioc3_sio_init(void)
417 {
418         struct ioc3 *ioc3;
419         nasid_t nid;
420         long loops;
421
422         nid = get_nasid();
423         ioc3 = (struct ioc3 *) KL_CONFIG_CH_CONS_INFO(nid)->memory_base;
424
425         ioc3->sscr_a = 0;                       /* PIO mode for uarta.  */
426         ioc3->sscr_b = 0;                       /* PIO mode for uartb.  */
427         ioc3->sio_iec = ~0;
428         ioc3->sio_ies = (SIO_IR_SA_INT | SIO_IR_SB_INT);
429
430         loops=1000000; while(loops--);
431         ioc3->sregs.uarta.iu_fcr = 0;
432         ioc3->sregs.uartb.iu_fcr = 0;
433         loops=1000000; while(loops--);
434 }
435
436 static inline void ioc3_eth_init(void)
437 {
438         struct ioc3 *ioc3;
439         nasid_t nid;
440
441         nid = get_nasid();
442         ioc3 = (struct ioc3 *) KL_CONFIG_CH_CONS_INFO(nid)->memory_base;
443
444         ioc3->eier = 0;
445 }
446
447 void __init per_cpu_init(void)
448 {
449         cnodeid_t cnode = get_compact_nodeid();
450         int cpu = smp_processor_id();
451
452         clear_c0_status(ST0_IM);
453         per_hub_init(cnode);
454         cpu_time_init();
455         install_ipi();
456         /* Install our NMI handler if symmon hasn't installed one. */
457         install_cpu_nmi_handler(cputoslice(cpu));
458         set_c0_status(SRB_DEV0 | SRB_DEV1);
459 }
460
461 extern void ip27_setup_console(void);
462 extern void ip27_time_init(void);
463 extern void ip27_reboot_setup(void);
464
465 static int __init ip27_setup(void)
466 {
467         hubreg_t p, e, n_mode;
468         nasid_t nid;
469
470         ip27_setup_console();
471         ip27_reboot_setup();
472
473         /*
474          * hub_rtc init and cpu clock intr enabled for later calibrate_delay.
475          */
476         nid = get_nasid();
477         printk("IP27: Running on node %d.\n", nid);
478
479         p = LOCAL_HUB_L(PI_CPU_PRESENT_A) & 1;
480         e = LOCAL_HUB_L(PI_CPU_ENABLE_A) & 1;
481         printk("Node %d has %s primary CPU%s.\n", nid,
482                p ? "a" : "no",
483                e ? ", CPU is running" : "");
484
485         p = LOCAL_HUB_L(PI_CPU_PRESENT_B) & 1;
486         e = LOCAL_HUB_L(PI_CPU_ENABLE_B) & 1;
487         printk("Node %d has %s secondary CPU%s.\n", nid,
488                p ? "a" : "no",
489                e ? ", CPU is running" : "");
490
491         /*
492          * Try to catch kernel missconfigurations and give user an
493          * indication what option to select.
494          */
495         n_mode = LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_MORENODES_MASK;
496         printk("Machine is in %c mode.\n", n_mode ? 'N' : 'M');
497 #ifdef CONFIG_SGI_SN0_N_MODE
498         if (!n_mode)
499                 panic("Kernel compiled for M mode.");
500 #else
501         if (n_mode)
502                 panic("Kernel compiled for N mode.");
503 #endif
504
505         ioc3_sio_init();
506         ioc3_eth_init();
507         per_cpu_init();
508
509         set_io_port_base(IO_BASE);
510
511         board_time_init = ip27_time_init;
512
513         return 0;
514 }
515
516 early_initcall(ip27_setup);