book3s_xics.c 31 KB

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