blob: 67a078ffc4641a16c659c93bb049e3c3fb8193ba [file] [log] [blame]
David Daney5b3b1682009-01-08 16:46:40 -08001/*
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
4 * for more details.
5 *
David Daneyedfcbb82010-07-23 10:57:49 -07006 * Copyright (C) 2004-2008, 2009, 2010 Cavium Networks
David Daney5b3b1682009-01-08 16:46:40 -08007 */
Ralf Baechle773cb772009-06-23 10:36:38 +01008#include <linux/cpu.h>
David Daney5b3b1682009-01-08 16:46:40 -08009#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>
15
16#include <asm/mmu_context.h>
David Daney5b3b1682009-01-08 16:46:40 -080017#include <asm/time.h>
David Howellsb81947c2012-03-28 18:30:02 +010018#include <asm/setup.h>
David Daney5b3b1682009-01-08 16:46:40 -080019
20#include <asm/octeon/octeon.h>
21
Ralf Baechle773cb772009-06-23 10:36:38 +010022#include "octeon_boot.h"
23
David Daney5b3b1682009-01-08 16:46:40 -080024volatile unsigned long octeon_processor_boot = 0xff;
25volatile unsigned long octeon_processor_sp;
26volatile unsigned long octeon_processor_gp;
27
Ralf Baechle773cb772009-06-23 10:36:38 +010028#ifdef CONFIG_HOTPLUG_CPU
David Daneybabba4f2010-07-23 10:57:51 -070029uint64_t octeon_bootloader_entry_addr;
30EXPORT_SYMBOL(octeon_bootloader_entry_addr);
Ralf Baechle773cb772009-06-23 10:36:38 +010031#endif
32
David Daney5b3b1682009-01-08 16:46:40 -080033static irqreturn_t mailbox_interrupt(int irq, void *dev_id)
34{
35 const int coreid = cvmx_get_core_num();
36 uint64_t action;
37
38 /* Load the mailbox register to figure out what we're supposed to do */
David Daneye650ce02011-02-17 14:47:52 -080039 action = cvmx_read_csr(CVMX_CIU_MBOX_CLRX(coreid)) & 0xffff;
David Daney5b3b1682009-01-08 16:46:40 -080040
41 /* Clear the mailbox to clear the interrupt */
42 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(coreid), action);
43
44 if (action & SMP_CALL_FUNCTION)
45 smp_call_function_interrupt();
Peter Zijlstra184748c2011-04-05 17:23:39 +020046 if (action & SMP_RESCHEDULE_YOURSELF)
47 scheduler_ipi();
David Daney5b3b1682009-01-08 16:46:40 -080048
49 /* Check if we've been told to flush the icache */
50 if (action & SMP_ICACHE_FLUSH)
51 asm volatile ("synci 0($0)\n");
52 return IRQ_HANDLED;
53}
54
55/**
56 * Cause the function described by call_data to be executed on the passed
Ralf Baechle70342282013-01-22 12:59:30 +010057 * cpu. When the function has finished, increment the finished field of
David Daney5b3b1682009-01-08 16:46:40 -080058 * call_data.
59 */
60void octeon_send_ipi_single(int cpu, unsigned int action)
61{
62 int coreid = cpu_logical_map(cpu);
63 /*
64 pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
65 coreid, action);
66 */
67 cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
68}
69
David Daney067f3292009-10-01 16:47:38 -070070static inline void octeon_send_ipi_mask(const struct cpumask *mask,
71 unsigned int action)
David Daney5b3b1682009-01-08 16:46:40 -080072{
73 unsigned int i;
74
David Daney067f3292009-10-01 16:47:38 -070075 for_each_cpu_mask(i, *mask)
David Daney5b3b1682009-01-08 16:46:40 -080076 octeon_send_ipi_single(i, action);
77}
78
79/**
Rusty Russell5f054e32012-03-29 15:38:31 +103080 * Detect available CPUs, populate cpu_possible_mask
David Daney5b3b1682009-01-08 16:46:40 -080081 */
Ralf Baechle773cb772009-06-23 10:36:38 +010082static void octeon_smp_hotplug_setup(void)
83{
84#ifdef CONFIG_HOTPLUG_CPU
David Daneybabba4f2010-07-23 10:57:51 -070085 struct linux_app_boot_info *labi;
Ralf Baechle773cb772009-06-23 10:36:38 +010086
David Daneybabba4f2010-07-23 10:57:51 -070087 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
88 if (labi->labi_signature != LABI_SIGNATURE)
89 panic("The bootloader version on this board is incorrect.");
90
91 octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
Ralf Baechle773cb772009-06-23 10:36:38 +010092#endif
93}
94
David Daney5b3b1682009-01-08 16:46:40 -080095static void octeon_smp_setup(void)
96{
97 const int coreid = cvmx_get_core_num();
98 int cpus;
99 int id;
David Daney5b3b1682009-01-08 16:46:40 -0800100 int core_mask = octeon_get_boot_coremask();
David Daneyedfcbb82010-07-23 10:57:49 -0700101#ifdef CONFIG_HOTPLUG_CPU
102 unsigned int num_cores = cvmx_octeon_num_cores();
103#endif
David Daney5b3b1682009-01-08 16:46:40 -0800104
David Daneyedfcbb82010-07-23 10:57:49 -0700105 /* The present CPUs are initially just the boot cpu (CPU 0). */
106 for (id = 0; id < NR_CPUS; id++) {
107 set_cpu_possible(id, id == 0);
108 set_cpu_present(id, id == 0);
109 }
110
David Daney5b3b1682009-01-08 16:46:40 -0800111 __cpu_number_map[coreid] = 0;
112 __cpu_logical_map[0] = coreid;
David Daney5b3b1682009-01-08 16:46:40 -0800113
David Daneyedfcbb82010-07-23 10:57:49 -0700114 /* The present CPUs get the lowest CPU numbers. */
David Daney5b3b1682009-01-08 16:46:40 -0800115 cpus = 1;
David Daneyedfcbb82010-07-23 10:57:49 -0700116 for (id = 0; id < NR_CPUS; id++) {
David Daney5b3b1682009-01-08 16:46:40 -0800117 if ((id != coreid) && (core_mask & (1 << id))) {
David Daneyedfcbb82010-07-23 10:57:49 -0700118 set_cpu_possible(cpus, true);
119 set_cpu_present(cpus, true);
David Daney5b3b1682009-01-08 16:46:40 -0800120 __cpu_number_map[id] = cpus;
121 __cpu_logical_map[cpus] = id;
122 cpus++;
123 }
124 }
David Daneyedfcbb82010-07-23 10:57:49 -0700125
126#ifdef CONFIG_HOTPLUG_CPU
127 /*
Ralf Baechle70342282013-01-22 12:59:30 +0100128 * The possible CPUs are all those present on the chip. We
129 * will assign CPU numbers for possible cores as well. Cores
David Daneyedfcbb82010-07-23 10:57:49 -0700130 * are always consecutively numberd from 0.
131 */
132 for (id = 0; id < num_cores && id < NR_CPUS; id++) {
133 if (!(core_mask & (1 << id))) {
134 set_cpu_possible(cpus, true);
135 __cpu_number_map[id] = cpus;
136 __cpu_logical_map[cpus] = id;
137 cpus++;
138 }
139 }
140#endif
Ralf Baechle773cb772009-06-23 10:36:38 +0100141
142 octeon_smp_hotplug_setup();
David Daney5b3b1682009-01-08 16:46:40 -0800143}
144
145/**
146 * Firmware CPU startup hook
147 *
148 */
149static void octeon_boot_secondary(int cpu, struct task_struct *idle)
150{
151 int count;
152
153 pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
154 cpu_logical_map(cpu));
155
156 octeon_processor_sp = __KSTK_TOS(idle);
157 octeon_processor_gp = (unsigned long)(task_thread_info(idle));
158 octeon_processor_boot = cpu_logical_map(cpu);
159 mb();
160
161 count = 10000;
162 while (octeon_processor_sp && count) {
163 /* Waiting for processor to get the SP and GP */
164 udelay(1);
165 count--;
166 }
167 if (count == 0)
168 pr_err("Secondary boot timeout\n");
169}
170
171/**
172 * After we've done initial boot, this function is called to allow the
173 * board code to clean up state, if needed
174 */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000175static void octeon_init_secondary(void)
David Daney5b3b1682009-01-08 16:46:40 -0800176{
David Daneybabba4f2010-07-23 10:57:51 -0700177 unsigned int sr;
David Daney5b3b1682009-01-08 16:46:40 -0800178
David Daney0c326382011-03-25 12:38:51 -0700179 sr = set_c0_status(ST0_BEV);
180 write_c0_ebase((u32)ebase);
181 write_c0_status(sr);
182
183 octeon_check_cpu_bist();
184 octeon_init_cvmcount();
185
186 octeon_irq_setup_secondary();
David Daney0c326382011-03-25 12:38:51 -0700187}
188
189/**
190 * Callout to firmware before smp_init
191 *
192 */
193void octeon_prepare_cpus(unsigned int max_cpus)
194{
Ralf Baechle773cb772009-06-23 10:36:38 +0100195#ifdef CONFIG_HOTPLUG_CPU
David Daneybabba4f2010-07-23 10:57:51 -0700196 struct linux_app_boot_info *labi;
Ralf Baechle773cb772009-06-23 10:36:38 +0100197
David Daneybabba4f2010-07-23 10:57:51 -0700198 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
199
200 if (labi->labi_signature != LABI_SIGNATURE)
201 panic("The bootloader version on this board is incorrect.");
Ralf Baechle773cb772009-06-23 10:36:38 +0100202#endif
David Daneye650ce02011-02-17 14:47:52 -0800203 /*
204 * Only the low order mailbox bits are used for IPIs, leave
205 * the other bits alone.
206 */
207 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
Venkat Subbiahe63fb7a2011-10-03 13:31:10 -0700208 if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
209 IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
210 mailbox_interrupt)) {
Ralf Baechleab75dc02011-11-17 15:07:31 +0000211 panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
David Daney5b3b1682009-01-08 16:46:40 -0800212 }
David Daney5b3b1682009-01-08 16:46:40 -0800213}
214
215/**
216 * Last chance for the board code to finish SMP initialization before
217 * the CPU is "online".
218 */
219static void octeon_smp_finish(void)
220{
221#ifdef CONFIG_CAVIUM_GDB
222 unsigned long tmp;
223 /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
224 to be not masked by this core so we know the signal is received by
225 someone */
226 asm volatile ("dmfc0 %0, $22\n"
227 "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
228#endif
229
230 octeon_user_io_init();
231
232 /* to generate the first CPU timer interrupt */
233 write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
Yong Zhang1bcfecc2012-07-19 09:13:53 +0200234 local_irq_enable();
David Daney5b3b1682009-01-08 16:46:40 -0800235}
236
237/**
238 * Hook for after all CPUs are online
239 */
240static void octeon_cpus_done(void)
241{
242#ifdef CONFIG_CAVIUM_GDB
243 unsigned long tmp;
244 /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
245 to be not masked by this core so we know the signal is received by
246 someone */
247 asm volatile ("dmfc0 %0, $22\n"
248 "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
249#endif
250}
251
Ralf Baechle773cb772009-06-23 10:36:38 +0100252#ifdef CONFIG_HOTPLUG_CPU
253
254/* State of each CPU. */
255DEFINE_PER_CPU(int, cpu_state);
256
Ralf Baechle773cb772009-06-23 10:36:38 +0100257static int octeon_cpu_disable(void)
258{
259 unsigned int cpu = smp_processor_id();
260
261 if (cpu == 0)
262 return -EBUSY;
263
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030264 set_cpu_online(cpu, false);
Ralf Baechle773cb772009-06-23 10:36:38 +0100265 cpu_clear(cpu, cpu_callin_map);
266 local_irq_disable();
Ralf Baechle17efb592013-09-03 18:19:28 +0200267 octeon_fixup_irqs();
Ralf Baechle773cb772009-06-23 10:36:38 +0100268 local_irq_enable();
269
270 flush_cache_all();
271 local_flush_tlb_all();
272
Ralf Baechle773cb772009-06-23 10:36:38 +0100273 return 0;
274}
275
276static void octeon_cpu_die(unsigned int cpu)
277{
278 int coreid = cpu_logical_map(cpu);
David Daneybabba4f2010-07-23 10:57:51 -0700279 uint32_t mask, new_mask;
280 const struct cvmx_bootmem_named_block_desc *block_desc;
Ralf Baechle773cb772009-06-23 10:36:38 +0100281
Ralf Baechle773cb772009-06-23 10:36:38 +0100282 while (per_cpu(cpu_state, cpu) != CPU_DEAD)
283 cpu_relax();
284
285 /*
286 * This is a bit complicated strategics of getting/settig available
287 * cores mask, copied from bootloader
288 */
David Daneybabba4f2010-07-23 10:57:51 -0700289
290 mask = 1 << coreid;
Ralf Baechle773cb772009-06-23 10:36:38 +0100291 /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
292 block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
293
294 if (!block_desc) {
David Daneybabba4f2010-07-23 10:57:51 -0700295 struct linux_app_boot_info *labi;
296
297 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
298
299 labi->avail_coremask |= mask;
300 new_mask = labi->avail_coremask;
Ralf Baechle773cb772009-06-23 10:36:38 +0100301 } else { /* alternative, already initialized */
David Daneybabba4f2010-07-23 10:57:51 -0700302 uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
303 AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
304 *p |= mask;
305 new_mask = *p;
Ralf Baechle773cb772009-06-23 10:36:38 +0100306 }
307
David Daneybabba4f2010-07-23 10:57:51 -0700308 pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
309 mb();
Ralf Baechle773cb772009-06-23 10:36:38 +0100310 cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
311 cvmx_write_csr(CVMX_CIU_PP_RST, 0);
312}
313
314void play_dead(void)
315{
David Daneybabba4f2010-07-23 10:57:51 -0700316 int cpu = cpu_number_map(cvmx_get_core_num());
Ralf Baechle773cb772009-06-23 10:36:38 +0100317
318 idle_task_exit();
319 octeon_processor_boot = 0xff;
David Daneybabba4f2010-07-23 10:57:51 -0700320 per_cpu(cpu_state, cpu) = CPU_DEAD;
321
322 mb();
Ralf Baechle773cb772009-06-23 10:36:38 +0100323
324 while (1) /* core will be reset here */
325 ;
326}
327
328extern void kernel_entry(unsigned long arg1, ...);
329
330static void start_after_reset(void)
331{
Ralf Baechle70342282013-01-22 12:59:30 +0100332 kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
Ralf Baechle773cb772009-06-23 10:36:38 +0100333}
334
David Daneybabba4f2010-07-23 10:57:51 -0700335static int octeon_update_boot_vector(unsigned int cpu)
Ralf Baechle773cb772009-06-23 10:36:38 +0100336{
337
338 int coreid = cpu_logical_map(cpu);
David Daneybabba4f2010-07-23 10:57:51 -0700339 uint32_t avail_coremask;
340 const struct cvmx_bootmem_named_block_desc *block_desc;
Ralf Baechle773cb772009-06-23 10:36:38 +0100341 struct boot_init_vector *boot_vect =
David Daneybabba4f2010-07-23 10:57:51 -0700342 (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
Ralf Baechle773cb772009-06-23 10:36:38 +0100343
344 block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
345
346 if (!block_desc) {
David Daneybabba4f2010-07-23 10:57:51 -0700347 struct linux_app_boot_info *labi;
348
349 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
350
351 avail_coremask = labi->avail_coremask;
352 labi->avail_coremask &= ~(1 << coreid);
Ralf Baechle773cb772009-06-23 10:36:38 +0100353 } else { /* alternative, already initialized */
David Daneybabba4f2010-07-23 10:57:51 -0700354 avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
355 block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
Ralf Baechle773cb772009-06-23 10:36:38 +0100356 }
357
358 if (!(avail_coremask & (1 << coreid))) {
359 /* core not available, assume, that catched by simple-executive */
360 cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
361 cvmx_write_csr(CVMX_CIU_PP_RST, 0);
362 }
363
364 boot_vect[coreid].app_start_func_addr =
365 (uint32_t) (unsigned long) start_after_reset;
David Daneybabba4f2010-07-23 10:57:51 -0700366 boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
Ralf Baechle773cb772009-06-23 10:36:38 +0100367
David Daneybabba4f2010-07-23 10:57:51 -0700368 mb();
Ralf Baechle773cb772009-06-23 10:36:38 +0100369
370 cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
371
372 return 0;
373}
374
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000375static int octeon_cpu_callback(struct notifier_block *nfb,
Ralf Baechle773cb772009-06-23 10:36:38 +0100376 unsigned long action, void *hcpu)
377{
378 unsigned int cpu = (unsigned long)hcpu;
379
380 switch (action) {
381 case CPU_UP_PREPARE:
382 octeon_update_boot_vector(cpu);
383 break;
384 case CPU_ONLINE:
385 pr_info("Cpu %d online\n", cpu);
386 break;
387 case CPU_DEAD:
388 break;
389 }
390
391 return NOTIFY_OK;
392}
393
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000394static int register_cavium_notifier(void)
Ralf Baechle773cb772009-06-23 10:36:38 +0100395{
David Daney442f20122010-07-23 10:57:50 -0700396 hotcpu_notifier(octeon_cpu_callback, 0);
Ralf Baechle773cb772009-06-23 10:36:38 +0100397 return 0;
398}
Ralf Baechle773cb772009-06-23 10:36:38 +0100399late_initcall(register_cavium_notifier);
400
Ralf Baechle70342282013-01-22 12:59:30 +0100401#endif /* CONFIG_HOTPLUG_CPU */
Ralf Baechle773cb772009-06-23 10:36:38 +0100402
David Daney5b3b1682009-01-08 16:46:40 -0800403struct plat_smp_ops octeon_smp_ops = {
404 .send_ipi_single = octeon_send_ipi_single,
405 .send_ipi_mask = octeon_send_ipi_mask,
406 .init_secondary = octeon_init_secondary,
407 .smp_finish = octeon_smp_finish,
408 .cpus_done = octeon_cpus_done,
409 .boot_secondary = octeon_boot_secondary,
410 .smp_setup = octeon_smp_setup,
411 .prepare_cpus = octeon_prepare_cpus,
Ralf Baechle773cb772009-06-23 10:36:38 +0100412#ifdef CONFIG_HOTPLUG_CPU
413 .cpu_disable = octeon_cpu_disable,
414 .cpu_die = octeon_cpu_die,
415#endif
David Daney5b3b1682009-01-08 16:46:40 -0800416};