book3s_xics.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * Copyright 2012 Michael Ellerman, IBM Corporation.
  3. * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/err.h>
  12. #include <linux/gfp.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/kvm_book3s.h>
  15. #include <asm/kvm_ppc.h>
  16. #include <asm/hvcall.h>
  17. #include <asm/xics.h>
  18. #include <asm/debug.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include "book3s_xics.h"
  22. #if 1
  23. #define XICS_DBG(fmt...) do { } while (0)
  24. #else
  25. #define XICS_DBG(fmt...) trace_printk(fmt)
  26. #endif
  27. /*
  28. * LOCKING
  29. * =======
  30. *
  31. * Each ICS has a mutex protecting the information about the IRQ
  32. * sources and avoiding simultaneous deliveries if the same interrupt.
  33. *
  34. * ICP operations are done via a single compare & swap transaction
  35. * (most ICP state fits in the union kvmppc_icp_state)
  36. */
  37. /*
  38. * TODO
  39. * ====
  40. *
  41. * - To speed up resends, keep a bitmap of "resend" set bits in the
  42. * ICS
  43. *
  44. * - Speed up server# -> ICP lookup (array ? hash table ?)
  45. *
  46. * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
  47. * locks array to improve scalability
  48. *
  49. * - ioctl's to save/restore the entire state for snapshot & migration
  50. */
  51. /* -- ICS routines -- */
  52. static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  53. u32 new_irq);
  54. static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
  55. {
  56. struct ics_irq_state *state;
  57. struct kvmppc_ics *ics;
  58. u16 src;
  59. XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
  60. ics = kvmppc_xics_find_ics(xics, irq, &src);
  61. if (!ics) {
  62. XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
  63. return -EINVAL;
  64. }
  65. state = &ics->irq_state[src];
  66. if (!state->exists)
  67. return -EINVAL;
  68. /*
  69. * We set state->asserted locklessly. This should be fine as
  70. * we are the only setter, thus concurrent access is undefined
  71. * to begin with.
  72. */
  73. if (level == KVM_INTERRUPT_SET_LEVEL)
  74. state->asserted = 1;
  75. else if (level == KVM_INTERRUPT_UNSET) {
  76. state->asserted = 0;
  77. return 0;
  78. }
  79. /* Attempt delivery */
  80. icp_deliver_irq(xics, NULL, irq);
  81. return 0;
  82. }
  83. static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
  84. struct kvmppc_icp *icp)
  85. {
  86. int i;
  87. mutex_lock(&ics->lock);
  88. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  89. struct ics_irq_state *state = &ics->irq_state[i];
  90. if (!state->resend)
  91. continue;
  92. XICS_DBG("resend %#x prio %#x\n", state->number,
  93. state->priority);
  94. mutex_unlock(&ics->lock);
  95. icp_deliver_irq(xics, icp, state->number);
  96. mutex_lock(&ics->lock);
  97. }
  98. mutex_unlock(&ics->lock);
  99. }
  100. int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
  101. {
  102. struct kvmppc_xics *xics = kvm->arch.xics;
  103. struct kvmppc_icp *icp;
  104. struct kvmppc_ics *ics;
  105. struct ics_irq_state *state;
  106. u16 src;
  107. bool deliver;
  108. if (!xics)
  109. return -ENODEV;
  110. ics = kvmppc_xics_find_ics(xics, irq, &src);
  111. if (!ics)
  112. return -EINVAL;
  113. state = &ics->irq_state[src];
  114. icp = kvmppc_xics_find_server(kvm, server);
  115. if (!icp)
  116. return -EINVAL;
  117. mutex_lock(&ics->lock);
  118. XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
  119. irq, server, priority,
  120. state->masked_pending, state->resend);
  121. state->server = server;
  122. state->priority = priority;
  123. deliver = false;
  124. if ((state->masked_pending || state->resend) && priority != MASKED) {
  125. state->masked_pending = 0;
  126. deliver = true;
  127. }
  128. mutex_unlock(&ics->lock);
  129. if (deliver)
  130. icp_deliver_irq(xics, icp, irq);
  131. return 0;
  132. }
  133. int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
  134. {
  135. struct kvmppc_xics *xics = kvm->arch.xics;
  136. struct kvmppc_ics *ics;
  137. struct ics_irq_state *state;
  138. u16 src;
  139. if (!xics)
  140. return -ENODEV;
  141. ics = kvmppc_xics_find_ics(xics, irq, &src);
  142. if (!ics)
  143. return -EINVAL;
  144. state = &ics->irq_state[src];
  145. mutex_lock(&ics->lock);
  146. *server = state->server;
  147. *priority = state->priority;
  148. mutex_unlock(&ics->lock);
  149. return 0;
  150. }
  151. /* -- ICP routines, including hcalls -- */
  152. static inline bool icp_try_update(struct kvmppc_icp *icp,
  153. union kvmppc_icp_state old,
  154. union kvmppc_icp_state new,
  155. bool change_self)
  156. {
  157. bool success;
  158. /* Calculate new output value */
  159. new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
  160. /* Attempt atomic update */
  161. success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
  162. if (!success)
  163. goto bail;
  164. XICS_DBG("UPD [%04x] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
  165. icp->server_num,
  166. old.cppr, old.mfrr, old.pending_pri, old.xisr,
  167. old.need_resend, old.out_ee);
  168. XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
  169. new.cppr, new.mfrr, new.pending_pri, new.xisr,
  170. new.need_resend, new.out_ee);
  171. /*
  172. * Check for output state update
  173. *
  174. * Note that this is racy since another processor could be updating
  175. * the state already. This is why we never clear the interrupt output
  176. * here, we only ever set it. The clear only happens prior to doing
  177. * an update and only by the processor itself. Currently we do it
  178. * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
  179. *
  180. * We also do not try to figure out whether the EE state has changed,
  181. * we unconditionally set it if the new state calls for it for the
  182. * same reason.
  183. */
  184. if (new.out_ee) {
  185. kvmppc_book3s_queue_irqprio(icp->vcpu,
  186. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  187. if (!change_self)
  188. kvmppc_fast_vcpu_kick(icp->vcpu);
  189. }
  190. bail:
  191. return success;
  192. }
  193. static void icp_check_resend(struct kvmppc_xics *xics,
  194. struct kvmppc_icp *icp)
  195. {
  196. u32 icsid;
  197. /* Order this load with the test for need_resend in the caller */
  198. smp_rmb();
  199. for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
  200. struct kvmppc_ics *ics = xics->ics[icsid];
  201. if (!test_and_clear_bit(icsid, icp->resend_map))
  202. continue;
  203. if (!ics)
  204. continue;
  205. ics_check_resend(xics, ics, icp);
  206. }
  207. }
  208. static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
  209. u32 *reject)
  210. {
  211. union kvmppc_icp_state old_state, new_state;
  212. bool success;
  213. XICS_DBG("try deliver %#x(P:%#x) to server %#x\n", irq, priority,
  214. icp->server_num);
  215. do {
  216. old_state = new_state = ACCESS_ONCE(icp->state);
  217. *reject = 0;
  218. /* See if we can deliver */
  219. success = new_state.cppr > priority &&
  220. new_state.mfrr > priority &&
  221. new_state.pending_pri > priority;
  222. /*
  223. * If we can, check for a rejection and perform the
  224. * delivery
  225. */
  226. if (success) {
  227. *reject = new_state.xisr;
  228. new_state.xisr = irq;
  229. new_state.pending_pri = priority;
  230. } else {
  231. /*
  232. * If we failed to deliver we set need_resend
  233. * so a subsequent CPPR state change causes us
  234. * to try a new delivery.
  235. */
  236. new_state.need_resend = true;
  237. }
  238. } while (!icp_try_update(icp, old_state, new_state, false));
  239. return success;
  240. }
  241. static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  242. u32 new_irq)
  243. {
  244. struct ics_irq_state *state;
  245. struct kvmppc_ics *ics;
  246. u32 reject;
  247. u16 src;
  248. /*
  249. * This is used both for initial delivery of an interrupt and
  250. * for subsequent rejection.
  251. *
  252. * Rejection can be racy vs. resends. We have evaluated the
  253. * rejection in an atomic ICP transaction which is now complete,
  254. * so potentially the ICP can already accept the interrupt again.
  255. *
  256. * So we need to retry the delivery. Essentially the reject path
  257. * boils down to a failed delivery. Always.
  258. *
  259. * Now the interrupt could also have moved to a different target,
  260. * thus we may need to re-do the ICP lookup as well
  261. */
  262. again:
  263. /* Get the ICS state and lock it */
  264. ics = kvmppc_xics_find_ics(xics, new_irq, &src);
  265. if (!ics) {
  266. XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
  267. return;
  268. }
  269. state = &ics->irq_state[src];
  270. /* Get a lock on the ICS */
  271. mutex_lock(&ics->lock);
  272. /* Get our server */
  273. if (!icp || state->server != icp->server_num) {
  274. icp = kvmppc_xics_find_server(xics->kvm, state->server);
  275. if (!icp) {
  276. pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
  277. new_irq, state->server);
  278. goto out;
  279. }
  280. }
  281. /* Clear the resend bit of that interrupt */
  282. state->resend = 0;
  283. /*
  284. * If masked, bail out
  285. *
  286. * Note: PAPR doesn't mention anything about masked pending
  287. * when doing a resend, only when doing a delivery.
  288. *
  289. * However that would have the effect of losing a masked
  290. * interrupt that was rejected and isn't consistent with
  291. * the whole masked_pending business which is about not
  292. * losing interrupts that occur while masked.
  293. *
  294. * I don't differenciate normal deliveries and resends, this
  295. * implementation will differ from PAPR and not lose such
  296. * interrupts.
  297. */
  298. if (state->priority == MASKED) {
  299. XICS_DBG("irq %#x masked pending\n", new_irq);
  300. state->masked_pending = 1;
  301. goto out;
  302. }
  303. /*
  304. * Try the delivery, this will set the need_resend flag
  305. * in the ICP as part of the atomic transaction if the
  306. * delivery is not possible.
  307. *
  308. * Note that if successful, the new delivery might have itself
  309. * rejected an interrupt that was "delivered" before we took the
  310. * icp mutex.
  311. *
  312. * In this case we do the whole sequence all over again for the
  313. * new guy. We cannot assume that the rejected interrupt is less
  314. * favored than the new one, and thus doesn't need to be delivered,
  315. * because by the time we exit icp_try_to_deliver() the target
  316. * processor may well have alrady consumed & completed it, and thus
  317. * the rejected interrupt might actually be already acceptable.
  318. */
  319. if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
  320. /*
  321. * Delivery was successful, did we reject somebody else ?
  322. */
  323. if (reject && reject != XICS_IPI) {
  324. mutex_unlock(&ics->lock);
  325. new_irq = reject;
  326. goto again;
  327. }
  328. } else {
  329. /*
  330. * We failed to deliver the interrupt we need to set the
  331. * resend map bit and mark the ICS state as needing a resend
  332. */
  333. set_bit(ics->icsid, icp->resend_map);
  334. state->resend = 1;
  335. /*
  336. * If the need_resend flag got cleared in the ICP some time
  337. * between icp_try_to_deliver() atomic update and now, then
  338. * we know it might have missed the resend_map bit. So we
  339. * retry
  340. */
  341. smp_mb();
  342. if (!icp->state.need_resend) {
  343. mutex_unlock(&ics->lock);
  344. goto again;
  345. }
  346. }
  347. out:
  348. mutex_unlock(&ics->lock);
  349. }
  350. static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  351. u8 new_cppr)
  352. {
  353. union kvmppc_icp_state old_state, new_state;
  354. bool resend;
  355. /*
  356. * This handles several related states in one operation:
  357. *
  358. * ICP State: Down_CPPR
  359. *
  360. * Load CPPR with new value and if the XISR is 0
  361. * then check for resends:
  362. *
  363. * ICP State: Resend
  364. *
  365. * If MFRR is more favored than CPPR, check for IPIs
  366. * and notify ICS of a potential resend. This is done
  367. * asynchronously (when used in real mode, we will have
  368. * to exit here).
  369. *
  370. * We do not handle the complete Check_IPI as documented
  371. * here. In the PAPR, this state will be used for both
  372. * Set_MFRR and Down_CPPR. However, we know that we aren't
  373. * changing the MFRR state here so we don't need to handle
  374. * the case of an MFRR causing a reject of a pending irq,
  375. * this will have been handled when the MFRR was set in the
  376. * first place.
  377. *
  378. * Thus we don't have to handle rejects, only resends.
  379. *
  380. * When implementing real mode for HV KVM, resend will lead to
  381. * a H_TOO_HARD return and the whole transaction will be handled
  382. * in virtual mode.
  383. */
  384. do {
  385. old_state = new_state = ACCESS_ONCE(icp->state);
  386. /* Down_CPPR */
  387. new_state.cppr = new_cppr;
  388. /*
  389. * Cut down Resend / Check_IPI / IPI
  390. *
  391. * The logic is that we cannot have a pending interrupt
  392. * trumped by an IPI at this point (see above), so we
  393. * know that either the pending interrupt is already an
  394. * IPI (in which case we don't care to override it) or
  395. * it's either more favored than us or non existent
  396. */
  397. if (new_state.mfrr < new_cppr &&
  398. new_state.mfrr <= new_state.pending_pri) {
  399. WARN_ON(new_state.xisr != XICS_IPI &&
  400. new_state.xisr != 0);
  401. new_state.pending_pri = new_state.mfrr;
  402. new_state.xisr = XICS_IPI;
  403. }
  404. /* Latch/clear resend bit */
  405. resend = new_state.need_resend;
  406. new_state.need_resend = 0;
  407. } while (!icp_try_update(icp, old_state, new_state, true));
  408. /*
  409. * Now handle resend checks. Those are asynchronous to the ICP
  410. * state update in HW (ie bus transactions) so we can handle them
  411. * separately here too
  412. */
  413. if (resend)
  414. icp_check_resend(xics, icp);
  415. }
  416. static noinline unsigned long h_xirr(struct kvm_vcpu *vcpu)
  417. {
  418. union kvmppc_icp_state old_state, new_state;
  419. struct kvmppc_icp *icp = vcpu->arch.icp;
  420. u32 xirr;
  421. /* First, remove EE from the processor */
  422. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  423. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  424. /*
  425. * ICP State: Accept_Interrupt
  426. *
  427. * Return the pending interrupt (if any) along with the
  428. * current CPPR, then clear the XISR & set CPPR to the
  429. * pending priority
  430. */
  431. do {
  432. old_state = new_state = ACCESS_ONCE(icp->state);
  433. xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
  434. if (!old_state.xisr)
  435. break;
  436. new_state.cppr = new_state.pending_pri;
  437. new_state.pending_pri = 0xff;
  438. new_state.xisr = 0;
  439. } while (!icp_try_update(icp, old_state, new_state, true));
  440. XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
  441. return xirr;
  442. }
  443. static noinline int h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  444. unsigned long mfrr)
  445. {
  446. union kvmppc_icp_state old_state, new_state;
  447. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  448. struct kvmppc_icp *icp;
  449. u32 reject;
  450. bool resend;
  451. bool local;
  452. XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
  453. vcpu->vcpu_id, server, mfrr);
  454. icp = vcpu->arch.icp;
  455. local = icp->server_num == server;
  456. if (!local) {
  457. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  458. if (!icp)
  459. return H_PARAMETER;
  460. }
  461. /*
  462. * ICP state: Set_MFRR
  463. *
  464. * If the CPPR is more favored than the new MFRR, then
  465. * nothing needs to be rejected as there can be no XISR to
  466. * reject. If the MFRR is being made less favored then
  467. * there might be a previously-rejected interrupt needing
  468. * to be resent.
  469. *
  470. * If the CPPR is less favored, then we might be replacing
  471. * an interrupt, and thus need to possibly reject it as in
  472. *
  473. * ICP state: Check_IPI
  474. */
  475. do {
  476. old_state = new_state = ACCESS_ONCE(icp->state);
  477. /* Set_MFRR */
  478. new_state.mfrr = mfrr;
  479. /* Check_IPI */
  480. reject = 0;
  481. resend = false;
  482. if (mfrr < new_state.cppr) {
  483. /* Reject a pending interrupt if not an IPI */
  484. if (mfrr <= new_state.pending_pri)
  485. reject = new_state.xisr;
  486. new_state.pending_pri = mfrr;
  487. new_state.xisr = XICS_IPI;
  488. }
  489. if (mfrr > old_state.mfrr && mfrr > new_state.cppr) {
  490. resend = new_state.need_resend;
  491. new_state.need_resend = 0;
  492. }
  493. } while (!icp_try_update(icp, old_state, new_state, local));
  494. /* Handle reject */
  495. if (reject && reject != XICS_IPI)
  496. icp_deliver_irq(xics, icp, reject);
  497. /* Handle resend */
  498. if (resend)
  499. icp_check_resend(xics, icp);
  500. return H_SUCCESS;
  501. }
  502. static noinline void h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  503. {
  504. union kvmppc_icp_state old_state, new_state;
  505. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  506. struct kvmppc_icp *icp = vcpu->arch.icp;
  507. u32 reject;
  508. XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
  509. /*
  510. * ICP State: Set_CPPR
  511. *
  512. * We can safely compare the new value with the current
  513. * value outside of the transaction as the CPPR is only
  514. * ever changed by the processor on itself
  515. */
  516. if (cppr > icp->state.cppr)
  517. icp_down_cppr(xics, icp, cppr);
  518. else if (cppr == icp->state.cppr)
  519. return;
  520. /*
  521. * ICP State: Up_CPPR
  522. *
  523. * The processor is raising its priority, this can result
  524. * in a rejection of a pending interrupt:
  525. *
  526. * ICP State: Reject_Current
  527. *
  528. * We can remove EE from the current processor, the update
  529. * transaction will set it again if needed
  530. */
  531. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  532. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  533. do {
  534. old_state = new_state = ACCESS_ONCE(icp->state);
  535. reject = 0;
  536. new_state.cppr = cppr;
  537. if (cppr <= new_state.pending_pri) {
  538. reject = new_state.xisr;
  539. new_state.xisr = 0;
  540. new_state.pending_pri = 0xff;
  541. }
  542. } while (!icp_try_update(icp, old_state, new_state, true));
  543. /*
  544. * Check for rejects. They are handled by doing a new delivery
  545. * attempt (see comments in icp_deliver_irq).
  546. */
  547. if (reject && reject != XICS_IPI)
  548. icp_deliver_irq(xics, icp, reject);
  549. }
  550. static noinline int h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  551. {
  552. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  553. struct kvmppc_icp *icp = vcpu->arch.icp;
  554. struct kvmppc_ics *ics;
  555. struct ics_irq_state *state;
  556. u32 irq = xirr & 0x00ffffff;
  557. u16 src;
  558. XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
  559. /*
  560. * ICP State: EOI
  561. *
  562. * Note: If EOI is incorrectly used by SW to lower the CPPR
  563. * value (ie more favored), we do not check for rejection of
  564. * a pending interrupt, this is a SW error and PAPR sepcifies
  565. * that we don't have to deal with it.
  566. *
  567. * The sending of an EOI to the ICS is handled after the
  568. * CPPR update
  569. *
  570. * ICP State: Down_CPPR which we handle
  571. * in a separate function as it's shared with H_CPPR.
  572. */
  573. icp_down_cppr(xics, icp, xirr >> 24);
  574. /* IPIs have no EOI */
  575. if (irq == XICS_IPI)
  576. return H_SUCCESS;
  577. /*
  578. * EOI handling: If the interrupt is still asserted, we need to
  579. * resend it. We can take a lockless "peek" at the ICS state here.
  580. *
  581. * "Message" interrupts will never have "asserted" set
  582. */
  583. ics = kvmppc_xics_find_ics(xics, irq, &src);
  584. if (!ics) {
  585. XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
  586. return H_PARAMETER;
  587. }
  588. state = &ics->irq_state[src];
  589. /* Still asserted, resend it */
  590. if (state->asserted)
  591. icp_deliver_irq(xics, icp, irq);
  592. return H_SUCCESS;
  593. }
  594. int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
  595. {
  596. unsigned long res;
  597. int rc = H_SUCCESS;
  598. /* Check if we have an ICP */
  599. if (!vcpu->arch.icp || !vcpu->kvm->arch.xics)
  600. return H_HARDWARE;
  601. switch (req) {
  602. case H_XIRR:
  603. res = h_xirr(vcpu);
  604. kvmppc_set_gpr(vcpu, 4, res);
  605. break;
  606. case H_CPPR:
  607. h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
  608. break;
  609. case H_EOI:
  610. rc = h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
  611. break;
  612. case H_IPI:
  613. rc = h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
  614. kvmppc_get_gpr(vcpu, 5));
  615. break;
  616. }
  617. return rc;
  618. }
  619. /* -- Initialisation code etc. -- */
  620. static int xics_debug_show(struct seq_file *m, void *private)
  621. {
  622. struct kvmppc_xics *xics = m->private;
  623. struct kvm *kvm = xics->kvm;
  624. struct kvm_vcpu *vcpu;
  625. int icsid, i;
  626. if (!kvm)
  627. return 0;
  628. seq_printf(m, "=========\nICP state\n=========\n");
  629. kvm_for_each_vcpu(i, vcpu, kvm) {
  630. struct kvmppc_icp *icp = vcpu->arch.icp;
  631. union kvmppc_icp_state state;
  632. if (!icp)
  633. continue;
  634. state.raw = ACCESS_ONCE(icp->state.raw);
  635. seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
  636. icp->server_num, state.xisr,
  637. state.pending_pri, state.cppr, state.mfrr,
  638. state.out_ee, state.need_resend);
  639. }
  640. for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
  641. struct kvmppc_ics *ics = xics->ics[icsid];
  642. if (!ics)
  643. continue;
  644. seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
  645. icsid);
  646. mutex_lock(&ics->lock);
  647. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  648. struct ics_irq_state *irq = &ics->irq_state[i];
  649. seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
  650. irq->number, irq->server, irq->priority,
  651. irq->saved_priority, irq->asserted,
  652. irq->resend, irq->masked_pending);
  653. }
  654. mutex_unlock(&ics->lock);
  655. }
  656. return 0;
  657. }
  658. static int xics_debug_open(struct inode *inode, struct file *file)
  659. {
  660. return single_open(file, xics_debug_show, inode->i_private);
  661. }
  662. static const struct file_operations xics_debug_fops = {
  663. .open = xics_debug_open,
  664. .read = seq_read,
  665. .llseek = seq_lseek,
  666. .release = single_release,
  667. };
  668. static void xics_debugfs_init(struct kvmppc_xics *xics)
  669. {
  670. char *name;
  671. name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
  672. if (!name) {
  673. pr_err("%s: no memory for name\n", __func__);
  674. return;
  675. }
  676. xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
  677. xics, &xics_debug_fops);
  678. pr_debug("%s: created %s\n", __func__, name);
  679. kfree(name);
  680. }
  681. struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
  682. struct kvmppc_xics *xics, int irq)
  683. {
  684. struct kvmppc_ics *ics;
  685. int i, icsid;
  686. icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
  687. mutex_lock(&kvm->lock);
  688. /* ICS already exists - somebody else got here first */
  689. if (xics->ics[icsid])
  690. goto out;
  691. /* Create the ICS */
  692. ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
  693. if (!ics)
  694. goto out;
  695. mutex_init(&ics->lock);
  696. ics->icsid = icsid;
  697. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  698. ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
  699. ics->irq_state[i].priority = MASKED;
  700. ics->irq_state[i].saved_priority = MASKED;
  701. }
  702. smp_wmb();
  703. xics->ics[icsid] = ics;
  704. if (icsid > xics->max_icsid)
  705. xics->max_icsid = icsid;
  706. out:
  707. mutex_unlock(&kvm->lock);
  708. return xics->ics[icsid];
  709. }
  710. int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
  711. {
  712. struct kvmppc_icp *icp;
  713. if (!vcpu->kvm->arch.xics)
  714. return -ENODEV;
  715. if (kvmppc_xics_find_server(vcpu->kvm, server_num))
  716. return -EEXIST;
  717. icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
  718. if (!icp)
  719. return -ENOMEM;
  720. icp->vcpu = vcpu;
  721. icp->server_num = server_num;
  722. icp->state.mfrr = MASKED;
  723. icp->state.pending_pri = MASKED;
  724. vcpu->arch.icp = icp;
  725. XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
  726. return 0;
  727. }
  728. /* -- ioctls -- */
  729. int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args)
  730. {
  731. struct kvmppc_xics *xics;
  732. int r;
  733. /* locking against multiple callers? */
  734. xics = kvm->arch.xics;
  735. if (!xics)
  736. return -ENODEV;
  737. switch (args->level) {
  738. case KVM_INTERRUPT_SET:
  739. case KVM_INTERRUPT_SET_LEVEL:
  740. case KVM_INTERRUPT_UNSET:
  741. r = ics_deliver_irq(xics, args->irq, args->level);
  742. break;
  743. default:
  744. r = -EINVAL;
  745. }
  746. return r;
  747. }
  748. void kvmppc_xics_free(struct kvmppc_xics *xics)
  749. {
  750. int i;
  751. struct kvm *kvm = xics->kvm;
  752. debugfs_remove(xics->dentry);
  753. if (kvm)
  754. kvm->arch.xics = NULL;
  755. for (i = 0; i <= xics->max_icsid; i++)
  756. kfree(xics->ics[i]);
  757. kfree(xics);
  758. }
  759. int kvm_xics_create(struct kvm *kvm, u32 type)
  760. {
  761. struct kvmppc_xics *xics;
  762. int ret = 0;
  763. xics = kzalloc(sizeof(*xics), GFP_KERNEL);
  764. if (!xics)
  765. return -ENOMEM;
  766. xics->kvm = kvm;
  767. /* Already there ? */
  768. mutex_lock(&kvm->lock);
  769. if (kvm->arch.xics)
  770. ret = -EEXIST;
  771. else
  772. kvm->arch.xics = xics;
  773. mutex_unlock(&kvm->lock);
  774. if (ret)
  775. return ret;
  776. xics_debugfs_init(xics);
  777. return 0;
  778. }
  779. void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
  780. {
  781. if (!vcpu->arch.icp)
  782. return;
  783. kfree(vcpu->arch.icp);
  784. vcpu->arch.icp = NULL;
  785. vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
  786. }