smp.c 12 KB

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