PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/mips/cavium-octeon/smp.c

https://bitbucket.org/alfredchen/linux-gc
C | 512 lines | 353 code | 91 blank | 68 comment | 44 complexity | a6861b41db51088dd59eb6d5ec7775a8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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/sched/hotplug.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/time.h>
  20. #include <asm/setup.h>
  21. #include <asm/octeon/octeon.h>
  22. #include "octeon_boot.h"
  23. volatile unsigned long octeon_processor_boot = 0xff;
  24. volatile unsigned long octeon_processor_sp;
  25. volatile unsigned long octeon_processor_gp;
  26. #ifdef CONFIG_RELOCATABLE
  27. volatile unsigned long octeon_processor_relocated_kernel_entry;
  28. #endif /* CONFIG_RELOCATABLE */
  29. #ifdef CONFIG_HOTPLUG_CPU
  30. uint64_t octeon_bootloader_entry_addr;
  31. EXPORT_SYMBOL(octeon_bootloader_entry_addr);
  32. #endif
  33. extern void kernel_entry(unsigned long arg1, ...);
  34. static void octeon_icache_flush(void)
  35. {
  36. asm volatile ("synci 0($0)\n");
  37. }
  38. static void (*octeon_message_functions[8])(void) = {
  39. scheduler_ipi,
  40. generic_smp_call_function_interrupt,
  41. octeon_icache_flush,
  42. };
  43. static irqreturn_t mailbox_interrupt(int irq, void *dev_id)
  44. {
  45. u64 mbox_clrx = CVMX_CIU_MBOX_CLRX(cvmx_get_core_num());
  46. u64 action;
  47. int i;
  48. /*
  49. * Make sure the function array initialization remains
  50. * correct.
  51. */
  52. BUILD_BUG_ON(SMP_RESCHEDULE_YOURSELF != (1 << 0));
  53. BUILD_BUG_ON(SMP_CALL_FUNCTION != (1 << 1));
  54. BUILD_BUG_ON(SMP_ICACHE_FLUSH != (1 << 2));
  55. /*
  56. * Load the mailbox register to figure out what we're supposed
  57. * to do.
  58. */
  59. action = cvmx_read_csr(mbox_clrx);
  60. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  61. action &= 0xff;
  62. else
  63. action &= 0xffff;
  64. /* Clear the mailbox to clear the interrupt */
  65. cvmx_write_csr(mbox_clrx, action);
  66. for (i = 0; i < ARRAY_SIZE(octeon_message_functions) && action;) {
  67. if (action & 1) {
  68. void (*fn)(void) = octeon_message_functions[i];
  69. if (fn)
  70. fn();
  71. }
  72. action >>= 1;
  73. i++;
  74. }
  75. return IRQ_HANDLED;
  76. }
  77. /**
  78. * Cause the function described by call_data to be executed on the passed
  79. * cpu. When the function has finished, increment the finished field of
  80. * call_data.
  81. */
  82. void octeon_send_ipi_single(int cpu, unsigned int action)
  83. {
  84. int coreid = cpu_logical_map(cpu);
  85. /*
  86. pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
  87. coreid, action);
  88. */
  89. cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
  90. }
  91. static inline void octeon_send_ipi_mask(const struct cpumask *mask,
  92. unsigned int action)
  93. {
  94. unsigned int i;
  95. for_each_cpu(i, mask)
  96. octeon_send_ipi_single(i, action);
  97. }
  98. /**
  99. * Detect available CPUs, populate cpu_possible_mask
  100. */
  101. static void octeon_smp_hotplug_setup(void)
  102. {
  103. #ifdef CONFIG_HOTPLUG_CPU
  104. struct linux_app_boot_info *labi;
  105. if (!setup_max_cpus)
  106. return;
  107. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  108. if (labi->labi_signature != LABI_SIGNATURE) {
  109. pr_info("The bootloader on this board does not support HOTPLUG_CPU.");
  110. return;
  111. }
  112. octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
  113. #endif
  114. }
  115. static void __init octeon_smp_setup(void)
  116. {
  117. const int coreid = cvmx_get_core_num();
  118. int cpus;
  119. int id;
  120. struct cvmx_sysinfo *sysinfo = cvmx_sysinfo_get();
  121. #ifdef CONFIG_HOTPLUG_CPU
  122. int core_mask = octeon_get_boot_coremask();
  123. unsigned int num_cores = cvmx_octeon_num_cores();
  124. #endif
  125. /* The present CPUs are initially just the boot cpu (CPU 0). */
  126. for (id = 0; id < NR_CPUS; id++) {
  127. set_cpu_possible(id, id == 0);
  128. set_cpu_present(id, id == 0);
  129. }
  130. __cpu_number_map[coreid] = 0;
  131. __cpu_logical_map[0] = coreid;
  132. /* The present CPUs get the lowest CPU numbers. */
  133. cpus = 1;
  134. for (id = 0; id < NR_CPUS; id++) {
  135. if ((id != coreid) && cvmx_coremask_is_core_set(&sysinfo->core_mask, id)) {
  136. set_cpu_possible(cpus, true);
  137. set_cpu_present(cpus, true);
  138. __cpu_number_map[id] = cpus;
  139. __cpu_logical_map[cpus] = id;
  140. cpus++;
  141. }
  142. }
  143. #ifdef CONFIG_HOTPLUG_CPU
  144. /*
  145. * The possible CPUs are all those present on the chip. We
  146. * will assign CPU numbers for possible cores as well. Cores
  147. * are always consecutively numberd from 0.
  148. */
  149. for (id = 0; setup_max_cpus && octeon_bootloader_entry_addr &&
  150. id < num_cores && id < NR_CPUS; id++) {
  151. if (!(core_mask & (1 << id))) {
  152. set_cpu_possible(cpus, true);
  153. __cpu_number_map[id] = cpus;
  154. __cpu_logical_map[cpus] = id;
  155. cpus++;
  156. }
  157. }
  158. #endif
  159. octeon_smp_hotplug_setup();
  160. }
  161. #ifdef CONFIG_RELOCATABLE
  162. int plat_post_relocation(long offset)
  163. {
  164. unsigned long entry = (unsigned long)kernel_entry;
  165. /* Send secondaries into relocated kernel */
  166. octeon_processor_relocated_kernel_entry = entry + offset;
  167. return 0;
  168. }
  169. #endif /* CONFIG_RELOCATABLE */
  170. /**
  171. * Firmware CPU startup hook
  172. *
  173. */
  174. static void octeon_boot_secondary(int cpu, struct task_struct *idle)
  175. {
  176. int count;
  177. pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
  178. cpu_logical_map(cpu));
  179. octeon_processor_sp = __KSTK_TOS(idle);
  180. octeon_processor_gp = (unsigned long)(task_thread_info(idle));
  181. octeon_processor_boot = cpu_logical_map(cpu);
  182. mb();
  183. count = 10000;
  184. while (octeon_processor_sp && count) {
  185. /* Waiting for processor to get the SP and GP */
  186. udelay(1);
  187. count--;
  188. }
  189. if (count == 0)
  190. pr_err("Secondary boot timeout\n");
  191. }
  192. /**
  193. * After we've done initial boot, this function is called to allow the
  194. * board code to clean up state, if needed
  195. */
  196. static void octeon_init_secondary(void)
  197. {
  198. unsigned int sr;
  199. sr = set_c0_status(ST0_BEV);
  200. write_c0_ebase((u32)ebase);
  201. write_c0_status(sr);
  202. octeon_check_cpu_bist();
  203. octeon_init_cvmcount();
  204. octeon_irq_setup_secondary();
  205. }
  206. /**
  207. * Callout to firmware before smp_init
  208. *
  209. */
  210. static void __init octeon_prepare_cpus(unsigned int max_cpus)
  211. {
  212. /*
  213. * Only the low order mailbox bits are used for IPIs, leave
  214. * the other bits alone.
  215. */
  216. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
  217. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
  218. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
  219. mailbox_interrupt)) {
  220. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
  221. }
  222. }
  223. /**
  224. * Last chance for the board code to finish SMP initialization before
  225. * the CPU is "online".
  226. */
  227. static void octeon_smp_finish(void)
  228. {
  229. octeon_user_io_init();
  230. /* to generate the first CPU timer interrupt */
  231. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  232. local_irq_enable();
  233. }
  234. #ifdef CONFIG_HOTPLUG_CPU
  235. /* State of each CPU. */
  236. DEFINE_PER_CPU(int, cpu_state);
  237. static int octeon_cpu_disable(void)
  238. {
  239. unsigned int cpu = smp_processor_id();
  240. if (cpu == 0)
  241. return -EBUSY;
  242. if (!octeon_bootloader_entry_addr)
  243. return -ENOTSUPP;
  244. set_cpu_online(cpu, false);
  245. calculate_cpu_foreign_map();
  246. octeon_fixup_irqs();
  247. __flush_cache_all();
  248. local_flush_tlb_all();
  249. return 0;
  250. }
  251. static void octeon_cpu_die(unsigned int cpu)
  252. {
  253. int coreid = cpu_logical_map(cpu);
  254. uint32_t mask, new_mask;
  255. const struct cvmx_bootmem_named_block_desc *block_desc;
  256. while (per_cpu(cpu_state, cpu) != CPU_DEAD)
  257. cpu_relax();
  258. /*
  259. * This is a bit complicated strategics of getting/settig available
  260. * cores mask, copied from bootloader
  261. */
  262. mask = 1 << coreid;
  263. /* LINUX_APP_BOOT_BLOCK is initialized in bootoct binary */
  264. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  265. if (!block_desc) {
  266. struct linux_app_boot_info *labi;
  267. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  268. labi->avail_coremask |= mask;
  269. new_mask = labi->avail_coremask;
  270. } else { /* alternative, already initialized */
  271. uint32_t *p = (uint32_t *)PHYS_TO_XKSEG_CACHED(block_desc->base_addr +
  272. AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  273. *p |= mask;
  274. new_mask = *p;
  275. }
  276. pr_info("Reset core %d. Available Coremask = 0x%x \n", coreid, new_mask);
  277. mb();
  278. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  279. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  280. }
  281. void play_dead(void)
  282. {
  283. int cpu = cpu_number_map(cvmx_get_core_num());
  284. idle_task_exit();
  285. octeon_processor_boot = 0xff;
  286. per_cpu(cpu_state, cpu) = CPU_DEAD;
  287. mb();
  288. while (1) /* core will be reset here */
  289. ;
  290. }
  291. static void start_after_reset(void)
  292. {
  293. kernel_entry(0, 0, 0); /* set a2 = 0 for secondary core */
  294. }
  295. static int octeon_update_boot_vector(unsigned int cpu)
  296. {
  297. int coreid = cpu_logical_map(cpu);
  298. uint32_t avail_coremask;
  299. const struct cvmx_bootmem_named_block_desc *block_desc;
  300. struct boot_init_vector *boot_vect =
  301. (struct boot_init_vector *)PHYS_TO_XKSEG_CACHED(BOOTLOADER_BOOT_VECTOR);
  302. block_desc = cvmx_bootmem_find_named_block(LINUX_APP_BOOT_BLOCK_NAME);
  303. if (!block_desc) {
  304. struct linux_app_boot_info *labi;
  305. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  306. avail_coremask = labi->avail_coremask;
  307. labi->avail_coremask &= ~(1 << coreid);
  308. } else { /* alternative, already initialized */
  309. avail_coremask = *(uint32_t *)PHYS_TO_XKSEG_CACHED(
  310. block_desc->base_addr + AVAIL_COREMASK_OFFSET_IN_LINUX_APP_BOOT_BLOCK);
  311. }
  312. if (!(avail_coremask & (1 << coreid))) {
  313. /* core not available, assume, that caught by simple-executive */
  314. cvmx_write_csr(CVMX_CIU_PP_RST, 1 << coreid);
  315. cvmx_write_csr(CVMX_CIU_PP_RST, 0);
  316. }
  317. boot_vect[coreid].app_start_func_addr =
  318. (uint32_t) (unsigned long) start_after_reset;
  319. boot_vect[coreid].code_addr = octeon_bootloader_entry_addr;
  320. mb();
  321. cvmx_write_csr(CVMX_CIU_NMI, (1 << coreid) & avail_coremask);
  322. return 0;
  323. }
  324. static int register_cavium_notifier(void)
  325. {
  326. return cpuhp_setup_state_nocalls(CPUHP_MIPS_SOC_PREPARE,
  327. "mips/cavium:prepare",
  328. octeon_update_boot_vector, NULL);
  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. .boot_secondary = octeon_boot_secondary,
  338. .smp_setup = octeon_smp_setup,
  339. .prepare_cpus = octeon_prepare_cpus,
  340. #ifdef CONFIG_HOTPLUG_CPU
  341. .cpu_disable = octeon_cpu_disable,
  342. .cpu_die = octeon_cpu_die,
  343. #endif
  344. };
  345. static irqreturn_t octeon_78xx_reched_interrupt(int irq, void *dev_id)
  346. {
  347. scheduler_ipi();
  348. return IRQ_HANDLED;
  349. }
  350. static irqreturn_t octeon_78xx_call_function_interrupt(int irq, void *dev_id)
  351. {
  352. generic_smp_call_function_interrupt();
  353. return IRQ_HANDLED;
  354. }
  355. static irqreturn_t octeon_78xx_icache_flush_interrupt(int irq, void *dev_id)
  356. {
  357. octeon_icache_flush();
  358. return IRQ_HANDLED;
  359. }
  360. /*
  361. * Callout to firmware before smp_init
  362. */
  363. static void octeon_78xx_prepare_cpus(unsigned int max_cpus)
  364. {
  365. if (request_irq(OCTEON_IRQ_MBOX0 + 0,
  366. octeon_78xx_reched_interrupt,
  367. IRQF_PERCPU | IRQF_NO_THREAD, "Scheduler",
  368. octeon_78xx_reched_interrupt)) {
  369. panic("Cannot request_irq for SchedulerIPI");
  370. }
  371. if (request_irq(OCTEON_IRQ_MBOX0 + 1,
  372. octeon_78xx_call_function_interrupt,
  373. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-Call",
  374. octeon_78xx_call_function_interrupt)) {
  375. panic("Cannot request_irq for SMP-Call");
  376. }
  377. if (request_irq(OCTEON_IRQ_MBOX0 + 2,
  378. octeon_78xx_icache_flush_interrupt,
  379. IRQF_PERCPU | IRQF_NO_THREAD, "ICache-Flush",
  380. octeon_78xx_icache_flush_interrupt)) {
  381. panic("Cannot request_irq for ICache-Flush");
  382. }
  383. }
  384. static void octeon_78xx_send_ipi_single(int cpu, unsigned int action)
  385. {
  386. int i;
  387. for (i = 0; i < 8; i++) {
  388. if (action & 1)
  389. octeon_ciu3_mbox_send(cpu, i);
  390. action >>= 1;
  391. }
  392. }
  393. static void octeon_78xx_send_ipi_mask(const struct cpumask *mask,
  394. unsigned int action)
  395. {
  396. unsigned int cpu;
  397. for_each_cpu(cpu, mask)
  398. octeon_78xx_send_ipi_single(cpu, action);
  399. }
  400. static struct plat_smp_ops octeon_78xx_smp_ops = {
  401. .send_ipi_single = octeon_78xx_send_ipi_single,
  402. .send_ipi_mask = octeon_78xx_send_ipi_mask,
  403. .init_secondary = octeon_init_secondary,
  404. .smp_finish = octeon_smp_finish,
  405. .boot_secondary = octeon_boot_secondary,
  406. .smp_setup = octeon_smp_setup,
  407. .prepare_cpus = octeon_78xx_prepare_cpus,
  408. #ifdef CONFIG_HOTPLUG_CPU
  409. .cpu_disable = octeon_cpu_disable,
  410. .cpu_die = octeon_cpu_die,
  411. #endif
  412. };
  413. void __init octeon_setup_smp(void)
  414. {
  415. struct plat_smp_ops *ops;
  416. if (octeon_has_feature(OCTEON_FEATURE_CIU3))
  417. ops = &octeon_78xx_smp_ops;
  418. else
  419. ops = &octeon_smp_ops;
  420. register_smp_ops(ops);
  421. }