common.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016,2017 IBM Corporation.
  4. */
  5. #define pr_fmt(fmt) "xive: " fmt
  6. #include <linux/types.h>
  7. #include <linux/threads.h>
  8. #include <linux/kernel.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/smp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/init.h>
  16. #include <linux/cpu.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/msi.h>
  21. #include <linux/vmalloc.h>
  22. #include <asm/io.h>
  23. #include <asm/smp.h>
  24. #include <asm/machdep.h>
  25. #include <asm/irq.h>
  26. #include <asm/errno.h>
  27. #include <asm/xive.h>
  28. #include <asm/xive-regs.h>
  29. #include <asm/xmon.h>
  30. #include "xive-internal.h"
  31. #undef DEBUG_FLUSH
  32. #undef DEBUG_ALL
  33. #ifdef DEBUG_ALL
  34. #define DBG_VERBOSE(fmt, ...) pr_devel("cpu %d - " fmt, \
  35. smp_processor_id(), ## __VA_ARGS__)
  36. #else
  37. #define DBG_VERBOSE(fmt...) do { } while(0)
  38. #endif
  39. bool __xive_enabled;
  40. EXPORT_SYMBOL_GPL(__xive_enabled);
  41. bool xive_cmdline_disabled;
  42. /* We use only one priority for now */
  43. static u8 xive_irq_priority;
  44. /* TIMA exported to KVM */
  45. void __iomem *xive_tima;
  46. EXPORT_SYMBOL_GPL(xive_tima);
  47. u32 xive_tima_offset;
  48. /* Backend ops */
  49. static const struct xive_ops *xive_ops;
  50. /* Our global interrupt domain */
  51. static struct irq_domain *xive_irq_domain;
  52. #ifdef CONFIG_SMP
  53. /* The IPIs use the same logical irq number when on the same chip */
  54. static struct xive_ipi_desc {
  55. unsigned int irq;
  56. char name[16];
  57. atomic_t started;
  58. } *xive_ipis;
  59. /*
  60. * Use early_cpu_to_node() for hot-plugged CPUs
  61. */
  62. static unsigned int xive_ipi_cpu_to_irq(unsigned int cpu)
  63. {
  64. return xive_ipis[early_cpu_to_node(cpu)].irq;
  65. }
  66. #endif
  67. /* Xive state for each CPU */
  68. static DEFINE_PER_CPU(struct xive_cpu *, xive_cpu);
  69. /* An invalid CPU target */
  70. #define XIVE_INVALID_TARGET (-1)
  71. /*
  72. * Global toggle to switch on/off StoreEOI
  73. */
  74. static bool xive_store_eoi = true;
  75. static bool xive_is_store_eoi(struct xive_irq_data *xd)
  76. {
  77. return xd->flags & XIVE_IRQ_FLAG_STORE_EOI && xive_store_eoi;
  78. }
  79. /*
  80. * Read the next entry in a queue, return its content if it's valid
  81. * or 0 if there is no new entry.
  82. *
  83. * The queue pointer is moved forward unless "just_peek" is set
  84. */
  85. static u32 xive_read_eq(struct xive_q *q, bool just_peek)
  86. {
  87. u32 cur;
  88. if (!q->qpage)
  89. return 0;
  90. cur = be32_to_cpup(q->qpage + q->idx);
  91. /* Check valid bit (31) vs current toggle polarity */
  92. if ((cur >> 31) == q->toggle)
  93. return 0;
  94. /* If consuming from the queue ... */
  95. if (!just_peek) {
  96. /* Next entry */
  97. q->idx = (q->idx + 1) & q->msk;
  98. /* Wrap around: flip valid toggle */
  99. if (q->idx == 0)
  100. q->toggle ^= 1;
  101. }
  102. /* Mask out the valid bit (31) */
  103. return cur & 0x7fffffff;
  104. }
  105. /*
  106. * Scans all the queue that may have interrupts in them
  107. * (based on "pending_prio") in priority order until an
  108. * interrupt is found or all the queues are empty.
  109. *
  110. * Then updates the CPPR (Current Processor Priority
  111. * Register) based on the most favored interrupt found
  112. * (0xff if none) and return what was found (0 if none).
  113. *
  114. * If just_peek is set, return the most favored pending
  115. * interrupt if any but don't update the queue pointers.
  116. *
  117. * Note: This function can operate generically on any number
  118. * of queues (up to 8). The current implementation of the XIVE
  119. * driver only uses a single queue however.
  120. *
  121. * Note2: This will also "flush" "the pending_count" of a queue
  122. * into the "count" when that queue is observed to be empty.
  123. * This is used to keep track of the amount of interrupts
  124. * targetting a queue. When an interrupt is moved away from
  125. * a queue, we only decrement that queue count once the queue
  126. * has been observed empty to avoid races.
  127. */
  128. static u32 xive_scan_interrupts(struct xive_cpu *xc, bool just_peek)
  129. {
  130. u32 irq = 0;
  131. u8 prio = 0;
  132. /* Find highest pending priority */
  133. while (xc->pending_prio != 0) {
  134. struct xive_q *q;
  135. prio = ffs(xc->pending_prio) - 1;
  136. DBG_VERBOSE("scan_irq: trying prio %d\n", prio);
  137. /* Try to fetch */
  138. irq = xive_read_eq(&xc->queue[prio], just_peek);
  139. /* Found something ? That's it */
  140. if (irq) {
  141. if (just_peek || irq_to_desc(irq))
  142. break;
  143. /*
  144. * We should never get here; if we do then we must
  145. * have failed to synchronize the interrupt properly
  146. * when shutting it down.
  147. */
  148. pr_crit("xive: got interrupt %d without descriptor, dropping\n",
  149. irq);
  150. WARN_ON(1);
  151. continue;
  152. }
  153. /* Clear pending bits */
  154. xc->pending_prio &= ~(1 << prio);
  155. /*
  156. * Check if the queue count needs adjusting due to
  157. * interrupts being moved away. See description of
  158. * xive_dec_target_count()
  159. */
  160. q = &xc->queue[prio];
  161. if (atomic_read(&q->pending_count)) {
  162. int p = atomic_xchg(&q->pending_count, 0);
  163. if (p) {
  164. WARN_ON(p > atomic_read(&q->count));
  165. atomic_sub(p, &q->count);
  166. }
  167. }
  168. }
  169. /* If nothing was found, set CPPR to 0xff */
  170. if (irq == 0)
  171. prio = 0xff;
  172. /* Update HW CPPR to match if necessary */
  173. if (prio != xc->cppr) {
  174. DBG_VERBOSE("scan_irq: adjusting CPPR to %d\n", prio);
  175. xc->cppr = prio;
  176. out_8(xive_tima + xive_tima_offset + TM_CPPR, prio);
  177. }
  178. return irq;
  179. }
  180. /*
  181. * This is used to perform the magic loads from an ESB
  182. * described in xive-regs.h
  183. */
  184. static notrace u8 xive_esb_read(struct xive_irq_data *xd, u32 offset)
  185. {
  186. u64 val;
  187. if (offset == XIVE_ESB_SET_PQ_10 && xive_is_store_eoi(xd))
  188. offset |= XIVE_ESB_LD_ST_MO;
  189. if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
  190. val = xive_ops->esb_rw(xd->hw_irq, offset, 0, 0);
  191. else
  192. val = in_be64(xd->eoi_mmio + offset);
  193. return (u8)val;
  194. }
  195. static void xive_esb_write(struct xive_irq_data *xd, u32 offset, u64 data)
  196. {
  197. if ((xd->flags & XIVE_IRQ_FLAG_H_INT_ESB) && xive_ops->esb_rw)
  198. xive_ops->esb_rw(xd->hw_irq, offset, data, 1);
  199. else
  200. out_be64(xd->eoi_mmio + offset, data);
  201. }
  202. #if defined(CONFIG_XMON) || defined(CONFIG_DEBUG_FS)
  203. static void xive_irq_data_dump(struct xive_irq_data *xd, char *buffer, size_t size)
  204. {
  205. u64 val = xive_esb_read(xd, XIVE_ESB_GET);
  206. snprintf(buffer, size, "flags=%c%c%c PQ=%c%c 0x%016llx 0x%016llx",
  207. xive_is_store_eoi(xd) ? 'S' : ' ',
  208. xd->flags & XIVE_IRQ_FLAG_LSI ? 'L' : ' ',
  209. xd->flags & XIVE_IRQ_FLAG_H_INT_ESB ? 'H' : ' ',
  210. val & XIVE_ESB_VAL_P ? 'P' : '-',
  211. val & XIVE_ESB_VAL_Q ? 'Q' : '-',
  212. xd->trig_page, xd->eoi_page);
  213. }
  214. #endif
  215. #ifdef CONFIG_XMON
  216. static notrace void xive_dump_eq(const char *name, struct xive_q *q)
  217. {
  218. u32 i0, i1, idx;
  219. if (!q->qpage)
  220. return;
  221. idx = q->idx;
  222. i0 = be32_to_cpup(q->qpage + idx);
  223. idx = (idx + 1) & q->msk;
  224. i1 = be32_to_cpup(q->qpage + idx);
  225. xmon_printf("%s idx=%d T=%d %08x %08x ...", name,
  226. q->idx, q->toggle, i0, i1);
  227. }
  228. notrace void xmon_xive_do_dump(int cpu)
  229. {
  230. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  231. xmon_printf("CPU %d:", cpu);
  232. if (xc) {
  233. xmon_printf("pp=%02x CPPR=%02x ", xc->pending_prio, xc->cppr);
  234. #ifdef CONFIG_SMP
  235. {
  236. char buffer[128];
  237. xive_irq_data_dump(&xc->ipi_data, buffer, sizeof(buffer));
  238. xmon_printf("IPI=0x%08x %s", xc->hw_ipi, buffer);
  239. }
  240. #endif
  241. xive_dump_eq("EQ", &xc->queue[xive_irq_priority]);
  242. }
  243. xmon_printf("\n");
  244. }
  245. static struct irq_data *xive_get_irq_data(u32 hw_irq)
  246. {
  247. unsigned int irq = irq_find_mapping(xive_irq_domain, hw_irq);
  248. return irq ? irq_get_irq_data(irq) : NULL;
  249. }
  250. int xmon_xive_get_irq_config(u32 hw_irq, struct irq_data *d)
  251. {
  252. int rc;
  253. u32 target;
  254. u8 prio;
  255. u32 lirq;
  256. rc = xive_ops->get_irq_config(hw_irq, &target, &prio, &lirq);
  257. if (rc) {
  258. xmon_printf("IRQ 0x%08x : no config rc=%d\n", hw_irq, rc);
  259. return rc;
  260. }
  261. xmon_printf("IRQ 0x%08x : target=0x%x prio=%02x lirq=0x%x ",
  262. hw_irq, target, prio, lirq);
  263. if (!d)
  264. d = xive_get_irq_data(hw_irq);
  265. if (d) {
  266. char buffer[128];
  267. xive_irq_data_dump(irq_data_get_irq_handler_data(d),
  268. buffer, sizeof(buffer));
  269. xmon_printf("%s", buffer);
  270. }
  271. xmon_printf("\n");
  272. return 0;
  273. }
  274. void xmon_xive_get_irq_all(void)
  275. {
  276. unsigned int i;
  277. struct irq_desc *desc;
  278. for_each_irq_desc(i, desc) {
  279. struct irq_data *d = irq_domain_get_irq_data(xive_irq_domain, i);
  280. if (d)
  281. xmon_xive_get_irq_config(irqd_to_hwirq(d), d);
  282. }
  283. }
  284. #endif /* CONFIG_XMON */
  285. static unsigned int xive_get_irq(void)
  286. {
  287. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  288. u32 irq;
  289. /*
  290. * This can be called either as a result of a HW interrupt or
  291. * as a "replay" because EOI decided there was still something
  292. * in one of the queues.
  293. *
  294. * First we perform an ACK cycle in order to update our mask
  295. * of pending priorities. This will also have the effect of
  296. * updating the CPPR to the most favored pending interrupts.
  297. *
  298. * In the future, if we have a way to differentiate a first
  299. * entry (on HW interrupt) from a replay triggered by EOI,
  300. * we could skip this on replays unless we soft-mask tells us
  301. * that a new HW interrupt occurred.
  302. */
  303. xive_ops->update_pending(xc);
  304. DBG_VERBOSE("get_irq: pending=%02x\n", xc->pending_prio);
  305. /* Scan our queue(s) for interrupts */
  306. irq = xive_scan_interrupts(xc, false);
  307. DBG_VERBOSE("get_irq: got irq 0x%x, new pending=0x%02x\n",
  308. irq, xc->pending_prio);
  309. /* Return pending interrupt if any */
  310. if (irq == XIVE_BAD_IRQ)
  311. return 0;
  312. return irq;
  313. }
  314. /*
  315. * After EOI'ing an interrupt, we need to re-check the queue
  316. * to see if another interrupt is pending since multiple
  317. * interrupts can coalesce into a single notification to the
  318. * CPU.
  319. *
  320. * If we find that there is indeed more in there, we call
  321. * force_external_irq_replay() to make Linux synthetize an
  322. * external interrupt on the next call to local_irq_restore().
  323. */
  324. static void xive_do_queue_eoi(struct xive_cpu *xc)
  325. {
  326. if (xive_scan_interrupts(xc, true) != 0) {
  327. DBG_VERBOSE("eoi: pending=0x%02x\n", xc->pending_prio);
  328. force_external_irq_replay();
  329. }
  330. }
  331. /*
  332. * EOI an interrupt at the source. There are several methods
  333. * to do this depending on the HW version and source type
  334. */
  335. static void xive_do_source_eoi(struct xive_irq_data *xd)
  336. {
  337. u8 eoi_val;
  338. xd->stale_p = false;
  339. /* If the XIVE supports the new "store EOI facility, use it */
  340. if (xive_is_store_eoi(xd)) {
  341. xive_esb_write(xd, XIVE_ESB_STORE_EOI, 0);
  342. return;
  343. }
  344. /*
  345. * For LSIs, we use the "EOI cycle" special load rather than
  346. * PQ bits, as they are automatically re-triggered in HW when
  347. * still pending.
  348. */
  349. if (xd->flags & XIVE_IRQ_FLAG_LSI) {
  350. xive_esb_read(xd, XIVE_ESB_LOAD_EOI);
  351. return;
  352. }
  353. /*
  354. * Otherwise, we use the special MMIO that does a clear of
  355. * both P and Q and returns the old Q. This allows us to then
  356. * do a re-trigger if Q was set rather than synthesizing an
  357. * interrupt in software
  358. */
  359. eoi_val = xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
  360. DBG_VERBOSE("eoi_val=%x\n", eoi_val);
  361. /* Re-trigger if needed */
  362. if ((eoi_val & XIVE_ESB_VAL_Q) && xd->trig_mmio)
  363. out_be64(xd->trig_mmio, 0);
  364. }
  365. /* irq_chip eoi callback, called with irq descriptor lock held */
  366. static void xive_irq_eoi(struct irq_data *d)
  367. {
  368. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  369. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  370. DBG_VERBOSE("eoi_irq: irq=%d [0x%lx] pending=%02x\n",
  371. d->irq, irqd_to_hwirq(d), xc->pending_prio);
  372. /*
  373. * EOI the source if it hasn't been disabled and hasn't
  374. * been passed-through to a KVM guest
  375. */
  376. if (!irqd_irq_disabled(d) && !irqd_is_forwarded_to_vcpu(d) &&
  377. !(xd->flags & XIVE_IRQ_FLAG_NO_EOI))
  378. xive_do_source_eoi(xd);
  379. else
  380. xd->stale_p = true;
  381. /*
  382. * Clear saved_p to indicate that it's no longer occupying
  383. * a queue slot on the target queue
  384. */
  385. xd->saved_p = false;
  386. /* Check for more work in the queue */
  387. xive_do_queue_eoi(xc);
  388. }
  389. /*
  390. * Helper used to mask and unmask an interrupt source.
  391. */
  392. static void xive_do_source_set_mask(struct xive_irq_data *xd,
  393. bool mask)
  394. {
  395. u64 val;
  396. pr_debug("%s: HW 0x%x %smask\n", __func__, xd->hw_irq, mask ? "" : "un");
  397. /*
  398. * If the interrupt had P set, it may be in a queue.
  399. *
  400. * We need to make sure we don't re-enable it until it
  401. * has been fetched from that queue and EOId. We keep
  402. * a copy of that P state and use it to restore the
  403. * ESB accordingly on unmask.
  404. */
  405. if (mask) {
  406. val = xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
  407. if (!xd->stale_p && !!(val & XIVE_ESB_VAL_P))
  408. xd->saved_p = true;
  409. xd->stale_p = false;
  410. } else if (xd->saved_p) {
  411. xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
  412. xd->saved_p = false;
  413. } else {
  414. xive_esb_read(xd, XIVE_ESB_SET_PQ_00);
  415. xd->stale_p = false;
  416. }
  417. }
  418. /*
  419. * Try to chose "cpu" as a new interrupt target. Increments
  420. * the queue accounting for that target if it's not already
  421. * full.
  422. */
  423. static bool xive_try_pick_target(int cpu)
  424. {
  425. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  426. struct xive_q *q = &xc->queue[xive_irq_priority];
  427. int max;
  428. /*
  429. * Calculate max number of interrupts in that queue.
  430. *
  431. * We leave a gap of 1 just in case...
  432. */
  433. max = (q->msk + 1) - 1;
  434. return !!atomic_add_unless(&q->count, 1, max);
  435. }
  436. /*
  437. * Un-account an interrupt for a target CPU. We don't directly
  438. * decrement q->count since the interrupt might still be present
  439. * in the queue.
  440. *
  441. * Instead increment a separate counter "pending_count" which
  442. * will be substracted from "count" later when that CPU observes
  443. * the queue to be empty.
  444. */
  445. static void xive_dec_target_count(int cpu)
  446. {
  447. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  448. struct xive_q *q = &xc->queue[xive_irq_priority];
  449. if (WARN_ON(cpu < 0 || !xc)) {
  450. pr_err("%s: cpu=%d xc=%p\n", __func__, cpu, xc);
  451. return;
  452. }
  453. /*
  454. * We increment the "pending count" which will be used
  455. * to decrement the target queue count whenever it's next
  456. * processed and found empty. This ensure that we don't
  457. * decrement while we still have the interrupt there
  458. * occupying a slot.
  459. */
  460. atomic_inc(&q->pending_count);
  461. }
  462. /* Find a tentative CPU target in a CPU mask */
  463. static int xive_find_target_in_mask(const struct cpumask *mask,
  464. unsigned int fuzz)
  465. {
  466. int cpu, first, num, i;
  467. /* Pick up a starting point CPU in the mask based on fuzz */
  468. num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
  469. first = fuzz % num;
  470. /* Locate it */
  471. cpu = cpumask_first(mask);
  472. for (i = 0; i < first && cpu < nr_cpu_ids; i++)
  473. cpu = cpumask_next(cpu, mask);
  474. /* Sanity check */
  475. if (WARN_ON(cpu >= nr_cpu_ids))
  476. cpu = cpumask_first(cpu_online_mask);
  477. /* Remember first one to handle wrap-around */
  478. first = cpu;
  479. /*
  480. * Now go through the entire mask until we find a valid
  481. * target.
  482. */
  483. do {
  484. /*
  485. * We re-check online as the fallback case passes us
  486. * an untested affinity mask
  487. */
  488. if (cpu_online(cpu) && xive_try_pick_target(cpu))
  489. return cpu;
  490. cpu = cpumask_next(cpu, mask);
  491. /* Wrap around */
  492. if (cpu >= nr_cpu_ids)
  493. cpu = cpumask_first(mask);
  494. } while (cpu != first);
  495. return -1;
  496. }
  497. /*
  498. * Pick a target CPU for an interrupt. This is done at
  499. * startup or if the affinity is changed in a way that
  500. * invalidates the current target.
  501. */
  502. static int xive_pick_irq_target(struct irq_data *d,
  503. const struct cpumask *affinity)
  504. {
  505. static unsigned int fuzz;
  506. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  507. cpumask_var_t mask;
  508. int cpu = -1;
  509. /*
  510. * If we have chip IDs, first we try to build a mask of
  511. * CPUs matching the CPU and find a target in there
  512. */
  513. if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
  514. zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
  515. /* Build a mask of matching chip IDs */
  516. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  517. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  518. if (xc->chip_id == xd->src_chip)
  519. cpumask_set_cpu(cpu, mask);
  520. }
  521. /* Try to find a target */
  522. if (cpumask_empty(mask))
  523. cpu = -1;
  524. else
  525. cpu = xive_find_target_in_mask(mask, fuzz++);
  526. free_cpumask_var(mask);
  527. if (cpu >= 0)
  528. return cpu;
  529. fuzz--;
  530. }
  531. /* No chip IDs, fallback to using the affinity mask */
  532. return xive_find_target_in_mask(affinity, fuzz++);
  533. }
  534. static unsigned int xive_irq_startup(struct irq_data *d)
  535. {
  536. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  537. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  538. int target, rc;
  539. xd->saved_p = false;
  540. xd->stale_p = false;
  541. pr_debug("%s: irq %d [0x%x] data @%p\n", __func__, d->irq, hw_irq, d);
  542. /* Pick a target */
  543. target = xive_pick_irq_target(d, irq_data_get_affinity_mask(d));
  544. if (target == XIVE_INVALID_TARGET) {
  545. /* Try again breaking affinity */
  546. target = xive_pick_irq_target(d, cpu_online_mask);
  547. if (target == XIVE_INVALID_TARGET)
  548. return -ENXIO;
  549. pr_warn("irq %d started with broken affinity\n", d->irq);
  550. }
  551. /* Sanity check */
  552. if (WARN_ON(target == XIVE_INVALID_TARGET ||
  553. target >= nr_cpu_ids))
  554. target = smp_processor_id();
  555. xd->target = target;
  556. /*
  557. * Configure the logical number to be the Linux IRQ number
  558. * and set the target queue
  559. */
  560. rc = xive_ops->configure_irq(hw_irq,
  561. get_hard_smp_processor_id(target),
  562. xive_irq_priority, d->irq);
  563. if (rc)
  564. return rc;
  565. /* Unmask the ESB */
  566. xive_do_source_set_mask(xd, false);
  567. return 0;
  568. }
  569. /* called with irq descriptor lock held */
  570. static void xive_irq_shutdown(struct irq_data *d)
  571. {
  572. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  573. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  574. pr_debug("%s: irq %d [0x%x] data @%p\n", __func__, d->irq, hw_irq, d);
  575. if (WARN_ON(xd->target == XIVE_INVALID_TARGET))
  576. return;
  577. /* Mask the interrupt at the source */
  578. xive_do_source_set_mask(xd, true);
  579. /*
  580. * Mask the interrupt in HW in the IVT/EAS and set the number
  581. * to be the "bad" IRQ number
  582. */
  583. xive_ops->configure_irq(hw_irq,
  584. get_hard_smp_processor_id(xd->target),
  585. 0xff, XIVE_BAD_IRQ);
  586. xive_dec_target_count(xd->target);
  587. xd->target = XIVE_INVALID_TARGET;
  588. }
  589. static void xive_irq_unmask(struct irq_data *d)
  590. {
  591. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  592. pr_debug("%s: irq %d data @%p\n", __func__, d->irq, xd);
  593. xive_do_source_set_mask(xd, false);
  594. }
  595. static void xive_irq_mask(struct irq_data *d)
  596. {
  597. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  598. pr_debug("%s: irq %d data @%p\n", __func__, d->irq, xd);
  599. xive_do_source_set_mask(xd, true);
  600. }
  601. static int xive_irq_set_affinity(struct irq_data *d,
  602. const struct cpumask *cpumask,
  603. bool force)
  604. {
  605. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  606. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  607. u32 target, old_target;
  608. int rc = 0;
  609. pr_debug("%s: irq %d/0x%x\n", __func__, d->irq, hw_irq);
  610. /* Is this valid ? */
  611. if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids)
  612. return -EINVAL;
  613. /*
  614. * If existing target is already in the new mask, and is
  615. * online then do nothing.
  616. */
  617. if (xd->target != XIVE_INVALID_TARGET &&
  618. cpu_online(xd->target) &&
  619. cpumask_test_cpu(xd->target, cpumask))
  620. return IRQ_SET_MASK_OK;
  621. /* Pick a new target */
  622. target = xive_pick_irq_target(d, cpumask);
  623. /* No target found */
  624. if (target == XIVE_INVALID_TARGET)
  625. return -ENXIO;
  626. /* Sanity check */
  627. if (WARN_ON(target >= nr_cpu_ids))
  628. target = smp_processor_id();
  629. old_target = xd->target;
  630. /*
  631. * Only configure the irq if it's not currently passed-through to
  632. * a KVM guest
  633. */
  634. if (!irqd_is_forwarded_to_vcpu(d))
  635. rc = xive_ops->configure_irq(hw_irq,
  636. get_hard_smp_processor_id(target),
  637. xive_irq_priority, d->irq);
  638. if (rc < 0) {
  639. pr_err("Error %d reconfiguring irq %d\n", rc, d->irq);
  640. return rc;
  641. }
  642. pr_debug(" target: 0x%x\n", target);
  643. xd->target = target;
  644. /* Give up previous target */
  645. if (old_target != XIVE_INVALID_TARGET)
  646. xive_dec_target_count(old_target);
  647. return IRQ_SET_MASK_OK;
  648. }
  649. static int xive_irq_set_type(struct irq_data *d, unsigned int flow_type)
  650. {
  651. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  652. /*
  653. * We only support these. This has really no effect other than setting
  654. * the corresponding descriptor bits mind you but those will in turn
  655. * affect the resend function when re-enabling an edge interrupt.
  656. *
  657. * Set the default to edge as explained in map().
  658. */
  659. if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
  660. flow_type = IRQ_TYPE_EDGE_RISING;
  661. if (flow_type != IRQ_TYPE_EDGE_RISING &&
  662. flow_type != IRQ_TYPE_LEVEL_LOW)
  663. return -EINVAL;
  664. irqd_set_trigger_type(d, flow_type);
  665. /*
  666. * Double check it matches what the FW thinks
  667. *
  668. * NOTE: We don't know yet if the PAPR interface will provide
  669. * the LSI vs MSI information apart from the device-tree so
  670. * this check might have to move into an optional backend call
  671. * that is specific to the native backend
  672. */
  673. if ((flow_type == IRQ_TYPE_LEVEL_LOW) !=
  674. !!(xd->flags & XIVE_IRQ_FLAG_LSI)) {
  675. pr_warn("Interrupt %d (HW 0x%x) type mismatch, Linux says %s, FW says %s\n",
  676. d->irq, (u32)irqd_to_hwirq(d),
  677. (flow_type == IRQ_TYPE_LEVEL_LOW) ? "Level" : "Edge",
  678. (xd->flags & XIVE_IRQ_FLAG_LSI) ? "Level" : "Edge");
  679. }
  680. return IRQ_SET_MASK_OK_NOCOPY;
  681. }
  682. static int xive_irq_retrigger(struct irq_data *d)
  683. {
  684. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  685. /* This should be only for MSIs */
  686. if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
  687. return 0;
  688. /*
  689. * To perform a retrigger, we first set the PQ bits to
  690. * 11, then perform an EOI.
  691. */
  692. xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
  693. xive_do_source_eoi(xd);
  694. return 1;
  695. }
  696. /*
  697. * Caller holds the irq descriptor lock, so this won't be called
  698. * concurrently with xive_get_irqchip_state on the same interrupt.
  699. */
  700. static int xive_irq_set_vcpu_affinity(struct irq_data *d, void *state)
  701. {
  702. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  703. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  704. int rc;
  705. u8 pq;
  706. /*
  707. * This is called by KVM with state non-NULL for enabling
  708. * pass-through or NULL for disabling it
  709. */
  710. if (state) {
  711. irqd_set_forwarded_to_vcpu(d);
  712. /* Set it to PQ=10 state to prevent further sends */
  713. pq = xive_esb_read(xd, XIVE_ESB_SET_PQ_10);
  714. if (!xd->stale_p) {
  715. xd->saved_p = !!(pq & XIVE_ESB_VAL_P);
  716. xd->stale_p = !xd->saved_p;
  717. }
  718. /* No target ? nothing to do */
  719. if (xd->target == XIVE_INVALID_TARGET) {
  720. /*
  721. * An untargetted interrupt should have been
  722. * also masked at the source
  723. */
  724. WARN_ON(xd->saved_p);
  725. return 0;
  726. }
  727. /*
  728. * If P was set, adjust state to PQ=11 to indicate
  729. * that a resend is needed for the interrupt to reach
  730. * the guest. Also remember the value of P.
  731. *
  732. * This also tells us that it's in flight to a host queue
  733. * or has already been fetched but hasn't been EOIed yet
  734. * by the host. This it's potentially using up a host
  735. * queue slot. This is important to know because as long
  736. * as this is the case, we must not hard-unmask it when
  737. * "returning" that interrupt to the host.
  738. *
  739. * This saved_p is cleared by the host EOI, when we know
  740. * for sure the queue slot is no longer in use.
  741. */
  742. if (xd->saved_p) {
  743. xive_esb_read(xd, XIVE_ESB_SET_PQ_11);
  744. /*
  745. * Sync the XIVE source HW to ensure the interrupt
  746. * has gone through the EAS before we change its
  747. * target to the guest. That should guarantee us
  748. * that we *will* eventually get an EOI for it on
  749. * the host. Otherwise there would be a small window
  750. * for P to be seen here but the interrupt going
  751. * to the guest queue.
  752. */
  753. if (xive_ops->sync_source)
  754. xive_ops->sync_source(hw_irq);
  755. }
  756. } else {
  757. irqd_clr_forwarded_to_vcpu(d);
  758. /* No host target ? hard mask and return */
  759. if (xd->target == XIVE_INVALID_TARGET) {
  760. xive_do_source_set_mask(xd, true);
  761. return 0;
  762. }
  763. /*
  764. * Sync the XIVE source HW to ensure the interrupt
  765. * has gone through the EAS before we change its
  766. * target to the host.
  767. */
  768. if (xive_ops->sync_source)
  769. xive_ops->sync_source(hw_irq);
  770. /*
  771. * By convention we are called with the interrupt in
  772. * a PQ=10 or PQ=11 state, ie, it won't fire and will
  773. * have latched in Q whether there's a pending HW
  774. * interrupt or not.
  775. *
  776. * First reconfigure the target.
  777. */
  778. rc = xive_ops->configure_irq(hw_irq,
  779. get_hard_smp_processor_id(xd->target),
  780. xive_irq_priority, d->irq);
  781. if (rc)
  782. return rc;
  783. /*
  784. * Then if saved_p is not set, effectively re-enable the
  785. * interrupt with an EOI. If it is set, we know there is
  786. * still a message in a host queue somewhere that will be
  787. * EOId eventually.
  788. *
  789. * Note: We don't check irqd_irq_disabled(). Effectively,
  790. * we *will* let the irq get through even if masked if the
  791. * HW is still firing it in order to deal with the whole
  792. * saved_p business properly. If the interrupt triggers
  793. * while masked, the generic code will re-mask it anyway.
  794. */
  795. if (!xd->saved_p)
  796. xive_do_source_eoi(xd);
  797. }
  798. return 0;
  799. }
  800. /* Called with irq descriptor lock held. */
  801. static int xive_get_irqchip_state(struct irq_data *data,
  802. enum irqchip_irq_state which, bool *state)
  803. {
  804. struct xive_irq_data *xd = irq_data_get_irq_handler_data(data);
  805. u8 pq;
  806. switch (which) {
  807. case IRQCHIP_STATE_ACTIVE:
  808. pq = xive_esb_read(xd, XIVE_ESB_GET);
  809. /*
  810. * The esb value being all 1's means we couldn't get
  811. * the PQ state of the interrupt through mmio. It may
  812. * happen, for example when querying a PHB interrupt
  813. * while the PHB is in an error state. We consider the
  814. * interrupt to be inactive in that case.
  815. */
  816. *state = (pq != XIVE_ESB_INVALID) && !xd->stale_p &&
  817. (xd->saved_p || (!!(pq & XIVE_ESB_VAL_P) &&
  818. !irqd_irq_disabled(data)));
  819. return 0;
  820. default:
  821. return -EINVAL;
  822. }
  823. }
  824. static struct irq_chip xive_irq_chip = {
  825. .name = "XIVE-IRQ",
  826. .irq_startup = xive_irq_startup,
  827. .irq_shutdown = xive_irq_shutdown,
  828. .irq_eoi = xive_irq_eoi,
  829. .irq_mask = xive_irq_mask,
  830. .irq_unmask = xive_irq_unmask,
  831. .irq_set_affinity = xive_irq_set_affinity,
  832. .irq_set_type = xive_irq_set_type,
  833. .irq_retrigger = xive_irq_retrigger,
  834. .irq_set_vcpu_affinity = xive_irq_set_vcpu_affinity,
  835. .irq_get_irqchip_state = xive_get_irqchip_state,
  836. };
  837. bool is_xive_irq(struct irq_chip *chip)
  838. {
  839. return chip == &xive_irq_chip;
  840. }
  841. EXPORT_SYMBOL_GPL(is_xive_irq);
  842. void xive_cleanup_irq_data(struct xive_irq_data *xd)
  843. {
  844. pr_debug("%s for HW 0x%x\n", __func__, xd->hw_irq);
  845. if (xd->eoi_mmio) {
  846. iounmap(xd->eoi_mmio);
  847. if (xd->eoi_mmio == xd->trig_mmio)
  848. xd->trig_mmio = NULL;
  849. xd->eoi_mmio = NULL;
  850. }
  851. if (xd->trig_mmio) {
  852. iounmap(xd->trig_mmio);
  853. xd->trig_mmio = NULL;
  854. }
  855. }
  856. EXPORT_SYMBOL_GPL(xive_cleanup_irq_data);
  857. static int xive_irq_alloc_data(unsigned int virq, irq_hw_number_t hw)
  858. {
  859. struct xive_irq_data *xd;
  860. int rc;
  861. xd = kzalloc(sizeof(struct xive_irq_data), GFP_KERNEL);
  862. if (!xd)
  863. return -ENOMEM;
  864. rc = xive_ops->populate_irq_data(hw, xd);
  865. if (rc) {
  866. kfree(xd);
  867. return rc;
  868. }
  869. xd->target = XIVE_INVALID_TARGET;
  870. irq_set_handler_data(virq, xd);
  871. /*
  872. * Turn OFF by default the interrupt being mapped. A side
  873. * effect of this check is the mapping the ESB page of the
  874. * interrupt in the Linux address space. This prevents page
  875. * fault issues in the crash handler which masks all
  876. * interrupts.
  877. */
  878. xive_esb_read(xd, XIVE_ESB_SET_PQ_01);
  879. return 0;
  880. }
  881. void xive_irq_free_data(unsigned int virq)
  882. {
  883. struct xive_irq_data *xd = irq_get_handler_data(virq);
  884. if (!xd)
  885. return;
  886. irq_set_handler_data(virq, NULL);
  887. xive_cleanup_irq_data(xd);
  888. kfree(xd);
  889. }
  890. EXPORT_SYMBOL_GPL(xive_irq_free_data);
  891. #ifdef CONFIG_SMP
  892. static void xive_cause_ipi(int cpu)
  893. {
  894. struct xive_cpu *xc;
  895. struct xive_irq_data *xd;
  896. xc = per_cpu(xive_cpu, cpu);
  897. DBG_VERBOSE("IPI CPU %d -> %d (HW IRQ 0x%x)\n",
  898. smp_processor_id(), cpu, xc->hw_ipi);
  899. xd = &xc->ipi_data;
  900. if (WARN_ON(!xd->trig_mmio))
  901. return;
  902. out_be64(xd->trig_mmio, 0);
  903. }
  904. static irqreturn_t xive_muxed_ipi_action(int irq, void *dev_id)
  905. {
  906. return smp_ipi_demux();
  907. }
  908. static void xive_ipi_eoi(struct irq_data *d)
  909. {
  910. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  911. /* Handle possible race with unplug and drop stale IPIs */
  912. if (!xc)
  913. return;
  914. DBG_VERBOSE("IPI eoi: irq=%d [0x%lx] (HW IRQ 0x%x) pending=%02x\n",
  915. d->irq, irqd_to_hwirq(d), xc->hw_ipi, xc->pending_prio);
  916. xive_do_source_eoi(&xc->ipi_data);
  917. xive_do_queue_eoi(xc);
  918. }
  919. static void xive_ipi_do_nothing(struct irq_data *d)
  920. {
  921. /*
  922. * Nothing to do, we never mask/unmask IPIs, but the callback
  923. * has to exist for the struct irq_chip.
  924. */
  925. }
  926. static struct irq_chip xive_ipi_chip = {
  927. .name = "XIVE-IPI",
  928. .irq_eoi = xive_ipi_eoi,
  929. .irq_mask = xive_ipi_do_nothing,
  930. .irq_unmask = xive_ipi_do_nothing,
  931. };
  932. /*
  933. * IPIs are marked per-cpu. We use separate HW interrupts under the
  934. * hood but associated with the same "linux" interrupt
  935. */
  936. struct xive_ipi_alloc_info {
  937. irq_hw_number_t hwirq;
  938. };
  939. static int xive_ipi_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  940. unsigned int nr_irqs, void *arg)
  941. {
  942. struct xive_ipi_alloc_info *info = arg;
  943. int i;
  944. for (i = 0; i < nr_irqs; i++) {
  945. irq_domain_set_info(domain, virq + i, info->hwirq + i, &xive_ipi_chip,
  946. domain->host_data, handle_percpu_irq,
  947. NULL, NULL);
  948. }
  949. return 0;
  950. }
  951. static const struct irq_domain_ops xive_ipi_irq_domain_ops = {
  952. .alloc = xive_ipi_irq_domain_alloc,
  953. };
  954. static int __init xive_init_ipis(void)
  955. {
  956. struct fwnode_handle *fwnode;
  957. struct irq_domain *ipi_domain;
  958. unsigned int node;
  959. int ret = -ENOMEM;
  960. fwnode = irq_domain_alloc_named_fwnode("XIVE-IPI");
  961. if (!fwnode)
  962. goto out;
  963. ipi_domain = irq_domain_create_linear(fwnode, nr_node_ids,
  964. &xive_ipi_irq_domain_ops, NULL);
  965. if (!ipi_domain)
  966. goto out_free_fwnode;
  967. xive_ipis = kcalloc(nr_node_ids, sizeof(*xive_ipis), GFP_KERNEL | __GFP_NOFAIL);
  968. if (!xive_ipis)
  969. goto out_free_domain;
  970. for_each_node(node) {
  971. struct xive_ipi_desc *xid = &xive_ipis[node];
  972. struct xive_ipi_alloc_info info = { node };
  973. /*
  974. * Map one IPI interrupt per node for all cpus of that node.
  975. * Since the HW interrupt number doesn't have any meaning,
  976. * simply use the node number.
  977. */
  978. ret = irq_domain_alloc_irqs(ipi_domain, 1, node, &info);
  979. if (ret < 0)
  980. goto out_free_xive_ipis;
  981. xid->irq = ret;
  982. snprintf(xid->name, sizeof(xid->name), "IPI-%d", node);
  983. }
  984. return ret;
  985. out_free_xive_ipis:
  986. kfree(xive_ipis);
  987. out_free_domain:
  988. irq_domain_remove(ipi_domain);
  989. out_free_fwnode:
  990. irq_domain_free_fwnode(fwnode);
  991. out:
  992. return ret;
  993. }
  994. static int xive_request_ipi(unsigned int cpu)
  995. {
  996. struct xive_ipi_desc *xid = &xive_ipis[early_cpu_to_node(cpu)];
  997. int ret;
  998. if (atomic_inc_return(&xid->started) > 1)
  999. return 0;
  1000. ret = request_irq(xid->irq, xive_muxed_ipi_action,
  1001. IRQF_NO_DEBUG | IRQF_PERCPU | IRQF_NO_THREAD,
  1002. xid->name, NULL);
  1003. WARN(ret < 0, "Failed to request IPI %d: %d\n", xid->irq, ret);
  1004. return ret;
  1005. }
  1006. static int xive_setup_cpu_ipi(unsigned int cpu)
  1007. {
  1008. unsigned int xive_ipi_irq = xive_ipi_cpu_to_irq(cpu);
  1009. struct xive_cpu *xc;
  1010. int rc;
  1011. pr_debug("Setting up IPI for CPU %d\n", cpu);
  1012. xc = per_cpu(xive_cpu, cpu);
  1013. /* Check if we are already setup */
  1014. if (xc->hw_ipi != XIVE_BAD_IRQ)
  1015. return 0;
  1016. /* Register the IPI */
  1017. xive_request_ipi(cpu);
  1018. /* Grab an IPI from the backend, this will populate xc->hw_ipi */
  1019. if (xive_ops->get_ipi(cpu, xc))
  1020. return -EIO;
  1021. /*
  1022. * Populate the IRQ data in the xive_cpu structure and
  1023. * configure the HW / enable the IPIs.
  1024. */
  1025. rc = xive_ops->populate_irq_data(xc->hw_ipi, &xc->ipi_data);
  1026. if (rc) {
  1027. pr_err("Failed to populate IPI data on CPU %d\n", cpu);
  1028. return -EIO;
  1029. }
  1030. rc = xive_ops->configure_irq(xc->hw_ipi,
  1031. get_hard_smp_processor_id(cpu),
  1032. xive_irq_priority, xive_ipi_irq);
  1033. if (rc) {
  1034. pr_err("Failed to map IPI CPU %d\n", cpu);
  1035. return -EIO;
  1036. }
  1037. pr_debug("CPU %d HW IPI 0x%x, virq %d, trig_mmio=%p\n", cpu,
  1038. xc->hw_ipi, xive_ipi_irq, xc->ipi_data.trig_mmio);
  1039. /* Unmask it */
  1040. xive_do_source_set_mask(&xc->ipi_data, false);
  1041. return 0;
  1042. }
  1043. noinstr static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc)
  1044. {
  1045. unsigned int xive_ipi_irq = xive_ipi_cpu_to_irq(cpu);
  1046. /* Disable the IPI and free the IRQ data */
  1047. /* Already cleaned up ? */
  1048. if (xc->hw_ipi == XIVE_BAD_IRQ)
  1049. return;
  1050. /* TODO: clear IPI mapping */
  1051. /* Mask the IPI */
  1052. xive_do_source_set_mask(&xc->ipi_data, true);
  1053. /*
  1054. * Note: We don't call xive_cleanup_irq_data() to free
  1055. * the mappings as this is called from an IPI on kexec
  1056. * which is not a safe environment to call iounmap()
  1057. */
  1058. /* Deconfigure/mask in the backend */
  1059. xive_ops->configure_irq(xc->hw_ipi, hard_smp_processor_id(),
  1060. 0xff, xive_ipi_irq);
  1061. /* Free the IPIs in the backend */
  1062. xive_ops->put_ipi(cpu, xc);
  1063. }
  1064. void __init xive_smp_probe(void)
  1065. {
  1066. smp_ops->cause_ipi = xive_cause_ipi;
  1067. /* Register the IPI */
  1068. xive_init_ipis();
  1069. /* Allocate and setup IPI for the boot CPU */
  1070. xive_setup_cpu_ipi(smp_processor_id());
  1071. }
  1072. #endif /* CONFIG_SMP */
  1073. static int xive_irq_domain_map(struct irq_domain *h, unsigned int virq,
  1074. irq_hw_number_t hw)
  1075. {
  1076. int rc;
  1077. /*
  1078. * Mark interrupts as edge sensitive by default so that resend
  1079. * actually works. Will fix that up below if needed.
  1080. */
  1081. irq_clear_status_flags(virq, IRQ_LEVEL);
  1082. rc = xive_irq_alloc_data(virq, hw);
  1083. if (rc)
  1084. return rc;
  1085. irq_set_chip_and_handler(virq, &xive_irq_chip, handle_fasteoi_irq);
  1086. return 0;
  1087. }
  1088. static void xive_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
  1089. {
  1090. xive_irq_free_data(virq);
  1091. }
  1092. static int xive_irq_domain_xlate(struct irq_domain *h, struct device_node *ct,
  1093. const u32 *intspec, unsigned int intsize,
  1094. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  1095. {
  1096. *out_hwirq = intspec[0];
  1097. /*
  1098. * If intsize is at least 2, we look for the type in the second cell,
  1099. * we assume the LSB indicates a level interrupt.
  1100. */
  1101. if (intsize > 1) {
  1102. if (intspec[1] & 1)
  1103. *out_flags = IRQ_TYPE_LEVEL_LOW;
  1104. else
  1105. *out_flags = IRQ_TYPE_EDGE_RISING;
  1106. } else
  1107. *out_flags = IRQ_TYPE_LEVEL_LOW;
  1108. return 0;
  1109. }
  1110. static int xive_irq_domain_match(struct irq_domain *h, struct device_node *node,
  1111. enum irq_domain_bus_token bus_token)
  1112. {
  1113. return xive_ops->match(node);
  1114. }
  1115. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  1116. static const char * const esb_names[] = { "RESET", "OFF", "PENDING", "QUEUED" };
  1117. static const struct {
  1118. u64 mask;
  1119. char *name;
  1120. } xive_irq_flags[] = {
  1121. { XIVE_IRQ_FLAG_STORE_EOI, "STORE_EOI" },
  1122. { XIVE_IRQ_FLAG_LSI, "LSI" },
  1123. { XIVE_IRQ_FLAG_H_INT_ESB, "H_INT_ESB" },
  1124. { XIVE_IRQ_FLAG_NO_EOI, "NO_EOI" },
  1125. };
  1126. static void xive_irq_domain_debug_show(struct seq_file *m, struct irq_domain *d,
  1127. struct irq_data *irqd, int ind)
  1128. {
  1129. struct xive_irq_data *xd;
  1130. u64 val;
  1131. int i;
  1132. /* No IRQ domain level information. To be done */
  1133. if (!irqd)
  1134. return;
  1135. if (!is_xive_irq(irq_data_get_irq_chip(irqd)))
  1136. return;
  1137. seq_printf(m, "%*sXIVE:\n", ind, "");
  1138. ind++;
  1139. xd = irq_data_get_irq_handler_data(irqd);
  1140. if (!xd) {
  1141. seq_printf(m, "%*snot assigned\n", ind, "");
  1142. return;
  1143. }
  1144. val = xive_esb_read(xd, XIVE_ESB_GET);
  1145. seq_printf(m, "%*sESB: %s\n", ind, "", esb_names[val & 0x3]);
  1146. seq_printf(m, "%*sPstate: %s %s\n", ind, "", xd->stale_p ? "stale" : "",
  1147. xd->saved_p ? "saved" : "");
  1148. seq_printf(m, "%*sTarget: %d\n", ind, "", xd->target);
  1149. seq_printf(m, "%*sChip: %d\n", ind, "", xd->src_chip);
  1150. seq_printf(m, "%*sTrigger: 0x%016llx\n", ind, "", xd->trig_page);
  1151. seq_printf(m, "%*sEOI: 0x%016llx\n", ind, "", xd->eoi_page);
  1152. seq_printf(m, "%*sFlags: 0x%llx\n", ind, "", xd->flags);
  1153. for (i = 0; i < ARRAY_SIZE(xive_irq_flags); i++) {
  1154. if (xd->flags & xive_irq_flags[i].mask)
  1155. seq_printf(m, "%*s%s\n", ind + 12, "", xive_irq_flags[i].name);
  1156. }
  1157. }
  1158. #endif
  1159. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  1160. static int xive_irq_domain_translate(struct irq_domain *d,
  1161. struct irq_fwspec *fwspec,
  1162. unsigned long *hwirq,
  1163. unsigned int *type)
  1164. {
  1165. return xive_irq_domain_xlate(d, to_of_node(fwspec->fwnode),
  1166. fwspec->param, fwspec->param_count,
  1167. hwirq, type);
  1168. }
  1169. static int xive_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1170. unsigned int nr_irqs, void *arg)
  1171. {
  1172. struct irq_fwspec *fwspec = arg;
  1173. irq_hw_number_t hwirq;
  1174. unsigned int type = IRQ_TYPE_NONE;
  1175. int i, rc;
  1176. rc = xive_irq_domain_translate(domain, fwspec, &hwirq, &type);
  1177. if (rc)
  1178. return rc;
  1179. pr_debug("%s %d/0x%lx #%d\n", __func__, virq, hwirq, nr_irqs);
  1180. for (i = 0; i < nr_irqs; i++) {
  1181. /* TODO: call xive_irq_domain_map() */
  1182. /*
  1183. * Mark interrupts as edge sensitive by default so that resend
  1184. * actually works. Will fix that up below if needed.
  1185. */
  1186. irq_clear_status_flags(virq, IRQ_LEVEL);
  1187. /* allocates and sets handler data */
  1188. rc = xive_irq_alloc_data(virq + i, hwirq + i);
  1189. if (rc)
  1190. return rc;
  1191. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  1192. &xive_irq_chip, domain->host_data);
  1193. irq_set_handler(virq + i, handle_fasteoi_irq);
  1194. }
  1195. return 0;
  1196. }
  1197. static void xive_irq_domain_free(struct irq_domain *domain,
  1198. unsigned int virq, unsigned int nr_irqs)
  1199. {
  1200. int i;
  1201. pr_debug("%s %d #%d\n", __func__, virq, nr_irqs);
  1202. for (i = 0; i < nr_irqs; i++)
  1203. xive_irq_free_data(virq + i);
  1204. }
  1205. #endif
  1206. static const struct irq_domain_ops xive_irq_domain_ops = {
  1207. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  1208. .alloc = xive_irq_domain_alloc,
  1209. .free = xive_irq_domain_free,
  1210. .translate = xive_irq_domain_translate,
  1211. #endif
  1212. .match = xive_irq_domain_match,
  1213. .map = xive_irq_domain_map,
  1214. .unmap = xive_irq_domain_unmap,
  1215. .xlate = xive_irq_domain_xlate,
  1216. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  1217. .debug_show = xive_irq_domain_debug_show,
  1218. #endif
  1219. };
  1220. static void __init xive_init_host(struct device_node *np)
  1221. {
  1222. xive_irq_domain = irq_domain_add_tree(np, &xive_irq_domain_ops, NULL);
  1223. if (WARN_ON(xive_irq_domain == NULL))
  1224. return;
  1225. irq_set_default_host(xive_irq_domain);
  1226. }
  1227. static void xive_cleanup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1228. {
  1229. if (xc->queue[xive_irq_priority].qpage)
  1230. xive_ops->cleanup_queue(cpu, xc, xive_irq_priority);
  1231. }
  1232. static int xive_setup_cpu_queues(unsigned int cpu, struct xive_cpu *xc)
  1233. {
  1234. int rc = 0;
  1235. /* We setup 1 queues for now with a 64k page */
  1236. if (!xc->queue[xive_irq_priority].qpage)
  1237. rc = xive_ops->setup_queue(cpu, xc, xive_irq_priority);
  1238. return rc;
  1239. }
  1240. static int xive_prepare_cpu(unsigned int cpu)
  1241. {
  1242. struct xive_cpu *xc;
  1243. xc = per_cpu(xive_cpu, cpu);
  1244. if (!xc) {
  1245. xc = kzalloc_node(sizeof(struct xive_cpu),
  1246. GFP_KERNEL, cpu_to_node(cpu));
  1247. if (!xc)
  1248. return -ENOMEM;
  1249. xc->hw_ipi = XIVE_BAD_IRQ;
  1250. xc->chip_id = XIVE_INVALID_CHIP_ID;
  1251. if (xive_ops->prepare_cpu)
  1252. xive_ops->prepare_cpu(cpu, xc);
  1253. per_cpu(xive_cpu, cpu) = xc;
  1254. }
  1255. /* Setup EQs if not already */
  1256. return xive_setup_cpu_queues(cpu, xc);
  1257. }
  1258. static void xive_setup_cpu(void)
  1259. {
  1260. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1261. /* The backend might have additional things to do */
  1262. if (xive_ops->setup_cpu)
  1263. xive_ops->setup_cpu(smp_processor_id(), xc);
  1264. /* Set CPPR to 0xff to enable flow of interrupts */
  1265. xc->cppr = 0xff;
  1266. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1267. }
  1268. #ifdef CONFIG_SMP
  1269. void xive_smp_setup_cpu(void)
  1270. {
  1271. pr_debug("SMP setup CPU %d\n", smp_processor_id());
  1272. /* This will have already been done on the boot CPU */
  1273. if (smp_processor_id() != boot_cpuid)
  1274. xive_setup_cpu();
  1275. }
  1276. int xive_smp_prepare_cpu(unsigned int cpu)
  1277. {
  1278. int rc;
  1279. /* Allocate per-CPU data and queues */
  1280. rc = xive_prepare_cpu(cpu);
  1281. if (rc)
  1282. return rc;
  1283. /* Allocate and setup IPI for the new CPU */
  1284. return xive_setup_cpu_ipi(cpu);
  1285. }
  1286. #ifdef CONFIG_HOTPLUG_CPU
  1287. static void xive_flush_cpu_queue(unsigned int cpu, struct xive_cpu *xc)
  1288. {
  1289. u32 irq;
  1290. /* We assume local irqs are disabled */
  1291. WARN_ON(!irqs_disabled());
  1292. /* Check what's already in the CPU queue */
  1293. while ((irq = xive_scan_interrupts(xc, false)) != 0) {
  1294. /*
  1295. * We need to re-route that interrupt to its new destination.
  1296. * First get and lock the descriptor
  1297. */
  1298. struct irq_desc *desc = irq_to_desc(irq);
  1299. struct irq_data *d = irq_desc_get_irq_data(desc);
  1300. struct xive_irq_data *xd;
  1301. /*
  1302. * Ignore anything that isn't a XIVE irq and ignore
  1303. * IPIs, so can just be dropped.
  1304. */
  1305. if (d->domain != xive_irq_domain)
  1306. continue;
  1307. /*
  1308. * The IRQ should have already been re-routed, it's just a
  1309. * stale in the old queue, so re-trigger it in order to make
  1310. * it reach is new destination.
  1311. */
  1312. #ifdef DEBUG_FLUSH
  1313. pr_info("CPU %d: Got irq %d while offline, re-sending...\n",
  1314. cpu, irq);
  1315. #endif
  1316. raw_spin_lock(&desc->lock);
  1317. xd = irq_desc_get_handler_data(desc);
  1318. /*
  1319. * Clear saved_p to indicate that it's no longer pending
  1320. */
  1321. xd->saved_p = false;
  1322. /*
  1323. * For LSIs, we EOI, this will cause a resend if it's
  1324. * still asserted. Otherwise do an MSI retrigger.
  1325. */
  1326. if (xd->flags & XIVE_IRQ_FLAG_LSI)
  1327. xive_do_source_eoi(xd);
  1328. else
  1329. xive_irq_retrigger(d);
  1330. raw_spin_unlock(&desc->lock);
  1331. }
  1332. }
  1333. void xive_smp_disable_cpu(void)
  1334. {
  1335. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1336. unsigned int cpu = smp_processor_id();
  1337. /* Migrate interrupts away from the CPU */
  1338. irq_migrate_all_off_this_cpu();
  1339. /* Set CPPR to 0 to disable flow of interrupts */
  1340. xc->cppr = 0;
  1341. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1342. /* Flush everything still in the queue */
  1343. xive_flush_cpu_queue(cpu, xc);
  1344. /* Re-enable CPPR */
  1345. xc->cppr = 0xff;
  1346. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0xff);
  1347. }
  1348. void xive_flush_interrupt(void)
  1349. {
  1350. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1351. unsigned int cpu = smp_processor_id();
  1352. /* Called if an interrupt occurs while the CPU is hot unplugged */
  1353. xive_flush_cpu_queue(cpu, xc);
  1354. }
  1355. #endif /* CONFIG_HOTPLUG_CPU */
  1356. #endif /* CONFIG_SMP */
  1357. noinstr void xive_teardown_cpu(void)
  1358. {
  1359. struct xive_cpu *xc = __this_cpu_read(xive_cpu);
  1360. unsigned int cpu = smp_processor_id();
  1361. /* Set CPPR to 0 to disable flow of interrupts */
  1362. xc->cppr = 0;
  1363. out_8(xive_tima + xive_tima_offset + TM_CPPR, 0);
  1364. if (xive_ops->teardown_cpu)
  1365. xive_ops->teardown_cpu(cpu, xc);
  1366. #ifdef CONFIG_SMP
  1367. /* Get rid of IPI */
  1368. xive_cleanup_cpu_ipi(cpu, xc);
  1369. #endif
  1370. /* Disable and free the queues */
  1371. xive_cleanup_cpu_queues(cpu, xc);
  1372. }
  1373. void xive_shutdown(void)
  1374. {
  1375. xive_ops->shutdown();
  1376. }
  1377. bool __init xive_core_init(struct device_node *np, const struct xive_ops *ops,
  1378. void __iomem *area, u32 offset, u8 max_prio)
  1379. {
  1380. xive_tima = area;
  1381. xive_tima_offset = offset;
  1382. xive_ops = ops;
  1383. xive_irq_priority = max_prio;
  1384. ppc_md.get_irq = xive_get_irq;
  1385. __xive_enabled = true;
  1386. pr_debug("Initializing host..\n");
  1387. xive_init_host(np);
  1388. pr_debug("Initializing boot CPU..\n");
  1389. /* Allocate per-CPU data and queues */
  1390. xive_prepare_cpu(smp_processor_id());
  1391. /* Get ready for interrupts */
  1392. xive_setup_cpu();
  1393. pr_info("Interrupt handling initialized with %s backend\n",
  1394. xive_ops->name);
  1395. pr_info("Using priority %d for all interrupts\n", max_prio);
  1396. return true;
  1397. }
  1398. __be32 *xive_queue_page_alloc(unsigned int cpu, u32 queue_shift)
  1399. {
  1400. unsigned int alloc_order;
  1401. struct page *pages;
  1402. __be32 *qpage;
  1403. alloc_order = xive_alloc_order(queue_shift);
  1404. pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
  1405. if (!pages)
  1406. return ERR_PTR(-ENOMEM);
  1407. qpage = (__be32 *)page_address(pages);
  1408. memset(qpage, 0, 1 << queue_shift);
  1409. return qpage;
  1410. }
  1411. static int __init xive_off(char *arg)
  1412. {
  1413. xive_cmdline_disabled = true;
  1414. return 1;
  1415. }
  1416. __setup("xive=off", xive_off);
  1417. static int __init xive_store_eoi_cmdline(char *arg)
  1418. {
  1419. if (!arg)
  1420. return 1;
  1421. if (strncmp(arg, "off", 3) == 0) {
  1422. pr_info("StoreEOI disabled on kernel command line\n");
  1423. xive_store_eoi = false;
  1424. }
  1425. return 1;
  1426. }
  1427. __setup("xive.store-eoi=", xive_store_eoi_cmdline);
  1428. #ifdef CONFIG_DEBUG_FS
  1429. static void xive_debug_show_ipi(struct seq_file *m, int cpu)
  1430. {
  1431. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  1432. seq_printf(m, "CPU %d: ", cpu);
  1433. if (xc) {
  1434. seq_printf(m, "pp=%02x CPPR=%02x ", xc->pending_prio, xc->cppr);
  1435. #ifdef CONFIG_SMP
  1436. {
  1437. char buffer[128];
  1438. xive_irq_data_dump(&xc->ipi_data, buffer, sizeof(buffer));
  1439. seq_printf(m, "IPI=0x%08x %s", xc->hw_ipi, buffer);
  1440. }
  1441. #endif
  1442. }
  1443. seq_puts(m, "\n");
  1444. }
  1445. static void xive_debug_show_irq(struct seq_file *m, struct irq_data *d)
  1446. {
  1447. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1448. int rc;
  1449. u32 target;
  1450. u8 prio;
  1451. u32 lirq;
  1452. char buffer[128];
  1453. rc = xive_ops->get_irq_config(hw_irq, &target, &prio, &lirq);
  1454. if (rc) {
  1455. seq_printf(m, "IRQ 0x%08x : no config rc=%d\n", hw_irq, rc);
  1456. return;
  1457. }
  1458. seq_printf(m, "IRQ 0x%08x : target=0x%x prio=%02x lirq=0x%x ",
  1459. hw_irq, target, prio, lirq);
  1460. xive_irq_data_dump(irq_data_get_irq_handler_data(d), buffer, sizeof(buffer));
  1461. seq_puts(m, buffer);
  1462. seq_puts(m, "\n");
  1463. }
  1464. static int xive_irq_debug_show(struct seq_file *m, void *private)
  1465. {
  1466. unsigned int i;
  1467. struct irq_desc *desc;
  1468. for_each_irq_desc(i, desc) {
  1469. struct irq_data *d = irq_domain_get_irq_data(xive_irq_domain, i);
  1470. if (d)
  1471. xive_debug_show_irq(m, d);
  1472. }
  1473. return 0;
  1474. }
  1475. DEFINE_SHOW_ATTRIBUTE(xive_irq_debug);
  1476. static int xive_ipi_debug_show(struct seq_file *m, void *private)
  1477. {
  1478. int cpu;
  1479. if (xive_ops->debug_show)
  1480. xive_ops->debug_show(m, private);
  1481. for_each_online_cpu(cpu)
  1482. xive_debug_show_ipi(m, cpu);
  1483. return 0;
  1484. }
  1485. DEFINE_SHOW_ATTRIBUTE(xive_ipi_debug);
  1486. static void xive_eq_debug_show_one(struct seq_file *m, struct xive_q *q, u8 prio)
  1487. {
  1488. int i;
  1489. seq_printf(m, "EQ%d idx=%d T=%d\n", prio, q->idx, q->toggle);
  1490. if (q->qpage) {
  1491. for (i = 0; i < q->msk + 1; i++) {
  1492. if (!(i % 8))
  1493. seq_printf(m, "%05d ", i);
  1494. seq_printf(m, "%08x%s", be32_to_cpup(q->qpage + i),
  1495. (i + 1) % 8 ? " " : "\n");
  1496. }
  1497. }
  1498. seq_puts(m, "\n");
  1499. }
  1500. static int xive_eq_debug_show(struct seq_file *m, void *private)
  1501. {
  1502. int cpu = (long)m->private;
  1503. struct xive_cpu *xc = per_cpu(xive_cpu, cpu);
  1504. if (xc)
  1505. xive_eq_debug_show_one(m, &xc->queue[xive_irq_priority],
  1506. xive_irq_priority);
  1507. return 0;
  1508. }
  1509. DEFINE_SHOW_ATTRIBUTE(xive_eq_debug);
  1510. static void xive_core_debugfs_create(void)
  1511. {
  1512. struct dentry *xive_dir;
  1513. struct dentry *xive_eq_dir;
  1514. long cpu;
  1515. char name[16];
  1516. xive_dir = debugfs_create_dir("xive", arch_debugfs_dir);
  1517. if (IS_ERR(xive_dir))
  1518. return;
  1519. debugfs_create_file("ipis", 0400, xive_dir,
  1520. NULL, &xive_ipi_debug_fops);
  1521. debugfs_create_file("interrupts", 0400, xive_dir,
  1522. NULL, &xive_irq_debug_fops);
  1523. xive_eq_dir = debugfs_create_dir("eqs", xive_dir);
  1524. for_each_possible_cpu(cpu) {
  1525. snprintf(name, sizeof(name), "cpu%ld", cpu);
  1526. debugfs_create_file(name, 0400, xive_eq_dir, (void *)cpu,
  1527. &xive_eq_debug_fops);
  1528. }
  1529. debugfs_create_bool("store-eoi", 0600, xive_dir, &xive_store_eoi);
  1530. if (xive_ops->debug_create)
  1531. xive_ops->debug_create(xive_dir);
  1532. }
  1533. #else
  1534. static inline void xive_core_debugfs_create(void) { }
  1535. #endif /* CONFIG_DEBUG_FS */
  1536. int xive_core_debug_init(void)
  1537. {
  1538. if (xive_enabled() && IS_ENABLED(CONFIG_DEBUG_FS))
  1539. xive_core_debugfs_create();
  1540. return 0;
  1541. }