smp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * RajeshwarR: Dec 11, 2007
  9. * -- Added support for Inter Processor Interrupts
  10. *
  11. * Vineetg: Nov 1st, 2007
  12. * -- Initial Write (Borrowed heavily from ARM)
  13. */
  14. #include <linux/spinlock.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/profile.h>
  18. #include <linux/mm.h>
  19. #include <linux/cpu.h>
  20. #include <linux/irq.h>
  21. #include <linux/atomic.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/reboot.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/export.h>
  26. #include <linux/of_fdt.h>
  27. #include <asm/processor.h>
  28. #include <asm/setup.h>
  29. #include <asm/mach_desc.h>
  30. #ifndef CONFIG_ARC_HAS_LLSC
  31. arch_spinlock_t smp_atomic_ops_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  32. arch_spinlock_t smp_bitops_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  33. EXPORT_SYMBOL_GPL(smp_atomic_ops_lock);
  34. EXPORT_SYMBOL_GPL(smp_bitops_lock);
  35. #endif
  36. struct plat_smp_ops __weak plat_smp_ops;
  37. /* XXX: per cpu ? Only needed once in early seconday boot */
  38. struct task_struct *secondary_idle_tsk;
  39. /* Called from start_kernel */
  40. void __init smp_prepare_boot_cpu(void)
  41. {
  42. }
  43. static int __init arc_get_cpu_map(const char *name, struct cpumask *cpumask)
  44. {
  45. unsigned long dt_root = of_get_flat_dt_root();
  46. const char *buf;
  47. buf = of_get_flat_dt_prop(dt_root, name, NULL);
  48. if (!buf)
  49. return -EINVAL;
  50. if (cpulist_parse(buf, cpumask))
  51. return -EINVAL;
  52. return 0;
  53. }
  54. /*
  55. * Read from DeviceTree and setup cpu possible mask. If there is no
  56. * "possible-cpus" property in DeviceTree pretend all [0..NR_CPUS-1] exist.
  57. */
  58. static void __init arc_init_cpu_possible(void)
  59. {
  60. struct cpumask cpumask;
  61. if (arc_get_cpu_map("possible-cpus", &cpumask)) {
  62. pr_warn("Failed to get possible-cpus from dtb, pretending all %u cpus exist\n",
  63. NR_CPUS);
  64. cpumask_setall(&cpumask);
  65. }
  66. if (!cpumask_test_cpu(0, &cpumask))
  67. panic("Master cpu (cpu[0]) is missed in cpu possible mask!");
  68. init_cpu_possible(&cpumask);
  69. }
  70. /*
  71. * Called from setup_arch() before calling setup_processor()
  72. *
  73. * - Initialise the CPU possible map early - this describes the CPUs
  74. * which may be present or become present in the system.
  75. * - Call early smp init hook. This can initialize a specific multi-core
  76. * IP which is say common to several platforms (hence not part of
  77. * platform specific int_early() hook)
  78. */
  79. void __init smp_init_cpus(void)
  80. {
  81. arc_init_cpu_possible();
  82. if (plat_smp_ops.init_early_smp)
  83. plat_smp_ops.init_early_smp();
  84. }
  85. /* called from init ( ) => process 1 */
  86. void __init smp_prepare_cpus(unsigned int max_cpus)
  87. {
  88. /*
  89. * if platform didn't set the present map already, do it now
  90. * boot cpu is set to present already by init/main.c
  91. */
  92. if (num_present_cpus() <= 1)
  93. init_cpu_present(cpu_possible_mask);
  94. }
  95. void __init smp_cpus_done(unsigned int max_cpus)
  96. {
  97. }
  98. /*
  99. * Default smp boot helper for Run-on-reset case where all cores start off
  100. * together. Non-masters need to wait for Master to start running.
  101. * This is implemented using a flag in memory, which Non-masters spin-wait on.
  102. * Master sets it to cpu-id of core to "ungate" it.
  103. */
  104. static volatile int wake_flag;
  105. #ifdef CONFIG_ISA_ARCOMPACT
  106. #define __boot_read(f) f
  107. #define __boot_write(f, v) f = v
  108. #else
  109. #define __boot_read(f) arc_read_uncached_32(&f)
  110. #define __boot_write(f, v) arc_write_uncached_32(&f, v)
  111. #endif
  112. static void arc_default_smp_cpu_kick(int cpu, unsigned long pc)
  113. {
  114. BUG_ON(cpu == 0);
  115. __boot_write(wake_flag, cpu);
  116. }
  117. void arc_platform_smp_wait_to_boot(int cpu)
  118. {
  119. /* for halt-on-reset, we've waited already */
  120. if (IS_ENABLED(CONFIG_ARC_SMP_HALT_ON_RESET))
  121. return;
  122. while (__boot_read(wake_flag) != cpu)
  123. ;
  124. __boot_write(wake_flag, 0);
  125. }
  126. const char *arc_platform_smp_cpuinfo(void)
  127. {
  128. return plat_smp_ops.info ? : "";
  129. }
  130. /*
  131. * The very first "C" code executed by secondary
  132. * Called from asm stub in head.S
  133. * "current"/R25 already setup by low level boot code
  134. */
  135. void start_kernel_secondary(void)
  136. {
  137. struct mm_struct *mm = &init_mm;
  138. unsigned int cpu = smp_processor_id();
  139. /* MMU, Caches, Vector Table, Interrupts etc */
  140. setup_processor();
  141. mmget(mm);
  142. mmgrab(mm);
  143. current->active_mm = mm;
  144. cpumask_set_cpu(cpu, mm_cpumask(mm));
  145. /* Some SMP H/w setup - for each cpu */
  146. if (plat_smp_ops.init_per_cpu)
  147. plat_smp_ops.init_per_cpu(cpu);
  148. if (machine_desc->init_per_cpu)
  149. machine_desc->init_per_cpu(cpu);
  150. notify_cpu_starting(cpu);
  151. set_cpu_online(cpu, true);
  152. pr_info("## CPU%u LIVE ##: Executing Code...\n", cpu);
  153. local_irq_enable();
  154. preempt_disable();
  155. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  156. }
  157. /*
  158. * Called from kernel_init( ) -> smp_init( ) - for each CPU
  159. *
  160. * At this point, Secondary Processor is "HALT"ed:
  161. * -It booted, but was halted in head.S
  162. * -It was configured to halt-on-reset
  163. * So need to wake it up.
  164. *
  165. * Essential requirements being where to run from (PC) and stack (SP)
  166. */
  167. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  168. {
  169. unsigned long wait_till;
  170. secondary_idle_tsk = idle;
  171. pr_info("Idle Task [%d] %p", cpu, idle);
  172. pr_info("Trying to bring up CPU%u ...\n", cpu);
  173. if (plat_smp_ops.cpu_kick)
  174. plat_smp_ops.cpu_kick(cpu,
  175. (unsigned long)first_lines_of_secondary);
  176. else
  177. arc_default_smp_cpu_kick(cpu, (unsigned long)NULL);
  178. /* wait for 1 sec after kicking the secondary */
  179. wait_till = jiffies + HZ;
  180. while (time_before(jiffies, wait_till)) {
  181. if (cpu_online(cpu))
  182. break;
  183. }
  184. if (!cpu_online(cpu)) {
  185. pr_info("Timeout: CPU%u FAILED to comeup !!!\n", cpu);
  186. return -1;
  187. }
  188. secondary_idle_tsk = NULL;
  189. return 0;
  190. }
  191. /*
  192. * not supported here
  193. */
  194. int setup_profiling_timer(unsigned int multiplier)
  195. {
  196. return -EINVAL;
  197. }
  198. /*****************************************************************************/
  199. /* Inter Processor Interrupt Handling */
  200. /*****************************************************************************/
  201. enum ipi_msg_type {
  202. IPI_EMPTY = 0,
  203. IPI_RESCHEDULE = 1,
  204. IPI_CALL_FUNC,
  205. IPI_CPU_STOP,
  206. };
  207. /*
  208. * In arches with IRQ for each msg type (above), receiver can use IRQ-id to
  209. * figure out what msg was sent. For those which don't (ARC has dedicated IPI
  210. * IRQ), the msg-type needs to be conveyed via per-cpu data
  211. */
  212. static DEFINE_PER_CPU(unsigned long, ipi_data);
  213. static void ipi_send_msg_one(int cpu, enum ipi_msg_type msg)
  214. {
  215. unsigned long __percpu *ipi_data_ptr = per_cpu_ptr(&ipi_data, cpu);
  216. unsigned long old, new;
  217. unsigned long flags;
  218. pr_debug("%d Sending msg [%d] to %d\n", smp_processor_id(), msg, cpu);
  219. local_irq_save(flags);
  220. /*
  221. * Atomically write new msg bit (in case others are writing too),
  222. * and read back old value
  223. */
  224. do {
  225. new = old = READ_ONCE(*ipi_data_ptr);
  226. new |= 1U << msg;
  227. } while (cmpxchg(ipi_data_ptr, old, new) != old);
  228. /*
  229. * Call the platform specific IPI kick function, but avoid if possible:
  230. * Only do so if there's no pending msg from other concurrent sender(s).
  231. * Otherwise, recevier will see this msg as well when it takes the
  232. * IPI corresponding to that msg. This is true, even if it is already in
  233. * IPI handler, because !@old means it has not yet dequeued the msg(s)
  234. * so @new msg can be a free-loader
  235. */
  236. if (plat_smp_ops.ipi_send && !old)
  237. plat_smp_ops.ipi_send(cpu);
  238. local_irq_restore(flags);
  239. }
  240. static void ipi_send_msg(const struct cpumask *callmap, enum ipi_msg_type msg)
  241. {
  242. unsigned int cpu;
  243. for_each_cpu(cpu, callmap)
  244. ipi_send_msg_one(cpu, msg);
  245. }
  246. void smp_send_reschedule(int cpu)
  247. {
  248. ipi_send_msg_one(cpu, IPI_RESCHEDULE);
  249. }
  250. void smp_send_stop(void)
  251. {
  252. struct cpumask targets;
  253. cpumask_copy(&targets, cpu_online_mask);
  254. cpumask_clear_cpu(smp_processor_id(), &targets);
  255. ipi_send_msg(&targets, IPI_CPU_STOP);
  256. }
  257. void arch_send_call_function_single_ipi(int cpu)
  258. {
  259. ipi_send_msg_one(cpu, IPI_CALL_FUNC);
  260. }
  261. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  262. {
  263. ipi_send_msg(mask, IPI_CALL_FUNC);
  264. }
  265. /*
  266. * ipi_cpu_stop - handle IPI from smp_send_stop()
  267. */
  268. static void ipi_cpu_stop(void)
  269. {
  270. machine_halt();
  271. }
  272. static inline int __do_IPI(unsigned long msg)
  273. {
  274. int rc = 0;
  275. switch (msg) {
  276. case IPI_RESCHEDULE:
  277. scheduler_ipi();
  278. break;
  279. case IPI_CALL_FUNC:
  280. generic_smp_call_function_interrupt();
  281. break;
  282. case IPI_CPU_STOP:
  283. ipi_cpu_stop();
  284. break;
  285. default:
  286. rc = 1;
  287. }
  288. return rc;
  289. }
  290. /*
  291. * arch-common ISR to handle for inter-processor interrupts
  292. * Has hooks for platform specific IPI
  293. */
  294. irqreturn_t do_IPI(int irq, void *dev_id)
  295. {
  296. unsigned long pending;
  297. unsigned long __maybe_unused copy;
  298. pr_debug("IPI [%ld] received on cpu %d\n",
  299. *this_cpu_ptr(&ipi_data), smp_processor_id());
  300. if (plat_smp_ops.ipi_clear)
  301. plat_smp_ops.ipi_clear(irq);
  302. /*
  303. * "dequeue" the msg corresponding to this IPI (and possibly other
  304. * piggybacked msg from elided IPIs: see ipi_send_msg_one() above)
  305. */
  306. copy = pending = xchg(this_cpu_ptr(&ipi_data), 0);
  307. do {
  308. unsigned long msg = __ffs(pending);
  309. int rc;
  310. rc = __do_IPI(msg);
  311. if (rc)
  312. pr_info("IPI with bogus msg %ld in %ld\n", msg, copy);
  313. pending &= ~(1U << msg);
  314. } while (pending);
  315. return IRQ_HANDLED;
  316. }
  317. /*
  318. * API called by platform code to hookup arch-common ISR to their IPI IRQ
  319. *
  320. * Note: If IPI is provided by platform (vs. say ARC MCIP), their intc setup/map
  321. * function needs to call call irq_set_percpu_devid() for IPI IRQ, otherwise
  322. * request_percpu_irq() below will fail
  323. */
  324. static DEFINE_PER_CPU(int, ipi_dev);
  325. int smp_ipi_irq_setup(int cpu, irq_hw_number_t hwirq)
  326. {
  327. int *dev = per_cpu_ptr(&ipi_dev, cpu);
  328. unsigned int virq = irq_find_mapping(NULL, hwirq);
  329. if (!virq)
  330. panic("Cannot find virq for root domain and hwirq=%lu", hwirq);
  331. /* Boot cpu calls request, all call enable */
  332. if (!cpu) {
  333. int rc;
  334. rc = request_percpu_irq(virq, do_IPI, "IPI Interrupt", dev);
  335. if (rc)
  336. panic("Percpu IRQ request failed for %u\n", virq);
  337. }
  338. enable_percpu_irq(virq, 0);
  339. return 0;
  340. }