smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/init.h>
  10. #include <linux/delay.h>
  11. #include <linux/smp.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/sched.h>
  15. #include <linux/module.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/system.h>
  18. #include <asm/time.h>
  19. #include <asm/octeon/octeon.h>
  20. #include "octeon_boot.h"
  21. volatile unsigned long octeon_processor_boot = 0xff;
  22. volatile unsigned long octeon_processor_sp;
  23. volatile unsigned long octeon_processor_gp;
  24. #ifdef CONFIG_HOTPLUG_CPU
  25. uint64_t octeon_bootloader_entry_addr;
  26. EXPORT_SYMBOL(octeon_bootloader_entry_addr);
  27. #endif
  28. static irqreturn_t mailbox_interrupt(int irq, void *dev_id)
  29. {
  30. const int coreid = cvmx_get_core_num();
  31. uint64_t action;
  32. /* Load the mailbox register to figure out what we're supposed to do */
  33. action = cvmx_read_csr(CVMX_CIU_MBOX_CLRX(coreid));
  34. /* Clear the mailbox to clear the interrupt */
  35. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(coreid), action);
  36. if (action & SMP_CALL_FUNCTION)
  37. smp_call_function_interrupt();
  38. /* Check if we've been told to flush the icache */
  39. if (action & SMP_ICACHE_FLUSH)
  40. asm volatile ("synci 0($0)\n");
  41. return IRQ_HANDLED;
  42. }
  43. /**
  44. * Cause the function described by call_data to be executed on the passed
  45. * cpu. When the function has finished, increment the finished field of
  46. * call_data.
  47. */
  48. void octeon_send_ipi_single(int cpu, unsigned int action)
  49. {
  50. int coreid = cpu_logical_map(cpu);
  51. /*
  52. pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
  53. coreid, action);
  54. */
  55. cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
  56. }
  57. static inline void octeon_send_ipi_mask(const struct cpumask *mask,
  58. unsigned int action)
  59. {
  60. unsigned int i;
  61. for_each_cpu_mask(i, *mask)
  62. octeon_send_ipi_single(i, action);
  63. }
  64. /**
  65. * Detect available CPUs, populate cpu_possible_map
  66. */
  67. static void octeon_smp_hotplug_setup(void)
  68. {
  69. #ifdef CONFIG_HOTPLUG_CPU
  70. struct linux_app_boot_info *labi;
  71. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  72. if (labi->labi_signature != LABI_SIGNATURE)
  73. panic("The bootloader version on this board is incorrect.");
  74. octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
  75. #endif
  76. }
  77. static void octeon_smp_setup(void)
  78. {
  79. const int coreid = cvmx_get_core_num();
  80. int cpus;
  81. int id;
  82. int core_mask = octeon_get_boot_coremask();
  83. #ifdef CONFIG_HOTPLUG_CPU
  84. unsigned int num_cores = cvmx_octeon_num_cores();
  85. #endif
  86. /* The present CPUs are initially just the boot cpu (CPU 0). */
  87. for (id = 0; id < NR_CPUS; id++) {
  88. set_cpu_possible(id, id == 0);
  89. set_cpu_present(id, id == 0);
  90. }
  91. __cpu_number_map[coreid] = 0;
  92. __cpu_logical_map[0] = coreid;
  93. /* The present CPUs get the lowest CPU numbers. */
  94. cpus = 1;
  95. for (id = 0; id < NR_CPUS; id++) {
  96. if ((id != coreid) && (core_mask & (1 << id))) {
  97. set_cpu_possible(cpus, true);
  98. set_cpu_present(cpus, true);
  99. __cpu_number_map[id] = cpus;
  100. __cpu_logical_map[cpus] = id;
  101. cpus++;
  102. }
  103. }
  104. #ifdef CONFIG_HOTPLUG_CPU
  105. /*
  106. * The possible CPUs are all those present on the chip. We
  107. * will assign CPU numbers for possible cores as well. Cores
  108. * are always consecutively numberd from 0.
  109. */
  110. for (id = 0; id < num_cores && id < NR_CPUS; id++) {
  111. if (!(core_mask & (1 << id))) {
  112. set_cpu_possible(cpus, true);
  113. __cpu_number_map[id] = cpus;
  114. __cpu_logical_map[cpus] = id;
  115. cpus++;
  116. }
  117. }
  118. #endif
  119. octeon_smp_hotplug_setup();
  120. }
  121. /**
  122. * Firmware CPU startup hook
  123. *
  124. */
  125. static void octeon_boot_secondary(int cpu, struct task_struct *idle)
  126. {
  127. int count;
  128. pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
  129. cpu_logical_map(cpu));
  130. octeon_processor_sp = __KSTK_TOS(idle);
  131. octeon_processor_gp = (unsigned long)(task_thread_info(idle));
  132. octeon_processor_boot = cpu_logical_map(cpu);
  133. mb();
  134. count = 10000;
  135. while (octeon_processor_sp && count) {
  136. /* Waiting for processor to get the SP and GP */
  137. udelay(1);
  138. count--;
  139. }
  140. if (count == 0)
  141. pr_err("Secondary boot timeout\n");
  142. }
  143. /**
  144. * After we've done initial boot, this function is called to allow the
  145. * board code to clean up state, if needed
  146. */
  147. static void octeon_init_secondary(void)
  148. {
  149. const int coreid = cvmx_get_core_num();
  150. union cvmx_ciu_intx_sum0 interrupt_enable;
  151. unsigned int sr;
  152. #ifdef CONFIG_HOTPLUG_CPU
  153. struct linux_app_boot_info *labi;
  154. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  155. if (labi->labi_signature != LABI_SIGNATURE)
  156. panic("The bootloader version on this board is incorrect.");
  157. #endif
  158. sr = set_c0_status(ST0_BEV);
  159. write_c0_ebase((u32)ebase);
  160. write_c0_status(sr);
  161. octeon_check_cpu_bist();
  162. octeon_init_cvmcount();
  163. /*
  164. pr_info("SMP: CPU%d (CoreId %lu) started\n", cpu, coreid);
  165. */
  166. /* Enable Mailbox interrupts to this core. These are the only
  167. interrupts allowed on line 3 */
  168. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(coreid), 0xffffffff);
  169. interrupt_enable.u64 = 0;
  170. interrupt_enable.s.mbox = 0x3;
  171. cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid * 2)), interrupt_enable.u64);
  172. cvmx_write_csr(CVMX_CIU_INTX_EN0((coreid * 2 + 1)), 0);
  173. cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid * 2)), 0);
  174. cvmx_write_csr(CVMX_CIU_INTX_EN1((coreid * 2 + 1)), 0);
  175. /* Enable core interrupt processing for 2,3 and 7 */
  176. set_c0_status(0x8c01);
  177. }
  178. /**
  179. * Callout to firmware before smp_init
  180. *
  181. */
  182. void octeon_prepare_cpus(unsigned int max_cpus)
  183. {
  184. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffffffff);
  185. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt, IRQF_DISABLED,
  186. "mailbox0", mailbox_interrupt)) {
  187. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)\n");
  188. }
  189. if (request_irq(OCTEON_IRQ_MBOX1, mailbox_interrupt, IRQF_DISABLED,
  190. "mailbox1", mailbox_interrupt)) {
  191. panic("Cannot request_irq(OCTEON_IRQ_MBOX1)\n");
  192. }
  193. }
  194. /**
  195. * Last chance for the board code to finish SMP initialization before
  196. * the CPU is "online".
  197. */
  198. static void octeon_smp_finish(void)
  199. {
  200. #ifdef CONFIG_CAVIUM_GDB
  201. unsigned long tmp;
  202. /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
  203. to be not masked by this core so we know the signal is received by
  204. someone */
  205. asm volatile ("dmfc0 %0, $22\n"
  206. "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
  207. #endif
  208. octeon_user_io_init();
  209. /* to generate the first CPU timer interrupt */
  210. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  211. }
  212. /**
  213. * Hook for after all CPUs are online
  214. */
  215. static void octeon_cpus_done(void)
  216. {
  217. #ifdef CONFIG_CAVIUM_GDB
  218. unsigned long tmp;
  219. /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
  220. to be not masked by this core so we know the signal is received by
  221. someone */
  222. asm volatile ("dmfc0 %0, $22\n"
  223. "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
  224. #endif
  225. }
  226. #ifdef CONFIG_HOTPLUG_CPU
  227. /* State of each CPU. */
  228. DEFINE_PER_CPU(int, cpu_state);
  229. extern void fixup_irqs(void);
  230. static DEFINE_SPINLOCK(smp_reserve_lock);
  231. static int octeon_cpu_disable(void)
  232. {
  233. unsigned int cpu = smp_processor_id();
  234. if (cpu == 0)
  235. return -EBUSY;
  236. spin_lock(&smp_reserve_lock);
  237. cpu_clear(cpu, cpu_online_map);
  238. cpu_clear(cpu, cpu_callin_map);
  239. local_irq_disable();
  240. fixup_irqs();
  241. local_irq_enable();
  242. flush_cache_all();
  243. local_flush_tlb_all();
  244. spin_unlock(&smp_reserve_lock);
  245. return 0;
  246. }
  247. static void octeon_cpu_die(unsigned int cpu)
  248. {
  249. int coreid = cpu_logical_map(cpu);
  250. uint32_t mask, new_mask;
  251. const struct cvmx_bootmem_named_block_desc *block_desc;
  252. while (per_cpu(cpu_state, cpu) != CPU_DEAD)
  253. cpu_relax();
  254. /*
  255. * This is a bit complicated strategics of getting/settig available
  256. * cores mask, copied from bootloader
  257. */
  258. mask = 1 << coreid;
  259. /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
  260. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  261. if (!block_desc) {
  262. struct linux_app_boot_info *labi;
  263. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  264. labi->avail_coremask |= mask;
  265. new_mask = labi->avail_coremask;
  266. } else { /* alternative, already initialized */
  267. uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
  268. AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  269. *p |= mask;
  270. new_mask = *p;
  271. }
  272. pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
  273. mb();
  274. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  275. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  276. }
  277. void play_dead(void)
  278. {
  279. int cpu = cpu_number_map(cvmx_get_core_num());
  280. idle_task_exit();
  281. octeon_processor_boot = 0xff;
  282. per_cpu(cpu_state, cpu) = CPU_DEAD;
  283. mb();
  284. while (1) /* core will be reset here */
  285. ;
  286. }
  287. extern void kernel_entry(unsigned long arg1, ...);
  288. static void start_after_reset(void)
  289. {
  290. kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
  291. }
  292. static int octeon_update_boot_vector(unsigned int cpu)
  293. {
  294. int coreid = cpu_logical_map(cpu);
  295. uint32_t avail_coremask;
  296. const struct cvmx_bootmem_named_block_desc *block_desc;
  297. struct boot_init_vector *boot_vect =
  298. (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
  299. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  300. if (!block_desc) {
  301. struct linux_app_boot_info *labi;
  302. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  303. avail_coremask = labi->avail_coremask;
  304. labi->avail_coremask &= ~(1 << coreid);
  305. } else { /* alternative, already initialized */
  306. avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
  307. block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  308. }
  309. if (!(avail_coremask & (1 << coreid))) {
  310. /* core not available, assume, that catched by simple-executive */
  311. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  312. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  313. }
  314. boot_vect[coreid].app_start_func_addr =
  315. (uint32_t) (unsigned long) start_after_reset;
  316. boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
  317. mb();
  318. cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
  319. return 0;
  320. }
  321. static int __cpuinit octeon_cpu_callback(struct notifier_block *nfb,
  322. unsigned long action, void *hcpu)
  323. {
  324. unsigned int cpu = (unsigned long)hcpu;
  325. switch (action) {
  326. case CPU_UP_PREPARE:
  327. octeon_update_boot_vector(cpu);
  328. break;
  329. case CPU_ONLINE:
  330. pr_info("Cpu %d online\n", cpu);
  331. break;
  332. case CPU_DEAD:
  333. break;
  334. }
  335. return NOTIFY_OK;
  336. }
  337. static int __cpuinit register_cavium_notifier(void)
  338. {
  339. hotcpu_notifier(octeon_cpu_callback, 0);
  340. return 0;
  341. }
  342. late_initcall(register_cavium_notifier);
  343. #endif /* CONFIG_HOTPLUG_CPU */
  344. struct plat_smp_ops octeon_smp_ops = {
  345. .send_ipi_single = octeon_send_ipi_single,
  346. .send_ipi_mask = octeon_send_ipi_mask,
  347. .init_secondary = octeon_init_secondary,
  348. .smp_finish = octeon_smp_finish,
  349. .cpus_done = octeon_cpus_done,
  350. .boot_secondary = octeon_boot_secondary,
  351. .smp_setup = octeon_smp_setup,
  352. .prepare_cpus = octeon_prepare_cpus,
  353. #ifdef CONFIG_HOTPLUG_CPU
  354. .cpu_disable = octeon_cpu_disable,
  355. .cpu_die = octeon_cpu_die,
  356. #endif
  357. };