xics_kvm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
  3. *
  4. * PAPR Virtualized Interrupt System, aka ICS/ICP aka xics, in-kernel emulation
  5. *
  6. * Copyright (c) 2013 David Gibson, IBM Corporation.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "qemu/osdep.h"
  28. #include "qapi/error.h"
  29. #include "qemu-common.h"
  30. #include "cpu.h"
  31. #include "trace.h"
  32. #include "sysemu/kvm.h"
  33. #include "hw/ppc/spapr.h"
  34. #include "hw/ppc/spapr_cpu_core.h"
  35. #include "hw/ppc/xics.h"
  36. #include "hw/ppc/xics_spapr.h"
  37. #include "kvm_ppc.h"
  38. #include "qemu/config-file.h"
  39. #include "qemu/error-report.h"
  40. #include <sys/ioctl.h>
  41. static int kernel_xics_fd = -1;
  42. typedef struct KVMEnabledICP {
  43. unsigned long vcpu_id;
  44. QLIST_ENTRY(KVMEnabledICP) node;
  45. } KVMEnabledICP;
  46. static QLIST_HEAD(, KVMEnabledICP)
  47. kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
  48. static void kvm_disable_icps(void)
  49. {
  50. KVMEnabledICP *enabled_icp, *next;
  51. QLIST_FOREACH_SAFE(enabled_icp, &kvm_enabled_icps, node, next) {
  52. QLIST_REMOVE(enabled_icp, node);
  53. g_free(enabled_icp);
  54. }
  55. }
  56. /*
  57. * ICP-KVM
  58. */
  59. void icp_get_kvm_state(ICPState *icp)
  60. {
  61. uint64_t state;
  62. int ret;
  63. /* The KVM XICS device is not in use */
  64. if (kernel_xics_fd == -1) {
  65. return;
  66. }
  67. /* ICP for this CPU thread is not in use, exiting */
  68. if (!icp->cs) {
  69. return;
  70. }
  71. ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
  72. if (ret != 0) {
  73. error_report("Unable to retrieve KVM interrupt controller state"
  74. " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
  75. exit(1);
  76. }
  77. icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
  78. icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
  79. & KVM_REG_PPC_ICP_MFRR_MASK;
  80. icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
  81. & KVM_REG_PPC_ICP_PPRI_MASK;
  82. }
  83. static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
  84. {
  85. icp_get_kvm_state(arg.host_ptr);
  86. }
  87. void icp_synchronize_state(ICPState *icp)
  88. {
  89. if (icp->cs) {
  90. run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
  91. }
  92. }
  93. int icp_set_kvm_state(ICPState *icp, Error **errp)
  94. {
  95. uint64_t state;
  96. int ret;
  97. /* The KVM XICS device is not in use */
  98. if (kernel_xics_fd == -1) {
  99. return 0;
  100. }
  101. /* ICP for this CPU thread is not in use, exiting */
  102. if (!icp->cs) {
  103. return 0;
  104. }
  105. state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
  106. | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
  107. | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
  108. ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
  109. if (ret < 0) {
  110. error_setg_errno(errp, -ret,
  111. "Unable to restore KVM interrupt controller state (0x%"
  112. PRIx64 ") for CPU %ld", state,
  113. kvm_arch_vcpu_id(icp->cs));
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. void icp_kvm_realize(DeviceState *dev, Error **errp)
  119. {
  120. ICPState *icp = ICP(dev);
  121. CPUState *cs;
  122. KVMEnabledICP *enabled_icp;
  123. unsigned long vcpu_id;
  124. int ret;
  125. /* The KVM XICS device is not in use */
  126. if (kernel_xics_fd == -1) {
  127. return;
  128. }
  129. cs = icp->cs;
  130. vcpu_id = kvm_arch_vcpu_id(cs);
  131. /*
  132. * If we are reusing a parked vCPU fd corresponding to the CPU
  133. * which was hot-removed earlier we don't have to renable
  134. * KVM_CAP_IRQ_XICS capability again.
  135. */
  136. QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
  137. if (enabled_icp->vcpu_id == vcpu_id) {
  138. return;
  139. }
  140. }
  141. ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
  142. if (ret < 0) {
  143. error_setg(errp, "Unable to connect CPU%ld to kernel XICS: %s", vcpu_id,
  144. strerror(errno));
  145. return;
  146. }
  147. enabled_icp = g_malloc(sizeof(*enabled_icp));
  148. enabled_icp->vcpu_id = vcpu_id;
  149. QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
  150. }
  151. /*
  152. * ICS-KVM
  153. */
  154. void ics_get_kvm_state(ICSState *ics)
  155. {
  156. uint64_t state;
  157. int i;
  158. /* The KVM XICS device is not in use */
  159. if (kernel_xics_fd == -1) {
  160. return;
  161. }
  162. for (i = 0; i < ics->nr_irqs; i++) {
  163. ICSIRQState *irq = &ics->irqs[i];
  164. if (ics_irq_free(ics, i)) {
  165. continue;
  166. }
  167. kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
  168. i + ics->offset, &state, false, &error_fatal);
  169. irq->server = state & KVM_XICS_DESTINATION_MASK;
  170. irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
  171. & KVM_XICS_PRIORITY_MASK;
  172. /*
  173. * To be consistent with the software emulation in xics.c, we
  174. * split out the masked state + priority that we get from the
  175. * kernel into 'current priority' (0xff if masked) and
  176. * 'saved priority' (if masked, this is the priority the
  177. * interrupt had before it was masked). Masking and unmasking
  178. * are done with the ibm,int-off and ibm,int-on RTAS calls.
  179. */
  180. if (state & KVM_XICS_MASKED) {
  181. irq->priority = 0xff;
  182. } else {
  183. irq->priority = irq->saved_priority;
  184. }
  185. irq->status = 0;
  186. if (state & KVM_XICS_PENDING) {
  187. if (state & KVM_XICS_LEVEL_SENSITIVE) {
  188. irq->status |= XICS_STATUS_ASSERTED;
  189. } else {
  190. /*
  191. * A pending edge-triggered interrupt (or MSI)
  192. * must have been rejected previously when we
  193. * first detected it and tried to deliver it,
  194. * so mark it as pending and previously rejected
  195. * for consistency with how xics.c works.
  196. */
  197. irq->status |= XICS_STATUS_MASKED_PENDING
  198. | XICS_STATUS_REJECTED;
  199. }
  200. }
  201. if (state & KVM_XICS_PRESENTED) {
  202. irq->status |= XICS_STATUS_PRESENTED;
  203. }
  204. if (state & KVM_XICS_QUEUED) {
  205. irq->status |= XICS_STATUS_QUEUED;
  206. }
  207. }
  208. }
  209. void ics_synchronize_state(ICSState *ics)
  210. {
  211. ics_get_kvm_state(ics);
  212. }
  213. int ics_set_kvm_state_one(ICSState *ics, int srcno, Error **errp)
  214. {
  215. uint64_t state;
  216. ICSIRQState *irq = &ics->irqs[srcno];
  217. int ret;
  218. /* The KVM XICS device is not in use */
  219. if (kernel_xics_fd == -1) {
  220. return 0;
  221. }
  222. state = irq->server;
  223. state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
  224. << KVM_XICS_PRIORITY_SHIFT;
  225. if (irq->priority != irq->saved_priority) {
  226. assert(irq->priority == 0xff);
  227. }
  228. if (irq->priority == 0xff) {
  229. state |= KVM_XICS_MASKED;
  230. }
  231. if (irq->flags & XICS_FLAGS_IRQ_LSI) {
  232. state |= KVM_XICS_LEVEL_SENSITIVE;
  233. if (irq->status & XICS_STATUS_ASSERTED) {
  234. state |= KVM_XICS_PENDING;
  235. }
  236. } else {
  237. if (irq->status & XICS_STATUS_MASKED_PENDING) {
  238. state |= KVM_XICS_PENDING;
  239. }
  240. }
  241. if (irq->status & XICS_STATUS_PRESENTED) {
  242. state |= KVM_XICS_PRESENTED;
  243. }
  244. if (irq->status & XICS_STATUS_QUEUED) {
  245. state |= KVM_XICS_QUEUED;
  246. }
  247. ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
  248. srcno + ics->offset, &state, true, errp);
  249. if (ret < 0) {
  250. return ret;
  251. }
  252. return 0;
  253. }
  254. int ics_set_kvm_state(ICSState *ics, Error **errp)
  255. {
  256. int i;
  257. /* The KVM XICS device is not in use */
  258. if (kernel_xics_fd == -1) {
  259. return 0;
  260. }
  261. for (i = 0; i < ics->nr_irqs; i++) {
  262. Error *local_err = NULL;
  263. int ret;
  264. if (ics_irq_free(ics, i)) {
  265. continue;
  266. }
  267. ret = ics_set_kvm_state_one(ics, i, &local_err);
  268. if (ret < 0) {
  269. error_propagate(errp, local_err);
  270. return ret;
  271. }
  272. }
  273. return 0;
  274. }
  275. void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
  276. {
  277. struct kvm_irq_level args;
  278. int rc;
  279. /* The KVM XICS device should be in use */
  280. assert(kernel_xics_fd != -1);
  281. args.irq = srcno + ics->offset;
  282. if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
  283. if (!val) {
  284. return;
  285. }
  286. args.level = KVM_INTERRUPT_SET;
  287. } else {
  288. args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
  289. }
  290. rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
  291. if (rc < 0) {
  292. perror("kvm_irq_line");
  293. }
  294. }
  295. int xics_kvm_connect(SpaprInterruptController *intc, Error **errp)
  296. {
  297. ICSState *ics = ICS_SPAPR(intc);
  298. int rc;
  299. CPUState *cs;
  300. Error *local_err = NULL;
  301. /*
  302. * The KVM XICS device already in use. This is the case when
  303. * rebooting under the XICS-only interrupt mode.
  304. */
  305. if (kernel_xics_fd != -1) {
  306. return 0;
  307. }
  308. if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
  309. error_setg(errp,
  310. "KVM and IRQ_XICS capability must be present for in-kernel XICS");
  311. return -1;
  312. }
  313. rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
  314. if (rc < 0) {
  315. error_setg_errno(&local_err, -rc,
  316. "kvmppc_define_rtas_kernel_token: ibm,set-xive");
  317. goto fail;
  318. }
  319. rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
  320. if (rc < 0) {
  321. error_setg_errno(&local_err, -rc,
  322. "kvmppc_define_rtas_kernel_token: ibm,get-xive");
  323. goto fail;
  324. }
  325. rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
  326. if (rc < 0) {
  327. error_setg_errno(&local_err, -rc,
  328. "kvmppc_define_rtas_kernel_token: ibm,int-on");
  329. goto fail;
  330. }
  331. rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
  332. if (rc < 0) {
  333. error_setg_errno(&local_err, -rc,
  334. "kvmppc_define_rtas_kernel_token: ibm,int-off");
  335. goto fail;
  336. }
  337. /* Create the KVM XICS device */
  338. rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
  339. if (rc < 0) {
  340. error_setg_errno(&local_err, -rc, "Error on KVM_CREATE_DEVICE for XICS");
  341. goto fail;
  342. }
  343. kernel_xics_fd = rc;
  344. kvm_kernel_irqchip = true;
  345. kvm_msi_via_irqfd_allowed = true;
  346. kvm_gsi_direct_mapping = true;
  347. /* Create the presenters */
  348. CPU_FOREACH(cs) {
  349. PowerPCCPU *cpu = POWERPC_CPU(cs);
  350. icp_kvm_realize(DEVICE(spapr_cpu_state(cpu)->icp), &local_err);
  351. if (local_err) {
  352. goto fail;
  353. }
  354. }
  355. /* Update the KVM sources */
  356. ics_set_kvm_state(ics, &local_err);
  357. if (local_err) {
  358. goto fail;
  359. }
  360. /* Connect the presenters to the initial VCPUs of the machine */
  361. CPU_FOREACH(cs) {
  362. PowerPCCPU *cpu = POWERPC_CPU(cs);
  363. icp_set_kvm_state(spapr_cpu_state(cpu)->icp, &local_err);
  364. if (local_err) {
  365. goto fail;
  366. }
  367. }
  368. return 0;
  369. fail:
  370. error_propagate(errp, local_err);
  371. xics_kvm_disconnect(intc);
  372. return -1;
  373. }
  374. void xics_kvm_disconnect(SpaprInterruptController *intc)
  375. {
  376. /*
  377. * Only on P9 using the XICS-on XIVE KVM device:
  378. *
  379. * When the KVM device fd is closed, the device is destroyed and
  380. * removed from the list of devices of the VM. The VCPU presenters
  381. * are also detached from the device.
  382. */
  383. if (kernel_xics_fd != -1) {
  384. close(kernel_xics_fd);
  385. kernel_xics_fd = -1;
  386. }
  387. kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
  388. kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
  389. kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
  390. kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
  391. kvm_kernel_irqchip = false;
  392. kvm_msi_via_irqfd_allowed = false;
  393. kvm_gsi_direct_mapping = false;
  394. /* Clear the presenter from the VCPUs */
  395. kvm_disable_icps();
  396. }
  397. /*
  398. * This is a heuristic to detect older KVMs on POWER9 hosts that don't
  399. * support destruction of a KVM XICS device while the VM is running.
  400. * Required to start a spapr machine with ic-mode=dual,kernel-irqchip=on.
  401. */
  402. bool xics_kvm_has_broken_disconnect(SpaprMachineState *spapr)
  403. {
  404. int rc;
  405. rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
  406. if (rc < 0) {
  407. /*
  408. * The error is ignored on purpose. The KVM XICS setup code
  409. * will catch it again anyway. The goal here is to see if
  410. * close() actually destroys the device or not.
  411. */
  412. return false;
  413. }
  414. close(rc);
  415. rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
  416. if (rc >= 0) {
  417. close(rc);
  418. return false;
  419. }
  420. return errno == EEXIST;
  421. }