smp-bmips.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  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. *
  6. * Copyright (C) 2011 by Kevin Cernekee (cernekee@gmail.com)
  7. *
  8. * SMP support for BMIPS
  9. */
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/delay.h>
  14. #include <linux/smp.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/cpu.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/reboot.h>
  20. #include <linux/io.h>
  21. #include <linux/compiler.h>
  22. #include <linux/linkage.h>
  23. #include <linux/bug.h>
  24. #include <linux/kernel.h>
  25. #include <asm/time.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/processor.h>
  28. #include <asm/bootinfo.h>
  29. #include <asm/pmon.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/mipsregs.h>
  33. #include <asm/bmips.h>
  34. #include <asm/traps.h>
  35. #include <asm/barrier.h>
  36. static int __maybe_unused max_cpus = 1;
  37. /* these may be configured by the platform code */
  38. int bmips_smp_enabled = 1;
  39. int bmips_cpu_offset;
  40. cpumask_t bmips_booted_mask;
  41. #ifdef CONFIG_SMP
  42. /* initial $sp, $gp - used by arch/mips/kernel/bmips_vec.S */
  43. unsigned long bmips_smp_boot_sp;
  44. unsigned long bmips_smp_boot_gp;
  45. static void bmips_send_ipi_single(int cpu, unsigned int action);
  46. static irqreturn_t bmips_ipi_interrupt(int irq, void *dev_id);
  47. /* SW interrupts 0,1 are used for interprocessor signaling */
  48. #define IPI0_IRQ (MIPS_CPU_IRQ_BASE + 0)
  49. #define IPI1_IRQ (MIPS_CPU_IRQ_BASE + 1)
  50. #define CPUNUM(cpu, shift) (((cpu) + bmips_cpu_offset) << (shift))
  51. #define ACTION_CLR_IPI(cpu, ipi) (0x2000 | CPUNUM(cpu, 9) | ((ipi) << 8))
  52. #define ACTION_SET_IPI(cpu, ipi) (0x3000 | CPUNUM(cpu, 9) | ((ipi) << 8))
  53. #define ACTION_BOOT_THREAD(cpu) (0x08 | CPUNUM(cpu, 0))
  54. static void __init bmips_smp_setup(void)
  55. {
  56. int i, cpu = 1, boot_cpu = 0;
  57. #if defined(CONFIG_CPU_BMIPS4350) || defined(CONFIG_CPU_BMIPS4380)
  58. int cpu_hw_intr;
  59. /* arbitration priority */
  60. clear_c0_brcm_cmt_ctrl(0x30);
  61. /* NBK and weak order flags */
  62. set_c0_brcm_config_0(0x30000);
  63. /* Find out if we are running on TP0 or TP1 */
  64. boot_cpu = !!(read_c0_brcm_cmt_local() & (1 << 31));
  65. /*
  66. * MIPS interrupts 0,1 (SW INT 0,1) cross over to the other thread
  67. * MIPS interrupt 2 (HW INT 0) is the CPU0 L1 controller output
  68. * MIPS interrupt 3 (HW INT 1) is the CPU1 L1 controller output
  69. */
  70. if (boot_cpu == 0)
  71. cpu_hw_intr = 0x02;
  72. else
  73. cpu_hw_intr = 0x1d;
  74. change_c0_brcm_cmt_intr(0xf8018000, (cpu_hw_intr << 27) | (0x03 << 15));
  75. /* single core, 2 threads (2 pipelines) */
  76. max_cpus = 2;
  77. #elif defined(CONFIG_CPU_BMIPS5000)
  78. /* enable raceless SW interrupts */
  79. set_c0_brcm_config(0x03 << 22);
  80. /* route HW interrupt 0 to CPU0, HW interrupt 1 to CPU1 */
  81. change_c0_brcm_mode(0x1f << 27, 0x02 << 27);
  82. /* N cores, 2 threads per core */
  83. max_cpus = (((read_c0_brcm_config() >> 6) & 0x03) + 1) << 1;
  84. /* clear any pending SW interrupts */
  85. for (i = 0; i < max_cpus; i++) {
  86. write_c0_brcm_action(ACTION_CLR_IPI(i, 0));
  87. write_c0_brcm_action(ACTION_CLR_IPI(i, 1));
  88. }
  89. #endif
  90. if (!bmips_smp_enabled)
  91. max_cpus = 1;
  92. /* this can be overridden by the BSP */
  93. if (!board_ebase_setup)
  94. board_ebase_setup = &bmips_ebase_setup;
  95. __cpu_number_map[boot_cpu] = 0;
  96. __cpu_logical_map[0] = boot_cpu;
  97. for (i = 0; i < max_cpus; i++) {
  98. if (i != boot_cpu) {
  99. __cpu_number_map[i] = cpu;
  100. __cpu_logical_map[cpu] = i;
  101. cpu++;
  102. }
  103. set_cpu_possible(i, 1);
  104. set_cpu_present(i, 1);
  105. }
  106. }
  107. /*
  108. * IPI IRQ setup - runs on CPU0
  109. */
  110. static void bmips_prepare_cpus(unsigned int max_cpus)
  111. {
  112. if (request_irq(IPI0_IRQ, bmips_ipi_interrupt, IRQF_PERCPU,
  113. "smp_ipi0", NULL))
  114. panic("Can't request IPI0 interrupt\n");
  115. if (request_irq(IPI1_IRQ, bmips_ipi_interrupt, IRQF_PERCPU,
  116. "smp_ipi1", NULL))
  117. panic("Can't request IPI1 interrupt\n");
  118. }
  119. /*
  120. * Tell the hardware to boot CPUx - runs on CPU0
  121. */
  122. static void bmips_boot_secondary(int cpu, struct task_struct *idle)
  123. {
  124. bmips_smp_boot_sp = __KSTK_TOS(idle);
  125. bmips_smp_boot_gp = (unsigned long)task_thread_info(idle);
  126. mb();
  127. /*
  128. * Initial boot sequence for secondary CPU:
  129. * bmips_reset_nmi_vec @ a000_0000 ->
  130. * bmips_smp_entry ->
  131. * plat_wired_tlb_setup (cached function call; optional) ->
  132. * start_secondary (cached jump)
  133. *
  134. * Warm restart sequence:
  135. * play_dead WAIT loop ->
  136. * bmips_smp_int_vec @ BMIPS_WARM_RESTART_VEC ->
  137. * eret to play_dead ->
  138. * bmips_secondary_reentry ->
  139. * start_secondary
  140. */
  141. pr_info("SMP: Booting CPU%d...\n", cpu);
  142. if (cpumask_test_cpu(cpu, &bmips_booted_mask))
  143. bmips_send_ipi_single(cpu, 0);
  144. else {
  145. #if defined(CONFIG_CPU_BMIPS4350) || defined(CONFIG_CPU_BMIPS4380)
  146. /* Reset slave TP1 if booting from TP0 */
  147. if (cpu_logical_map(cpu) == 1)
  148. set_c0_brcm_cmt_ctrl(0x01);
  149. #elif defined(CONFIG_CPU_BMIPS5000)
  150. if (cpu & 0x01)
  151. write_c0_brcm_action(ACTION_BOOT_THREAD(cpu));
  152. else {
  153. /*
  154. * core N thread 0 was already booted; just
  155. * pulse the NMI line
  156. */
  157. bmips_write_zscm_reg(0x210, 0xc0000000);
  158. udelay(10);
  159. bmips_write_zscm_reg(0x210, 0x00);
  160. }
  161. #endif
  162. cpumask_set_cpu(cpu, &bmips_booted_mask);
  163. }
  164. }
  165. /*
  166. * Early setup - runs on secondary CPU after cache probe
  167. */
  168. static void bmips_init_secondary(void)
  169. {
  170. /* move NMI vector to kseg0, in case XKS01 is enabled */
  171. #if defined(CONFIG_CPU_BMIPS4350) || defined(CONFIG_CPU_BMIPS4380)
  172. void __iomem *cbr = BMIPS_GET_CBR();
  173. unsigned long old_vec;
  174. unsigned long relo_vector;
  175. int boot_cpu;
  176. boot_cpu = !!(read_c0_brcm_cmt_local() & (1 << 31));
  177. relo_vector = boot_cpu ? BMIPS_RELO_VECTOR_CONTROL_0 :
  178. BMIPS_RELO_VECTOR_CONTROL_1;
  179. old_vec = __raw_readl(cbr + relo_vector);
  180. __raw_writel(old_vec & ~0x20000000, cbr + relo_vector);
  181. clear_c0_cause(smp_processor_id() ? C_SW1 : C_SW0);
  182. #elif defined(CONFIG_CPU_BMIPS5000)
  183. write_c0_brcm_bootvec(read_c0_brcm_bootvec() &
  184. (smp_processor_id() & 0x01 ? ~0x20000000 : ~0x2000));
  185. write_c0_brcm_action(ACTION_CLR_IPI(smp_processor_id(), 0));
  186. #endif
  187. }
  188. /*
  189. * Late setup - runs on secondary CPU before entering the idle loop
  190. */
  191. static void bmips_smp_finish(void)
  192. {
  193. pr_info("SMP: CPU%d is running\n", smp_processor_id());
  194. /* make sure there won't be a timer interrupt for a little while */
  195. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  196. irq_enable_hazard();
  197. set_c0_status(IE_SW0 | IE_SW1 | IE_IRQ1 | IE_IRQ5 | ST0_IE);
  198. irq_enable_hazard();
  199. }
  200. /*
  201. * Runs on CPU0 after all CPUs have been booted
  202. */
  203. static void bmips_cpus_done(void)
  204. {
  205. }
  206. #if defined(CONFIG_CPU_BMIPS5000)
  207. /*
  208. * BMIPS5000 raceless IPIs
  209. *
  210. * Each CPU has two inbound SW IRQs which are independent of all other CPUs.
  211. * IPI0 is used for SMP_RESCHEDULE_YOURSELF
  212. * IPI1 is used for SMP_CALL_FUNCTION
  213. */
  214. static void bmips_send_ipi_single(int cpu, unsigned int action)
  215. {
  216. write_c0_brcm_action(ACTION_SET_IPI(cpu, action == SMP_CALL_FUNCTION));
  217. }
  218. static irqreturn_t bmips_ipi_interrupt(int irq, void *dev_id)
  219. {
  220. int action = irq - IPI0_IRQ;
  221. write_c0_brcm_action(ACTION_CLR_IPI(smp_processor_id(), action));
  222. if (action == 0)
  223. scheduler_ipi();
  224. else
  225. smp_call_function_interrupt();
  226. return IRQ_HANDLED;
  227. }
  228. #else
  229. /*
  230. * BMIPS43xx racey IPIs
  231. *
  232. * We use one inbound SW IRQ for each CPU.
  233. *
  234. * A spinlock must be held in order to keep CPUx from accidentally clearing
  235. * an incoming IPI when it writes CP0 CAUSE to raise an IPI on CPUy. The
  236. * same spinlock is used to protect the action masks.
  237. */
  238. static DEFINE_SPINLOCK(ipi_lock);
  239. static DEFINE_PER_CPU(int, ipi_action_mask);
  240. static void bmips_send_ipi_single(int cpu, unsigned int action)
  241. {
  242. unsigned long flags;
  243. spin_lock_irqsave(&ipi_lock, flags);
  244. set_c0_cause(cpu ? C_SW1 : C_SW0);
  245. per_cpu(ipi_action_mask, cpu) |= action;
  246. irq_enable_hazard();
  247. spin_unlock_irqrestore(&ipi_lock, flags);
  248. }
  249. static irqreturn_t bmips_ipi_interrupt(int irq, void *dev_id)
  250. {
  251. unsigned long flags;
  252. int action, cpu = irq - IPI0_IRQ;
  253. spin_lock_irqsave(&ipi_lock, flags);
  254. action = __get_cpu_var(ipi_action_mask);
  255. per_cpu(ipi_action_mask, cpu) = 0;
  256. clear_c0_cause(cpu ? C_SW1 : C_SW0);
  257. spin_unlock_irqrestore(&ipi_lock, flags);
  258. if (action & SMP_RESCHEDULE_YOURSELF)
  259. scheduler_ipi();
  260. if (action & SMP_CALL_FUNCTION)
  261. smp_call_function_interrupt();
  262. return IRQ_HANDLED;
  263. }
  264. #endif /* BMIPS type */
  265. static void bmips_send_ipi_mask(const struct cpumask *mask,
  266. unsigned int action)
  267. {
  268. unsigned int i;
  269. for_each_cpu(i, mask)
  270. bmips_send_ipi_single(i, action);
  271. }
  272. #ifdef CONFIG_HOTPLUG_CPU
  273. static int bmips_cpu_disable(void)
  274. {
  275. unsigned int cpu = smp_processor_id();
  276. if (cpu == 0)
  277. return -EBUSY;
  278. pr_info("SMP: CPU%d is offline\n", cpu);
  279. set_cpu_online(cpu, false);
  280. cpu_clear(cpu, cpu_callin_map);
  281. local_flush_tlb_all();
  282. local_flush_icache_range(0, ~0);
  283. return 0;
  284. }
  285. static void bmips_cpu_die(unsigned int cpu)
  286. {
  287. }
  288. void __ref play_dead(void)
  289. {
  290. idle_task_exit();
  291. /* flush data cache */
  292. _dma_cache_wback_inv(0, ~0);
  293. /*
  294. * Wakeup is on SW0 or SW1; disable everything else
  295. * Use BEV !IV (BMIPS_WARM_RESTART_VEC) to avoid the regular Linux
  296. * IRQ handlers; this clears ST0_IE and returns immediately.
  297. */
  298. clear_c0_cause(CAUSEF_IV | C_SW0 | C_SW1);
  299. change_c0_status(IE_IRQ5 | IE_IRQ1 | IE_SW0 | IE_SW1 | ST0_IE | ST0_BEV,
  300. IE_SW0 | IE_SW1 | ST0_IE | ST0_BEV);
  301. irq_disable_hazard();
  302. /*
  303. * wait for SW interrupt from bmips_boot_secondary(), then jump
  304. * back to start_secondary()
  305. */
  306. __asm__ __volatile__(
  307. " wait\n"
  308. " j bmips_secondary_reentry\n"
  309. : : : "memory");
  310. }
  311. #endif /* CONFIG_HOTPLUG_CPU */
  312. struct plat_smp_ops bmips_smp_ops = {
  313. .smp_setup = bmips_smp_setup,
  314. .prepare_cpus = bmips_prepare_cpus,
  315. .boot_secondary = bmips_boot_secondary,
  316. .smp_finish = bmips_smp_finish,
  317. .init_secondary = bmips_init_secondary,
  318. .cpus_done = bmips_cpus_done,
  319. .send_ipi_single = bmips_send_ipi_single,
  320. .send_ipi_mask = bmips_send_ipi_mask,
  321. #ifdef CONFIG_HOTPLUG_CPU
  322. .cpu_disable = bmips_cpu_disable,
  323. .cpu_die = bmips_cpu_die,
  324. #endif
  325. };
  326. #endif /* CONFIG_SMP */
  327. /***********************************************************************
  328. * BMIPS vector relocation
  329. * This is primarily used for SMP boot, but it is applicable to some
  330. * UP BMIPS systems as well.
  331. ***********************************************************************/
  332. static void bmips_wr_vec(unsigned long dst, char *start, char *end)
  333. {
  334. memcpy((void *)dst, start, end - start);
  335. dma_cache_wback((unsigned long)start, end - start);
  336. local_flush_icache_range(dst, dst + (end - start));
  337. instruction_hazard();
  338. }
  339. static inline void bmips_nmi_handler_setup(void)
  340. {
  341. bmips_wr_vec(BMIPS_NMI_RESET_VEC, &bmips_reset_nmi_vec,
  342. &bmips_reset_nmi_vec_end);
  343. bmips_wr_vec(BMIPS_WARM_RESTART_VEC, &bmips_smp_int_vec,
  344. &bmips_smp_int_vec_end);
  345. }
  346. void bmips_ebase_setup(void)
  347. {
  348. unsigned long new_ebase = ebase;
  349. void __iomem __maybe_unused *cbr;
  350. BUG_ON(ebase != CKSEG0);
  351. #if defined(CONFIG_CPU_BMIPS4350)
  352. /*
  353. * BMIPS4350 cannot relocate the normal vectors, but it
  354. * can relocate the BEV=1 vectors. So CPU1 starts up at
  355. * the relocated BEV=1, IV=0 general exception vector @
  356. * 0xa000_0380.
  357. *
  358. * set_uncached_handler() is used here because:
  359. * - CPU1 will run this from uncached space
  360. * - None of the cacheflush functions are set up yet
  361. */
  362. set_uncached_handler(BMIPS_WARM_RESTART_VEC - CKSEG0,
  363. &bmips_smp_int_vec, 0x80);
  364. __sync();
  365. return;
  366. #elif defined(CONFIG_CPU_BMIPS4380)
  367. /*
  368. * 0x8000_0000: reset/NMI (initially in kseg1)
  369. * 0x8000_0400: normal vectors
  370. */
  371. new_ebase = 0x80000400;
  372. cbr = BMIPS_GET_CBR();
  373. __raw_writel(0x80080800, cbr + BMIPS_RELO_VECTOR_CONTROL_0);
  374. __raw_writel(0xa0080800, cbr + BMIPS_RELO_VECTOR_CONTROL_1);
  375. #elif defined(CONFIG_CPU_BMIPS5000)
  376. /*
  377. * 0x8000_0000: reset/NMI (initially in kseg1)
  378. * 0x8000_1000: normal vectors
  379. */
  380. new_ebase = 0x80001000;
  381. write_c0_brcm_bootvec(0xa0088008);
  382. write_c0_ebase(new_ebase);
  383. if (max_cpus > 2)
  384. bmips_write_zscm_reg(0xa0, 0xa008a008);
  385. #else
  386. return;
  387. #endif
  388. board_nmi_handler_setup = &bmips_nmi_handler_setup;
  389. ebase = new_ebase;
  390. }
  391. asmlinkage void __weak plat_wired_tlb_setup(void)
  392. {
  393. /*
  394. * Called when starting/restarting a secondary CPU.
  395. * Kernel stacks and other important data might only be accessible
  396. * once the wired entries are present.
  397. */
  398. }