2
0

xics_kvm.c 14 KB

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