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

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

https://gitlab.com/joemail44.jp/linux-next
C | 511 lines | 354 code | 90 blank | 67 comment | 45 complexity | 2037f2fc688332a08b59e536251ddbe1 MD5 | raw file
  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 void octeon_icache_flush(void)
  28. {
  29. asm volatile ("synci 0($0)\n");
  30. }
  31. static void (*octeon_message_functions[8])(void) = {
  32. scheduler_ipi,
  33. generic_smp_call_function_interrupt,
  34. octeon_icache_flush,
  35. };
  36. static irqreturn_t mailbox_interrupt(int irq, void *dev_id)
  37. {
  38. u64 mbox_clrx = CVMX_CIU_MBOX_CLRX(cvmx_get_core_num());
  39. u64 action;
  40. int i;
  41. /*
  42. * Make sure the function array initialization remains
  43. * correct.
  44. */
  45. BUILD_BUG_ON(SMP_RESCHEDULE_YOURSELF != (1 << 0));
  46. BUILD_BUG_ON(SMP_CALL_FUNCTION != (1 << 1));
  47. BUILD_BUG_ON(SMP_ICACHE_FLUSH != (1 << 2));
  48. /*
  49. * Load the mailbox register to figure out what we're supposed
  50. * to do.
  51. */
  52. action = cvmx_read_csr(mbox_clrx);
  53. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  54. action &= 0xff;
  55. else
  56. action &= 0xffff;
  57. /* Clear the mailbox to clear the interrupt */
  58. cvmx_write_csr(mbox_clrx, action);
  59. for (i = 0; i < ARRAY_SIZE(octeon_message_functions) && action;) {
  60. if (action & 1) {
  61. void (*fn)(void) = octeon_message_functions[i];
  62. if (fn)
  63. fn();
  64. }
  65. action >>= 1;
  66. i++;
  67. }
  68. return IRQ_HANDLED;
  69. }
  70. /**
  71. * Cause the function described by call_data to be executed on the passed
  72. * cpu. When the function has finished, increment the finished field of
  73. * call_data.
  74. */
  75. void octeon_send_ipi_single(int cpu, unsigned int action)
  76. {
  77. int coreid = cpu_logical_map(cpu);
  78. /*
  79. pr_info("SMP: Mailbox send cpu=%d, coreid=%d, action=%u\n", cpu,
  80. coreid, action);
  81. */
  82. cvmx_write_csr(CVMX_CIU_MBOX_SETX(coreid), action);
  83. }
  84. static inline void octeon_send_ipi_mask(const struct cpumask *mask,
  85. unsigned int action)
  86. {
  87. unsigned int i;
  88. for_each_cpu(i, mask)
  89. octeon_send_ipi_single(i, action);
  90. }
  91. /**
  92. * Detect available CPUs, populate cpu_possible_mask
  93. */
  94. static void octeon_smp_hotplug_setup(void)
  95. {
  96. #ifdef CONFIG_HOTPLUG_CPU
  97. struct linux_app_boot_info *labi;
  98. if (!setup_max_cpus)
  99. return;
  100. labi = (struct linux_app_boot_info *)PHYS_TO_XKSEG_CACHED(LABI_ADDR_IN_BOOTLOADER);
  101. if (labi->labi_signature != LABI_SIGNATURE) {
  102. pr_info("The bootloader on this board does not support HOTPLUG_CPU.");
  103. return;
  104. }
  105. octeon_bootloader_entry_addr = labi->InitTLBStart_addr;
  106. #endif
  107. }
  108. static void __init octeon_smp_setup(void)
  109. {
  110. const int coreid = cvmx_get_core_num();
  111. int cpus;
  112. int id;
  113. struct cvmx_sysinfo *sysinfo = cvmx_sysinfo_get();
  114. #ifdef CONFIG_HOTPLUG_CPU
  115. int core_mask = octeon_get_boot_coremask();
  116. unsigned int num_cores = cvmx_octeon_num_cores();
  117. #endif
  118. /* The present CPUs are initially just the boot cpu (CPU 0). */
  119. for (id = 0; id < NR_CPUS; id++) {
  120. set_cpu_possible(id, id == 0);
  121. set_cpu_present(id, id == 0);
  122. }
  123. __cpu_number_map[coreid] = 0;
  124. __cpu_logical_map[0] = coreid;
  125. /* The present CPUs get the lowest CPU numbers. */
  126. cpus = 1;
  127. for (id = 0; id < NR_CPUS; id++) {
  128. if ((id != coreid) && cvmx_coremask_is_core_set(&sysinfo->core_mask, id)) {
  129. set_cpu_possible(cpus, true);
  130. set_cpu_present(cpus, true);
  131. __cpu_number_map[id] = cpus;
  132. __cpu_logical_map[cpus] = id;
  133. cpus++;
  134. }
  135. }
  136. #ifdef CONFIG_HOTPLUG_CPU
  137. /*
  138. * The possible CPUs are all those present on the chip. We
  139. * will assign CPU numbers for possible cores as well. Cores
  140. * are always consecutively numberd from 0.
  141. */
  142. for (id = 0; setup_max_cpus && octeon_bootloader_entry_addr &&
  143. id < num_cores && id < NR_CPUS; id++) {
  144. if (!(core_mask & (1 << id))) {
  145. set_cpu_possible(cpus, true);
  146. __cpu_number_map[id] = cpus;
  147. __cpu_logical_map[cpus] = id;
  148. cpus++;
  149. }
  150. }
  151. #endif
  152. octeon_smp_hotplug_setup();
  153. }
  154. /**
  155. * Firmware CPU startup hook
  156. *
  157. */
  158. static void octeon_boot_secondary(int cpu, struct task_struct *idle)
  159. {
  160. int count;
  161. pr_info("SMP: Booting CPU%02d (CoreId %2d)...\n", cpu,
  162. cpu_logical_map(cpu));
  163. octeon_processor_sp = __KSTK_TOS(idle);
  164. octeon_processor_gp = (unsigned long)(task_thread_info(idle));
  165. octeon_processor_boot = cpu_logical_map(cpu);
  166. mb();
  167. count = 10000;
  168. while (octeon_processor_sp && count) {
  169. /* Waiting for processor to get the SP and GP */
  170. udelay(1);
  171. count--;
  172. }
  173. if (count == 0)
  174. pr_err("Secondary boot timeout\n");
  175. }
  176. /**
  177. * After we've done initial boot, this function is called to allow the
  178. * board code to clean up state, if needed
  179. */
  180. static void octeon_init_secondary(void)
  181. {
  182. unsigned int sr;
  183. sr = set_c0_status(ST0_BEV);
  184. write_c0_ebase((u32)ebase);
  185. write_c0_status(sr);
  186. octeon_check_cpu_bist();
  187. octeon_init_cvmcount();
  188. octeon_irq_setup_secondary();
  189. }
  190. /**
  191. * Callout to firmware before smp_init
  192. *
  193. */
  194. static void __init octeon_prepare_cpus(unsigned int max_cpus)
  195. {
  196. /*
  197. * Only the low order mailbox bits are used for IPIs, leave
  198. * the other bits alone.
  199. */
  200. cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), 0xffff);
  201. if (request_irq(OCTEON_IRQ_MBOX0, mailbox_interrupt,
  202. IRQF_PERCPU | IRQF_NO_THREAD, "SMP-IPI",
  203. mailbox_interrupt)) {
  204. panic("Cannot request_irq(OCTEON_IRQ_MBOX0)");
  205. }
  206. }
  207. /**
  208. * Last chance for the board code to finish SMP initialization before
  209. * the CPU is "online".
  210. */
  211. static void octeon_smp_finish(void)
  212. {
  213. octeon_user_io_init();
  214. /* to generate the first CPU timer interrupt */
  215. write_c0_compare(read_c0_count() + mips_hpt_frequency / HZ);
  216. local_irq_enable();
  217. }
  218. #ifdef CONFIG_HOTPLUG_CPU
  219. /* State of each CPU. */
  220. DEFINE_PER_CPU(int, cpu_state);
  221. static int octeon_cpu_disable(void)
  222. {
  223. unsigned int cpu = smp_processor_id();
  224. if (cpu == 0)
  225. return -EBUSY;
  226. if (!octeon_bootloader_entry_addr)
  227. return -ENOTSUPP;
  228. set_cpu_online(cpu, false);
  229. cpumask_clear_cpu(cpu, &cpu_callin_map);
  230. octeon_fixup_irqs();
  231. __flush_cache_all();
  232. local_flush_tlb_all();
  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 caught 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 octeon_cpu_callback(struct notifier_block *nfb,
  310. unsigned long action, void *hcpu)
  311. {
  312. unsigned int cpu = (unsigned long)hcpu;
  313. switch (action & ~CPU_TASKS_FROZEN) {
  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 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. .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. }