book3s_xics.c 36 KB

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