smp.c 9.2 KB

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