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
6 * Copyright (C) 2004-2008 Cavium Networks
9 #include <linux/init.h>
10 #include <linux/delay.h>
11 #include <linux/smp.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
17 #include <asm/mmu_context.h>
18 #include <asm/system.h>
21 #include <asm/octeon/octeon.h>
23 #include "octeon_boot.h"
25 volatile unsigned long octeon_processor_boot
= 0xff;
26 volatile unsigned long octeon_processor_sp
;
27 volatile unsigned long octeon_processor_gp
;
29 #ifdef CONFIG_HOTPLUG_CPU
30 static unsigned int InitTLBStart_addr
;
33 static irqreturn_t
mailbox_interrupt(int irq
, void *dev_id
)
35 const int coreid
= cvmx_get_core_num();
38 /* Load the mailbox register to figure out what we're supposed to do */
39 action
= cvmx_read_csr(CVMX_CIU_MBOX_CLRX(coreid
));
41 /* Clear the mailbox to clear the interrupt */
42 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(coreid
), action
);
44 if (action
& SMP_CALL_FUNCTION
)
45 smp_call_function_interrupt();
47 /* Check if we've been told to flush the icache */
48 if (action
& SMP_ICACHE_FLUSH
)
49 asm volatile ("synci 0($0)\n");
54 * Cause the function described by call_data to be executed on the passed
55 * cpu. When the function has finished, increment the finished field of
58 void octeon_send_ipi_single(int cpu
, unsigned int action
)
60 int coreid
= cpu_logical_map(cpu
);
62 pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
65 cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid
), action
);
68 static inline void octeon_send_ipi_mask(const struct cpumask
*mask
,
73 for_each_cpu_mask(i
, *mask
)
74 octeon_send_ipi_single(i
, action
);
78 * Detect available CPUs, populate cpu_possible_map
80 static void octeon_smp_hotplug_setup(void)
82 #ifdef CONFIG_HOTPLUG_CPU
83 uint32_t labi_signature
;
86 cvmx_read64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
87 LABI_ADDR_IN_BOOTLOADER
+
88 offsetof(struct linux_app_boot_info
,
90 if (labi_signature
!= LABI_SIGNATURE
)
91 pr_err("The bootloader version on this board is incorrect\n");
93 cvmx_read64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
94 LABI_ADDR_IN_BOOTLOADER
+
95 offsetof(struct linux_app_boot_info
,
100 static void octeon_smp_setup(void)
102 const int coreid
= cvmx_get_core_num();
106 int core_mask
= octeon_get_boot_coremask();
108 cpus_clear(cpu_possible_map
);
109 __cpu_number_map
[coreid
] = 0;
110 __cpu_logical_map
[0] = coreid
;
111 cpu_set(0, cpu_possible_map
);
114 for (id
= 0; id
< 16; id
++) {
115 if ((id
!= coreid
) && (core_mask
& (1 << id
))) {
116 cpu_set(cpus
, cpu_possible_map
);
117 __cpu_number_map
[id
] = cpus
;
118 __cpu_logical_map
[cpus
] = id
;
122 cpu_present_map
= cpu_possible_map
;
124 octeon_smp_hotplug_setup();
128 * Firmware CPU startup hook
131 static void octeon_boot_secondary(int cpu
, struct task_struct
*idle
)
135 pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu
,
136 cpu_logical_map(cpu
));
138 octeon_processor_sp
= __KSTK_TOS(idle
);
139 octeon_processor_gp
= (unsigned long)(task_thread_info(idle
));
140 octeon_processor_boot
= cpu_logical_map(cpu
);
144 while (octeon_processor_sp
&& count
) {
145 /* Waiting for processor to get the SP and GP */
150 pr_err("Secondary boot timeout\n");
154 * After we've done initial boot, this function is called to allow the
155 * board code to clean up state, if needed
157 static void octeon_init_secondary(void)
159 const int coreid
= cvmx_get_core_num();
160 union cvmx_ciu_intx_sum0 interrupt_enable
;
162 #ifdef CONFIG_HOTPLUG_CPU
163 unsigned int cur_exception_base
;
165 cur_exception_base
= cvmx_read64_uint32(
166 CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
167 LABI_ADDR_IN_BOOTLOADER
+
168 offsetof(struct linux_app_boot_info
,
169 cur_exception_base
)));
170 /* cur_exception_base is incremented in bootloader after setting */
171 write_c0_ebase((unsigned int)(cur_exception_base
- EXCEPTION_BASE_INCR
));
173 octeon_check_cpu_bist();
174 octeon_init_cvmcount();
176 pr_info("SMP: CPU%d (CoreId %lu) started\n", cpu, coreid);
178 /* Enable Mailbox interrupts to this core. These are the only
179 interrupts allowed on line 3 */
180 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(coreid
), 0xffffffff);
181 interrupt_enable
.u64
= 0;
182 interrupt_enable
.s
.mbox
= 0x3;
183 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid
* 2)), interrupt_enable
.u64
);
184 cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid
* 2 + 1)), 0);
185 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid
* 2)), 0);
186 cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid
* 2 + 1)), 0);
187 /* Enable core interrupt processing for 2,3 and 7 */
188 set_c0_status(0x8c01);
192 * Callout to firmware before smp_init
195 void octeon_prepare_cpus(unsigned int max_cpus
)
197 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffffffff);
198 if (request_irq(OCTEON_IRQ_MBOX0
, mailbox_interrupt
, IRQF_DISABLED
,
199 "mailbox0", mailbox_interrupt
)) {
200 panic("Cannot request_irq(OCTEON_IRQ_MBOX0)\n");
202 if (request_irq(OCTEON_IRQ_MBOX1
, mailbox_interrupt
, IRQF_DISABLED
,
203 "mailbox1", mailbox_interrupt
)) {
204 panic("Cannot request_irq(OCTEON_IRQ_MBOX1)\n");
209 * Last chance for the board code to finish SMP initialization before
210 * the CPU is "online".
212 static void octeon_smp_finish(void)
214 #ifdef CONFIG_CAVIUM_GDB
216 /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
217 to be not masked by this core so we know the signal is received by
219 asm volatile ("dmfc0 %0, $22\n"
220 "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp
));
223 octeon_user_io_init();
225 /* to generate the first CPU timer interrupt */
226 write_c0_compare(read_c0_count() + mips_hpt_frequency
/ HZ
);
230 * Hook for after all CPUs are online
232 static void octeon_cpus_done(void)
234 #ifdef CONFIG_CAVIUM_GDB
236 /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
237 to be not masked by this core so we know the signal is received by
239 asm volatile ("dmfc0 %0, $22\n"
240 "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp
));
244 #ifdef CONFIG_HOTPLUG_CPU
246 /* State of each CPU. */
247 DEFINE_PER_CPU(int, cpu_state
);
249 extern void fixup_irqs(void);
251 static DEFINE_SPINLOCK(smp_reserve_lock
);
253 static int octeon_cpu_disable(void)
255 unsigned int cpu
= smp_processor_id();
260 spin_lock(&smp_reserve_lock
);
262 cpu_clear(cpu
, cpu_online_map
);
263 cpu_clear(cpu
, cpu_callin_map
);
269 local_flush_tlb_all();
271 spin_unlock(&smp_reserve_lock
);
276 static void octeon_cpu_die(unsigned int cpu
)
278 int coreid
= cpu_logical_map(cpu
);
279 uint32_t avail_coremask
;
280 struct cvmx_bootmem_named_block_desc
*block_desc
;
282 while (per_cpu(cpu_state
, cpu
) != CPU_DEAD
)
286 * This is a bit complicated strategics of getting/settig available
287 * cores mask, copied from bootloader
289 /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
290 block_desc
= cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME
);
294 cvmx_read64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
295 LABI_ADDR_IN_BOOTLOADER
+
297 (struct linux_app_boot_info
,
299 } else { /* alternative, already initialized */
301 cvmx_read64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
302 block_desc
->base_addr
+
303 AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK
));
306 avail_coremask
|= 1 << coreid
;
308 /* Setting avail_coremask for bootoct binary */
310 cvmx_write64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
311 LABI_ADDR_IN_BOOTLOADER
+
312 offsetof(struct linux_app_boot_info
,
316 cvmx_write64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
317 block_desc
->base_addr
+
318 AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK
),
322 pr_info("Reset core %d. Available Coremask = %x\n", coreid
,
324 cvmx_write_csr(CVMX_CIU_PP_RST
, 1 << coreid
);
325 cvmx_write_csr(CVMX_CIU_PP_RST
, 0);
330 int coreid
= cvmx_get_core_num();
333 octeon_processor_boot
= 0xff;
334 per_cpu(cpu_state
, coreid
) = CPU_DEAD
;
336 while (1) /* core will be reset here */
340 extern void kernel_entry(unsigned long arg1
, ...);
342 static void start_after_reset(void)
344 kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
347 int octeon_update_boot_vector(unsigned int cpu
)
350 int coreid
= cpu_logical_map(cpu
);
351 unsigned int avail_coremask
;
352 struct cvmx_bootmem_named_block_desc
*block_desc
;
353 struct boot_init_vector
*boot_vect
=
354 (struct boot_init_vector
*) cvmx_phys_to_ptr(0x0 +
355 BOOTLOADER_BOOT_VECTOR
);
357 block_desc
= cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME
);
361 cvmx_read64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
362 LABI_ADDR_IN_BOOTLOADER
+
363 offsetof(struct linux_app_boot_info
,
365 } else { /* alternative, already initialized */
367 cvmx_read64_uint32(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
368 block_desc
->base_addr
+
369 AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK
));
372 if (!(avail_coremask
& (1 << coreid
))) {
373 /* core not available, assume, that catched by simple-executive */
374 cvmx_write_csr(CVMX_CIU_PP_RST
, 1 << coreid
);
375 cvmx_write_csr(CVMX_CIU_PP_RST
, 0);
378 boot_vect
[coreid
].app_start_func_addr
=
379 (uint32_t) (unsigned long) start_after_reset
;
380 boot_vect
[coreid
].code_addr
= InitTLBStart_addr
;
384 cvmx_write_csr(CVMX_CIU_NMI
, (1 << coreid
) & avail_coremask
);
389 static int __cpuinit
octeon_cpu_callback(struct notifier_block
*nfb
,
390 unsigned long action
, void *hcpu
)
392 unsigned int cpu
= (unsigned long)hcpu
;
396 octeon_update_boot_vector(cpu
);
399 pr_info("Cpu %d online\n", cpu
);
408 static struct notifier_block __cpuinitdata octeon_cpu_notifier
= {
409 .notifier_call
= octeon_cpu_callback
,
412 static int __cpuinit
register_cavium_notifier(void)
414 register_hotcpu_notifier(&octeon_cpu_notifier
);
419 late_initcall(register_cavium_notifier
);
421 #endif /* CONFIG_HOTPLUG_CPU */
423 struct plat_smp_ops octeon_smp_ops
= {
424 .send_ipi_single
= octeon_send_ipi_single
,
425 .send_ipi_mask
= octeon_send_ipi_mask
,
426 .init_secondary
= octeon_init_secondary
,
427 .smp_finish
= octeon_smp_finish
,
428 .cpus_done
= octeon_cpus_done
,
429 .boot_secondary
= octeon_boot_secondary
,
430 .smp_setup
= octeon_smp_setup
,
431 .prepare_cpus
= octeon_prepare_cpus
,
432 #ifdef CONFIG_HOTPLUG_CPU
433 .cpu_disable
= octeon_cpu_disable
,
434 .cpu_die
= octeon_cpu_die
,