2
0

spapr_xive_kvm.c 24 KB

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