smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. }
  159. /**
  160. * Callout to firmware before smp_init
  161. *
  162. */
  163. void octeon_prepare_cpus(unsigned int max_cpus)
  164. {
  165. #ifdef CONFIG_HOTPLUG_CPU
  166. struct linux_app_boot_info *labi;
  167. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  168. if (labi->labi_signature != LABI_SIGNATURE)
  169. panic("The bootloader version on this board is incorrect.");
  170. #endif
  171. /*
  172. * Only the low order mailbox bits are used for IPIs, leave
  173. * the other bits alone.
  174. */
  175. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
  176. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
  177. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
  178. mailbox_interrupt)) {
  179. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
  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. local_irq_enable();
  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 int octeon_cpu_disable(void)
  220. {
  221. unsigned int cpu = smp_processor_id();
  222. if (cpu == 0)
  223. return -EBUSY;
  224. set_cpu_online(cpu, false);
  225. cpu_clear(cpu, cpu_callin_map);
  226. local_irq_disable();
  227. fixup_irqs();
  228. local_irq_enable();
  229. flush_cache_all();
  230. local_flush_tlb_all();
  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. };