smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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) 2004-2008, 2009, 2010 Cavium Networks
  7. */
  8. #include <linux/cpu.h>
  9. #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/sched/hotplug.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <linux/kexec.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/time.h>
  21. #include <asm/setup.h>
  22. #include <asm/octeon/octeon.h>
  23. #include "octeon_boot.h"
  24. volatile unsigned long octeon_processor_boot = 0xff;
  25. volatile unsigned long octeon_processor_sp;
  26. volatile unsigned long octeon_processor_gp;
  27. #ifdef CONFIG_RELOCATABLE
  28. volatile unsigned long octeon_processor_relocated_kernel_entry;
  29. #endif /* CONFIG_RELOCATABLE */
  30. #ifdef CONFIG_HOTPLUG_CPU
  31. uint64_t octeon_bootloader_entry_addr;
  32. EXPORT_SYMBOL(octeon_bootloader_entry_addr);
  33. #endif
  34. extern void kernel_entry(unsigned long arg1, ...);
  35. static void octeon_icache_flush(void)
  36. {
  37. asm volatile ("synci 0($0)\n");
  38. }
  39. static void (*octeon_message_functions[8])(void) = {
  40. scheduler_ipi,
  41. generic_smp_call_function_interrupt,
  42. octeon_icache_flush,
  43. };
  44. static irqreturn_t mailbox_interrupt(int irq, void *dev_id)
  45. {
  46. u64 mbox_clrx = CVMX_CIU_MBOX_CLRX(cvmx_get_core_num());
  47. u64 action;
  48. int i;
  49. /*
  50. * Make sure the function array initialization remains
  51. * correct.
  52. */
  53. BUILD_BUG_ON(SMP_RESCHEDULE_YOURSELF != (1 << 0));
  54. BUILD_BUG_ON(SMP_CALL_FUNCTION != (1 << 1));
  55. BUILD_BUG_ON(SMP_ICACHE_FLUSH != (1 << 2));
  56. /*
  57. * Load the mailbox register to figure out what we're supposed
  58. * to do.
  59. */
  60. action = cvmx_read_csr(mbox_clrx);
  61. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  62. action &= 0xff;
  63. else
  64. action &= 0xffff;
  65. /* Clear the mailbox to clear the interrupt */
  66. cvmx_write_csr(mbox_clrx, action);
  67. for (i = 0; i < ARRAY_SIZE(octeon_message_functions) && action;) {
  68. if (action & 1) {
  69. void (*fn)(void) = octeon_message_functions[i];
  70. if (fn)
  71. fn();
  72. }
  73. action >>= 1;
  74. i++;
  75. }
  76. return IRQ_HANDLED;
  77. }
  78. /*
  79. * Cause the function described by call_data to be executed on the passed
  80. * cpu. When the function has finished, increment the finished field of
  81. * call_data.
  82. */
  83. void octeon_send_ipi_single(int cpu, unsigned int action)
  84. {
  85. int coreid = cpu_logical_map(cpu);
  86. /*
  87. pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
  88. coreid, action);
  89. */
  90. cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
  91. }
  92. static inline void octeon_send_ipi_mask(const struct cpumask *mask,
  93. unsigned int action)
  94. {
  95. unsigned int i;
  96. for_each_cpu(i, mask)
  97. octeon_send_ipi_single(i, action);
  98. }
  99. /*
  100. * Detect available CPUs, populate cpu_possible_mask
  101. */
  102. static void octeon_smp_hotplug_setup(void)
  103. {
  104. #ifdef CONFIG_HOTPLUG_CPU
  105. struct linux_app_boot_info *labi;
  106. if (!setup_max_cpus)
  107. return;
  108. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  109. if (labi->labi_signature != LABI_SIGNATURE) {
  110. pr_info("The bootloader on this board does not support HOTPLUG_CPU.");
  111. return;
  112. }
  113. octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
  114. #endif
  115. }
  116. static void __init octeon_smp_setup(void)
  117. {
  118. const int coreid = cvmx_get_core_num();
  119. int cpus;
  120. int id;
  121. struct cvmx_sysinfo *sysinfo = cvmx_sysinfo_get();
  122. #ifdef CONFIG_HOTPLUG_CPU
  123. int core_mask = octeon_get_boot_coremask();
  124. unsigned int num_cores = cvmx_octeon_num_cores();
  125. #endif
  126. /* The present CPUs are initially just the boot cpu (CPU 0). */
  127. for (id = 0; id < NR_CPUS; id++) {
  128. set_cpu_possible(id, id == 0);
  129. set_cpu_present(id, id == 0);
  130. }
  131. __cpu_number_map[coreid] = 0;
  132. __cpu_logical_map[0] = coreid;
  133. /* The present CPUs get the lowest CPU numbers. */
  134. cpus = 1;
  135. for (id = 0; id < NR_CPUS; id++) {
  136. if ((id != coreid) && cvmx_coremask_is_core_set(&sysinfo->core_mask, id)) {
  137. set_cpu_possible(cpus, true);
  138. set_cpu_present(cpus, true);
  139. __cpu_number_map[id] = cpus;
  140. __cpu_logical_map[cpus] = id;
  141. cpus++;
  142. }
  143. }
  144. #ifdef CONFIG_HOTPLUG_CPU
  145. /*
  146. * The possible CPUs are all those present on the chip. We
  147. * will assign CPU numbers for possible cores as well. Cores
  148. * are always consecutively numberd from 0.
  149. */
  150. for (id = 0; setup_max_cpus && octeon_bootloader_entry_addr &&
  151. id < num_cores && id < NR_CPUS; id++) {
  152. if (!(core_mask & (1 << id))) {
  153. set_cpu_possible(cpus, true);
  154. __cpu_number_map[id] = cpus;
  155. __cpu_logical_map[cpus] = id;
  156. cpus++;
  157. }
  158. }
  159. #endif
  160. octeon_smp_hotplug_setup();
  161. }
  162. #ifdef CONFIG_RELOCATABLE
  163. int plat_post_relocation(long offset)
  164. {
  165. unsigned long entry = (unsigned long)kernel_entry;
  166. /* Send secondaries into relocated kernel */
  167. octeon_processor_relocated_kernel_entry = entry + offset;
  168. return 0;
  169. }
  170. #endif /* CONFIG_RELOCATABLE */
  171. /*
  172. * Firmware CPU startup hook
  173. */
  174. static int octeon_boot_secondary(int cpu, struct task_struct *idle)
  175. {
  176. int count;
  177. pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
  178. cpu_logical_map(cpu));
  179. octeon_processor_sp = __KSTK_TOS(idle);
  180. octeon_processor_gp = (unsigned long)(task_thread_info(idle));
  181. octeon_processor_boot = cpu_logical_map(cpu);
  182. mb();
  183. count = 10000;
  184. while (octeon_processor_sp && count) {
  185. /* Waiting for processor to get the SP and GP */
  186. udelay(1);
  187. count--;
  188. }
  189. if (count == 0) {
  190. pr_err("Secondary boot timeout\n");
  191. return -ETIMEDOUT;
  192. }
  193. return 0;
  194. }
  195. /*
  196. * After we've done initial boot, this function is called to allow the
  197. * board code to clean up state, if needed
  198. */
  199. static void octeon_init_secondary(void)
  200. {
  201. unsigned int sr;
  202. sr = set_c0_status(ST0_BEV);
  203. write_c0_ebase((u32)ebase);
  204. write_c0_status(sr);
  205. octeon_check_cpu_bist();
  206. octeon_init_cvmcount();
  207. octeon_irq_setup_secondary();
  208. }
  209. /*
  210. * Callout to firmware before smp_init
  211. */
  212. static void __init octeon_prepare_cpus(unsigned int max_cpus)
  213. {
  214. /*
  215. * Only the low order mailbox bits are used for IPIs, leave
  216. * the other bits alone.
  217. */
  218. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
  219. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
  220. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
  221. mailbox_interrupt)) {
  222. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
  223. }
  224. }
  225. /*
  226. * Last chance for the board code to finish SMP initialization before
  227. * the CPU is "online".
  228. */
  229. static void octeon_smp_finish(void)
  230. {
  231. octeon_user_io_init();
  232. /* to generate the first CPU timer interrupt */
  233. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  234. local_irq_enable();
  235. }
  236. #ifdef CONFIG_HOTPLUG_CPU
  237. /* State of each CPU. */
  238. static DEFINE_PER_CPU(int, cpu_state);
  239. static int octeon_cpu_disable(void)
  240. {
  241. unsigned int cpu = smp_processor_id();
  242. if (!octeon_bootloader_entry_addr)
  243. return -ENOTSUPP;
  244. set_cpu_online(cpu, false);
  245. calculate_cpu_foreign_map();
  246. octeon_fixup_irqs();
  247. __flush_cache_all();
  248. local_flush_tlb_all();
  249. return 0;
  250. }
  251. static void octeon_cpu_die(unsigned int cpu)
  252. {
  253. int coreid = cpu_logical_map(cpu);
  254. uint32_t mask, new_mask;
  255. const struct cvmx_bootmem_named_block_desc *block_desc;
  256. while (per_cpu(cpu_state, cpu) != CPU_DEAD)
  257. cpu_relax();
  258. /*
  259. * This is a bit complicated strategics of getting/settig available
  260. * cores mask, copied from bootloader
  261. */
  262. mask = 1 << coreid;
  263. /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
  264. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  265. if (!block_desc) {
  266. struct linux_app_boot_info *labi;
  267. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  268. labi->avail_coremask |= mask;
  269. new_mask = labi->avail_coremask;
  270. } else { /* alternative, already initialized */
  271. uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
  272. AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  273. *p |= mask;
  274. new_mask = *p;
  275. }
  276. pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
  277. mb();
  278. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  279. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  280. }
  281. void play_dead(void)
  282. {
  283. int cpu = cpu_number_map(cvmx_get_core_num());
  284. idle_task_exit();
  285. octeon_processor_boot = 0xff;
  286. per_cpu(cpu_state, cpu) = CPU_DEAD;
  287. mb();
  288. while (1) /* core will be reset here */
  289. ;
  290. }
  291. static void start_after_reset(void)
  292. {
  293. kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
  294. }
  295. static int octeon_update_boot_vector(unsigned int cpu)
  296. {
  297. int coreid = cpu_logical_map(cpu);
  298. uint32_t avail_coremask;
  299. const struct cvmx_bootmem_named_block_desc *block_desc;
  300. struct boot_init_vector *boot_vect =
  301. (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
  302. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  303. if (!block_desc) {
  304. struct linux_app_boot_info *labi;
  305. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  306. avail_coremask = labi->avail_coremask;
  307. labi->avail_coremask &= ~(1 << coreid);
  308. } else { /* alternative, already initialized */
  309. avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
  310. block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  311. }
  312. if (!(avail_coremask & (1 << coreid))) {
  313. /* core not available, assume, that caught by simple-executive */
  314. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  315. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  316. }
  317. boot_vect[coreid].app_start_func_addr =
  318. (uint32_t) (unsigned long) start_after_reset;
  319. boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
  320. mb();
  321. cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
  322. return 0;
  323. }
  324. static int register_cavium_notifier(void)
  325. {
  326. return cpuhp_setup_state_nocalls(CPUHP_MIPS_SOC_PREPARE,
  327. "mips/cavium:prepare",
  328. octeon_update_boot_vector, NULL);
  329. }
  330. late_initcall(register_cavium_notifier);
  331. #endif /* CONFIG_HOTPLUG_CPU */
  332. static const struct plat_smp_ops octeon_smp_ops = {
  333. .send_ipi_single = octeon_send_ipi_single,
  334. .send_ipi_mask = octeon_send_ipi_mask,
  335. .init_secondary = octeon_init_secondary,
  336. .smp_finish = octeon_smp_finish,
  337. .boot_secondary = octeon_boot_secondary,
  338. .smp_setup = octeon_smp_setup,
  339. .prepare_cpus = octeon_prepare_cpus,
  340. #ifdef CONFIG_HOTPLUG_CPU
  341. .cpu_disable = octeon_cpu_disable,
  342. .cpu_die = octeon_cpu_die,
  343. #endif
  344. #ifdef CONFIG_KEXEC
  345. .kexec_nonboot_cpu = kexec_nonboot_cpu_jump,
  346. #endif
  347. };
  348. static irqreturn_t octeon_78xx_reched_interrupt(int irq, void *dev_id)
  349. {
  350. scheduler_ipi();
  351. return IRQ_HANDLED;
  352. }
  353. static irqreturn_t octeon_78xx_call_function_interrupt(int irq, void *dev_id)
  354. {
  355. generic_smp_call_function_interrupt();
  356. return IRQ_HANDLED;
  357. }
  358. static irqreturn_t octeon_78xx_icache_flush_interrupt(int irq, void *dev_id)
  359. {
  360. octeon_icache_flush();
  361. return IRQ_HANDLED;
  362. }
  363. /*
  364. * Callout to firmware before smp_init
  365. */
  366. static void octeon_78xx_prepare_cpus(unsigned int max_cpus)
  367. {
  368. if (request_irq(OCTEON_IRQ_MBOX0 + 0,
  369. octeon_78xx_reched_interrupt,
  370. IRQF_PERCPU | IRQF_NO_THREAD, "Scheduler",
  371. octeon_78xx_reched_interrupt)) {
  372. panic("Cannot request_irq for SchedulerIPI");
  373. }
  374. if (request_irq(OCTEON_IRQ_MBOX0 + 1,
  375. octeon_78xx_call_function_interrupt,
  376. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-Call",
  377. octeon_78xx_call_function_interrupt)) {
  378. panic("Cannot request_irq for SMP-Call");
  379. }
  380. if (request_irq(OCTEON_IRQ_MBOX0 + 2,
  381. octeon_78xx_icache_flush_interrupt,
  382. IRQF_PERCPU | IRQF_NO_THREAD, "ICache-Flush",
  383. octeon_78xx_icache_flush_interrupt)) {
  384. panic("Cannot request_irq for ICache-Flush");
  385. }
  386. }
  387. static void octeon_78xx_send_ipi_single(int cpu, unsigned int action)
  388. {
  389. int i;
  390. for (i = 0; i < 8; i++) {
  391. if (action & 1)
  392. octeon_ciu3_mbox_send(cpu, i);
  393. action >>= 1;
  394. }
  395. }
  396. static void octeon_78xx_send_ipi_mask(const struct cpumask *mask,
  397. unsigned int action)
  398. {
  399. unsigned int cpu;
  400. for_each_cpu(cpu, mask)
  401. octeon_78xx_send_ipi_single(cpu, action);
  402. }
  403. static const struct plat_smp_ops octeon_78xx_smp_ops = {
  404. .send_ipi_single = octeon_78xx_send_ipi_single,
  405. .send_ipi_mask = octeon_78xx_send_ipi_mask,
  406. .init_secondary = octeon_init_secondary,
  407. .smp_finish = octeon_smp_finish,
  408. .boot_secondary = octeon_boot_secondary,
  409. .smp_setup = octeon_smp_setup,
  410. .prepare_cpus = octeon_78xx_prepare_cpus,
  411. #ifdef CONFIG_HOTPLUG_CPU
  412. .cpu_disable = octeon_cpu_disable,
  413. .cpu_die = octeon_cpu_die,
  414. #endif
  415. #ifdef CONFIG_KEXEC
  416. .kexec_nonboot_cpu = kexec_nonboot_cpu_jump,
  417. #endif
  418. };
  419. void __init octeon_setup_smp(void)
  420. {
  421. const struct plat_smp_ops *ops;
  422. if (octeon_has_feature(OCTEON_FEATURE_CIU3))
  423. ops = &octeon_78xx_smp_ops;
  424. else
  425. ops = &octeon_smp_ops;
  426. register_smp_ops(ops);
  427. }