common.c 36 KB

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