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, 2009, 2010 Cavium Networks
9 #include <linux/delay.h>
10 #include <linux/smp.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/sched.h>
14 #include <linux/module.h>
16 #include <asm/mmu_context.h>
18 #include <asm/setup.h>
20 #include <asm/octeon/octeon.h>
22 #include "octeon_boot.h"
24 volatile unsigned long octeon_processor_boot
= 0xff;
25 volatile unsigned long octeon_processor_sp
;
26 volatile unsigned long octeon_processor_gp
;
28 #ifdef CONFIG_HOTPLUG_CPU
29 uint64_t octeon_bootloader_entry_addr
;
30 EXPORT_SYMBOL(octeon_bootloader_entry_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
)) & 0xffff;
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 generic_smp_call_function_interrupt();
46 if (action
& SMP_RESCHEDULE_YOURSELF
)
49 /* Check if we've been told to flush the icache */
50 if (action
& SMP_ICACHE_FLUSH
)
51 asm volatile ("synci 0($0)\n");
56 * Cause the function described by call_data to be executed on the passed
57 * cpu. When the function has finished, increment the finished field of
60 void octeon_send_ipi_single(int cpu
, unsigned int action
)
62 int coreid
= cpu_logical_map(cpu
);
64 pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
67 cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid
), action
);
70 static inline void octeon_send_ipi_mask(const struct cpumask
*mask
,
76 octeon_send_ipi_single(i
, action
);
80 * Detect available CPUs, populate cpu_possible_mask
82 static void octeon_smp_hotplug_setup(void)
84 #ifdef CONFIG_HOTPLUG_CPU
85 struct linux_app_boot_info
*labi
;
90 labi
= (struct linux_app_boot_info
*)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER
);
91 if (labi
->labi_signature
!= LABI_SIGNATURE
) {
92 pr_info("The bootloader on this board does not support HOTPLUG_CPU.");
96 octeon_bootloader_entry_addr
= labi
->InitTLBStart_addr
;
100 static void octeon_smp_setup(void)
102 const int coreid
= cvmx_get_core_num();
105 int core_mask
= octeon_get_boot_coremask();
106 struct cvmx_sysinfo
*sysinfo
= cvmx_sysinfo_get();
108 #ifdef CONFIG_HOTPLUG_CPU
109 unsigned int num_cores
= cvmx_octeon_num_cores();
112 /* The present CPUs are initially just the boot cpu (CPU 0). */
113 for (id
= 0; id
< NR_CPUS
; id
++) {
114 set_cpu_possible(id
, id
== 0);
115 set_cpu_present(id
, id
== 0);
118 __cpu_number_map
[coreid
] = 0;
119 __cpu_logical_map
[0] = coreid
;
121 /* The present CPUs get the lowest CPU numbers. */
123 for (id
= 0; id
< NR_CPUS
; id
++) {
124 if ((id
!= coreid
) && cvmx_coremask_is_core_set(&sysinfo
->core_mask
, id
)) {
125 set_cpu_possible(cpus
, true);
126 set_cpu_present(cpus
, true);
127 __cpu_number_map
[id
] = cpus
;
128 __cpu_logical_map
[cpus
] = id
;
133 #ifdef CONFIG_HOTPLUG_CPU
135 * The possible CPUs are all those present on the chip. We
136 * will assign CPU numbers for possible cores as well. Cores
137 * are always consecutively numberd from 0.
139 for (id
= 0; setup_max_cpus
&& octeon_bootloader_entry_addr
&&
140 id
< num_cores
&& id
< NR_CPUS
; id
++) {
141 if (!(core_mask
& (1 << id
))) {
142 set_cpu_possible(cpus
, true);
143 __cpu_number_map
[id
] = cpus
;
144 __cpu_logical_map
[cpus
] = id
;
150 octeon_smp_hotplug_setup();
154 * Firmware CPU startup hook
157 static void octeon_boot_secondary(int cpu
, struct task_struct
*idle
)
161 pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu
,
162 cpu_logical_map(cpu
));
164 octeon_processor_sp
= __KSTK_TOS(idle
);
165 octeon_processor_gp
= (unsigned long)(task_thread_info(idle
));
166 octeon_processor_boot
= cpu_logical_map(cpu
);
170 while (octeon_processor_sp
&& count
) {
171 /* Waiting for processor to get the SP and GP */
176 pr_err("Secondary boot timeout\n");
180 * After we've done initial boot, this function is called to allow the
181 * board code to clean up state, if needed
183 static void octeon_init_secondary(void)
187 sr
= set_c0_status(ST0_BEV
);
188 write_c0_ebase((u32
)ebase
);
191 octeon_check_cpu_bist();
192 octeon_init_cvmcount();
194 octeon_irq_setup_secondary();
198 * Callout to firmware before smp_init
201 void octeon_prepare_cpus(unsigned int max_cpus
)
204 * Only the low order mailbox bits are used for IPIs, leave
205 * the other bits alone.
207 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
208 if (request_irq(OCTEON_IRQ_MBOX0
, mailbox_interrupt
,
209 IRQF_PERCPU
| IRQF_NO_THREAD
, "SMP-IPI",
210 mailbox_interrupt
)) {
211 panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
216 * Last chance for the board code to finish SMP initialization before
217 * the CPU is "online".
219 static void octeon_smp_finish(void)
221 octeon_user_io_init();
223 /* to generate the first CPU timer interrupt */
224 write_c0_compare(read_c0_count() + mips_hpt_frequency
/ HZ
);
228 #ifdef CONFIG_HOTPLUG_CPU
230 /* State of each CPU. */
231 DEFINE_PER_CPU(int, cpu_state
);
233 static int octeon_cpu_disable(void)
235 unsigned int cpu
= smp_processor_id();
240 if (!octeon_bootloader_entry_addr
)
243 set_cpu_online(cpu
, false);
244 cpumask_clear_cpu(cpu
, &cpu_callin_map
);
248 local_flush_tlb_all();
253 static void octeon_cpu_die(unsigned int cpu
)
255 int coreid
= cpu_logical_map(cpu
);
256 uint32_t mask
, new_mask
;
257 const struct cvmx_bootmem_named_block_desc
*block_desc
;
259 while (per_cpu(cpu_state
, cpu
) != CPU_DEAD
)
263 * This is a bit complicated strategics of getting/settig available
264 * cores mask, copied from bootloader
268 /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
269 block_desc
= cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME
);
272 struct linux_app_boot_info
*labi
;
274 labi
= (struct linux_app_boot_info
*)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER
);
276 labi
->avail_coremask
|= mask
;
277 new_mask
= labi
->avail_coremask
;
278 } else { /* alternative, already initialized */
279 uint32_t *p
= (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc
->base_addr
+
280 AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK
);
285 pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid
, new_mask
);
287 cvmx_write_csr(CVMX_CIU_PP_RST
, 1 << coreid
);
288 cvmx_write_csr(CVMX_CIU_PP_RST
, 0);
293 int cpu
= cpu_number_map(cvmx_get_core_num());
296 octeon_processor_boot
= 0xff;
297 per_cpu(cpu_state
, cpu
) = CPU_DEAD
;
301 while (1) /* core will be reset here */
305 extern void kernel_entry(unsigned long arg1
, ...);
307 static void start_after_reset(void)
309 kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
312 static int octeon_update_boot_vector(unsigned int cpu
)
315 int coreid
= cpu_logical_map(cpu
);
316 uint32_t avail_coremask
;
317 const struct cvmx_bootmem_named_block_desc
*block_desc
;
318 struct boot_init_vector
*boot_vect
=
319 (struct boot_init_vector
*)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR
);
321 block_desc
= cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME
);
324 struct linux_app_boot_info
*labi
;
326 labi
= (struct linux_app_boot_info
*)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER
);
328 avail_coremask
= labi
->avail_coremask
;
329 labi
->avail_coremask
&= ~(1 << coreid
);
330 } else { /* alternative, already initialized */
331 avail_coremask
= *(uint32_t *)PHYS_TO_XKSEG_CACHED(
332 block_desc
->base_addr
+ AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK
);
335 if (!(avail_coremask
& (1 << coreid
))) {
336 /* core not available, assume, that caught by simple-executive */
337 cvmx_write_csr(CVMX_CIU_PP_RST
, 1 << coreid
);
338 cvmx_write_csr(CVMX_CIU_PP_RST
, 0);
341 boot_vect
[coreid
].app_start_func_addr
=
342 (uint32_t) (unsigned long) start_after_reset
;
343 boot_vect
[coreid
].code_addr
= octeon_bootloader_entry_addr
;
347 cvmx_write_csr(CVMX_CIU_NMI
, (1 << coreid
) & avail_coremask
);
352 static int octeon_cpu_callback(struct notifier_block
*nfb
,
353 unsigned long action
, void *hcpu
)
355 unsigned int cpu
= (unsigned long)hcpu
;
359 octeon_update_boot_vector(cpu
);
362 pr_info("Cpu %d online\n", cpu
);
371 static int register_cavium_notifier(void)
373 hotcpu_notifier(octeon_cpu_callback
, 0);
376 late_initcall(register_cavium_notifier
);
378 #endif /* CONFIG_HOTPLUG_CPU */
380 struct plat_smp_ops octeon_smp_ops
= {
381 .send_ipi_single
= octeon_send_ipi_single
,
382 .send_ipi_mask
= octeon_send_ipi_mask
,
383 .init_secondary
= octeon_init_secondary
,
384 .smp_finish
= octeon_smp_finish
,
385 .boot_secondary
= octeon_boot_secondary
,
386 .smp_setup
= octeon_smp_setup
,
387 .prepare_cpus
= octeon_prepare_cpus
,
388 #ifdef CONFIG_HOTPLUG_CPU
389 .cpu_disable
= octeon_cpu_disable
,
390 .cpu_die
= octeon_cpu_die
,