spapr_xive_kvm.c 25 KB

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