blob: 6b96fedf0407d675428e7ca742dadc5500a95090 [file] [log] [blame]
Paul Burton0ee958e2014-01-15 10:31:53 +00001/*
2 * Copyright (C) 2013 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 */
10
11#include <linux/io.h>
12#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/smp.h>
15#include <linux/types.h>
16
17#include <asm/cacheflush.h>
18#include <asm/gic.h>
19#include <asm/mips-cm.h>
20#include <asm/mips-cpc.h>
21#include <asm/mips_mt.h>
22#include <asm/mipsregs.h>
Paul Burton1d8f1f52014-04-14 14:13:57 +010023#include <asm/pm-cps.h>
Paul Burton0ee958e2014-01-15 10:31:53 +000024#include <asm/smp-cps.h>
25#include <asm/time.h>
26#include <asm/uasm.h>
27
28static DECLARE_BITMAP(core_power, NR_CPUS);
29
Paul Burton245a7862014-04-14 12:04:27 +010030struct core_boot_config *mips_cps_core_bootcfg;
Paul Burton0ee958e2014-01-15 10:31:53 +000031
Paul Burton245a7862014-04-14 12:04:27 +010032static unsigned core_vpe_count(unsigned core)
Paul Burton0ee958e2014-01-15 10:31:53 +000033{
Paul Burton245a7862014-04-14 12:04:27 +010034 unsigned cfg;
Paul Burton0ee958e2014-01-15 10:31:53 +000035
Paul Burton245a7862014-04-14 12:04:27 +010036 if (!config_enabled(CONFIG_MIPS_MT_SMP) || !cpu_has_mipsmt)
37 return 1;
Paul Burton0ee958e2014-01-15 10:31:53 +000038
Paul Burton245a7862014-04-14 12:04:27 +010039 write_gcr_cl_other(core << CM_GCR_Cx_OTHER_CORENUM_SHF);
40 cfg = read_gcr_co_config() & CM_GCR_Cx_CONFIG_PVPE_MSK;
41 return (cfg >> CM_GCR_Cx_CONFIG_PVPE_SHF) + 1;
Paul Burton0ee958e2014-01-15 10:31:53 +000042}
43
44static void __init cps_smp_setup(void)
45{
46 unsigned int ncores, nvpes, core_vpes;
47 int c, v;
Paul Burton0ee958e2014-01-15 10:31:53 +000048
49 /* Detect & record VPE topology */
50 ncores = mips_cm_numcores();
51 pr_info("VPE topology ");
52 for (c = nvpes = 0; c < ncores; c++) {
Paul Burton245a7862014-04-14 12:04:27 +010053 core_vpes = core_vpe_count(c);
Paul Burton0ee958e2014-01-15 10:31:53 +000054 pr_cont("%c%u", c ? ',' : '{', core_vpes);
55
Paul Burton245a7862014-04-14 12:04:27 +010056 /* Use the number of VPEs in core 0 for smp_num_siblings */
57 if (!c)
58 smp_num_siblings = core_vpes;
59
Paul Burton0ee958e2014-01-15 10:31:53 +000060 for (v = 0; v < min_t(int, core_vpes, NR_CPUS - nvpes); v++) {
61 cpu_data[nvpes + v].core = c;
62#ifdef CONFIG_MIPS_MT_SMP
63 cpu_data[nvpes + v].vpe_id = v;
64#endif
65 }
66
67 nvpes += core_vpes;
68 }
69 pr_cont("} total %u\n", nvpes);
70
71 /* Indicate present CPUs (CPU being synonymous with VPE) */
72 for (v = 0; v < min_t(unsigned, nvpes, NR_CPUS); v++) {
73 set_cpu_possible(v, true);
74 set_cpu_present(v, true);
75 __cpu_number_map[v] = v;
76 __cpu_logical_map[v] = v;
77 }
78
Paul Burton33b68662014-04-14 15:58:45 +010079 /* Set a coherent default CCA (CWB) */
80 change_c0_config(CONF_CM_CMASK, 0x5);
81
Paul Burton0ee958e2014-01-15 10:31:53 +000082 /* Core 0 is powered up (we're running on it) */
83 bitmap_set(core_power, 0, 1);
84
Paul Burton0ee958e2014-01-15 10:31:53 +000085 /* Initialise core 0 */
Paul Burton245a7862014-04-14 12:04:27 +010086 mips_cps_core_init();
Paul Burton0ee958e2014-01-15 10:31:53 +000087
Paul Burton0ee958e2014-01-15 10:31:53 +000088 /* Make core 0 coherent with everything */
89 write_gcr_cl_coherence(0xff);
90}
91
92static void __init cps_prepare_cpus(unsigned int max_cpus)
93{
Paul Burton5c399f62014-04-14 15:21:25 +010094 unsigned ncores, core_vpes, c, cca;
95 bool cca_unsuitable;
Paul Burton0f4d3d12014-04-14 12:21:49 +010096 u32 *entry_code;
Paul Burton245a7862014-04-14 12:04:27 +010097
Paul Burton0ee958e2014-01-15 10:31:53 +000098 mips_mt_set_cpuoptions();
Paul Burton245a7862014-04-14 12:04:27 +010099
Paul Burton5c399f62014-04-14 15:21:25 +0100100 /* Detect whether the CCA is unsuited to multi-core SMP */
101 cca = read_c0_config() & CONF_CM_CMASK;
102 switch (cca) {
103 case 0x4: /* CWBE */
104 case 0x5: /* CWB */
105 /* The CCA is coherent, multi-core is fine */
106 cca_unsuitable = false;
107 break;
108
109 default:
110 /* CCA is not coherent, multi-core is not usable */
111 cca_unsuitable = true;
112 }
113
114 /* Warn the user if the CCA prevents multi-core */
115 ncores = mips_cm_numcores();
116 if (cca_unsuitable && ncores > 1) {
117 pr_warn("Using only one core due to unsuitable CCA 0x%x\n",
118 cca);
119
120 for_each_present_cpu(c) {
121 if (cpu_data[c].core)
122 set_cpu_present(c, false);
123 }
124 }
125
Paul Burton0f4d3d12014-04-14 12:21:49 +0100126 /* Patch the start of mips_cps_core_entry to provide the CM base */
127 entry_code = (u32 *)&mips_cps_core_entry;
128 UASM_i_LA(&entry_code, 3, (long)mips_cm_base);
129 dma_cache_wback_inv((unsigned long)&mips_cps_core_entry,
130 (void *)entry_code - (void *)&mips_cps_core_entry);
131
Paul Burton245a7862014-04-14 12:04:27 +0100132 /* Allocate core boot configuration structs */
Paul Burton245a7862014-04-14 12:04:27 +0100133 mips_cps_core_bootcfg = kcalloc(ncores, sizeof(*mips_cps_core_bootcfg),
134 GFP_KERNEL);
135 if (!mips_cps_core_bootcfg) {
136 pr_err("Failed to allocate boot config for %u cores\n", ncores);
137 goto err_out;
138 }
139
140 /* Allocate VPE boot configuration structs */
141 for (c = 0; c < ncores; c++) {
142 core_vpes = core_vpe_count(c);
143 mips_cps_core_bootcfg[c].vpe_config = kcalloc(core_vpes,
144 sizeof(*mips_cps_core_bootcfg[c].vpe_config),
145 GFP_KERNEL);
146 if (!mips_cps_core_bootcfg[c].vpe_config) {
147 pr_err("Failed to allocate %u VPE boot configs\n",
148 core_vpes);
149 goto err_out;
150 }
151 }
152
153 /* Mark this CPU as booted */
154 atomic_set(&mips_cps_core_bootcfg[current_cpu_data.core].vpe_mask,
155 1 << cpu_vpe_id(&current_cpu_data));
156
157 return;
158err_out:
159 /* Clean up allocations */
160 if (mips_cps_core_bootcfg) {
161 for (c = 0; c < ncores; c++)
162 kfree(mips_cps_core_bootcfg[c].vpe_config);
163 kfree(mips_cps_core_bootcfg);
164 mips_cps_core_bootcfg = NULL;
165 }
166
167 /* Effectively disable SMP by declaring CPUs not present */
168 for_each_possible_cpu(c) {
169 if (c == 0)
170 continue;
171 set_cpu_present(c, false);
172 }
Paul Burton0ee958e2014-01-15 10:31:53 +0000173}
174
Paul Burton245a7862014-04-14 12:04:27 +0100175static void boot_core(unsigned core)
Paul Burton0ee958e2014-01-15 10:31:53 +0000176{
177 u32 access;
178
179 /* Select the appropriate core */
Paul Burton245a7862014-04-14 12:04:27 +0100180 write_gcr_cl_other(core << CM_GCR_Cx_OTHER_CORENUM_SHF);
Paul Burton0ee958e2014-01-15 10:31:53 +0000181
182 /* Set its reset vector */
183 write_gcr_co_reset_base(CKSEG1ADDR((unsigned long)mips_cps_core_entry));
184
185 /* Ensure its coherency is disabled */
186 write_gcr_co_coherence(0);
187
188 /* Ensure the core can access the GCRs */
189 access = read_gcr_access();
Paul Burton245a7862014-04-14 12:04:27 +0100190 access |= 1 << (CM_GCR_ACCESS_ACCESSEN_SHF + core);
Paul Burton0ee958e2014-01-15 10:31:53 +0000191 write_gcr_access(access);
192
Paul Burton0ee958e2014-01-15 10:31:53 +0000193 if (mips_cpc_present()) {
Paul Burton0ee958e2014-01-15 10:31:53 +0000194 /* Reset the core */
Paul Burtondd9233d2014-03-07 10:42:52 +0000195 mips_cpc_lock_other(core);
Paul Burton0ee958e2014-01-15 10:31:53 +0000196 write_cpc_co_cmd(CPC_Cx_CMD_RESET);
Paul Burtondd9233d2014-03-07 10:42:52 +0000197 mips_cpc_unlock_other();
Paul Burton0ee958e2014-01-15 10:31:53 +0000198 } else {
199 /* Take the core out of reset */
200 write_gcr_co_reset_release(0);
201 }
202
203 /* The core is now powered up */
Paul Burton245a7862014-04-14 12:04:27 +0100204 bitmap_set(core_power, core, 1);
Paul Burton0ee958e2014-01-15 10:31:53 +0000205}
206
Paul Burton245a7862014-04-14 12:04:27 +0100207static void remote_vpe_boot(void *dummy)
Paul Burton0ee958e2014-01-15 10:31:53 +0000208{
Paul Burton245a7862014-04-14 12:04:27 +0100209 mips_cps_boot_vpes();
Paul Burton0ee958e2014-01-15 10:31:53 +0000210}
211
212static void cps_boot_secondary(int cpu, struct task_struct *idle)
213{
Paul Burton245a7862014-04-14 12:04:27 +0100214 unsigned core = cpu_data[cpu].core;
215 unsigned vpe_id = cpu_vpe_id(&cpu_data[cpu]);
216 struct core_boot_config *core_cfg = &mips_cps_core_bootcfg[core];
217 struct vpe_boot_config *vpe_cfg = &core_cfg->vpe_config[vpe_id];
Paul Burton0ee958e2014-01-15 10:31:53 +0000218 unsigned int remote;
219 int err;
220
Paul Burton245a7862014-04-14 12:04:27 +0100221 vpe_cfg->pc = (unsigned long)&smp_bootstrap;
222 vpe_cfg->sp = __KSTK_TOS(idle);
223 vpe_cfg->gp = (unsigned long)task_thread_info(idle);
Paul Burton0ee958e2014-01-15 10:31:53 +0000224
Paul Burton245a7862014-04-14 12:04:27 +0100225 atomic_or(1 << cpu_vpe_id(&cpu_data[cpu]), &core_cfg->vpe_mask);
226
Paul Burton1d8f1f52014-04-14 14:13:57 +0100227 preempt_disable();
228
Paul Burton245a7862014-04-14 12:04:27 +0100229 if (!test_bit(core, core_power)) {
Paul Burton0ee958e2014-01-15 10:31:53 +0000230 /* Boot a VPE on a powered down core */
Paul Burton245a7862014-04-14 12:04:27 +0100231 boot_core(core);
Paul Burton1d8f1f52014-04-14 14:13:57 +0100232 goto out;
Paul Burton0ee958e2014-01-15 10:31:53 +0000233 }
234
Paul Burton245a7862014-04-14 12:04:27 +0100235 if (core != current_cpu_data.core) {
Paul Burton0ee958e2014-01-15 10:31:53 +0000236 /* Boot a VPE on another powered up core */
237 for (remote = 0; remote < NR_CPUS; remote++) {
Paul Burton245a7862014-04-14 12:04:27 +0100238 if (cpu_data[remote].core != core)
Paul Burton0ee958e2014-01-15 10:31:53 +0000239 continue;
240 if (cpu_online(remote))
241 break;
242 }
243 BUG_ON(remote >= NR_CPUS);
244
Paul Burton245a7862014-04-14 12:04:27 +0100245 err = smp_call_function_single(remote, remote_vpe_boot,
246 NULL, 1);
Paul Burton0ee958e2014-01-15 10:31:53 +0000247 if (err)
248 panic("Failed to call remote CPU\n");
Paul Burton1d8f1f52014-04-14 14:13:57 +0100249 goto out;
Paul Burton0ee958e2014-01-15 10:31:53 +0000250 }
251
252 BUG_ON(!cpu_has_mipsmt);
253
254 /* Boot a VPE on this core */
Paul Burton245a7862014-04-14 12:04:27 +0100255 mips_cps_boot_vpes();
Paul Burton1d8f1f52014-04-14 14:13:57 +0100256out:
257 preempt_enable();
Paul Burton0ee958e2014-01-15 10:31:53 +0000258}
259
260static void cps_init_secondary(void)
261{
262 /* Disable MT - we only want to run 1 TC per VPE */
263 if (cpu_has_mipsmt)
264 dmt();
265
Paul Burton0ee958e2014-01-15 10:31:53 +0000266 change_c0_status(ST0_IM, STATUSF_IP3 | STATUSF_IP4 |
267 STATUSF_IP6 | STATUSF_IP7);
268}
269
270static void cps_smp_finish(void)
271{
272 write_c0_compare(read_c0_count() + (8 * mips_hpt_frequency / HZ));
273
274#ifdef CONFIG_MIPS_MT_FPAFF
275 /* If we have an FPU, enroll ourselves in the FPU-full mask */
276 if (cpu_has_fpu)
277 cpu_set(smp_processor_id(), mt_fpu_cpumask);
278#endif /* CONFIG_MIPS_MT_FPAFF */
279
280 local_irq_enable();
281}
282
283static void cps_cpus_done(void)
284{
285}
286
Paul Burton1d8f1f52014-04-14 14:13:57 +0100287#ifdef CONFIG_HOTPLUG_CPU
288
289static int cps_cpu_disable(void)
290{
291 unsigned cpu = smp_processor_id();
292 struct core_boot_config *core_cfg;
293
294 if (!cpu)
295 return -EBUSY;
296
297 if (!cps_pm_support_state(CPS_PM_POWER_GATED))
298 return -EINVAL;
299
300 core_cfg = &mips_cps_core_bootcfg[current_cpu_data.core];
301 atomic_sub(1 << cpu_vpe_id(&current_cpu_data), &core_cfg->vpe_mask);
302 smp_mb__after_atomic_dec();
303 set_cpu_online(cpu, false);
304 cpu_clear(cpu, cpu_callin_map);
305
306 return 0;
307}
308
309static DECLARE_COMPLETION(cpu_death_chosen);
310static unsigned cpu_death_sibling;
311static enum {
312 CPU_DEATH_HALT,
313 CPU_DEATH_POWER,
314} cpu_death;
315
316void play_dead(void)
317{
318 unsigned cpu, core;
319
320 local_irq_disable();
321 idle_task_exit();
322 cpu = smp_processor_id();
323 cpu_death = CPU_DEATH_POWER;
324
325 if (cpu_has_mipsmt) {
326 core = cpu_data[cpu].core;
327
328 /* Look for another online VPE within the core */
329 for_each_online_cpu(cpu_death_sibling) {
330 if (cpu_data[cpu_death_sibling].core != core)
331 continue;
332
333 /*
334 * There is an online VPE within the core. Just halt
335 * this TC and leave the core alone.
336 */
337 cpu_death = CPU_DEATH_HALT;
338 break;
339 }
340 }
341
342 /* This CPU has chosen its way out */
343 complete(&cpu_death_chosen);
344
345 if (cpu_death == CPU_DEATH_HALT) {
346 /* Halt this TC */
347 write_c0_tchalt(TCHALT_H);
348 instruction_hazard();
349 } else {
350 /* Power down the core */
351 cps_pm_enter_state(CPS_PM_POWER_GATED);
352 }
353
354 /* This should never be reached */
355 panic("Failed to offline CPU %u", cpu);
356}
357
358static void wait_for_sibling_halt(void *ptr_cpu)
359{
360 unsigned cpu = (unsigned)ptr_cpu;
361 unsigned vpe_id = cpu_data[cpu].vpe_id;
362 unsigned halted;
363 unsigned long flags;
364
365 do {
366 local_irq_save(flags);
367 settc(vpe_id);
368 halted = read_tc_c0_tchalt();
369 local_irq_restore(flags);
370 } while (!(halted & TCHALT_H));
371}
372
373static void cps_cpu_die(unsigned int cpu)
374{
375 unsigned core = cpu_data[cpu].core;
376 unsigned stat;
377 int err;
378
379 /* Wait for the cpu to choose its way out */
380 if (!wait_for_completion_timeout(&cpu_death_chosen,
381 msecs_to_jiffies(5000))) {
382 pr_err("CPU%u: didn't offline\n", cpu);
383 return;
384 }
385
386 /*
387 * Now wait for the CPU to actually offline. Without doing this that
388 * offlining may race with one or more of:
389 *
390 * - Onlining the CPU again.
391 * - Powering down the core if another VPE within it is offlined.
392 * - A sibling VPE entering a non-coherent state.
393 *
394 * In the non-MT halt case (ie. infinite loop) the CPU is doing nothing
395 * with which we could race, so do nothing.
396 */
397 if (cpu_death == CPU_DEATH_POWER) {
398 /*
399 * Wait for the core to enter a powered down or clock gated
400 * state, the latter happening when a JTAG probe is connected
401 * in which case the CPC will refuse to power down the core.
402 */
403 do {
404 mips_cpc_lock_other(core);
405 stat = read_cpc_co_stat_conf();
406 stat &= CPC_Cx_STAT_CONF_SEQSTATE_MSK;
407 mips_cpc_unlock_other();
408 } while (stat != CPC_Cx_STAT_CONF_SEQSTATE_D0 &&
409 stat != CPC_Cx_STAT_CONF_SEQSTATE_D2 &&
410 stat != CPC_Cx_STAT_CONF_SEQSTATE_U2);
411
412 /* Indicate the core is powered off */
413 bitmap_clear(core_power, core, 1);
414 } else if (cpu_has_mipsmt) {
415 /*
416 * Have a CPU with access to the offlined CPUs registers wait
417 * for its TC to halt.
418 */
419 err = smp_call_function_single(cpu_death_sibling,
420 wait_for_sibling_halt,
421 (void *)cpu, 1);
422 if (err)
423 panic("Failed to call remote sibling CPU\n");
424 }
425}
426
427#endif /* CONFIG_HOTPLUG_CPU */
428
Paul Burton0ee958e2014-01-15 10:31:53 +0000429static struct plat_smp_ops cps_smp_ops = {
430 .smp_setup = cps_smp_setup,
431 .prepare_cpus = cps_prepare_cpus,
432 .boot_secondary = cps_boot_secondary,
433 .init_secondary = cps_init_secondary,
434 .smp_finish = cps_smp_finish,
435 .send_ipi_single = gic_send_ipi_single,
436 .send_ipi_mask = gic_send_ipi_mask,
437 .cpus_done = cps_cpus_done,
Paul Burton1d8f1f52014-04-14 14:13:57 +0100438#ifdef CONFIG_HOTPLUG_CPU
439 .cpu_disable = cps_cpu_disable,
440 .cpu_die = cps_cpu_die,
441#endif
Paul Burton0ee958e2014-01-15 10:31:53 +0000442};
443
Paul Burton68c12322014-03-14 16:06:16 +0000444bool mips_cps_smp_in_use(void)
445{
446 extern struct plat_smp_ops *mp_ops;
447 return mp_ops == &cps_smp_ops;
448}
449
Paul Burton0ee958e2014-01-15 10:31:53 +0000450int register_cps_smp_ops(void)
451{
452 if (!mips_cm_present()) {
453 pr_warn("MIPS CPS SMP unable to proceed without a CM\n");
454 return -ENODEV;
455 }
456
457 /* check we have a GIC - we need one for IPIs */
458 if (!(read_gcr_gic_status() & CM_GCR_GIC_STATUS_EX_MSK)) {
459 pr_warn("MIPS CPS SMP unable to proceed without a GIC\n");
460 return -ENODEV;
461 }
462
463 register_smp_ops(&cps_smp_ops);
464 return 0;
465}