smp.c 10 KB

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