f624f9da5262a5009f2159c69ef62e0408d044d3
[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/module.h>
16 #include <linux/cpumask.h>
17 #include <asm/cpu.h>
18 #include <asm/io.h>
19 #include <asm/pgtable.h>
20 #include <asm/time.h>
21 #include <asm/sn/types.h>
22 #include <asm/sn/sn0/addrs.h>
23 #include <asm/sn/sn0/hubni.h>
24 #include <asm/sn/sn0/hubio.h>
25 #include <asm/sn/klconfig.h>
26 #include <asm/sn/ioc3.h>
27 #include <asm/mipsregs.h>
28 #include <asm/sn/gda.h>
29 #include <asm/sn/hub.h>
30 #include <asm/sn/intr.h>
31 #include <asm/current.h>
32 #include <asm/smp.h>
33 #include <asm/processor.h>
34 #include <asm/mmu_context.h>
35 #include <asm/thread_info.h>
36 #include <asm/sn/launch.h>
37 #include <asm/sn/sn_private.h>
38 #include <asm/sn/sn0/ip27.h>
39 #include <asm/sn/mapped_kernel.h>
40
41 #define CPU_NONE                (cpuid_t)-1
42
43 static DECLARE_BITMAP(hub_init_mask, MAX_COMPACT_NODES);
44 nasid_t master_nasid = INVALID_NASID;
45
46 cnodeid_t       nasid_to_compact_node[MAX_NASIDS];
47 nasid_t         compact_to_nasid_node[MAX_COMPACT_NODES];
48 cnodeid_t       cpuid_to_compact_node[MAXCPUS];
49
50 EXPORT_SYMBOL(nasid_to_compact_node);
51
52 extern void pcibr_setup(cnodeid_t);
53
54 extern void xtalk_probe_node(cnodeid_t nid);
55
56 static void __init per_hub_init(cnodeid_t cnode)
57 {
58         struct hub_data *hub = hub_data(cnode);
59         nasid_t nasid = COMPACT_TO_NASID_NODEID(cnode);
60
61         cpu_set(smp_processor_id(), hub->h_cpus);
62
63         if (test_and_set_bit(cnode, hub_init_mask))
64                 return;
65
66         /*
67          * Set CRB timeout at 5ms, (< PI timeout of 10ms)
68          */
69         REMOTE_HUB_S(nasid, IIO_ICTP, 0x800);
70         REMOTE_HUB_S(nasid, IIO_ICTO, 0xff);
71
72         hub_rtc_init(cnode);
73         xtalk_probe_node(cnode);
74
75 #ifdef CONFIG_REPLICATE_EXHANDLERS
76         /*
77          * If this is not a headless node initialization,
78          * copy over the caliased exception handlers.
79          */
80         if (get_compact_nodeid() == cnode) {
81                 extern char except_vec0, except_vec1_r4k;
82                 extern char except_vec2_generic, except_vec3_generic;
83
84                 memcpy((void *)(KSEG0 + 0x100), &except_vec2_generic, 0x80);
85                 memcpy((void *)(KSEG0 + 0x180), &except_vec3_generic, 0x80);
86                 memcpy((void *)KSEG0, &except_vec0, 0x80);
87                 memcpy((void *)KSEG0 + 0x080, &except_vec1_r4k, 0x80);
88                 memcpy((void *)(KSEG0 + 0x100), (void *) KSEG0, 0x80);
89                 memcpy((void *)(KSEG0 + 0x180), &except_vec3_generic, 0x100);
90                 __flush_cache_all();
91         }
92 #endif
93 }
94
95 void __init per_cpu_init(void)
96 {
97         int cpu = smp_processor_id();
98         int slice = LOCAL_HUB_L(PI_CPU_NUM);
99         cnodeid_t cnode = get_compact_nodeid();
100         struct hub_data *hub = hub_data(cnode);
101         struct slice_data *si = hub->slice + slice;
102         int i;
103
104         if (test_and_set_bit(slice, &hub->slice_map))
105                 return;
106
107         clear_c0_status(ST0_IM);
108
109         for (i = 0; i < LEVELS_PER_SLICE; i++)
110                 si->level_to_irq[i] = -1;
111
112         /*
113          * Some interrupts are reserved by hardware or by software convention.
114          * Mark these as reserved right away so they won't be used accidently
115          * later.
116          */
117         for (i = 0; i <= BASE_PCI_IRQ; i++) {
118                 __set_bit(i, si->irq_alloc_mask);
119                 LOCAL_HUB_S(PI_INT_PEND_MOD, i);
120         }
121
122         __set_bit(IP_PEND0_6_63, si->irq_alloc_mask);
123         LOCAL_HUB_S(PI_INT_PEND_MOD, IP_PEND0_6_63);
124
125         for (i = NI_BRDCAST_ERR_A; i <= MSC_PANIC_INTR; i++) {
126                 __set_bit(i, si->irq_alloc_mask + 1);
127                 LOCAL_HUB_S(PI_INT_PEND_MOD, i);
128         }
129
130         LOCAL_HUB_L(PI_INT_PEND0);
131
132         /*
133          * We use this so we can find the local hub's data as fast as only
134          * possible.
135          */
136         cpu_data[cpu].data = si;
137
138         cpu_time_init();
139         install_ipi();
140
141         /* Install our NMI handler if symmon hasn't installed one. */
142         install_cpu_nmi_handler(cputoslice(cpu));
143
144         set_c0_status(SRB_DEV0 | SRB_DEV1);
145
146         per_hub_init(cnode);
147 }
148
149 /*
150  * get_nasid() returns the physical node id number of the caller.
151  */
152 nasid_t
153 get_nasid(void)
154 {
155         return (nasid_t)((LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_NODEID_MASK)
156                          >> NSRI_NODEID_SHFT);
157 }
158
159 /*
160  * Map the physical node id to a virtual node id (virtual node ids are contiguous).
161  */
162 cnodeid_t get_compact_nodeid(void)
163 {
164         return NASID_TO_COMPACT_NODEID(get_nasid());
165 }
166
167 /* Extracted from the IOC3 meta driver.  FIXME.  */
168 static inline void ioc3_sio_init(void)
169 {
170         struct ioc3 *ioc3;
171         nasid_t nid;
172         long loops;
173
174         nid = get_nasid();
175         ioc3 = (struct ioc3 *) KL_CONFIG_CH_CONS_INFO(nid)->memory_base;
176
177         ioc3->sscr_a = 0;                       /* PIO mode for uarta.  */
178         ioc3->sscr_b = 0;                       /* PIO mode for uartb.  */
179         ioc3->sio_iec = ~0;
180         ioc3->sio_ies = (SIO_IR_SA_INT | SIO_IR_SB_INT);
181
182         loops=1000000; while(loops--);
183         ioc3->sregs.uarta.iu_fcr = 0;
184         ioc3->sregs.uartb.iu_fcr = 0;
185         loops=1000000; while(loops--);
186 }
187
188 static inline void ioc3_eth_init(void)
189 {
190         struct ioc3 *ioc3;
191         nasid_t nid;
192
193         nid = get_nasid();
194         ioc3 = (struct ioc3 *) KL_CONFIG_CH_CONS_INFO(nid)->memory_base;
195
196         ioc3->eier = 0;
197 }
198
199 extern void ip27_setup_console(void);
200 extern void ip27_time_init(void);
201 extern void ip27_reboot_setup(void);
202
203 static int __init ip27_setup(void)
204 {
205         hubreg_t p, e, n_mode;
206         nasid_t nid;
207
208         ip27_setup_console();
209         ip27_reboot_setup();
210
211         /*
212          * hub_rtc init and cpu clock intr enabled for later calibrate_delay.
213          */
214         nid = get_nasid();
215         printk("IP27: Running on node %d.\n", nid);
216
217         p = LOCAL_HUB_L(PI_CPU_PRESENT_A) & 1;
218         e = LOCAL_HUB_L(PI_CPU_ENABLE_A) & 1;
219         printk("Node %d has %s primary CPU%s.\n", nid,
220                p ? "a" : "no",
221                e ? ", CPU is running" : "");
222
223         p = LOCAL_HUB_L(PI_CPU_PRESENT_B) & 1;
224         e = LOCAL_HUB_L(PI_CPU_ENABLE_B) & 1;
225         printk("Node %d has %s secondary CPU%s.\n", nid,
226                p ? "a" : "no",
227                e ? ", CPU is running" : "");
228
229         /*
230          * Try to catch kernel missconfigurations and give user an
231          * indication what option to select.
232          */
233         n_mode = LOCAL_HUB_L(NI_STATUS_REV_ID) & NSRI_MORENODES_MASK;
234         printk("Machine is in %c mode.\n", n_mode ? 'N' : 'M');
235 #ifdef CONFIG_SGI_SN0_N_MODE
236         if (!n_mode)
237                 panic("Kernel compiled for M mode.");
238 #else
239         if (n_mode)
240                 panic("Kernel compiled for N mode.");
241 #endif
242
243         ioc3_sio_init();
244         ioc3_eth_init();
245         per_cpu_init();
246
247         set_io_port_base(IO_BASE);
248
249         board_time_init = ip27_time_init;
250
251         return 0;
252 }
253
254 early_initcall(ip27_setup);