book3s_xive.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980
  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. kvmppc_set_gpr(vcpu, 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. kvmppc_set_gpr(vcpu, 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 synthesized 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. * subsequent 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. subsequent 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-%lld-%d",
  740. vcpu->kvm->arch.lpid, xc->server_num);
  741. else
  742. name = kasprintf(GFP_KERNEL, "kvm-%lld-%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, int irq)
  1508. {
  1509. struct irq_data *d = irq_get_irq_data(irq);
  1510. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  1511. /*
  1512. * This slightly odd sequence gives the right result
  1513. * (i.e. stale_p set if xive_esc_on is false) even if
  1514. * we race with xive_esc_irq() and xive_irq_eoi().
  1515. */
  1516. xd->stale_p = false;
  1517. smp_mb(); /* paired with smb_wmb in xive_esc_irq */
  1518. if (!vcpu->arch.xive_esc_on)
  1519. xd->stale_p = true;
  1520. }
  1521. void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
  1522. {
  1523. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1524. struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
  1525. int i;
  1526. if (!kvmppc_xics_enabled(vcpu))
  1527. return;
  1528. if (!xc)
  1529. return;
  1530. pr_devel("cleanup_vcpu(cpu=%d)\n", xc->server_num);
  1531. /* Ensure no interrupt is still routed to that VP */
  1532. xc->valid = false;
  1533. kvmppc_xive_disable_vcpu_interrupts(vcpu);
  1534. /* Mask the VP IPI */
  1535. xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01);
  1536. /* Free escalations */
  1537. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1538. if (xc->esc_virq[i]) {
  1539. if (kvmppc_xive_has_single_escalation(xc->xive))
  1540. xive_cleanup_single_escalation(vcpu, xc->esc_virq[i]);
  1541. free_irq(xc->esc_virq[i], vcpu);
  1542. irq_dispose_mapping(xc->esc_virq[i]);
  1543. kfree(xc->esc_virq_names[i]);
  1544. }
  1545. }
  1546. /* Disable the VP */
  1547. xive_native_disable_vp(xc->vp_id);
  1548. /* Clear the cam word so guest entry won't try to push context */
  1549. vcpu->arch.xive_cam_word = 0;
  1550. /* Free the queues */
  1551. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1552. struct xive_q *q = &xc->queues[i];
  1553. xive_native_disable_queue(xc->vp_id, q, i);
  1554. if (q->qpage) {
  1555. free_pages((unsigned long)q->qpage,
  1556. xive->q_page_order);
  1557. q->qpage = NULL;
  1558. }
  1559. }
  1560. /* Free the IPI */
  1561. if (xc->vp_ipi) {
  1562. xive_cleanup_irq_data(&xc->vp_ipi_data);
  1563. xive_native_free_irq(xc->vp_ipi);
  1564. }
  1565. /* Free the VP */
  1566. kfree(xc);
  1567. /* Cleanup the vcpu */
  1568. vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
  1569. vcpu->arch.xive_vcpu = NULL;
  1570. }
  1571. static bool kvmppc_xive_vcpu_id_valid(struct kvmppc_xive *xive, u32 cpu)
  1572. {
  1573. /* We have a block of xive->nr_servers VPs. We just need to check
  1574. * packed vCPU ids are below that.
  1575. */
  1576. return kvmppc_pack_vcpu_id(xive->kvm, cpu) < xive->nr_servers;
  1577. }
  1578. int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp)
  1579. {
  1580. u32 vp_id;
  1581. if (!kvmppc_xive_vcpu_id_valid(xive, cpu)) {
  1582. pr_devel("Out of bounds !\n");
  1583. return -EINVAL;
  1584. }
  1585. if (xive->vp_base == XIVE_INVALID_VP) {
  1586. xive->vp_base = xive_native_alloc_vp_block(xive->nr_servers);
  1587. pr_devel("VP_Base=%x nr_servers=%d\n", xive->vp_base, xive->nr_servers);
  1588. if (xive->vp_base == XIVE_INVALID_VP)
  1589. return -ENOSPC;
  1590. }
  1591. vp_id = kvmppc_xive_vp(xive, cpu);
  1592. if (kvmppc_xive_vp_in_use(xive->kvm, vp_id)) {
  1593. pr_devel("Duplicate !\n");
  1594. return -EEXIST;
  1595. }
  1596. *vp = vp_id;
  1597. return 0;
  1598. }
  1599. int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
  1600. struct kvm_vcpu *vcpu, u32 cpu)
  1601. {
  1602. struct kvmppc_xive *xive = dev->private;
  1603. struct kvmppc_xive_vcpu *xc;
  1604. int i, r = -EBUSY;
  1605. u32 vp_id;
  1606. pr_devel("connect_vcpu(cpu=%d)\n", cpu);
  1607. if (dev->ops != &kvm_xive_ops) {
  1608. pr_devel("Wrong ops !\n");
  1609. return -EPERM;
  1610. }
  1611. if (xive->kvm != vcpu->kvm)
  1612. return -EPERM;
  1613. if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
  1614. return -EBUSY;
  1615. /* We need to synchronize with queue provisioning */
  1616. mutex_lock(&xive->lock);
  1617. r = kvmppc_xive_compute_vp_id(xive, cpu, &vp_id);
  1618. if (r)
  1619. goto bail;
  1620. xc = kzalloc(sizeof(*xc), GFP_KERNEL);
  1621. if (!xc) {
  1622. r = -ENOMEM;
  1623. goto bail;
  1624. }
  1625. vcpu->arch.xive_vcpu = xc;
  1626. xc->xive = xive;
  1627. xc->vcpu = vcpu;
  1628. xc->server_num = cpu;
  1629. xc->vp_id = vp_id;
  1630. xc->mfrr = 0xff;
  1631. xc->valid = true;
  1632. r = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
  1633. if (r)
  1634. goto bail;
  1635. if (!kvmppc_xive_check_save_restore(vcpu)) {
  1636. pr_err("inconsistent save-restore setup for VCPU %d\n", cpu);
  1637. r = -EIO;
  1638. goto bail;
  1639. }
  1640. /* Configure VCPU fields for use by assembly push/pull */
  1641. vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
  1642. vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
  1643. /* Allocate IPI */
  1644. xc->vp_ipi = xive_native_alloc_irq();
  1645. if (!xc->vp_ipi) {
  1646. pr_err("Failed to allocate xive irq for VCPU IPI\n");
  1647. r = -EIO;
  1648. goto bail;
  1649. }
  1650. pr_devel(" IPI=0x%x\n", xc->vp_ipi);
  1651. r = xive_native_populate_irq_data(xc->vp_ipi, &xc->vp_ipi_data);
  1652. if (r)
  1653. goto bail;
  1654. /*
  1655. * Enable the VP first as the single escalation mode will
  1656. * affect escalation interrupts numbering
  1657. */
  1658. r = xive_native_enable_vp(xc->vp_id, kvmppc_xive_has_single_escalation(xive));
  1659. if (r) {
  1660. pr_err("Failed to enable VP in OPAL, err %d\n", r);
  1661. goto bail;
  1662. }
  1663. /*
  1664. * Initialize queues. Initially we set them all for no queueing
  1665. * and we enable escalation for queue 0 only which we'll use for
  1666. * our mfrr change notifications. If the VCPU is hot-plugged, we
  1667. * do handle provisioning however based on the existing "map"
  1668. * of enabled queues.
  1669. */
  1670. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1671. struct xive_q *q = &xc->queues[i];
  1672. /* Single escalation, no queue 7 */
  1673. if (i == 7 && kvmppc_xive_has_single_escalation(xive))
  1674. break;
  1675. /* Is queue already enabled ? Provision it */
  1676. if (xive->qmap & (1 << i)) {
  1677. r = xive_provision_queue(vcpu, i);
  1678. if (r == 0 && !kvmppc_xive_has_single_escalation(xive))
  1679. kvmppc_xive_attach_escalation(
  1680. vcpu, i, kvmppc_xive_has_single_escalation(xive));
  1681. if (r)
  1682. goto bail;
  1683. } else {
  1684. r = xive_native_configure_queue(xc->vp_id,
  1685. q, i, NULL, 0, true);
  1686. if (r) {
  1687. pr_err("Failed to configure queue %d for VCPU %d\n",
  1688. i, cpu);
  1689. goto bail;
  1690. }
  1691. }
  1692. }
  1693. /* If not done above, attach priority 0 escalation */
  1694. r = kvmppc_xive_attach_escalation(vcpu, 0, kvmppc_xive_has_single_escalation(xive));
  1695. if (r)
  1696. goto bail;
  1697. /* Route the IPI */
  1698. r = xive_native_configure_irq(xc->vp_ipi, xc->vp_id, 0, XICS_IPI);
  1699. if (!r)
  1700. xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_00);
  1701. bail:
  1702. mutex_unlock(&xive->lock);
  1703. if (r) {
  1704. kvmppc_xive_cleanup_vcpu(vcpu);
  1705. return r;
  1706. }
  1707. vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
  1708. return 0;
  1709. }
  1710. /*
  1711. * Scanning of queues before/after migration save
  1712. */
  1713. static void xive_pre_save_set_queued(struct kvmppc_xive *xive, u32 irq)
  1714. {
  1715. struct kvmppc_xive_src_block *sb;
  1716. struct kvmppc_xive_irq_state *state;
  1717. u16 idx;
  1718. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1719. if (!sb)
  1720. return;
  1721. state = &sb->irq_state[idx];
  1722. /* Some sanity checking */
  1723. if (!state->valid) {
  1724. pr_err("invalid irq 0x%x in cpu queue!\n", irq);
  1725. return;
  1726. }
  1727. /*
  1728. * If the interrupt is in a queue it should have P set.
  1729. * We warn so that gets reported. A backtrace isn't useful
  1730. * so no need to use a WARN_ON.
  1731. */
  1732. if (!state->saved_p)
  1733. pr_err("Interrupt 0x%x is marked in a queue but P not set !\n", irq);
  1734. /* Set flag */
  1735. state->in_queue = true;
  1736. }
  1737. static void xive_pre_save_mask_irq(struct kvmppc_xive *xive,
  1738. struct kvmppc_xive_src_block *sb,
  1739. u32 irq)
  1740. {
  1741. struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
  1742. if (!state->valid)
  1743. return;
  1744. /* Mask and save state, this will also sync HW queues */
  1745. state->saved_scan_prio = xive_lock_and_mask(xive, sb, state);
  1746. /* Transfer P and Q */
  1747. state->saved_p = state->old_p;
  1748. state->saved_q = state->old_q;
  1749. /* Unlock */
  1750. arch_spin_unlock(&sb->lock);
  1751. }
  1752. static void xive_pre_save_unmask_irq(struct kvmppc_xive *xive,
  1753. struct kvmppc_xive_src_block *sb,
  1754. u32 irq)
  1755. {
  1756. struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
  1757. if (!state->valid)
  1758. return;
  1759. /*
  1760. * Lock / exclude EOI (not technically necessary if the
  1761. * guest isn't running concurrently. If this becomes a
  1762. * performance issue we can probably remove the lock.
  1763. */
  1764. xive_lock_for_unmask(sb, state);
  1765. /* Restore mask/prio if it wasn't masked */
  1766. if (state->saved_scan_prio != MASKED)
  1767. xive_finish_unmask(xive, sb, state, state->saved_scan_prio);
  1768. /* Unlock */
  1769. arch_spin_unlock(&sb->lock);
  1770. }
  1771. static void xive_pre_save_queue(struct kvmppc_xive *xive, struct xive_q *q)
  1772. {
  1773. u32 idx = q->idx;
  1774. u32 toggle = q->toggle;
  1775. u32 irq;
  1776. do {
  1777. irq = __xive_read_eq(q->qpage, q->msk, &idx, &toggle);
  1778. if (irq > XICS_IPI)
  1779. xive_pre_save_set_queued(xive, irq);
  1780. } while(irq);
  1781. }
  1782. static void xive_pre_save_scan(struct kvmppc_xive *xive)
  1783. {
  1784. struct kvm_vcpu *vcpu = NULL;
  1785. unsigned long i;
  1786. int j;
  1787. /*
  1788. * See comment in xive_get_source() about how this
  1789. * work. Collect a stable state for all interrupts
  1790. */
  1791. for (i = 0; i <= xive->max_sbid; i++) {
  1792. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1793. if (!sb)
  1794. continue;
  1795. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1796. xive_pre_save_mask_irq(xive, sb, j);
  1797. }
  1798. /* Then scan the queues and update the "in_queue" flag */
  1799. kvm_for_each_vcpu(i, vcpu, xive->kvm) {
  1800. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1801. if (!xc)
  1802. continue;
  1803. for (j = 0; j < KVMPPC_XIVE_Q_COUNT; j++) {
  1804. if (xc->queues[j].qpage)
  1805. xive_pre_save_queue(xive, &xc->queues[j]);
  1806. }
  1807. }
  1808. /* Finally restore interrupt states */
  1809. for (i = 0; i <= xive->max_sbid; i++) {
  1810. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1811. if (!sb)
  1812. continue;
  1813. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1814. xive_pre_save_unmask_irq(xive, sb, j);
  1815. }
  1816. }
  1817. static void xive_post_save_scan(struct kvmppc_xive *xive)
  1818. {
  1819. u32 i, j;
  1820. /* Clear all the in_queue flags */
  1821. for (i = 0; i <= xive->max_sbid; i++) {
  1822. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1823. if (!sb)
  1824. continue;
  1825. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1826. sb->irq_state[j].in_queue = false;
  1827. }
  1828. /* Next get_source() will do a new scan */
  1829. xive->saved_src_count = 0;
  1830. }
  1831. /*
  1832. * This returns the source configuration and state to user space.
  1833. */
  1834. static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr)
  1835. {
  1836. struct kvmppc_xive_src_block *sb;
  1837. struct kvmppc_xive_irq_state *state;
  1838. u64 __user *ubufp = (u64 __user *) addr;
  1839. u64 val, prio;
  1840. u16 idx;
  1841. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1842. if (!sb)
  1843. return -ENOENT;
  1844. state = &sb->irq_state[idx];
  1845. if (!state->valid)
  1846. return -ENOENT;
  1847. pr_devel("get_source(%ld)...\n", irq);
  1848. /*
  1849. * So to properly save the state into something that looks like a
  1850. * XICS migration stream we cannot treat interrupts individually.
  1851. *
  1852. * We need, instead, mask them all (& save their previous PQ state)
  1853. * to get a stable state in the HW, then sync them to ensure that
  1854. * any interrupt that had already fired hits its queue, and finally
  1855. * scan all the queues to collect which interrupts are still present
  1856. * in the queues, so we can set the "pending" flag on them and
  1857. * they can be resent on restore.
  1858. *
  1859. * So we do it all when the "first" interrupt gets saved, all the
  1860. * state is collected at that point, the rest of xive_get_source()
  1861. * will merely collect and convert that state to the expected
  1862. * userspace bit mask.
  1863. */
  1864. if (xive->saved_src_count == 0)
  1865. xive_pre_save_scan(xive);
  1866. xive->saved_src_count++;
  1867. /* Convert saved state into something compatible with xics */
  1868. val = state->act_server;
  1869. prio = state->saved_scan_prio;
  1870. if (prio == MASKED) {
  1871. val |= KVM_XICS_MASKED;
  1872. prio = state->saved_priority;
  1873. }
  1874. val |= prio << KVM_XICS_PRIORITY_SHIFT;
  1875. if (state->lsi) {
  1876. val |= KVM_XICS_LEVEL_SENSITIVE;
  1877. if (state->saved_p)
  1878. val |= KVM_XICS_PENDING;
  1879. } else {
  1880. if (state->saved_p)
  1881. val |= KVM_XICS_PRESENTED;
  1882. if (state->saved_q)
  1883. val |= KVM_XICS_QUEUED;
  1884. /*
  1885. * We mark it pending (which will attempt a re-delivery)
  1886. * if we are in a queue *or* we were masked and had
  1887. * Q set which is equivalent to the XICS "masked pending"
  1888. * state
  1889. */
  1890. if (state->in_queue || (prio == MASKED && state->saved_q))
  1891. val |= KVM_XICS_PENDING;
  1892. }
  1893. /*
  1894. * If that was the last interrupt saved, reset the
  1895. * in_queue flags
  1896. */
  1897. if (xive->saved_src_count == xive->src_count)
  1898. xive_post_save_scan(xive);
  1899. /* Copy the result to userspace */
  1900. if (put_user(val, ubufp))
  1901. return -EFAULT;
  1902. return 0;
  1903. }
  1904. struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(
  1905. struct kvmppc_xive *xive, int irq)
  1906. {
  1907. struct kvmppc_xive_src_block *sb;
  1908. int i, bid;
  1909. bid = irq >> KVMPPC_XICS_ICS_SHIFT;
  1910. mutex_lock(&xive->lock);
  1911. /* block already exists - somebody else got here first */
  1912. if (xive->src_blocks[bid])
  1913. goto out;
  1914. /* Create the ICS */
  1915. sb = kzalloc(sizeof(*sb), GFP_KERNEL);
  1916. if (!sb)
  1917. goto out;
  1918. sb->id = bid;
  1919. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  1920. sb->irq_state[i].number = (bid << KVMPPC_XICS_ICS_SHIFT) | i;
  1921. sb->irq_state[i].eisn = 0;
  1922. sb->irq_state[i].guest_priority = MASKED;
  1923. sb->irq_state[i].saved_priority = MASKED;
  1924. sb->irq_state[i].act_priority = MASKED;
  1925. }
  1926. smp_wmb();
  1927. xive->src_blocks[bid] = sb;
  1928. if (bid > xive->max_sbid)
  1929. xive->max_sbid = bid;
  1930. out:
  1931. mutex_unlock(&xive->lock);
  1932. return xive->src_blocks[bid];
  1933. }
  1934. static bool xive_check_delayed_irq(struct kvmppc_xive *xive, u32 irq)
  1935. {
  1936. struct kvm *kvm = xive->kvm;
  1937. struct kvm_vcpu *vcpu = NULL;
  1938. unsigned long i;
  1939. kvm_for_each_vcpu(i, vcpu, kvm) {
  1940. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1941. if (!xc)
  1942. continue;
  1943. if (xc->delayed_irq == irq) {
  1944. xc->delayed_irq = 0;
  1945. xive->delayed_irqs--;
  1946. return true;
  1947. }
  1948. }
  1949. return false;
  1950. }
  1951. static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr)
  1952. {
  1953. struct kvmppc_xive_src_block *sb;
  1954. struct kvmppc_xive_irq_state *state;
  1955. u64 __user *ubufp = (u64 __user *) addr;
  1956. u16 idx;
  1957. u64 val;
  1958. u8 act_prio, guest_prio;
  1959. u32 server;
  1960. int rc = 0;
  1961. if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
  1962. return -ENOENT;
  1963. pr_devel("set_source(irq=0x%lx)\n", irq);
  1964. /* Find the source */
  1965. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1966. if (!sb) {
  1967. pr_devel("No source, creating source block...\n");
  1968. sb = kvmppc_xive_create_src_block(xive, irq);
  1969. if (!sb) {
  1970. pr_devel("Failed to create block...\n");
  1971. return -ENOMEM;
  1972. }
  1973. }
  1974. state = &sb->irq_state[idx];
  1975. /* Read user passed data */
  1976. if (get_user(val, ubufp)) {
  1977. pr_devel("fault getting user info !\n");
  1978. return -EFAULT;
  1979. }
  1980. server = val & KVM_XICS_DESTINATION_MASK;
  1981. guest_prio = val >> KVM_XICS_PRIORITY_SHIFT;
  1982. pr_devel(" val=0x016%llx (server=0x%x, guest_prio=%d)\n",
  1983. val, server, guest_prio);
  1984. /*
  1985. * If the source doesn't already have an IPI, allocate
  1986. * one and get the corresponding data
  1987. */
  1988. if (!state->ipi_number) {
  1989. state->ipi_number = xive_native_alloc_irq();
  1990. if (state->ipi_number == 0) {
  1991. pr_devel("Failed to allocate IPI !\n");
  1992. return -ENOMEM;
  1993. }
  1994. xive_native_populate_irq_data(state->ipi_number, &state->ipi_data);
  1995. pr_devel(" src_ipi=0x%x\n", state->ipi_number);
  1996. }
  1997. /*
  1998. * We use lock_and_mask() to set us in the right masked
  1999. * state. We will override that state from the saved state
  2000. * further down, but this will handle the cases of interrupts
  2001. * that need FW masking. We set the initial guest_priority to
  2002. * 0 before calling it to ensure it actually performs the masking.
  2003. */
  2004. state->guest_priority = 0;
  2005. xive_lock_and_mask(xive, sb, state);
  2006. /*
  2007. * Now, we select a target if we have one. If we don't we
  2008. * leave the interrupt untargetted. It means that an interrupt
  2009. * can become "untargetted" across migration if it was masked
  2010. * by set_xive() but there is little we can do about it.
  2011. */
  2012. /* First convert prio and mark interrupt as untargetted */
  2013. act_prio = xive_prio_from_guest(guest_prio);
  2014. state->act_priority = MASKED;
  2015. /*
  2016. * We need to drop the lock due to the mutex below. Hopefully
  2017. * nothing is touching that interrupt yet since it hasn't been
  2018. * advertized to a running guest yet
  2019. */
  2020. arch_spin_unlock(&sb->lock);
  2021. /* If we have a priority target the interrupt */
  2022. if (act_prio != MASKED) {
  2023. /* First, check provisioning of queues */
  2024. mutex_lock(&xive->lock);
  2025. rc = xive_check_provisioning(xive->kvm, act_prio);
  2026. mutex_unlock(&xive->lock);
  2027. /* Target interrupt */
  2028. if (rc == 0)
  2029. rc = xive_target_interrupt(xive->kvm, state,
  2030. server, act_prio);
  2031. /*
  2032. * If provisioning or targetting failed, leave it
  2033. * alone and masked. It will remain disabled until
  2034. * the guest re-targets it.
  2035. */
  2036. }
  2037. /*
  2038. * Find out if this was a delayed irq stashed in an ICP,
  2039. * in which case, treat it as pending
  2040. */
  2041. if (xive->delayed_irqs && xive_check_delayed_irq(xive, irq)) {
  2042. val |= KVM_XICS_PENDING;
  2043. pr_devel(" Found delayed ! forcing PENDING !\n");
  2044. }
  2045. /* Cleanup the SW state */
  2046. state->old_p = false;
  2047. state->old_q = false;
  2048. state->lsi = false;
  2049. state->asserted = false;
  2050. /* Restore LSI state */
  2051. if (val & KVM_XICS_LEVEL_SENSITIVE) {
  2052. state->lsi = true;
  2053. if (val & KVM_XICS_PENDING)
  2054. state->asserted = true;
  2055. pr_devel(" LSI ! Asserted=%d\n", state->asserted);
  2056. }
  2057. /*
  2058. * Restore P and Q. If the interrupt was pending, we
  2059. * force Q and !P, which will trigger a resend.
  2060. *
  2061. * That means that a guest that had both an interrupt
  2062. * pending (queued) and Q set will restore with only
  2063. * one instance of that interrupt instead of 2, but that
  2064. * is perfectly fine as coalescing interrupts that haven't
  2065. * been presented yet is always allowed.
  2066. */
  2067. if (val & KVM_XICS_PRESENTED && !(val & KVM_XICS_PENDING))
  2068. state->old_p = true;
  2069. if (val & KVM_XICS_QUEUED || val & KVM_XICS_PENDING)
  2070. state->old_q = true;
  2071. pr_devel(" P=%d, Q=%d\n", state->old_p, state->old_q);
  2072. /*
  2073. * If the interrupt was unmasked, update guest priority and
  2074. * perform the appropriate state transition and do a
  2075. * re-trigger if necessary.
  2076. */
  2077. if (val & KVM_XICS_MASKED) {
  2078. pr_devel(" masked, saving prio\n");
  2079. state->guest_priority = MASKED;
  2080. state->saved_priority = guest_prio;
  2081. } else {
  2082. pr_devel(" unmasked, restoring to prio %d\n", guest_prio);
  2083. xive_finish_unmask(xive, sb, state, guest_prio);
  2084. state->saved_priority = guest_prio;
  2085. }
  2086. /* Increment the number of valid sources and mark this one valid */
  2087. if (!state->valid)
  2088. xive->src_count++;
  2089. state->valid = true;
  2090. return 0;
  2091. }
  2092. int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  2093. bool line_status)
  2094. {
  2095. struct kvmppc_xive *xive = kvm->arch.xive;
  2096. struct kvmppc_xive_src_block *sb;
  2097. struct kvmppc_xive_irq_state *state;
  2098. u16 idx;
  2099. if (!xive)
  2100. return -ENODEV;
  2101. sb = kvmppc_xive_find_source(xive, irq, &idx);
  2102. if (!sb)
  2103. return -EINVAL;
  2104. /* Perform locklessly .... (we need to do some RCUisms here...) */
  2105. state = &sb->irq_state[idx];
  2106. if (!state->valid)
  2107. return -EINVAL;
  2108. /* We don't allow a trigger on a passed-through interrupt */
  2109. if (state->pt_number)
  2110. return -EINVAL;
  2111. if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL)
  2112. state->asserted = true;
  2113. else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
  2114. state->asserted = false;
  2115. return 0;
  2116. }
  2117. /* Trigger the IPI */
  2118. xive_irq_trigger(&state->ipi_data);
  2119. return 0;
  2120. }
  2121. int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr)
  2122. {
  2123. u32 __user *ubufp = (u32 __user *) addr;
  2124. u32 nr_servers;
  2125. int rc = 0;
  2126. if (get_user(nr_servers, ubufp))
  2127. return -EFAULT;
  2128. pr_devel("%s nr_servers=%u\n", __func__, nr_servers);
  2129. if (!nr_servers || nr_servers > KVM_MAX_VCPU_IDS)
  2130. return -EINVAL;
  2131. mutex_lock(&xive->lock);
  2132. if (xive->vp_base != XIVE_INVALID_VP)
  2133. /* The VP block is allocated once and freed when the device
  2134. * is released. Better not allow to change its size since its
  2135. * used by connect_vcpu to validate vCPU ids are valid (eg,
  2136. * setting it back to a higher value could allow connect_vcpu
  2137. * to come up with a VP id that goes beyond the VP block, which
  2138. * is likely to cause a crash in OPAL).
  2139. */
  2140. rc = -EBUSY;
  2141. else if (nr_servers > KVM_MAX_VCPUS)
  2142. /* We don't need more servers. Higher vCPU ids get packed
  2143. * down below KVM_MAX_VCPUS by kvmppc_pack_vcpu_id().
  2144. */
  2145. xive->nr_servers = KVM_MAX_VCPUS;
  2146. else
  2147. xive->nr_servers = nr_servers;
  2148. mutex_unlock(&xive->lock);
  2149. return rc;
  2150. }
  2151. static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  2152. {
  2153. struct kvmppc_xive *xive = dev->private;
  2154. /* We honor the existing XICS ioctl */
  2155. switch (attr->group) {
  2156. case KVM_DEV_XICS_GRP_SOURCES:
  2157. return xive_set_source(xive, attr->attr, attr->addr);
  2158. case KVM_DEV_XICS_GRP_CTRL:
  2159. switch (attr->attr) {
  2160. case KVM_DEV_XICS_NR_SERVERS:
  2161. return kvmppc_xive_set_nr_servers(xive, attr->addr);
  2162. }
  2163. }
  2164. return -ENXIO;
  2165. }
  2166. static int xive_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  2167. {
  2168. struct kvmppc_xive *xive = dev->private;
  2169. /* We honor the existing XICS ioctl */
  2170. switch (attr->group) {
  2171. case KVM_DEV_XICS_GRP_SOURCES:
  2172. return xive_get_source(xive, attr->attr, attr->addr);
  2173. }
  2174. return -ENXIO;
  2175. }
  2176. static int xive_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  2177. {
  2178. /* We honor the same limits as XICS, at least for now */
  2179. switch (attr->group) {
  2180. case KVM_DEV_XICS_GRP_SOURCES:
  2181. if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
  2182. attr->attr < KVMPPC_XICS_NR_IRQS)
  2183. return 0;
  2184. break;
  2185. case KVM_DEV_XICS_GRP_CTRL:
  2186. switch (attr->attr) {
  2187. case KVM_DEV_XICS_NR_SERVERS:
  2188. return 0;
  2189. }
  2190. }
  2191. return -ENXIO;
  2192. }
  2193. static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd)
  2194. {
  2195. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
  2196. xive_native_configure_irq(hw_num, 0, MASKED, 0);
  2197. }
  2198. void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb)
  2199. {
  2200. int i;
  2201. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  2202. struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
  2203. if (!state->valid)
  2204. continue;
  2205. kvmppc_xive_cleanup_irq(state->ipi_number, &state->ipi_data);
  2206. xive_cleanup_irq_data(&state->ipi_data);
  2207. xive_native_free_irq(state->ipi_number);
  2208. /* Pass-through, cleanup too but keep IRQ hw data */
  2209. if (state->pt_number)
  2210. kvmppc_xive_cleanup_irq(state->pt_number, state->pt_data);
  2211. state->valid = false;
  2212. }
  2213. }
  2214. /*
  2215. * Called when device fd is closed. kvm->lock is held.
  2216. */
  2217. static void kvmppc_xive_release(struct kvm_device *dev)
  2218. {
  2219. struct kvmppc_xive *xive = dev->private;
  2220. struct kvm *kvm = xive->kvm;
  2221. struct kvm_vcpu *vcpu;
  2222. unsigned long i;
  2223. pr_devel("Releasing xive device\n");
  2224. /*
  2225. * Since this is the device release function, we know that
  2226. * userspace does not have any open fd referring to the
  2227. * device. Therefore there can not be any of the device
  2228. * attribute set/get functions being executed concurrently,
  2229. * and similarly, the connect_vcpu and set/clr_mapped
  2230. * functions also cannot be being executed.
  2231. */
  2232. debugfs_remove(xive->dentry);
  2233. /*
  2234. * We should clean up the vCPU interrupt presenters first.
  2235. */
  2236. kvm_for_each_vcpu(i, vcpu, kvm) {
  2237. /*
  2238. * Take vcpu->mutex to ensure that no one_reg get/set ioctl
  2239. * (i.e. kvmppc_xive_[gs]et_icp) can be done concurrently.
  2240. * Holding the vcpu->mutex also means that the vcpu cannot
  2241. * be executing the KVM_RUN ioctl, and therefore it cannot
  2242. * be executing the XIVE push or pull code or accessing
  2243. * the XIVE MMIO regions.
  2244. */
  2245. mutex_lock(&vcpu->mutex);
  2246. kvmppc_xive_cleanup_vcpu(vcpu);
  2247. mutex_unlock(&vcpu->mutex);
  2248. }
  2249. /*
  2250. * Now that we have cleared vcpu->arch.xive_vcpu, vcpu->arch.irq_type
  2251. * and vcpu->arch.xive_esc_[vr]addr on each vcpu, we are safe
  2252. * against xive code getting called during vcpu execution or
  2253. * set/get one_reg operations.
  2254. */
  2255. kvm->arch.xive = NULL;
  2256. /* Mask and free interrupts */
  2257. for (i = 0; i <= xive->max_sbid; i++) {
  2258. if (xive->src_blocks[i])
  2259. kvmppc_xive_free_sources(xive->src_blocks[i]);
  2260. kfree(xive->src_blocks[i]);
  2261. xive->src_blocks[i] = NULL;
  2262. }
  2263. if (xive->vp_base != XIVE_INVALID_VP)
  2264. xive_native_free_vp_block(xive->vp_base);
  2265. /*
  2266. * A reference of the kvmppc_xive pointer is now kept under
  2267. * the xive_devices struct of the machine for reuse. It is
  2268. * freed when the VM is destroyed for now until we fix all the
  2269. * execution paths.
  2270. */
  2271. kfree(dev);
  2272. }
  2273. /*
  2274. * When the guest chooses the interrupt mode (XICS legacy or XIVE
  2275. * native), the VM will switch of KVM device. The previous device will
  2276. * be "released" before the new one is created.
  2277. *
  2278. * Until we are sure all execution paths are well protected, provide a
  2279. * fail safe (transitional) method for device destruction, in which
  2280. * the XIVE device pointer is recycled and not directly freed.
  2281. */
  2282. struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type)
  2283. {
  2284. struct kvmppc_xive **kvm_xive_device = type == KVM_DEV_TYPE_XIVE ?
  2285. &kvm->arch.xive_devices.native :
  2286. &kvm->arch.xive_devices.xics_on_xive;
  2287. struct kvmppc_xive *xive = *kvm_xive_device;
  2288. if (!xive) {
  2289. xive = kzalloc(sizeof(*xive), GFP_KERNEL);
  2290. *kvm_xive_device = xive;
  2291. } else {
  2292. memset(xive, 0, sizeof(*xive));
  2293. }
  2294. return xive;
  2295. }
  2296. /*
  2297. * Create a XICS device with XIVE backend. kvm->lock is held.
  2298. */
  2299. static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
  2300. {
  2301. struct kvmppc_xive *xive;
  2302. struct kvm *kvm = dev->kvm;
  2303. pr_devel("Creating xive for partition\n");
  2304. /* Already there ? */
  2305. if (kvm->arch.xive)
  2306. return -EEXIST;
  2307. xive = kvmppc_xive_get_device(kvm, type);
  2308. if (!xive)
  2309. return -ENOMEM;
  2310. dev->private = xive;
  2311. xive->dev = dev;
  2312. xive->kvm = kvm;
  2313. mutex_init(&xive->lock);
  2314. /* We use the default queue size set by the host */
  2315. xive->q_order = xive_native_default_eq_shift();
  2316. if (xive->q_order < PAGE_SHIFT)
  2317. xive->q_page_order = 0;
  2318. else
  2319. xive->q_page_order = xive->q_order - PAGE_SHIFT;
  2320. /* VP allocation is delayed to the first call to connect_vcpu */
  2321. xive->vp_base = XIVE_INVALID_VP;
  2322. /* KVM_MAX_VCPUS limits the number of VMs to roughly 64 per sockets
  2323. * on a POWER9 system.
  2324. */
  2325. xive->nr_servers = KVM_MAX_VCPUS;
  2326. if (xive_native_has_single_escalation())
  2327. xive->flags |= KVMPPC_XIVE_FLAG_SINGLE_ESCALATION;
  2328. if (xive_native_has_save_restore())
  2329. xive->flags |= KVMPPC_XIVE_FLAG_SAVE_RESTORE;
  2330. kvm->arch.xive = xive;
  2331. return 0;
  2332. }
  2333. int kvmppc_xive_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
  2334. {
  2335. /* The VM should have configured XICS mode before doing XICS hcalls. */
  2336. if (!kvmppc_xics_enabled(vcpu))
  2337. return H_TOO_HARD;
  2338. switch (req) {
  2339. case H_XIRR:
  2340. return xive_vm_h_xirr(vcpu);
  2341. case H_CPPR:
  2342. return xive_vm_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
  2343. case H_EOI:
  2344. return xive_vm_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
  2345. case H_IPI:
  2346. return xive_vm_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
  2347. kvmppc_get_gpr(vcpu, 5));
  2348. case H_IPOLL:
  2349. return xive_vm_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
  2350. case H_XIRR_X:
  2351. xive_vm_h_xirr(vcpu);
  2352. kvmppc_set_gpr(vcpu, 5, get_tb() + kvmppc_get_tb_offset(vcpu));
  2353. return H_SUCCESS;
  2354. }
  2355. return H_UNSUPPORTED;
  2356. }
  2357. EXPORT_SYMBOL_GPL(kvmppc_xive_xics_hcall);
  2358. int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu)
  2359. {
  2360. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  2361. unsigned int i;
  2362. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  2363. struct xive_q *q = &xc->queues[i];
  2364. u32 i0, i1, idx;
  2365. if (!q->qpage && !xc->esc_virq[i])
  2366. continue;
  2367. if (q->qpage) {
  2368. seq_printf(m, " q[%d]: ", i);
  2369. idx = q->idx;
  2370. i0 = be32_to_cpup(q->qpage + idx);
  2371. idx = (idx + 1) & q->msk;
  2372. i1 = be32_to_cpup(q->qpage + idx);
  2373. seq_printf(m, "T=%d %08x %08x...\n", q->toggle,
  2374. i0, i1);
  2375. }
  2376. if (xc->esc_virq[i]) {
  2377. struct irq_data *d = irq_get_irq_data(xc->esc_virq[i]);
  2378. struct xive_irq_data *xd =
  2379. irq_data_get_irq_handler_data(d);
  2380. u64 pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
  2381. seq_printf(m, " ESC %d %c%c EOI @%llx",
  2382. xc->esc_virq[i],
  2383. (pq & XIVE_ESB_VAL_P) ? 'P' : '-',
  2384. (pq & XIVE_ESB_VAL_Q) ? 'Q' : '-',
  2385. xd->eoi_page);
  2386. seq_puts(m, "\n");
  2387. }
  2388. }
  2389. return 0;
  2390. }
  2391. void kvmppc_xive_debug_show_sources(struct seq_file *m,
  2392. struct kvmppc_xive_src_block *sb)
  2393. {
  2394. int i;
  2395. seq_puts(m, " LISN HW/CHIP TYPE PQ EISN CPU/PRIO\n");
  2396. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  2397. struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
  2398. struct xive_irq_data *xd;
  2399. u64 pq;
  2400. u32 hw_num;
  2401. if (!state->valid)
  2402. continue;
  2403. kvmppc_xive_select_irq(state, &hw_num, &xd);
  2404. pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
  2405. seq_printf(m, "%08x %08x/%02x", state->number, hw_num,
  2406. xd->src_chip);
  2407. if (state->lsi)
  2408. seq_printf(m, " %cLSI", state->asserted ? '^' : ' ');
  2409. else
  2410. seq_puts(m, " MSI");
  2411. seq_printf(m, " %s %c%c %08x % 4d/%d",
  2412. state->ipi_number == hw_num ? "IPI" : " PT",
  2413. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  2414. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  2415. state->eisn, state->act_server,
  2416. state->act_priority);
  2417. seq_puts(m, "\n");
  2418. }
  2419. }
  2420. static int xive_debug_show(struct seq_file *m, void *private)
  2421. {
  2422. struct kvmppc_xive *xive = m->private;
  2423. struct kvm *kvm = xive->kvm;
  2424. struct kvm_vcpu *vcpu;
  2425. u64 t_rm_h_xirr = 0;
  2426. u64 t_rm_h_ipoll = 0;
  2427. u64 t_rm_h_cppr = 0;
  2428. u64 t_rm_h_eoi = 0;
  2429. u64 t_rm_h_ipi = 0;
  2430. u64 t_vm_h_xirr = 0;
  2431. u64 t_vm_h_ipoll = 0;
  2432. u64 t_vm_h_cppr = 0;
  2433. u64 t_vm_h_eoi = 0;
  2434. u64 t_vm_h_ipi = 0;
  2435. unsigned long i;
  2436. if (!kvm)
  2437. return 0;
  2438. seq_puts(m, "=========\nVCPU state\n=========\n");
  2439. kvm_for_each_vcpu(i, vcpu, kvm) {
  2440. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  2441. if (!xc)
  2442. continue;
  2443. seq_printf(m, "VCPU %d: VP:%#x/%02x\n"
  2444. " CPPR:%#x HWCPPR:%#x MFRR:%#x PEND:%#x h_xirr: R=%lld V=%lld\n",
  2445. xc->server_num, xc->vp_id, xc->vp_chip_id,
  2446. xc->cppr, xc->hw_cppr,
  2447. xc->mfrr, xc->pending,
  2448. xc->stat_rm_h_xirr, xc->stat_vm_h_xirr);
  2449. kvmppc_xive_debug_show_queues(m, vcpu);
  2450. t_rm_h_xirr += xc->stat_rm_h_xirr;
  2451. t_rm_h_ipoll += xc->stat_rm_h_ipoll;
  2452. t_rm_h_cppr += xc->stat_rm_h_cppr;
  2453. t_rm_h_eoi += xc->stat_rm_h_eoi;
  2454. t_rm_h_ipi += xc->stat_rm_h_ipi;
  2455. t_vm_h_xirr += xc->stat_vm_h_xirr;
  2456. t_vm_h_ipoll += xc->stat_vm_h_ipoll;
  2457. t_vm_h_cppr += xc->stat_vm_h_cppr;
  2458. t_vm_h_eoi += xc->stat_vm_h_eoi;
  2459. t_vm_h_ipi += xc->stat_vm_h_ipi;
  2460. }
  2461. seq_puts(m, "Hcalls totals\n");
  2462. seq_printf(m, " H_XIRR R=%10lld V=%10lld\n", t_rm_h_xirr, t_vm_h_xirr);
  2463. seq_printf(m, " H_IPOLL R=%10lld V=%10lld\n", t_rm_h_ipoll, t_vm_h_ipoll);
  2464. seq_printf(m, " H_CPPR R=%10lld V=%10lld\n", t_rm_h_cppr, t_vm_h_cppr);
  2465. seq_printf(m, " H_EOI R=%10lld V=%10lld\n", t_rm_h_eoi, t_vm_h_eoi);
  2466. seq_printf(m, " H_IPI R=%10lld V=%10lld\n", t_rm_h_ipi, t_vm_h_ipi);
  2467. seq_puts(m, "=========\nSources\n=========\n");
  2468. for (i = 0; i <= xive->max_sbid; i++) {
  2469. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  2470. if (sb) {
  2471. arch_spin_lock(&sb->lock);
  2472. kvmppc_xive_debug_show_sources(m, sb);
  2473. arch_spin_unlock(&sb->lock);
  2474. }
  2475. }
  2476. return 0;
  2477. }
  2478. DEFINE_SHOW_ATTRIBUTE(xive_debug);
  2479. static void xive_debugfs_init(struct kvmppc_xive *xive)
  2480. {
  2481. xive->dentry = debugfs_create_file("xive", S_IRUGO, xive->kvm->debugfs_dentry,
  2482. xive, &xive_debug_fops);
  2483. pr_debug("%s: created\n", __func__);
  2484. }
  2485. static void kvmppc_xive_init(struct kvm_device *dev)
  2486. {
  2487. struct kvmppc_xive *xive = dev->private;
  2488. /* Register some debug interfaces */
  2489. xive_debugfs_init(xive);
  2490. }
  2491. struct kvm_device_ops kvm_xive_ops = {
  2492. .name = "kvm-xive",
  2493. .create = kvmppc_xive_create,
  2494. .init = kvmppc_xive_init,
  2495. .release = kvmppc_xive_release,
  2496. .set_attr = xive_set_attr,
  2497. .get_attr = xive_get_attr,
  2498. .has_attr = xive_has_attr,
  2499. };