common.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*
  2. * Copyright 2016,2017 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) "xive: " fmt
  10. #include <linux/types.h>
  11. #include <linux/threads.h>
  12. #include <linux/kernel.h>
  13. #include <linux/irq.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/smp.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/init.h>
  19. #include <linux/cpu.h>
  20. #include <linux/of.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/msi.h>
  24. #include <asm/prom.h>
  25. #include <asm/io.h>
  26. #include <asm/smp.h>
  27. #include <asm/machdep.h>
  28. #include <asm/irq.h>
  29. #include <asm/errno.h>
  30. #include <asm/xive.h>
  31. #include <asm/xive-regs.h>
  32. #include <asm/xmon.h>
  33. #include "xive-internal.h"
  34. #undef DEBUG_FLUSH
  35. #undef DEBUG_ALL
  36. #ifdef DEBUG_ALL
  37. #define DBG_VERBOSE(fmt...) pr_devel(fmt)
  38. #else
  39. #define DBG_VERBOSE(fmt...) do { } while(0)
  40. #endif
  41. bool __xive_enabled;
  42. EXPORT_SYMBOL_GPL(__xive_enabled);
  43. bool xive_cmdline_disabled;
  44. /* We use only one priority for now */
  45. static u8 xive_irq_priority;
  46. /* TIMA exported to KVM */
  47. void __iomem *xive_tima;
  48. EXPORT_SYMBOL_GPL(xive_tima);
  49. u32 xive_tima_offset;
  50. /* Backend ops */
  51. static const struct xive_ops *xive_ops;
  52. /* Our global interrupt domain */
  53. static struct irq_domain *xive_irq_domain;
  54. #ifdef CONFIG_SMP
  55. /* The IPIs all use the same logical irq number */
  56. static u32 xive_ipi_irq;
  57. #endif
  58. /* Xive state for each CPU */
  59. static DEFINE_PER_CPU(struct xive_cpu *, xive_cpu);
  60. /*
  61. * A "disabled" interrupt should never fire, to catch problems
  62. * we set its logical number to this
  63. */
  64. #define XIVE_BAD_IRQ 0x7fffffff
  65. #define XIVE_MAX_IRQ (XIVE_BAD_IRQ - 1)
  66. /* An invalid CPU target */
  67. #define XIVE_INVALID_TARGET (-1)
  68. /*
  69. * Read the next entry in a queue, return its content if it's valid
  70. * or 0 if there is no new entry.
  71. *
  72. * The queue pointer is moved forward unless "just_peek" is set
  73. */
  74. static u32 xive_read_eq(struct xive_q *q, bool just_peek)
  75. {
  76. u32 cur;
  77. if (!q->qpage)
  78. return 0;
  79. cur = be32_to_cpup(q->qpage + q->idx);
  80. /* Check valid bit (31) vs current toggle polarity */
  81. if ((cur >> 31) == q->toggle)
  82. return 0;
  83. /* If consuming from the queue ... */
  84. if (!just_peek) {
  85. /* Next entry */
  86. q->idx = (q->idx + 1) & q->msk;
  87. /* Wrap around: flip valid toggle */
  88. if (q->idx == 0)
  89. q->toggle ^= 1;
  90. }
  91. /* Mask out the valid bit (31) */
  92. return cur & 0x7fffffff;
  93. }
  94. /*
  95. * Scans all the queue that may have interrupts in them
  96. * (based on "pending_prio") in priority order until an
  97. * interrupt is found or all the queues are empty.
  98. *
  99. * Then updates the CPPR (Current Processor Priority
  100. * Register) based on the most favored interrupt found
  101. * (0xff if none) and return what was found (0 if none).
  102. *
  103. * If just_peek is set, return the most favored pending
  104. * interrupt if any but don't update the queue pointers.
  105. *
  106. * Note: This function can operate generically on any number
  107. * of queues (up to 8). The current implementation of the XIVE
  108. * driver only uses a single queue however.
  109. *
  110. * Note2: This will also "flush" "the pending_count" of a queue
  111. * into the "count" when that queue is observed to be empty.
  112. * This is used to keep track of the amount of interrupts
  113. * targetting a queue. When an interrupt is moved away from
  114. * a queue, we only decrement that queue count once the queue
  115. * has been observed empty to avoid races.
  116. */
  117. static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek)
  118. {
  119. u32 irq = 0;
  120. u8 prio;
  121. /* Find highest pending priority */
  122. while (xc->pending_prio != 0) {
  123. struct xive_q *q;
  124. prio = ffs(xc->pending_prio) - 1;
  125. DBG_VERBOSE("scan_irq: trying prio %d\n", prio);
  126. /* Try to fetch */
  127. irq = xive_read_eq(&xc->queue[prio], just_peek);
  128. /* Found something ? That's it */
  129. if (irq)
  130. break;
  131. /* Clear pending bits */
  132. xc->pending_prio &= ~(1 << prio);
  133. /*
  134. * Check if the queue count needs adjusting due to
  135. * interrupts being moved away. See description of
  136. * xive_dec_target_count()
  137. */
  138. q = &xc->queue[prio];
  139. if (atomic_read(&q->pending_count)) {
  140. int p = atomic_xchg(&q->pending_count, 0);
  141. if (p) {
  142. WARN_ON(p > atomic_read(&q->count));
  143. atomic_sub(p, &q->count);
  144. }
  145. }
  146. }
  147. /* If nothing was found, set CPPR to 0xff */
  148. if (irq == 0)
  149. prio = 0xff;
  150. /* Update HW CPPR to match if necessary */
  151. if (prio != xc->cppr) {
  152. DBG_VERBOSE("scan_irq: adjusting CPPR to %d\n", prio);
  153. xc->cppr = prio;
  154. out_8(xive_tima + xive_tima_offset + TM_CPPR, prio);
  155. }
  156. return irq;
  157. }
  158. /*
  159. * This is used to perform the magic loads from an ESB
  160. * described in xive.h
  161. */
  162. static u8 xive_poke_esb(struct xive_irq_data *xd, u32 offset)
  163. {
  164. u64 val;
  165. /* Handle HW errata */
  166. if (xd->flags & XIVE_IRQ_FLAG_SHIFT_BUG)
  167. offset |= offset << 4;
  168. val = in_be64(xd->eoi_mmio + offset);
  169. return (u8)val;
  170. }
  171. #ifdef CONFIG_XMON
  172. static void xive_dump_eq(const char *name, struct xive_q *q)
  173. {
  174. u32 i0, i1, idx;
  175. if (!q->qpage)
  176. return;
  177. idx = q->idx;
  178. i0 = be32_to_cpup(q->qpage + idx);
  179. idx = (idx + 1) & q->msk;
  180. i1 = be32_to_cpup(q->qpage + idx);
  181. xmon_printf(" %s Q T=%d %08x %08x ...\n", name,
  182. q->toggle, i0, i1);
  183. }
  184. void xmon_xive_do_dump(int cpu)
  185. {
  186. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  187. xmon_printf("XIVE state for CPU %d:\n", cpu);
  188. xmon_printf(" pp=%02x cppr=%02x\n", xc->pending_prio, xc->cppr);
  189. xive_dump_eq("IRQ", &xc->queue[xive_irq_priority]);
  190. #ifdef CONFIG_SMP
  191. {
  192. u64 val = xive_poke_esb(&xc->ipi_data, XIVE_ESB_GET);
  193. xmon_printf(" IPI state: %x:%c%c\n", xc->hw_ipi,
  194. val & XIVE_ESB_VAL_P ? 'P' : 'p',
  195. val & XIVE_ESB_VAL_P ? 'Q' : 'q');
  196. }
  197. #endif
  198. }
  199. #endif /* CONFIG_XMON */
  200. static unsigned int xive_get_irq(void)
  201. {
  202. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  203. u32 irq;
  204. /*
  205. * This can be called either as a result of a HW interrupt or
  206. * as a "replay" because EOI decided there was still something
  207. * in one of the queues.
  208. *
  209. * First we perform an ACK cycle in order to update our mask
  210. * of pending priorities. This will also have the effect of
  211. * updating the CPPR to the most favored pending interrupts.
  212. *
  213. * In the future, if we have a way to differenciate a first
  214. * entry (on HW interrupt) from a replay triggered by EOI,
  215. * we could skip this on replays unless we soft-mask tells us
  216. * that a new HW interrupt occurred.
  217. */
  218. xive_ops->update_pending(xc);
  219. DBG_VERBOSE("get_irq: pending=%02x\n", xc->pending_prio);
  220. /* Scan our queue(s) for interrupts */
  221. irq = xive_scan_interrupts(xc, false);
  222. DBG_VERBOSE("get_irq: got irq 0x%x, new pending=0x%02x\n",
  223. irq, xc->pending_prio);
  224. /* Return pending interrupt if any */
  225. if (irq == XIVE_BAD_IRQ)
  226. return 0;
  227. return irq;
  228. }
  229. /*
  230. * After EOI'ing an interrupt, we need to re-check the queue
  231. * to see if another interrupt is pending since multiple
  232. * interrupts can coalesce into a single notification to the
  233. * CPU.
  234. *
  235. * If we find that there is indeed more in there, we call
  236. * force_external_irq_replay() to make Linux synthetize an
  237. * external interrupt on the next call to local_irq_restore().
  238. */
  239. static void xive_do_queue_eoi(struct xive_cpu *xc)
  240. {
  241. if (xive_scan_interrupts(xc, true) != 0) {
  242. DBG_VERBOSE("eoi: pending=0x%02x\n", xc->pending_prio);
  243. force_external_irq_replay();
  244. }
  245. }
  246. /*
  247. * EOI an interrupt at the source. There are several methods
  248. * to do this depending on the HW version and source type
  249. */
  250. void xive_do_source_eoi(u32 hw_irq, struct xive_irq_data *xd)
  251. {
  252. /* If the XIVE supports the new "store EOI facility, use it */
  253. if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
  254. out_be64(xd->eoi_mmio + XIVE_ESB_STORE_EOI, 0);
  255. else if (hw_irq && xd->flags & XIVE_IRQ_FLAG_EOI_FW) {
  256. /*
  257. * The FW told us to call it. This happens for some
  258. * interrupt sources that need additional HW whacking
  259. * beyond the ESB manipulation. For example LPC interrupts
  260. * on P9 DD1.0 need a latch to be clared in the LPC bridge
  261. * itself. The Firmware will take care of it.
  262. */
  263. if (WARN_ON_ONCE(!xive_ops->eoi))
  264. return;
  265. xive_ops->eoi(hw_irq);
  266. } else {
  267. u8 eoi_val;
  268. /*
  269. * Otherwise for EOI, we use the special MMIO that does
  270. * a clear of both P and Q and returns the old Q,
  271. * except for LSIs where we use the "EOI cycle" special
  272. * load.
  273. *
  274. * This allows us to then do a re-trigger if Q was set
  275. * rather than synthesizing an interrupt in software
  276. *
  277. * For LSIs, using the HW EOI cycle works around a problem
  278. * on P9 DD1 PHBs where the other ESB accesses don't work
  279. * properly.
  280. */
  281. if (xd->flags & XIVE_IRQ_FLAG_LSI)
  282. in_be64(xd->eoi_mmio);
  283. else {
  284. eoi_val = xive_poke_esb(xd, XIVE_ESB_SET_PQ_00);
  285. DBG_VERBOSE("eoi_val=%x\n", offset, eoi_val);
  286. /* Re-trigger if needed */
  287. if ((eoi_val & XIVE_ESB_VAL_Q) && xd->trig_mmio)
  288. out_be64(xd->trig_mmio, 0);
  289. }
  290. }
  291. }
  292. /* irq_chip eoi callback */
  293. static void xive_irq_eoi(struct irq_data *d)
  294. {
  295. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  296. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  297. DBG_VERBOSE("eoi_irq: irq=%d [0x%lx] pending=%02x\n",
  298. d->irq, irqd_to_hwirq(d), xc->pending_prio);
  299. /*
  300. * EOI the source if it hasn't been disabled and hasn't
  301. * been passed-through to a KVM guest
  302. */
  303. if (!irqd_irq_disabled(d) && !irqd_is_forwarded_to_vcpu(d))
  304. xive_do_source_eoi(irqd_to_hwirq(d), xd);
  305. /*
  306. * Clear saved_p to indicate that it's no longer occupying
  307. * a queue slot on the target queue
  308. */
  309. xd->saved_p = false;
  310. /* Check for more work in the queue */
  311. xive_do_queue_eoi(xc);
  312. }
  313. /*
  314. * Helper used to mask and unmask an interrupt source. This
  315. * is only called for normal interrupts that do not require
  316. * masking/unmasking via firmware.
  317. */
  318. static void xive_do_source_set_mask(struct xive_irq_data *xd,
  319. bool mask)
  320. {
  321. u64 val;
  322. /*
  323. * If the interrupt had P set, it may be in a queue.
  324. *
  325. * We need to make sure we don't re-enable it until it
  326. * has been fetched from that queue and EOId. We keep
  327. * a copy of that P state and use it to restore the
  328. * ESB accordingly on unmask.
  329. */
  330. if (mask) {
  331. val = xive_poke_esb(xd, XIVE_ESB_SET_PQ_01);
  332. xd->saved_p = !!(val & XIVE_ESB_VAL_P);
  333. } else if (xd->saved_p)
  334. xive_poke_esb(xd, XIVE_ESB_SET_PQ_10);
  335. else
  336. xive_poke_esb(xd, XIVE_ESB_SET_PQ_00);
  337. }
  338. /*
  339. * Try to chose "cpu" as a new interrupt target. Increments
  340. * the queue accounting for that target if it's not already
  341. * full.
  342. */
  343. static bool xive_try_pick_target(int cpu)
  344. {
  345. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  346. struct xive_q *q = &xc->queue[xive_irq_priority];
  347. int max;
  348. /*
  349. * Calculate max number of interrupts in that queue.
  350. *
  351. * We leave a gap of 1 just in case...
  352. */
  353. max = (q->msk + 1) - 1;
  354. return !!atomic_add_unless(&q->count, 1, max);
  355. }
  356. /*
  357. * Un-account an interrupt for a target CPU. We don't directly
  358. * decrement q->count since the interrupt might still be present
  359. * in the queue.
  360. *
  361. * Instead increment a separate counter "pending_count" which
  362. * will be substracted from "count" later when that CPU observes
  363. * the queue to be empty.
  364. */
  365. static void xive_dec_target_count(int cpu)
  366. {
  367. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  368. struct xive_q *q = &xc->queue[xive_irq_priority];
  369. if (unlikely(WARN_ON(cpu < 0 || !xc))) {
  370. pr_err("%s: cpu=%d xc=%p\n", __func__, cpu, xc);
  371. return;
  372. }
  373. /*
  374. * We increment the "pending count" which will be used
  375. * to decrement the target queue count whenever it's next
  376. * processed and found empty. This ensure that we don't
  377. * decrement while we still have the interrupt there
  378. * occupying a slot.
  379. */
  380. atomic_inc(&q->pending_count);
  381. }
  382. /* Find a tentative CPU target in a CPU mask */
  383. static int xive_find_target_in_mask(const struct cpumask *mask,
  384. unsigned int fuzz)
  385. {
  386. int cpu, first, num, i;
  387. /* Pick up a starting point CPU in the mask based on fuzz */
  388. num = cpumask_weight(mask);
  389. first = fuzz % num;
  390. /* Locate it */
  391. cpu = cpumask_first(mask);
  392. for (i = 0; i < first && cpu < nr_cpu_ids; i++)
  393. cpu = cpumask_next(cpu, mask);
  394. /* Sanity check */
  395. if (WARN_ON(cpu >= nr_cpu_ids))
  396. cpu = cpumask_first(cpu_online_mask);
  397. /* Remember first one to handle wrap-around */
  398. first = cpu;
  399. /*
  400. * Now go through the entire mask until we find a valid
  401. * target.
  402. */
  403. for (;;) {
  404. /*
  405. * We re-check online as the fallback case passes us
  406. * an untested affinity mask
  407. */
  408. if (cpu_online(cpu) && xive_try_pick_target(cpu))
  409. return cpu;
  410. cpu = cpumask_next(cpu, mask);
  411. if (cpu == first)
  412. break;
  413. /* Wrap around */
  414. if (cpu >= nr_cpu_ids)
  415. cpu = cpumask_first(mask);
  416. }
  417. return -1;
  418. }
  419. /*
  420. * Pick a target CPU for an interrupt. This is done at
  421. * startup or if the affinity is changed in a way that
  422. * invalidates the current target.
  423. */
  424. static int xive_pick_irq_target(struct irq_data *d,
  425. const struct cpumask *affinity)
  426. {
  427. static unsigned int fuzz;
  428. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  429. cpumask_var_t mask;
  430. int cpu = -1;
  431. /*
  432. * If we have chip IDs, first we try to build a mask of
  433. * CPUs matching the CPU and find a target in there
  434. */
  435. if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
  436. zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
  437. /* Build a mask of matching chip IDs */
  438. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  439. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  440. if (xc->chip_id == xd->src_chip)
  441. cpumask_set_cpu(cpu, mask);
  442. }
  443. /* Try to find a target */
  444. if (cpumask_empty(mask))
  445. cpu = -1;
  446. else
  447. cpu = xive_find_target_in_mask(mask, fuzz++);
  448. free_cpumask_var(mask);
  449. if (cpu >= 0)
  450. return cpu;
  451. fuzz--;
  452. }
  453. /* No chip IDs, fallback to using the affinity mask */
  454. return xive_find_target_in_mask(affinity, fuzz++);
  455. }
  456. static unsigned int xive_irq_startup(struct irq_data *d)
  457. {
  458. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  459. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  460. int target, rc;
  461. pr_devel("xive_irq_startup: irq %d [0x%x] data @%p\n",
  462. d->irq, hw_irq, d);
  463. #ifdef CONFIG_PCI_MSI
  464. /*
  465. * The generic MSI code returns with the interrupt disabled on the
  466. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  467. * at that level, so we do it here by hand.
  468. */
  469. if (irq_data_get_msi_desc(d))
  470. pci_msi_unmask_irq(d);
  471. #endif
  472. /* Pick a target */
  473. target = xive_pick_irq_target(d, irq_data_get_affinity_mask(d));
  474. if (target == XIVE_INVALID_TARGET) {
  475. /* Try again breaking affinity */
  476. target = xive_pick_irq_target(d, cpu_online_mask);
  477. if (target == XIVE_INVALID_TARGET)
  478. return -ENXIO;
  479. pr_warn("irq %d started with broken affinity\n", d->irq);
  480. }
  481. /* Sanity check */
  482. if (WARN_ON(target == XIVE_INVALID_TARGET ||
  483. target >= nr_cpu_ids))
  484. target = smp_processor_id();
  485. xd->target = target;
  486. /*
  487. * Configure the logical number to be the Linux IRQ number
  488. * and set the target queue
  489. */
  490. rc = xive_ops->configure_irq(hw_irq,
  491. get_hard_smp_processor_id(target),
  492. xive_irq_priority, d->irq);
  493. if (rc)
  494. return rc;
  495. /* Unmask the ESB */
  496. xive_do_source_set_mask(xd, false);
  497. return 0;
  498. }
  499. static void xive_irq_shutdown(struct irq_data *d)
  500. {
  501. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  502. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  503. pr_devel("xive_irq_shutdown: irq %d [0x%x] data @%p\n",
  504. d->irq, hw_irq, d);
  505. if (WARN_ON(xd->target == XIVE_INVALID_TARGET))
  506. return;
  507. /* Mask the interrupt at the source */
  508. xive_do_source_set_mask(xd, true);
  509. /*
  510. * The above may have set saved_p. We clear it otherwise it
  511. * will prevent re-enabling later on. It is ok to forget the
  512. * fact that the interrupt might be in a queue because we are
  513. * accounting that already in xive_dec_target_count() and will
  514. * be re-routing it to a new queue with proper accounting when
  515. * it's started up again
  516. */
  517. xd->saved_p = false;
  518. /*
  519. * Mask the interrupt in HW in the IVT/EAS and set the number
  520. * to be the "bad" IRQ number
  521. */
  522. xive_ops->configure_irq(hw_irq,
  523. get_hard_smp_processor_id(xd->target),
  524. 0xff, XIVE_BAD_IRQ);
  525. xive_dec_target_count(xd->target);
  526. xd->target = XIVE_INVALID_TARGET;
  527. }
  528. static void xive_irq_unmask(struct irq_data *d)
  529. {
  530. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  531. pr_devel("xive_irq_unmask: irq %d data @%p\n", d->irq, xd);
  532. /*
  533. * This is a workaround for PCI LSI problems on P9, for
  534. * these, we call FW to set the mask. The problems might
  535. * be fixed by P9 DD2.0, if that is the case, firmware
  536. * will no longer set that flag.
  537. */
  538. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
  539. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  540. xive_ops->configure_irq(hw_irq,
  541. get_hard_smp_processor_id(xd->target),
  542. xive_irq_priority, d->irq);
  543. return;
  544. }
  545. xive_do_source_set_mask(xd, false);
  546. }
  547. static void xive_irq_mask(struct irq_data *d)
  548. {
  549. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  550. pr_devel("xive_irq_mask: irq %d data @%p\n", d->irq, xd);
  551. /*
  552. * This is a workaround for PCI LSI problems on P9, for
  553. * these, we call OPAL to set the mask. The problems might
  554. * be fixed by P9 DD2.0, if that is the case, firmware
  555. * will no longer set that flag.
  556. */
  557. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
  558. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  559. xive_ops->configure_irq(hw_irq,
  560. get_hard_smp_processor_id(xd->target),
  561. 0xff, d->irq);
  562. return;
  563. }
  564. xive_do_source_set_mask(xd, true);
  565. }
  566. static int xive_irq_set_affinity(struct irq_data *d,
  567. const struct cpumask *cpumask,
  568. bool force)
  569. {
  570. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  571. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  572. u32 target, old_target;
  573. int rc = 0;
  574. pr_devel("xive_irq_set_affinity: irq %d\n", d->irq);
  575. /* Is this valid ? */
  576. if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids)
  577. return -EINVAL;
  578. /*
  579. * If existing target is already in the new mask, and is
  580. * online then do nothing.
  581. */
  582. if (xd->target != XIVE_INVALID_TARGET &&
  583. cpu_online(xd->target) &&
  584. cpumask_test_cpu(xd->target, cpumask))
  585. return IRQ_SET_MASK_OK;
  586. /* Pick a new target */
  587. target = xive_pick_irq_target(d, cpumask);
  588. /* No target found */
  589. if (target == XIVE_INVALID_TARGET)
  590. return -ENXIO;
  591. /* Sanity check */
  592. if (WARN_ON(target >= nr_cpu_ids))
  593. target = smp_processor_id();
  594. old_target = xd->target;
  595. /*
  596. * Only configure the irq if it's not currently passed-through to
  597. * a KVM guest
  598. */
  599. if (!irqd_is_forwarded_to_vcpu(d))
  600. rc = xive_ops->configure_irq(hw_irq,
  601. get_hard_smp_processor_id(target),
  602. xive_irq_priority, d->irq);
  603. if (rc < 0) {
  604. pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
  605. return rc;
  606. }
  607. pr_devel(" target: 0x%x\n", target);
  608. xd->target = target;
  609. /* Give up previous target */
  610. if (old_target != XIVE_INVALID_TARGET)
  611. xive_dec_target_count(old_target);
  612. return IRQ_SET_MASK_OK;
  613. }
  614. static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
  615. {
  616. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  617. /*
  618. * We only support these. This has really no effect other than setting
  619. * the corresponding descriptor bits mind you but those will in turn
  620. * affect the resend function when re-enabling an edge interrupt.
  621. *
  622. * Set set the default to edge as explained in map().
  623. */
  624. if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
  625. flow_type = IRQ_TYPE_EDGE_RISING;
  626. if (flow_type != IRQ_TYPE_EDGE_RISING &&
  627. flow_type != IRQ_TYPE_LEVEL_LOW)
  628. return -EINVAL;
  629. irqd_set_trigger_type(d, flow_type);
  630. /*
  631. * Double check it matches what the FW thinks
  632. *
  633. * NOTE: We don't know yet if the PAPR interface will provide
  634. * the LSI vs MSI information apart from the device-tree so
  635. * this check might have to move into an optional backend call
  636. * that is specific to the native backend
  637. */
  638. if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
  639. !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
  640. pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
  641. d->irq, (u32)irqd_to_hwirq(d),
  642. (flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
  643. (xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
  644. }
  645. return IRQ_SET_MASK_OK_NOCOPY;
  646. }
  647. static int xive_irq_retrigger(struct irq_data *d)
  648. {
  649. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  650. /* This should be only for MSIs */
  651. if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
  652. return 0;
  653. /*
  654. * To perform a retrigger, we first set the PQ bits to
  655. * 11, then perform an EOI.
  656. */
  657. xive_poke_esb(xd, XIVE_ESB_SET_PQ_11);
  658. /*
  659. * Note: We pass "0" to the hw_irq argument in order to
  660. * avoid calling into the backend EOI code which we don't
  661. * want to do in the case of a re-trigger. Backends typically
  662. * only do EOI for LSIs anyway.
  663. */
  664. xive_do_source_eoi(0, xd);
  665. return 1;
  666. }
  667. static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
  668. {
  669. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  670. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  671. int rc;
  672. u8 pq;
  673. /*
  674. * We only support this on interrupts that do not require
  675. * firmware calls for masking and unmasking
  676. */
  677. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW)
  678. return -EIO;
  679. /*
  680. * This is called by KVM with state non-NULL for enabling
  681. * pass-through or NULL for disabling it
  682. */
  683. if (state) {
  684. irqd_set_forwarded_to_vcpu(d);
  685. /* Set it to PQ=10 state to prevent further sends */
  686. pq = xive_poke_esb(xd, XIVE_ESB_SET_PQ_10);
  687. /* No target ? nothing to do */
  688. if (xd->target == XIVE_INVALID_TARGET) {
  689. /*
  690. * An untargetted interrupt should have been
  691. * also masked at the source
  692. */
  693. WARN_ON(pq & 2);
  694. return 0;
  695. }
  696. /*
  697. * If P was set, adjust state to PQ=11 to indicate
  698. * that a resend is needed for the interrupt to reach
  699. * the guest. Also remember the value of P.
  700. *
  701. * This also tells us that it's in flight to a host queue
  702. * or has already been fetched but hasn't been EOIed yet
  703. * by the host. This it's potentially using up a host
  704. * queue slot. This is important to know because as long
  705. * as this is the case, we must not hard-unmask it when
  706. * "returning" that interrupt to the host.
  707. *
  708. * This saved_p is cleared by the host EOI, when we know
  709. * for sure the queue slot is no longer in use.
  710. */
  711. if (pq & 2) {
  712. pq = xive_poke_esb(xd, XIVE_ESB_SET_PQ_11);
  713. xd->saved_p = true;
  714. /*
  715. * Sync the XIVE source HW to ensure the interrupt
  716. * has gone through the EAS before we change its
  717. * target to the guest. That should guarantee us
  718. * that we *will* eventually get an EOI for it on
  719. * the host. Otherwise there would be a small window
  720. * for P to be seen here but the interrupt going
  721. * to the guest queue.
  722. */
  723. if (xive_ops->sync_source)
  724. xive_ops->sync_source(hw_irq);
  725. } else
  726. xd->saved_p = false;
  727. } else {
  728. irqd_clr_forwarded_to_vcpu(d);
  729. /* No host target ? hard mask and return */
  730. if (xd->target == XIVE_INVALID_TARGET) {
  731. xive_do_source_set_mask(xd, true);
  732. return 0;
  733. }
  734. /*
  735. * Sync the XIVE source HW to ensure the interrupt
  736. * has gone through the EAS before we change its
  737. * target to the host.
  738. */
  739. if (xive_ops->sync_source)
  740. xive_ops->sync_source(hw_irq);
  741. /*
  742. * By convention we are called with the interrupt in
  743. * a PQ=10 or PQ=11 state, ie, it won't fire and will
  744. * have latched in Q whether there's a pending HW
  745. * interrupt or not.
  746. *
  747. * First reconfigure the target.
  748. */
  749. rc = xive_ops->configure_irq(hw_irq,
  750. get_hard_smp_processor_id(xd->target),
  751. xive_irq_priority, d->irq);
  752. if (rc)
  753. return rc;
  754. /*
  755. * Then if saved_p is not set, effectively re-enable the
  756. * interrupt with an EOI. If it is set, we know there is
  757. * still a message in a host queue somewhere that will be
  758. * EOId eventually.
  759. *
  760. * Note: We don't check irqd_irq_disabled(). Effectively,
  761. * we *will* let the irq get through even if masked if the
  762. * HW is still firing it in order to deal with the whole
  763. * saved_p business properly. If the interrupt triggers
  764. * while masked, the generic code will re-mask it anyway.
  765. */
  766. if (!xd->saved_p)
  767. xive_do_source_eoi(hw_irq, xd);
  768. }
  769. return 0;
  770. }
  771. static struct irq_chip xive_irq_chip = {
  772. .name = "XIVE-IRQ",
  773. .irq_startup = xive_irq_startup,
  774. .irq_shutdown = xive_irq_shutdown,
  775. .irq_eoi = xive_irq_eoi,
  776. .irq_mask = xive_irq_mask,
  777. .irq_unmask = xive_irq_unmask,
  778. .irq_set_affinity = xive_irq_set_affinity,
  779. .irq_set_type = xive_irq_set_type,
  780. .irq_retrigger = xive_irq_retrigger,
  781. .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
  782. };
  783. bool is_xive_irq(struct irq_chip *chip)
  784. {
  785. return chip == &xive_irq_chip;
  786. }
  787. EXPORT_SYMBOL_GPL(is_xive_irq);
  788. void xive_cleanup_irq_data(struct xive_irq_data *xd)
  789. {
  790. if (xd->eoi_mmio) {
  791. iounmap(xd->eoi_mmio);
  792. if (xd->eoi_mmio == xd->trig_mmio)
  793. xd->trig_mmio = NULL;
  794. xd->eoi_mmio = NULL;
  795. }
  796. if (xd->trig_mmio) {
  797. iounmap(xd->trig_mmio);
  798. xd->trig_mmio = NULL;
  799. }
  800. }
  801. EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
  802. static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
  803. {
  804. struct xive_irq_data *xd;
  805. int rc;
  806. xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
  807. if (!xd)
  808. return -ENOMEM;
  809. rc = xive_ops->populate_irq_data(hw, xd);
  810. if (rc) {
  811. kfree(xd);
  812. return rc;
  813. }
  814. xd->target = XIVE_INVALID_TARGET;
  815. irq_set_handler_data(virq, xd);
  816. return 0;
  817. }
  818. static void xive_irq_free_data(unsigned int virq)
  819. {
  820. struct xive_irq_data *xd = irq_get_handler_data(virq);
  821. if (!xd)
  822. return;
  823. irq_set_handler_data(virq, NULL);
  824. xive_cleanup_irq_data(xd);
  825. kfree(xd);
  826. }
  827. #ifdef CONFIG_SMP
  828. static void xive_cause_ipi(int cpu)
  829. {
  830. struct xive_cpu *xc;
  831. struct xive_irq_data *xd;
  832. xc = per_cpu(xive_cpu, cpu);
  833. DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
  834. smp_processor_id(), cpu, xc->hw_ipi);
  835. xd = &xc->ipi_data;
  836. if (WARN_ON(!xd->trig_mmio))
  837. return;
  838. out_be64(xd->trig_mmio, 0);
  839. }
  840. static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
  841. {
  842. return smp_ipi_demux();
  843. }
  844. static void xive_ipi_eoi(struct irq_data *d)
  845. {
  846. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  847. /* Handle possible race with unplug and drop stale IPIs */
  848. if (!xc)
  849. return;
  850. xive_do_source_eoi(xc->hw_ipi, &xc->ipi_data);
  851. xive_do_queue_eoi(xc);
  852. }
  853. static void xive_ipi_do_nothing(struct irq_data *d)
  854. {
  855. /*
  856. * Nothing to do, we never mask/unmask IPIs, but the callback
  857. * has to exist for the struct irq_chip.
  858. */
  859. }
  860. static struct irq_chip xive_ipi_chip = {
  861. .name = "XIVE-IPI",
  862. .irq_eoi = xive_ipi_eoi,
  863. .irq_mask = xive_ipi_do_nothing,
  864. .irq_unmask = xive_ipi_do_nothing,
  865. };
  866. static void __init xive_request_ipi(void)
  867. {
  868. unsigned int virq;
  869. /*
  870. * Initialization failed, move on, we might manage to
  871. * reach the point where we display our errors before
  872. * the system falls appart
  873. */
  874. if (!xive_irq_domain)
  875. return;
  876. /* Initialize it */
  877. virq = irq_create_mapping(xive_irq_domain, 0);
  878. xive_ipi_irq = virq;
  879. WARN_ON(request_irq(virq, xive_muxed_ipi_action,
  880. IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));
  881. }
  882. static int xive_setup_cpu_ipi(unsigned int cpu)
  883. {
  884. struct xive_cpu *xc;
  885. int rc;
  886. pr_debug("Setting up IPI for CPU %d\n", cpu);
  887. xc = per_cpu(xive_cpu, cpu);
  888. /* Check if we are already setup */
  889. if (xc->hw_ipi != 0)
  890. return 0;
  891. /* Grab an IPI from the backend, this will populate xc->hw_ipi */
  892. if (xive_ops->get_ipi(cpu, xc))
  893. return -EIO;
  894. /*
  895. * Populate the IRQ data in the xive_cpu structure and
  896. * configure the HW / enable the IPIs.
  897. */
  898. rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
  899. if (rc) {
  900. pr_err("Failed to populate IPI data on CPU %d\n", cpu);
  901. return -EIO;
  902. }
  903. rc = xive_ops->configure_irq(xc->hw_ipi,
  904. get_hard_smp_processor_id(cpu),
  905. xive_irq_priority, xive_ipi_irq);
  906. if (rc) {
  907. pr_err("Failed to map IPI CPU %d\n", cpu);
  908. return -EIO;
  909. }
  910. pr_devel("CPU %d HW IPI %x, virq %d, trig_mmio=%p\n", cpu,
  911. xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
  912. /* Unmask it */
  913. xive_do_source_set_mask(&xc->ipi_data, false);
  914. return 0;
  915. }
  916. static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
  917. {
  918. /* Disable the IPI and free the IRQ data */
  919. /* Already cleaned up ? */
  920. if (xc->hw_ipi == 0)
  921. return;
  922. /* Mask the IPI */
  923. xive_do_source_set_mask(&xc->ipi_data, true);
  924. /*
  925. * Note: We don't call xive_cleanup_irq_data() to free
  926. * the mappings as this is called from an IPI on kexec
  927. * which is not a safe environment to call iounmap()
  928. */
  929. /* Deconfigure/mask in the backend */
  930. xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
  931. 0xff, xive_ipi_irq);
  932. /* Free the IPIs in the backend */
  933. xive_ops->put_ipi(cpu, xc);
  934. }
  935. void __init xive_smp_probe(void)
  936. {
  937. smp_ops->cause_ipi = xive_cause_ipi;
  938. /* Register the IPI */
  939. xive_request_ipi();
  940. /* Allocate and setup IPI for the boot CPU */
  941. xive_setup_cpu_ipi(smp_processor_id());
  942. }
  943. #endif /* CONFIG_SMP */
  944. static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
  945. irq_hw_number_t hw)
  946. {
  947. int rc;
  948. /*
  949. * Mark interrupts as edge sensitive by default so that resend
  950. * actually works. Will fix that up below if needed.
  951. */
  952. irq_clear_status_flags(virq, IRQ_LEVEL);
  953. #ifdef CONFIG_SMP
  954. /* IPIs are special and come up with HW number 0 */
  955. if (hw == 0) {
  956. /*
  957. * IPIs are marked per-cpu. We use separate HW interrupts under
  958. * the hood but associated with the same "linux" interrupt
  959. */
  960. irq_set_chip_and_handler(virq, &xive_ipi_chip,
  961. handle_percpu_irq);
  962. return 0;
  963. }
  964. #endif
  965. rc = xive_irq_alloc_data(virq, hw);
  966. if (rc)
  967. return rc;
  968. irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
  969. return 0;
  970. }
  971. static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
  972. {
  973. struct irq_data *data = irq_get_irq_data(virq);
  974. unsigned int hw_irq;
  975. /* XXX Assign BAD number */
  976. if (!data)
  977. return;
  978. hw_irq = (unsigned int)irqd_to_hwirq(data);
  979. if (hw_irq)
  980. xive_irq_free_data(virq);
  981. }
  982. static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
  983. const u32 *intspec, unsigned int intsize,
  984. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  985. {
  986. *out_hwirq = intspec[0];
  987. /*
  988. * If intsize is at least 2, we look for the type in the second cell,
  989. * we assume the LSB indicates a level interrupt.
  990. */
  991. if (intsize > 1) {
  992. if (intspec[1] & 1)
  993. *out_flags = IRQ_TYPE_LEVEL_LOW;
  994. else
  995. *out_flags = IRQ_TYPE_EDGE_RISING;
  996. } else
  997. *out_flags = IRQ_TYPE_LEVEL_LOW;
  998. return 0;
  999. }
  1000. static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
  1001. enum irq_domain_bus_token bus_token)
  1002. {
  1003. return xive_ops->match(node);
  1004. }
  1005. static const struct irq_domain_ops xive_irq_domain_ops = {
  1006. .match = xive_irq_domain_match,
  1007. .map = xive_irq_domain_map,
  1008. .unmap = xive_irq_domain_unmap,
  1009. .xlate = xive_irq_domain_xlate,
  1010. };
  1011. static void __init xive_init_host(void)
  1012. {
  1013. xive_irq_domain = irq_domain_add_nomap(NULL, XIVE_MAX_IRQ,
  1014. &xive_irq_domain_ops, NULL);
  1015. if (WARN_ON(xive_irq_domain == NULL))
  1016. return;
  1017. irq_set_default_host(xive_irq_domain);
  1018. }
  1019. static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1020. {
  1021. if (xc->queue[xive_irq_priority].qpage)
  1022. xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
  1023. }
  1024. static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1025. {
  1026. int rc = 0;
  1027. /* We setup 1 queues for now with a 64k page */
  1028. if (!xc->queue[xive_irq_priority].qpage)
  1029. rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
  1030. return rc;
  1031. }
  1032. static int xive_prepare_cpu(unsigned int cpu)
  1033. {
  1034. struct xive_cpu *xc;
  1035. xc = per_cpu(xive_cpu, cpu);
  1036. if (!xc) {
  1037. struct device_node *np;
  1038. xc = kzalloc_node(sizeof(struct xive_cpu),
  1039. GFP_KERNEL, cpu_to_node(cpu));
  1040. if (!xc)
  1041. return -ENOMEM;
  1042. np = of_get_cpu_node(cpu, NULL);
  1043. if (np)
  1044. xc->chip_id = of_get_ibm_chip_id(np);
  1045. of_node_put(np);
  1046. per_cpu(xive_cpu, cpu) = xc;
  1047. }
  1048. /* Setup EQs if not already */
  1049. return xive_setup_cpu_queues(cpu, xc);
  1050. }
  1051. static void xive_setup_cpu(void)
  1052. {
  1053. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1054. /* Debug: Dump the TM state */
  1055. pr_devel("CPU %d [HW 0x%02x] VT=%02x\n",
  1056. smp_processor_id(), hard_smp_processor_id(),
  1057. in_8(xive_tima + xive_tima_offset + TM_WORD2));
  1058. /* The backend might have additional things to do */
  1059. if (xive_ops->setup_cpu)
  1060. xive_ops->setup_cpu(smp_processor_id(), xc);
  1061. /* Set CPPR to 0xff to enable flow of interrupts */
  1062. xc->cppr = 0xff;
  1063. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1064. }
  1065. #ifdef CONFIG_SMP
  1066. void xive_smp_setup_cpu(void)
  1067. {
  1068. pr_devel("SMP setup CPU %d\n", smp_processor_id());
  1069. /* This will have already been done on the boot CPU */
  1070. if (smp_processor_id() != boot_cpuid)
  1071. xive_setup_cpu();
  1072. }
  1073. int xive_smp_prepare_cpu(unsigned int cpu)
  1074. {
  1075. int rc;
  1076. /* Allocate per-CPU data and queues */
  1077. rc = xive_prepare_cpu(cpu);
  1078. if (rc)
  1079. return rc;
  1080. /* Allocate and setup IPI for the new CPU */
  1081. return xive_setup_cpu_ipi(cpu);
  1082. }
  1083. #ifdef CONFIG_HOTPLUG_CPU
  1084. static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
  1085. {
  1086. u32 irq;
  1087. /* We assume local irqs are disabled */
  1088. WARN_ON(!irqs_disabled());
  1089. /* Check what's already in the CPU queue */
  1090. while ((irq = xive_scan_interrupts(xc, false)) != 0) {
  1091. /*
  1092. * We need to re-route that interrupt to its new destination.
  1093. * First get and lock the descriptor
  1094. */
  1095. struct irq_desc *desc = irq_to_desc(irq);
  1096. struct irq_data *d = irq_desc_get_irq_data(desc);
  1097. struct xive_irq_data *xd;
  1098. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1099. /*
  1100. * Ignore anything that isn't a XIVE irq and ignore
  1101. * IPIs, so can just be dropped.
  1102. */
  1103. if (d->domain != xive_irq_domain || hw_irq == 0)
  1104. continue;
  1105. /*
  1106. * The IRQ should have already been re-routed, it's just a
  1107. * stale in the old queue, so re-trigger it in order to make
  1108. * it reach is new destination.
  1109. */
  1110. #ifdef DEBUG_FLUSH
  1111. pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
  1112. cpu, irq);
  1113. #endif
  1114. raw_spin_lock(&desc->lock);
  1115. xd = irq_desc_get_handler_data(desc);
  1116. /*
  1117. * For LSIs, we EOI, this will cause a resend if it's
  1118. * still asserted. Otherwise do an MSI retrigger.
  1119. */
  1120. if (xd->flags & XIVE_IRQ_FLAG_LSI)
  1121. xive_do_source_eoi(irqd_to_hwirq(d), xd);
  1122. else
  1123. xive_irq_retrigger(d);
  1124. raw_spin_unlock(&desc->lock);
  1125. }
  1126. }
  1127. void xive_smp_disable_cpu(void)
  1128. {
  1129. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1130. unsigned int cpu = smp_processor_id();
  1131. /* Migrate interrupts away from the CPU */
  1132. irq_migrate_all_off_this_cpu();
  1133. /* Set CPPR to 0 to disable flow of interrupts */
  1134. xc->cppr = 0;
  1135. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1136. /* Flush everything still in the queue */
  1137. xive_flush_cpu_queue(cpu, xc);
  1138. /* Re-enable CPPR */
  1139. xc->cppr = 0xff;
  1140. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1141. }
  1142. void xive_flush_interrupt(void)
  1143. {
  1144. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1145. unsigned int cpu = smp_processor_id();
  1146. /* Called if an interrupt occurs while the CPU is hot unplugged */
  1147. xive_flush_cpu_queue(cpu, xc);
  1148. }
  1149. #endif /* CONFIG_HOTPLUG_CPU */
  1150. #endif /* CONFIG_SMP */
  1151. void xive_kexec_teardown_cpu(int secondary)
  1152. {
  1153. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1154. unsigned int cpu = smp_processor_id();
  1155. /* Set CPPR to 0 to disable flow of interrupts */
  1156. xc->cppr = 0;
  1157. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1158. /* Backend cleanup if any */
  1159. if (xive_ops->teardown_cpu)
  1160. xive_ops->teardown_cpu(cpu, xc);
  1161. #ifdef CONFIG_SMP
  1162. /* Get rid of IPI */
  1163. xive_cleanup_cpu_ipi(cpu, xc);
  1164. #endif
  1165. /* Disable and free the queues */
  1166. xive_cleanup_cpu_queues(cpu, xc);
  1167. }
  1168. void xive_shutdown(void)
  1169. {
  1170. xive_ops->shutdown();
  1171. }
  1172. bool xive_core_init(const struct xive_ops *ops, void __iomem *area, u32 offset,
  1173. u8 max_prio)
  1174. {
  1175. xive_tima = area;
  1176. xive_tima_offset = offset;
  1177. xive_ops = ops;
  1178. xive_irq_priority = max_prio;
  1179. ppc_md.get_irq = xive_get_irq;
  1180. __xive_enabled = true;
  1181. pr_devel("Initializing host..\n");
  1182. xive_init_host();
  1183. pr_devel("Initializing boot CPU..\n");
  1184. /* Allocate per-CPU data and queues */
  1185. xive_prepare_cpu(smp_processor_id());
  1186. /* Get ready for interrupts */
  1187. xive_setup_cpu();
  1188. pr_info("Interrupt handling initialized with %s backend\n",
  1189. xive_ops->name);
  1190. pr_info("Using priority %d for all interrupts\n", max_prio);
  1191. return true;
  1192. }
  1193. static int __init xive_off(char *arg)
  1194. {
  1195. xive_cmdline_disabled = true;
  1196. return 0;
  1197. }
  1198. __setup("xive=off", xive_off);