smp.c 9.4 KB

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