book3s_xive.c 50 KB

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