blob: 42395c561aa5cd058ca6fedd2f02260211310dd2 [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)
Alex Smith4ace6132015-07-24 16:57:49 +010045 generic_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
Rusty Russell8dd92892015-03-05 10:49:17 +103075 for_each_cpu(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
Aaro Koskinen5ca0e372014-06-28 00:59:51 +030087 if (!setup_max_cpus)
88 return;
89
David Daneybabba4f2010-07-23 10:57:51 -070090 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
Aaro Koskineneac44d92014-06-28 00:59:52 +030091 if (labi->labi_signature != LABI_SIGNATURE) {
92 pr_info("The bootloader on this board does not support HOTPLUG_CPU.");
93 return;
94 }
David Daneybabba4f2010-07-23 10:57:51 -070095
96 octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
Ralf Baechle773cb772009-06-23 10:36:38 +010097#endif
98}
99
David Daney5b3b1682009-01-08 16:46:40 -0800100static void octeon_smp_setup(void)
101{
102 const int coreid = cvmx_get_core_num();
103 int cpus;
104 int id;
David Daney5b3b1682009-01-08 16:46:40 -0800105 int core_mask = octeon_get_boot_coremask();
David Daney7d52ab12016-02-01 17:46:54 -0800106 struct cvmx_sysinfo *sysinfo = cvmx_sysinfo_get();
107
David Daneyedfcbb82010-07-23 10:57:49 -0700108#ifdef CONFIG_HOTPLUG_CPU
109 unsigned int num_cores = cvmx_octeon_num_cores();
110#endif
David Daney5b3b1682009-01-08 16:46:40 -0800111
David Daneyedfcbb82010-07-23 10:57:49 -0700112 /* 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);
116 }
117
David Daney5b3b1682009-01-08 16:46:40 -0800118 __cpu_number_map[coreid] = 0;
119 __cpu_logical_map[0] = coreid;
David Daney5b3b1682009-01-08 16:46:40 -0800120
David Daneyedfcbb82010-07-23 10:57:49 -0700121 /* The present CPUs get the lowest CPU numbers. */
David Daney5b3b1682009-01-08 16:46:40 -0800122 cpus = 1;
David Daneyedfcbb82010-07-23 10:57:49 -0700123 for (id = 0; id < NR_CPUS; id++) {
David Daney7d52ab12016-02-01 17:46:54 -0800124 if ((id != coreid) && cvmx_coremask_is_core_set(&sysinfo->core_mask, id)) {
David Daneyedfcbb82010-07-23 10:57:49 -0700125 set_cpu_possible(cpus, true);
126 set_cpu_present(cpus, true);
David Daney5b3b1682009-01-08 16:46:40 -0800127 __cpu_number_map[id] = cpus;
128 __cpu_logical_map[cpus] = id;
129 cpus++;
130 }
131 }
David Daneyedfcbb82010-07-23 10:57:49 -0700132
133#ifdef CONFIG_HOTPLUG_CPU
134 /*
Ralf Baechle70342282013-01-22 12:59:30 +0100135 * The possible CPUs are all those present on the chip. We
136 * will assign CPU numbers for possible cores as well. Cores
David Daneyedfcbb82010-07-23 10:57:49 -0700137 * are always consecutively numberd from 0.
138 */
Aaro Koskineneac44d92014-06-28 00:59:52 +0300139 for (id = 0; setup_max_cpus && octeon_bootloader_entry_addr &&
140 id < num_cores && id < NR_CPUS; id++) {
David Daneyedfcbb82010-07-23 10:57:49 -0700141 if (!(core_mask & (1 << id))) {
142 set_cpu_possible(cpus, true);
143 __cpu_number_map[id] = cpus;
144 __cpu_logical_map[cpus] = id;
145 cpus++;
146 }
147 }
148#endif
Ralf Baechle773cb772009-06-23 10:36:38 +0100149
150 octeon_smp_hotplug_setup();
David Daney5b3b1682009-01-08 16:46:40 -0800151}
152
153/**
154 * Firmware CPU startup hook
155 *
156 */
157static void octeon_boot_secondary(int cpu, struct task_struct *idle)
158{
159 int count;
160
161 pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
162 cpu_logical_map(cpu));
163
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);
167 mb();
168
169 count = 10000;
170 while (octeon_processor_sp && count) {
171 /* Waiting for processor to get the SP and GP */
172 udelay(1);
173 count--;
174 }
175 if (count == 0)
176 pr_err("Secondary boot timeout\n");
177}
178
179/**
180 * After we've done initial boot, this function is called to allow the
181 * board code to clean up state, if needed
182 */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000183static void octeon_init_secondary(void)
David Daney5b3b1682009-01-08 16:46:40 -0800184{
David Daneybabba4f2010-07-23 10:57:51 -0700185 unsigned int sr;
David Daney5b3b1682009-01-08 16:46:40 -0800186
David Daney0c326382011-03-25 12:38:51 -0700187 sr = set_c0_status(ST0_BEV);
188 write_c0_ebase((u32)ebase);
189 write_c0_status(sr);
190
191 octeon_check_cpu_bist();
192 octeon_init_cvmcount();
193
194 octeon_irq_setup_secondary();
David Daney0c326382011-03-25 12:38:51 -0700195}
196
197/**
198 * Callout to firmware before smp_init
199 *
200 */
201void octeon_prepare_cpus(unsigned int max_cpus)
202{
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{
David Daney5b3b1682009-01-08 16:46:40 -0800221 octeon_user_io_init();
222
223 /* to generate the first CPU timer interrupt */
224 write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
Yong Zhang1bcfecc2012-07-19 09:13:53 +0200225 local_irq_enable();
David Daney5b3b1682009-01-08 16:46:40 -0800226}
227
Ralf Baechle773cb772009-06-23 10:36:38 +0100228#ifdef CONFIG_HOTPLUG_CPU
229
230/* State of each CPU. */
231DEFINE_PER_CPU(int, cpu_state);
232
Ralf Baechle773cb772009-06-23 10:36:38 +0100233static int octeon_cpu_disable(void)
234{
235 unsigned int cpu = smp_processor_id();
236
237 if (cpu == 0)
238 return -EBUSY;
239
Aaro Koskineneac44d92014-06-28 00:59:52 +0300240 if (!octeon_bootloader_entry_addr)
241 return -ENOTSUPP;
242
Rusty Russell0b5f9c02012-03-29 15:38:30 +1030243 set_cpu_online(cpu, false);
Rusty Russell8dd92892015-03-05 10:49:17 +1030244 cpumask_clear_cpu(cpu, &cpu_callin_map);
Ralf Baechle17efb592013-09-03 18:19:28 +0200245 octeon_fixup_irqs();
Ralf Baechle773cb772009-06-23 10:36:38 +0100246
Ralf Baechle9329c152016-01-27 18:07:00 +0100247 __flush_cache_all();
Ralf Baechle773cb772009-06-23 10:36:38 +0100248 local_flush_tlb_all();
249
Ralf Baechle773cb772009-06-23 10:36:38 +0100250 return 0;
251}
252
253static void octeon_cpu_die(unsigned int cpu)
254{
255 int coreid = cpu_logical_map(cpu);
David Daneybabba4f2010-07-23 10:57:51 -0700256 uint32_t mask, new_mask;
257 const struct cvmx_bootmem_named_block_desc *block_desc;
Ralf Baechle773cb772009-06-23 10:36:38 +0100258
Ralf Baechle773cb772009-06-23 10:36:38 +0100259 while (per_cpu(cpu_state, cpu) != CPU_DEAD)
260 cpu_relax();
261
262 /*
263 * This is a bit complicated strategics of getting/settig available
264 * cores mask, copied from bootloader
265 */
David Daneybabba4f2010-07-23 10:57:51 -0700266
267 mask = 1 << coreid;
Ralf Baechle773cb772009-06-23 10:36:38 +0100268 /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
269 block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
270
271 if (!block_desc) {
David Daneybabba4f2010-07-23 10:57:51 -0700272 struct linux_app_boot_info *labi;
273
274 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
275
276 labi->avail_coremask |= mask;
277 new_mask = labi->avail_coremask;
Ralf Baechle773cb772009-06-23 10:36:38 +0100278 } else { /* alternative, already initialized */
David Daneybabba4f2010-07-23 10:57:51 -0700279 uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
280 AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
281 *p |= mask;
282 new_mask = *p;
Ralf Baechle773cb772009-06-23 10:36:38 +0100283 }
284
David Daneybabba4f2010-07-23 10:57:51 -0700285 pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
286 mb();
Ralf Baechle773cb772009-06-23 10:36:38 +0100287 cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
288 cvmx_write_csr(CVMX_CIU_PP_RST, 0);
289}
290
291void play_dead(void)
292{
David Daneybabba4f2010-07-23 10:57:51 -0700293 int cpu = cpu_number_map(cvmx_get_core_num());
Ralf Baechle773cb772009-06-23 10:36:38 +0100294
295 idle_task_exit();
296 octeon_processor_boot = 0xff;
David Daneybabba4f2010-07-23 10:57:51 -0700297 per_cpu(cpu_state, cpu) = CPU_DEAD;
298
299 mb();
Ralf Baechle773cb772009-06-23 10:36:38 +0100300
301 while (1) /* core will be reset here */
302 ;
303}
304
305extern void kernel_entry(unsigned long arg1, ...);
306
307static void start_after_reset(void)
308{
Ralf Baechle70342282013-01-22 12:59:30 +0100309 kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
Ralf Baechle773cb772009-06-23 10:36:38 +0100310}
311
David Daneybabba4f2010-07-23 10:57:51 -0700312static int octeon_update_boot_vector(unsigned int cpu)
Ralf Baechle773cb772009-06-23 10:36:38 +0100313{
314
315 int coreid = cpu_logical_map(cpu);
David Daneybabba4f2010-07-23 10:57:51 -0700316 uint32_t avail_coremask;
317 const struct cvmx_bootmem_named_block_desc *block_desc;
Ralf Baechle773cb772009-06-23 10:36:38 +0100318 struct boot_init_vector *boot_vect =
David Daneybabba4f2010-07-23 10:57:51 -0700319 (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
Ralf Baechle773cb772009-06-23 10:36:38 +0100320
321 block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
322
323 if (!block_desc) {
David Daneybabba4f2010-07-23 10:57:51 -0700324 struct linux_app_boot_info *labi;
325
326 labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
327
328 avail_coremask = labi->avail_coremask;
329 labi->avail_coremask &= ~(1 << coreid);
Ralf Baechle773cb772009-06-23 10:36:38 +0100330 } else { /* alternative, already initialized */
David Daneybabba4f2010-07-23 10:57:51 -0700331 avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
332 block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
Ralf Baechle773cb772009-06-23 10:36:38 +0100333 }
334
335 if (!(avail_coremask & (1 << coreid))) {
Adam Buchbinder92a76f62016-02-25 00:44:58 -0800336 /* core not available, assume, that caught by simple-executive */
Ralf Baechle773cb772009-06-23 10:36:38 +0100337 cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
338 cvmx_write_csr(CVMX_CIU_PP_RST, 0);
339 }
340
341 boot_vect[coreid].app_start_func_addr =
342 (uint32_t) (unsigned long) start_after_reset;
David Daneybabba4f2010-07-23 10:57:51 -0700343 boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
Ralf Baechle773cb772009-06-23 10:36:38 +0100344
David Daneybabba4f2010-07-23 10:57:51 -0700345 mb();
Ralf Baechle773cb772009-06-23 10:36:38 +0100346
347 cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
348
349 return 0;
350}
351
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000352static int octeon_cpu_callback(struct notifier_block *nfb,
Ralf Baechle773cb772009-06-23 10:36:38 +0100353 unsigned long action, void *hcpu)
354{
355 unsigned int cpu = (unsigned long)hcpu;
356
357 switch (action) {
358 case CPU_UP_PREPARE:
359 octeon_update_boot_vector(cpu);
360 break;
361 case CPU_ONLINE:
362 pr_info("Cpu %d online\n", cpu);
363 break;
364 case CPU_DEAD:
365 break;
366 }
367
368 return NOTIFY_OK;
369}
370
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000371static int register_cavium_notifier(void)
Ralf Baechle773cb772009-06-23 10:36:38 +0100372{
David Daney442f20122010-07-23 10:57:50 -0700373 hotcpu_notifier(octeon_cpu_callback, 0);
Ralf Baechle773cb772009-06-23 10:36:38 +0100374 return 0;
375}
Ralf Baechle773cb772009-06-23 10:36:38 +0100376late_initcall(register_cavium_notifier);
377
Ralf Baechle70342282013-01-22 12:59:30 +0100378#endif /* CONFIG_HOTPLUG_CPU */
Ralf Baechle773cb772009-06-23 10:36:38 +0100379
David Daney5b3b1682009-01-08 16:46:40 -0800380struct 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,
David Daney5b3b1682009-01-08 16:46:40 -0800385 .boot_secondary = octeon_boot_secondary,
386 .smp_setup = octeon_smp_setup,
387 .prepare_cpus = octeon_prepare_cpus,
Ralf Baechle773cb772009-06-23 10:36:38 +0100388#ifdef CONFIG_HOTPLUG_CPU
389 .cpu_disable = octeon_cpu_disable,
390 .cpu_die = octeon_cpu_die,
391#endif
David Daney5b3b1682009-01-08 16:46:40 -0800392};