smp-bmips.c 13 KB

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