smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 __cpuinit octeon_init_secondary(void)
  148. {
  149. unsigned int sr;
  150. sr = set_c0_status(ST0_BEV);
  151. write_c0_ebase((u32)ebase);
  152. write_c0_status(sr);
  153. octeon_check_cpu_bist();
  154. octeon_init_cvmcount();
  155. octeon_irq_setup_secondary();
  156. raw_local_irq_enable();
  157. }
  158. /**
  159. * Callout to firmware before smp_init
  160. *
  161. */
  162. void octeon_prepare_cpus(unsigned int max_cpus)
  163. {
  164. #ifdef CONFIG_HOTPLUG_CPU
  165. struct linux_app_boot_info *labi;
  166. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  167. if (labi->labi_signature != LABI_SIGNATURE)
  168. panic("The bootloader version on this board is incorrect.");
  169. #endif
  170. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffffffff);
  171. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt, IRQF_DISABLED,
  172. "mailbox0", mailbox_interrupt)) {
  173. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)\n");
  174. }
  175. if (request_irq(OCTEON_IRQ_MBOX1, mailbox_interrupt, IRQF_DISABLED,
  176. "mailbox1", mailbox_interrupt)) {
  177. panic("Cannot request_irq(OCTEON_IRQ_MBOX1)\n");
  178. }
  179. }
  180. /**
  181. * Last chance for the board code to finish SMP initialization before
  182. * the CPU is "online".
  183. */
  184. static void octeon_smp_finish(void)
  185. {
  186. #ifdef CONFIG_CAVIUM_GDB
  187. unsigned long tmp;
  188. /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
  189. to be not masked by this core so we know the signal is received by
  190. someone */
  191. asm volatile ("dmfc0 %0, $22\n"
  192. "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
  193. #endif
  194. octeon_user_io_init();
  195. /* to generate the first CPU timer interrupt */
  196. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  197. }
  198. /**
  199. * Hook for after all CPUs are online
  200. */
  201. static void octeon_cpus_done(void)
  202. {
  203. #ifdef CONFIG_CAVIUM_GDB
  204. unsigned long tmp;
  205. /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
  206. to be not masked by this core so we know the signal is received by
  207. someone */
  208. asm volatile ("dmfc0 %0, $22\n"
  209. "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
  210. #endif
  211. }
  212. #ifdef CONFIG_HOTPLUG_CPU
  213. /* State of each CPU. */
  214. DEFINE_PER_CPU(int, cpu_state);
  215. extern void fixup_irqs(void);
  216. static DEFINE_SPINLOCK(smp_reserve_lock);
  217. static int octeon_cpu_disable(void)
  218. {
  219. unsigned int cpu = smp_processor_id();
  220. if (cpu == 0)
  221. return -EBUSY;
  222. spin_lock(&smp_reserve_lock);
  223. cpu_clear(cpu, cpu_online_map);
  224. cpu_clear(cpu, cpu_callin_map);
  225. local_irq_disable();
  226. fixup_irqs();
  227. local_irq_enable();
  228. flush_cache_all();
  229. local_flush_tlb_all();
  230. spin_unlock(&smp_reserve_lock);
  231. return 0;
  232. }
  233. static void octeon_cpu_die(unsigned int cpu)
  234. {
  235. int coreid = cpu_logical_map(cpu);
  236. uint32_t mask, new_mask;
  237. const struct cvmx_bootmem_named_block_desc *block_desc;
  238. while (per_cpu(cpu_state, cpu) != CPU_DEAD)
  239. cpu_relax();
  240. /*
  241. * This is a bit complicated strategics of getting/settig available
  242. * cores mask, copied from bootloader
  243. */
  244. mask = 1 << coreid;
  245. /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
  246. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  247. if (!block_desc) {
  248. struct linux_app_boot_info *labi;
  249. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  250. labi->avail_coremask |= mask;
  251. new_mask = labi->avail_coremask;
  252. } else { /* alternative, already initialized */
  253. uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
  254. AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  255. *p |= mask;
  256. new_mask = *p;
  257. }
  258. pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
  259. mb();
  260. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  261. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  262. }
  263. void play_dead(void)
  264. {
  265. int cpu = cpu_number_map(cvmx_get_core_num());
  266. idle_task_exit();
  267. octeon_processor_boot = 0xff;
  268. per_cpu(cpu_state, cpu) = CPU_DEAD;
  269. mb();
  270. while (1) /* core will be reset here */
  271. ;
  272. }
  273. extern void kernel_entry(unsigned long arg1, ...);
  274. static void start_after_reset(void)
  275. {
  276. kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
  277. }
  278. static int octeon_update_boot_vector(unsigned int cpu)
  279. {
  280. int coreid = cpu_logical_map(cpu);
  281. uint32_t avail_coremask;
  282. const struct cvmx_bootmem_named_block_desc *block_desc;
  283. struct boot_init_vector *boot_vect =
  284. (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
  285. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  286. if (!block_desc) {
  287. struct linux_app_boot_info *labi;
  288. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  289. avail_coremask = labi->avail_coremask;
  290. labi->avail_coremask &= ~(1 << coreid);
  291. } else { /* alternative, already initialized */
  292. avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
  293. block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  294. }
  295. if (!(avail_coremask & (1 << coreid))) {
  296. /* core not available, assume, that catched by simple-executive */
  297. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  298. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  299. }
  300. boot_vect[coreid].app_start_func_addr =
  301. (uint32_t) (unsigned long) start_after_reset;
  302. boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
  303. mb();
  304. cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
  305. return 0;
  306. }
  307. static int __cpuinit octeon_cpu_callback(struct notifier_block *nfb,
  308. unsigned long action, void *hcpu)
  309. {
  310. unsigned int cpu = (unsigned long)hcpu;
  311. switch (action) {
  312. case CPU_UP_PREPARE:
  313. octeon_update_boot_vector(cpu);
  314. break;
  315. case CPU_ONLINE:
  316. pr_info("Cpu %d online\n", cpu);
  317. break;
  318. case CPU_DEAD:
  319. break;
  320. }
  321. return NOTIFY_OK;
  322. }
  323. static int __cpuinit register_cavium_notifier(void)
  324. {
  325. hotcpu_notifier(octeon_cpu_callback, 0);
  326. return 0;
  327. }
  328. late_initcall(register_cavium_notifier);
  329. #endif /* CONFIG_HOTPLUG_CPU */
  330. struct plat_smp_ops octeon_smp_ops = {
  331. .send_ipi_single = octeon_send_ipi_single,
  332. .send_ipi_mask = octeon_send_ipi_mask,
  333. .init_secondary = octeon_init_secondary,
  334. .smp_finish = octeon_smp_finish,
  335. .cpus_done = octeon_cpus_done,
  336. .boot_secondary = octeon_boot_secondary,
  337. .smp_setup = octeon_smp_setup,
  338. .prepare_cpus = octeon_prepare_cpus,
  339. #ifdef CONFIG_HOTPLUG_CPU
  340. .cpu_disable = octeon_cpu_disable,
  341. .cpu_die = octeon_cpu_die,
  342. #endif
  343. };