common.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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. DBG_VERBOSE("IPI eoi: irq=%d [0x%lx] (HW IRQ 0x%x) pending=%02x\n",
  866. d->irq, irqd_to_hwirq(d), xc->hw_ipi, xc->pending_prio);
  867. /* Handle possible race with unplug and drop stale IPIs */
  868. if (!xc)
  869. return;
  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);