common.c 36 KB

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