common.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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 notrace u8 xive_esb_read(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 notrace 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. notrace 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_esb_read(&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. xive_esb_read(xd, XIVE_ESB_LOAD_EOI);
  283. else {
  284. eoi_val = xive_esb_read(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_esb_read(xd, XIVE_ESB_SET_PQ_01);
  332. xd->saved_p = !!(val & XIVE_ESB_VAL_P);
  333. } else if (xd->saved_p)
  334. xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
  335. else
  336. xive_esb_read(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 = min_t(int, cpumask_weight(mask), nr_cpu_ids);
  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. /* Don't do anything if the interrupt isn't started */
  579. if (!irqd_is_started(d))
  580. return IRQ_SET_MASK_OK;
  581. /*
  582. * If existing target is already in the new mask, and is
  583. * online then do nothing.
  584. */
  585. if (xd->target != XIVE_INVALID_TARGET &&
  586. cpu_online(xd->target) &&
  587. cpumask_test_cpu(xd->target, cpumask))
  588. return IRQ_SET_MASK_OK;
  589. /* Pick a new target */
  590. target = xive_pick_irq_target(d, cpumask);
  591. /* No target found */
  592. if (target == XIVE_INVALID_TARGET)
  593. return -ENXIO;
  594. /* Sanity check */
  595. if (WARN_ON(target >= nr_cpu_ids))
  596. target = smp_processor_id();
  597. old_target = xd->target;
  598. /*
  599. * Only configure the irq if it's not currently passed-through to
  600. * a KVM guest
  601. */
  602. if (!irqd_is_forwarded_to_vcpu(d))
  603. rc = xive_ops->configure_irq(hw_irq,
  604. get_hard_smp_processor_id(target),
  605. xive_irq_priority, d->irq);
  606. if (rc < 0) {
  607. pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
  608. return rc;
  609. }
  610. pr_devel(" target: 0x%x\n", target);
  611. xd->target = target;
  612. /* Give up previous target */
  613. if (old_target != XIVE_INVALID_TARGET)
  614. xive_dec_target_count(old_target);
  615. return IRQ_SET_MASK_OK;
  616. }
  617. static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
  618. {
  619. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  620. /*
  621. * We only support these. This has really no effect other than setting
  622. * the corresponding descriptor bits mind you but those will in turn
  623. * affect the resend function when re-enabling an edge interrupt.
  624. *
  625. * Set set the default to edge as explained in map().
  626. */
  627. if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
  628. flow_type = IRQ_TYPE_EDGE_RISING;
  629. if (flow_type != IRQ_TYPE_EDGE_RISING &&
  630. flow_type != IRQ_TYPE_LEVEL_LOW)
  631. return -EINVAL;
  632. irqd_set_trigger_type(d, flow_type);
  633. /*
  634. * Double check it matches what the FW thinks
  635. *
  636. * NOTE: We don't know yet if the PAPR interface will provide
  637. * the LSI vs MSI information apart from the device-tree so
  638. * this check might have to move into an optional backend call
  639. * that is specific to the native backend
  640. */
  641. if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
  642. !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
  643. pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
  644. d->irq, (u32)irqd_to_hwirq(d),
  645. (flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
  646. (xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
  647. }
  648. return IRQ_SET_MASK_OK_NOCOPY;
  649. }
  650. static int xive_irq_retrigger(struct irq_data *d)
  651. {
  652. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  653. /* This should be only for MSIs */
  654. if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
  655. return 0;
  656. /*
  657. * To perform a retrigger, we first set the PQ bits to
  658. * 11, then perform an EOI.
  659. */
  660. xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
  661. /*
  662. * Note: We pass "0" to the hw_irq argument in order to
  663. * avoid calling into the backend EOI code which we don't
  664. * want to do in the case of a re-trigger. Backends typically
  665. * only do EOI for LSIs anyway.
  666. */
  667. xive_do_source_eoi(0, xd);
  668. return 1;
  669. }
  670. static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
  671. {
  672. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  673. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  674. int rc;
  675. u8 pq;
  676. /*
  677. * We only support this on interrupts that do not require
  678. * firmware calls for masking and unmasking
  679. */
  680. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW)
  681. return -EIO;
  682. /*
  683. * This is called by KVM with state non-NULL for enabling
  684. * pass-through or NULL for disabling it
  685. */
  686. if (state) {
  687. irqd_set_forwarded_to_vcpu(d);
  688. /* Set it to PQ=10 state to prevent further sends */
  689. pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
  690. /* No target ? nothing to do */
  691. if (xd->target == XIVE_INVALID_TARGET) {
  692. /*
  693. * An untargetted interrupt should have been
  694. * also masked at the source
  695. */
  696. WARN_ON(pq & 2);
  697. return 0;
  698. }
  699. /*
  700. * If P was set, adjust state to PQ=11 to indicate
  701. * that a resend is needed for the interrupt to reach
  702. * the guest. Also remember the value of P.
  703. *
  704. * This also tells us that it's in flight to a host queue
  705. * or has already been fetched but hasn't been EOIed yet
  706. * by the host. This it's potentially using up a host
  707. * queue slot. This is important to know because as long
  708. * as this is the case, we must not hard-unmask it when
  709. * "returning" that interrupt to the host.
  710. *
  711. * This saved_p is cleared by the host EOI, when we know
  712. * for sure the queue slot is no longer in use.
  713. */
  714. if (pq & 2) {
  715. pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
  716. xd->saved_p = true;
  717. /*
  718. * Sync the XIVE source HW to ensure the interrupt
  719. * has gone through the EAS before we change its
  720. * target to the guest. That should guarantee us
  721. * that we *will* eventually get an EOI for it on
  722. * the host. Otherwise there would be a small window
  723. * for P to be seen here but the interrupt going
  724. * to the guest queue.
  725. */
  726. if (xive_ops->sync_source)
  727. xive_ops->sync_source(hw_irq);
  728. } else
  729. xd->saved_p = false;
  730. } else {
  731. irqd_clr_forwarded_to_vcpu(d);
  732. /* No host target ? hard mask and return */
  733. if (xd->target == XIVE_INVALID_TARGET) {
  734. xive_do_source_set_mask(xd, true);
  735. return 0;
  736. }
  737. /*
  738. * Sync the XIVE source HW to ensure the interrupt
  739. * has gone through the EAS before we change its
  740. * target to the host.
  741. */
  742. if (xive_ops->sync_source)
  743. xive_ops->sync_source(hw_irq);
  744. /*
  745. * By convention we are called with the interrupt in
  746. * a PQ=10 or PQ=11 state, ie, it won't fire and will
  747. * have latched in Q whether there's a pending HW
  748. * interrupt or not.
  749. *
  750. * First reconfigure the target.
  751. */
  752. rc = xive_ops->configure_irq(hw_irq,
  753. get_hard_smp_processor_id(xd->target),
  754. xive_irq_priority, d->irq);
  755. if (rc)
  756. return rc;
  757. /*
  758. * Then if saved_p is not set, effectively re-enable the
  759. * interrupt with an EOI. If it is set, we know there is
  760. * still a message in a host queue somewhere that will be
  761. * EOId eventually.
  762. *
  763. * Note: We don't check irqd_irq_disabled(). Effectively,
  764. * we *will* let the irq get through even if masked if the
  765. * HW is still firing it in order to deal with the whole
  766. * saved_p business properly. If the interrupt triggers
  767. * while masked, the generic code will re-mask it anyway.
  768. */
  769. if (!xd->saved_p)
  770. xive_do_source_eoi(hw_irq, xd);
  771. }
  772. return 0;
  773. }
  774. static struct irq_chip xive_irq_chip = {
  775. .name = "XIVE-IRQ",
  776. .irq_startup = xive_irq_startup,
  777. .irq_shutdown = xive_irq_shutdown,
  778. .irq_eoi = xive_irq_eoi,
  779. .irq_mask = xive_irq_mask,
  780. .irq_unmask = xive_irq_unmask,
  781. .irq_set_affinity = xive_irq_set_affinity,
  782. .irq_set_type = xive_irq_set_type,
  783. .irq_retrigger = xive_irq_retrigger,
  784. .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
  785. };
  786. bool is_xive_irq(struct irq_chip *chip)
  787. {
  788. return chip == &xive_irq_chip;
  789. }
  790. EXPORT_SYMBOL_GPL(is_xive_irq);
  791. void xive_cleanup_irq_data(struct xive_irq_data *xd)
  792. {
  793. if (xd->eoi_mmio) {
  794. iounmap(xd->eoi_mmio);
  795. if (xd->eoi_mmio == xd->trig_mmio)
  796. xd->trig_mmio = NULL;
  797. xd->eoi_mmio = NULL;
  798. }
  799. if (xd->trig_mmio) {
  800. iounmap(xd->trig_mmio);
  801. xd->trig_mmio = NULL;
  802. }
  803. }
  804. EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
  805. static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
  806. {
  807. struct xive_irq_data *xd;
  808. int rc;
  809. xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
  810. if (!xd)
  811. return -ENOMEM;
  812. rc = xive_ops->populate_irq_data(hw, xd);
  813. if (rc) {
  814. kfree(xd);
  815. return rc;
  816. }
  817. xd->target = XIVE_INVALID_TARGET;
  818. irq_set_handler_data(virq, xd);
  819. return 0;
  820. }
  821. static void xive_irq_free_data(unsigned int virq)
  822. {
  823. struct xive_irq_data *xd = irq_get_handler_data(virq);
  824. if (!xd)
  825. return;
  826. irq_set_handler_data(virq, NULL);
  827. xive_cleanup_irq_data(xd);
  828. kfree(xd);
  829. }
  830. #ifdef CONFIG_SMP
  831. static void xive_cause_ipi(int cpu)
  832. {
  833. struct xive_cpu *xc;
  834. struct xive_irq_data *xd;
  835. xc = per_cpu(xive_cpu, cpu);
  836. DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
  837. smp_processor_id(), cpu, xc->hw_ipi);
  838. xd = &xc->ipi_data;
  839. if (WARN_ON(!xd->trig_mmio))
  840. return;
  841. out_be64(xd->trig_mmio, 0);
  842. }
  843. static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
  844. {
  845. return smp_ipi_demux();
  846. }
  847. static void xive_ipi_eoi(struct irq_data *d)
  848. {
  849. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  850. /* Handle possible race with unplug and drop stale IPIs */
  851. if (!xc)
  852. return;
  853. xive_do_source_eoi(xc->hw_ipi, &xc->ipi_data);
  854. xive_do_queue_eoi(xc);
  855. }
  856. static void xive_ipi_do_nothing(struct irq_data *d)
  857. {
  858. /*
  859. * Nothing to do, we never mask/unmask IPIs, but the callback
  860. * has to exist for the struct irq_chip.
  861. */
  862. }
  863. static struct irq_chip xive_ipi_chip = {
  864. .name = "XIVE-IPI",
  865. .irq_eoi = xive_ipi_eoi,
  866. .irq_mask = xive_ipi_do_nothing,
  867. .irq_unmask = xive_ipi_do_nothing,
  868. };
  869. static void __init xive_request_ipi(void)
  870. {
  871. unsigned int virq;
  872. /*
  873. * Initialization failed, move on, we might manage to
  874. * reach the point where we display our errors before
  875. * the system falls appart
  876. */
  877. if (!xive_irq_domain)
  878. return;
  879. /* Initialize it */
  880. virq = irq_create_mapping(xive_irq_domain, 0);
  881. xive_ipi_irq = virq;
  882. WARN_ON(request_irq(virq, xive_muxed_ipi_action,
  883. IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));
  884. }
  885. static int xive_setup_cpu_ipi(unsigned int cpu)
  886. {
  887. struct xive_cpu *xc;
  888. int rc;
  889. pr_debug("Setting up IPI for CPU %d\n", cpu);
  890. xc = per_cpu(xive_cpu, cpu);
  891. /* Check if we are already setup */
  892. if (xc->hw_ipi != 0)
  893. return 0;
  894. /* Grab an IPI from the backend, this will populate xc->hw_ipi */
  895. if (xive_ops->get_ipi(cpu, xc))
  896. return -EIO;
  897. /*
  898. * Populate the IRQ data in the xive_cpu structure and
  899. * configure the HW / enable the IPIs.
  900. */
  901. rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
  902. if (rc) {
  903. pr_err("Failed to populate IPI data on CPU %d\n", cpu);
  904. return -EIO;
  905. }
  906. rc = xive_ops->configure_irq(xc->hw_ipi,
  907. get_hard_smp_processor_id(cpu),
  908. xive_irq_priority, xive_ipi_irq);
  909. if (rc) {
  910. pr_err("Failed to map IPI CPU %d\n", cpu);
  911. return -EIO;
  912. }
  913. pr_devel("CPU %d HW IPI %x, virq %d, trig_mmio=%p\n", cpu,
  914. xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
  915. /* Unmask it */
  916. xive_do_source_set_mask(&xc->ipi_data, false);
  917. return 0;
  918. }
  919. static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
  920. {
  921. /* Disable the IPI and free the IRQ data */
  922. /* Already cleaned up ? */
  923. if (xc->hw_ipi == 0)
  924. return;
  925. /* Mask the IPI */
  926. xive_do_source_set_mask(&xc->ipi_data, true);
  927. /*
  928. * Note: We don't call xive_cleanup_irq_data() to free
  929. * the mappings as this is called from an IPI on kexec
  930. * which is not a safe environment to call iounmap()
  931. */
  932. /* Deconfigure/mask in the backend */
  933. xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
  934. 0xff, xive_ipi_irq);
  935. /* Free the IPIs in the backend */
  936. xive_ops->put_ipi(cpu, xc);
  937. }
  938. void __init xive_smp_probe(void)
  939. {
  940. smp_ops->cause_ipi = xive_cause_ipi;
  941. /* Register the IPI */
  942. xive_request_ipi();
  943. /* Allocate and setup IPI for the boot CPU */
  944. xive_setup_cpu_ipi(smp_processor_id());
  945. }
  946. #endif /* CONFIG_SMP */
  947. static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
  948. irq_hw_number_t hw)
  949. {
  950. int rc;
  951. /*
  952. * Mark interrupts as edge sensitive by default so that resend
  953. * actually works. Will fix that up below if needed.
  954. */
  955. irq_clear_status_flags(virq, IRQ_LEVEL);
  956. #ifdef CONFIG_SMP
  957. /* IPIs are special and come up with HW number 0 */
  958. if (hw == 0) {
  959. /*
  960. * IPIs are marked per-cpu. We use separate HW interrupts under
  961. * the hood but associated with the same "linux" interrupt
  962. */
  963. irq_set_chip_and_handler(virq, &xive_ipi_chip,
  964. handle_percpu_irq);
  965. return 0;
  966. }
  967. #endif
  968. rc = xive_irq_alloc_data(virq, hw);
  969. if (rc)
  970. return rc;
  971. irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
  972. return 0;
  973. }
  974. static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
  975. {
  976. struct irq_data *data = irq_get_irq_data(virq);
  977. unsigned int hw_irq;
  978. /* XXX Assign BAD number */
  979. if (!data)
  980. return;
  981. hw_irq = (unsigned int)irqd_to_hwirq(data);
  982. if (hw_irq)
  983. xive_irq_free_data(virq);
  984. }
  985. static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
  986. const u32 *intspec, unsigned int intsize,
  987. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  988. {
  989. *out_hwirq = intspec[0];
  990. /*
  991. * If intsize is at least 2, we look for the type in the second cell,
  992. * we assume the LSB indicates a level interrupt.
  993. */
  994. if (intsize > 1) {
  995. if (intspec[1] & 1)
  996. *out_flags = IRQ_TYPE_LEVEL_LOW;
  997. else
  998. *out_flags = IRQ_TYPE_EDGE_RISING;
  999. } else
  1000. *out_flags = IRQ_TYPE_LEVEL_LOW;
  1001. return 0;
  1002. }
  1003. static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
  1004. enum irq_domain_bus_token bus_token)
  1005. {
  1006. return xive_ops->match(node);
  1007. }
  1008. static const struct irq_domain_ops xive_irq_domain_ops = {
  1009. .match = xive_irq_domain_match,
  1010. .map = xive_irq_domain_map,
  1011. .unmap = xive_irq_domain_unmap,
  1012. .xlate = xive_irq_domain_xlate,
  1013. };
  1014. static void __init xive_init_host(void)
  1015. {
  1016. xive_irq_domain = irq_domain_add_nomap(NULL, XIVE_MAX_IRQ,
  1017. &xive_irq_domain_ops, NULL);
  1018. if (WARN_ON(xive_irq_domain == NULL))
  1019. return;
  1020. irq_set_default_host(xive_irq_domain);
  1021. }
  1022. static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1023. {
  1024. if (xc->queue[xive_irq_priority].qpage)
  1025. xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
  1026. }
  1027. static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1028. {
  1029. int rc = 0;
  1030. /* We setup 1 queues for now with a 64k page */
  1031. if (!xc->queue[xive_irq_priority].qpage)
  1032. rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
  1033. return rc;
  1034. }
  1035. static int xive_prepare_cpu(unsigned int cpu)
  1036. {
  1037. struct xive_cpu *xc;
  1038. xc = per_cpu(xive_cpu, cpu);
  1039. if (!xc) {
  1040. struct device_node *np;
  1041. xc = kzalloc_node(sizeof(struct xive_cpu),
  1042. GFP_KERNEL, cpu_to_node(cpu));
  1043. if (!xc)
  1044. return -ENOMEM;
  1045. np = of_get_cpu_node(cpu, NULL);
  1046. if (np)
  1047. xc->chip_id = of_get_ibm_chip_id(np);
  1048. of_node_put(np);
  1049. per_cpu(xive_cpu, cpu) = xc;
  1050. }
  1051. /* Setup EQs if not already */
  1052. return xive_setup_cpu_queues(cpu, xc);
  1053. }
  1054. static void xive_setup_cpu(void)
  1055. {
  1056. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1057. /* Debug: Dump the TM state */
  1058. pr_devel("CPU %d [HW 0x%02x] VT=%02x\n",
  1059. smp_processor_id(), hard_smp_processor_id(),
  1060. in_8(xive_tima + xive_tima_offset + TM_WORD2));
  1061. /* The backend might have additional things to do */
  1062. if (xive_ops->setup_cpu)
  1063. xive_ops->setup_cpu(smp_processor_id(), xc);
  1064. /* Set CPPR to 0xff to enable flow of interrupts */
  1065. xc->cppr = 0xff;
  1066. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1067. }
  1068. #ifdef CONFIG_SMP
  1069. void xive_smp_setup_cpu(void)
  1070. {
  1071. pr_devel("SMP setup CPU %d\n", smp_processor_id());
  1072. /* This will have already been done on the boot CPU */
  1073. if (smp_processor_id() != boot_cpuid)
  1074. xive_setup_cpu();
  1075. }
  1076. int xive_smp_prepare_cpu(unsigned int cpu)
  1077. {
  1078. int rc;
  1079. /* Allocate per-CPU data and queues */
  1080. rc = xive_prepare_cpu(cpu);
  1081. if (rc)
  1082. return rc;
  1083. /* Allocate and setup IPI for the new CPU */
  1084. return xive_setup_cpu_ipi(cpu);
  1085. }
  1086. #ifdef CONFIG_HOTPLUG_CPU
  1087. static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
  1088. {
  1089. u32 irq;
  1090. /* We assume local irqs are disabled */
  1091. WARN_ON(!irqs_disabled());
  1092. /* Check what's already in the CPU queue */
  1093. while ((irq = xive_scan_interrupts(xc, false)) != 0) {
  1094. /*
  1095. * We need to re-route that interrupt to its new destination.
  1096. * First get and lock the descriptor
  1097. */
  1098. struct irq_desc *desc = irq_to_desc(irq);
  1099. struct irq_data *d = irq_desc_get_irq_data(desc);
  1100. struct xive_irq_data *xd;
  1101. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1102. /*
  1103. * Ignore anything that isn't a XIVE irq and ignore
  1104. * IPIs, so can just be dropped.
  1105. */
  1106. if (d->domain != xive_irq_domain || hw_irq == 0)
  1107. continue;
  1108. /*
  1109. * The IRQ should have already been re-routed, it's just a
  1110. * stale in the old queue, so re-trigger it in order to make
  1111. * it reach is new destination.
  1112. */
  1113. #ifdef DEBUG_FLUSH
  1114. pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
  1115. cpu, irq);
  1116. #endif
  1117. raw_spin_lock(&desc->lock);
  1118. xd = irq_desc_get_handler_data(desc);
  1119. /*
  1120. * For LSIs, we EOI, this will cause a resend if it's
  1121. * still asserted. Otherwise do an MSI retrigger.
  1122. */
  1123. if (xd->flags & XIVE_IRQ_FLAG_LSI)
  1124. xive_do_source_eoi(irqd_to_hwirq(d), xd);
  1125. else
  1126. xive_irq_retrigger(d);
  1127. raw_spin_unlock(&desc->lock);
  1128. }
  1129. }
  1130. void xive_smp_disable_cpu(void)
  1131. {
  1132. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1133. unsigned int cpu = smp_processor_id();
  1134. /* Migrate interrupts away from the CPU */
  1135. irq_migrate_all_off_this_cpu();
  1136. /* Set CPPR to 0 to disable flow of interrupts */
  1137. xc->cppr = 0;
  1138. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1139. /* Flush everything still in the queue */
  1140. xive_flush_cpu_queue(cpu, xc);
  1141. /* Re-enable CPPR */
  1142. xc->cppr = 0xff;
  1143. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1144. }
  1145. void xive_flush_interrupt(void)
  1146. {
  1147. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1148. unsigned int cpu = smp_processor_id();
  1149. /* Called if an interrupt occurs while the CPU is hot unplugged */
  1150. xive_flush_cpu_queue(cpu, xc);
  1151. }
  1152. #endif /* CONFIG_HOTPLUG_CPU */
  1153. #endif /* CONFIG_SMP */
  1154. void xive_teardown_cpu(void)
  1155. {
  1156. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1157. unsigned int cpu = smp_processor_id();
  1158. /* Set CPPR to 0 to disable flow of interrupts */
  1159. xc->cppr = 0;
  1160. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1161. if (xive_ops->teardown_cpu)
  1162. xive_ops->teardown_cpu(cpu, xc);
  1163. }
  1164. void xive_kexec_teardown_cpu(int secondary)
  1165. {
  1166. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1167. unsigned int cpu = smp_processor_id();
  1168. /* Set CPPR to 0 to disable flow of interrupts */
  1169. xc->cppr = 0;
  1170. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1171. /* Backend cleanup if any */
  1172. if (xive_ops->teardown_cpu)
  1173. xive_ops->teardown_cpu(cpu, xc);
  1174. #ifdef CONFIG_SMP
  1175. /* Get rid of IPI */
  1176. xive_cleanup_cpu_ipi(cpu, xc);
  1177. #endif
  1178. /* Disable and free the queues */
  1179. xive_cleanup_cpu_queues(cpu, xc);
  1180. }
  1181. void xive_shutdown(void)
  1182. {
  1183. xive_ops->shutdown();
  1184. }
  1185. bool __init xive_core_init(const struct xive_ops *ops, void __iomem *area, u32 offset,
  1186. u8 max_prio)
  1187. {
  1188. xive_tima = area;
  1189. xive_tima_offset = offset;
  1190. xive_ops = ops;
  1191. xive_irq_priority = max_prio;
  1192. ppc_md.get_irq = xive_get_irq;
  1193. __xive_enabled = true;
  1194. pr_devel("Initializing host..\n");
  1195. xive_init_host();
  1196. pr_devel("Initializing boot CPU..\n");
  1197. /* Allocate per-CPU data and queues */
  1198. xive_prepare_cpu(smp_processor_id());
  1199. /* Get ready for interrupts */
  1200. xive_setup_cpu();
  1201. pr_info("Interrupt handling initialized with %s backend\n",
  1202. xive_ops->name);
  1203. pr_info("Using priority %d for all interrupts\n", max_prio);
  1204. return true;
  1205. }
  1206. __be32 *xive_queue_page_alloc(unsigned int cpu, u32 queue_shift)
  1207. {
  1208. unsigned int alloc_order;
  1209. struct page *pages;
  1210. __be32 *qpage;
  1211. alloc_order = xive_alloc_order(queue_shift);
  1212. pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
  1213. if (!pages)
  1214. return ERR_PTR(-ENOMEM);
  1215. qpage = (__be32 *)page_address(pages);
  1216. memset(qpage, 0, 1 << queue_shift);
  1217. return qpage;
  1218. }
  1219. static int __init xive_off(char *arg)
  1220. {
  1221. xive_cmdline_disabled = true;
  1222. return 0;
  1223. }
  1224. __setup("xive=off", xive_off);