common.c 37 KB

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