book3s_xive.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation.
  4. */
  5. #define pr_fmt(fmt) "xive-kvm: " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/kvm_host.h>
  8. #include <linux/err.h>
  9. #include <linux/gfp.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/delay.h>
  12. #include <linux/percpu.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/irqdomain.h>
  16. #include <asm/kvm_book3s.h>
  17. #include <asm/kvm_ppc.h>
  18. #include <asm/hvcall.h>
  19. #include <asm/xics.h>
  20. #include <asm/xive.h>
  21. #include <asm/xive-regs.h>
  22. #include <asm/debug.h>
  23. #include <asm/time.h>
  24. #include <asm/opal.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/seq_file.h>
  27. #include "book3s_xive.h"
  28. #define __x_eoi_page(xd) ((void __iomem *)((xd)->eoi_mmio))
  29. #define __x_trig_page(xd) ((void __iomem *)((xd)->trig_mmio))
  30. /* Dummy interrupt used when taking interrupts out of a queue in H_CPPR */
  31. #define XICS_DUMMY 1
  32. static void xive_vm_ack_pending(struct kvmppc_xive_vcpu *xc)
  33. {
  34. u8 cppr;
  35. u16 ack;
  36. /*
  37. * Ensure any previous store to CPPR is ordered vs.
  38. * the subsequent loads from PIPR or ACK.
  39. */
  40. eieio();
  41. /* Perform the acknowledge OS to register cycle. */
  42. ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_OS_REG));
  43. /* Synchronize subsequent queue accesses */
  44. mb();
  45. /* XXX Check grouping level */
  46. /* Anything ? */
  47. if (!((ack >> 8) & TM_QW1_NSR_EO))
  48. return;
  49. /* Grab CPPR of the most favored pending interrupt */
  50. cppr = ack & 0xff;
  51. if (cppr < 8)
  52. xc->pending |= 1 << cppr;
  53. /* Check consistency */
  54. if (cppr >= xc->hw_cppr)
  55. pr_warn("KVM-XIVE: CPU %d odd ack CPPR, got %d at %d\n",
  56. smp_processor_id(), cppr, xc->hw_cppr);
  57. /*
  58. * Update our image of the HW CPPR. We don't yet modify
  59. * xc->cppr, this will be done as we scan for interrupts
  60. * in the queues.
  61. */
  62. xc->hw_cppr = cppr;
  63. }
  64. static u8 xive_vm_esb_load(struct xive_irq_data *xd, u32 offset)
  65. {
  66. u64 val;
  67. if (offset == XIVE_ESB_SET_PQ_10 && xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
  68. offset |= XIVE_ESB_LD_ST_MO;
  69. val = __raw_readq(__x_eoi_page(xd) + offset);
  70. #ifdef __LITTLE_ENDIAN__
  71. val >>= 64-8;
  72. #endif
  73. return (u8)val;
  74. }
  75. static void xive_vm_source_eoi(u32 hw_irq, struct xive_irq_data *xd)
  76. {
  77. /* If the XIVE supports the new "store EOI facility, use it */
  78. if (xd->flags & XIVE_IRQ_FLAG_STORE_EOI)
  79. __raw_writeq(0, __x_eoi_page(xd) + XIVE_ESB_STORE_EOI);
  80. else if (xd->flags & XIVE_IRQ_FLAG_LSI) {
  81. /*
  82. * For LSIs the HW EOI cycle is used rather than PQ bits,
  83. * as they are automatically re-triggred in HW when still
  84. * pending.
  85. */
  86. __raw_readq(__x_eoi_page(xd) + XIVE_ESB_LOAD_EOI);
  87. } else {
  88. uint64_t eoi_val;
  89. /*
  90. * Otherwise for EOI, we use the special MMIO that does
  91. * a clear of both P and Q and returns the old Q,
  92. * except for LSIs where we use the "EOI cycle" special
  93. * load.
  94. *
  95. * This allows us to then do a re-trigger if Q was set
  96. * rather than synthetizing an interrupt in software
  97. */
  98. eoi_val = xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_00);
  99. /* Re-trigger if needed */
  100. if ((eoi_val & 1) && __x_trig_page(xd))
  101. __raw_writeq(0, __x_trig_page(xd));
  102. }
  103. }
  104. enum {
  105. scan_fetch,
  106. scan_poll,
  107. scan_eoi,
  108. };
  109. static u32 xive_vm_scan_interrupts(struct kvmppc_xive_vcpu *xc,
  110. u8 pending, int scan_type)
  111. {
  112. u32 hirq = 0;
  113. u8 prio = 0xff;
  114. /* Find highest pending priority */
  115. while ((xc->mfrr != 0xff || pending != 0) && hirq == 0) {
  116. struct xive_q *q;
  117. u32 idx, toggle;
  118. __be32 *qpage;
  119. /*
  120. * If pending is 0 this will return 0xff which is what
  121. * we want
  122. */
  123. prio = ffs(pending) - 1;
  124. /* Don't scan past the guest cppr */
  125. if (prio >= xc->cppr || prio > 7) {
  126. if (xc->mfrr < xc->cppr) {
  127. prio = xc->mfrr;
  128. hirq = XICS_IPI;
  129. }
  130. break;
  131. }
  132. /* Grab queue and pointers */
  133. q = &xc->queues[prio];
  134. idx = q->idx;
  135. toggle = q->toggle;
  136. /*
  137. * Snapshot the queue page. The test further down for EOI
  138. * must use the same "copy" that was used by __xive_read_eq
  139. * since qpage can be set concurrently and we don't want
  140. * to miss an EOI.
  141. */
  142. qpage = READ_ONCE(q->qpage);
  143. skip_ipi:
  144. /*
  145. * Try to fetch from the queue. Will return 0 for a
  146. * non-queueing priority (ie, qpage = 0).
  147. */
  148. hirq = __xive_read_eq(qpage, q->msk, &idx, &toggle);
  149. /*
  150. * If this was a signal for an MFFR change done by
  151. * H_IPI we skip it. Additionally, if we were fetching
  152. * we EOI it now, thus re-enabling reception of a new
  153. * such signal.
  154. *
  155. * We also need to do that if prio is 0 and we had no
  156. * page for the queue. In this case, we have non-queued
  157. * IPI that needs to be EOId.
  158. *
  159. * This is safe because if we have another pending MFRR
  160. * change that wasn't observed above, the Q bit will have
  161. * been set and another occurrence of the IPI will trigger.
  162. */
  163. if (hirq == XICS_IPI || (prio == 0 && !qpage)) {
  164. if (scan_type == scan_fetch) {
  165. xive_vm_source_eoi(xc->vp_ipi,
  166. &xc->vp_ipi_data);
  167. q->idx = idx;
  168. q->toggle = toggle;
  169. }
  170. /* Loop back on same queue with updated idx/toggle */
  171. WARN_ON(hirq && hirq != XICS_IPI);
  172. if (hirq)
  173. goto skip_ipi;
  174. }
  175. /* If it's the dummy interrupt, continue searching */
  176. if (hirq == XICS_DUMMY)
  177. goto skip_ipi;
  178. /* Clear the pending bit if the queue is now empty */
  179. if (!hirq) {
  180. pending &= ~(1 << prio);
  181. /*
  182. * Check if the queue count needs adjusting due to
  183. * interrupts being moved away.
  184. */
  185. if (atomic_read(&q->pending_count)) {
  186. int p = atomic_xchg(&q->pending_count, 0);
  187. if (p) {
  188. WARN_ON(p > atomic_read(&q->count));
  189. atomic_sub(p, &q->count);
  190. }
  191. }
  192. }
  193. /*
  194. * If the most favoured prio we found pending is less
  195. * favored (or equal) than a pending IPI, we return
  196. * the IPI instead.
  197. */
  198. if (prio >= xc->mfrr && xc->mfrr < xc->cppr) {
  199. prio = xc->mfrr;
  200. hirq = XICS_IPI;
  201. break;
  202. }
  203. /* If fetching, update queue pointers */
  204. if (scan_type == scan_fetch) {
  205. q->idx = idx;
  206. q->toggle = toggle;
  207. }
  208. }
  209. /* If we are just taking a "peek", do nothing else */
  210. if (scan_type == scan_poll)
  211. return hirq;
  212. /* Update the pending bits */
  213. xc->pending = pending;
  214. /*
  215. * If this is an EOI that's it, no CPPR adjustment done here,
  216. * all we needed was cleanup the stale pending bits and check
  217. * if there's anything left.
  218. */
  219. if (scan_type == scan_eoi)
  220. return hirq;
  221. /*
  222. * If we found an interrupt, adjust what the guest CPPR should
  223. * be as if we had just fetched that interrupt from HW.
  224. *
  225. * Note: This can only make xc->cppr smaller as the previous
  226. * loop will only exit with hirq != 0 if prio is lower than
  227. * the current xc->cppr. Thus we don't need to re-check xc->mfrr
  228. * for pending IPIs.
  229. */
  230. if (hirq)
  231. xc->cppr = prio;
  232. /*
  233. * If it was an IPI the HW CPPR might have been lowered too much
  234. * as the HW interrupt we use for IPIs is routed to priority 0.
  235. *
  236. * We re-sync it here.
  237. */
  238. if (xc->cppr != xc->hw_cppr) {
  239. xc->hw_cppr = xc->cppr;
  240. __raw_writeb(xc->cppr, xive_tima + TM_QW1_OS + TM_CPPR);
  241. }
  242. return hirq;
  243. }
  244. static unsigned long xive_vm_h_xirr(struct kvm_vcpu *vcpu)
  245. {
  246. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  247. u8 old_cppr;
  248. u32 hirq;
  249. pr_devel("H_XIRR\n");
  250. xc->stat_vm_h_xirr++;
  251. /* First collect pending bits from HW */
  252. xive_vm_ack_pending(xc);
  253. pr_devel(" new pending=0x%02x hw_cppr=%d cppr=%d\n",
  254. xc->pending, xc->hw_cppr, xc->cppr);
  255. /* Grab previous CPPR and reverse map it */
  256. old_cppr = xive_prio_to_guest(xc->cppr);
  257. /* Scan for actual interrupts */
  258. hirq = xive_vm_scan_interrupts(xc, xc->pending, scan_fetch);
  259. pr_devel(" got hirq=0x%x hw_cppr=%d cppr=%d\n",
  260. hirq, xc->hw_cppr, xc->cppr);
  261. /* That should never hit */
  262. if (hirq & 0xff000000)
  263. pr_warn("XIVE: Weird guest interrupt number 0x%08x\n", hirq);
  264. /*
  265. * XXX We could check if the interrupt is masked here and
  266. * filter it. If we chose to do so, we would need to do:
  267. *
  268. * if (masked) {
  269. * lock();
  270. * if (masked) {
  271. * old_Q = true;
  272. * hirq = 0;
  273. * }
  274. * unlock();
  275. * }
  276. */
  277. /* Return interrupt and old CPPR in GPR4 */
  278. vcpu->arch.regs.gpr[4] = hirq | (old_cppr << 24);
  279. return H_SUCCESS;
  280. }
  281. static unsigned long xive_vm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
  282. {
  283. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  284. u8 pending = xc->pending;
  285. u32 hirq;
  286. pr_devel("H_IPOLL(server=%ld)\n", server);
  287. xc->stat_vm_h_ipoll++;
  288. /* Grab the target VCPU if not the current one */
  289. if (xc->server_num != server) {
  290. vcpu = kvmppc_xive_find_server(vcpu->kvm, server);
  291. if (!vcpu)
  292. return H_PARAMETER;
  293. xc = vcpu->arch.xive_vcpu;
  294. /* Scan all priorities */
  295. pending = 0xff;
  296. } else {
  297. /* Grab pending interrupt if any */
  298. __be64 qw1 = __raw_readq(xive_tima + TM_QW1_OS);
  299. u8 pipr = be64_to_cpu(qw1) & 0xff;
  300. if (pipr < 8)
  301. pending |= 1 << pipr;
  302. }
  303. hirq = xive_vm_scan_interrupts(xc, pending, scan_poll);
  304. /* Return interrupt and old CPPR in GPR4 */
  305. vcpu->arch.regs.gpr[4] = hirq | (xc->cppr << 24);
  306. return H_SUCCESS;
  307. }
  308. static void xive_vm_push_pending_to_hw(struct kvmppc_xive_vcpu *xc)
  309. {
  310. u8 pending, prio;
  311. pending = xc->pending;
  312. if (xc->mfrr != 0xff) {
  313. if (xc->mfrr < 8)
  314. pending |= 1 << xc->mfrr;
  315. else
  316. pending |= 0x80;
  317. }
  318. if (!pending)
  319. return;
  320. prio = ffs(pending) - 1;
  321. __raw_writeb(prio, xive_tima + TM_SPC_SET_OS_PENDING);
  322. }
  323. static void xive_vm_scan_for_rerouted_irqs(struct kvmppc_xive *xive,
  324. struct kvmppc_xive_vcpu *xc)
  325. {
  326. unsigned int prio;
  327. /* For each priority that is now masked */
  328. for (prio = xc->cppr; prio < KVMPPC_XIVE_Q_COUNT; prio++) {
  329. struct xive_q *q = &xc->queues[prio];
  330. struct kvmppc_xive_irq_state *state;
  331. struct kvmppc_xive_src_block *sb;
  332. u32 idx, toggle, entry, irq, hw_num;
  333. struct xive_irq_data *xd;
  334. __be32 *qpage;
  335. u16 src;
  336. idx = q->idx;
  337. toggle = q->toggle;
  338. qpage = READ_ONCE(q->qpage);
  339. if (!qpage)
  340. continue;
  341. /* For each interrupt in the queue */
  342. for (;;) {
  343. entry = be32_to_cpup(qpage + idx);
  344. /* No more ? */
  345. if ((entry >> 31) == toggle)
  346. break;
  347. irq = entry & 0x7fffffff;
  348. /* Skip dummies and IPIs */
  349. if (irq == XICS_DUMMY || irq == XICS_IPI)
  350. goto next;
  351. sb = kvmppc_xive_find_source(xive, irq, &src);
  352. if (!sb)
  353. goto next;
  354. state = &sb->irq_state[src];
  355. /* Has it been rerouted ? */
  356. if (xc->server_num == state->act_server)
  357. goto next;
  358. /*
  359. * Allright, it *has* been re-routed, kill it from
  360. * the queue.
  361. */
  362. qpage[idx] = cpu_to_be32((entry & 0x80000000) | XICS_DUMMY);
  363. /* Find the HW interrupt */
  364. kvmppc_xive_select_irq(state, &hw_num, &xd);
  365. /* If it's not an LSI, set PQ to 11 the EOI will force a resend */
  366. if (!(xd->flags & XIVE_IRQ_FLAG_LSI))
  367. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_11);
  368. /* EOI the source */
  369. xive_vm_source_eoi(hw_num, xd);
  370. next:
  371. idx = (idx + 1) & q->msk;
  372. if (idx == 0)
  373. toggle ^= 1;
  374. }
  375. }
  376. }
  377. static int xive_vm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  378. {
  379. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  380. struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
  381. u8 old_cppr;
  382. pr_devel("H_CPPR(cppr=%ld)\n", cppr);
  383. xc->stat_vm_h_cppr++;
  384. /* Map CPPR */
  385. cppr = xive_prio_from_guest(cppr);
  386. /* Remember old and update SW state */
  387. old_cppr = xc->cppr;
  388. xc->cppr = cppr;
  389. /*
  390. * Order the above update of xc->cppr with the subsequent
  391. * read of xc->mfrr inside push_pending_to_hw()
  392. */
  393. smp_mb();
  394. if (cppr > old_cppr) {
  395. /*
  396. * We are masking less, we need to look for pending things
  397. * to deliver and set VP pending bits accordingly to trigger
  398. * a new interrupt otherwise we might miss MFRR changes for
  399. * which we have optimized out sending an IPI signal.
  400. */
  401. xive_vm_push_pending_to_hw(xc);
  402. } else {
  403. /*
  404. * We are masking more, we need to check the queue for any
  405. * interrupt that has been routed to another CPU, take
  406. * it out (replace it with the dummy) and retrigger it.
  407. *
  408. * This is necessary since those interrupts may otherwise
  409. * never be processed, at least not until this CPU restores
  410. * its CPPR.
  411. *
  412. * This is in theory racy vs. HW adding new interrupts to
  413. * the queue. In practice this works because the interesting
  414. * cases are when the guest has done a set_xive() to move the
  415. * interrupt away, which flushes the xive, followed by the
  416. * target CPU doing a H_CPPR. So any new interrupt coming into
  417. * the queue must still be routed to us and isn't a source
  418. * of concern.
  419. */
  420. xive_vm_scan_for_rerouted_irqs(xive, xc);
  421. }
  422. /* Apply new CPPR */
  423. xc->hw_cppr = cppr;
  424. __raw_writeb(cppr, xive_tima + TM_QW1_OS + TM_CPPR);
  425. return H_SUCCESS;
  426. }
  427. static int xive_vm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  428. {
  429. struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
  430. struct kvmppc_xive_src_block *sb;
  431. struct kvmppc_xive_irq_state *state;
  432. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  433. struct xive_irq_data *xd;
  434. u8 new_cppr = xirr >> 24;
  435. u32 irq = xirr & 0x00ffffff, hw_num;
  436. u16 src;
  437. int rc = 0;
  438. pr_devel("H_EOI(xirr=%08lx)\n", xirr);
  439. xc->stat_vm_h_eoi++;
  440. xc->cppr = xive_prio_from_guest(new_cppr);
  441. /*
  442. * IPIs are synthetized from MFRR and thus don't need
  443. * any special EOI handling. The underlying interrupt
  444. * used to signal MFRR changes is EOId when fetched from
  445. * the queue.
  446. */
  447. if (irq == XICS_IPI || irq == 0) {
  448. /*
  449. * This barrier orders the setting of xc->cppr vs.
  450. * subsquent test of xc->mfrr done inside
  451. * scan_interrupts and push_pending_to_hw
  452. */
  453. smp_mb();
  454. goto bail;
  455. }
  456. /* Find interrupt source */
  457. sb = kvmppc_xive_find_source(xive, irq, &src);
  458. if (!sb) {
  459. pr_devel(" source not found !\n");
  460. rc = H_PARAMETER;
  461. /* Same as above */
  462. smp_mb();
  463. goto bail;
  464. }
  465. state = &sb->irq_state[src];
  466. kvmppc_xive_select_irq(state, &hw_num, &xd);
  467. state->in_eoi = true;
  468. /*
  469. * This barrier orders both setting of in_eoi above vs,
  470. * subsequent test of guest_priority, and the setting
  471. * of xc->cppr vs. subsquent test of xc->mfrr done inside
  472. * scan_interrupts and push_pending_to_hw
  473. */
  474. smp_mb();
  475. again:
  476. if (state->guest_priority == MASKED) {
  477. arch_spin_lock(&sb->lock);
  478. if (state->guest_priority != MASKED) {
  479. arch_spin_unlock(&sb->lock);
  480. goto again;
  481. }
  482. pr_devel(" EOI on saved P...\n");
  483. /* Clear old_p, that will cause unmask to perform an EOI */
  484. state->old_p = false;
  485. arch_spin_unlock(&sb->lock);
  486. } else {
  487. pr_devel(" EOI on source...\n");
  488. /* Perform EOI on the source */
  489. xive_vm_source_eoi(hw_num, xd);
  490. /* If it's an emulated LSI, check level and resend */
  491. if (state->lsi && state->asserted)
  492. __raw_writeq(0, __x_trig_page(xd));
  493. }
  494. /*
  495. * This barrier orders the above guest_priority check
  496. * and spin_lock/unlock with clearing in_eoi below.
  497. *
  498. * It also has to be a full mb() as it must ensure
  499. * the MMIOs done in source_eoi() are completed before
  500. * state->in_eoi is visible.
  501. */
  502. mb();
  503. state->in_eoi = false;
  504. bail:
  505. /* Re-evaluate pending IRQs and update HW */
  506. xive_vm_scan_interrupts(xc, xc->pending, scan_eoi);
  507. xive_vm_push_pending_to_hw(xc);
  508. pr_devel(" after scan pending=%02x\n", xc->pending);
  509. /* Apply new CPPR */
  510. xc->hw_cppr = xc->cppr;
  511. __raw_writeb(xc->cppr, xive_tima + TM_QW1_OS + TM_CPPR);
  512. return rc;
  513. }
  514. static int xive_vm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  515. unsigned long mfrr)
  516. {
  517. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  518. pr_devel("H_IPI(server=%08lx,mfrr=%ld)\n", server, mfrr);
  519. xc->stat_vm_h_ipi++;
  520. /* Find target */
  521. vcpu = kvmppc_xive_find_server(vcpu->kvm, server);
  522. if (!vcpu)
  523. return H_PARAMETER;
  524. xc = vcpu->arch.xive_vcpu;
  525. /* Locklessly write over MFRR */
  526. xc->mfrr = mfrr;
  527. /*
  528. * The load of xc->cppr below and the subsequent MMIO store
  529. * to the IPI must happen after the above mfrr update is
  530. * globally visible so that:
  531. *
  532. * - Synchronize with another CPU doing an H_EOI or a H_CPPR
  533. * updating xc->cppr then reading xc->mfrr.
  534. *
  535. * - The target of the IPI sees the xc->mfrr update
  536. */
  537. mb();
  538. /* Shoot the IPI if most favored than target cppr */
  539. if (mfrr < xc->cppr)
  540. __raw_writeq(0, __x_trig_page(&xc->vp_ipi_data));
  541. return H_SUCCESS;
  542. }
  543. /*
  544. * We leave a gap of a couple of interrupts in the queue to
  545. * account for the IPI and additional safety guard.
  546. */
  547. #define XIVE_Q_GAP 2
  548. static bool kvmppc_xive_vcpu_has_save_restore(struct kvm_vcpu *vcpu)
  549. {
  550. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  551. /* Check enablement at VP level */
  552. return xc->vp_cam & TM_QW1W2_HO;
  553. }
  554. bool kvmppc_xive_check_save_restore(struct kvm_vcpu *vcpu)
  555. {
  556. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  557. struct kvmppc_xive *xive = xc->xive;
  558. if (xive->flags & KVMPPC_XIVE_FLAG_SAVE_RESTORE)
  559. return kvmppc_xive_vcpu_has_save_restore(vcpu);
  560. return true;
  561. }
  562. /*
  563. * Push a vcpu's context to the XIVE on guest entry.
  564. * This assumes we are in virtual mode (MMU on)
  565. */
  566. void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu)
  567. {
  568. void __iomem *tima = local_paca->kvm_hstate.xive_tima_virt;
  569. u64 pq;
  570. /*
  571. * Nothing to do if the platform doesn't have a XIVE
  572. * or this vCPU doesn't have its own XIVE context
  573. * (e.g. because it's not using an in-kernel interrupt controller).
  574. */
  575. if (!tima || !vcpu->arch.xive_cam_word)
  576. return;
  577. eieio();
  578. if (!kvmppc_xive_vcpu_has_save_restore(vcpu))
  579. __raw_writeq(vcpu->arch.xive_saved_state.w01, tima + TM_QW1_OS);
  580. __raw_writel(vcpu->arch.xive_cam_word, tima + TM_QW1_OS + TM_WORD2);
  581. vcpu->arch.xive_pushed = 1;
  582. eieio();
  583. /*
  584. * We clear the irq_pending flag. There is a small chance of a
  585. * race vs. the escalation interrupt happening on another
  586. * processor setting it again, but the only consequence is to
  587. * cause a spurious wakeup on the next H_CEDE, which is not an
  588. * issue.
  589. */
  590. vcpu->arch.irq_pending = 0;
  591. /*
  592. * In single escalation mode, if the escalation interrupt is
  593. * on, we mask it.
  594. */
  595. if (vcpu->arch.xive_esc_on) {
  596. pq = __raw_readq((void __iomem *)(vcpu->arch.xive_esc_vaddr +
  597. XIVE_ESB_SET_PQ_01));
  598. mb();
  599. /*
  600. * We have a possible subtle race here: The escalation
  601. * interrupt might have fired and be on its way to the
  602. * host queue while we mask it, and if we unmask it
  603. * early enough (re-cede right away), there is a
  604. * theoretical possibility that it fires again, thus
  605. * landing in the target queue more than once which is
  606. * a big no-no.
  607. *
  608. * Fortunately, solving this is rather easy. If the
  609. * above load setting PQ to 01 returns a previous
  610. * value where P is set, then we know the escalation
  611. * interrupt is somewhere on its way to the host. In
  612. * that case we simply don't clear the xive_esc_on
  613. * flag below. It will be eventually cleared by the
  614. * handler for the escalation interrupt.
  615. *
  616. * Then, when doing a cede, we check that flag again
  617. * before re-enabling the escalation interrupt, and if
  618. * set, we abort the cede.
  619. */
  620. if (!(pq & XIVE_ESB_VAL_P))
  621. /* Now P is 0, we can clear the flag */
  622. vcpu->arch.xive_esc_on = 0;
  623. }
  624. }
  625. EXPORT_SYMBOL_GPL(kvmppc_xive_push_vcpu);
  626. /*
  627. * Pull a vcpu's context from the XIVE on guest exit.
  628. * This assumes we are in virtual mode (MMU on)
  629. */
  630. void kvmppc_xive_pull_vcpu(struct kvm_vcpu *vcpu)
  631. {
  632. void __iomem *tima = local_paca->kvm_hstate.xive_tima_virt;
  633. if (!vcpu->arch.xive_pushed)
  634. return;
  635. /*
  636. * Should not have been pushed if there is no tima
  637. */
  638. if (WARN_ON(!tima))
  639. return;
  640. eieio();
  641. /* First load to pull the context, we ignore the value */
  642. __raw_readl(tima + TM_SPC_PULL_OS_CTX);
  643. /* Second load to recover the context state (Words 0 and 1) */
  644. if (!kvmppc_xive_vcpu_has_save_restore(vcpu))
  645. vcpu->arch.xive_saved_state.w01 = __raw_readq(tima + TM_QW1_OS);
  646. /* Fixup some of the state for the next load */
  647. vcpu->arch.xive_saved_state.lsmfb = 0;
  648. vcpu->arch.xive_saved_state.ack = 0xff;
  649. vcpu->arch.xive_pushed = 0;
  650. eieio();
  651. }
  652. EXPORT_SYMBOL_GPL(kvmppc_xive_pull_vcpu);
  653. bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu)
  654. {
  655. void __iomem *esc_vaddr = (void __iomem *)vcpu->arch.xive_esc_vaddr;
  656. bool ret = true;
  657. if (!esc_vaddr)
  658. return ret;
  659. /* we are using XIVE with single escalation */
  660. if (vcpu->arch.xive_esc_on) {
  661. /*
  662. * If we still have a pending escalation, abort the cede,
  663. * and we must set PQ to 10 rather than 00 so that we don't
  664. * potentially end up with two entries for the escalation
  665. * interrupt in the XIVE interrupt queue. In that case
  666. * we also don't want to set xive_esc_on to 1 here in
  667. * case we race with xive_esc_irq().
  668. */
  669. ret = false;
  670. /*
  671. * The escalation interrupts are special as we don't EOI them.
  672. * There is no need to use the load-after-store ordering offset
  673. * to set PQ to 10 as we won't use StoreEOI.
  674. */
  675. __raw_readq(esc_vaddr + XIVE_ESB_SET_PQ_10);
  676. } else {
  677. vcpu->arch.xive_esc_on = true;
  678. mb();
  679. __raw_readq(esc_vaddr + XIVE_ESB_SET_PQ_00);
  680. }
  681. mb();
  682. return ret;
  683. }
  684. EXPORT_SYMBOL_GPL(kvmppc_xive_rearm_escalation);
  685. /*
  686. * This is a simple trigger for a generic XIVE IRQ. This must
  687. * only be called for interrupts that support a trigger page
  688. */
  689. static bool xive_irq_trigger(struct xive_irq_data *xd)
  690. {
  691. /* This should be only for MSIs */
  692. if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
  693. return false;
  694. /* Those interrupts should always have a trigger page */
  695. if (WARN_ON(!xd->trig_mmio))
  696. return false;
  697. out_be64(xd->trig_mmio, 0);
  698. return true;
  699. }
  700. static irqreturn_t xive_esc_irq(int irq, void *data)
  701. {
  702. struct kvm_vcpu *vcpu = data;
  703. vcpu->arch.irq_pending = 1;
  704. smp_mb();
  705. if (vcpu->arch.ceded || vcpu->arch.nested)
  706. kvmppc_fast_vcpu_kick(vcpu);
  707. /* Since we have the no-EOI flag, the interrupt is effectively
  708. * disabled now. Clearing xive_esc_on means we won't bother
  709. * doing so on the next entry.
  710. *
  711. * This also allows the entry code to know that if a PQ combination
  712. * of 10 is observed while xive_esc_on is true, it means the queue
  713. * contains an unprocessed escalation interrupt. We don't make use of
  714. * that knowledge today but might (see comment in book3s_hv_rmhandler.S)
  715. */
  716. vcpu->arch.xive_esc_on = false;
  717. /* This orders xive_esc_on = false vs. subsequent stale_p = true */
  718. smp_wmb(); /* goes with smp_mb() in cleanup_single_escalation */
  719. return IRQ_HANDLED;
  720. }
  721. int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio,
  722. bool single_escalation)
  723. {
  724. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  725. struct xive_q *q = &xc->queues[prio];
  726. char *name = NULL;
  727. int rc;
  728. /* Already there ? */
  729. if (xc->esc_virq[prio])
  730. return 0;
  731. /* Hook up the escalation interrupt */
  732. xc->esc_virq[prio] = irq_create_mapping(NULL, q->esc_irq);
  733. if (!xc->esc_virq[prio]) {
  734. pr_err("Failed to map escalation interrupt for queue %d of VCPU %d\n",
  735. prio, xc->server_num);
  736. return -EIO;
  737. }
  738. if (single_escalation)
  739. name = kasprintf(GFP_KERNEL, "kvm-%d-%d",
  740. vcpu->kvm->arch.lpid, xc->server_num);
  741. else
  742. name = kasprintf(GFP_KERNEL, "kvm-%d-%d-%d",
  743. vcpu->kvm->arch.lpid, xc->server_num, prio);
  744. if (!name) {
  745. pr_err("Failed to allocate escalation irq name for queue %d of VCPU %d\n",
  746. prio, xc->server_num);
  747. rc = -ENOMEM;
  748. goto error;
  749. }
  750. pr_devel("Escalation %s irq %d (prio %d)\n", name, xc->esc_virq[prio], prio);
  751. rc = request_irq(xc->esc_virq[prio], xive_esc_irq,
  752. IRQF_NO_THREAD, name, vcpu);
  753. if (rc) {
  754. pr_err("Failed to request escalation interrupt for queue %d of VCPU %d\n",
  755. prio, xc->server_num);
  756. goto error;
  757. }
  758. xc->esc_virq_names[prio] = name;
  759. /* In single escalation mode, we grab the ESB MMIO of the
  760. * interrupt and mask it. Also populate the VCPU v/raddr
  761. * of the ESB page for use by asm entry/exit code. Finally
  762. * set the XIVE_IRQ_FLAG_NO_EOI flag which will prevent the
  763. * core code from performing an EOI on the escalation
  764. * interrupt, thus leaving it effectively masked after
  765. * it fires once.
  766. */
  767. if (single_escalation) {
  768. struct irq_data *d = irq_get_irq_data(xc->esc_virq[prio]);
  769. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  770. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
  771. vcpu->arch.xive_esc_raddr = xd->eoi_page;
  772. vcpu->arch.xive_esc_vaddr = (__force u64)xd->eoi_mmio;
  773. xd->flags |= XIVE_IRQ_FLAG_NO_EOI;
  774. }
  775. return 0;
  776. error:
  777. irq_dispose_mapping(xc->esc_virq[prio]);
  778. xc->esc_virq[prio] = 0;
  779. kfree(name);
  780. return rc;
  781. }
  782. static int xive_provision_queue(struct kvm_vcpu *vcpu, u8 prio)
  783. {
  784. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  785. struct kvmppc_xive *xive = xc->xive;
  786. struct xive_q *q = &xc->queues[prio];
  787. void *qpage;
  788. int rc;
  789. if (WARN_ON(q->qpage))
  790. return 0;
  791. /* Allocate the queue and retrieve infos on current node for now */
  792. qpage = (__be32 *)__get_free_pages(GFP_KERNEL, xive->q_page_order);
  793. if (!qpage) {
  794. pr_err("Failed to allocate queue %d for VCPU %d\n",
  795. prio, xc->server_num);
  796. return -ENOMEM;
  797. }
  798. memset(qpage, 0, 1 << xive->q_order);
  799. /*
  800. * Reconfigure the queue. This will set q->qpage only once the
  801. * queue is fully configured. This is a requirement for prio 0
  802. * as we will stop doing EOIs for every IPI as soon as we observe
  803. * qpage being non-NULL, and instead will only EOI when we receive
  804. * corresponding queue 0 entries
  805. */
  806. rc = xive_native_configure_queue(xc->vp_id, q, prio, qpage,
  807. xive->q_order, true);
  808. if (rc)
  809. pr_err("Failed to configure queue %d for VCPU %d\n",
  810. prio, xc->server_num);
  811. return rc;
  812. }
  813. /* Called with xive->lock held */
  814. static int xive_check_provisioning(struct kvm *kvm, u8 prio)
  815. {
  816. struct kvmppc_xive *xive = kvm->arch.xive;
  817. struct kvm_vcpu *vcpu;
  818. unsigned long i;
  819. int rc;
  820. lockdep_assert_held(&xive->lock);
  821. /* Already provisioned ? */
  822. if (xive->qmap & (1 << prio))
  823. return 0;
  824. pr_devel("Provisioning prio... %d\n", prio);
  825. /* Provision each VCPU and enable escalations if needed */
  826. kvm_for_each_vcpu(i, vcpu, kvm) {
  827. if (!vcpu->arch.xive_vcpu)
  828. continue;
  829. rc = xive_provision_queue(vcpu, prio);
  830. if (rc == 0 && !kvmppc_xive_has_single_escalation(xive))
  831. kvmppc_xive_attach_escalation(vcpu, prio,
  832. kvmppc_xive_has_single_escalation(xive));
  833. if (rc)
  834. return rc;
  835. }
  836. /* Order previous stores and mark it as provisioned */
  837. mb();
  838. xive->qmap |= (1 << prio);
  839. return 0;
  840. }
  841. static void xive_inc_q_pending(struct kvm *kvm, u32 server, u8 prio)
  842. {
  843. struct kvm_vcpu *vcpu;
  844. struct kvmppc_xive_vcpu *xc;
  845. struct xive_q *q;
  846. /* Locate target server */
  847. vcpu = kvmppc_xive_find_server(kvm, server);
  848. if (!vcpu) {
  849. pr_warn("%s: Can't find server %d\n", __func__, server);
  850. return;
  851. }
  852. xc = vcpu->arch.xive_vcpu;
  853. if (WARN_ON(!xc))
  854. return;
  855. q = &xc->queues[prio];
  856. atomic_inc(&q->pending_count);
  857. }
  858. static int xive_try_pick_queue(struct kvm_vcpu *vcpu, u8 prio)
  859. {
  860. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  861. struct xive_q *q;
  862. u32 max;
  863. if (WARN_ON(!xc))
  864. return -ENXIO;
  865. if (!xc->valid)
  866. return -ENXIO;
  867. q = &xc->queues[prio];
  868. if (WARN_ON(!q->qpage))
  869. return -ENXIO;
  870. /* Calculate max number of interrupts in that queue. */
  871. max = (q->msk + 1) - XIVE_Q_GAP;
  872. return atomic_add_unless(&q->count, 1, max) ? 0 : -EBUSY;
  873. }
  874. int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio)
  875. {
  876. struct kvm_vcpu *vcpu;
  877. unsigned long i;
  878. int rc;
  879. /* Locate target server */
  880. vcpu = kvmppc_xive_find_server(kvm, *server);
  881. if (!vcpu) {
  882. pr_devel("Can't find server %d\n", *server);
  883. return -EINVAL;
  884. }
  885. pr_devel("Finding irq target on 0x%x/%d...\n", *server, prio);
  886. /* Try pick it */
  887. rc = xive_try_pick_queue(vcpu, prio);
  888. if (rc == 0)
  889. return rc;
  890. pr_devel(" .. failed, looking up candidate...\n");
  891. /* Failed, pick another VCPU */
  892. kvm_for_each_vcpu(i, vcpu, kvm) {
  893. if (!vcpu->arch.xive_vcpu)
  894. continue;
  895. rc = xive_try_pick_queue(vcpu, prio);
  896. if (rc == 0) {
  897. *server = vcpu->arch.xive_vcpu->server_num;
  898. pr_devel(" found on 0x%x/%d\n", *server, prio);
  899. return rc;
  900. }
  901. }
  902. pr_devel(" no available target !\n");
  903. /* No available target ! */
  904. return -EBUSY;
  905. }
  906. static u8 xive_lock_and_mask(struct kvmppc_xive *xive,
  907. struct kvmppc_xive_src_block *sb,
  908. struct kvmppc_xive_irq_state *state)
  909. {
  910. struct xive_irq_data *xd;
  911. u32 hw_num;
  912. u8 old_prio;
  913. u64 val;
  914. /*
  915. * Take the lock, set masked, try again if racing
  916. * with H_EOI
  917. */
  918. for (;;) {
  919. arch_spin_lock(&sb->lock);
  920. old_prio = state->guest_priority;
  921. state->guest_priority = MASKED;
  922. mb();
  923. if (!state->in_eoi)
  924. break;
  925. state->guest_priority = old_prio;
  926. arch_spin_unlock(&sb->lock);
  927. }
  928. /* No change ? Bail */
  929. if (old_prio == MASKED)
  930. return old_prio;
  931. /* Get the right irq */
  932. kvmppc_xive_select_irq(state, &hw_num, &xd);
  933. /* Set PQ to 10, return old P and old Q and remember them */
  934. val = xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_10);
  935. state->old_p = !!(val & 2);
  936. state->old_q = !!(val & 1);
  937. /*
  938. * Synchronize hardware to sensure the queues are updated when
  939. * masking
  940. */
  941. xive_native_sync_source(hw_num);
  942. return old_prio;
  943. }
  944. static void xive_lock_for_unmask(struct kvmppc_xive_src_block *sb,
  945. struct kvmppc_xive_irq_state *state)
  946. {
  947. /*
  948. * Take the lock try again if racing with H_EOI
  949. */
  950. for (;;) {
  951. arch_spin_lock(&sb->lock);
  952. if (!state->in_eoi)
  953. break;
  954. arch_spin_unlock(&sb->lock);
  955. }
  956. }
  957. static void xive_finish_unmask(struct kvmppc_xive *xive,
  958. struct kvmppc_xive_src_block *sb,
  959. struct kvmppc_xive_irq_state *state,
  960. u8 prio)
  961. {
  962. struct xive_irq_data *xd;
  963. u32 hw_num;
  964. /* If we aren't changing a thing, move on */
  965. if (state->guest_priority != MASKED)
  966. goto bail;
  967. /* Get the right irq */
  968. kvmppc_xive_select_irq(state, &hw_num, &xd);
  969. /* Old Q set, set PQ to 11 */
  970. if (state->old_q)
  971. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_11);
  972. /*
  973. * If not old P, then perform an "effective" EOI,
  974. * on the source. This will handle the cases where
  975. * FW EOI is needed.
  976. */
  977. if (!state->old_p)
  978. xive_vm_source_eoi(hw_num, xd);
  979. /* Synchronize ordering and mark unmasked */
  980. mb();
  981. bail:
  982. state->guest_priority = prio;
  983. }
  984. /*
  985. * Target an interrupt to a given server/prio, this will fallback
  986. * to another server if necessary and perform the HW targetting
  987. * updates as needed
  988. *
  989. * NOTE: Must be called with the state lock held
  990. */
  991. static int xive_target_interrupt(struct kvm *kvm,
  992. struct kvmppc_xive_irq_state *state,
  993. u32 server, u8 prio)
  994. {
  995. struct kvmppc_xive *xive = kvm->arch.xive;
  996. u32 hw_num;
  997. int rc;
  998. /*
  999. * This will return a tentative server and actual
  1000. * priority. The count for that new target will have
  1001. * already been incremented.
  1002. */
  1003. rc = kvmppc_xive_select_target(kvm, &server, prio);
  1004. /*
  1005. * We failed to find a target ? Not much we can do
  1006. * at least until we support the GIQ.
  1007. */
  1008. if (rc)
  1009. return rc;
  1010. /*
  1011. * Increment the old queue pending count if there
  1012. * was one so that the old queue count gets adjusted later
  1013. * when observed to be empty.
  1014. */
  1015. if (state->act_priority != MASKED)
  1016. xive_inc_q_pending(kvm,
  1017. state->act_server,
  1018. state->act_priority);
  1019. /*
  1020. * Update state and HW
  1021. */
  1022. state->act_priority = prio;
  1023. state->act_server = server;
  1024. /* Get the right irq */
  1025. kvmppc_xive_select_irq(state, &hw_num, NULL);
  1026. return xive_native_configure_irq(hw_num,
  1027. kvmppc_xive_vp(xive, server),
  1028. prio, state->number);
  1029. }
  1030. /*
  1031. * Targetting rules: In order to avoid losing track of
  1032. * pending interrupts across mask and unmask, which would
  1033. * allow queue overflows, we implement the following rules:
  1034. *
  1035. * - Unless it was never enabled (or we run out of capacity)
  1036. * an interrupt is always targetted at a valid server/queue
  1037. * pair even when "masked" by the guest. This pair tends to
  1038. * be the last one used but it can be changed under some
  1039. * circumstances. That allows us to separate targetting
  1040. * from masking, we only handle accounting during (re)targetting,
  1041. * this also allows us to let an interrupt drain into its target
  1042. * queue after masking, avoiding complex schemes to remove
  1043. * interrupts out of remote processor queues.
  1044. *
  1045. * - When masking, we set PQ to 10 and save the previous value
  1046. * of P and Q.
  1047. *
  1048. * - When unmasking, if saved Q was set, we set PQ to 11
  1049. * otherwise we leave PQ to the HW state which will be either
  1050. * 10 if nothing happened or 11 if the interrupt fired while
  1051. * masked. Effectively we are OR'ing the previous Q into the
  1052. * HW Q.
  1053. *
  1054. * Then if saved P is clear, we do an effective EOI (Q->P->Trigger)
  1055. * which will unmask the interrupt and shoot a new one if Q was
  1056. * set.
  1057. *
  1058. * Otherwise (saved P is set) we leave PQ unchanged (so 10 or 11,
  1059. * effectively meaning an H_EOI from the guest is still expected
  1060. * for that interrupt).
  1061. *
  1062. * - If H_EOI occurs while masked, we clear the saved P.
  1063. *
  1064. * - When changing target, we account on the new target and
  1065. * increment a separate "pending" counter on the old one.
  1066. * This pending counter will be used to decrement the old
  1067. * target's count when its queue has been observed empty.
  1068. */
  1069. int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
  1070. u32 priority)
  1071. {
  1072. struct kvmppc_xive *xive = kvm->arch.xive;
  1073. struct kvmppc_xive_src_block *sb;
  1074. struct kvmppc_xive_irq_state *state;
  1075. u8 new_act_prio;
  1076. int rc = 0;
  1077. u16 idx;
  1078. if (!xive)
  1079. return -ENODEV;
  1080. pr_devel("set_xive ! irq 0x%x server 0x%x prio %d\n",
  1081. irq, server, priority);
  1082. /* First, check provisioning of queues */
  1083. if (priority != MASKED) {
  1084. mutex_lock(&xive->lock);
  1085. rc = xive_check_provisioning(xive->kvm,
  1086. xive_prio_from_guest(priority));
  1087. mutex_unlock(&xive->lock);
  1088. }
  1089. if (rc) {
  1090. pr_devel(" provisioning failure %d !\n", rc);
  1091. return rc;
  1092. }
  1093. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1094. if (!sb)
  1095. return -EINVAL;
  1096. state = &sb->irq_state[idx];
  1097. /*
  1098. * We first handle masking/unmasking since the locking
  1099. * might need to be retried due to EOIs, we'll handle
  1100. * targetting changes later. These functions will return
  1101. * with the SB lock held.
  1102. *
  1103. * xive_lock_and_mask() will also set state->guest_priority
  1104. * but won't otherwise change other fields of the state.
  1105. *
  1106. * xive_lock_for_unmask will not actually unmask, this will
  1107. * be done later by xive_finish_unmask() once the targetting
  1108. * has been done, so we don't try to unmask an interrupt
  1109. * that hasn't yet been targetted.
  1110. */
  1111. if (priority == MASKED)
  1112. xive_lock_and_mask(xive, sb, state);
  1113. else
  1114. xive_lock_for_unmask(sb, state);
  1115. /*
  1116. * Then we handle targetting.
  1117. *
  1118. * First calculate a new "actual priority"
  1119. */
  1120. new_act_prio = state->act_priority;
  1121. if (priority != MASKED)
  1122. new_act_prio = xive_prio_from_guest(priority);
  1123. pr_devel(" new_act_prio=%x act_server=%x act_prio=%x\n",
  1124. new_act_prio, state->act_server, state->act_priority);
  1125. /*
  1126. * Then check if we actually need to change anything,
  1127. *
  1128. * The condition for re-targetting the interrupt is that
  1129. * we have a valid new priority (new_act_prio is not 0xff)
  1130. * and either the server or the priority changed.
  1131. *
  1132. * Note: If act_priority was ff and the new priority is
  1133. * also ff, we don't do anything and leave the interrupt
  1134. * untargetted. An attempt of doing an int_on on an
  1135. * untargetted interrupt will fail. If that is a problem
  1136. * we could initialize interrupts with valid default
  1137. */
  1138. if (new_act_prio != MASKED &&
  1139. (state->act_server != server ||
  1140. state->act_priority != new_act_prio))
  1141. rc = xive_target_interrupt(kvm, state, server, new_act_prio);
  1142. /*
  1143. * Perform the final unmasking of the interrupt source
  1144. * if necessary
  1145. */
  1146. if (priority != MASKED)
  1147. xive_finish_unmask(xive, sb, state, priority);
  1148. /*
  1149. * Finally Update saved_priority to match. Only int_on/off
  1150. * set this field to a different value.
  1151. */
  1152. state->saved_priority = priority;
  1153. arch_spin_unlock(&sb->lock);
  1154. return rc;
  1155. }
  1156. int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
  1157. u32 *priority)
  1158. {
  1159. struct kvmppc_xive *xive = kvm->arch.xive;
  1160. struct kvmppc_xive_src_block *sb;
  1161. struct kvmppc_xive_irq_state *state;
  1162. u16 idx;
  1163. if (!xive)
  1164. return -ENODEV;
  1165. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1166. if (!sb)
  1167. return -EINVAL;
  1168. state = &sb->irq_state[idx];
  1169. arch_spin_lock(&sb->lock);
  1170. *server = state->act_server;
  1171. *priority = state->guest_priority;
  1172. arch_spin_unlock(&sb->lock);
  1173. return 0;
  1174. }
  1175. int kvmppc_xive_int_on(struct kvm *kvm, u32 irq)
  1176. {
  1177. struct kvmppc_xive *xive = kvm->arch.xive;
  1178. struct kvmppc_xive_src_block *sb;
  1179. struct kvmppc_xive_irq_state *state;
  1180. u16 idx;
  1181. if (!xive)
  1182. return -ENODEV;
  1183. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1184. if (!sb)
  1185. return -EINVAL;
  1186. state = &sb->irq_state[idx];
  1187. pr_devel("int_on(irq=0x%x)\n", irq);
  1188. /*
  1189. * Check if interrupt was not targetted
  1190. */
  1191. if (state->act_priority == MASKED) {
  1192. pr_devel("int_on on untargetted interrupt\n");
  1193. return -EINVAL;
  1194. }
  1195. /* If saved_priority is 0xff, do nothing */
  1196. if (state->saved_priority == MASKED)
  1197. return 0;
  1198. /*
  1199. * Lock and unmask it.
  1200. */
  1201. xive_lock_for_unmask(sb, state);
  1202. xive_finish_unmask(xive, sb, state, state->saved_priority);
  1203. arch_spin_unlock(&sb->lock);
  1204. return 0;
  1205. }
  1206. int kvmppc_xive_int_off(struct kvm *kvm, u32 irq)
  1207. {
  1208. struct kvmppc_xive *xive = kvm->arch.xive;
  1209. struct kvmppc_xive_src_block *sb;
  1210. struct kvmppc_xive_irq_state *state;
  1211. u16 idx;
  1212. if (!xive)
  1213. return -ENODEV;
  1214. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1215. if (!sb)
  1216. return -EINVAL;
  1217. state = &sb->irq_state[idx];
  1218. pr_devel("int_off(irq=0x%x)\n", irq);
  1219. /*
  1220. * Lock and mask
  1221. */
  1222. state->saved_priority = xive_lock_and_mask(xive, sb, state);
  1223. arch_spin_unlock(&sb->lock);
  1224. return 0;
  1225. }
  1226. static bool xive_restore_pending_irq(struct kvmppc_xive *xive, u32 irq)
  1227. {
  1228. struct kvmppc_xive_src_block *sb;
  1229. struct kvmppc_xive_irq_state *state;
  1230. u16 idx;
  1231. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1232. if (!sb)
  1233. return false;
  1234. state = &sb->irq_state[idx];
  1235. if (!state->valid)
  1236. return false;
  1237. /*
  1238. * Trigger the IPI. This assumes we never restore a pass-through
  1239. * interrupt which should be safe enough
  1240. */
  1241. xive_irq_trigger(&state->ipi_data);
  1242. return true;
  1243. }
  1244. u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu)
  1245. {
  1246. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1247. if (!xc)
  1248. return 0;
  1249. /* Return the per-cpu state for state saving/migration */
  1250. return (u64)xc->cppr << KVM_REG_PPC_ICP_CPPR_SHIFT |
  1251. (u64)xc->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT |
  1252. (u64)0xff << KVM_REG_PPC_ICP_PPRI_SHIFT;
  1253. }
  1254. int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
  1255. {
  1256. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1257. struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
  1258. u8 cppr, mfrr;
  1259. u32 xisr;
  1260. if (!xc || !xive)
  1261. return -ENOENT;
  1262. /* Grab individual state fields. We don't use pending_pri */
  1263. cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
  1264. xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
  1265. KVM_REG_PPC_ICP_XISR_MASK;
  1266. mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
  1267. pr_devel("set_icp vcpu %d cppr=0x%x mfrr=0x%x xisr=0x%x\n",
  1268. xc->server_num, cppr, mfrr, xisr);
  1269. /*
  1270. * We can't update the state of a "pushed" VCPU, but that
  1271. * shouldn't happen because the vcpu->mutex makes running a
  1272. * vcpu mutually exclusive with doing one_reg get/set on it.
  1273. */
  1274. if (WARN_ON(vcpu->arch.xive_pushed))
  1275. return -EIO;
  1276. /* Update VCPU HW saved state */
  1277. vcpu->arch.xive_saved_state.cppr = cppr;
  1278. xc->hw_cppr = xc->cppr = cppr;
  1279. /*
  1280. * Update MFRR state. If it's not 0xff, we mark the VCPU as
  1281. * having a pending MFRR change, which will re-evaluate the
  1282. * target. The VCPU will thus potentially get a spurious
  1283. * interrupt but that's not a big deal.
  1284. */
  1285. xc->mfrr = mfrr;
  1286. if (mfrr < cppr)
  1287. xive_irq_trigger(&xc->vp_ipi_data);
  1288. /*
  1289. * Now saved XIRR is "interesting". It means there's something in
  1290. * the legacy "1 element" queue... for an IPI we simply ignore it,
  1291. * as the MFRR restore will handle that. For anything else we need
  1292. * to force a resend of the source.
  1293. * However the source may not have been setup yet. If that's the
  1294. * case, we keep that info and increment a counter in the xive to
  1295. * tell subsequent xive_set_source() to go look.
  1296. */
  1297. if (xisr > XICS_IPI && !xive_restore_pending_irq(xive, xisr)) {
  1298. xc->delayed_irq = xisr;
  1299. xive->delayed_irqs++;
  1300. pr_devel(" xisr restore delayed\n");
  1301. }
  1302. return 0;
  1303. }
  1304. int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
  1305. unsigned long host_irq)
  1306. {
  1307. struct kvmppc_xive *xive = kvm->arch.xive;
  1308. struct kvmppc_xive_src_block *sb;
  1309. struct kvmppc_xive_irq_state *state;
  1310. struct irq_data *host_data =
  1311. irq_domain_get_irq_data(irq_get_default_host(), host_irq);
  1312. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(host_data);
  1313. u16 idx;
  1314. u8 prio;
  1315. int rc;
  1316. if (!xive)
  1317. return -ENODEV;
  1318. pr_debug("%s: GIRQ 0x%lx host IRQ %ld XIVE HW IRQ 0x%x\n",
  1319. __func__, guest_irq, host_irq, hw_irq);
  1320. sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
  1321. if (!sb)
  1322. return -EINVAL;
  1323. state = &sb->irq_state[idx];
  1324. /*
  1325. * Mark the passed-through interrupt as going to a VCPU,
  1326. * this will prevent further EOIs and similar operations
  1327. * from the XIVE code. It will also mask the interrupt
  1328. * to either PQ=10 or 11 state, the latter if the interrupt
  1329. * is pending. This will allow us to unmask or retrigger it
  1330. * after routing it to the guest with a simple EOI.
  1331. *
  1332. * The "state" argument is a "token", all it needs is to be
  1333. * non-NULL to switch to passed-through or NULL for the
  1334. * other way around. We may not yet have an actual VCPU
  1335. * target here and we don't really care.
  1336. */
  1337. rc = irq_set_vcpu_affinity(host_irq, state);
  1338. if (rc) {
  1339. pr_err("Failed to set VCPU affinity for host IRQ %ld\n", host_irq);
  1340. return rc;
  1341. }
  1342. /*
  1343. * Mask and read state of IPI. We need to know if its P bit
  1344. * is set as that means it's potentially already using a
  1345. * queue entry in the target
  1346. */
  1347. prio = xive_lock_and_mask(xive, sb, state);
  1348. pr_devel(" old IPI prio %02x P:%d Q:%d\n", prio,
  1349. state->old_p, state->old_q);
  1350. /* Turn the IPI hard off */
  1351. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
  1352. /*
  1353. * Reset ESB guest mapping. Needed when ESB pages are exposed
  1354. * to the guest in XIVE native mode
  1355. */
  1356. if (xive->ops && xive->ops->reset_mapped)
  1357. xive->ops->reset_mapped(kvm, guest_irq);
  1358. /* Grab info about irq */
  1359. state->pt_number = hw_irq;
  1360. state->pt_data = irq_data_get_irq_handler_data(host_data);
  1361. /*
  1362. * Configure the IRQ to match the existing configuration of
  1363. * the IPI if it was already targetted. Otherwise this will
  1364. * mask the interrupt in a lossy way (act_priority is 0xff)
  1365. * which is fine for a never started interrupt.
  1366. */
  1367. xive_native_configure_irq(hw_irq,
  1368. kvmppc_xive_vp(xive, state->act_server),
  1369. state->act_priority, state->number);
  1370. /*
  1371. * We do an EOI to enable the interrupt (and retrigger if needed)
  1372. * if the guest has the interrupt unmasked and the P bit was *not*
  1373. * set in the IPI. If it was set, we know a slot may still be in
  1374. * use in the target queue thus we have to wait for a guest
  1375. * originated EOI
  1376. */
  1377. if (prio != MASKED && !state->old_p)
  1378. xive_vm_source_eoi(hw_irq, state->pt_data);
  1379. /* Clear old_p/old_q as they are no longer relevant */
  1380. state->old_p = state->old_q = false;
  1381. /* Restore guest prio (unlocks EOI) */
  1382. mb();
  1383. state->guest_priority = prio;
  1384. arch_spin_unlock(&sb->lock);
  1385. return 0;
  1386. }
  1387. EXPORT_SYMBOL_GPL(kvmppc_xive_set_mapped);
  1388. int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
  1389. unsigned long host_irq)
  1390. {
  1391. struct kvmppc_xive *xive = kvm->arch.xive;
  1392. struct kvmppc_xive_src_block *sb;
  1393. struct kvmppc_xive_irq_state *state;
  1394. u16 idx;
  1395. u8 prio;
  1396. int rc;
  1397. if (!xive)
  1398. return -ENODEV;
  1399. pr_debug("%s: GIRQ 0x%lx host IRQ %ld\n", __func__, guest_irq, host_irq);
  1400. sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
  1401. if (!sb)
  1402. return -EINVAL;
  1403. state = &sb->irq_state[idx];
  1404. /*
  1405. * Mask and read state of IRQ. We need to know if its P bit
  1406. * is set as that means it's potentially already using a
  1407. * queue entry in the target
  1408. */
  1409. prio = xive_lock_and_mask(xive, sb, state);
  1410. pr_devel(" old IRQ prio %02x P:%d Q:%d\n", prio,
  1411. state->old_p, state->old_q);
  1412. /*
  1413. * If old_p is set, the interrupt is pending, we switch it to
  1414. * PQ=11. This will force a resend in the host so the interrupt
  1415. * isn't lost to whatever host driver may pick it up
  1416. */
  1417. if (state->old_p)
  1418. xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_11);
  1419. /* Release the passed-through interrupt to the host */
  1420. rc = irq_set_vcpu_affinity(host_irq, NULL);
  1421. if (rc) {
  1422. pr_err("Failed to clr VCPU affinity for host IRQ %ld\n", host_irq);
  1423. return rc;
  1424. }
  1425. /* Forget about the IRQ */
  1426. state->pt_number = 0;
  1427. state->pt_data = NULL;
  1428. /*
  1429. * Reset ESB guest mapping. Needed when ESB pages are exposed
  1430. * to the guest in XIVE native mode
  1431. */
  1432. if (xive->ops && xive->ops->reset_mapped) {
  1433. xive->ops->reset_mapped(kvm, guest_irq);
  1434. }
  1435. /* Reconfigure the IPI */
  1436. xive_native_configure_irq(state->ipi_number,
  1437. kvmppc_xive_vp(xive, state->act_server),
  1438. state->act_priority, state->number);
  1439. /*
  1440. * If old_p is set (we have a queue entry potentially
  1441. * occupied) or the interrupt is masked, we set the IPI
  1442. * to PQ=10 state. Otherwise we just re-enable it (PQ=00).
  1443. */
  1444. if (prio == MASKED || state->old_p)
  1445. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_10);
  1446. else
  1447. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_00);
  1448. /* Restore guest prio (unlocks EOI) */
  1449. mb();
  1450. state->guest_priority = prio;
  1451. arch_spin_unlock(&sb->lock);
  1452. return 0;
  1453. }
  1454. EXPORT_SYMBOL_GPL(kvmppc_xive_clr_mapped);
  1455. void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)
  1456. {
  1457. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1458. struct kvm *kvm = vcpu->kvm;
  1459. struct kvmppc_xive *xive = kvm->arch.xive;
  1460. int i, j;
  1461. for (i = 0; i <= xive->max_sbid; i++) {
  1462. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1463. if (!sb)
  1464. continue;
  1465. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
  1466. struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
  1467. if (!state->valid)
  1468. continue;
  1469. if (state->act_priority == MASKED)
  1470. continue;
  1471. if (state->act_server != xc->server_num)
  1472. continue;
  1473. /* Clean it up */
  1474. arch_spin_lock(&sb->lock);
  1475. state->act_priority = MASKED;
  1476. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
  1477. xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
  1478. if (state->pt_number) {
  1479. xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
  1480. xive_native_configure_irq(state->pt_number, 0, MASKED, 0);
  1481. }
  1482. arch_spin_unlock(&sb->lock);
  1483. }
  1484. }
  1485. /* Disable vcpu's escalation interrupt */
  1486. if (vcpu->arch.xive_esc_on) {
  1487. __raw_readq((void __iomem *)(vcpu->arch.xive_esc_vaddr +
  1488. XIVE_ESB_SET_PQ_01));
  1489. vcpu->arch.xive_esc_on = false;
  1490. }
  1491. /*
  1492. * Clear pointers to escalation interrupt ESB.
  1493. * This is safe because the vcpu->mutex is held, preventing
  1494. * any other CPU from concurrently executing a KVM_RUN ioctl.
  1495. */
  1496. vcpu->arch.xive_esc_vaddr = 0;
  1497. vcpu->arch.xive_esc_raddr = 0;
  1498. }
  1499. /*
  1500. * In single escalation mode, the escalation interrupt is marked so
  1501. * that EOI doesn't re-enable it, but just sets the stale_p flag to
  1502. * indicate that the P bit has already been dealt with. However, the
  1503. * assembly code that enters the guest sets PQ to 00 without clearing
  1504. * stale_p (because it has no easy way to address it). Hence we have
  1505. * to adjust stale_p before shutting down the interrupt.
  1506. */
  1507. void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
  1508. struct kvmppc_xive_vcpu *xc, int irq)
  1509. {
  1510. struct irq_data *d = irq_get_irq_data(irq);
  1511. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  1512. /*
  1513. * This slightly odd sequence gives the right result
  1514. * (i.e. stale_p set if xive_esc_on is false) even if
  1515. * we race with xive_esc_irq() and xive_irq_eoi().
  1516. */
  1517. xd->stale_p = false;
  1518. smp_mb(); /* paired with smb_wmb in xive_esc_irq */
  1519. if (!vcpu->arch.xive_esc_on)
  1520. xd->stale_p = true;
  1521. }
  1522. void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
  1523. {
  1524. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1525. struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
  1526. int i;
  1527. if (!kvmppc_xics_enabled(vcpu))
  1528. return;
  1529. if (!xc)
  1530. return;
  1531. pr_devel("cleanup_vcpu(cpu=%d)\n", xc->server_num);
  1532. /* Ensure no interrupt is still routed to that VP */
  1533. xc->valid = false;
  1534. kvmppc_xive_disable_vcpu_interrupts(vcpu);
  1535. /* Mask the VP IPI */
  1536. xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01);
  1537. /* Free escalations */
  1538. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1539. if (xc->esc_virq[i]) {
  1540. if (kvmppc_xive_has_single_escalation(xc->xive))
  1541. xive_cleanup_single_escalation(vcpu, xc,
  1542. xc->esc_virq[i]);
  1543. free_irq(xc->esc_virq[i], vcpu);
  1544. irq_dispose_mapping(xc->esc_virq[i]);
  1545. kfree(xc->esc_virq_names[i]);
  1546. }
  1547. }
  1548. /* Disable the VP */
  1549. xive_native_disable_vp(xc->vp_id);
  1550. /* Clear the cam word so guest entry won't try to push context */
  1551. vcpu->arch.xive_cam_word = 0;
  1552. /* Free the queues */
  1553. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1554. struct xive_q *q = &xc->queues[i];
  1555. xive_native_disable_queue(xc->vp_id, q, i);
  1556. if (q->qpage) {
  1557. free_pages((unsigned long)q->qpage,
  1558. xive->q_page_order);
  1559. q->qpage = NULL;
  1560. }
  1561. }
  1562. /* Free the IPI */
  1563. if (xc->vp_ipi) {
  1564. xive_cleanup_irq_data(&xc->vp_ipi_data);
  1565. xive_native_free_irq(xc->vp_ipi);
  1566. }
  1567. /* Free the VP */
  1568. kfree(xc);
  1569. /* Cleanup the vcpu */
  1570. vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
  1571. vcpu->arch.xive_vcpu = NULL;
  1572. }
  1573. static bool kvmppc_xive_vcpu_id_valid(struct kvmppc_xive *xive, u32 cpu)
  1574. {
  1575. /* We have a block of xive->nr_servers VPs. We just need to check
  1576. * packed vCPU ids are below that.
  1577. */
  1578. return kvmppc_pack_vcpu_id(xive->kvm, cpu) < xive->nr_servers;
  1579. }
  1580. int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp)
  1581. {
  1582. u32 vp_id;
  1583. if (!kvmppc_xive_vcpu_id_valid(xive, cpu)) {
  1584. pr_devel("Out of bounds !\n");
  1585. return -EINVAL;
  1586. }
  1587. if (xive->vp_base == XIVE_INVALID_VP) {
  1588. xive->vp_base = xive_native_alloc_vp_block(xive->nr_servers);
  1589. pr_devel("VP_Base=%x nr_servers=%d\n", xive->vp_base, xive->nr_servers);
  1590. if (xive->vp_base == XIVE_INVALID_VP)
  1591. return -ENOSPC;
  1592. }
  1593. vp_id = kvmppc_xive_vp(xive, cpu);
  1594. if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
  1595. pr_devel("Duplicate !\n");
  1596. return -EEXIST;
  1597. }
  1598. *vp = vp_id;
  1599. return 0;
  1600. }
  1601. int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
  1602. struct kvm_vcpu *vcpu, u32 cpu)
  1603. {
  1604. struct kvmppc_xive *xive = dev->private;
  1605. struct kvmppc_xive_vcpu *xc;
  1606. int i, r = -EBUSY;
  1607. u32 vp_id;
  1608. pr_devel("connect_vcpu(cpu=%d)\n", cpu);
  1609. if (dev->ops != &kvm_xive_ops) {
  1610. pr_devel("Wrong ops !\n");
  1611. return -EPERM;
  1612. }
  1613. if (xive->kvm != vcpu->kvm)
  1614. return -EPERM;
  1615. if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
  1616. return -EBUSY;
  1617. /* We need to synchronize with queue provisioning */
  1618. mutex_lock(&xive->lock);
  1619. r = kvmppc_xive_compute_vp_id(xive, cpu, &vp_id);
  1620. if (r)
  1621. goto bail;
  1622. xc = kzalloc(sizeof(*xc), GFP_KERNEL);
  1623. if (!xc) {
  1624. r = -ENOMEM;
  1625. goto bail;
  1626. }
  1627. vcpu->arch.xive_vcpu = xc;
  1628. xc->xive = xive;
  1629. xc->vcpu = vcpu;
  1630. xc->server_num = cpu;
  1631. xc->vp_id = vp_id;
  1632. xc->mfrr = 0xff;
  1633. xc->valid = true;
  1634. r = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
  1635. if (r)
  1636. goto bail;
  1637. if (!kvmppc_xive_check_save_restore(vcpu)) {
  1638. pr_err("inconsistent save-restore setup for VCPU %d\n", cpu);
  1639. r = -EIO;
  1640. goto bail;
  1641. }
  1642. /* Configure VCPU fields for use by assembly push/pull */
  1643. vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
  1644. vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
  1645. /* Allocate IPI */
  1646. xc->vp_ipi = xive_native_alloc_irq();
  1647. if (!xc->vp_ipi) {
  1648. pr_err("Failed to allocate xive irq for VCPU IPI\n");
  1649. r = -EIO;
  1650. goto bail;
  1651. }
  1652. pr_devel(" IPI=0x%x\n", xc->vp_ipi);
  1653. r = xive_native_populate_irq_data(xc->vp_ipi, &xc->vp_ipi_data);
  1654. if (r)
  1655. goto bail;
  1656. /*
  1657. * Enable the VP first as the single escalation mode will
  1658. * affect escalation interrupts numbering
  1659. */
  1660. r = xive_native_enable_vp(xc->vp_id, kvmppc_xive_has_single_escalation(xive));
  1661. if (r) {
  1662. pr_err("Failed to enable VP in OPAL, err %d\n", r);
  1663. goto bail;
  1664. }
  1665. /*
  1666. * Initialize queues. Initially we set them all for no queueing
  1667. * and we enable escalation for queue 0 only which we'll use for
  1668. * our mfrr change notifications. If the VCPU is hot-plugged, we
  1669. * do handle provisioning however based on the existing "map"
  1670. * of enabled queues.
  1671. */
  1672. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1673. struct xive_q *q = &xc->queues[i];
  1674. /* Single escalation, no queue 7 */
  1675. if (i == 7 && kvmppc_xive_has_single_escalation(xive))
  1676. break;
  1677. /* Is queue already enabled ? Provision it */
  1678. if (xive->qmap & (1 << i)) {
  1679. r = xive_provision_queue(vcpu, i);
  1680. if (r == 0 && !kvmppc_xive_has_single_escalation(xive))
  1681. kvmppc_xive_attach_escalation(
  1682. vcpu, i, kvmppc_xive_has_single_escalation(xive));
  1683. if (r)
  1684. goto bail;
  1685. } else {
  1686. r = xive_native_configure_queue(xc->vp_id,
  1687. q, i, NULL, 0, true);
  1688. if (r) {
  1689. pr_err("Failed to configure queue %d for VCPU %d\n",
  1690. i, cpu);
  1691. goto bail;
  1692. }
  1693. }
  1694. }
  1695. /* If not done above, attach priority 0 escalation */
  1696. r = kvmppc_xive_attach_escalation(vcpu, 0, kvmppc_xive_has_single_escalation(xive));
  1697. if (r)
  1698. goto bail;
  1699. /* Route the IPI */
  1700. r = xive_native_configure_irq(xc->vp_ipi, xc->vp_id, 0, XICS_IPI);
  1701. if (!r)
  1702. xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_00);
  1703. bail:
  1704. mutex_unlock(&xive->lock);
  1705. if (r) {
  1706. kvmppc_xive_cleanup_vcpu(vcpu);
  1707. return r;
  1708. }
  1709. vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
  1710. return 0;
  1711. }
  1712. /*
  1713. * Scanning of queues before/after migration save
  1714. */
  1715. static void xive_pre_save_set_queued(struct kvmppc_xive *xive, u32 irq)
  1716. {
  1717. struct kvmppc_xive_src_block *sb;
  1718. struct kvmppc_xive_irq_state *state;
  1719. u16 idx;
  1720. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1721. if (!sb)
  1722. return;
  1723. state = &sb->irq_state[idx];
  1724. /* Some sanity checking */
  1725. if (!state->valid) {
  1726. pr_err("invalid irq 0x%x in cpu queue!\n", irq);
  1727. return;
  1728. }
  1729. /*
  1730. * If the interrupt is in a queue it should have P set.
  1731. * We warn so that gets reported. A backtrace isn't useful
  1732. * so no need to use a WARN_ON.
  1733. */
  1734. if (!state->saved_p)
  1735. pr_err("Interrupt 0x%x is marked in a queue but P not set !\n", irq);
  1736. /* Set flag */
  1737. state->in_queue = true;
  1738. }
  1739. static void xive_pre_save_mask_irq(struct kvmppc_xive *xive,
  1740. struct kvmppc_xive_src_block *sb,
  1741. u32 irq)
  1742. {
  1743. struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
  1744. if (!state->valid)
  1745. return;
  1746. /* Mask and save state, this will also sync HW queues */
  1747. state->saved_scan_prio = xive_lock_and_mask(xive, sb, state);
  1748. /* Transfer P and Q */
  1749. state->saved_p = state->old_p;
  1750. state->saved_q = state->old_q;
  1751. /* Unlock */
  1752. arch_spin_unlock(&sb->lock);
  1753. }
  1754. static void xive_pre_save_unmask_irq(struct kvmppc_xive *xive,
  1755. struct kvmppc_xive_src_block *sb,
  1756. u32 irq)
  1757. {
  1758. struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
  1759. if (!state->valid)
  1760. return;
  1761. /*
  1762. * Lock / exclude EOI (not technically necessary if the
  1763. * guest isn't running concurrently. If this becomes a
  1764. * performance issue we can probably remove the lock.
  1765. */
  1766. xive_lock_for_unmask(sb, state);
  1767. /* Restore mask/prio if it wasn't masked */
  1768. if (state->saved_scan_prio != MASKED)
  1769. xive_finish_unmask(xive, sb, state, state->saved_scan_prio);
  1770. /* Unlock */
  1771. arch_spin_unlock(&sb->lock);
  1772. }
  1773. static void xive_pre_save_queue(struct kvmppc_xive *xive, struct xive_q *q)
  1774. {
  1775. u32 idx = q->idx;
  1776. u32 toggle = q->toggle;
  1777. u32 irq;
  1778. do {
  1779. irq = __xive_read_eq(q->qpage, q->msk, &idx, &toggle);
  1780. if (irq > XICS_IPI)
  1781. xive_pre_save_set_queued(xive, irq);
  1782. } while(irq);
  1783. }
  1784. static void xive_pre_save_scan(struct kvmppc_xive *xive)
  1785. {
  1786. struct kvm_vcpu *vcpu = NULL;
  1787. unsigned long i;
  1788. int j;
  1789. /*
  1790. * See comment in xive_get_source() about how this
  1791. * work. Collect a stable state for all interrupts
  1792. */
  1793. for (i = 0; i <= xive->max_sbid; i++) {
  1794. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1795. if (!sb)
  1796. continue;
  1797. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1798. xive_pre_save_mask_irq(xive, sb, j);
  1799. }
  1800. /* Then scan the queues and update the "in_queue" flag */
  1801. kvm_for_each_vcpu(i, vcpu, xive->kvm) {
  1802. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1803. if (!xc)
  1804. continue;
  1805. for (j = 0; j < KVMPPC_XIVE_Q_COUNT; j++) {
  1806. if (xc->queues[j].qpage)
  1807. xive_pre_save_queue(xive, &xc->queues[j]);
  1808. }
  1809. }
  1810. /* Finally restore interrupt states */
  1811. for (i = 0; i <= xive->max_sbid; i++) {
  1812. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1813. if (!sb)
  1814. continue;
  1815. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1816. xive_pre_save_unmask_irq(xive, sb, j);
  1817. }
  1818. }
  1819. static void xive_post_save_scan(struct kvmppc_xive *xive)
  1820. {
  1821. u32 i, j;
  1822. /* Clear all the in_queue flags */
  1823. for (i = 0; i <= xive->max_sbid; i++) {
  1824. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1825. if (!sb)
  1826. continue;
  1827. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1828. sb->irq_state[j].in_queue = false;
  1829. }
  1830. /* Next get_source() will do a new scan */
  1831. xive->saved_src_count = 0;
  1832. }
  1833. /*
  1834. * This returns the source configuration and state to user space.
  1835. */
  1836. static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr)
  1837. {
  1838. struct kvmppc_xive_src_block *sb;
  1839. struct kvmppc_xive_irq_state *state;
  1840. u64 __user *ubufp = (u64 __user *) addr;
  1841. u64 val, prio;
  1842. u16 idx;
  1843. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1844. if (!sb)
  1845. return -ENOENT;
  1846. state = &sb->irq_state[idx];
  1847. if (!state->valid)
  1848. return -ENOENT;
  1849. pr_devel("get_source(%ld)...\n", irq);
  1850. /*
  1851. * So to properly save the state into something that looks like a
  1852. * XICS migration stream we cannot treat interrupts individually.
  1853. *
  1854. * We need, instead, mask them all (& save their previous PQ state)
  1855. * to get a stable state in the HW, then sync them to ensure that
  1856. * any interrupt that had already fired hits its queue, and finally
  1857. * scan all the queues to collect which interrupts are still present
  1858. * in the queues, so we can set the "pending" flag on them and
  1859. * they can be resent on restore.
  1860. *
  1861. * So we do it all when the "first" interrupt gets saved, all the
  1862. * state is collected at that point, the rest of xive_get_source()
  1863. * will merely collect and convert that state to the expected
  1864. * userspace bit mask.
  1865. */
  1866. if (xive->saved_src_count == 0)
  1867. xive_pre_save_scan(xive);
  1868. xive->saved_src_count++;
  1869. /* Convert saved state into something compatible with xics */
  1870. val = state->act_server;
  1871. prio = state->saved_scan_prio;
  1872. if (prio == MASKED) {
  1873. val |= KVM_XICS_MASKED;
  1874. prio = state->saved_priority;
  1875. }
  1876. val |= prio << KVM_XICS_PRIORITY_SHIFT;
  1877. if (state->lsi) {
  1878. val |= KVM_XICS_LEVEL_SENSITIVE;
  1879. if (state->saved_p)
  1880. val |= KVM_XICS_PENDING;
  1881. } else {
  1882. if (state->saved_p)
  1883. val |= KVM_XICS_PRESENTED;
  1884. if (state->saved_q)
  1885. val |= KVM_XICS_QUEUED;
  1886. /*
  1887. * We mark it pending (which will attempt a re-delivery)
  1888. * if we are in a queue *or* we were masked and had
  1889. * Q set which is equivalent to the XICS "masked pending"
  1890. * state
  1891. */
  1892. if (state->in_queue || (prio == MASKED && state->saved_q))
  1893. val |= KVM_XICS_PENDING;
  1894. }
  1895. /*
  1896. * If that was the last interrupt saved, reset the
  1897. * in_queue flags
  1898. */
  1899. if (xive->saved_src_count == xive->src_count)
  1900. xive_post_save_scan(xive);
  1901. /* Copy the result to userspace */
  1902. if (put_user(val, ubufp))
  1903. return -EFAULT;
  1904. return 0;
  1905. }
  1906. struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(
  1907. struct kvmppc_xive *xive, int irq)
  1908. {
  1909. struct kvmppc_xive_src_block *sb;
  1910. int i, bid;
  1911. bid = irq >> KVMPPC_XICS_ICS_SHIFT;
  1912. mutex_lock(&xive->lock);
  1913. /* block already exists - somebody else got here first */
  1914. if (xive->src_blocks[bid])
  1915. goto out;
  1916. /* Create the ICS */
  1917. sb = kzalloc(sizeof(*sb), GFP_KERNEL);
  1918. if (!sb)
  1919. goto out;
  1920. sb->id = bid;
  1921. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  1922. sb->irq_state[i].number = (bid << KVMPPC_XICS_ICS_SHIFT) | i;
  1923. sb->irq_state[i].eisn = 0;
  1924. sb->irq_state[i].guest_priority = MASKED;
  1925. sb->irq_state[i].saved_priority = MASKED;
  1926. sb->irq_state[i].act_priority = MASKED;
  1927. }
  1928. smp_wmb();
  1929. xive->src_blocks[bid] = sb;
  1930. if (bid > xive->max_sbid)
  1931. xive->max_sbid = bid;
  1932. out:
  1933. mutex_unlock(&xive->lock);
  1934. return xive->src_blocks[bid];
  1935. }
  1936. static bool xive_check_delayed_irq(struct kvmppc_xive *xive, u32 irq)
  1937. {
  1938. struct kvm *kvm = xive->kvm;
  1939. struct kvm_vcpu *vcpu = NULL;
  1940. unsigned long i;
  1941. kvm_for_each_vcpu(i, vcpu, kvm) {
  1942. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1943. if (!xc)
  1944. continue;
  1945. if (xc->delayed_irq == irq) {
  1946. xc->delayed_irq = 0;
  1947. xive->delayed_irqs--;
  1948. return true;
  1949. }
  1950. }
  1951. return false;
  1952. }
  1953. static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr)
  1954. {
  1955. struct kvmppc_xive_src_block *sb;
  1956. struct kvmppc_xive_irq_state *state;
  1957. u64 __user *ubufp = (u64 __user *) addr;
  1958. u16 idx;
  1959. u64 val;
  1960. u8 act_prio, guest_prio;
  1961. u32 server;
  1962. int rc = 0;
  1963. if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
  1964. return -ENOENT;
  1965. pr_devel("set_source(irq=0x%lx)\n", irq);
  1966. /* Find the source */
  1967. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1968. if (!sb) {
  1969. pr_devel("No source, creating source block...\n");
  1970. sb = kvmppc_xive_create_src_block(xive, irq);
  1971. if (!sb) {
  1972. pr_devel("Failed to create block...\n");
  1973. return -ENOMEM;
  1974. }
  1975. }
  1976. state = &sb->irq_state[idx];
  1977. /* Read user passed data */
  1978. if (get_user(val, ubufp)) {
  1979. pr_devel("fault getting user info !\n");
  1980. return -EFAULT;
  1981. }
  1982. server = val & KVM_XICS_DESTINATION_MASK;
  1983. guest_prio = val >> KVM_XICS_PRIORITY_SHIFT;
  1984. pr_devel(" val=0x016%llx (server=0x%x, guest_prio=%d)\n",
  1985. val, server, guest_prio);
  1986. /*
  1987. * If the source doesn't already have an IPI, allocate
  1988. * one and get the corresponding data
  1989. */
  1990. if (!state->ipi_number) {
  1991. state->ipi_number = xive_native_alloc_irq();
  1992. if (state->ipi_number == 0) {
  1993. pr_devel("Failed to allocate IPI !\n");
  1994. return -ENOMEM;
  1995. }
  1996. xive_native_populate_irq_data(state->ipi_number, &state->ipi_data);
  1997. pr_devel(" src_ipi=0x%x\n", state->ipi_number);
  1998. }
  1999. /*
  2000. * We use lock_and_mask() to set us in the right masked
  2001. * state. We will override that state from the saved state
  2002. * further down, but this will handle the cases of interrupts
  2003. * that need FW masking. We set the initial guest_priority to
  2004. * 0 before calling it to ensure it actually performs the masking.
  2005. */
  2006. state->guest_priority = 0;
  2007. xive_lock_and_mask(xive, sb, state);
  2008. /*
  2009. * Now, we select a target if we have one. If we don't we
  2010. * leave the interrupt untargetted. It means that an interrupt
  2011. * can become "untargetted" accross migration if it was masked
  2012. * by set_xive() but there is little we can do about it.
  2013. */
  2014. /* First convert prio and mark interrupt as untargetted */
  2015. act_prio = xive_prio_from_guest(guest_prio);
  2016. state->act_priority = MASKED;
  2017. /*
  2018. * We need to drop the lock due to the mutex below. Hopefully
  2019. * nothing is touching that interrupt yet since it hasn't been
  2020. * advertized to a running guest yet
  2021. */
  2022. arch_spin_unlock(&sb->lock);
  2023. /* If we have a priority target the interrupt */
  2024. if (act_prio != MASKED) {
  2025. /* First, check provisioning of queues */
  2026. mutex_lock(&xive->lock);
  2027. rc = xive_check_provisioning(xive->kvm, act_prio);
  2028. mutex_unlock(&xive->lock);
  2029. /* Target interrupt */
  2030. if (rc == 0)
  2031. rc = xive_target_interrupt(xive->kvm, state,
  2032. server, act_prio);
  2033. /*
  2034. * If provisioning or targetting failed, leave it
  2035. * alone and masked. It will remain disabled until
  2036. * the guest re-targets it.
  2037. */
  2038. }
  2039. /*
  2040. * Find out if this was a delayed irq stashed in an ICP,
  2041. * in which case, treat it as pending
  2042. */
  2043. if (xive->delayed_irqs && xive_check_delayed_irq(xive, irq)) {
  2044. val |= KVM_XICS_PENDING;
  2045. pr_devel(" Found delayed ! forcing PENDING !\n");
  2046. }
  2047. /* Cleanup the SW state */
  2048. state->old_p = false;
  2049. state->old_q = false;
  2050. state->lsi = false;
  2051. state->asserted = false;
  2052. /* Restore LSI state */
  2053. if (val & KVM_XICS_LEVEL_SENSITIVE) {
  2054. state->lsi = true;
  2055. if (val & KVM_XICS_PENDING)
  2056. state->asserted = true;
  2057. pr_devel(" LSI ! Asserted=%d\n", state->asserted);
  2058. }
  2059. /*
  2060. * Restore P and Q. If the interrupt was pending, we
  2061. * force Q and !P, which will trigger a resend.
  2062. *
  2063. * That means that a guest that had both an interrupt
  2064. * pending (queued) and Q set will restore with only
  2065. * one instance of that interrupt instead of 2, but that
  2066. * is perfectly fine as coalescing interrupts that haven't
  2067. * been presented yet is always allowed.
  2068. */
  2069. if (val & KVM_XICS_PRESENTED && !(val & KVM_XICS_PENDING))
  2070. state->old_p = true;
  2071. if (val & KVM_XICS_QUEUED || val & KVM_XICS_PENDING)
  2072. state->old_q = true;
  2073. pr_devel(" P=%d, Q=%d\n", state->old_p, state->old_q);
  2074. /*
  2075. * If the interrupt was unmasked, update guest priority and
  2076. * perform the appropriate state transition and do a
  2077. * re-trigger if necessary.
  2078. */
  2079. if (val & KVM_XICS_MASKED) {
  2080. pr_devel(" masked, saving prio\n");
  2081. state->guest_priority = MASKED;
  2082. state->saved_priority = guest_prio;
  2083. } else {
  2084. pr_devel(" unmasked, restoring to prio %d\n", guest_prio);
  2085. xive_finish_unmask(xive, sb, state, guest_prio);
  2086. state->saved_priority = guest_prio;
  2087. }
  2088. /* Increment the number of valid sources and mark this one valid */
  2089. if (!state->valid)
  2090. xive->src_count++;
  2091. state->valid = true;
  2092. return 0;
  2093. }
  2094. int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  2095. bool line_status)
  2096. {
  2097. struct kvmppc_xive *xive = kvm->arch.xive;
  2098. struct kvmppc_xive_src_block *sb;
  2099. struct kvmppc_xive_irq_state *state;
  2100. u16 idx;
  2101. if (!xive)
  2102. return -ENODEV;
  2103. sb = kvmppc_xive_find_source(xive, irq, &idx);
  2104. if (!sb)
  2105. return -EINVAL;
  2106. /* Perform locklessly .... (we need to do some RCUisms here...) */
  2107. state = &sb->irq_state[idx];
  2108. if (!state->valid)
  2109. return -EINVAL;
  2110. /* We don't allow a trigger on a passed-through interrupt */
  2111. if (state->pt_number)
  2112. return -EINVAL;
  2113. if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL)
  2114. state->asserted = true;
  2115. else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
  2116. state->asserted = false;
  2117. return 0;
  2118. }
  2119. /* Trigger the IPI */
  2120. xive_irq_trigger(&state->ipi_data);
  2121. return 0;
  2122. }
  2123. int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr)
  2124. {
  2125. u32 __user *ubufp = (u32 __user *) addr;
  2126. u32 nr_servers;
  2127. int rc = 0;
  2128. if (get_user(nr_servers, ubufp))
  2129. return -EFAULT;
  2130. pr_devel("%s nr_servers=%u\n", __func__, nr_servers);
  2131. if (!nr_servers || nr_servers > KVM_MAX_VCPU_IDS)
  2132. return -EINVAL;
  2133. mutex_lock(&xive->lock);
  2134. if (xive->vp_base != XIVE_INVALID_VP)
  2135. /* The VP block is allocated once and freed when the device
  2136. * is released. Better not allow to change its size since its
  2137. * used by connect_vcpu to validate vCPU ids are valid (eg,
  2138. * setting it back to a higher value could allow connect_vcpu
  2139. * to come up with a VP id that goes beyond the VP block, which
  2140. * is likely to cause a crash in OPAL).
  2141. */
  2142. rc = -EBUSY;
  2143. else if (nr_servers > KVM_MAX_VCPUS)
  2144. /* We don't need more servers. Higher vCPU ids get packed
  2145. * down below KVM_MAX_VCPUS by kvmppc_pack_vcpu_id().
  2146. */
  2147. xive->nr_servers = KVM_MAX_VCPUS;
  2148. else
  2149. xive->nr_servers = nr_servers;
  2150. mutex_unlock(&xive->lock);
  2151. return rc;
  2152. }
  2153. static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  2154. {
  2155. struct kvmppc_xive *xive = dev->private;
  2156. /* We honor the existing XICS ioctl */
  2157. switch (attr->group) {
  2158. case KVM_DEV_XICS_GRP_SOURCES:
  2159. return xive_set_source(xive, attr->attr, attr->addr);
  2160. case KVM_DEV_XICS_GRP_CTRL:
  2161. switch (attr->attr) {
  2162. case KVM_DEV_XICS_NR_SERVERS:
  2163. return kvmppc_xive_set_nr_servers(xive, attr->addr);
  2164. }
  2165. }
  2166. return -ENXIO;
  2167. }
  2168. static int xive_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  2169. {
  2170. struct kvmppc_xive *xive = dev->private;
  2171. /* We honor the existing XICS ioctl */
  2172. switch (attr->group) {
  2173. case KVM_DEV_XICS_GRP_SOURCES:
  2174. return xive_get_source(xive, attr->attr, attr->addr);
  2175. }
  2176. return -ENXIO;
  2177. }
  2178. static int xive_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  2179. {
  2180. /* We honor the same limits as XICS, at least for now */
  2181. switch (attr->group) {
  2182. case KVM_DEV_XICS_GRP_SOURCES:
  2183. if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
  2184. attr->attr < KVMPPC_XICS_NR_IRQS)
  2185. return 0;
  2186. break;
  2187. case KVM_DEV_XICS_GRP_CTRL:
  2188. switch (attr->attr) {
  2189. case KVM_DEV_XICS_NR_SERVERS:
  2190. return 0;
  2191. }
  2192. }
  2193. return -ENXIO;
  2194. }
  2195. static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd)
  2196. {
  2197. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
  2198. xive_native_configure_irq(hw_num, 0, MASKED, 0);
  2199. }
  2200. void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb)
  2201. {
  2202. int i;
  2203. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  2204. struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
  2205. if (!state->valid)
  2206. continue;
  2207. kvmppc_xive_cleanup_irq(state->ipi_number, &state->ipi_data);
  2208. xive_cleanup_irq_data(&state->ipi_data);
  2209. xive_native_free_irq(state->ipi_number);
  2210. /* Pass-through, cleanup too but keep IRQ hw data */
  2211. if (state->pt_number)
  2212. kvmppc_xive_cleanup_irq(state->pt_number, state->pt_data);
  2213. state->valid = false;
  2214. }
  2215. }
  2216. /*
  2217. * Called when device fd is closed. kvm->lock is held.
  2218. */
  2219. static void kvmppc_xive_release(struct kvm_device *dev)
  2220. {
  2221. struct kvmppc_xive *xive = dev->private;
  2222. struct kvm *kvm = xive->kvm;
  2223. struct kvm_vcpu *vcpu;
  2224. unsigned long i;
  2225. pr_devel("Releasing xive device\n");
  2226. /*
  2227. * Since this is the device release function, we know that
  2228. * userspace does not have any open fd referring to the
  2229. * device. Therefore there can not be any of the device
  2230. * attribute set/get functions being executed concurrently,
  2231. * and similarly, the connect_vcpu and set/clr_mapped
  2232. * functions also cannot be being executed.
  2233. */
  2234. debugfs_remove(xive->dentry);
  2235. /*
  2236. * We should clean up the vCPU interrupt presenters first.
  2237. */
  2238. kvm_for_each_vcpu(i, vcpu, kvm) {
  2239. /*
  2240. * Take vcpu->mutex to ensure that no one_reg get/set ioctl
  2241. * (i.e. kvmppc_xive_[gs]et_icp) can be done concurrently.
  2242. * Holding the vcpu->mutex also means that the vcpu cannot
  2243. * be executing the KVM_RUN ioctl, and therefore it cannot
  2244. * be executing the XIVE push or pull code or accessing
  2245. * the XIVE MMIO regions.
  2246. */
  2247. mutex_lock(&vcpu->mutex);
  2248. kvmppc_xive_cleanup_vcpu(vcpu);
  2249. mutex_unlock(&vcpu->mutex);
  2250. }
  2251. /*
  2252. * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type
  2253. * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe
  2254. * against xive code getting called during vcpu execution or
  2255. * set/get one_reg operations.
  2256. */
  2257. kvm->arch.xive = NULL;
  2258. /* Mask and free interrupts */
  2259. for (i = 0; i <= xive->max_sbid; i++) {
  2260. if (xive->src_blocks[i])
  2261. kvmppc_xive_free_sources(xive->src_blocks[i]);
  2262. kfree(xive->src_blocks[i]);
  2263. xive->src_blocks[i] = NULL;
  2264. }
  2265. if (xive->vp_base != XIVE_INVALID_VP)
  2266. xive_native_free_vp_block(xive->vp_base);
  2267. /*
  2268. * A reference of the kvmppc_xive pointer is now kept under
  2269. * the xive_devices struct of the machine for reuse. It is
  2270. * freed when the VM is destroyed for now until we fix all the
  2271. * execution paths.
  2272. */
  2273. kfree(dev);
  2274. }
  2275. /*
  2276. * When the guest chooses the interrupt mode (XICS legacy or XIVE
  2277. * native), the VM will switch of KVM device. The previous device will
  2278. * be "released" before the new one is created.
  2279. *
  2280. * Until we are sure all execution paths are well protected, provide a
  2281. * fail safe (transitional) method for device destruction, in which
  2282. * the XIVE device pointer is recycled and not directly freed.
  2283. */
  2284. struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type)
  2285. {
  2286. struct kvmppc_xive **kvm_xive_device = type == KVM_DEV_TYPE_XIVE ?
  2287. &kvm->arch.xive_devices.native :
  2288. &kvm->arch.xive_devices.xics_on_xive;
  2289. struct kvmppc_xive *xive = *kvm_xive_device;
  2290. if (!xive) {
  2291. xive = kzalloc(sizeof(*xive), GFP_KERNEL);
  2292. *kvm_xive_device = xive;
  2293. } else {
  2294. memset(xive, 0, sizeof(*xive));
  2295. }
  2296. return xive;
  2297. }
  2298. /*
  2299. * Create a XICS device with XIVE backend. kvm->lock is held.
  2300. */
  2301. static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
  2302. {
  2303. struct kvmppc_xive *xive;
  2304. struct kvm *kvm = dev->kvm;
  2305. pr_devel("Creating xive for partition\n");
  2306. /* Already there ? */
  2307. if (kvm->arch.xive)
  2308. return -EEXIST;
  2309. xive = kvmppc_xive_get_device(kvm, type);
  2310. if (!xive)
  2311. return -ENOMEM;
  2312. dev->private = xive;
  2313. xive->dev = dev;
  2314. xive->kvm = kvm;
  2315. mutex_init(&xive->lock);
  2316. /* We use the default queue size set by the host */
  2317. xive->q_order = xive_native_default_eq_shift();
  2318. if (xive->q_order < PAGE_SHIFT)
  2319. xive->q_page_order = 0;
  2320. else
  2321. xive->q_page_order = xive->q_order - PAGE_SHIFT;
  2322. /* VP allocation is delayed to the first call to connect_vcpu */
  2323. xive->vp_base = XIVE_INVALID_VP;
  2324. /* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
  2325. * on a POWER9 system.
  2326. */
  2327. xive->nr_servers = KVM_MAX_VCPUS;
  2328. if (xive_native_has_single_escalation())
  2329. xive->flags |= KVMPPC_XIVE_FLAG_SINGLE_ESCALATION;
  2330. if (xive_native_has_save_restore())
  2331. xive->flags |= KVMPPC_XIVE_FLAG_SAVE_RESTORE;
  2332. kvm->arch.xive = xive;
  2333. return 0;
  2334. }
  2335. int kvmppc_xive_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
  2336. {
  2337. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  2338. /* The VM should have configured XICS mode before doing XICS hcalls. */
  2339. if (!kvmppc_xics_enabled(vcpu))
  2340. return H_TOO_HARD;
  2341. switch (req) {
  2342. case H_XIRR:
  2343. return xive_vm_h_xirr(vcpu);
  2344. case H_CPPR:
  2345. return xive_vm_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
  2346. case H_EOI:
  2347. return xive_vm_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
  2348. case H_IPI:
  2349. return xive_vm_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
  2350. kvmppc_get_gpr(vcpu, 5));
  2351. case H_IPOLL:
  2352. return xive_vm_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
  2353. case H_XIRR_X:
  2354. xive_vm_h_xirr(vcpu);
  2355. kvmppc_set_gpr(vcpu, 5, get_tb() + vc->tb_offset);
  2356. return H_SUCCESS;
  2357. }
  2358. return H_UNSUPPORTED;
  2359. }
  2360. EXPORT_SYMBOL_GPL(kvmppc_xive_xics_hcall);
  2361. int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu)
  2362. {
  2363. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  2364. unsigned int i;
  2365. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  2366. struct xive_q *q = &xc->queues[i];
  2367. u32 i0, i1, idx;
  2368. if (!q->qpage && !xc->esc_virq[i])
  2369. continue;
  2370. if (q->qpage) {
  2371. seq_printf(m, " q[%d]: ", i);
  2372. idx = q->idx;
  2373. i0 = be32_to_cpup(q->qpage + idx);
  2374. idx = (idx + 1) & q->msk;
  2375. i1 = be32_to_cpup(q->qpage + idx);
  2376. seq_printf(m, "T=%d %08x %08x...\n", q->toggle,
  2377. i0, i1);
  2378. }
  2379. if (xc->esc_virq[i]) {
  2380. struct irq_data *d = irq_get_irq_data(xc->esc_virq[i]);
  2381. struct xive_irq_data *xd =
  2382. irq_data_get_irq_handler_data(d);
  2383. u64 pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
  2384. seq_printf(m, " ESC %d %c%c EOI @%llx",
  2385. xc->esc_virq[i],
  2386. (pq & XIVE_ESB_VAL_P) ? 'P' : '-',
  2387. (pq & XIVE_ESB_VAL_Q) ? 'Q' : '-',
  2388. xd->eoi_page);
  2389. seq_puts(m, "\n");
  2390. }
  2391. }
  2392. return 0;
  2393. }
  2394. void kvmppc_xive_debug_show_sources(struct seq_file *m,
  2395. struct kvmppc_xive_src_block *sb)
  2396. {
  2397. int i;
  2398. seq_puts(m, " LISN HW/CHIP TYPE PQ EISN CPU/PRIO\n");
  2399. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  2400. struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
  2401. struct xive_irq_data *xd;
  2402. u64 pq;
  2403. u32 hw_num;
  2404. if (!state->valid)
  2405. continue;
  2406. kvmppc_xive_select_irq(state, &hw_num, &xd);
  2407. pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
  2408. seq_printf(m, "%08x %08x/%02x", state->number, hw_num,
  2409. xd->src_chip);
  2410. if (state->lsi)
  2411. seq_printf(m, " %cLSI", state->asserted ? '^' : ' ');
  2412. else
  2413. seq_puts(m, " MSI");
  2414. seq_printf(m, " %s %c%c %08x % 4d/%d",
  2415. state->ipi_number == hw_num ? "IPI" : " PT",
  2416. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  2417. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  2418. state->eisn, state->act_server,
  2419. state->act_priority);
  2420. seq_puts(m, "\n");
  2421. }
  2422. }
  2423. static int xive_debug_show(struct seq_file *m, void *private)
  2424. {
  2425. struct kvmppc_xive *xive = m->private;
  2426. struct kvm *kvm = xive->kvm;
  2427. struct kvm_vcpu *vcpu;
  2428. u64 t_rm_h_xirr = 0;
  2429. u64 t_rm_h_ipoll = 0;
  2430. u64 t_rm_h_cppr = 0;
  2431. u64 t_rm_h_eoi = 0;
  2432. u64 t_rm_h_ipi = 0;
  2433. u64 t_vm_h_xirr = 0;
  2434. u64 t_vm_h_ipoll = 0;
  2435. u64 t_vm_h_cppr = 0;
  2436. u64 t_vm_h_eoi = 0;
  2437. u64 t_vm_h_ipi = 0;
  2438. unsigned long i;
  2439. if (!kvm)
  2440. return 0;
  2441. seq_puts(m, "=========\nVCPU state\n=========\n");
  2442. kvm_for_each_vcpu(i, vcpu, kvm) {
  2443. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  2444. if (!xc)
  2445. continue;
  2446. seq_printf(m, "VCPU %d: VP:%#x/%02x\n"
  2447. " CPPR:%#x HWCPPR:%#x MFRR:%#x PEND:%#x h_xirr: R=%lld V=%lld\n",
  2448. xc->server_num, xc->vp_id, xc->vp_chip_id,
  2449. xc->cppr, xc->hw_cppr,
  2450. xc->mfrr, xc->pending,
  2451. xc->stat_rm_h_xirr, xc->stat_vm_h_xirr);
  2452. kvmppc_xive_debug_show_queues(m, vcpu);
  2453. t_rm_h_xirr += xc->stat_rm_h_xirr;
  2454. t_rm_h_ipoll += xc->stat_rm_h_ipoll;
  2455. t_rm_h_cppr += xc->stat_rm_h_cppr;
  2456. t_rm_h_eoi += xc->stat_rm_h_eoi;
  2457. t_rm_h_ipi += xc->stat_rm_h_ipi;
  2458. t_vm_h_xirr += xc->stat_vm_h_xirr;
  2459. t_vm_h_ipoll += xc->stat_vm_h_ipoll;
  2460. t_vm_h_cppr += xc->stat_vm_h_cppr;
  2461. t_vm_h_eoi += xc->stat_vm_h_eoi;
  2462. t_vm_h_ipi += xc->stat_vm_h_ipi;
  2463. }
  2464. seq_puts(m, "Hcalls totals\n");
  2465. seq_printf(m, " H_XIRR R=%10lld V=%10lld\n", t_rm_h_xirr, t_vm_h_xirr);
  2466. seq_printf(m, " H_IPOLL R=%10lld V=%10lld\n", t_rm_h_ipoll, t_vm_h_ipoll);
  2467. seq_printf(m, " H_CPPR R=%10lld V=%10lld\n", t_rm_h_cppr, t_vm_h_cppr);
  2468. seq_printf(m, " H_EOI R=%10lld V=%10lld\n", t_rm_h_eoi, t_vm_h_eoi);
  2469. seq_printf(m, " H_IPI R=%10lld V=%10lld\n", t_rm_h_ipi, t_vm_h_ipi);
  2470. seq_puts(m, "=========\nSources\n=========\n");
  2471. for (i = 0; i <= xive->max_sbid; i++) {
  2472. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  2473. if (sb) {
  2474. arch_spin_lock(&sb->lock);
  2475. kvmppc_xive_debug_show_sources(m, sb);
  2476. arch_spin_unlock(&sb->lock);
  2477. }
  2478. }
  2479. return 0;
  2480. }
  2481. DEFINE_SHOW_ATTRIBUTE(xive_debug);
  2482. static void xive_debugfs_init(struct kvmppc_xive *xive)
  2483. {
  2484. xive->dentry = debugfs_create_file("xive", S_IRUGO, xive->kvm->debugfs_dentry,
  2485. xive, &xive_debug_fops);
  2486. pr_debug("%s: created\n", __func__);
  2487. }
  2488. static void kvmppc_xive_init(struct kvm_device *dev)
  2489. {
  2490. struct kvmppc_xive *xive = dev->private;
  2491. /* Register some debug interfaces */
  2492. xive_debugfs_init(xive);
  2493. }
  2494. struct kvm_device_ops kvm_xive_ops = {
  2495. .name = "kvm-xive",
  2496. .create = kvmppc_xive_create,
  2497. .init = kvmppc_xive_init,
  2498. .release = kvmppc_xive_release,
  2499. .set_attr = xive_set_attr,
  2500. .get_attr = xive_get_attr,
  2501. .has_attr = xive_has_attr,
  2502. };