smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/time.h>
  18. #include <asm/setup.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)) & 0xffff;
  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. if (action & SMP_RESCHEDULE_YOURSELF)
  39. scheduler_ipi();
  40. /* Check if we've been told to flush the icache */
  41. if (action & SMP_ICACHE_FLUSH)
  42. asm volatile ("synci 0($0)\n");
  43. return IRQ_HANDLED;
  44. }
  45. /**
  46. * Cause the function described by call_data to be executed on the passed
  47. * cpu. When the function has finished, increment the finished field of
  48. * call_data.
  49. */
  50. void octeon_send_ipi_single(int cpu, unsigned int action)
  51. {
  52. int coreid = cpu_logical_map(cpu);
  53. /*
  54. pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
  55. coreid, action);
  56. */
  57. cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
  58. }
  59. static inline void octeon_send_ipi_mask(const struct cpumask *mask,
  60. unsigned int action)
  61. {
  62. unsigned int i;
  63. for_each_cpu_mask(i, *mask)
  64. octeon_send_ipi_single(i, action);
  65. }
  66. /**
  67. * Detect available CPUs, populate cpu_possible_mask
  68. */
  69. static void octeon_smp_hotplug_setup(void)
  70. {
  71. #ifdef CONFIG_HOTPLUG_CPU
  72. struct linux_app_boot_info *labi;
  73. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  74. if (labi->labi_signature != LABI_SIGNATURE)
  75. panic("The bootloader version on this board is incorrect.");
  76. octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
  77. #endif
  78. }
  79. static void octeon_smp_setup(void)
  80. {
  81. const int coreid = cvmx_get_core_num();
  82. int cpus;
  83. int id;
  84. int core_mask = octeon_get_boot_coremask();
  85. #ifdef CONFIG_HOTPLUG_CPU
  86. unsigned int num_cores = cvmx_octeon_num_cores();
  87. #endif
  88. /* The present CPUs are initially just the boot cpu (CPU 0). */
  89. for (id = 0; id < NR_CPUS; id++) {
  90. set_cpu_possible(id, id == 0);
  91. set_cpu_present(id, id == 0);
  92. }
  93. __cpu_number_map[coreid] = 0;
  94. __cpu_logical_map[0] = coreid;
  95. /* The present CPUs get the lowest CPU numbers. */
  96. cpus = 1;
  97. for (id = 0; id < NR_CPUS; id++) {
  98. if ((id != coreid) && (core_mask & (1 << id))) {
  99. set_cpu_possible(cpus, true);
  100. set_cpu_present(cpus, true);
  101. __cpu_number_map[id] = cpus;
  102. __cpu_logical_map[cpus] = id;
  103. cpus++;
  104. }
  105. }
  106. #ifdef CONFIG_HOTPLUG_CPU
  107. /*
  108. * The possible CPUs are all those present on the chip. We
  109. * will assign CPU numbers for possible cores as well. Cores
  110. * are always consecutively numberd from 0.
  111. */
  112. for (id = 0; id < num_cores && id < NR_CPUS; id++) {
  113. if (!(core_mask & (1 << id))) {
  114. set_cpu_possible(cpus, true);
  115. __cpu_number_map[id] = cpus;
  116. __cpu_logical_map[cpus] = id;
  117. cpus++;
  118. }
  119. }
  120. #endif
  121. octeon_smp_hotplug_setup();
  122. }
  123. /**
  124. * Firmware CPU startup hook
  125. *
  126. */
  127. static void octeon_boot_secondary(int cpu, struct task_struct *idle)
  128. {
  129. int count;
  130. pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
  131. cpu_logical_map(cpu));
  132. octeon_processor_sp = __KSTK_TOS(idle);
  133. octeon_processor_gp = (unsigned long)(task_thread_info(idle));
  134. octeon_processor_boot = cpu_logical_map(cpu);
  135. mb();
  136. count = 10000;
  137. while (octeon_processor_sp && count) {
  138. /* Waiting for processor to get the SP and GP */
  139. udelay(1);
  140. count--;
  141. }
  142. if (count == 0)
  143. pr_err("Secondary boot timeout\n");
  144. }
  145. /**
  146. * After we've done initial boot, this function is called to allow the
  147. * board code to clean up state, if needed
  148. */
  149. static void __cpuinit octeon_init_secondary(void)
  150. {
  151. unsigned int sr;
  152. sr = set_c0_status(ST0_BEV);
  153. write_c0_ebase((u32)ebase);
  154. write_c0_status(sr);
  155. octeon_check_cpu_bist();
  156. octeon_init_cvmcount();
  157. octeon_irq_setup_secondary();
  158. raw_local_irq_enable();
  159. }
  160. /**
  161. * Callout to firmware before smp_init
  162. *
  163. */
  164. void octeon_prepare_cpus(unsigned int max_cpus)
  165. {
  166. #ifdef CONFIG_HOTPLUG_CPU
  167. struct linux_app_boot_info *labi;
  168. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  169. if (labi->labi_signature != LABI_SIGNATURE)
  170. panic("The bootloader version on this board is incorrect.");
  171. #endif
  172. /*
  173. * Only the low order mailbox bits are used for IPIs, leave
  174. * the other bits alone.
  175. */
  176. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
  177. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
  178. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
  179. mailbox_interrupt)) {
  180. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
  181. }
  182. }
  183. /**
  184. * Last chance for the board code to finish SMP initialization before
  185. * the CPU is "online".
  186. */
  187. static void octeon_smp_finish(void)
  188. {
  189. #ifdef CONFIG_CAVIUM_GDB
  190. unsigned long tmp;
  191. /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
  192. to be not masked by this core so we know the signal is received by
  193. someone */
  194. asm volatile ("dmfc0 %0, $22\n"
  195. "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
  196. #endif
  197. octeon_user_io_init();
  198. /* to generate the first CPU timer interrupt */
  199. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  200. }
  201. /**
  202. * Hook for after all CPUs are online
  203. */
  204. static void octeon_cpus_done(void)
  205. {
  206. #ifdef CONFIG_CAVIUM_GDB
  207. unsigned long tmp;
  208. /* Pulse MCD0 signal on Ctrl-C to stop all the cores. Also set the MCD0
  209. to be not masked by this core so we know the signal is received by
  210. someone */
  211. asm volatile ("dmfc0 %0, $22\n"
  212. "ori %0, %0, 0x9100\n" "dmtc0 %0, $22\n" : "=r" (tmp));
  213. #endif
  214. }
  215. #ifdef CONFIG_HOTPLUG_CPU
  216. /* State of each CPU. */
  217. DEFINE_PER_CPU(int, cpu_state);
  218. extern void fixup_irqs(void);
  219. static DEFINE_SPINLOCK(smp_reserve_lock);
  220. static int octeon_cpu_disable(void)
  221. {
  222. unsigned int cpu = smp_processor_id();
  223. if (cpu == 0)
  224. return -EBUSY;
  225. spin_lock(&smp_reserve_lock);
  226. set_cpu_online(cpu, false);
  227. cpu_clear(cpu, cpu_callin_map);
  228. local_irq_disable();
  229. fixup_irqs();
  230. local_irq_enable();
  231. flush_cache_all();
  232. local_flush_tlb_all();
  233. spin_unlock(&smp_reserve_lock);
  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 catched 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 __cpuinit octeon_cpu_callback(struct notifier_block *nfb,
  311. unsigned long action, void *hcpu)
  312. {
  313. unsigned int cpu = (unsigned long)hcpu;
  314. switch (action) {
  315. case CPU_UP_PREPARE:
  316. octeon_update_boot_vector(cpu);
  317. break;
  318. case CPU_ONLINE:
  319. pr_info("Cpu %d online\n", cpu);
  320. break;
  321. case CPU_DEAD:
  322. break;
  323. }
  324. return NOTIFY_OK;
  325. }
  326. static int __cpuinit register_cavium_notifier(void)
  327. {
  328. hotcpu_notifier(octeon_cpu_callback, 0);
  329. return 0;
  330. }
  331. late_initcall(register_cavium_notifier);
  332. #endif /* CONFIG_HOTPLUG_CPU */
  333. struct plat_smp_ops octeon_smp_ops = {
  334. .send_ipi_single = octeon_send_ipi_single,
  335. .send_ipi_mask = octeon_send_ipi_mask,
  336. .init_secondary = octeon_init_secondary,
  337. .smp_finish = octeon_smp_finish,
  338. .cpus_done = octeon_cpus_done,
  339. .boot_secondary = octeon_boot_secondary,
  340. .smp_setup = octeon_smp_setup,
  341. .prepare_cpus = octeon_prepare_cpus,
  342. #ifdef CONFIG_HOTPLUG_CPU
  343. .cpu_disable = octeon_cpu_disable,
  344. .cpu_die = octeon_cpu_die,
  345. #endif
  346. };