common.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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_Q ? '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 differentiate 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 needed 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 the HW EOI cycle is used rather than PQ bits,
  287. * as they are automatically re-triggred in HW when still
  288. * pending.
  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. !(xd->flags & XIVE_IRQ_NO_EOI))
  314. xive_do_source_eoi(irqd_to_hwirq(d), xd);
  315. /*
  316. * Clear saved_p to indicate that it's no longer occupying
  317. * a queue slot on the target queue
  318. */
  319. xd->saved_p = false;
  320. /* Check for more work in the queue */
  321. xive_do_queue_eoi(xc);
  322. }
  323. /*
  324. * Helper used to mask and unmask an interrupt source. This
  325. * is only called for normal interrupts that do not require
  326. * masking/unmasking via firmware.
  327. */
  328. static void xive_do_source_set_mask(struct xive_irq_data *xd,
  329. bool mask)
  330. {
  331. u64 val;
  332. /*
  333. * If the interrupt had P set, it may be in a queue.
  334. *
  335. * We need to make sure we don't re-enable it until it
  336. * has been fetched from that queue and EOId. We keep
  337. * a copy of that P state and use it to restore the
  338. * ESB accordingly on unmask.
  339. */
  340. if (mask) {
  341. val = xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
  342. xd->saved_p = !!(val & XIVE_ESB_VAL_P);
  343. } else if (xd->saved_p)
  344. xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
  345. else
  346. xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
  347. }
  348. /*
  349. * Try to chose "cpu" as a new interrupt target. Increments
  350. * the queue accounting for that target if it's not already
  351. * full.
  352. */
  353. static bool xive_try_pick_target(int cpu)
  354. {
  355. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  356. struct xive_q *q = &xc->queue[xive_irq_priority];
  357. int max;
  358. /*
  359. * Calculate max number of interrupts in that queue.
  360. *
  361. * We leave a gap of 1 just in case...
  362. */
  363. max = (q->msk + 1) - 1;
  364. return !!atomic_add_unless(&q->count, 1, max);
  365. }
  366. /*
  367. * Un-account an interrupt for a target CPU. We don't directly
  368. * decrement q->count since the interrupt might still be present
  369. * in the queue.
  370. *
  371. * Instead increment a separate counter "pending_count" which
  372. * will be substracted from "count" later when that CPU observes
  373. * the queue to be empty.
  374. */
  375. static void xive_dec_target_count(int cpu)
  376. {
  377. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  378. struct xive_q *q = &xc->queue[xive_irq_priority];
  379. if (unlikely(WARN_ON(cpu < 0 || !xc))) {
  380. pr_err("%s: cpu=%d xc=%p\n", __func__, cpu, xc);
  381. return;
  382. }
  383. /*
  384. * We increment the "pending count" which will be used
  385. * to decrement the target queue count whenever it's next
  386. * processed and found empty. This ensure that we don't
  387. * decrement while we still have the interrupt there
  388. * occupying a slot.
  389. */
  390. atomic_inc(&q->pending_count);
  391. }
  392. /* Find a tentative CPU target in a CPU mask */
  393. static int xive_find_target_in_mask(const struct cpumask *mask,
  394. unsigned int fuzz)
  395. {
  396. int cpu, first, num, i;
  397. /* Pick up a starting point CPU in the mask based on fuzz */
  398. num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
  399. first = fuzz % num;
  400. /* Locate it */
  401. cpu = cpumask_first(mask);
  402. for (i = 0; i < first && cpu < nr_cpu_ids; i++)
  403. cpu = cpumask_next(cpu, mask);
  404. /* Sanity check */
  405. if (WARN_ON(cpu >= nr_cpu_ids))
  406. cpu = cpumask_first(cpu_online_mask);
  407. /* Remember first one to handle wrap-around */
  408. first = cpu;
  409. /*
  410. * Now go through the entire mask until we find a valid
  411. * target.
  412. */
  413. do {
  414. /*
  415. * We re-check online as the fallback case passes us
  416. * an untested affinity mask
  417. */
  418. if (cpu_online(cpu) && xive_try_pick_target(cpu))
  419. return cpu;
  420. cpu = cpumask_next(cpu, mask);
  421. /* Wrap around */
  422. if (cpu >= nr_cpu_ids)
  423. cpu = cpumask_first(mask);
  424. } while (cpu != first);
  425. return -1;
  426. }
  427. /*
  428. * Pick a target CPU for an interrupt. This is done at
  429. * startup or if the affinity is changed in a way that
  430. * invalidates the current target.
  431. */
  432. static int xive_pick_irq_target(struct irq_data *d,
  433. const struct cpumask *affinity)
  434. {
  435. static unsigned int fuzz;
  436. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  437. cpumask_var_t mask;
  438. int cpu = -1;
  439. /*
  440. * If we have chip IDs, first we try to build a mask of
  441. * CPUs matching the CPU and find a target in there
  442. */
  443. if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
  444. zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
  445. /* Build a mask of matching chip IDs */
  446. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  447. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  448. if (xc->chip_id == xd->src_chip)
  449. cpumask_set_cpu(cpu, mask);
  450. }
  451. /* Try to find a target */
  452. if (cpumask_empty(mask))
  453. cpu = -1;
  454. else
  455. cpu = xive_find_target_in_mask(mask, fuzz++);
  456. free_cpumask_var(mask);
  457. if (cpu >= 0)
  458. return cpu;
  459. fuzz--;
  460. }
  461. /* No chip IDs, fallback to using the affinity mask */
  462. return xive_find_target_in_mask(affinity, fuzz++);
  463. }
  464. static unsigned int xive_irq_startup(struct irq_data *d)
  465. {
  466. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  467. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  468. int target, rc;
  469. pr_devel("xive_irq_startup: irq %d [0x%x] data @%p\n",
  470. d->irq, hw_irq, d);
  471. #ifdef CONFIG_PCI_MSI
  472. /*
  473. * The generic MSI code returns with the interrupt disabled on the
  474. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  475. * at that level, so we do it here by hand.
  476. */
  477. if (irq_data_get_msi_desc(d))
  478. pci_msi_unmask_irq(d);
  479. #endif
  480. /* Pick a target */
  481. target = xive_pick_irq_target(d, irq_data_get_affinity_mask(d));
  482. if (target == XIVE_INVALID_TARGET) {
  483. /* Try again breaking affinity */
  484. target = xive_pick_irq_target(d, cpu_online_mask);
  485. if (target == XIVE_INVALID_TARGET)
  486. return -ENXIO;
  487. pr_warn("irq %d started with broken affinity\n", d->irq);
  488. }
  489. /* Sanity check */
  490. if (WARN_ON(target == XIVE_INVALID_TARGET ||
  491. target >= nr_cpu_ids))
  492. target = smp_processor_id();
  493. xd->target = target;
  494. /*
  495. * Configure the logical number to be the Linux IRQ number
  496. * and set the target queue
  497. */
  498. rc = xive_ops->configure_irq(hw_irq,
  499. get_hard_smp_processor_id(target),
  500. xive_irq_priority, d->irq);
  501. if (rc)
  502. return rc;
  503. /* Unmask the ESB */
  504. xive_do_source_set_mask(xd, false);
  505. return 0;
  506. }
  507. static void xive_irq_shutdown(struct irq_data *d)
  508. {
  509. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  510. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  511. pr_devel("xive_irq_shutdown: irq %d [0x%x] data @%p\n",
  512. d->irq, hw_irq, d);
  513. if (WARN_ON(xd->target == XIVE_INVALID_TARGET))
  514. return;
  515. /* Mask the interrupt at the source */
  516. xive_do_source_set_mask(xd, true);
  517. /*
  518. * The above may have set saved_p. We clear it otherwise it
  519. * will prevent re-enabling later on. It is ok to forget the
  520. * fact that the interrupt might be in a queue because we are
  521. * accounting that already in xive_dec_target_count() and will
  522. * be re-routing it to a new queue with proper accounting when
  523. * it's started up again
  524. */
  525. xd->saved_p = false;
  526. /*
  527. * Mask the interrupt in HW in the IVT/EAS and set the number
  528. * to be the "bad" IRQ number
  529. */
  530. xive_ops->configure_irq(hw_irq,
  531. get_hard_smp_processor_id(xd->target),
  532. 0xff, XIVE_BAD_IRQ);
  533. xive_dec_target_count(xd->target);
  534. xd->target = XIVE_INVALID_TARGET;
  535. }
  536. static void xive_irq_unmask(struct irq_data *d)
  537. {
  538. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  539. pr_devel("xive_irq_unmask: irq %d data @%p\n", d->irq, xd);
  540. /*
  541. * This is a workaround for PCI LSI problems on P9, for
  542. * these, we call FW to set the mask. The problems might
  543. * be fixed by P9 DD2.0, if that is the case, firmware
  544. * will no longer set that flag.
  545. */
  546. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
  547. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  548. xive_ops->configure_irq(hw_irq,
  549. get_hard_smp_processor_id(xd->target),
  550. xive_irq_priority, d->irq);
  551. return;
  552. }
  553. xive_do_source_set_mask(xd, false);
  554. }
  555. static void xive_irq_mask(struct irq_data *d)
  556. {
  557. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  558. pr_devel("xive_irq_mask: irq %d data @%p\n", d->irq, xd);
  559. /*
  560. * This is a workaround for PCI LSI problems on P9, for
  561. * these, we call OPAL to set the mask. The problems might
  562. * be fixed by P9 DD2.0, if that is the case, firmware
  563. * will no longer set that flag.
  564. */
  565. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW) {
  566. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  567. xive_ops->configure_irq(hw_irq,
  568. get_hard_smp_processor_id(xd->target),
  569. 0xff, d->irq);
  570. return;
  571. }
  572. xive_do_source_set_mask(xd, true);
  573. }
  574. static int xive_irq_set_affinity(struct irq_data *d,
  575. const struct cpumask *cpumask,
  576. bool force)
  577. {
  578. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  579. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  580. u32 target, old_target;
  581. int rc = 0;
  582. pr_devel("xive_irq_set_affinity: irq %d\n", d->irq);
  583. /* Is this valid ? */
  584. if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids)
  585. return -EINVAL;
  586. /* Don't do anything if the interrupt isn't started */
  587. if (!irqd_is_started(d))
  588. return IRQ_SET_MASK_OK;
  589. /*
  590. * If existing target is already in the new mask, and is
  591. * online then do nothing.
  592. */
  593. if (xd->target != XIVE_INVALID_TARGET &&
  594. cpu_online(xd->target) &&
  595. cpumask_test_cpu(xd->target, cpumask))
  596. return IRQ_SET_MASK_OK;
  597. /* Pick a new target */
  598. target = xive_pick_irq_target(d, cpumask);
  599. /* No target found */
  600. if (target == XIVE_INVALID_TARGET)
  601. return -ENXIO;
  602. /* Sanity check */
  603. if (WARN_ON(target >= nr_cpu_ids))
  604. target = smp_processor_id();
  605. old_target = xd->target;
  606. /*
  607. * Only configure the irq if it's not currently passed-through to
  608. * a KVM guest
  609. */
  610. if (!irqd_is_forwarded_to_vcpu(d))
  611. rc = xive_ops->configure_irq(hw_irq,
  612. get_hard_smp_processor_id(target),
  613. xive_irq_priority, d->irq);
  614. if (rc < 0) {
  615. pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
  616. return rc;
  617. }
  618. pr_devel(" target: 0x%x\n", target);
  619. xd->target = target;
  620. /* Give up previous target */
  621. if (old_target != XIVE_INVALID_TARGET)
  622. xive_dec_target_count(old_target);
  623. return IRQ_SET_MASK_OK;
  624. }
  625. static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
  626. {
  627. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  628. /*
  629. * We only support these. This has really no effect other than setting
  630. * the corresponding descriptor bits mind you but those will in turn
  631. * affect the resend function when re-enabling an edge interrupt.
  632. *
  633. * Set set the default to edge as explained in map().
  634. */
  635. if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
  636. flow_type = IRQ_TYPE_EDGE_RISING;
  637. if (flow_type != IRQ_TYPE_EDGE_RISING &&
  638. flow_type != IRQ_TYPE_LEVEL_LOW)
  639. return -EINVAL;
  640. irqd_set_trigger_type(d, flow_type);
  641. /*
  642. * Double check it matches what the FW thinks
  643. *
  644. * NOTE: We don't know yet if the PAPR interface will provide
  645. * the LSI vs MSI information apart from the device-tree so
  646. * this check might have to move into an optional backend call
  647. * that is specific to the native backend
  648. */
  649. if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
  650. !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
  651. pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
  652. d->irq, (u32)irqd_to_hwirq(d),
  653. (flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
  654. (xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
  655. }
  656. return IRQ_SET_MASK_OK_NOCOPY;
  657. }
  658. static int xive_irq_retrigger(struct irq_data *d)
  659. {
  660. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  661. /* This should be only for MSIs */
  662. if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
  663. return 0;
  664. /*
  665. * To perform a retrigger, we first set the PQ bits to
  666. * 11, then perform an EOI.
  667. */
  668. xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
  669. /*
  670. * Note: We pass "0" to the hw_irq argument in order to
  671. * avoid calling into the backend EOI code which we don't
  672. * want to do in the case of a re-trigger. Backends typically
  673. * only do EOI for LSIs anyway.
  674. */
  675. xive_do_source_eoi(0, xd);
  676. return 1;
  677. }
  678. static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
  679. {
  680. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  681. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  682. int rc;
  683. u8 pq;
  684. /*
  685. * We only support this on interrupts that do not require
  686. * firmware calls for masking and unmasking
  687. */
  688. if (xd->flags & XIVE_IRQ_FLAG_MASK_FW)
  689. return -EIO;
  690. /*
  691. * This is called by KVM with state non-NULL for enabling
  692. * pass-through or NULL for disabling it
  693. */
  694. if (state) {
  695. irqd_set_forwarded_to_vcpu(d);
  696. /* Set it to PQ=10 state to prevent further sends */
  697. pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
  698. /* No target ? nothing to do */
  699. if (xd->target == XIVE_INVALID_TARGET) {
  700. /*
  701. * An untargetted interrupt should have been
  702. * also masked at the source
  703. */
  704. WARN_ON(pq & 2);
  705. return 0;
  706. }
  707. /*
  708. * If P was set, adjust state to PQ=11 to indicate
  709. * that a resend is needed for the interrupt to reach
  710. * the guest. Also remember the value of P.
  711. *
  712. * This also tells us that it's in flight to a host queue
  713. * or has already been fetched but hasn't been EOIed yet
  714. * by the host. This it's potentially using up a host
  715. * queue slot. This is important to know because as long
  716. * as this is the case, we must not hard-unmask it when
  717. * "returning" that interrupt to the host.
  718. *
  719. * This saved_p is cleared by the host EOI, when we know
  720. * for sure the queue slot is no longer in use.
  721. */
  722. if (pq & 2) {
  723. pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
  724. xd->saved_p = true;
  725. /*
  726. * Sync the XIVE source HW to ensure the interrupt
  727. * has gone through the EAS before we change its
  728. * target to the guest. That should guarantee us
  729. * that we *will* eventually get an EOI for it on
  730. * the host. Otherwise there would be a small window
  731. * for P to be seen here but the interrupt going
  732. * to the guest queue.
  733. */
  734. if (xive_ops->sync_source)
  735. xive_ops->sync_source(hw_irq);
  736. } else
  737. xd->saved_p = false;
  738. } else {
  739. irqd_clr_forwarded_to_vcpu(d);
  740. /* No host target ? hard mask and return */
  741. if (xd->target == XIVE_INVALID_TARGET) {
  742. xive_do_source_set_mask(xd, true);
  743. return 0;
  744. }
  745. /*
  746. * Sync the XIVE source HW to ensure the interrupt
  747. * has gone through the EAS before we change its
  748. * target to the host.
  749. */
  750. if (xive_ops->sync_source)
  751. xive_ops->sync_source(hw_irq);
  752. /*
  753. * By convention we are called with the interrupt in
  754. * a PQ=10 or PQ=11 state, ie, it won't fire and will
  755. * have latched in Q whether there's a pending HW
  756. * interrupt or not.
  757. *
  758. * First reconfigure the target.
  759. */
  760. rc = xive_ops->configure_irq(hw_irq,
  761. get_hard_smp_processor_id(xd->target),
  762. xive_irq_priority, d->irq);
  763. if (rc)
  764. return rc;
  765. /*
  766. * Then if saved_p is not set, effectively re-enable the
  767. * interrupt with an EOI. If it is set, we know there is
  768. * still a message in a host queue somewhere that will be
  769. * EOId eventually.
  770. *
  771. * Note: We don't check irqd_irq_disabled(). Effectively,
  772. * we *will* let the irq get through even if masked if the
  773. * HW is still firing it in order to deal with the whole
  774. * saved_p business properly. If the interrupt triggers
  775. * while masked, the generic code will re-mask it anyway.
  776. */
  777. if (!xd->saved_p)
  778. xive_do_source_eoi(hw_irq, xd);
  779. }
  780. return 0;
  781. }
  782. static struct irq_chip xive_irq_chip = {
  783. .name = "XIVE-IRQ",
  784. .irq_startup = xive_irq_startup,
  785. .irq_shutdown = xive_irq_shutdown,
  786. .irq_eoi = xive_irq_eoi,
  787. .irq_mask = xive_irq_mask,
  788. .irq_unmask = xive_irq_unmask,
  789. .irq_set_affinity = xive_irq_set_affinity,
  790. .irq_set_type = xive_irq_set_type,
  791. .irq_retrigger = xive_irq_retrigger,
  792. .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
  793. };
  794. bool is_xive_irq(struct irq_chip *chip)
  795. {
  796. return chip == &xive_irq_chip;
  797. }
  798. EXPORT_SYMBOL_GPL(is_xive_irq);
  799. void xive_cleanup_irq_data(struct xive_irq_data *xd)
  800. {
  801. if (xd->eoi_mmio) {
  802. unmap_kernel_range((unsigned long)xd->eoi_mmio,
  803. 1u << xd->esb_shift);
  804. iounmap(xd->eoi_mmio);
  805. if (xd->eoi_mmio == xd->trig_mmio)
  806. xd->trig_mmio = NULL;
  807. xd->eoi_mmio = NULL;
  808. }
  809. if (xd->trig_mmio) {
  810. unmap_kernel_range((unsigned long)xd->trig_mmio,
  811. 1u << xd->esb_shift);
  812. iounmap(xd->trig_mmio);
  813. xd->trig_mmio = NULL;
  814. }
  815. }
  816. EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
  817. static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
  818. {
  819. struct xive_irq_data *xd;
  820. int rc;
  821. xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
  822. if (!xd)
  823. return -ENOMEM;
  824. rc = xive_ops->populate_irq_data(hw, xd);
  825. if (rc) {
  826. kfree(xd);
  827. return rc;
  828. }
  829. xd->target = XIVE_INVALID_TARGET;
  830. irq_set_handler_data(virq, xd);
  831. /*
  832. * Turn OFF by default the interrupt being mapped. A side
  833. * effect of this check is the mapping the ESB page of the
  834. * interrupt in the Linux address space. This prevents page
  835. * fault issues in the crash handler which masks all
  836. * interrupts.
  837. */
  838. xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
  839. return 0;
  840. }
  841. static void xive_irq_free_data(unsigned int virq)
  842. {
  843. struct xive_irq_data *xd = irq_get_handler_data(virq);
  844. if (!xd)
  845. return;
  846. irq_set_handler_data(virq, NULL);
  847. xive_cleanup_irq_data(xd);
  848. kfree(xd);
  849. }
  850. #ifdef CONFIG_SMP
  851. static void xive_cause_ipi(int cpu)
  852. {
  853. struct xive_cpu *xc;
  854. struct xive_irq_data *xd;
  855. xc = per_cpu(xive_cpu, cpu);
  856. DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
  857. smp_processor_id(), cpu, xc->hw_ipi);
  858. xd = &xc->ipi_data;
  859. if (WARN_ON(!xd->trig_mmio))
  860. return;
  861. out_be64(xd->trig_mmio, 0);
  862. }
  863. static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
  864. {
  865. return smp_ipi_demux();
  866. }
  867. static void xive_ipi_eoi(struct irq_data *d)
  868. {
  869. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  870. /* Handle possible race with unplug and drop stale IPIs */
  871. if (!xc)
  872. return;
  873. DBG_VERBOSE("IPI eoi: irq=%d [0x%lx] (HW IRQ 0x%x) pending=%02x\n",
  874. d->irq, irqd_to_hwirq(d), xc->hw_ipi, xc->pending_prio);
  875. xive_do_source_eoi(xc->hw_ipi, &xc->ipi_data);
  876. xive_do_queue_eoi(xc);
  877. }
  878. static void xive_ipi_do_nothing(struct irq_data *d)
  879. {
  880. /*
  881. * Nothing to do, we never mask/unmask IPIs, but the callback
  882. * has to exist for the struct irq_chip.
  883. */
  884. }
  885. static struct irq_chip xive_ipi_chip = {
  886. .name = "XIVE-IPI",
  887. .irq_eoi = xive_ipi_eoi,
  888. .irq_mask = xive_ipi_do_nothing,
  889. .irq_unmask = xive_ipi_do_nothing,
  890. };
  891. static void __init xive_request_ipi(void)
  892. {
  893. unsigned int virq;
  894. /*
  895. * Initialization failed, move on, we might manage to
  896. * reach the point where we display our errors before
  897. * the system falls appart
  898. */
  899. if (!xive_irq_domain)
  900. return;
  901. /* Initialize it */
  902. virq = irq_create_mapping(xive_irq_domain, 0);
  903. xive_ipi_irq = virq;
  904. WARN_ON(request_irq(virq, xive_muxed_ipi_action,
  905. IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));
  906. }
  907. static int xive_setup_cpu_ipi(unsigned int cpu)
  908. {
  909. struct xive_cpu *xc;
  910. int rc;
  911. pr_debug("Setting up IPI for CPU %d\n", cpu);
  912. xc = per_cpu(xive_cpu, cpu);
  913. /* Check if we are already setup */
  914. if (xc->hw_ipi != XIVE_BAD_IRQ)
  915. return 0;
  916. /* Grab an IPI from the backend, this will populate xc->hw_ipi */
  917. if (xive_ops->get_ipi(cpu, xc))
  918. return -EIO;
  919. /*
  920. * Populate the IRQ data in the xive_cpu structure and
  921. * configure the HW / enable the IPIs.
  922. */
  923. rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
  924. if (rc) {
  925. pr_err("Failed to populate IPI data on CPU %d\n", cpu);
  926. return -EIO;
  927. }
  928. rc = xive_ops->configure_irq(xc->hw_ipi,
  929. get_hard_smp_processor_id(cpu),
  930. xive_irq_priority, xive_ipi_irq);
  931. if (rc) {
  932. pr_err("Failed to map IPI CPU %d\n", cpu);
  933. return -EIO;
  934. }
  935. pr_devel("CPU %d HW IPI %x, virq %d, trig_mmio=%p\n", cpu,
  936. xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
  937. /* Unmask it */
  938. xive_do_source_set_mask(&xc->ipi_data, false);
  939. return 0;
  940. }
  941. static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
  942. {
  943. /* Disable the IPI and free the IRQ data */
  944. /* Already cleaned up ? */
  945. if (xc->hw_ipi == XIVE_BAD_IRQ)
  946. return;
  947. /* Mask the IPI */
  948. xive_do_source_set_mask(&xc->ipi_data, true);
  949. /*
  950. * Note: We don't call xive_cleanup_irq_data() to free
  951. * the mappings as this is called from an IPI on kexec
  952. * which is not a safe environment to call iounmap()
  953. */
  954. /* Deconfigure/mask in the backend */
  955. xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
  956. 0xff, xive_ipi_irq);
  957. /* Free the IPIs in the backend */
  958. xive_ops->put_ipi(cpu, xc);
  959. }
  960. void __init xive_smp_probe(void)
  961. {
  962. smp_ops->cause_ipi = xive_cause_ipi;
  963. /* Register the IPI */
  964. xive_request_ipi();
  965. /* Allocate and setup IPI for the boot CPU */
  966. xive_setup_cpu_ipi(smp_processor_id());
  967. }
  968. #endif /* CONFIG_SMP */
  969. static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
  970. irq_hw_number_t hw)
  971. {
  972. int rc;
  973. /*
  974. * Mark interrupts as edge sensitive by default so that resend
  975. * actually works. Will fix that up below if needed.
  976. */
  977. irq_clear_status_flags(virq, IRQ_LEVEL);
  978. #ifdef CONFIG_SMP
  979. /* IPIs are special and come up with HW number 0 */
  980. if (hw == 0) {
  981. /*
  982. * IPIs are marked per-cpu. We use separate HW interrupts under
  983. * the hood but associated with the same "linux" interrupt
  984. */
  985. irq_set_chip_and_handler(virq, &xive_ipi_chip,
  986. handle_percpu_irq);
  987. return 0;
  988. }
  989. #endif
  990. rc = xive_irq_alloc_data(virq, hw);
  991. if (rc)
  992. return rc;
  993. irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
  994. return 0;
  995. }
  996. static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
  997. {
  998. struct irq_data *data = irq_get_irq_data(virq);
  999. unsigned int hw_irq;
  1000. /* XXX Assign BAD number */
  1001. if (!data)
  1002. return;
  1003. hw_irq = (unsigned int)irqd_to_hwirq(data);
  1004. if (hw_irq)
  1005. xive_irq_free_data(virq);
  1006. }
  1007. static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
  1008. const u32 *intspec, unsigned int intsize,
  1009. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  1010. {
  1011. *out_hwirq = intspec[0];
  1012. /*
  1013. * If intsize is at least 2, we look for the type in the second cell,
  1014. * we assume the LSB indicates a level interrupt.
  1015. */
  1016. if (intsize > 1) {
  1017. if (intspec[1] & 1)
  1018. *out_flags = IRQ_TYPE_LEVEL_LOW;
  1019. else
  1020. *out_flags = IRQ_TYPE_EDGE_RISING;
  1021. } else
  1022. *out_flags = IRQ_TYPE_LEVEL_LOW;
  1023. return 0;
  1024. }
  1025. static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
  1026. enum irq_domain_bus_token bus_token)
  1027. {
  1028. return xive_ops->match(node);
  1029. }
  1030. static const struct irq_domain_ops xive_irq_domain_ops = {
  1031. .match = xive_irq_domain_match,
  1032. .map = xive_irq_domain_map,
  1033. .unmap = xive_irq_domain_unmap,
  1034. .xlate = xive_irq_domain_xlate,
  1035. };
  1036. static void __init xive_init_host(void)
  1037. {
  1038. xive_irq_domain = irq_domain_add_nomap(NULL, XIVE_MAX_IRQ,
  1039. &xive_irq_domain_ops, NULL);
  1040. if (WARN_ON(xive_irq_domain == NULL))
  1041. return;
  1042. irq_set_default_host(xive_irq_domain);
  1043. }
  1044. static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1045. {
  1046. if (xc->queue[xive_irq_priority].qpage)
  1047. xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
  1048. }
  1049. static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1050. {
  1051. int rc = 0;
  1052. /* We setup 1 queues for now with a 64k page */
  1053. if (!xc->queue[xive_irq_priority].qpage)
  1054. rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
  1055. return rc;
  1056. }
  1057. static int xive_prepare_cpu(unsigned int cpu)
  1058. {
  1059. struct xive_cpu *xc;
  1060. xc = per_cpu(xive_cpu, cpu);
  1061. if (!xc) {
  1062. struct device_node *np;
  1063. xc = kzalloc_node(sizeof(struct xive_cpu),
  1064. GFP_KERNEL, cpu_to_node(cpu));
  1065. if (!xc)
  1066. return -ENOMEM;
  1067. np = of_get_cpu_node(cpu, NULL);
  1068. if (np)
  1069. xc->chip_id = of_get_ibm_chip_id(np);
  1070. of_node_put(np);
  1071. xc->hw_ipi = XIVE_BAD_IRQ;
  1072. per_cpu(xive_cpu, cpu) = xc;
  1073. }
  1074. /* Setup EQs if not already */
  1075. return xive_setup_cpu_queues(cpu, xc);
  1076. }
  1077. static void xive_setup_cpu(void)
  1078. {
  1079. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1080. /* The backend might have additional things to do */
  1081. if (xive_ops->setup_cpu)
  1082. xive_ops->setup_cpu(smp_processor_id(), xc);
  1083. /* Set CPPR to 0xff to enable flow of interrupts */
  1084. xc->cppr = 0xff;
  1085. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1086. }
  1087. #ifdef CONFIG_SMP
  1088. void xive_smp_setup_cpu(void)
  1089. {
  1090. pr_devel("SMP setup CPU %d\n", smp_processor_id());
  1091. /* This will have already been done on the boot CPU */
  1092. if (smp_processor_id() != boot_cpuid)
  1093. xive_setup_cpu();
  1094. }
  1095. int xive_smp_prepare_cpu(unsigned int cpu)
  1096. {
  1097. int rc;
  1098. /* Allocate per-CPU data and queues */
  1099. rc = xive_prepare_cpu(cpu);
  1100. if (rc)
  1101. return rc;
  1102. /* Allocate and setup IPI for the new CPU */
  1103. return xive_setup_cpu_ipi(cpu);
  1104. }
  1105. #ifdef CONFIG_HOTPLUG_CPU
  1106. static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
  1107. {
  1108. u32 irq;
  1109. /* We assume local irqs are disabled */
  1110. WARN_ON(!irqs_disabled());
  1111. /* Check what's already in the CPU queue */
  1112. while ((irq = xive_scan_interrupts(xc, false)) != 0) {
  1113. /*
  1114. * We need to re-route that interrupt to its new destination.
  1115. * First get and lock the descriptor
  1116. */
  1117. struct irq_desc *desc = irq_to_desc(irq);
  1118. struct irq_data *d = irq_desc_get_irq_data(desc);
  1119. struct xive_irq_data *xd;
  1120. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1121. /*
  1122. * Ignore anything that isn't a XIVE irq and ignore
  1123. * IPIs, so can just be dropped.
  1124. */
  1125. if (d->domain != xive_irq_domain || hw_irq == 0)
  1126. continue;
  1127. /*
  1128. * The IRQ should have already been re-routed, it's just a
  1129. * stale in the old queue, so re-trigger it in order to make
  1130. * it reach is new destination.
  1131. */
  1132. #ifdef DEBUG_FLUSH
  1133. pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
  1134. cpu, irq);
  1135. #endif
  1136. raw_spin_lock(&desc->lock);
  1137. xd = irq_desc_get_handler_data(desc);
  1138. /*
  1139. * For LSIs, we EOI, this will cause a resend if it's
  1140. * still asserted. Otherwise do an MSI retrigger.
  1141. */
  1142. if (xd->flags & XIVE_IRQ_FLAG_LSI)
  1143. xive_do_source_eoi(irqd_to_hwirq(d), xd);
  1144. else
  1145. xive_irq_retrigger(d);
  1146. raw_spin_unlock(&desc->lock);
  1147. }
  1148. }
  1149. void xive_smp_disable_cpu(void)
  1150. {
  1151. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1152. unsigned int cpu = smp_processor_id();
  1153. /* Migrate interrupts away from the CPU */
  1154. irq_migrate_all_off_this_cpu();
  1155. /* Set CPPR to 0 to disable flow of interrupts */
  1156. xc->cppr = 0;
  1157. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1158. /* Flush everything still in the queue */
  1159. xive_flush_cpu_queue(cpu, xc);
  1160. /* Re-enable CPPR */
  1161. xc->cppr = 0xff;
  1162. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1163. }
  1164. void xive_flush_interrupt(void)
  1165. {
  1166. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1167. unsigned int cpu = smp_processor_id();
  1168. /* Called if an interrupt occurs while the CPU is hot unplugged */
  1169. xive_flush_cpu_queue(cpu, xc);
  1170. }
  1171. #endif /* CONFIG_HOTPLUG_CPU */
  1172. #endif /* CONFIG_SMP */
  1173. void xive_teardown_cpu(void)
  1174. {
  1175. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1176. unsigned int cpu = smp_processor_id();
  1177. /* Set CPPR to 0 to disable flow of interrupts */
  1178. xc->cppr = 0;
  1179. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1180. if (xive_ops->teardown_cpu)
  1181. xive_ops->teardown_cpu(cpu, xc);
  1182. #ifdef CONFIG_SMP
  1183. /* Get rid of IPI */
  1184. xive_cleanup_cpu_ipi(cpu, xc);
  1185. #endif
  1186. /* Disable and free the queues */
  1187. xive_cleanup_cpu_queues(cpu, xc);
  1188. }
  1189. void xive_shutdown(void)
  1190. {
  1191. xive_ops->shutdown();
  1192. }
  1193. bool __init xive_core_init(const struct xive_ops *ops, void __iomem *area, u32 offset,
  1194. u8 max_prio)
  1195. {
  1196. xive_tima = area;
  1197. xive_tima_offset = offset;
  1198. xive_ops = ops;
  1199. xive_irq_priority = max_prio;
  1200. ppc_md.get_irq = xive_get_irq;
  1201. __xive_enabled = true;
  1202. pr_devel("Initializing host..\n");
  1203. xive_init_host();
  1204. pr_devel("Initializing boot CPU..\n");
  1205. /* Allocate per-CPU data and queues */
  1206. xive_prepare_cpu(smp_processor_id());
  1207. /* Get ready for interrupts */
  1208. xive_setup_cpu();
  1209. pr_info("Interrupt handling initialized with %s backend\n",
  1210. xive_ops->name);
  1211. pr_info("Using priority %d for all interrupts\n", max_prio);
  1212. return true;
  1213. }
  1214. __be32 *xive_queue_page_alloc(unsigned int cpu, u32 queue_shift)
  1215. {
  1216. unsigned int alloc_order;
  1217. struct page *pages;
  1218. __be32 *qpage;
  1219. alloc_order = xive_alloc_order(queue_shift);
  1220. pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
  1221. if (!pages)
  1222. return ERR_PTR(-ENOMEM);
  1223. qpage = (__be32 *)page_address(pages);
  1224. memset(qpage, 0, 1 << queue_shift);
  1225. return qpage;
  1226. }
  1227. static int __init xive_off(char *arg)
  1228. {
  1229. xive_cmdline_disabled = true;
  1230. return 0;
  1231. }
  1232. __setup("xive=off", xive_off);