book3s_xive.c 52 KB

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