smp.c 9.4 KB

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