book3s_xics.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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 int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
  552. {
  553. union kvmppc_icp_state state;
  554. struct kvmppc_icp *icp;
  555. icp = vcpu->arch.icp;
  556. if (icp->server_num != server) {
  557. icp = kvmppc_xics_find_server(vcpu->kvm, server);
  558. if (!icp)
  559. return H_PARAMETER;
  560. }
  561. state = ACCESS_ONCE(icp->state);
  562. kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
  563. kvmppc_set_gpr(vcpu, 5, state.mfrr);
  564. return H_SUCCESS;
  565. }
  566. static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  567. {
  568. union kvmppc_icp_state old_state, new_state;
  569. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  570. struct kvmppc_icp *icp = vcpu->arch.icp;
  571. u32 reject;
  572. XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
  573. /*
  574. * ICP State: Set_CPPR
  575. *
  576. * We can safely compare the new value with the current
  577. * value outside of the transaction as the CPPR is only
  578. * ever changed by the processor on itself
  579. */
  580. if (cppr > icp->state.cppr)
  581. icp_down_cppr(xics, icp, cppr);
  582. else if (cppr == icp->state.cppr)
  583. return;
  584. /*
  585. * ICP State: Up_CPPR
  586. *
  587. * The processor is raising its priority, this can result
  588. * in a rejection of a pending interrupt:
  589. *
  590. * ICP State: Reject_Current
  591. *
  592. * We can remove EE from the current processor, the update
  593. * transaction will set it again if needed
  594. */
  595. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  596. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  597. do {
  598. old_state = new_state = ACCESS_ONCE(icp->state);
  599. reject = 0;
  600. new_state.cppr = cppr;
  601. if (cppr <= new_state.pending_pri) {
  602. reject = new_state.xisr;
  603. new_state.xisr = 0;
  604. new_state.pending_pri = 0xff;
  605. }
  606. } while (!icp_try_update(icp, old_state, new_state, true));
  607. /*
  608. * Check for rejects. They are handled by doing a new delivery
  609. * attempt (see comments in icp_deliver_irq).
  610. */
  611. if (reject && reject != XICS_IPI)
  612. icp_deliver_irq(xics, icp, reject);
  613. }
  614. static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  615. {
  616. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  617. struct kvmppc_icp *icp = vcpu->arch.icp;
  618. struct kvmppc_ics *ics;
  619. struct ics_irq_state *state;
  620. u32 irq = xirr & 0x00ffffff;
  621. u16 src;
  622. XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
  623. /*
  624. * ICP State: EOI
  625. *
  626. * Note: If EOI is incorrectly used by SW to lower the CPPR
  627. * value (ie more favored), we do not check for rejection of
  628. * a pending interrupt, this is a SW error and PAPR sepcifies
  629. * that we don't have to deal with it.
  630. *
  631. * The sending of an EOI to the ICS is handled after the
  632. * CPPR update
  633. *
  634. * ICP State: Down_CPPR which we handle
  635. * in a separate function as it's shared with H_CPPR.
  636. */
  637. icp_down_cppr(xics, icp, xirr >> 24);
  638. /* IPIs have no EOI */
  639. if (irq == XICS_IPI)
  640. return H_SUCCESS;
  641. /*
  642. * EOI handling: If the interrupt is still asserted, we need to
  643. * resend it. We can take a lockless "peek" at the ICS state here.
  644. *
  645. * "Message" interrupts will never have "asserted" set
  646. */
  647. ics = kvmppc_xics_find_ics(xics, irq, &src);
  648. if (!ics) {
  649. XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
  650. return H_PARAMETER;
  651. }
  652. state = &ics->irq_state[src];
  653. /* Still asserted, resend it */
  654. if (state->asserted)
  655. icp_deliver_irq(xics, icp, irq);
  656. return H_SUCCESS;
  657. }
  658. static noinline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
  659. {
  660. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  661. struct kvmppc_icp *icp = vcpu->arch.icp;
  662. XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
  663. hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
  664. if (icp->rm_action & XICS_RM_KICK_VCPU)
  665. kvmppc_fast_vcpu_kick(icp->rm_kick_target);
  666. if (icp->rm_action & XICS_RM_CHECK_RESEND)
  667. icp_check_resend(xics, icp);
  668. if (icp->rm_action & XICS_RM_REJECT)
  669. icp_deliver_irq(xics, icp, icp->rm_reject);
  670. icp->rm_action = 0;
  671. return H_SUCCESS;
  672. }
  673. int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
  674. {
  675. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  676. unsigned long res;
  677. int rc = H_SUCCESS;
  678. /* Check if we have an ICP */
  679. if (!xics || !vcpu->arch.icp)
  680. return H_HARDWARE;
  681. /* These requests don't have real-mode implementations at present */
  682. switch (req) {
  683. case H_XIRR_X:
  684. res = kvmppc_h_xirr(vcpu);
  685. kvmppc_set_gpr(vcpu, 4, res);
  686. kvmppc_set_gpr(vcpu, 5, get_tb());
  687. return rc;
  688. case H_IPOLL:
  689. rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
  690. return rc;
  691. }
  692. /* Check for real mode returning too hard */
  693. if (xics->real_mode)
  694. return kvmppc_xics_rm_complete(vcpu, req);
  695. switch (req) {
  696. case H_XIRR:
  697. res = kvmppc_h_xirr(vcpu);
  698. kvmppc_set_gpr(vcpu, 4, res);
  699. break;
  700. case H_CPPR:
  701. kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
  702. break;
  703. case H_EOI:
  704. rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
  705. break;
  706. case H_IPI:
  707. rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
  708. kvmppc_get_gpr(vcpu, 5));
  709. break;
  710. }
  711. return rc;
  712. }
  713. /* -- Initialisation code etc. -- */
  714. static int xics_debug_show(struct seq_file *m, void *private)
  715. {
  716. struct kvmppc_xics *xics = m->private;
  717. struct kvm *kvm = xics->kvm;
  718. struct kvm_vcpu *vcpu;
  719. int icsid, i;
  720. if (!kvm)
  721. return 0;
  722. seq_printf(m, "=========\nICP state\n=========\n");
  723. kvm_for_each_vcpu(i, vcpu, kvm) {
  724. struct kvmppc_icp *icp = vcpu->arch.icp;
  725. union kvmppc_icp_state state;
  726. if (!icp)
  727. continue;
  728. state.raw = ACCESS_ONCE(icp->state.raw);
  729. seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
  730. icp->server_num, state.xisr,
  731. state.pending_pri, state.cppr, state.mfrr,
  732. state.out_ee, state.need_resend);
  733. }
  734. for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
  735. struct kvmppc_ics *ics = xics->ics[icsid];
  736. if (!ics)
  737. continue;
  738. seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
  739. icsid);
  740. mutex_lock(&ics->lock);
  741. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  742. struct ics_irq_state *irq = &ics->irq_state[i];
  743. seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
  744. irq->number, irq->server, irq->priority,
  745. irq->saved_priority, irq->asserted,
  746. irq->resend, irq->masked_pending);
  747. }
  748. mutex_unlock(&ics->lock);
  749. }
  750. return 0;
  751. }
  752. static int xics_debug_open(struct inode *inode, struct file *file)
  753. {
  754. return single_open(file, xics_debug_show, inode->i_private);
  755. }
  756. static const struct file_operations xics_debug_fops = {
  757. .open = xics_debug_open,
  758. .read = seq_read,
  759. .llseek = seq_lseek,
  760. .release = single_release,
  761. };
  762. static void xics_debugfs_init(struct kvmppc_xics *xics)
  763. {
  764. char *name;
  765. name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
  766. if (!name) {
  767. pr_err("%s: no memory for name\n", __func__);
  768. return;
  769. }
  770. xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
  771. xics, &xics_debug_fops);
  772. pr_debug("%s: created %s\n", __func__, name);
  773. kfree(name);
  774. }
  775. static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
  776. struct kvmppc_xics *xics, int irq)
  777. {
  778. struct kvmppc_ics *ics;
  779. int i, icsid;
  780. icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
  781. mutex_lock(&kvm->lock);
  782. /* ICS already exists - somebody else got here first */
  783. if (xics->ics[icsid])
  784. goto out;
  785. /* Create the ICS */
  786. ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
  787. if (!ics)
  788. goto out;
  789. mutex_init(&ics->lock);
  790. ics->icsid = icsid;
  791. for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
  792. ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
  793. ics->irq_state[i].priority = MASKED;
  794. ics->irq_state[i].saved_priority = MASKED;
  795. }
  796. smp_wmb();
  797. xics->ics[icsid] = ics;
  798. if (icsid > xics->max_icsid)
  799. xics->max_icsid = icsid;
  800. out:
  801. mutex_unlock(&kvm->lock);
  802. return xics->ics[icsid];
  803. }
  804. int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
  805. {
  806. struct kvmppc_icp *icp;
  807. if (!vcpu->kvm->arch.xics)
  808. return -ENODEV;
  809. if (kvmppc_xics_find_server(vcpu->kvm, server_num))
  810. return -EEXIST;
  811. icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
  812. if (!icp)
  813. return -ENOMEM;
  814. icp->vcpu = vcpu;
  815. icp->server_num = server_num;
  816. icp->state.mfrr = MASKED;
  817. icp->state.pending_pri = MASKED;
  818. vcpu->arch.icp = icp;
  819. XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
  820. return 0;
  821. }
  822. u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
  823. {
  824. struct kvmppc_icp *icp = vcpu->arch.icp;
  825. union kvmppc_icp_state state;
  826. if (!icp)
  827. return 0;
  828. state = icp->state;
  829. return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
  830. ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
  831. ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
  832. ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
  833. }
  834. int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
  835. {
  836. struct kvmppc_icp *icp = vcpu->arch.icp;
  837. struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
  838. union kvmppc_icp_state old_state, new_state;
  839. struct kvmppc_ics *ics;
  840. u8 cppr, mfrr, pending_pri;
  841. u32 xisr;
  842. u16 src;
  843. bool resend;
  844. if (!icp || !xics)
  845. return -ENOENT;
  846. cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
  847. xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
  848. KVM_REG_PPC_ICP_XISR_MASK;
  849. mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
  850. pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
  851. /* Require the new state to be internally consistent */
  852. if (xisr == 0) {
  853. if (pending_pri != 0xff)
  854. return -EINVAL;
  855. } else if (xisr == XICS_IPI) {
  856. if (pending_pri != mfrr || pending_pri >= cppr)
  857. return -EINVAL;
  858. } else {
  859. if (pending_pri >= mfrr || pending_pri >= cppr)
  860. return -EINVAL;
  861. ics = kvmppc_xics_find_ics(xics, xisr, &src);
  862. if (!ics)
  863. return -EINVAL;
  864. }
  865. new_state.raw = 0;
  866. new_state.cppr = cppr;
  867. new_state.xisr = xisr;
  868. new_state.mfrr = mfrr;
  869. new_state.pending_pri = pending_pri;
  870. /*
  871. * Deassert the CPU interrupt request.
  872. * icp_try_update will reassert it if necessary.
  873. */
  874. kvmppc_book3s_dequeue_irqprio(icp->vcpu,
  875. BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
  876. /*
  877. * Note that if we displace an interrupt from old_state.xisr,
  878. * we don't mark it as rejected. We expect userspace to set
  879. * the state of the interrupt sources to be consistent with
  880. * the ICP states (either before or afterwards, which doesn't
  881. * matter). We do handle resends due to CPPR becoming less
  882. * favoured because that is necessary to end up with a
  883. * consistent state in the situation where userspace restores
  884. * the ICS states before the ICP states.
  885. */
  886. do {
  887. old_state = ACCESS_ONCE(icp->state);
  888. if (new_state.mfrr <= old_state.mfrr) {
  889. resend = false;
  890. new_state.need_resend = old_state.need_resend;
  891. } else {
  892. resend = old_state.need_resend;
  893. new_state.need_resend = 0;
  894. }
  895. } while (!icp_try_update(icp, old_state, new_state, false));
  896. if (resend)
  897. icp_check_resend(xics, icp);
  898. return 0;
  899. }
  900. static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
  901. {
  902. int ret;
  903. struct kvmppc_ics *ics;
  904. struct ics_irq_state *irqp;
  905. u64 __user *ubufp = (u64 __user *) addr;
  906. u16 idx;
  907. u64 val, prio;
  908. ics = kvmppc_xics_find_ics(xics, irq, &idx);
  909. if (!ics)
  910. return -ENOENT;
  911. irqp = &ics->irq_state[idx];
  912. mutex_lock(&ics->lock);
  913. ret = -ENOENT;
  914. if (irqp->exists) {
  915. val = irqp->server;
  916. prio = irqp->priority;
  917. if (prio == MASKED) {
  918. val |= KVM_XICS_MASKED;
  919. prio = irqp->saved_priority;
  920. }
  921. val |= prio << KVM_XICS_PRIORITY_SHIFT;
  922. if (irqp->asserted)
  923. val |= KVM_XICS_LEVEL_SENSITIVE | KVM_XICS_PENDING;
  924. else if (irqp->masked_pending || irqp->resend)
  925. val |= KVM_XICS_PENDING;
  926. ret = 0;
  927. }
  928. mutex_unlock(&ics->lock);
  929. if (!ret && put_user(val, ubufp))
  930. ret = -EFAULT;
  931. return ret;
  932. }
  933. static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
  934. {
  935. struct kvmppc_ics *ics;
  936. struct ics_irq_state *irqp;
  937. u64 __user *ubufp = (u64 __user *) addr;
  938. u16 idx;
  939. u64 val;
  940. u8 prio;
  941. u32 server;
  942. if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
  943. return -ENOENT;
  944. ics = kvmppc_xics_find_ics(xics, irq, &idx);
  945. if (!ics) {
  946. ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
  947. if (!ics)
  948. return -ENOMEM;
  949. }
  950. irqp = &ics->irq_state[idx];
  951. if (get_user(val, ubufp))
  952. return -EFAULT;
  953. server = val & KVM_XICS_DESTINATION_MASK;
  954. prio = val >> KVM_XICS_PRIORITY_SHIFT;
  955. if (prio != MASKED &&
  956. kvmppc_xics_find_server(xics->kvm, server) == NULL)
  957. return -EINVAL;
  958. mutex_lock(&ics->lock);
  959. irqp->server = server;
  960. irqp->saved_priority = prio;
  961. if (val & KVM_XICS_MASKED)
  962. prio = MASKED;
  963. irqp->priority = prio;
  964. irqp->resend = 0;
  965. irqp->masked_pending = 0;
  966. irqp->asserted = 0;
  967. if ((val & KVM_XICS_PENDING) && (val & KVM_XICS_LEVEL_SENSITIVE))
  968. irqp->asserted = 1;
  969. irqp->exists = 1;
  970. mutex_unlock(&ics->lock);
  971. if (val & KVM_XICS_PENDING)
  972. icp_deliver_irq(xics, NULL, irqp->number);
  973. return 0;
  974. }
  975. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  976. bool line_status)
  977. {
  978. struct kvmppc_xics *xics = kvm->arch.xics;
  979. return ics_deliver_irq(xics, irq, level, line_status);
  980. }
  981. static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  982. {
  983. struct kvmppc_xics *xics = dev->private;
  984. switch (attr->group) {
  985. case KVM_DEV_XICS_GRP_SOURCES:
  986. return xics_set_source(xics, attr->attr, attr->addr);
  987. }
  988. return -ENXIO;
  989. }
  990. static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  991. {
  992. struct kvmppc_xics *xics = dev->private;
  993. switch (attr->group) {
  994. case KVM_DEV_XICS_GRP_SOURCES:
  995. return xics_get_source(xics, attr->attr, attr->addr);
  996. }
  997. return -ENXIO;
  998. }
  999. static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1000. {
  1001. switch (attr->group) {
  1002. case KVM_DEV_XICS_GRP_SOURCES:
  1003. if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
  1004. attr->attr < KVMPPC_XICS_NR_IRQS)
  1005. return 0;
  1006. break;
  1007. }
  1008. return -ENXIO;
  1009. }
  1010. static void kvmppc_xics_free(struct kvm_device *dev)
  1011. {
  1012. struct kvmppc_xics *xics = dev->private;
  1013. int i;
  1014. struct kvm *kvm = xics->kvm;
  1015. debugfs_remove(xics->dentry);
  1016. if (kvm)
  1017. kvm->arch.xics = NULL;
  1018. for (i = 0; i <= xics->max_icsid; i++)
  1019. kfree(xics->ics[i]);
  1020. kfree(xics);
  1021. kfree(dev);
  1022. }
  1023. static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
  1024. {
  1025. struct kvmppc_xics *xics;
  1026. struct kvm *kvm = dev->kvm;
  1027. int ret = 0;
  1028. xics = kzalloc(sizeof(*xics), GFP_KERNEL);
  1029. if (!xics)
  1030. return -ENOMEM;
  1031. dev->private = xics;
  1032. xics->dev = dev;
  1033. xics->kvm = kvm;
  1034. /* Already there ? */
  1035. mutex_lock(&kvm->lock);
  1036. if (kvm->arch.xics)
  1037. ret = -EEXIST;
  1038. else
  1039. kvm->arch.xics = xics;
  1040. mutex_unlock(&kvm->lock);
  1041. if (ret)
  1042. return ret;
  1043. xics_debugfs_init(xics);
  1044. #ifdef CONFIG_KVM_BOOK3S_64_HV
  1045. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  1046. /* Enable real mode support */
  1047. xics->real_mode = ENABLE_REALMODE;
  1048. xics->real_mode_dbg = DEBUG_REALMODE;
  1049. }
  1050. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  1051. return 0;
  1052. }
  1053. struct kvm_device_ops kvm_xics_ops = {
  1054. .name = "kvm-xics",
  1055. .create = kvmppc_xics_create,
  1056. .destroy = kvmppc_xics_free,
  1057. .set_attr = xics_set_attr,
  1058. .get_attr = xics_get_attr,
  1059. .has_attr = xics_has_attr,
  1060. };
  1061. int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
  1062. u32 xcpu)
  1063. {
  1064. struct kvmppc_xics *xics = dev->private;
  1065. int r = -EBUSY;
  1066. if (dev->ops != &kvm_xics_ops)
  1067. return -EPERM;
  1068. if (xics->kvm != vcpu->kvm)
  1069. return -EPERM;
  1070. if (vcpu->arch.irq_type)
  1071. return -EBUSY;
  1072. r = kvmppc_xics_create_icp(vcpu, xcpu);
  1073. if (!r)
  1074. vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
  1075. return r;
  1076. }
  1077. void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
  1078. {
  1079. if (!vcpu->arch.icp)
  1080. return;
  1081. kfree(vcpu->arch.icp);
  1082. vcpu->arch.icp = NULL;
  1083. vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
  1084. }