smp-bmips.c 11 KB

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