common.c 37 KB

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