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.
6 * Copyright (C) 2000 - 2001 by Kanoj Sarcar (kanoj@sgi.com)
7 * Copyright (C) 2000 - 2001 by Silicon Graphics, Inc.
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 */
15 #include <linux/cpumask.h>
18 #include <asm/pgtable.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>
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>
40 #define CPU_NONE (cpuid_t)-1
42 static DECLARE_BITMAP(hub_init_mask, MAX_COMPACT_NODES);
43 static hubreg_t region_mask;
45 static int router_distance;
46 nasid_t master_nasid = INVALID_NASID;
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];
53 static hubreg_t get_region(cnodeid_t cnode)
56 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_FINEREG_SHFT;
58 return COMPACT_TO_NASID_NODEID(cnode) >> NASID_TO_COARSEREG_SHFT;
61 static void gen_region_mask(hubreg_t *region_mask, int maxnodes)
66 for (cnode = 0; cnode < maxnodes; cnode++) {
67 (*region_mask) |= 1ULL << get_region(cnode);
71 static int is_fine_dirmode(void)
73 return (((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_REGIONSIZE_MASK)
74 >> NSRI_REGIONSIZE_SHFT) & REGIONSIZE_FINE);
77 extern void pcibr_setup(cnodeid_t);
79 static __init void per_slice_init(cnodeid_t cnode, int slice)
81 struct slice_data *si = hub_data[cnode]->slice + slice;
82 int cpu = smp_processor_id();
85 for (i = 0; i < LEVELS_PER_SLICE; i++)
86 si->level_to_irq[i] = -1;
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
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);
97 __set_bit(IP_PEND0_6_63, si->irq_alloc_mask);
98 LOCAL_HUB_S(PI_INT_PEND_MOD, IP_PEND0_6_63);
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);
105 LOCAL_HUB_L(PI_INT_PEND0);
108 * We use this so we can find the local hub's data as fast as only
111 cpu_data[cpu].data = si;
114 extern void xtalk_probe_node(cnodeid_t nid);
116 void __init per_hub_init(cnodeid_t cnode)
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);
122 cpu_set(smp_processor_id(), hub->h_cpus);
124 if (!test_and_set_bit(slice, &hub->slice_map))
125 per_slice_init(cnode, slice);
127 if (test_and_set_bit(cnode, hub_init_mask))
131 * Set CRB timeout at 5ms, (< PI timeout of 10ms)
133 REMOTE_HUB_S(nasid, IIO_ICTP, 0x800);
134 REMOTE_HUB_S(nasid, IIO_ICTO, 0xff);
137 xtalk_probe_node(cnode);
139 #ifdef CONFIG_REPLICATE_EXHANDLERS
141 * If this is not a headless node initialization,
142 * copy over the caliased exception handlers.
144 if (get_compact_nodeid() == cnode) {
145 extern char except_vec0, except_vec1_r10k;
146 extern char except_vec2_generic, except_vec3_generic;
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);
160 * get_nasid() returns the physical node id number of the caller.
165 return (nasid_t)((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_NODEID_MASK)
166 >> NSRI_NODEID_SHFT);
170 * Map the physical node id to a virtual node id (virtual node ids are contiguous).
172 cnodeid_t get_compact_nodeid(void)
174 return NASID_TO_COMPACT_NODEID(get_nasid());
177 #define rou_rflag rou_flags
179 static void router_recurse(klrou_t *router_a, klrou_t *router_b, int depth)
185 if (router_a->rou_rflag == 1)
188 if (depth >= router_distance)
191 router_a->rou_rflag = 1;
193 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
194 if (router_a->rou_port[port].port_nasid == INVALID_NASID)
197 brd = (lboard_t *)NODE_OFFSET_TO_K0(
198 router_a->rou_port[port].port_nasid,
199 router_a->rou_port[port].port_offset);
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;
208 router_recurse(router, router_b, depth + 1);
212 router_a->rou_rflag = 0;
215 int node_distance(nasid_t nasid_a, nasid_t nasid_b)
217 klrou_t *router, *router_a = NULL, *router_b = NULL;
218 lboard_t *brd, *dest_brd;
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);
227 if (nasid == -1) continue;
229 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
236 if (brd->brd_flags & DUPLICATE_BOARD)
239 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
240 router->rou_rflag = 0;
242 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
243 if (router->rou_port[port].port_nasid == INVALID_NASID)
246 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
247 router->rou_port[port].port_nasid,
248 router->rou_port[port].port_offset);
250 if (dest_brd->brd_type == KLTYPE_IP27) {
251 if (dest_brd->brd_nasid == nasid_a)
253 if (dest_brd->brd_nasid == nasid_b)
258 } while ((brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)));
261 if (router_a == NULL) {
262 printk("node_distance: router_a NULL\n");
265 if (router_b == NULL) {
266 printk("node_distance: router_b NULL\n");
270 if (nasid_a == nasid_b)
273 if (router_a == router_b)
276 router_distance = 100;
277 router_recurse(router_a, router_b, 2);
279 return router_distance;
282 static void init_topology_matrix(void)
284 nasid_t nasid, nasid2;
287 for (row = 0; row < MAX_COMPACT_NODES; row++)
288 for (col = 0; col < MAX_COMPACT_NODES; col++)
289 node_distances[row][col] = -1;
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);
300 static void dump_topology(void)
304 lboard_t *brd, *dest_brd;
310 printk("************** Topology ********************\n");
313 for (col = 0; col < numnodes; col++)
314 printk("%02d ", col);
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]);
323 for (cnode = 0; cnode < numnodes; cnode++) {
324 nasid = COMPACT_TO_NASID_NODEID(cnode);
326 if (nasid == -1) continue;
328 brd = find_lboard_class((lboard_t *)KL_CONFIG_INFO(nasid),
335 if (brd->brd_flags & DUPLICATE_BOARD)
337 printk("Router %d:", router_num);
340 router = (klrou_t *)NODE_OFFSET_TO_K0(NASID_GET(brd), brd->brd_compts[0]);
342 for (port = 1; port <= MAX_ROUTER_PORTS; port++) {
343 if (router->rou_port[port].port_nasid == INVALID_NASID)
346 dest_brd = (lboard_t *)NODE_OFFSET_TO_K0(
347 router->rou_port[port].port_nasid,
348 router->rou_port[port].port_offset);
350 if (dest_brd->brd_type == KLTYPE_IP27)
351 printk(" %d", dest_brd->brd_nasid);
352 if (dest_brd->brd_type == KLTYPE_ROUTER)
357 } while ( (brd = find_lboard_class(KLCF_NEXT(brd), KLTYPE_ROUTER)) );
365 master_nasid = get_nasid();
366 fine_mode = is_fine_dirmode();
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.
376 init_topology_matrix();
379 gen_region_mask(®ion_mask, numnodes);
381 setup_replication_mask(numnodes);
384 * Set all nodes' calias sizes to 8k
386 for (i = 0; i < numnodes; i++) {
389 nasid = COMPACT_TO_NASID_NODEID(i);
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.
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);
400 REMOTE_HUB_S(nasid, PI_CALIAS_SIZE, PI_CALIAS_SIZE_0);
405 * Set up all hubs to have a big window pointing at
406 * widget 0. Memory mode, widget 0, offset 0
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)));
415 /* Extracted from the IOC3 meta driver. FIXME. */
416 static inline void ioc3_sio_init(void)
423 ioc3 = (struct ioc3 *) KL_CONFIG_CH_CONS_INFO(nid)->memory_base;
425 ioc3->sscr_a = 0; /* PIO mode for uarta. */
426 ioc3->sscr_b = 0; /* PIO mode for uartb. */
428 ioc3->sio_ies = (SIO_IR_SA_INT | SIO_IR_SB_INT);
430 loops=1000000; while(loops--);
431 ioc3->sregs.uarta.iu_fcr = 0;
432 ioc3->sregs.uartb.iu_fcr = 0;
433 loops=1000000; while(loops--);
436 static inline void ioc3_eth_init(void)
442 ioc3 = (struct ioc3 *) KL_CONFIG_CH_CONS_INFO(nid)->memory_base;
447 void __init per_cpu_init(void)
449 cnodeid_t cnode = get_compact_nodeid();
450 int cpu = smp_processor_id();
452 clear_c0_status(ST0_IM);
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);
461 extern void ip27_setup_console(void);
462 extern void ip27_time_init(void);
463 extern void ip27_reboot_setup(void);
465 static int __init ip27_setup(void)
467 hubreg_t p, e, n_mode;
470 ip27_setup_console();
474 * hub_rtc init and cpu clock intr enabled for later calibrate_delay.
477 printk("IP27: Running on node %d.\n", nid);
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,
483 e ? ", CPU is running" : "");
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,
489 e ? ", CPU is running" : "");
492 * Try to catch kernel missconfigurations and give user an
493 * indication what option to select.
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
499 panic("Kernel compiled for M mode.");
502 panic("Kernel compiled for N mode.");
509 set_io_port_base(IO_BASE);
511 board_time_init = ip27_time_init;
516 early_initcall(ip27_setup);