book3s_xics.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 <linux/anon_inodes.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/kvm_book3s.h>
  16. #include <asm/kvm_ppc.h>
  17. #include <asm/hvcall.h>
  18. #include <asm/xics.h>
  19. #include <asm/debug.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/seq_file.h>
  22. #include "book3s_xics.h"
  23. #if 1
  24. #define XICS_DBG(fmt...) do { } while (0)
  25. #else
  26. #define XICS_DBG(fmt...) trace_printk(fmt)
  27. #endif
  28. #define ENABLE_REALMODE true
  29. #define DEBUG_REALMODE false
  30. /*
  31. * LOCKING
  32. * =======
  33. *
  34. * Each ICS has a mutex protecting the information about the IRQ
  35. * sources and avoiding simultaneous deliveries if the same interrupt.
  36. *
  37. * ICP operations are done via a single compare & swap transaction
  38. * (most ICP state fits in the union kvmppc_icp_state)
  39. */
  40. /*
  41. * TODO
  42. * ====
  43. *
  44. * - To speed up resends, keep a bitmap of "resend" set bits in the
  45. * ICS
  46. *
  47. * - Speed up server# -> ICP lookup (array ? hash table ?)
  48. *
  49. * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
  50. * locks array to improve scalability
  51. */
  52. /* -- ICS routines -- */
  53. static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  54. u32 new_irq);
  55. static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level,
  56. bool report_status)
  57. {
  58. struct ics_irq_state *state;
  59. struct kvmppc_ics *ics;
  60. u16 src;
  61. XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
  62. ics = kvmppc_xics_find_ics(xics, irq, &src);
  63. if (!ics) {
  64. XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
  65. return -EINVAL;
  66. }
  67. state = &ics->irq_state[src];
  68. if (!state->exists)
  69. return -EINVAL;
  70. if (report_status)
  71. return state->asserted;
  72. /*
  73. * We set state->asserted locklessly. This should be fine as
  74. * we are the only setter, thus concurrent access is undefined
  75. * to begin with.
  76. */
  77. if (level == KVM_INTERRUPT_SET_LEVEL)
  78. state->asserted = 1;
  79. else if (level == KVM_INTERRUPT_UNSET) {
  80. state->asserted = 0;
  81. return 0;
  82. }
  83. /* Attempt delivery */
  84. icp_deliver_irq(xics, NULL, irq);
  85. return state->asserted;
  86. }
  87. static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
  88. struct kvmppc_icp *icp)
  89. {
  90. int i;
  91. mutex_lock(&ics->lock);
  92. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  93. struct ics_irq_state *state = &ics->irq_state[i];
  94. if (!state->resend)
  95. continue;
  96. XICS_DBG("resend %#x prio %#x\n", state->number,
  97. state->priority);
  98. mutex_unlock(&ics->lock);
  99. icp_deliver_irq(xics, icp, state->number);
  100. mutex_lock(&ics->lock);
  101. }
  102. mutex_unlock(&ics->lock);
  103. }
  104. static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
  105. struct ics_irq_state *state,
  106. u32 server, u32 priority, u32 saved_priority)
  107. {
  108. bool deliver;
  109. mutex_lock(&ics->lock);
  110. state->server = server;
  111. state->priority = priority;
  112. state->saved_priority = saved_priority;
  113. deliver = false;
  114. if ((state->masked_pending || state->resend) && priority != MASKED) {
  115. state->masked_pending = 0;
  116. deliver = true;
  117. }
  118. mutex_unlock(&ics->lock);
  119. return deliver;
  120. }
  121. int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
  122. {
  123. struct kvmppc_xics *xics = kvm->arch.xics;
  124. struct kvmppc_icp *icp;
  125. struct kvmppc_ics *ics;
  126. struct ics_irq_state *state;
  127. u16 src;
  128. if (!xics)
  129. return -ENODEV;
  130. ics = kvmppc_xics_find_ics(xics, irq, &src);
  131. if (!ics)
  132. return -EINVAL;
  133. state = &ics->irq_state[src];
  134. icp = kvmppc_xics_find_server(kvm, server);
  135. if (!icp)
  136. return -EINVAL;
  137. XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
  138. irq, server, priority,
  139. state->masked_pending, state->resend);
  140. if (write_xive(xics, ics, state, server, priority, priority))
  141. icp_deliver_irq(xics, icp, irq);
  142. return 0;
  143. }
  144. int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
  145. {
  146. struct kvmppc_xics *xics = kvm->arch.xics;
  147. struct kvmppc_ics *ics;
  148. struct ics_irq_state *state;
  149. u16 src;
  150. if (!xics)
  151. return -ENODEV;
  152. ics = kvmppc_xics_find_ics(xics, irq, &src);
  153. if (!ics)
  154. return -EINVAL;
  155. state = &ics->irq_state[src];
  156. mutex_lock(&ics->lock);
  157. *server = state->server;
  158. *priority = state->priority;
  159. mutex_unlock(&ics->lock);
  160. return 0;
  161. }
  162. int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
  163. {
  164. struct kvmppc_xics *xics = kvm->arch.xics;
  165. struct kvmppc_icp *icp;
  166. struct kvmppc_ics *ics;
  167. struct ics_irq_state *state;
  168. u16 src;
  169. if (!xics)
  170. return -ENODEV;
  171. ics = kvmppc_xics_find_ics(xics, irq, &src);
  172. if (!ics)
  173. return -EINVAL;
  174. state = &ics->irq_state[src];
  175. icp = kvmppc_xics_find_server(kvm, state->server);
  176. if (!icp)
  177. return -EINVAL;
  178. if (write_xive(xics, ics, state, state->server, state->saved_priority,
  179. state->saved_priority))
  180. icp_deliver_irq(xics, icp, irq);
  181. return 0;
  182. }
  183. int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
  184. {
  185. struct kvmppc_xics *xics = kvm->arch.xics;
  186. struct kvmppc_ics *ics;
  187. struct ics_irq_state *state;
  188. u16 src;
  189. if (!xics)
  190. return -ENODEV;
  191. ics = kvmppc_xics_find_ics(xics, irq, &src);
  192. if (!ics)
  193. return -EINVAL;
  194. state = &ics->irq_state[src];
  195. write_xive(xics, ics, state, state->server, MASKED, state->priority);
  196. return 0;
  197. }
  198. /* -- ICP routines, including hcalls -- */
  199. static inline bool icp_try_update(struct kvmppc_icp *icp,
  200. union kvmppc_icp_state old,
  201. union kvmppc_icp_state new,
  202. bool change_self)
  203. {
  204. bool success;
  205. /* Calculate new output value */
  206. new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
  207. /* Attempt atomic update */
  208. success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
  209. if (!success)
  210. goto bail;
  211. XICS_DBG("UPD [%04x] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
  212. icp->server_num,
  213. old.cppr, old.mfrr, old.pending_pri, old.xisr,
  214. old.need_resend, old.out_ee);
  215. XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
  216. new.cppr, new.mfrr, new.pending_pri, new.xisr,
  217. new.need_resend, new.out_ee);
  218. /*
  219. * Check for output state update
  220. *
  221. * Note that this is racy since another processor could be updating
  222. * the state already. This is why we never clear the interrupt output
  223. * here, we only ever set it. The clear only happens prior to doing
  224. * an update and only by the processor itself. Currently we do it
  225. * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
  226. *
  227. * We also do not try to figure out whether the EE state has changed,
  228. * we unconditionally set it if the new state calls for it. The reason
  229. * for that is that we opportunistically remove the pending interrupt
  230. * flag when raising CPPR, so we need to set it back here if an
  231. * interrupt is still pending.
  232. */
  233. if (new.out_ee) {
  234. kvmppc_book3s_queue_irqprio(icp->vcpu,
  235. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  236. if (!change_self)
  237. kvmppc_fast_vcpu_kick(icp->vcpu);
  238. }
  239. bail:
  240. return success;
  241. }
  242. static void icp_check_resend(struct kvmppc_xics *xics,
  243. struct kvmppc_icp *icp)
  244. {
  245. u32 icsid;
  246. /* Order this load with the test for need_resend in the caller */
  247. smp_rmb();
  248. for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
  249. struct kvmppc_ics *ics = xics->ics[icsid];
  250. if (!test_and_clear_bit(icsid, icp->resend_map))
  251. continue;
  252. if (!ics)
  253. continue;
  254. ics_check_resend(xics, ics, icp);
  255. }
  256. }
  257. static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
  258. u32 *reject)
  259. {
  260. union kvmppc_icp_state old_state, new_state;
  261. bool success;
  262. XICS_DBG("try deliver %#x(P:%#x) to server %#x\n", irq, priority,
  263. icp->server_num);
  264. do {
  265. old_state = new_state = ACCESS_ONCE(icp->state);
  266. *reject = 0;
  267. /* See if we can deliver */
  268. success = new_state.cppr > priority &&
  269. new_state.mfrr > priority &&
  270. new_state.pending_pri > priority;
  271. /*
  272. * If we can, check for a rejection and perform the
  273. * delivery
  274. */
  275. if (success) {
  276. *reject = new_state.xisr;
  277. new_state.xisr = irq;
  278. new_state.pending_pri = priority;
  279. } else {
  280. /*
  281. * If we failed to deliver we set need_resend
  282. * so a subsequent CPPR state change causes us
  283. * to try a new delivery.
  284. */
  285. new_state.need_resend = true;
  286. }
  287. } while (!icp_try_update(icp, old_state, new_state, false));
  288. return success;
  289. }
  290. static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  291. u32 new_irq)
  292. {
  293. struct ics_irq_state *state;
  294. struct kvmppc_ics *ics;
  295. u32 reject;
  296. u16 src;
  297. /*
  298. * This is used both for initial delivery of an interrupt and
  299. * for subsequent rejection.
  300. *
  301. * Rejection can be racy vs. resends. We have evaluated the
  302. * rejection in an atomic ICP transaction which is now complete,
  303. * so potentially the ICP can already accept the interrupt again.
  304. *
  305. * So we need to retry the delivery. Essentially the reject path
  306. * boils down to a failed delivery. Always.
  307. *
  308. * Now the interrupt could also have moved to a different target,
  309. * thus we may need to re-do the ICP lookup as well
  310. */
  311. again:
  312. /* Get the ICS state and lock it */
  313. ics = kvmppc_xics_find_ics(xics, new_irq, &src);
  314. if (!ics) {
  315. XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
  316. return;
  317. }
  318. state = &ics->irq_state[src];
  319. /* Get a lock on the ICS */
  320. mutex_lock(&ics->lock);
  321. /* Get our server */
  322. if (!icp || state->server != icp->server_num) {
  323. icp = kvmppc_xics_find_server(xics->kvm, state->server);
  324. if (!icp) {
  325. pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
  326. new_irq, state->server);
  327. goto out;
  328. }
  329. }
  330. /* Clear the resend bit of that interrupt */
  331. state->resend = 0;
  332. /*
  333. * If masked, bail out
  334. *
  335. * Note: PAPR doesn't mention anything about masked pending
  336. * when doing a resend, only when doing a delivery.
  337. *
  338. * However that would have the effect of losing a masked
  339. * interrupt that was rejected and isn't consistent with
  340. * the whole masked_pending business which is about not
  341. * losing interrupts that occur while masked.
  342. *
  343. * I don't differenciate normal deliveries and resends, this
  344. * implementation will differ from PAPR and not lose such
  345. * interrupts.
  346. */
  347. if (state->priority == MASKED) {
  348. XICS_DBG("irq %#x masked pending\n", new_irq);
  349. state->masked_pending = 1;
  350. goto out;
  351. }
  352. /*
  353. * Try the delivery, this will set the need_resend flag
  354. * in the ICP as part of the atomic transaction if the
  355. * delivery is not possible.
  356. *
  357. * Note that if successful, the new delivery might have itself
  358. * rejected an interrupt that was "delivered" before we took the
  359. * icp mutex.
  360. *
  361. * In this case we do the whole sequence all over again for the
  362. * new guy. We cannot assume that the rejected interrupt is less
  363. * favored than the new one, and thus doesn't need to be delivered,
  364. * because by the time we exit icp_try_to_deliver() the target
  365. * processor may well have alrady consumed & completed it, and thus
  366. * the rejected interrupt might actually be already acceptable.
  367. */
  368. if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
  369. /*
  370. * Delivery was successful, did we reject somebody else ?
  371. */
  372. if (reject && reject != XICS_IPI) {
  373. mutex_unlock(&ics->lock);
  374. new_irq = reject;
  375. goto again;
  376. }
  377. } else {
  378. /*
  379. * We failed to deliver the interrupt we need to set the
  380. * resend map bit and mark the ICS state as needing a resend
  381. */
  382. set_bit(ics->icsid, icp->resend_map);
  383. state->resend = 1;
  384. /*
  385. * If the need_resend flag got cleared in the ICP some time
  386. * between icp_try_to_deliver() atomic update and now, then
  387. * we know it might have missed the resend_map bit. So we
  388. * retry
  389. */
  390. smp_mb();
  391. if (!icp->state.need_resend) {
  392. mutex_unlock(&ics->lock);
  393. goto again;
  394. }
  395. }
  396. out:
  397. mutex_unlock(&ics->lock);
  398. }
  399. static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
  400. u8 new_cppr)
  401. {
  402. union kvmppc_icp_state old_state, new_state;
  403. bool resend;
  404. /*
  405. * This handles several related states in one operation:
  406. *
  407. * ICP State: Down_CPPR
  408. *
  409. * Load CPPR with new value and if the XISR is 0
  410. * then check for resends:
  411. *
  412. * ICP State: Resend
  413. *
  414. * If MFRR is more favored than CPPR, check for IPIs
  415. * and notify ICS of a potential resend. This is done
  416. * asynchronously (when used in real mode, we will have
  417. * to exit here).
  418. *
  419. * We do not handle the complete Check_IPI as documented
  420. * here. In the PAPR, this state will be used for both
  421. * Set_MFRR and Down_CPPR. However, we know that we aren't
  422. * changing the MFRR state here so we don't need to handle
  423. * the case of an MFRR causing a reject of a pending irq,
  424. * this will have been handled when the MFRR was set in the
  425. * first place.
  426. *
  427. * Thus we don't have to handle rejects, only resends.
  428. *
  429. * When implementing real mode for HV KVM, resend will lead to
  430. * a H_TOO_HARD return and the whole transaction will be handled
  431. * in virtual mode.
  432. */
  433. do {
  434. old_state = new_state = ACCESS_ONCE(icp->state);
  435. /* Down_CPPR */
  436. new_state.cppr = new_cppr;
  437. /*
  438. * Cut down Resend / Check_IPI / IPI
  439. *
  440. * The logic is that we cannot have a pending interrupt
  441. * trumped by an IPI at this point (see above), so we
  442. * know that either the pending interrupt is already an
  443. * IPI (in which case we don't care to override it) or
  444. * it's either more favored than us or non existent
  445. */
  446. if (new_state.mfrr < new_cppr &&
  447. new_state.mfrr <= new_state.pending_pri) {
  448. WARN_ON(new_state.xisr != XICS_IPI &&
  449. new_state.xisr != 0);
  450. new_state.pending_pri = new_state.mfrr;
  451. new_state.xisr = XICS_IPI;
  452. }
  453. /* Latch/clear resend bit */
  454. resend = new_state.need_resend;
  455. new_state.need_resend = 0;
  456. } while (!icp_try_update(icp, old_state, new_state, true));
  457. /*
  458. * Now handle resend checks. Those are asynchronous to the ICP
  459. * state update in HW (ie bus transactions) so we can handle them
  460. * separately here too
  461. */
  462. if (resend)
  463. icp_check_resend(xics, icp);
  464. }
  465. static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
  466. {
  467. union kvmppc_icp_state old_state, new_state;
  468. struct kvmppc_icp *icp = vcpu->arch.icp;
  469. u32 xirr;
  470. /* First, remove EE from the processor */
  471. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  472. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  473. /*
  474. * ICP State: Accept_Interrupt
  475. *
  476. * Return the pending interrupt (if any) along with the
  477. * current CPPR, then clear the XISR & set CPPR to the
  478. * pending priority
  479. */
  480. do {
  481. old_state = new_state = ACCESS_ONCE(icp->state);
  482. xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
  483. if (!old_state.xisr)
  484. break;
  485. new_state.cppr = new_state.pending_pri;
  486. new_state.pending_pri = 0xff;
  487. new_state.xisr = 0;
  488. } while (!icp_try_update(icp, old_state, new_state, true));
  489. XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
  490. return xirr;
  491. }
  492. static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  493. unsigned long mfrr)
  494. {
  495. union kvmppc_icp_state old_state, new_state;
  496. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  497. struct kvmppc_icp *icp;
  498. u32 reject;
  499. bool resend;
  500. bool local;
  501. XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
  502. vcpu->vcpu_id, server, mfrr);
  503. icp = vcpu->arch.icp;
  504. local = icp->server_num == server;
  505. if (!local) {
  506. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  507. if (!icp)
  508. return H_PARAMETER;
  509. }
  510. /*
  511. * ICP state: Set_MFRR
  512. *
  513. * If the CPPR is more favored than the new MFRR, then
  514. * nothing needs to be rejected as there can be no XISR to
  515. * reject. If the MFRR is being made less favored then
  516. * there might be a previously-rejected interrupt needing
  517. * to be resent.
  518. *
  519. * If the CPPR is less favored, then we might be replacing
  520. * an interrupt, and thus need to possibly reject it as in
  521. *
  522. * ICP state: Check_IPI
  523. */
  524. do {
  525. old_state = new_state = ACCESS_ONCE(icp->state);
  526. /* Set_MFRR */
  527. new_state.mfrr = mfrr;
  528. /* Check_IPI */
  529. reject = 0;
  530. resend = false;
  531. if (mfrr < new_state.cppr) {
  532. /* Reject a pending interrupt if not an IPI */
  533. if (mfrr <= new_state.pending_pri)
  534. reject = new_state.xisr;
  535. new_state.pending_pri = mfrr;
  536. new_state.xisr = XICS_IPI;
  537. }
  538. if (mfrr > old_state.mfrr && mfrr > new_state.cppr) {
  539. resend = new_state.need_resend;
  540. new_state.need_resend = 0;
  541. }
  542. } while (!icp_try_update(icp, old_state, new_state, local));
  543. /* Handle reject */
  544. if (reject && reject != XICS_IPI)
  545. icp_deliver_irq(xics, icp, reject);
  546. /* Handle resend */
  547. if (resend)
  548. icp_check_resend(xics, icp);
  549. return H_SUCCESS;
  550. }
  551. static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  552. {
  553. union kvmppc_icp_state old_state, new_state;
  554. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  555. struct kvmppc_icp *icp = vcpu->arch.icp;
  556. u32 reject;
  557. XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
  558. /*
  559. * ICP State: Set_CPPR
  560. *
  561. * We can safely compare the new value with the current
  562. * value outside of the transaction as the CPPR is only
  563. * ever changed by the processor on itself
  564. */
  565. if (cppr > icp->state.cppr)
  566. icp_down_cppr(xics, icp, cppr);
  567. else if (cppr == icp->state.cppr)
  568. return;
  569. /*
  570. * ICP State: Up_CPPR
  571. *
  572. * The processor is raising its priority, this can result
  573. * in a rejection of a pending interrupt:
  574. *
  575. * ICP State: Reject_Current
  576. *
  577. * We can remove EE from the current processor, the update
  578. * transaction will set it again if needed
  579. */
  580. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  581. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  582. do {
  583. old_state = new_state = ACCESS_ONCE(icp->state);
  584. reject = 0;
  585. new_state.cppr = cppr;
  586. if (cppr <= new_state.pending_pri) {
  587. reject = new_state.xisr;
  588. new_state.xisr = 0;
  589. new_state.pending_pri = 0xff;
  590. }
  591. } while (!icp_try_update(icp, old_state, new_state, true));
  592. /*
  593. * Check for rejects. They are handled by doing a new delivery
  594. * attempt (see comments in icp_deliver_irq).
  595. */
  596. if (reject && reject != XICS_IPI)
  597. icp_deliver_irq(xics, icp, reject);
  598. }
  599. static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  600. {
  601. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  602. struct kvmppc_icp *icp = vcpu->arch.icp;
  603. struct kvmppc_ics *ics;
  604. struct ics_irq_state *state;
  605. u32 irq = xirr & 0x00ffffff;
  606. u16 src;
  607. XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
  608. /*
  609. * ICP State: EOI
  610. *
  611. * Note: If EOI is incorrectly used by SW to lower the CPPR
  612. * value (ie more favored), we do not check for rejection of
  613. * a pending interrupt, this is a SW error and PAPR sepcifies
  614. * that we don't have to deal with it.
  615. *
  616. * The sending of an EOI to the ICS is handled after the
  617. * CPPR update
  618. *
  619. * ICP State: Down_CPPR which we handle
  620. * in a separate function as it's shared with H_CPPR.
  621. */
  622. icp_down_cppr(xics, icp, xirr >> 24);
  623. /* IPIs have no EOI */
  624. if (irq == XICS_IPI)
  625. return H_SUCCESS;
  626. /*
  627. * EOI handling: If the interrupt is still asserted, we need to
  628. * resend it. We can take a lockless "peek" at the ICS state here.
  629. *
  630. * "Message" interrupts will never have "asserted" set
  631. */
  632. ics = kvmppc_xics_find_ics(xics, irq, &src);
  633. if (!ics) {
  634. XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
  635. return H_PARAMETER;
  636. }
  637. state = &ics->irq_state[src];
  638. /* Still asserted, resend it */
  639. if (state->asserted)
  640. icp_deliver_irq(xics, icp, irq);
  641. return H_SUCCESS;
  642. }
  643. static noinline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
  644. {
  645. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  646. struct kvmppc_icp *icp = vcpu->arch.icp;
  647. XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
  648. hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
  649. if (icp->rm_action & XICS_RM_KICK_VCPU)
  650. kvmppc_fast_vcpu_kick(icp->rm_kick_target);
  651. if (icp->rm_action & XICS_RM_CHECK_RESEND)
  652. icp_check_resend(xics, icp);
  653. if (icp->rm_action & XICS_RM_REJECT)
  654. icp_deliver_irq(xics, icp, icp->rm_reject);
  655. icp->rm_action = 0;
  656. return H_SUCCESS;
  657. }
  658. int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
  659. {
  660. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  661. unsigned long res;
  662. int rc = H_SUCCESS;
  663. /* Check if we have an ICP */
  664. if (!xics || !vcpu->arch.icp)
  665. return H_HARDWARE;
  666. /* Check for real mode returning too hard */
  667. if (xics->real_mode)
  668. return kvmppc_xics_rm_complete(vcpu, req);
  669. switch (req) {
  670. case H_XIRR:
  671. res = kvmppc_h_xirr(vcpu);
  672. kvmppc_set_gpr(vcpu, 4, res);
  673. break;
  674. case H_CPPR:
  675. kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
  676. break;
  677. case H_EOI:
  678. rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
  679. break;
  680. case H_IPI:
  681. rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
  682. kvmppc_get_gpr(vcpu, 5));
  683. break;
  684. }
  685. return rc;
  686. }
  687. /* -- Initialisation code etc. -- */
  688. static int xics_debug_show(struct seq_file *m, void *private)
  689. {
  690. struct kvmppc_xics *xics = m->private;
  691. struct kvm *kvm = xics->kvm;
  692. struct kvm_vcpu *vcpu;
  693. int icsid, i;
  694. if (!kvm)
  695. return 0;
  696. seq_printf(m, "=========\nICP state\n=========\n");
  697. kvm_for_each_vcpu(i, vcpu, kvm) {
  698. struct kvmppc_icp *icp = vcpu->arch.icp;
  699. union kvmppc_icp_state state;
  700. if (!icp)
  701. continue;
  702. state.raw = ACCESS_ONCE(icp->state.raw);
  703. seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
  704. icp->server_num, state.xisr,
  705. state.pending_pri, state.cppr, state.mfrr,
  706. state.out_ee, state.need_resend);
  707. }
  708. for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
  709. struct kvmppc_ics *ics = xics->ics[icsid];
  710. if (!ics)
  711. continue;
  712. seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
  713. icsid);
  714. mutex_lock(&ics->lock);
  715. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  716. struct ics_irq_state *irq = &ics->irq_state[i];
  717. seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
  718. irq->number, irq->server, irq->priority,
  719. irq->saved_priority, irq->asserted,
  720. irq->resend, irq->masked_pending);
  721. }
  722. mutex_unlock(&ics->lock);
  723. }
  724. return 0;
  725. }
  726. static int xics_debug_open(struct inode *inode, struct file *file)
  727. {
  728. return single_open(file, xics_debug_show, inode->i_private);
  729. }
  730. static const struct file_operations xics_debug_fops = {
  731. .open = xics_debug_open,
  732. .read = seq_read,
  733. .llseek = seq_lseek,
  734. .release = single_release,
  735. };
  736. static void xics_debugfs_init(struct kvmppc_xics *xics)
  737. {
  738. char *name;
  739. name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
  740. if (!name) {
  741. pr_err("%s: no memory for name\n", __func__);
  742. return;
  743. }
  744. xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
  745. xics, &xics_debug_fops);
  746. pr_debug("%s: created %s\n", __func__, name);
  747. kfree(name);
  748. }
  749. static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
  750. struct kvmppc_xics *xics, int irq)
  751. {
  752. struct kvmppc_ics *ics;
  753. int i, icsid;
  754. icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
  755. mutex_lock(&kvm->lock);
  756. /* ICS already exists - somebody else got here first */
  757. if (xics->ics[icsid])
  758. goto out;
  759. /* Create the ICS */
  760. ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
  761. if (!ics)
  762. goto out;
  763. mutex_init(&ics->lock);
  764. ics->icsid = icsid;
  765. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  766. ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
  767. ics->irq_state[i].priority = MASKED;
  768. ics->irq_state[i].saved_priority = MASKED;
  769. }
  770. smp_wmb();
  771. xics->ics[icsid] = ics;
  772. if (icsid > xics->max_icsid)
  773. xics->max_icsid = icsid;
  774. out:
  775. mutex_unlock(&kvm->lock);
  776. return xics->ics[icsid];
  777. }
  778. int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
  779. {
  780. struct kvmppc_icp *icp;
  781. if (!vcpu->kvm->arch.xics)
  782. return -ENODEV;
  783. if (kvmppc_xics_find_server(vcpu->kvm, server_num))
  784. return -EEXIST;
  785. icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
  786. if (!icp)
  787. return -ENOMEM;
  788. icp->vcpu = vcpu;
  789. icp->server_num = server_num;
  790. icp->state.mfrr = MASKED;
  791. icp->state.pending_pri = MASKED;
  792. vcpu->arch.icp = icp;
  793. XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
  794. return 0;
  795. }
  796. u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
  797. {
  798. struct kvmppc_icp *icp = vcpu->arch.icp;
  799. union kvmppc_icp_state state;
  800. if (!icp)
  801. return 0;
  802. state = icp->state;
  803. return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
  804. ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
  805. ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
  806. ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
  807. }
  808. int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
  809. {
  810. struct kvmppc_icp *icp = vcpu->arch.icp;
  811. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  812. union kvmppc_icp_state old_state, new_state;
  813. struct kvmppc_ics *ics;
  814. u8 cppr, mfrr, pending_pri;
  815. u32 xisr;
  816. u16 src;
  817. bool resend;
  818. if (!icp || !xics)
  819. return -ENOENT;
  820. cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
  821. xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
  822. KVM_REG_PPC_ICP_XISR_MASK;
  823. mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
  824. pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
  825. /* Require the new state to be internally consistent */
  826. if (xisr == 0) {
  827. if (pending_pri != 0xff)
  828. return -EINVAL;
  829. } else if (xisr == XICS_IPI) {
  830. if (pending_pri != mfrr || pending_pri >= cppr)
  831. return -EINVAL;
  832. } else {
  833. if (pending_pri >= mfrr || pending_pri >= cppr)
  834. return -EINVAL;
  835. ics = kvmppc_xics_find_ics(xics, xisr, &src);
  836. if (!ics)
  837. return -EINVAL;
  838. }
  839. new_state.raw = 0;
  840. new_state.cppr = cppr;
  841. new_state.xisr = xisr;
  842. new_state.mfrr = mfrr;
  843. new_state.pending_pri = pending_pri;
  844. /*
  845. * Deassert the CPU interrupt request.
  846. * icp_try_update will reassert it if necessary.
  847. */
  848. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  849. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  850. /*
  851. * Note that if we displace an interrupt from old_state.xisr,
  852. * we don't mark it as rejected. We expect userspace to set
  853. * the state of the interrupt sources to be consistent with
  854. * the ICP states (either before or afterwards, which doesn't
  855. * matter). We do handle resends due to CPPR becoming less
  856. * favoured because that is necessary to end up with a
  857. * consistent state in the situation where userspace restores
  858. * the ICS states before the ICP states.
  859. */
  860. do {
  861. old_state = ACCESS_ONCE(icp->state);
  862. if (new_state.mfrr <= old_state.mfrr) {
  863. resend = false;
  864. new_state.need_resend = old_state.need_resend;
  865. } else {
  866. resend = old_state.need_resend;
  867. new_state.need_resend = 0;
  868. }
  869. } while (!icp_try_update(icp, old_state, new_state, false));
  870. if (resend)
  871. icp_check_resend(xics, icp);
  872. return 0;
  873. }
  874. static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
  875. {
  876. int ret;
  877. struct kvmppc_ics *ics;
  878. struct ics_irq_state *irqp;
  879. u64 __user *ubufp = (u64 __user *) addr;
  880. u16 idx;
  881. u64 val, prio;
  882. ics = kvmppc_xics_find_ics(xics, irq, &idx);
  883. if (!ics)
  884. return -ENOENT;
  885. irqp = &ics->irq_state[idx];
  886. mutex_lock(&ics->lock);
  887. ret = -ENOENT;
  888. if (irqp->exists) {
  889. val = irqp->server;
  890. prio = irqp->priority;
  891. if (prio == MASKED) {
  892. val |= KVM_XICS_MASKED;
  893. prio = irqp->saved_priority;
  894. }
  895. val |= prio << KVM_XICS_PRIORITY_SHIFT;
  896. if (irqp->asserted)
  897. val |= KVM_XICS_LEVEL_SENSITIVE | KVM_XICS_PENDING;
  898. else if (irqp->masked_pending || irqp->resend)
  899. val |= KVM_XICS_PENDING;
  900. ret = 0;
  901. }
  902. mutex_unlock(&ics->lock);
  903. if (!ret && put_user(val, ubufp))
  904. ret = -EFAULT;
  905. return ret;
  906. }
  907. static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
  908. {
  909. struct kvmppc_ics *ics;
  910. struct ics_irq_state *irqp;
  911. u64 __user *ubufp = (u64 __user *) addr;
  912. u16 idx;
  913. u64 val;
  914. u8 prio;
  915. u32 server;
  916. if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
  917. return -ENOENT;
  918. ics = kvmppc_xics_find_ics(xics, irq, &idx);
  919. if (!ics) {
  920. ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
  921. if (!ics)
  922. return -ENOMEM;
  923. }
  924. irqp = &ics->irq_state[idx];
  925. if (get_user(val, ubufp))
  926. return -EFAULT;
  927. server = val & KVM_XICS_DESTINATION_MASK;
  928. prio = val >> KVM_XICS_PRIORITY_SHIFT;
  929. if (prio != MASKED &&
  930. kvmppc_xics_find_server(xics->kvm, server) == NULL)
  931. return -EINVAL;
  932. mutex_lock(&ics->lock);
  933. irqp->server = server;
  934. irqp->saved_priority = prio;
  935. if (val & KVM_XICS_MASKED)
  936. prio = MASKED;
  937. irqp->priority = prio;
  938. irqp->resend = 0;
  939. irqp->masked_pending = 0;
  940. irqp->asserted = 0;
  941. if ((val & KVM_XICS_PENDING) && (val & KVM_XICS_LEVEL_SENSITIVE))
  942. irqp->asserted = 1;
  943. irqp->exists = 1;
  944. mutex_unlock(&ics->lock);
  945. if (val & KVM_XICS_PENDING)
  946. icp_deliver_irq(xics, NULL, irqp->number);
  947. return 0;
  948. }
  949. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  950. bool line_status)
  951. {
  952. struct kvmppc_xics *xics = kvm->arch.xics;
  953. return ics_deliver_irq(xics, irq, level, line_status);
  954. }
  955. static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  956. {
  957. struct kvmppc_xics *xics = dev->private;
  958. switch (attr->group) {
  959. case KVM_DEV_XICS_GRP_SOURCES:
  960. return xics_set_source(xics, attr->attr, attr->addr);
  961. }
  962. return -ENXIO;
  963. }
  964. static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  965. {
  966. struct kvmppc_xics *xics = dev->private;
  967. switch (attr->group) {
  968. case KVM_DEV_XICS_GRP_SOURCES:
  969. return xics_get_source(xics, attr->attr, attr->addr);
  970. }
  971. return -ENXIO;
  972. }
  973. static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  974. {
  975. switch (attr->group) {
  976. case KVM_DEV_XICS_GRP_SOURCES:
  977. if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
  978. attr->attr < KVMPPC_XICS_NR_IRQS)
  979. return 0;
  980. break;
  981. }
  982. return -ENXIO;
  983. }
  984. static void kvmppc_xics_free(struct kvm_device *dev)
  985. {
  986. struct kvmppc_xics *xics = dev->private;
  987. int i;
  988. struct kvm *kvm = xics->kvm;
  989. debugfs_remove(xics->dentry);
  990. if (kvm)
  991. kvm->arch.xics = NULL;
  992. for (i = 0; i <= xics->max_icsid; i++)
  993. kfree(xics->ics[i]);
  994. kfree(xics);
  995. kfree(dev);
  996. }
  997. static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
  998. {
  999. struct kvmppc_xics *xics;
  1000. struct kvm *kvm = dev->kvm;
  1001. int ret = 0;
  1002. xics = kzalloc(sizeof(*xics), GFP_KERNEL);
  1003. if (!xics)
  1004. return -ENOMEM;
  1005. dev->private = xics;
  1006. xics->dev = dev;
  1007. xics->kvm = kvm;
  1008. /* Already there ? */
  1009. mutex_lock(&kvm->lock);
  1010. if (kvm->arch.xics)
  1011. ret = -EEXIST;
  1012. else
  1013. kvm->arch.xics = xics;
  1014. mutex_unlock(&kvm->lock);
  1015. if (ret)
  1016. return ret;
  1017. xics_debugfs_init(xics);
  1018. #ifdef CONFIG_KVM_BOOK3S_64_HV
  1019. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  1020. /* Enable real mode support */
  1021. xics->real_mode = ENABLE_REALMODE;
  1022. xics->real_mode_dbg = DEBUG_REALMODE;
  1023. }
  1024. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  1025. return 0;
  1026. }
  1027. struct kvm_device_ops kvm_xics_ops = {
  1028. .name = "kvm-xics",
  1029. .create = kvmppc_xics_create,
  1030. .destroy = kvmppc_xics_free,
  1031. .set_attr = xics_set_attr,
  1032. .get_attr = xics_get_attr,
  1033. .has_attr = xics_has_attr,
  1034. };
  1035. int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
  1036. u32 xcpu)
  1037. {
  1038. struct kvmppc_xics *xics = dev->private;
  1039. int r = -EBUSY;
  1040. if (dev->ops != &kvm_xics_ops)
  1041. return -EPERM;
  1042. if (xics->kvm != vcpu->kvm)
  1043. return -EPERM;
  1044. if (vcpu->arch.irq_type)
  1045. return -EBUSY;
  1046. r = kvmppc_xics_create_icp(vcpu, xcpu);
  1047. if (!r)
  1048. vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
  1049. return r;
  1050. }
  1051. void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
  1052. {
  1053. if (!vcpu->arch.icp)
  1054. return;
  1055. kfree(vcpu->arch.icp);
  1056. vcpu->arch.icp = NULL;
  1057. vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
  1058. }