smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <asm/mmu_context.h>
  19. #include <asm/time.h>
  20. #include <asm/setup.h>
  21. #include <asm/octeon/octeon.h>
  22. #include "octeon_boot.h"
  23. volatile unsigned long octeon_processor_boot = 0xff;
  24. volatile unsigned long octeon_processor_sp;
  25. volatile unsigned long octeon_processor_gp;
  26. #ifdef CONFIG_RELOCATABLE
  27. volatile unsigned long octeon_processor_relocated_kernel_entry;
  28. #endif /* CONFIG_RELOCATABLE */
  29. #ifdef CONFIG_HOTPLUG_CPU
  30. uint64_t octeon_bootloader_entry_addr;
  31. EXPORT_SYMBOL(octeon_bootloader_entry_addr);
  32. #endif
  33. extern void kernel_entry(unsigned long arg1, ...);
  34. static void octeon_icache_flush(void)
  35. {
  36. asm volatile ("synci 0($0)\n");
  37. }
  38. static void (*octeon_message_functions[8])(void) = {
  39. scheduler_ipi,
  40. generic_smp_call_function_interrupt,
  41. octeon_icache_flush,
  42. };
  43. static irqreturn_t mailbox_interrupt(int irq, void *dev_id)
  44. {
  45. u64 mbox_clrx = CVMX_CIU_MBOX_CLRX(cvmx_get_core_num());
  46. u64 action;
  47. int i;
  48. /*
  49. * Make sure the function array initialization remains
  50. * correct.
  51. */
  52. BUILD_BUG_ON(SMP_RESCHEDULE_YOURSELF != (1 << 0));
  53. BUILD_BUG_ON(SMP_CALL_FUNCTION != (1 << 1));
  54. BUILD_BUG_ON(SMP_ICACHE_FLUSH != (1 << 2));
  55. /*
  56. * Load the mailbox register to figure out what we're supposed
  57. * to do.
  58. */
  59. action = cvmx_read_csr(mbox_clrx);
  60. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  61. action &= 0xff;
  62. else
  63. action &= 0xffff;
  64. /* Clear the mailbox to clear the interrupt */
  65. cvmx_write_csr(mbox_clrx, action);
  66. for (i = 0; i < ARRAY_SIZE(octeon_message_functions) && action;) {
  67. if (action & 1) {
  68. void (*fn)(void) = octeon_message_functions[i];
  69. if (fn)
  70. fn();
  71. }
  72. action >>= 1;
  73. i++;
  74. }
  75. return IRQ_HANDLED;
  76. }
  77. /**
  78. * Cause the function described by call_data to be executed on the passed
  79. * cpu. When the function has finished, increment the finished field of
  80. * call_data.
  81. */
  82. void octeon_send_ipi_single(int cpu, unsigned int action)
  83. {
  84. int coreid = cpu_logical_map(cpu);
  85. /*
  86. pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
  87. coreid, action);
  88. */
  89. cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
  90. }
  91. static inline void octeon_send_ipi_mask(const struct cpumask *mask,
  92. unsigned int action)
  93. {
  94. unsigned int i;
  95. for_each_cpu(i, mask)
  96. octeon_send_ipi_single(i, action);
  97. }
  98. /**
  99. * Detect available CPUs, populate cpu_possible_mask
  100. */
  101. static void octeon_smp_hotplug_setup(void)
  102. {
  103. #ifdef CONFIG_HOTPLUG_CPU
  104. struct linux_app_boot_info *labi;
  105. if (!setup_max_cpus)
  106. return;
  107. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  108. if (labi->labi_signature != LABI_SIGNATURE) {
  109. pr_info("The bootloader on this board does not support HOTPLUG_CPU.");
  110. return;
  111. }
  112. octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
  113. #endif
  114. }
  115. static void __init octeon_smp_setup(void)
  116. {
  117. const int coreid = cvmx_get_core_num();
  118. int cpus;
  119. int id;
  120. struct cvmx_sysinfo *sysinfo = cvmx_sysinfo_get();
  121. #ifdef CONFIG_HOTPLUG_CPU
  122. int core_mask = octeon_get_boot_coremask();
  123. unsigned int num_cores = cvmx_octeon_num_cores();
  124. #endif
  125. /* The present CPUs are initially just the boot cpu (CPU 0). */
  126. for (id = 0; id < NR_CPUS; id++) {
  127. set_cpu_possible(id, id == 0);
  128. set_cpu_present(id, id == 0);
  129. }
  130. __cpu_number_map[coreid] = 0;
  131. __cpu_logical_map[0] = coreid;
  132. /* The present CPUs get the lowest CPU numbers. */
  133. cpus = 1;
  134. for (id = 0; id < NR_CPUS; id++) {
  135. if ((id != coreid) && cvmx_coremask_is_core_set(&sysinfo->core_mask, id)) {
  136. set_cpu_possible(cpus, true);
  137. set_cpu_present(cpus, true);
  138. __cpu_number_map[id] = cpus;
  139. __cpu_logical_map[cpus] = id;
  140. cpus++;
  141. }
  142. }
  143. #ifdef CONFIG_HOTPLUG_CPU
  144. /*
  145. * The possible CPUs are all those present on the chip. We
  146. * will assign CPU numbers for possible cores as well. Cores
  147. * are always consecutively numberd from 0.
  148. */
  149. for (id = 0; setup_max_cpus && octeon_bootloader_entry_addr &&
  150. id < num_cores && id < NR_CPUS; id++) {
  151. if (!(core_mask & (1 << id))) {
  152. set_cpu_possible(cpus, true);
  153. __cpu_number_map[id] = cpus;
  154. __cpu_logical_map[cpus] = id;
  155. cpus++;
  156. }
  157. }
  158. #endif
  159. octeon_smp_hotplug_setup();
  160. }
  161. #ifdef CONFIG_RELOCATABLE
  162. int plat_post_relocation(long offset)
  163. {
  164. unsigned long entry = (unsigned long)kernel_entry;
  165. /* Send secondaries into relocated kernel */
  166. octeon_processor_relocated_kernel_entry = entry + offset;
  167. return 0;
  168. }
  169. #endif /* CONFIG_RELOCATABLE */
  170. /**
  171. * Firmware CPU startup hook
  172. *
  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. */
  213. static void __init octeon_prepare_cpus(unsigned int max_cpus)
  214. {
  215. /*
  216. * Only the low order mailbox bits are used for IPIs, leave
  217. * the other bits alone.
  218. */
  219. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
  220. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
  221. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
  222. mailbox_interrupt)) {
  223. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
  224. }
  225. }
  226. /**
  227. * Last chance for the board code to finish SMP initialization before
  228. * the CPU is "online".
  229. */
  230. static void octeon_smp_finish(void)
  231. {
  232. octeon_user_io_init();
  233. /* to generate the first CPU timer interrupt */
  234. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  235. local_irq_enable();
  236. }
  237. #ifdef CONFIG_HOTPLUG_CPU
  238. /* State of each CPU. */
  239. DEFINE_PER_CPU(int, cpu_state);
  240. static int octeon_cpu_disable(void)
  241. {
  242. unsigned int cpu = smp_processor_id();
  243. if (cpu == 0)
  244. return -EBUSY;
  245. if (!octeon_bootloader_entry_addr)
  246. return -ENOTSUPP;
  247. set_cpu_online(cpu, false);
  248. calculate_cpu_foreign_map();
  249. octeon_fixup_irqs();
  250. __flush_cache_all();
  251. local_flush_tlb_all();
  252. return 0;
  253. }
  254. static void octeon_cpu_die(unsigned int cpu)
  255. {
  256. int coreid = cpu_logical_map(cpu);
  257. uint32_t mask, new_mask;
  258. const struct cvmx_bootmem_named_block_desc *block_desc;
  259. while (per_cpu(cpu_state, cpu) != CPU_DEAD)
  260. cpu_relax();
  261. /*
  262. * This is a bit complicated strategics of getting/settig available
  263. * cores mask, copied from bootloader
  264. */
  265. mask = 1 << coreid;
  266. /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
  267. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  268. if (!block_desc) {
  269. struct linux_app_boot_info *labi;
  270. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  271. labi->avail_coremask |= mask;
  272. new_mask = labi->avail_coremask;
  273. } else { /* alternative, already initialized */
  274. uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
  275. AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  276. *p |= mask;
  277. new_mask = *p;
  278. }
  279. pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
  280. mb();
  281. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  282. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  283. }
  284. void play_dead(void)
  285. {
  286. int cpu = cpu_number_map(cvmx_get_core_num());
  287. idle_task_exit();
  288. octeon_processor_boot = 0xff;
  289. per_cpu(cpu_state, cpu) = CPU_DEAD;
  290. mb();
  291. while (1) /* core will be reset here */
  292. ;
  293. }
  294. static void start_after_reset(void)
  295. {
  296. kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
  297. }
  298. static int octeon_update_boot_vector(unsigned int cpu)
  299. {
  300. int coreid = cpu_logical_map(cpu);
  301. uint32_t avail_coremask;
  302. const struct cvmx_bootmem_named_block_desc *block_desc;
  303. struct boot_init_vector *boot_vect =
  304. (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
  305. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  306. if (!block_desc) {
  307. struct linux_app_boot_info *labi;
  308. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  309. avail_coremask = labi->avail_coremask;
  310. labi->avail_coremask &= ~(1 << coreid);
  311. } else { /* alternative, already initialized */
  312. avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
  313. block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  314. }
  315. if (!(avail_coremask & (1 << coreid))) {
  316. /* core not available, assume, that caught by simple-executive */
  317. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  318. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  319. }
  320. boot_vect[coreid].app_start_func_addr =
  321. (uint32_t) (unsigned long) start_after_reset;
  322. boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
  323. mb();
  324. cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
  325. return 0;
  326. }
  327. static int register_cavium_notifier(void)
  328. {
  329. return cpuhp_setup_state_nocalls(CPUHP_MIPS_SOC_PREPARE,
  330. "mips/cavium:prepare",
  331. octeon_update_boot_vector, NULL);
  332. }
  333. late_initcall(register_cavium_notifier);
  334. #endif /* CONFIG_HOTPLUG_CPU */
  335. const struct plat_smp_ops octeon_smp_ops = {
  336. .send_ipi_single = octeon_send_ipi_single,
  337. .send_ipi_mask = octeon_send_ipi_mask,
  338. .init_secondary = octeon_init_secondary,
  339. .smp_finish = octeon_smp_finish,
  340. .boot_secondary = octeon_boot_secondary,
  341. .smp_setup = octeon_smp_setup,
  342. .prepare_cpus = octeon_prepare_cpus,
  343. #ifdef CONFIG_HOTPLUG_CPU
  344. .cpu_disable = octeon_cpu_disable,
  345. .cpu_die = octeon_cpu_die,
  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. };
  416. void __init octeon_setup_smp(void)
  417. {
  418. const struct plat_smp_ops *ops;
  419. if (octeon_has_feature(OCTEON_FEATURE_CIU3))
  420. ops = &octeon_78xx_smp_ops;
  421. else
  422. ops = &octeon_smp_ops;
  423. register_smp_ops(ops);
  424. }