book3s_xive.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. /*
  2. * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation.
  7. */
  8. #define pr_fmt(fmt) "xive-kvm: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/err.h>
  12. #include <linux/gfp.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/delay.h>
  15. #include <linux/percpu.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/kvm_book3s.h>
  19. #include <asm/kvm_ppc.h>
  20. #include <asm/hvcall.h>
  21. #include <asm/xics.h>
  22. #include <asm/xive.h>
  23. #include <asm/xive-regs.h>
  24. #include <asm/debug.h>
  25. #include <asm/debugfs.h>
  26. #include <asm/time.h>
  27. #include <asm/opal.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/seq_file.h>
  30. #include "book3s_xive.h"
  31. /*
  32. * Virtual mode variants of the hcalls for use on radix/radix
  33. * with AIL. They require the VCPU's VP to be "pushed"
  34. *
  35. * We still instanciate them here because we use some of the
  36. * generated utility functions as well in this file.
  37. */
  38. #define XIVE_RUNTIME_CHECKS
  39. #define X_PFX xive_vm_
  40. #define X_STATIC static
  41. #define X_STAT_PFX stat_vm_
  42. #define __x_tima xive_tima
  43. #define __x_eoi_page(xd) ((void __iomem *)((xd)->eoi_mmio))
  44. #define __x_trig_page(xd) ((void __iomem *)((xd)->trig_mmio))
  45. #define __x_writeb __raw_writeb
  46. #define __x_readw __raw_readw
  47. #define __x_readq __raw_readq
  48. #define __x_writeq __raw_writeq
  49. #include "book3s_xive_template.c"
  50. /*
  51. * We leave a gap of a couple of interrupts in the queue to
  52. * account for the IPI and additional safety guard.
  53. */
  54. #define XIVE_Q_GAP 2
  55. /*
  56. * This is a simple trigger for a generic XIVE IRQ. This must
  57. * only be called for interrupts that support a trigger page
  58. */
  59. static bool xive_irq_trigger(struct xive_irq_data *xd)
  60. {
  61. /* This should be only for MSIs */
  62. if (WARN_ON(xd->flags & XIVE_IRQ_FLAG_LSI))
  63. return false;
  64. /* Those interrupts should always have a trigger page */
  65. if (WARN_ON(!xd->trig_mmio))
  66. return false;
  67. out_be64(xd->trig_mmio, 0);
  68. return true;
  69. }
  70. static irqreturn_t xive_esc_irq(int irq, void *data)
  71. {
  72. struct kvm_vcpu *vcpu = data;
  73. vcpu->arch.irq_pending = 1;
  74. smp_mb();
  75. if (vcpu->arch.ceded)
  76. kvmppc_fast_vcpu_kick(vcpu);
  77. /* Since we have the no-EOI flag, the interrupt is effectively
  78. * disabled now. Clearing xive_esc_on means we won't bother
  79. * doing so on the next entry.
  80. *
  81. * This also allows the entry code to know that if a PQ combination
  82. * of 10 is observed while xive_esc_on is true, it means the queue
  83. * contains an unprocessed escalation interrupt. We don't make use of
  84. * that knowledge today but might (see comment in book3s_hv_rmhandler.S)
  85. */
  86. vcpu->arch.xive_esc_on = false;
  87. return IRQ_HANDLED;
  88. }
  89. static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)
  90. {
  91. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  92. struct xive_q *q = &xc->queues[prio];
  93. char *name = NULL;
  94. int rc;
  95. /* Already there ? */
  96. if (xc->esc_virq[prio])
  97. return 0;
  98. /* Hook up the escalation interrupt */
  99. xc->esc_virq[prio] = irq_create_mapping(NULL, q->esc_irq);
  100. if (!xc->esc_virq[prio]) {
  101. pr_err("Failed to map escalation interrupt for queue %d of VCPU %d\n",
  102. prio, xc->server_num);
  103. return -EIO;
  104. }
  105. if (xc->xive->single_escalation)
  106. name = kasprintf(GFP_KERNEL, "kvm-%d-%d",
  107. vcpu->kvm->arch.lpid, xc->server_num);
  108. else
  109. name = kasprintf(GFP_KERNEL, "kvm-%d-%d-%d",
  110. vcpu->kvm->arch.lpid, xc->server_num, prio);
  111. if (!name) {
  112. pr_err("Failed to allocate escalation irq name for queue %d of VCPU %d\n",
  113. prio, xc->server_num);
  114. rc = -ENOMEM;
  115. goto error;
  116. }
  117. pr_devel("Escalation %s irq %d (prio %d)\n", name, xc->esc_virq[prio], prio);
  118. rc = request_irq(xc->esc_virq[prio], xive_esc_irq,
  119. IRQF_NO_THREAD, name, vcpu);
  120. if (rc) {
  121. pr_err("Failed to request escalation interrupt for queue %d of VCPU %d\n",
  122. prio, xc->server_num);
  123. goto error;
  124. }
  125. xc->esc_virq_names[prio] = name;
  126. /* In single escalation mode, we grab the ESB MMIO of the
  127. * interrupt and mask it. Also populate the VCPU v/raddr
  128. * of the ESB page for use by asm entry/exit code. Finally
  129. * set the XIVE_IRQ_NO_EOI flag which will prevent the
  130. * core code from performing an EOI on the escalation
  131. * interrupt, thus leaving it effectively masked after
  132. * it fires once.
  133. */
  134. if (xc->xive->single_escalation) {
  135. struct irq_data *d = irq_get_irq_data(xc->esc_virq[prio]);
  136. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  137. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
  138. vcpu->arch.xive_esc_raddr = xd->eoi_page;
  139. vcpu->arch.xive_esc_vaddr = (__force u64)xd->eoi_mmio;
  140. xd->flags |= XIVE_IRQ_NO_EOI;
  141. }
  142. return 0;
  143. error:
  144. irq_dispose_mapping(xc->esc_virq[prio]);
  145. xc->esc_virq[prio] = 0;
  146. kfree(name);
  147. return rc;
  148. }
  149. static int xive_provision_queue(struct kvm_vcpu *vcpu, u8 prio)
  150. {
  151. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  152. struct kvmppc_xive *xive = xc->xive;
  153. struct xive_q *q = &xc->queues[prio];
  154. void *qpage;
  155. int rc;
  156. if (WARN_ON(q->qpage))
  157. return 0;
  158. /* Allocate the queue and retrieve infos on current node for now */
  159. qpage = (__be32 *)__get_free_pages(GFP_KERNEL, xive->q_page_order);
  160. if (!qpage) {
  161. pr_err("Failed to allocate queue %d for VCPU %d\n",
  162. prio, xc->server_num);
  163. return -ENOMEM;
  164. }
  165. memset(qpage, 0, 1 << xive->q_order);
  166. /*
  167. * Reconfigure the queue. This will set q->qpage only once the
  168. * queue is fully configured. This is a requirement for prio 0
  169. * as we will stop doing EOIs for every IPI as soon as we observe
  170. * qpage being non-NULL, and instead will only EOI when we receive
  171. * corresponding queue 0 entries
  172. */
  173. rc = xive_native_configure_queue(xc->vp_id, q, prio, qpage,
  174. xive->q_order, true);
  175. if (rc)
  176. pr_err("Failed to configure queue %d for VCPU %d\n",
  177. prio, xc->server_num);
  178. return rc;
  179. }
  180. /* Called with kvm_lock held */
  181. static int xive_check_provisioning(struct kvm *kvm, u8 prio)
  182. {
  183. struct kvmppc_xive *xive = kvm->arch.xive;
  184. struct kvm_vcpu *vcpu;
  185. int i, rc;
  186. lockdep_assert_held(&kvm->lock);
  187. /* Already provisioned ? */
  188. if (xive->qmap & (1 << prio))
  189. return 0;
  190. pr_devel("Provisioning prio... %d\n", prio);
  191. /* Provision each VCPU and enable escalations if needed */
  192. kvm_for_each_vcpu(i, vcpu, kvm) {
  193. if (!vcpu->arch.xive_vcpu)
  194. continue;
  195. rc = xive_provision_queue(vcpu, prio);
  196. if (rc == 0 && !xive->single_escalation)
  197. xive_attach_escalation(vcpu, prio);
  198. if (rc)
  199. return rc;
  200. }
  201. /* Order previous stores and mark it as provisioned */
  202. mb();
  203. xive->qmap |= (1 << prio);
  204. return 0;
  205. }
  206. static void xive_inc_q_pending(struct kvm *kvm, u32 server, u8 prio)
  207. {
  208. struct kvm_vcpu *vcpu;
  209. struct kvmppc_xive_vcpu *xc;
  210. struct xive_q *q;
  211. /* Locate target server */
  212. vcpu = kvmppc_xive_find_server(kvm, server);
  213. if (!vcpu) {
  214. pr_warn("%s: Can't find server %d\n", __func__, server);
  215. return;
  216. }
  217. xc = vcpu->arch.xive_vcpu;
  218. if (WARN_ON(!xc))
  219. return;
  220. q = &xc->queues[prio];
  221. atomic_inc(&q->pending_count);
  222. }
  223. static int xive_try_pick_queue(struct kvm_vcpu *vcpu, u8 prio)
  224. {
  225. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  226. struct xive_q *q;
  227. u32 max;
  228. if (WARN_ON(!xc))
  229. return -ENXIO;
  230. if (!xc->valid)
  231. return -ENXIO;
  232. q = &xc->queues[prio];
  233. if (WARN_ON(!q->qpage))
  234. return -ENXIO;
  235. /* Calculate max number of interrupts in that queue. */
  236. max = (q->msk + 1) - XIVE_Q_GAP;
  237. return atomic_add_unless(&q->count, 1, max) ? 0 : -EBUSY;
  238. }
  239. static int xive_select_target(struct kvm *kvm, u32 *server, u8 prio)
  240. {
  241. struct kvm_vcpu *vcpu;
  242. int i, rc;
  243. /* Locate target server */
  244. vcpu = kvmppc_xive_find_server(kvm, *server);
  245. if (!vcpu) {
  246. pr_devel("Can't find server %d\n", *server);
  247. return -EINVAL;
  248. }
  249. pr_devel("Finding irq target on 0x%x/%d...\n", *server, prio);
  250. /* Try pick it */
  251. rc = xive_try_pick_queue(vcpu, prio);
  252. if (rc == 0)
  253. return rc;
  254. pr_devel(" .. failed, looking up candidate...\n");
  255. /* Failed, pick another VCPU */
  256. kvm_for_each_vcpu(i, vcpu, kvm) {
  257. if (!vcpu->arch.xive_vcpu)
  258. continue;
  259. rc = xive_try_pick_queue(vcpu, prio);
  260. if (rc == 0) {
  261. *server = vcpu->arch.xive_vcpu->server_num;
  262. pr_devel(" found on 0x%x/%d\n", *server, prio);
  263. return rc;
  264. }
  265. }
  266. pr_devel(" no available target !\n");
  267. /* No available target ! */
  268. return -EBUSY;
  269. }
  270. static u8 xive_lock_and_mask(struct kvmppc_xive *xive,
  271. struct kvmppc_xive_src_block *sb,
  272. struct kvmppc_xive_irq_state *state)
  273. {
  274. struct xive_irq_data *xd;
  275. u32 hw_num;
  276. u8 old_prio;
  277. u64 val;
  278. /*
  279. * Take the lock, set masked, try again if racing
  280. * with H_EOI
  281. */
  282. for (;;) {
  283. arch_spin_lock(&sb->lock);
  284. old_prio = state->guest_priority;
  285. state->guest_priority = MASKED;
  286. mb();
  287. if (!state->in_eoi)
  288. break;
  289. state->guest_priority = old_prio;
  290. arch_spin_unlock(&sb->lock);
  291. }
  292. /* No change ? Bail */
  293. if (old_prio == MASKED)
  294. return old_prio;
  295. /* Get the right irq */
  296. kvmppc_xive_select_irq(state, &hw_num, &xd);
  297. /*
  298. * If the interrupt is marked as needing masking via
  299. * firmware, we do it here. Firmware masking however
  300. * is "lossy", it won't return the old p and q bits
  301. * and won't set the interrupt to a state where it will
  302. * record queued ones. If this is an issue we should do
  303. * lazy masking instead.
  304. *
  305. * For now, we work around this in unmask by forcing
  306. * an interrupt whenever we unmask a non-LSI via FW
  307. * (if ever).
  308. */
  309. if (xd->flags & OPAL_XIVE_IRQ_MASK_VIA_FW) {
  310. xive_native_configure_irq(hw_num,
  311. xive->vp_base + state->act_server,
  312. MASKED, state->number);
  313. /* set old_p so we can track if an H_EOI was done */
  314. state->old_p = true;
  315. state->old_q = false;
  316. } else {
  317. /* Set PQ to 10, return old P and old Q and remember them */
  318. val = xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_10);
  319. state->old_p = !!(val & 2);
  320. state->old_q = !!(val & 1);
  321. /*
  322. * Synchronize hardware to sensure the queues are updated
  323. * when masking
  324. */
  325. xive_native_sync_source(hw_num);
  326. }
  327. return old_prio;
  328. }
  329. static void xive_lock_for_unmask(struct kvmppc_xive_src_block *sb,
  330. struct kvmppc_xive_irq_state *state)
  331. {
  332. /*
  333. * Take the lock try again if racing with H_EOI
  334. */
  335. for (;;) {
  336. arch_spin_lock(&sb->lock);
  337. if (!state->in_eoi)
  338. break;
  339. arch_spin_unlock(&sb->lock);
  340. }
  341. }
  342. static void xive_finish_unmask(struct kvmppc_xive *xive,
  343. struct kvmppc_xive_src_block *sb,
  344. struct kvmppc_xive_irq_state *state,
  345. u8 prio)
  346. {
  347. struct xive_irq_data *xd;
  348. u32 hw_num;
  349. /* If we aren't changing a thing, move on */
  350. if (state->guest_priority != MASKED)
  351. goto bail;
  352. /* Get the right irq */
  353. kvmppc_xive_select_irq(state, &hw_num, &xd);
  354. /*
  355. * See command in xive_lock_and_mask() concerning masking
  356. * via firmware.
  357. */
  358. if (xd->flags & OPAL_XIVE_IRQ_MASK_VIA_FW) {
  359. xive_native_configure_irq(hw_num,
  360. xive->vp_base + state->act_server,
  361. state->act_priority, state->number);
  362. /* If an EOI is needed, do it here */
  363. if (!state->old_p)
  364. xive_vm_source_eoi(hw_num, xd);
  365. /* If this is not an LSI, force a trigger */
  366. if (!(xd->flags & OPAL_XIVE_IRQ_LSI))
  367. xive_irq_trigger(xd);
  368. goto bail;
  369. }
  370. /* Old Q set, set PQ to 11 */
  371. if (state->old_q)
  372. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_11);
  373. /*
  374. * If not old P, then perform an "effective" EOI,
  375. * on the source. This will handle the cases where
  376. * FW EOI is needed.
  377. */
  378. if (!state->old_p)
  379. xive_vm_source_eoi(hw_num, xd);
  380. /* Synchronize ordering and mark unmasked */
  381. mb();
  382. bail:
  383. state->guest_priority = prio;
  384. }
  385. /*
  386. * Target an interrupt to a given server/prio, this will fallback
  387. * to another server if necessary and perform the HW targetting
  388. * updates as needed
  389. *
  390. * NOTE: Must be called with the state lock held
  391. */
  392. static int xive_target_interrupt(struct kvm *kvm,
  393. struct kvmppc_xive_irq_state *state,
  394. u32 server, u8 prio)
  395. {
  396. struct kvmppc_xive *xive = kvm->arch.xive;
  397. u32 hw_num;
  398. int rc;
  399. /*
  400. * This will return a tentative server and actual
  401. * priority. The count for that new target will have
  402. * already been incremented.
  403. */
  404. rc = xive_select_target(kvm, &server, prio);
  405. /*
  406. * We failed to find a target ? Not much we can do
  407. * at least until we support the GIQ.
  408. */
  409. if (rc)
  410. return rc;
  411. /*
  412. * Increment the old queue pending count if there
  413. * was one so that the old queue count gets adjusted later
  414. * when observed to be empty.
  415. */
  416. if (state->act_priority != MASKED)
  417. xive_inc_q_pending(kvm,
  418. state->act_server,
  419. state->act_priority);
  420. /*
  421. * Update state and HW
  422. */
  423. state->act_priority = prio;
  424. state->act_server = server;
  425. /* Get the right irq */
  426. kvmppc_xive_select_irq(state, &hw_num, NULL);
  427. return xive_native_configure_irq(hw_num,
  428. xive->vp_base + server,
  429. prio, state->number);
  430. }
  431. /*
  432. * Targetting rules: In order to avoid losing track of
  433. * pending interrupts accross mask and unmask, which would
  434. * allow queue overflows, we implement the following rules:
  435. *
  436. * - Unless it was never enabled (or we run out of capacity)
  437. * an interrupt is always targetted at a valid server/queue
  438. * pair even when "masked" by the guest. This pair tends to
  439. * be the last one used but it can be changed under some
  440. * circumstances. That allows us to separate targetting
  441. * from masking, we only handle accounting during (re)targetting,
  442. * this also allows us to let an interrupt drain into its target
  443. * queue after masking, avoiding complex schemes to remove
  444. * interrupts out of remote processor queues.
  445. *
  446. * - When masking, we set PQ to 10 and save the previous value
  447. * of P and Q.
  448. *
  449. * - When unmasking, if saved Q was set, we set PQ to 11
  450. * otherwise we leave PQ to the HW state which will be either
  451. * 10 if nothing happened or 11 if the interrupt fired while
  452. * masked. Effectively we are OR'ing the previous Q into the
  453. * HW Q.
  454. *
  455. * Then if saved P is clear, we do an effective EOI (Q->P->Trigger)
  456. * which will unmask the interrupt and shoot a new one if Q was
  457. * set.
  458. *
  459. * Otherwise (saved P is set) we leave PQ unchanged (so 10 or 11,
  460. * effectively meaning an H_EOI from the guest is still expected
  461. * for that interrupt).
  462. *
  463. * - If H_EOI occurs while masked, we clear the saved P.
  464. *
  465. * - When changing target, we account on the new target and
  466. * increment a separate "pending" counter on the old one.
  467. * This pending counter will be used to decrement the old
  468. * target's count when its queue has been observed empty.
  469. */
  470. int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
  471. u32 priority)
  472. {
  473. struct kvmppc_xive *xive = kvm->arch.xive;
  474. struct kvmppc_xive_src_block *sb;
  475. struct kvmppc_xive_irq_state *state;
  476. u8 new_act_prio;
  477. int rc = 0;
  478. u16 idx;
  479. if (!xive)
  480. return -ENODEV;
  481. pr_devel("set_xive ! irq 0x%x server 0x%x prio %d\n",
  482. irq, server, priority);
  483. /* First, check provisioning of queues */
  484. if (priority != MASKED)
  485. rc = xive_check_provisioning(xive->kvm,
  486. xive_prio_from_guest(priority));
  487. if (rc) {
  488. pr_devel(" provisioning failure %d !\n", rc);
  489. return rc;
  490. }
  491. sb = kvmppc_xive_find_source(xive, irq, &idx);
  492. if (!sb)
  493. return -EINVAL;
  494. state = &sb->irq_state[idx];
  495. /*
  496. * We first handle masking/unmasking since the locking
  497. * might need to be retried due to EOIs, we'll handle
  498. * targetting changes later. These functions will return
  499. * with the SB lock held.
  500. *
  501. * xive_lock_and_mask() will also set state->guest_priority
  502. * but won't otherwise change other fields of the state.
  503. *
  504. * xive_lock_for_unmask will not actually unmask, this will
  505. * be done later by xive_finish_unmask() once the targetting
  506. * has been done, so we don't try to unmask an interrupt
  507. * that hasn't yet been targetted.
  508. */
  509. if (priority == MASKED)
  510. xive_lock_and_mask(xive, sb, state);
  511. else
  512. xive_lock_for_unmask(sb, state);
  513. /*
  514. * Then we handle targetting.
  515. *
  516. * First calculate a new "actual priority"
  517. */
  518. new_act_prio = state->act_priority;
  519. if (priority != MASKED)
  520. new_act_prio = xive_prio_from_guest(priority);
  521. pr_devel(" new_act_prio=%x act_server=%x act_prio=%x\n",
  522. new_act_prio, state->act_server, state->act_priority);
  523. /*
  524. * Then check if we actually need to change anything,
  525. *
  526. * The condition for re-targetting the interrupt is that
  527. * we have a valid new priority (new_act_prio is not 0xff)
  528. * and either the server or the priority changed.
  529. *
  530. * Note: If act_priority was ff and the new priority is
  531. * also ff, we don't do anything and leave the interrupt
  532. * untargetted. An attempt of doing an int_on on an
  533. * untargetted interrupt will fail. If that is a problem
  534. * we could initialize interrupts with valid default
  535. */
  536. if (new_act_prio != MASKED &&
  537. (state->act_server != server ||
  538. state->act_priority != new_act_prio))
  539. rc = xive_target_interrupt(kvm, state, server, new_act_prio);
  540. /*
  541. * Perform the final unmasking of the interrupt source
  542. * if necessary
  543. */
  544. if (priority != MASKED)
  545. xive_finish_unmask(xive, sb, state, priority);
  546. /*
  547. * Finally Update saved_priority to match. Only int_on/off
  548. * set this field to a different value.
  549. */
  550. state->saved_priority = priority;
  551. arch_spin_unlock(&sb->lock);
  552. return rc;
  553. }
  554. int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
  555. u32 *priority)
  556. {
  557. struct kvmppc_xive *xive = kvm->arch.xive;
  558. struct kvmppc_xive_src_block *sb;
  559. struct kvmppc_xive_irq_state *state;
  560. u16 idx;
  561. if (!xive)
  562. return -ENODEV;
  563. sb = kvmppc_xive_find_source(xive, irq, &idx);
  564. if (!sb)
  565. return -EINVAL;
  566. state = &sb->irq_state[idx];
  567. arch_spin_lock(&sb->lock);
  568. *server = state->act_server;
  569. *priority = state->guest_priority;
  570. arch_spin_unlock(&sb->lock);
  571. return 0;
  572. }
  573. int kvmppc_xive_int_on(struct kvm *kvm, u32 irq)
  574. {
  575. struct kvmppc_xive *xive = kvm->arch.xive;
  576. struct kvmppc_xive_src_block *sb;
  577. struct kvmppc_xive_irq_state *state;
  578. u16 idx;
  579. if (!xive)
  580. return -ENODEV;
  581. sb = kvmppc_xive_find_source(xive, irq, &idx);
  582. if (!sb)
  583. return -EINVAL;
  584. state = &sb->irq_state[idx];
  585. pr_devel("int_on(irq=0x%x)\n", irq);
  586. /*
  587. * Check if interrupt was not targetted
  588. */
  589. if (state->act_priority == MASKED) {
  590. pr_devel("int_on on untargetted interrupt\n");
  591. return -EINVAL;
  592. }
  593. /* If saved_priority is 0xff, do nothing */
  594. if (state->saved_priority == MASKED)
  595. return 0;
  596. /*
  597. * Lock and unmask it.
  598. */
  599. xive_lock_for_unmask(sb, state);
  600. xive_finish_unmask(xive, sb, state, state->saved_priority);
  601. arch_spin_unlock(&sb->lock);
  602. return 0;
  603. }
  604. int kvmppc_xive_int_off(struct kvm *kvm, u32 irq)
  605. {
  606. struct kvmppc_xive *xive = kvm->arch.xive;
  607. struct kvmppc_xive_src_block *sb;
  608. struct kvmppc_xive_irq_state *state;
  609. u16 idx;
  610. if (!xive)
  611. return -ENODEV;
  612. sb = kvmppc_xive_find_source(xive, irq, &idx);
  613. if (!sb)
  614. return -EINVAL;
  615. state = &sb->irq_state[idx];
  616. pr_devel("int_off(irq=0x%x)\n", irq);
  617. /*
  618. * Lock and mask
  619. */
  620. state->saved_priority = xive_lock_and_mask(xive, sb, state);
  621. arch_spin_unlock(&sb->lock);
  622. return 0;
  623. }
  624. static bool xive_restore_pending_irq(struct kvmppc_xive *xive, u32 irq)
  625. {
  626. struct kvmppc_xive_src_block *sb;
  627. struct kvmppc_xive_irq_state *state;
  628. u16 idx;
  629. sb = kvmppc_xive_find_source(xive, irq, &idx);
  630. if (!sb)
  631. return false;
  632. state = &sb->irq_state[idx];
  633. if (!state->valid)
  634. return false;
  635. /*
  636. * Trigger the IPI. This assumes we never restore a pass-through
  637. * interrupt which should be safe enough
  638. */
  639. xive_irq_trigger(&state->ipi_data);
  640. return true;
  641. }
  642. u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu)
  643. {
  644. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  645. if (!xc)
  646. return 0;
  647. /* Return the per-cpu state for state saving/migration */
  648. return (u64)xc->cppr << KVM_REG_PPC_ICP_CPPR_SHIFT |
  649. (u64)xc->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT |
  650. (u64)0xff << KVM_REG_PPC_ICP_PPRI_SHIFT;
  651. }
  652. int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
  653. {
  654. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  655. struct kvmppc_xive *xive = vcpu->kvm->arch.xive;
  656. u8 cppr, mfrr;
  657. u32 xisr;
  658. if (!xc || !xive)
  659. return -ENOENT;
  660. /* Grab individual state fields. We don't use pending_pri */
  661. cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
  662. xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
  663. KVM_REG_PPC_ICP_XISR_MASK;
  664. mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
  665. pr_devel("set_icp vcpu %d cppr=0x%x mfrr=0x%x xisr=0x%x\n",
  666. xc->server_num, cppr, mfrr, xisr);
  667. /*
  668. * We can't update the state of a "pushed" VCPU, but that
  669. * shouldn't happen.
  670. */
  671. if (WARN_ON(vcpu->arch.xive_pushed))
  672. return -EIO;
  673. /* Update VCPU HW saved state */
  674. vcpu->arch.xive_saved_state.cppr = cppr;
  675. xc->hw_cppr = xc->cppr = cppr;
  676. /*
  677. * Update MFRR state. If it's not 0xff, we mark the VCPU as
  678. * having a pending MFRR change, which will re-evaluate the
  679. * target. The VCPU will thus potentially get a spurious
  680. * interrupt but that's not a big deal.
  681. */
  682. xc->mfrr = mfrr;
  683. if (mfrr < cppr)
  684. xive_irq_trigger(&xc->vp_ipi_data);
  685. /*
  686. * Now saved XIRR is "interesting". It means there's something in
  687. * the legacy "1 element" queue... for an IPI we simply ignore it,
  688. * as the MFRR restore will handle that. For anything else we need
  689. * to force a resend of the source.
  690. * However the source may not have been setup yet. If that's the
  691. * case, we keep that info and increment a counter in the xive to
  692. * tell subsequent xive_set_source() to go look.
  693. */
  694. if (xisr > XICS_IPI && !xive_restore_pending_irq(xive, xisr)) {
  695. xc->delayed_irq = xisr;
  696. xive->delayed_irqs++;
  697. pr_devel(" xisr restore delayed\n");
  698. }
  699. return 0;
  700. }
  701. int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
  702. struct irq_desc *host_desc)
  703. {
  704. struct kvmppc_xive *xive = kvm->arch.xive;
  705. struct kvmppc_xive_src_block *sb;
  706. struct kvmppc_xive_irq_state *state;
  707. struct irq_data *host_data = irq_desc_get_irq_data(host_desc);
  708. unsigned int host_irq = irq_desc_get_irq(host_desc);
  709. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(host_data);
  710. u16 idx;
  711. u8 prio;
  712. int rc;
  713. if (!xive)
  714. return -ENODEV;
  715. pr_devel("set_mapped girq 0x%lx host HW irq 0x%x...\n",guest_irq, hw_irq);
  716. sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
  717. if (!sb)
  718. return -EINVAL;
  719. state = &sb->irq_state[idx];
  720. /*
  721. * Mark the passed-through interrupt as going to a VCPU,
  722. * this will prevent further EOIs and similar operations
  723. * from the XIVE code. It will also mask the interrupt
  724. * to either PQ=10 or 11 state, the latter if the interrupt
  725. * is pending. This will allow us to unmask or retrigger it
  726. * after routing it to the guest with a simple EOI.
  727. *
  728. * The "state" argument is a "token", all it needs is to be
  729. * non-NULL to switch to passed-through or NULL for the
  730. * other way around. We may not yet have an actual VCPU
  731. * target here and we don't really care.
  732. */
  733. rc = irq_set_vcpu_affinity(host_irq, state);
  734. if (rc) {
  735. pr_err("Failed to set VCPU affinity for irq %d\n", host_irq);
  736. return rc;
  737. }
  738. /*
  739. * Mask and read state of IPI. We need to know if its P bit
  740. * is set as that means it's potentially already using a
  741. * queue entry in the target
  742. */
  743. prio = xive_lock_and_mask(xive, sb, state);
  744. pr_devel(" old IPI prio %02x P:%d Q:%d\n", prio,
  745. state->old_p, state->old_q);
  746. /* Turn the IPI hard off */
  747. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
  748. /* Grab info about irq */
  749. state->pt_number = hw_irq;
  750. state->pt_data = irq_data_get_irq_handler_data(host_data);
  751. /*
  752. * Configure the IRQ to match the existing configuration of
  753. * the IPI if it was already targetted. Otherwise this will
  754. * mask the interrupt in a lossy way (act_priority is 0xff)
  755. * which is fine for a never started interrupt.
  756. */
  757. xive_native_configure_irq(hw_irq,
  758. xive->vp_base + state->act_server,
  759. state->act_priority, state->number);
  760. /*
  761. * We do an EOI to enable the interrupt (and retrigger if needed)
  762. * if the guest has the interrupt unmasked and the P bit was *not*
  763. * set in the IPI. If it was set, we know a slot may still be in
  764. * use in the target queue thus we have to wait for a guest
  765. * originated EOI
  766. */
  767. if (prio != MASKED && !state->old_p)
  768. xive_vm_source_eoi(hw_irq, state->pt_data);
  769. /* Clear old_p/old_q as they are no longer relevant */
  770. state->old_p = state->old_q = false;
  771. /* Restore guest prio (unlocks EOI) */
  772. mb();
  773. state->guest_priority = prio;
  774. arch_spin_unlock(&sb->lock);
  775. return 0;
  776. }
  777. EXPORT_SYMBOL_GPL(kvmppc_xive_set_mapped);
  778. int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
  779. struct irq_desc *host_desc)
  780. {
  781. struct kvmppc_xive *xive = kvm->arch.xive;
  782. struct kvmppc_xive_src_block *sb;
  783. struct kvmppc_xive_irq_state *state;
  784. unsigned int host_irq = irq_desc_get_irq(host_desc);
  785. u16 idx;
  786. u8 prio;
  787. int rc;
  788. if (!xive)
  789. return -ENODEV;
  790. pr_devel("clr_mapped girq 0x%lx...\n", guest_irq);
  791. sb = kvmppc_xive_find_source(xive, guest_irq, &idx);
  792. if (!sb)
  793. return -EINVAL;
  794. state = &sb->irq_state[idx];
  795. /*
  796. * Mask and read state of IRQ. We need to know if its P bit
  797. * is set as that means it's potentially already using a
  798. * queue entry in the target
  799. */
  800. prio = xive_lock_and_mask(xive, sb, state);
  801. pr_devel(" old IRQ prio %02x P:%d Q:%d\n", prio,
  802. state->old_p, state->old_q);
  803. /*
  804. * If old_p is set, the interrupt is pending, we switch it to
  805. * PQ=11. This will force a resend in the host so the interrupt
  806. * isn't lost to whatver host driver may pick it up
  807. */
  808. if (state->old_p)
  809. xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_11);
  810. /* Release the passed-through interrupt to the host */
  811. rc = irq_set_vcpu_affinity(host_irq, NULL);
  812. if (rc) {
  813. pr_err("Failed to clr VCPU affinity for irq %d\n", host_irq);
  814. return rc;
  815. }
  816. /* Forget about the IRQ */
  817. state->pt_number = 0;
  818. state->pt_data = NULL;
  819. /* Reconfigure the IPI */
  820. xive_native_configure_irq(state->ipi_number,
  821. xive->vp_base + state->act_server,
  822. state->act_priority, state->number);
  823. /*
  824. * If old_p is set (we have a queue entry potentially
  825. * occupied) or the interrupt is masked, we set the IPI
  826. * to PQ=10 state. Otherwise we just re-enable it (PQ=00).
  827. */
  828. if (prio == MASKED || state->old_p)
  829. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_10);
  830. else
  831. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_00);
  832. /* Restore guest prio (unlocks EOI) */
  833. mb();
  834. state->guest_priority = prio;
  835. arch_spin_unlock(&sb->lock);
  836. return 0;
  837. }
  838. EXPORT_SYMBOL_GPL(kvmppc_xive_clr_mapped);
  839. static void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)
  840. {
  841. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  842. struct kvm *kvm = vcpu->kvm;
  843. struct kvmppc_xive *xive = kvm->arch.xive;
  844. int i, j;
  845. for (i = 0; i <= xive->max_sbid; i++) {
  846. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  847. if (!sb)
  848. continue;
  849. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++) {
  850. struct kvmppc_xive_irq_state *state = &sb->irq_state[j];
  851. if (!state->valid)
  852. continue;
  853. if (state->act_priority == MASKED)
  854. continue;
  855. if (state->act_server != xc->server_num)
  856. continue;
  857. /* Clean it up */
  858. arch_spin_lock(&sb->lock);
  859. state->act_priority = MASKED;
  860. xive_vm_esb_load(&state->ipi_data, XIVE_ESB_SET_PQ_01);
  861. xive_native_configure_irq(state->ipi_number, 0, MASKED, 0);
  862. if (state->pt_number) {
  863. xive_vm_esb_load(state->pt_data, XIVE_ESB_SET_PQ_01);
  864. xive_native_configure_irq(state->pt_number, 0, MASKED, 0);
  865. }
  866. arch_spin_unlock(&sb->lock);
  867. }
  868. }
  869. }
  870. void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu)
  871. {
  872. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  873. struct kvmppc_xive *xive = xc->xive;
  874. int i;
  875. pr_devel("cleanup_vcpu(cpu=%d)\n", xc->server_num);
  876. /* Ensure no interrupt is still routed to that VP */
  877. xc->valid = false;
  878. kvmppc_xive_disable_vcpu_interrupts(vcpu);
  879. /* Mask the VP IPI */
  880. xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_01);
  881. /* Disable the VP */
  882. xive_native_disable_vp(xc->vp_id);
  883. /* Free the queues & associated interrupts */
  884. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  885. struct xive_q *q = &xc->queues[i];
  886. /* Free the escalation irq */
  887. if (xc->esc_virq[i]) {
  888. free_irq(xc->esc_virq[i], vcpu);
  889. irq_dispose_mapping(xc->esc_virq[i]);
  890. kfree(xc->esc_virq_names[i]);
  891. }
  892. /* Free the queue */
  893. xive_native_disable_queue(xc->vp_id, q, i);
  894. if (q->qpage) {
  895. free_pages((unsigned long)q->qpage,
  896. xive->q_page_order);
  897. q->qpage = NULL;
  898. }
  899. }
  900. /* Free the IPI */
  901. if (xc->vp_ipi) {
  902. xive_cleanup_irq_data(&xc->vp_ipi_data);
  903. xive_native_free_irq(xc->vp_ipi);
  904. }
  905. /* Free the VP */
  906. kfree(xc);
  907. }
  908. int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
  909. struct kvm_vcpu *vcpu, u32 cpu)
  910. {
  911. struct kvmppc_xive *xive = dev->private;
  912. struct kvmppc_xive_vcpu *xc;
  913. int i, r = -EBUSY;
  914. pr_devel("connect_vcpu(cpu=%d)\n", cpu);
  915. if (dev->ops != &kvm_xive_ops) {
  916. pr_devel("Wrong ops !\n");
  917. return -EPERM;
  918. }
  919. if (xive->kvm != vcpu->kvm)
  920. return -EPERM;
  921. if (vcpu->arch.irq_type)
  922. return -EBUSY;
  923. if (kvmppc_xive_find_server(vcpu->kvm, cpu)) {
  924. pr_devel("Duplicate !\n");
  925. return -EEXIST;
  926. }
  927. if (cpu >= KVM_MAX_VCPUS) {
  928. pr_devel("Out of bounds !\n");
  929. return -EINVAL;
  930. }
  931. xc = kzalloc(sizeof(*xc), GFP_KERNEL);
  932. if (!xc)
  933. return -ENOMEM;
  934. /* We need to synchronize with queue provisioning */
  935. mutex_lock(&vcpu->kvm->lock);
  936. vcpu->arch.xive_vcpu = xc;
  937. xc->xive = xive;
  938. xc->vcpu = vcpu;
  939. xc->server_num = cpu;
  940. xc->vp_id = xive->vp_base + cpu;
  941. xc->mfrr = 0xff;
  942. xc->valid = true;
  943. r = xive_native_get_vp_info(xc->vp_id, &xc->vp_cam, &xc->vp_chip_id);
  944. if (r)
  945. goto bail;
  946. /* Configure VCPU fields for use by assembly push/pull */
  947. vcpu->arch.xive_saved_state.w01 = cpu_to_be64(0xff000000);
  948. vcpu->arch.xive_cam_word = cpu_to_be32(xc->vp_cam | TM_QW1W2_VO);
  949. /* Allocate IPI */
  950. xc->vp_ipi = xive_native_alloc_irq();
  951. if (!xc->vp_ipi) {
  952. pr_err("Failed to allocate xive irq for VCPU IPI\n");
  953. r = -EIO;
  954. goto bail;
  955. }
  956. pr_devel(" IPI=0x%x\n", xc->vp_ipi);
  957. r = xive_native_populate_irq_data(xc->vp_ipi, &xc->vp_ipi_data);
  958. if (r)
  959. goto bail;
  960. /*
  961. * Enable the VP first as the single escalation mode will
  962. * affect escalation interrupts numbering
  963. */
  964. r = xive_native_enable_vp(xc->vp_id, xive->single_escalation);
  965. if (r) {
  966. pr_err("Failed to enable VP in OPAL, err %d\n", r);
  967. goto bail;
  968. }
  969. /*
  970. * Initialize queues. Initially we set them all for no queueing
  971. * and we enable escalation for queue 0 only which we'll use for
  972. * our mfrr change notifications. If the VCPU is hot-plugged, we
  973. * do handle provisioning however based on the existing "map"
  974. * of enabled queues.
  975. */
  976. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  977. struct xive_q *q = &xc->queues[i];
  978. /* Single escalation, no queue 7 */
  979. if (i == 7 && xive->single_escalation)
  980. break;
  981. /* Is queue already enabled ? Provision it */
  982. if (xive->qmap & (1 << i)) {
  983. r = xive_provision_queue(vcpu, i);
  984. if (r == 0 && !xive->single_escalation)
  985. xive_attach_escalation(vcpu, i);
  986. if (r)
  987. goto bail;
  988. } else {
  989. r = xive_native_configure_queue(xc->vp_id,
  990. q, i, NULL, 0, true);
  991. if (r) {
  992. pr_err("Failed to configure queue %d for VCPU %d\n",
  993. i, cpu);
  994. goto bail;
  995. }
  996. }
  997. }
  998. /* If not done above, attach priority 0 escalation */
  999. r = xive_attach_escalation(vcpu, 0);
  1000. if (r)
  1001. goto bail;
  1002. /* Route the IPI */
  1003. r = xive_native_configure_irq(xc->vp_ipi, xc->vp_id, 0, XICS_IPI);
  1004. if (!r)
  1005. xive_vm_esb_load(&xc->vp_ipi_data, XIVE_ESB_SET_PQ_00);
  1006. bail:
  1007. mutex_unlock(&vcpu->kvm->lock);
  1008. if (r) {
  1009. kvmppc_xive_cleanup_vcpu(vcpu);
  1010. return r;
  1011. }
  1012. vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
  1013. return 0;
  1014. }
  1015. /*
  1016. * Scanning of queues before/after migration save
  1017. */
  1018. static void xive_pre_save_set_queued(struct kvmppc_xive *xive, u32 irq)
  1019. {
  1020. struct kvmppc_xive_src_block *sb;
  1021. struct kvmppc_xive_irq_state *state;
  1022. u16 idx;
  1023. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1024. if (!sb)
  1025. return;
  1026. state = &sb->irq_state[idx];
  1027. /* Some sanity checking */
  1028. if (!state->valid) {
  1029. pr_err("invalid irq 0x%x in cpu queue!\n", irq);
  1030. return;
  1031. }
  1032. /*
  1033. * If the interrupt is in a queue it should have P set.
  1034. * We warn so that gets reported. A backtrace isn't useful
  1035. * so no need to use a WARN_ON.
  1036. */
  1037. if (!state->saved_p)
  1038. pr_err("Interrupt 0x%x is marked in a queue but P not set !\n", irq);
  1039. /* Set flag */
  1040. state->in_queue = true;
  1041. }
  1042. static void xive_pre_save_mask_irq(struct kvmppc_xive *xive,
  1043. struct kvmppc_xive_src_block *sb,
  1044. u32 irq)
  1045. {
  1046. struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
  1047. if (!state->valid)
  1048. return;
  1049. /* Mask and save state, this will also sync HW queues */
  1050. state->saved_scan_prio = xive_lock_and_mask(xive, sb, state);
  1051. /* Transfer P and Q */
  1052. state->saved_p = state->old_p;
  1053. state->saved_q = state->old_q;
  1054. /* Unlock */
  1055. arch_spin_unlock(&sb->lock);
  1056. }
  1057. static void xive_pre_save_unmask_irq(struct kvmppc_xive *xive,
  1058. struct kvmppc_xive_src_block *sb,
  1059. u32 irq)
  1060. {
  1061. struct kvmppc_xive_irq_state *state = &sb->irq_state[irq];
  1062. if (!state->valid)
  1063. return;
  1064. /*
  1065. * Lock / exclude EOI (not technically necessary if the
  1066. * guest isn't running concurrently. If this becomes a
  1067. * performance issue we can probably remove the lock.
  1068. */
  1069. xive_lock_for_unmask(sb, state);
  1070. /* Restore mask/prio if it wasn't masked */
  1071. if (state->saved_scan_prio != MASKED)
  1072. xive_finish_unmask(xive, sb, state, state->saved_scan_prio);
  1073. /* Unlock */
  1074. arch_spin_unlock(&sb->lock);
  1075. }
  1076. static void xive_pre_save_queue(struct kvmppc_xive *xive, struct xive_q *q)
  1077. {
  1078. u32 idx = q->idx;
  1079. u32 toggle = q->toggle;
  1080. u32 irq;
  1081. do {
  1082. irq = __xive_read_eq(q->qpage, q->msk, &idx, &toggle);
  1083. if (irq > XICS_IPI)
  1084. xive_pre_save_set_queued(xive, irq);
  1085. } while(irq);
  1086. }
  1087. static void xive_pre_save_scan(struct kvmppc_xive *xive)
  1088. {
  1089. struct kvm_vcpu *vcpu = NULL;
  1090. int i, j;
  1091. /*
  1092. * See comment in xive_get_source() about how this
  1093. * work. Collect a stable state for all interrupts
  1094. */
  1095. for (i = 0; i <= xive->max_sbid; i++) {
  1096. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1097. if (!sb)
  1098. continue;
  1099. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1100. xive_pre_save_mask_irq(xive, sb, j);
  1101. }
  1102. /* Then scan the queues and update the "in_queue" flag */
  1103. kvm_for_each_vcpu(i, vcpu, xive->kvm) {
  1104. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1105. if (!xc)
  1106. continue;
  1107. for (j = 0; j < KVMPPC_XIVE_Q_COUNT; j++) {
  1108. if (xc->queues[j].qpage)
  1109. xive_pre_save_queue(xive, &xc->queues[j]);
  1110. }
  1111. }
  1112. /* Finally restore interrupt states */
  1113. for (i = 0; i <= xive->max_sbid; i++) {
  1114. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1115. if (!sb)
  1116. continue;
  1117. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1118. xive_pre_save_unmask_irq(xive, sb, j);
  1119. }
  1120. }
  1121. static void xive_post_save_scan(struct kvmppc_xive *xive)
  1122. {
  1123. u32 i, j;
  1124. /* Clear all the in_queue flags */
  1125. for (i = 0; i <= xive->max_sbid; i++) {
  1126. struct kvmppc_xive_src_block *sb = xive->src_blocks[i];
  1127. if (!sb)
  1128. continue;
  1129. for (j = 0; j < KVMPPC_XICS_IRQ_PER_ICS; j++)
  1130. sb->irq_state[j].in_queue = false;
  1131. }
  1132. /* Next get_source() will do a new scan */
  1133. xive->saved_src_count = 0;
  1134. }
  1135. /*
  1136. * This returns the source configuration and state to user space.
  1137. */
  1138. static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr)
  1139. {
  1140. struct kvmppc_xive_src_block *sb;
  1141. struct kvmppc_xive_irq_state *state;
  1142. u64 __user *ubufp = (u64 __user *) addr;
  1143. u64 val, prio;
  1144. u16 idx;
  1145. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1146. if (!sb)
  1147. return -ENOENT;
  1148. state = &sb->irq_state[idx];
  1149. if (!state->valid)
  1150. return -ENOENT;
  1151. pr_devel("get_source(%ld)...\n", irq);
  1152. /*
  1153. * So to properly save the state into something that looks like a
  1154. * XICS migration stream we cannot treat interrupts individually.
  1155. *
  1156. * We need, instead, mask them all (& save their previous PQ state)
  1157. * to get a stable state in the HW, then sync them to ensure that
  1158. * any interrupt that had already fired hits its queue, and finally
  1159. * scan all the queues to collect which interrupts are still present
  1160. * in the queues, so we can set the "pending" flag on them and
  1161. * they can be resent on restore.
  1162. *
  1163. * So we do it all when the "first" interrupt gets saved, all the
  1164. * state is collected at that point, the rest of xive_get_source()
  1165. * will merely collect and convert that state to the expected
  1166. * userspace bit mask.
  1167. */
  1168. if (xive->saved_src_count == 0)
  1169. xive_pre_save_scan(xive);
  1170. xive->saved_src_count++;
  1171. /* Convert saved state into something compatible with xics */
  1172. val = state->act_server;
  1173. prio = state->saved_scan_prio;
  1174. if (prio == MASKED) {
  1175. val |= KVM_XICS_MASKED;
  1176. prio = state->saved_priority;
  1177. }
  1178. val |= prio << KVM_XICS_PRIORITY_SHIFT;
  1179. if (state->lsi) {
  1180. val |= KVM_XICS_LEVEL_SENSITIVE;
  1181. if (state->saved_p)
  1182. val |= KVM_XICS_PENDING;
  1183. } else {
  1184. if (state->saved_p)
  1185. val |= KVM_XICS_PRESENTED;
  1186. if (state->saved_q)
  1187. val |= KVM_XICS_QUEUED;
  1188. /*
  1189. * We mark it pending (which will attempt a re-delivery)
  1190. * if we are in a queue *or* we were masked and had
  1191. * Q set which is equivalent to the XICS "masked pending"
  1192. * state
  1193. */
  1194. if (state->in_queue || (prio == MASKED && state->saved_q))
  1195. val |= KVM_XICS_PENDING;
  1196. }
  1197. /*
  1198. * If that was the last interrupt saved, reset the
  1199. * in_queue flags
  1200. */
  1201. if (xive->saved_src_count == xive->src_count)
  1202. xive_post_save_scan(xive);
  1203. /* Copy the result to userspace */
  1204. if (put_user(val, ubufp))
  1205. return -EFAULT;
  1206. return 0;
  1207. }
  1208. static struct kvmppc_xive_src_block *xive_create_src_block(struct kvmppc_xive *xive,
  1209. int irq)
  1210. {
  1211. struct kvm *kvm = xive->kvm;
  1212. struct kvmppc_xive_src_block *sb;
  1213. int i, bid;
  1214. bid = irq >> KVMPPC_XICS_ICS_SHIFT;
  1215. mutex_lock(&kvm->lock);
  1216. /* block already exists - somebody else got here first */
  1217. if (xive->src_blocks[bid])
  1218. goto out;
  1219. /* Create the ICS */
  1220. sb = kzalloc(sizeof(*sb), GFP_KERNEL);
  1221. if (!sb)
  1222. goto out;
  1223. sb->id = bid;
  1224. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  1225. sb->irq_state[i].number = (bid << KVMPPC_XICS_ICS_SHIFT) | i;
  1226. sb->irq_state[i].guest_priority = MASKED;
  1227. sb->irq_state[i].saved_priority = MASKED;
  1228. sb->irq_state[i].act_priority = MASKED;
  1229. }
  1230. smp_wmb();
  1231. xive->src_blocks[bid] = sb;
  1232. if (bid > xive->max_sbid)
  1233. xive->max_sbid = bid;
  1234. out:
  1235. mutex_unlock(&kvm->lock);
  1236. return xive->src_blocks[bid];
  1237. }
  1238. static bool xive_check_delayed_irq(struct kvmppc_xive *xive, u32 irq)
  1239. {
  1240. struct kvm *kvm = xive->kvm;
  1241. struct kvm_vcpu *vcpu = NULL;
  1242. int i;
  1243. kvm_for_each_vcpu(i, vcpu, kvm) {
  1244. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1245. if (!xc)
  1246. continue;
  1247. if (xc->delayed_irq == irq) {
  1248. xc->delayed_irq = 0;
  1249. xive->delayed_irqs--;
  1250. return true;
  1251. }
  1252. }
  1253. return false;
  1254. }
  1255. static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr)
  1256. {
  1257. struct kvmppc_xive_src_block *sb;
  1258. struct kvmppc_xive_irq_state *state;
  1259. u64 __user *ubufp = (u64 __user *) addr;
  1260. u16 idx;
  1261. u64 val;
  1262. u8 act_prio, guest_prio;
  1263. u32 server;
  1264. int rc = 0;
  1265. if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
  1266. return -ENOENT;
  1267. pr_devel("set_source(irq=0x%lx)\n", irq);
  1268. /* Find the source */
  1269. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1270. if (!sb) {
  1271. pr_devel("No source, creating source block...\n");
  1272. sb = xive_create_src_block(xive, irq);
  1273. if (!sb) {
  1274. pr_devel("Failed to create block...\n");
  1275. return -ENOMEM;
  1276. }
  1277. }
  1278. state = &sb->irq_state[idx];
  1279. /* Read user passed data */
  1280. if (get_user(val, ubufp)) {
  1281. pr_devel("fault getting user info !\n");
  1282. return -EFAULT;
  1283. }
  1284. server = val & KVM_XICS_DESTINATION_MASK;
  1285. guest_prio = val >> KVM_XICS_PRIORITY_SHIFT;
  1286. pr_devel(" val=0x016%llx (server=0x%x, guest_prio=%d)\n",
  1287. val, server, guest_prio);
  1288. /*
  1289. * If the source doesn't already have an IPI, allocate
  1290. * one and get the corresponding data
  1291. */
  1292. if (!state->ipi_number) {
  1293. state->ipi_number = xive_native_alloc_irq();
  1294. if (state->ipi_number == 0) {
  1295. pr_devel("Failed to allocate IPI !\n");
  1296. return -ENOMEM;
  1297. }
  1298. xive_native_populate_irq_data(state->ipi_number, &state->ipi_data);
  1299. pr_devel(" src_ipi=0x%x\n", state->ipi_number);
  1300. }
  1301. /*
  1302. * We use lock_and_mask() to set us in the right masked
  1303. * state. We will override that state from the saved state
  1304. * further down, but this will handle the cases of interrupts
  1305. * that need FW masking. We set the initial guest_priority to
  1306. * 0 before calling it to ensure it actually performs the masking.
  1307. */
  1308. state->guest_priority = 0;
  1309. xive_lock_and_mask(xive, sb, state);
  1310. /*
  1311. * Now, we select a target if we have one. If we don't we
  1312. * leave the interrupt untargetted. It means that an interrupt
  1313. * can become "untargetted" accross migration if it was masked
  1314. * by set_xive() but there is little we can do about it.
  1315. */
  1316. /* First convert prio and mark interrupt as untargetted */
  1317. act_prio = xive_prio_from_guest(guest_prio);
  1318. state->act_priority = MASKED;
  1319. /*
  1320. * We need to drop the lock due to the mutex below. Hopefully
  1321. * nothing is touching that interrupt yet since it hasn't been
  1322. * advertized to a running guest yet
  1323. */
  1324. arch_spin_unlock(&sb->lock);
  1325. /* If we have a priority target the interrupt */
  1326. if (act_prio != MASKED) {
  1327. /* First, check provisioning of queues */
  1328. mutex_lock(&xive->kvm->lock);
  1329. rc = xive_check_provisioning(xive->kvm, act_prio);
  1330. mutex_unlock(&xive->kvm->lock);
  1331. /* Target interrupt */
  1332. if (rc == 0)
  1333. rc = xive_target_interrupt(xive->kvm, state,
  1334. server, act_prio);
  1335. /*
  1336. * If provisioning or targetting failed, leave it
  1337. * alone and masked. It will remain disabled until
  1338. * the guest re-targets it.
  1339. */
  1340. }
  1341. /*
  1342. * Find out if this was a delayed irq stashed in an ICP,
  1343. * in which case, treat it as pending
  1344. */
  1345. if (xive->delayed_irqs && xive_check_delayed_irq(xive, irq)) {
  1346. val |= KVM_XICS_PENDING;
  1347. pr_devel(" Found delayed ! forcing PENDING !\n");
  1348. }
  1349. /* Cleanup the SW state */
  1350. state->old_p = false;
  1351. state->old_q = false;
  1352. state->lsi = false;
  1353. state->asserted = false;
  1354. /* Restore LSI state */
  1355. if (val & KVM_XICS_LEVEL_SENSITIVE) {
  1356. state->lsi = true;
  1357. if (val & KVM_XICS_PENDING)
  1358. state->asserted = true;
  1359. pr_devel(" LSI ! Asserted=%d\n", state->asserted);
  1360. }
  1361. /*
  1362. * Restore P and Q. If the interrupt was pending, we
  1363. * force Q and !P, which will trigger a resend.
  1364. *
  1365. * That means that a guest that had both an interrupt
  1366. * pending (queued) and Q set will restore with only
  1367. * one instance of that interrupt instead of 2, but that
  1368. * is perfectly fine as coalescing interrupts that haven't
  1369. * been presented yet is always allowed.
  1370. */
  1371. if (val & KVM_XICS_PRESENTED && !(val & KVM_XICS_PENDING))
  1372. state->old_p = true;
  1373. if (val & KVM_XICS_QUEUED || val & KVM_XICS_PENDING)
  1374. state->old_q = true;
  1375. pr_devel(" P=%d, Q=%d\n", state->old_p, state->old_q);
  1376. /*
  1377. * If the interrupt was unmasked, update guest priority and
  1378. * perform the appropriate state transition and do a
  1379. * re-trigger if necessary.
  1380. */
  1381. if (val & KVM_XICS_MASKED) {
  1382. pr_devel(" masked, saving prio\n");
  1383. state->guest_priority = MASKED;
  1384. state->saved_priority = guest_prio;
  1385. } else {
  1386. pr_devel(" unmasked, restoring to prio %d\n", guest_prio);
  1387. xive_finish_unmask(xive, sb, state, guest_prio);
  1388. state->saved_priority = guest_prio;
  1389. }
  1390. /* Increment the number of valid sources and mark this one valid */
  1391. if (!state->valid)
  1392. xive->src_count++;
  1393. state->valid = true;
  1394. return 0;
  1395. }
  1396. int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  1397. bool line_status)
  1398. {
  1399. struct kvmppc_xive *xive = kvm->arch.xive;
  1400. struct kvmppc_xive_src_block *sb;
  1401. struct kvmppc_xive_irq_state *state;
  1402. u16 idx;
  1403. if (!xive)
  1404. return -ENODEV;
  1405. sb = kvmppc_xive_find_source(xive, irq, &idx);
  1406. if (!sb)
  1407. return -EINVAL;
  1408. /* Perform locklessly .... (we need to do some RCUisms here...) */
  1409. state = &sb->irq_state[idx];
  1410. if (!state->valid)
  1411. return -EINVAL;
  1412. /* We don't allow a trigger on a passed-through interrupt */
  1413. if (state->pt_number)
  1414. return -EINVAL;
  1415. if ((level == 1 && state->lsi) || level == KVM_INTERRUPT_SET_LEVEL)
  1416. state->asserted = 1;
  1417. else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
  1418. state->asserted = 0;
  1419. return 0;
  1420. }
  1421. /* Trigger the IPI */
  1422. xive_irq_trigger(&state->ipi_data);
  1423. return 0;
  1424. }
  1425. static int xive_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1426. {
  1427. struct kvmppc_xive *xive = dev->private;
  1428. /* We honor the existing XICS ioctl */
  1429. switch (attr->group) {
  1430. case KVM_DEV_XICS_GRP_SOURCES:
  1431. return xive_set_source(xive, attr->attr, attr->addr);
  1432. }
  1433. return -ENXIO;
  1434. }
  1435. static int xive_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1436. {
  1437. struct kvmppc_xive *xive = dev->private;
  1438. /* We honor the existing XICS ioctl */
  1439. switch (attr->group) {
  1440. case KVM_DEV_XICS_GRP_SOURCES:
  1441. return xive_get_source(xive, attr->attr, attr->addr);
  1442. }
  1443. return -ENXIO;
  1444. }
  1445. static int xive_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1446. {
  1447. /* We honor the same limits as XICS, at least for now */
  1448. switch (attr->group) {
  1449. case KVM_DEV_XICS_GRP_SOURCES:
  1450. if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
  1451. attr->attr < KVMPPC_XICS_NR_IRQS)
  1452. return 0;
  1453. break;
  1454. }
  1455. return -ENXIO;
  1456. }
  1457. static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd)
  1458. {
  1459. xive_vm_esb_load(xd, XIVE_ESB_SET_PQ_01);
  1460. xive_native_configure_irq(hw_num, 0, MASKED, 0);
  1461. xive_cleanup_irq_data(xd);
  1462. }
  1463. static void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb)
  1464. {
  1465. int i;
  1466. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  1467. struct kvmppc_xive_irq_state *state = &sb->irq_state[i];
  1468. if (!state->valid)
  1469. continue;
  1470. kvmppc_xive_cleanup_irq(state->ipi_number, &state->ipi_data);
  1471. xive_native_free_irq(state->ipi_number);
  1472. /* Pass-through, cleanup too */
  1473. if (state->pt_number)
  1474. kvmppc_xive_cleanup_irq(state->pt_number, state->pt_data);
  1475. state->valid = false;
  1476. }
  1477. }
  1478. static void kvmppc_xive_free(struct kvm_device *dev)
  1479. {
  1480. struct kvmppc_xive *xive = dev->private;
  1481. struct kvm *kvm = xive->kvm;
  1482. int i;
  1483. debugfs_remove(xive->dentry);
  1484. if (kvm)
  1485. kvm->arch.xive = NULL;
  1486. /* Mask and free interrupts */
  1487. for (i = 0; i <= xive->max_sbid; i++) {
  1488. if (xive->src_blocks[i])
  1489. kvmppc_xive_free_sources(xive->src_blocks[i]);
  1490. kfree(xive->src_blocks[i]);
  1491. xive->src_blocks[i] = NULL;
  1492. }
  1493. if (xive->vp_base != XIVE_INVALID_VP)
  1494. xive_native_free_vp_block(xive->vp_base);
  1495. kfree(xive);
  1496. kfree(dev);
  1497. }
  1498. static int kvmppc_xive_create(struct kvm_device *dev, u32 type)
  1499. {
  1500. struct kvmppc_xive *xive;
  1501. struct kvm *kvm = dev->kvm;
  1502. int ret = 0;
  1503. pr_devel("Creating xive for partition\n");
  1504. xive = kzalloc(sizeof(*xive), GFP_KERNEL);
  1505. if (!xive)
  1506. return -ENOMEM;
  1507. dev->private = xive;
  1508. xive->dev = dev;
  1509. xive->kvm = kvm;
  1510. /* Already there ? */
  1511. if (kvm->arch.xive)
  1512. ret = -EEXIST;
  1513. else
  1514. kvm->arch.xive = xive;
  1515. /* We use the default queue size set by the host */
  1516. xive->q_order = xive_native_default_eq_shift();
  1517. if (xive->q_order < PAGE_SHIFT)
  1518. xive->q_page_order = 0;
  1519. else
  1520. xive->q_page_order = xive->q_order - PAGE_SHIFT;
  1521. /* Allocate a bunch of VPs */
  1522. xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS);
  1523. pr_devel("VP_Base=%x\n", xive->vp_base);
  1524. if (xive->vp_base == XIVE_INVALID_VP)
  1525. ret = -ENOMEM;
  1526. xive->single_escalation = xive_native_has_single_escalation();
  1527. if (ret) {
  1528. kfree(xive);
  1529. return ret;
  1530. }
  1531. return 0;
  1532. }
  1533. static int xive_debug_show(struct seq_file *m, void *private)
  1534. {
  1535. struct kvmppc_xive *xive = m->private;
  1536. struct kvm *kvm = xive->kvm;
  1537. struct kvm_vcpu *vcpu;
  1538. u64 t_rm_h_xirr = 0;
  1539. u64 t_rm_h_ipoll = 0;
  1540. u64 t_rm_h_cppr = 0;
  1541. u64 t_rm_h_eoi = 0;
  1542. u64 t_rm_h_ipi = 0;
  1543. u64 t_vm_h_xirr = 0;
  1544. u64 t_vm_h_ipoll = 0;
  1545. u64 t_vm_h_cppr = 0;
  1546. u64 t_vm_h_eoi = 0;
  1547. u64 t_vm_h_ipi = 0;
  1548. unsigned int i;
  1549. if (!kvm)
  1550. return 0;
  1551. seq_printf(m, "=========\nVCPU state\n=========\n");
  1552. kvm_for_each_vcpu(i, vcpu, kvm) {
  1553. struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;
  1554. unsigned int i;
  1555. if (!xc)
  1556. continue;
  1557. seq_printf(m, "cpu server %#x CPPR:%#x HWCPPR:%#x"
  1558. " MFRR:%#x PEND:%#x h_xirr: R=%lld V=%lld\n",
  1559. xc->server_num, xc->cppr, xc->hw_cppr,
  1560. xc->mfrr, xc->pending,
  1561. xc->stat_rm_h_xirr, xc->stat_vm_h_xirr);
  1562. for (i = 0; i < KVMPPC_XIVE_Q_COUNT; i++) {
  1563. struct xive_q *q = &xc->queues[i];
  1564. u32 i0, i1, idx;
  1565. if (!q->qpage && !xc->esc_virq[i])
  1566. continue;
  1567. seq_printf(m, " [q%d]: ", i);
  1568. if (q->qpage) {
  1569. idx = q->idx;
  1570. i0 = be32_to_cpup(q->qpage + idx);
  1571. idx = (idx + 1) & q->msk;
  1572. i1 = be32_to_cpup(q->qpage + idx);
  1573. seq_printf(m, "T=%d %08x %08x... \n", q->toggle, i0, i1);
  1574. }
  1575. if (xc->esc_virq[i]) {
  1576. struct irq_data *d = irq_get_irq_data(xc->esc_virq[i]);
  1577. struct xive_irq_data *xd = irq_data_get_irq_handler_data(d);
  1578. u64 pq = xive_vm_esb_load(xd, XIVE_ESB_GET);
  1579. seq_printf(m, "E:%c%c I(%d:%llx:%llx)",
  1580. (pq & XIVE_ESB_VAL_P) ? 'P' : 'p',
  1581. (pq & XIVE_ESB_VAL_Q) ? 'Q' : 'q',
  1582. xc->esc_virq[i], pq, xd->eoi_page);
  1583. seq_printf(m, "\n");
  1584. }
  1585. }
  1586. t_rm_h_xirr += xc->stat_rm_h_xirr;
  1587. t_rm_h_ipoll += xc->stat_rm_h_ipoll;
  1588. t_rm_h_cppr += xc->stat_rm_h_cppr;
  1589. t_rm_h_eoi += xc->stat_rm_h_eoi;
  1590. t_rm_h_ipi += xc->stat_rm_h_ipi;
  1591. t_vm_h_xirr += xc->stat_vm_h_xirr;
  1592. t_vm_h_ipoll += xc->stat_vm_h_ipoll;
  1593. t_vm_h_cppr += xc->stat_vm_h_cppr;
  1594. t_vm_h_eoi += xc->stat_vm_h_eoi;
  1595. t_vm_h_ipi += xc->stat_vm_h_ipi;
  1596. }
  1597. seq_printf(m, "Hcalls totals\n");
  1598. seq_printf(m, " H_XIRR R=%10lld V=%10lld\n", t_rm_h_xirr, t_vm_h_xirr);
  1599. seq_printf(m, " H_IPOLL R=%10lld V=%10lld\n", t_rm_h_ipoll, t_vm_h_ipoll);
  1600. seq_printf(m, " H_CPPR R=%10lld V=%10lld\n", t_rm_h_cppr, t_vm_h_cppr);
  1601. seq_printf(m, " H_EOI R=%10lld V=%10lld\n", t_rm_h_eoi, t_vm_h_eoi);
  1602. seq_printf(m, " H_IPI R=%10lld V=%10lld\n", t_rm_h_ipi, t_vm_h_ipi);
  1603. return 0;
  1604. }
  1605. static int xive_debug_open(struct inode *inode, struct file *file)
  1606. {
  1607. return single_open(file, xive_debug_show, inode->i_private);
  1608. }
  1609. static const struct file_operations xive_debug_fops = {
  1610. .open = xive_debug_open,
  1611. .read = seq_read,
  1612. .llseek = seq_lseek,
  1613. .release = single_release,
  1614. };
  1615. static void xive_debugfs_init(struct kvmppc_xive *xive)
  1616. {
  1617. char *name;
  1618. name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive);
  1619. if (!name) {
  1620. pr_err("%s: no memory for name\n", __func__);
  1621. return;
  1622. }
  1623. xive->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
  1624. xive, &xive_debug_fops);
  1625. pr_debug("%s: created %s\n", __func__, name);
  1626. kfree(name);
  1627. }
  1628. static void kvmppc_xive_init(struct kvm_device *dev)
  1629. {
  1630. struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private;
  1631. /* Register some debug interfaces */
  1632. xive_debugfs_init(xive);
  1633. }
  1634. struct kvm_device_ops kvm_xive_ops = {
  1635. .name = "kvm-xive",
  1636. .create = kvmppc_xive_create,
  1637. .init = kvmppc_xive_init,
  1638. .destroy = kvmppc_xive_free,
  1639. .set_attr = xive_set_attr,
  1640. .get_attr = xive_get_attr,
  1641. .has_attr = xive_has_attr,
  1642. };
  1643. void kvmppc_xive_init_module(void)
  1644. {
  1645. __xive_vm_h_xirr = xive_vm_h_xirr;
  1646. __xive_vm_h_ipoll = xive_vm_h_ipoll;
  1647. __xive_vm_h_ipi = xive_vm_h_ipi;
  1648. __xive_vm_h_cppr = xive_vm_h_cppr;
  1649. __xive_vm_h_eoi = xive_vm_h_eoi;
  1650. }
  1651. void kvmppc_xive_exit_module(void)
  1652. {
  1653. __xive_vm_h_xirr = NULL;
  1654. __xive_vm_h_ipoll = NULL;
  1655. __xive_vm_h_ipi = NULL;
  1656. __xive_vm_h_cppr = NULL;
  1657. __xive_vm_h_eoi = NULL;
  1658. }