spapr_xive_kvm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * QEMU PowerPC sPAPR XIVE interrupt controller model
  3. *
  4. * Copyright (c) 2017-2019, IBM Corporation.
  5. *
  6. * This code is licensed under the GPL version 2 or later. See the
  7. * COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/log.h"
  11. #include "qemu/error-report.h"
  12. #include "qapi/error.h"
  13. #include "target/ppc/cpu.h"
  14. #include "sysemu/cpus.h"
  15. #include "sysemu/kvm.h"
  16. #include "sysemu/runstate.h"
  17. #include "hw/ppc/spapr.h"
  18. #include "hw/ppc/spapr_cpu_core.h"
  19. #include "hw/ppc/spapr_xive.h"
  20. #include "hw/ppc/xive.h"
  21. #include "kvm_ppc.h"
  22. #include <sys/ioctl.h>
  23. /*
  24. * Helpers for CPU hotplug
  25. *
  26. * TODO: make a common KVMEnabledCPU layer for XICS and XIVE
  27. */
  28. typedef struct KVMEnabledCPU {
  29. unsigned long vcpu_id;
  30. QLIST_ENTRY(KVMEnabledCPU) node;
  31. } KVMEnabledCPU;
  32. static QLIST_HEAD(, KVMEnabledCPU)
  33. kvm_enabled_cpus = QLIST_HEAD_INITIALIZER(&kvm_enabled_cpus);
  34. static bool kvm_cpu_is_enabled(CPUState *cs)
  35. {
  36. KVMEnabledCPU *enabled_cpu;
  37. unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
  38. QLIST_FOREACH(enabled_cpu, &kvm_enabled_cpus, node) {
  39. if (enabled_cpu->vcpu_id == vcpu_id) {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. static void kvm_cpu_enable(CPUState *cs)
  46. {
  47. KVMEnabledCPU *enabled_cpu;
  48. unsigned long vcpu_id = kvm_arch_vcpu_id(cs);
  49. enabled_cpu = g_malloc(sizeof(*enabled_cpu));
  50. enabled_cpu->vcpu_id = vcpu_id;
  51. QLIST_INSERT_HEAD(&kvm_enabled_cpus, enabled_cpu, node);
  52. }
  53. static void kvm_cpu_disable_all(void)
  54. {
  55. KVMEnabledCPU *enabled_cpu, *next;
  56. QLIST_FOREACH_SAFE(enabled_cpu, &kvm_enabled_cpus, node, next) {
  57. QLIST_REMOVE(enabled_cpu, node);
  58. g_free(enabled_cpu);
  59. }
  60. }
  61. /*
  62. * XIVE Thread Interrupt Management context (KVM)
  63. */
  64. void kvmppc_xive_cpu_set_state(XiveTCTX *tctx, Error **errp)
  65. {
  66. SpaprXive *xive = SPAPR_MACHINE(qdev_get_machine())->xive;
  67. uint64_t state[2];
  68. int ret;
  69. /* The KVM XIVE device is not in use yet */
  70. if (xive->fd == -1) {
  71. return;
  72. }
  73. /* word0 and word1 of the OS ring. */
  74. state[0] = *((uint64_t *) &tctx->regs[TM_QW1_OS]);
  75. ret = kvm_set_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
  76. if (ret != 0) {
  77. error_setg_errno(errp, errno,
  78. "XIVE: could not restore KVM state of CPU %ld",
  79. kvm_arch_vcpu_id(tctx->cs));
  80. }
  81. }
  82. void kvmppc_xive_cpu_get_state(XiveTCTX *tctx, Error **errp)
  83. {
  84. SpaprXive *xive = SPAPR_MACHINE(qdev_get_machine())->xive;
  85. uint64_t state[2] = { 0 };
  86. int ret;
  87. /* The KVM XIVE device is not in use */
  88. if (xive->fd == -1) {
  89. return;
  90. }
  91. ret = kvm_get_one_reg(tctx->cs, KVM_REG_PPC_VP_STATE, state);
  92. if (ret != 0) {
  93. error_setg_errno(errp, errno,
  94. "XIVE: could not capture KVM state of CPU %ld",
  95. kvm_arch_vcpu_id(tctx->cs));
  96. return;
  97. }
  98. /* word0 and word1 of the OS ring. */
  99. *((uint64_t *) &tctx->regs[TM_QW1_OS]) = state[0];
  100. }
  101. typedef struct {
  102. XiveTCTX *tctx;
  103. Error *err;
  104. } XiveCpuGetState;
  105. static void kvmppc_xive_cpu_do_synchronize_state(CPUState *cpu,
  106. run_on_cpu_data arg)
  107. {
  108. XiveCpuGetState *s = arg.host_ptr;
  109. kvmppc_xive_cpu_get_state(s->tctx, &s->err);
  110. }
  111. void kvmppc_xive_cpu_synchronize_state(XiveTCTX *tctx, Error **errp)
  112. {
  113. XiveCpuGetState s = {
  114. .tctx = tctx,
  115. .err = NULL,
  116. };
  117. /*
  118. * Kick the vCPU to make sure they are available for the KVM ioctl.
  119. */
  120. run_on_cpu(tctx->cs, kvmppc_xive_cpu_do_synchronize_state,
  121. RUN_ON_CPU_HOST_PTR(&s));
  122. if (s.err) {
  123. error_propagate(errp, s.err);
  124. return;
  125. }
  126. }
  127. void kvmppc_xive_cpu_connect(XiveTCTX *tctx, Error **errp)
  128. {
  129. SpaprXive *xive = SPAPR_MACHINE(qdev_get_machine())->xive;
  130. unsigned long vcpu_id;
  131. int ret;
  132. /* The KVM XIVE device is not in use */
  133. if (xive->fd == -1) {
  134. return;
  135. }
  136. /* Check if CPU was hot unplugged and replugged. */
  137. if (kvm_cpu_is_enabled(tctx->cs)) {
  138. return;
  139. }
  140. vcpu_id = kvm_arch_vcpu_id(tctx->cs);
  141. ret = kvm_vcpu_enable_cap(tctx->cs, KVM_CAP_PPC_IRQ_XIVE, 0, xive->fd,
  142. vcpu_id, 0);
  143. if (ret < 0) {
  144. error_setg(errp, "XIVE: unable to connect CPU%ld to KVM device: %s",
  145. vcpu_id, strerror(errno));
  146. return;
  147. }
  148. kvm_cpu_enable(tctx->cs);
  149. }
  150. /*
  151. * XIVE Interrupt Source (KVM)
  152. */
  153. void kvmppc_xive_set_source_config(SpaprXive *xive, uint32_t lisn, XiveEAS *eas,
  154. Error **errp)
  155. {
  156. uint32_t end_idx;
  157. uint32_t end_blk;
  158. uint8_t priority;
  159. uint32_t server;
  160. bool masked;
  161. uint32_t eisn;
  162. uint64_t kvm_src;
  163. Error *local_err = NULL;
  164. assert(xive_eas_is_valid(eas));
  165. end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
  166. end_blk = xive_get_field64(EAS_END_BLOCK, eas->w);
  167. eisn = xive_get_field64(EAS_END_DATA, eas->w);
  168. masked = xive_eas_is_masked(eas);
  169. spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
  170. kvm_src = priority << KVM_XIVE_SOURCE_PRIORITY_SHIFT &
  171. KVM_XIVE_SOURCE_PRIORITY_MASK;
  172. kvm_src |= server << KVM_XIVE_SOURCE_SERVER_SHIFT &
  173. KVM_XIVE_SOURCE_SERVER_MASK;
  174. kvm_src |= ((uint64_t) masked << KVM_XIVE_SOURCE_MASKED_SHIFT) &
  175. KVM_XIVE_SOURCE_MASKED_MASK;
  176. kvm_src |= ((uint64_t)eisn << KVM_XIVE_SOURCE_EISN_SHIFT) &
  177. KVM_XIVE_SOURCE_EISN_MASK;
  178. kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE_CONFIG, lisn,
  179. &kvm_src, true, &local_err);
  180. if (local_err) {
  181. error_propagate(errp, local_err);
  182. return;
  183. }
  184. }
  185. void kvmppc_xive_sync_source(SpaprXive *xive, uint32_t lisn, Error **errp)
  186. {
  187. kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE_SYNC, lisn,
  188. NULL, true, errp);
  189. }
  190. /*
  191. * At reset, the interrupt sources are simply created and MASKED. We
  192. * only need to inform the KVM XIVE device about their type: LSI or
  193. * MSI.
  194. */
  195. int kvmppc_xive_source_reset_one(XiveSource *xsrc, int srcno, Error **errp)
  196. {
  197. SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
  198. uint64_t state = 0;
  199. /* The KVM XIVE device is not in use */
  200. if (xive->fd == -1) {
  201. return -ENODEV;
  202. }
  203. if (xive_source_irq_is_lsi(xsrc, srcno)) {
  204. state |= KVM_XIVE_LEVEL_SENSITIVE;
  205. if (xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
  206. state |= KVM_XIVE_LEVEL_ASSERTED;
  207. }
  208. }
  209. return kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_SOURCE, srcno, &state,
  210. true, errp);
  211. }
  212. static void kvmppc_xive_source_reset(XiveSource *xsrc, Error **errp)
  213. {
  214. SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
  215. int i;
  216. for (i = 0; i < xsrc->nr_irqs; i++) {
  217. Error *local_err = NULL;
  218. if (!xive_eas_is_valid(&xive->eat[i])) {
  219. continue;
  220. }
  221. kvmppc_xive_source_reset_one(xsrc, i, &local_err);
  222. if (local_err) {
  223. error_propagate(errp, local_err);
  224. return;
  225. }
  226. }
  227. }
  228. /*
  229. * This is used to perform the magic loads on the ESB pages, described
  230. * in xive.h.
  231. *
  232. * Memory barriers should not be needed for loads (no store for now).
  233. */
  234. static uint64_t xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
  235. uint64_t data, bool write)
  236. {
  237. uint64_t *addr = xsrc->esb_mmap + xive_source_esb_mgmt(xsrc, srcno) +
  238. offset;
  239. if (write) {
  240. *addr = cpu_to_be64(data);
  241. return -1;
  242. } else {
  243. /* Prevent the compiler from optimizing away the load */
  244. volatile uint64_t value = be64_to_cpu(*addr);
  245. return value;
  246. }
  247. }
  248. static uint8_t xive_esb_read(XiveSource *xsrc, int srcno, uint32_t offset)
  249. {
  250. return xive_esb_rw(xsrc, srcno, offset, 0, 0) & 0x3;
  251. }
  252. static void xive_esb_trigger(XiveSource *xsrc, int srcno)
  253. {
  254. uint64_t *addr = xsrc->esb_mmap + xive_source_esb_page(xsrc, srcno);
  255. *addr = 0x0;
  256. }
  257. uint64_t kvmppc_xive_esb_rw(XiveSource *xsrc, int srcno, uint32_t offset,
  258. uint64_t data, bool write)
  259. {
  260. if (write) {
  261. return xive_esb_rw(xsrc, srcno, offset, data, 1);
  262. }
  263. /*
  264. * Special Load EOI handling for LSI sources. Q bit is never set
  265. * and the interrupt should be re-triggered if the level is still
  266. * asserted.
  267. */
  268. if (xive_source_irq_is_lsi(xsrc, srcno) &&
  269. offset == XIVE_ESB_LOAD_EOI) {
  270. xive_esb_read(xsrc, srcno, XIVE_ESB_SET_PQ_00);
  271. if (xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
  272. xive_esb_trigger(xsrc, srcno);
  273. }
  274. return 0;
  275. } else {
  276. return xive_esb_rw(xsrc, srcno, offset, 0, 0);
  277. }
  278. }
  279. static void kvmppc_xive_source_get_state(XiveSource *xsrc)
  280. {
  281. SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
  282. int i;
  283. for (i = 0; i < xsrc->nr_irqs; i++) {
  284. uint8_t pq;
  285. if (!xive_eas_is_valid(&xive->eat[i])) {
  286. continue;
  287. }
  288. /* Perform a load without side effect to retrieve the PQ bits */
  289. pq = xive_esb_read(xsrc, i, XIVE_ESB_GET);
  290. /* and save PQ locally */
  291. xive_source_esb_set(xsrc, i, pq);
  292. }
  293. }
  294. void kvmppc_xive_source_set_irq(void *opaque, int srcno, int val)
  295. {
  296. XiveSource *xsrc = opaque;
  297. SpaprXive *xive = SPAPR_XIVE(xsrc->xive);
  298. struct kvm_irq_level args;
  299. int rc;
  300. /* The KVM XIVE device should be in use */
  301. assert(xive->fd != -1);
  302. args.irq = srcno;
  303. if (!xive_source_irq_is_lsi(xsrc, srcno)) {
  304. if (!val) {
  305. return;
  306. }
  307. args.level = KVM_INTERRUPT_SET;
  308. } else {
  309. if (val) {
  310. xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
  311. args.level = KVM_INTERRUPT_SET_LEVEL;
  312. } else {
  313. xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
  314. args.level = KVM_INTERRUPT_UNSET;
  315. }
  316. }
  317. rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
  318. if (rc < 0) {
  319. error_report("XIVE: kvm_irq_line() failed : %s", strerror(errno));
  320. }
  321. }
  322. /*
  323. * sPAPR XIVE interrupt controller (KVM)
  324. */
  325. void kvmppc_xive_get_queue_config(SpaprXive *xive, uint8_t end_blk,
  326. uint32_t end_idx, XiveEND *end,
  327. Error **errp)
  328. {
  329. struct kvm_ppc_xive_eq kvm_eq = { 0 };
  330. uint64_t kvm_eq_idx;
  331. uint8_t priority;
  332. uint32_t server;
  333. Error *local_err = NULL;
  334. assert(xive_end_is_valid(end));
  335. /* Encode the tuple (server, prio) as a KVM EQ index */
  336. spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
  337. kvm_eq_idx = priority << KVM_XIVE_EQ_PRIORITY_SHIFT &
  338. KVM_XIVE_EQ_PRIORITY_MASK;
  339. kvm_eq_idx |= server << KVM_XIVE_EQ_SERVER_SHIFT &
  340. KVM_XIVE_EQ_SERVER_MASK;
  341. kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_EQ_CONFIG, kvm_eq_idx,
  342. &kvm_eq, false, &local_err);
  343. if (local_err) {
  344. error_propagate(errp, local_err);
  345. return;
  346. }
  347. /*
  348. * The EQ index and toggle bit are updated by HW. These are the
  349. * only fields from KVM we want to update QEMU with. The other END
  350. * fields should already be in the QEMU END table.
  351. */
  352. end->w1 = xive_set_field32(END_W1_GENERATION, 0ul, kvm_eq.qtoggle) |
  353. xive_set_field32(END_W1_PAGE_OFF, 0ul, kvm_eq.qindex);
  354. }
  355. void kvmppc_xive_set_queue_config(SpaprXive *xive, uint8_t end_blk,
  356. uint32_t end_idx, XiveEND *end,
  357. Error **errp)
  358. {
  359. struct kvm_ppc_xive_eq kvm_eq = { 0 };
  360. uint64_t kvm_eq_idx;
  361. uint8_t priority;
  362. uint32_t server;
  363. Error *local_err = NULL;
  364. /*
  365. * Build the KVM state from the local END structure.
  366. */
  367. kvm_eq.flags = 0;
  368. if (xive_get_field32(END_W0_UCOND_NOTIFY, end->w0)) {
  369. kvm_eq.flags |= KVM_XIVE_EQ_ALWAYS_NOTIFY;
  370. }
  371. /*
  372. * If the hcall is disabling the EQ, set the size and page address
  373. * to zero. When migrating, only valid ENDs are taken into
  374. * account.
  375. */
  376. if (xive_end_is_valid(end)) {
  377. kvm_eq.qshift = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
  378. kvm_eq.qaddr = xive_end_qaddr(end);
  379. /*
  380. * The EQ toggle bit and index should only be relevant when
  381. * restoring the EQ state
  382. */
  383. kvm_eq.qtoggle = xive_get_field32(END_W1_GENERATION, end->w1);
  384. kvm_eq.qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  385. } else {
  386. kvm_eq.qshift = 0;
  387. kvm_eq.qaddr = 0;
  388. }
  389. /* Encode the tuple (server, prio) as a KVM EQ index */
  390. spapr_xive_end_to_target(end_blk, end_idx, &server, &priority);
  391. kvm_eq_idx = priority << KVM_XIVE_EQ_PRIORITY_SHIFT &
  392. KVM_XIVE_EQ_PRIORITY_MASK;
  393. kvm_eq_idx |= server << KVM_XIVE_EQ_SERVER_SHIFT &
  394. KVM_XIVE_EQ_SERVER_MASK;
  395. kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_EQ_CONFIG, kvm_eq_idx,
  396. &kvm_eq, true, &local_err);
  397. if (local_err) {
  398. error_propagate(errp, local_err);
  399. return;
  400. }
  401. }
  402. void kvmppc_xive_reset(SpaprXive *xive, Error **errp)
  403. {
  404. kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL, KVM_DEV_XIVE_RESET,
  405. NULL, true, errp);
  406. }
  407. static void kvmppc_xive_get_queues(SpaprXive *xive, Error **errp)
  408. {
  409. Error *local_err = NULL;
  410. int i;
  411. for (i = 0; i < xive->nr_ends; i++) {
  412. if (!xive_end_is_valid(&xive->endt[i])) {
  413. continue;
  414. }
  415. kvmppc_xive_get_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
  416. &xive->endt[i], &local_err);
  417. if (local_err) {
  418. error_propagate(errp, local_err);
  419. return;
  420. }
  421. }
  422. }
  423. /*
  424. * The primary goal of the XIVE VM change handler is to mark the EQ
  425. * pages dirty when all XIVE event notifications have stopped.
  426. *
  427. * Whenever the VM is stopped, the VM change handler sets the source
  428. * PQs to PENDING to stop the flow of events and to possibly catch a
  429. * triggered interrupt occuring while the VM is stopped. The previous
  430. * state is saved in anticipation of a migration. The XIVE controller
  431. * is then synced through KVM to flush any in-flight event
  432. * notification and stabilize the EQs.
  433. *
  434. * At this stage, we can mark the EQ page dirty and let a migration
  435. * sequence transfer the EQ pages to the destination, which is done
  436. * just after the stop state.
  437. *
  438. * The previous configuration of the sources is restored when the VM
  439. * runs again. If an interrupt was queued while the VM was stopped,
  440. * simply generate a trigger.
  441. */
  442. static void kvmppc_xive_change_state_handler(void *opaque, int running,
  443. RunState state)
  444. {
  445. SpaprXive *xive = opaque;
  446. XiveSource *xsrc = &xive->source;
  447. Error *local_err = NULL;
  448. int i;
  449. /*
  450. * Restore the sources to their initial state. This is called when
  451. * the VM resumes after a stop or a migration.
  452. */
  453. if (running) {
  454. for (i = 0; i < xsrc->nr_irqs; i++) {
  455. uint8_t pq;
  456. uint8_t old_pq;
  457. if (!xive_eas_is_valid(&xive->eat[i])) {
  458. continue;
  459. }
  460. pq = xive_source_esb_get(xsrc, i);
  461. old_pq = xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_00 + (pq << 8));
  462. /*
  463. * An interrupt was queued while the VM was stopped,
  464. * generate a trigger.
  465. */
  466. if (pq == XIVE_ESB_RESET && old_pq == XIVE_ESB_QUEUED) {
  467. xive_esb_trigger(xsrc, i);
  468. }
  469. }
  470. return;
  471. }
  472. /*
  473. * Mask the sources, to stop the flow of event notifications, and
  474. * save the PQs locally in the XiveSource object. The XiveSource
  475. * state will be collected later on by its vmstate handler if a
  476. * migration is in progress.
  477. */
  478. for (i = 0; i < xsrc->nr_irqs; i++) {
  479. uint8_t pq;
  480. if (!xive_eas_is_valid(&xive->eat[i])) {
  481. continue;
  482. }
  483. pq = xive_esb_read(xsrc, i, XIVE_ESB_GET);
  484. /*
  485. * PQ is set to PENDING to possibly catch a triggered
  486. * interrupt occuring while the VM is stopped (hotplug event
  487. * for instance) .
  488. */
  489. if (pq != XIVE_ESB_OFF) {
  490. pq = xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_10);
  491. }
  492. xive_source_esb_set(xsrc, i, pq);
  493. }
  494. /*
  495. * Sync the XIVE controller in KVM, to flush in-flight event
  496. * notification that should be enqueued in the EQs and mark the
  497. * XIVE EQ pages dirty to collect all updates.
  498. */
  499. kvm_device_access(xive->fd, KVM_DEV_XIVE_GRP_CTRL,
  500. KVM_DEV_XIVE_EQ_SYNC, NULL, true, &local_err);
  501. if (local_err) {
  502. error_report_err(local_err);
  503. return;
  504. }
  505. }
  506. void kvmppc_xive_synchronize_state(SpaprXive *xive, Error **errp)
  507. {
  508. /* The KVM XIVE device is not in use */
  509. if (xive->fd == -1) {
  510. return;
  511. }
  512. /*
  513. * When the VM is stopped, the sources are masked and the previous
  514. * state is saved in anticipation of a migration. We should not
  515. * synchronize the source state in that case else we will override
  516. * the saved state.
  517. */
  518. if (runstate_is_running()) {
  519. kvmppc_xive_source_get_state(&xive->source);
  520. }
  521. /* EAT: there is no extra state to query from KVM */
  522. /* ENDT */
  523. kvmppc_xive_get_queues(xive, errp);
  524. }
  525. /*
  526. * The SpaprXive 'pre_save' method is called by the vmstate handler of
  527. * the SpaprXive model, after the XIVE controller is synced in the VM
  528. * change handler.
  529. */
  530. int kvmppc_xive_pre_save(SpaprXive *xive)
  531. {
  532. Error *local_err = NULL;
  533. /* The KVM XIVE device is not in use */
  534. if (xive->fd == -1) {
  535. return 0;
  536. }
  537. /* EAT: there is no extra state to query from KVM */
  538. /* ENDT */
  539. kvmppc_xive_get_queues(xive, &local_err);
  540. if (local_err) {
  541. error_report_err(local_err);
  542. return -1;
  543. }
  544. return 0;
  545. }
  546. /*
  547. * The SpaprXive 'post_load' method is not called by a vmstate
  548. * handler. It is called at the sPAPR machine level at the end of the
  549. * migration sequence by the sPAPR IRQ backend 'post_load' method,
  550. * when all XIVE states have been transferred and loaded.
  551. */
  552. int kvmppc_xive_post_load(SpaprXive *xive, int version_id)
  553. {
  554. Error *local_err = NULL;
  555. CPUState *cs;
  556. int i;
  557. /* The KVM XIVE device should be in use */
  558. assert(xive->fd != -1);
  559. /* Restore the ENDT first. The targetting depends on it. */
  560. for (i = 0; i < xive->nr_ends; i++) {
  561. if (!xive_end_is_valid(&xive->endt[i])) {
  562. continue;
  563. }
  564. kvmppc_xive_set_queue_config(xive, SPAPR_XIVE_BLOCK_ID, i,
  565. &xive->endt[i], &local_err);
  566. if (local_err) {
  567. error_report_err(local_err);
  568. return -1;
  569. }
  570. }
  571. /* Restore the EAT */
  572. for (i = 0; i < xive->nr_irqs; i++) {
  573. if (!xive_eas_is_valid(&xive->eat[i])) {
  574. continue;
  575. }
  576. /*
  577. * We can only restore the source config if the source has been
  578. * previously set in KVM. Since we don't do that for all interrupts
  579. * at reset time anymore, let's do it now.
  580. */
  581. kvmppc_xive_source_reset_one(&xive->source, i, &local_err);
  582. if (local_err) {
  583. error_report_err(local_err);
  584. return -1;
  585. }
  586. kvmppc_xive_set_source_config(xive, i, &xive->eat[i], &local_err);
  587. if (local_err) {
  588. error_report_err(local_err);
  589. return -1;
  590. }
  591. }
  592. /*
  593. * Restore the thread interrupt contexts of initial CPUs.
  594. *
  595. * The context of hotplugged CPUs is restored later, by the
  596. * 'post_load' handler of the XiveTCTX model because they are not
  597. * available at the time the SpaprXive 'post_load' method is
  598. * called. We can not restore the context of all CPUs in the
  599. * 'post_load' handler of XiveTCTX because the machine is not
  600. * necessarily connected to the KVM device at that time.
  601. */
  602. CPU_FOREACH(cs) {
  603. PowerPCCPU *cpu = POWERPC_CPU(cs);
  604. kvmppc_xive_cpu_set_state(spapr_cpu_state(cpu)->tctx, &local_err);
  605. if (local_err) {
  606. error_report_err(local_err);
  607. return -1;
  608. }
  609. }
  610. /* The source states will be restored when the machine starts running */
  611. return 0;
  612. }
  613. static void *kvmppc_xive_mmap(SpaprXive *xive, int pgoff, size_t len,
  614. Error **errp)
  615. {
  616. void *addr;
  617. uint32_t page_shift = 16; /* TODO: fix page_shift */
  618. addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, xive->fd,
  619. pgoff << page_shift);
  620. if (addr == MAP_FAILED) {
  621. error_setg_errno(errp, errno, "XIVE: unable to set memory mapping");
  622. return NULL;
  623. }
  624. return addr;
  625. }
  626. /*
  627. * All the XIVE memory regions are now backed by mappings from the KVM
  628. * XIVE device.
  629. */
  630. int kvmppc_xive_connect(SpaprInterruptController *intc, Error **errp)
  631. {
  632. SpaprXive *xive = SPAPR_XIVE(intc);
  633. XiveSource *xsrc = &xive->source;
  634. Error *local_err = NULL;
  635. size_t esb_len = (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
  636. size_t tima_len = 4ull << TM_SHIFT;
  637. CPUState *cs;
  638. /*
  639. * The KVM XIVE device already in use. This is the case when
  640. * rebooting under the XIVE-only interrupt mode.
  641. */
  642. if (xive->fd != -1) {
  643. return 0;
  644. }
  645. if (!kvmppc_has_cap_xive()) {
  646. error_setg(errp, "IRQ_XIVE capability must be present for KVM");
  647. return -1;
  648. }
  649. /* First, create the KVM XIVE device */
  650. xive->fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_XIVE, false);
  651. if (xive->fd < 0) {
  652. error_setg_errno(errp, -xive->fd, "XIVE: error creating KVM device");
  653. return -1;
  654. }
  655. /*
  656. * 1. Source ESB pages - KVM mapping
  657. */
  658. xsrc->esb_mmap = kvmppc_xive_mmap(xive, KVM_XIVE_ESB_PAGE_OFFSET, esb_len,
  659. &local_err);
  660. if (local_err) {
  661. goto fail;
  662. }
  663. memory_region_init_ram_device_ptr(&xsrc->esb_mmio_kvm, OBJECT(xsrc),
  664. "xive.esb", esb_len, xsrc->esb_mmap);
  665. memory_region_add_subregion_overlap(&xsrc->esb_mmio, 0,
  666. &xsrc->esb_mmio_kvm, 1);
  667. /*
  668. * 2. END ESB pages (No KVM support yet)
  669. */
  670. /*
  671. * 3. TIMA pages - KVM mapping
  672. */
  673. xive->tm_mmap = kvmppc_xive_mmap(xive, KVM_XIVE_TIMA_PAGE_OFFSET, tima_len,
  674. &local_err);
  675. if (local_err) {
  676. goto fail;
  677. }
  678. memory_region_init_ram_device_ptr(&xive->tm_mmio_kvm, OBJECT(xive),
  679. "xive.tima", tima_len, xive->tm_mmap);
  680. memory_region_add_subregion_overlap(&xive->tm_mmio, 0,
  681. &xive->tm_mmio_kvm, 1);
  682. xive->change = qemu_add_vm_change_state_handler(
  683. kvmppc_xive_change_state_handler, xive);
  684. /* Connect the presenters to the initial VCPUs of the machine */
  685. CPU_FOREACH(cs) {
  686. PowerPCCPU *cpu = POWERPC_CPU(cs);
  687. kvmppc_xive_cpu_connect(spapr_cpu_state(cpu)->tctx, &local_err);
  688. if (local_err) {
  689. goto fail;
  690. }
  691. }
  692. /* Update the KVM sources */
  693. kvmppc_xive_source_reset(xsrc, &local_err);
  694. if (local_err) {
  695. goto fail;
  696. }
  697. kvm_kernel_irqchip = true;
  698. kvm_msi_via_irqfd_allowed = true;
  699. kvm_gsi_direct_mapping = true;
  700. return 0;
  701. fail:
  702. error_propagate(errp, local_err);
  703. kvmppc_xive_disconnect(intc);
  704. return -1;
  705. }
  706. void kvmppc_xive_disconnect(SpaprInterruptController *intc)
  707. {
  708. SpaprXive *xive = SPAPR_XIVE(intc);
  709. XiveSource *xsrc;
  710. size_t esb_len;
  711. /* The KVM XIVE device is not in use */
  712. if (!xive || xive->fd == -1) {
  713. return;
  714. }
  715. /* Clear the KVM mapping */
  716. xsrc = &xive->source;
  717. esb_len = (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
  718. if (xsrc->esb_mmap) {
  719. memory_region_del_subregion(&xsrc->esb_mmio, &xsrc->esb_mmio_kvm);
  720. object_unparent(OBJECT(&xsrc->esb_mmio_kvm));
  721. munmap(xsrc->esb_mmap, esb_len);
  722. xsrc->esb_mmap = NULL;
  723. }
  724. if (xive->tm_mmap) {
  725. memory_region_del_subregion(&xive->tm_mmio, &xive->tm_mmio_kvm);
  726. object_unparent(OBJECT(&xive->tm_mmio_kvm));
  727. munmap(xive->tm_mmap, 4ull << TM_SHIFT);
  728. xive->tm_mmap = NULL;
  729. }
  730. /*
  731. * When the KVM device fd is closed, the KVM device is destroyed
  732. * and removed from the list of devices of the VM. The VCPU
  733. * presenters are also detached from the device.
  734. */
  735. if (xive->fd != -1) {
  736. close(xive->fd);
  737. xive->fd = -1;
  738. }
  739. kvm_kernel_irqchip = false;
  740. kvm_msi_via_irqfd_allowed = false;
  741. kvm_gsi_direct_mapping = false;
  742. /* Clear the local list of presenter (hotplug) */
  743. kvm_cpu_disable_all();
  744. /* VM Change state handler is not needed anymore */
  745. if (xive->change) {
  746. qemu_del_vm_change_state_handler(xive->change);
  747. xive->change = NULL;
  748. }
  749. }