hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/arch/powerpc/kvm/book3s_xics.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright 2012 Michael Ellerman, IBM Corporation.
34 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation.
4
- *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License, version 2, as
7
- * published by the Free Software Foundation.
85 */
96
107 #include <linux/kernel.h>
....@@ -310,7 +307,7 @@
310307 */
311308 if (new.out_ee) {
312309 kvmppc_book3s_queue_irqprio(icp->vcpu,
313
- BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
310
+ BOOK3S_INTERRUPT_EXTERNAL);
314311 if (!change_self)
315312 kvmppc_fast_vcpu_kick(icp->vcpu);
316313 }
....@@ -593,8 +590,7 @@
593590 u32 xirr;
594591
595592 /* First, remove EE from the processor */
596
- kvmppc_book3s_dequeue_irqprio(icp->vcpu,
597
- BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
593
+ kvmppc_book3s_dequeue_irqprio(icp->vcpu, BOOK3S_INTERRUPT_EXTERNAL);
598594
599595 /*
600596 * ICP State: Accept_Interrupt
....@@ -754,8 +750,7 @@
754750 * We can remove EE from the current processor, the update
755751 * transaction will set it again if needed
756752 */
757
- kvmppc_book3s_dequeue_irqprio(icp->vcpu,
758
- BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
753
+ kvmppc_book3s_dequeue_irqprio(icp->vcpu, BOOK3S_INTERRUPT_EXTERNAL);
759754
760755 do {
761756 old_state = new_state = READ_ONCE(icp->state);
....@@ -832,7 +827,7 @@
832827 *
833828 * Note: If EOI is incorrectly used by SW to lower the CPPR
834829 * value (ie more favored), we do not check for rejection of
835
- * a pending interrupt, this is a SW error and PAPR sepcifies
830
+ * a pending interrupt, this is a SW error and PAPR specifies
836831 * that we don't have to deal with it.
837832 *
838833 * The sending of an EOI to the ICS is handled after the
....@@ -1017,17 +1012,7 @@
10171012 return 0;
10181013 }
10191014
1020
-static int xics_debug_open(struct inode *inode, struct file *file)
1021
-{
1022
- return single_open(file, xics_debug_show, inode->i_private);
1023
-}
1024
-
1025
-static const struct file_operations xics_debug_fops = {
1026
- .open = xics_debug_open,
1027
- .read = seq_read,
1028
- .llseek = seq_lseek,
1029
- .release = single_release,
1030
-};
1015
+DEFINE_SHOW_ATTRIBUTE(xics_debug);
10311016
10321017 static void xics_debugfs_init(struct kvmppc_xics *xics)
10331018 {
....@@ -1167,8 +1152,7 @@
11671152 * Deassert the CPU interrupt request.
11681153 * icp_try_update will reassert it if necessary.
11691154 */
1170
- kvmppc_book3s_dequeue_irqprio(icp->vcpu,
1171
- BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
1155
+ kvmppc_book3s_dequeue_irqprio(icp->vcpu, BOOK3S_INTERRUPT_EXTERNAL);
11721156
11731157 /*
11741158 * Note that if we displace an interrupt from old_state.xisr,
....@@ -1350,50 +1334,101 @@
13501334 return -ENXIO;
13511335 }
13521336
1353
-static void kvmppc_xics_free(struct kvm_device *dev)
1337
+/*
1338
+ * Called when device fd is closed. kvm->lock is held.
1339
+ */
1340
+static void kvmppc_xics_release(struct kvm_device *dev)
13541341 {
13551342 struct kvmppc_xics *xics = dev->private;
13561343 int i;
13571344 struct kvm *kvm = xics->kvm;
1345
+ struct kvm_vcpu *vcpu;
1346
+
1347
+ pr_devel("Releasing xics device\n");
1348
+
1349
+ /*
1350
+ * Since this is the device release function, we know that
1351
+ * userspace does not have any open fd referring to the
1352
+ * device. Therefore there can not be any of the device
1353
+ * attribute set/get functions being executed concurrently,
1354
+ * and similarly, the connect_vcpu and set/clr_mapped
1355
+ * functions also cannot be being executed.
1356
+ */
13581357
13591358 debugfs_remove(xics->dentry);
1359
+
1360
+ /*
1361
+ * We should clean up the vCPU interrupt presenters first.
1362
+ */
1363
+ kvm_for_each_vcpu(i, vcpu, kvm) {
1364
+ /*
1365
+ * Take vcpu->mutex to ensure that no one_reg get/set ioctl
1366
+ * (i.e. kvmppc_xics_[gs]et_icp) can be done concurrently.
1367
+ * Holding the vcpu->mutex also means that execution is
1368
+ * excluded for the vcpu until the ICP was freed. When the vcpu
1369
+ * can execute again, vcpu->arch.icp and vcpu->arch.irq_type
1370
+ * have been cleared and the vcpu will not be going into the
1371
+ * XICS code anymore.
1372
+ */
1373
+ mutex_lock(&vcpu->mutex);
1374
+ kvmppc_xics_free_icp(vcpu);
1375
+ mutex_unlock(&vcpu->mutex);
1376
+ }
13601377
13611378 if (kvm)
13621379 kvm->arch.xics = NULL;
13631380
1364
- for (i = 0; i <= xics->max_icsid; i++)
1381
+ for (i = 0; i <= xics->max_icsid; i++) {
13651382 kfree(xics->ics[i]);
1366
- kfree(xics);
1383
+ xics->ics[i] = NULL;
1384
+ }
1385
+ /*
1386
+ * A reference of the kvmppc_xics pointer is now kept under
1387
+ * the xics_device pointer of the machine for reuse. It is
1388
+ * freed when the VM is destroyed for now until we fix all the
1389
+ * execution paths.
1390
+ */
13671391 kfree(dev);
1392
+}
1393
+
1394
+static struct kvmppc_xics *kvmppc_xics_get_device(struct kvm *kvm)
1395
+{
1396
+ struct kvmppc_xics **kvm_xics_device = &kvm->arch.xics_device;
1397
+ struct kvmppc_xics *xics = *kvm_xics_device;
1398
+
1399
+ if (!xics) {
1400
+ xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1401
+ *kvm_xics_device = xics;
1402
+ } else {
1403
+ memset(xics, 0, sizeof(*xics));
1404
+ }
1405
+
1406
+ return xics;
13681407 }
13691408
13701409 static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
13711410 {
13721411 struct kvmppc_xics *xics;
13731412 struct kvm *kvm = dev->kvm;
1374
- int ret = 0;
13751413
1376
- xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1414
+ pr_devel("Creating xics for partition\n");
1415
+
1416
+ /* Already there ? */
1417
+ if (kvm->arch.xics)
1418
+ return -EEXIST;
1419
+
1420
+ xics = kvmppc_xics_get_device(kvm);
13771421 if (!xics)
13781422 return -ENOMEM;
13791423
13801424 dev->private = xics;
13811425 xics->dev = dev;
13821426 xics->kvm = kvm;
1383
-
1384
- /* Already there ? */
1385
- if (kvm->arch.xics)
1386
- ret = -EEXIST;
1387
- else
1388
- kvm->arch.xics = xics;
1389
-
1390
- if (ret) {
1391
- kfree(xics);
1392
- return ret;
1393
- }
1427
+ kvm->arch.xics = xics;
13941428
13951429 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1396
- if (cpu_has_feature(CPU_FTR_ARCH_206)) {
1430
+ if (cpu_has_feature(CPU_FTR_ARCH_206) &&
1431
+ cpu_has_feature(CPU_FTR_HVMODE)) {
13971432 /* Enable real mode support */
13981433 xics->real_mode = ENABLE_REALMODE;
13991434 xics->real_mode_dbg = DEBUG_REALMODE;
....@@ -1414,7 +1449,7 @@
14141449 .name = "kvm-xics",
14151450 .create = kvmppc_xics_create,
14161451 .init = kvmppc_xics_init,
1417
- .destroy = kvmppc_xics_free,
1452
+ .release = kvmppc_xics_release,
14181453 .set_attr = xics_set_attr,
14191454 .get_attr = xics_get_attr,
14201455 .has_attr = xics_has_attr,
....@@ -1430,7 +1465,7 @@
14301465 return -EPERM;
14311466 if (xics->kvm != vcpu->kvm)
14321467 return -EPERM;
1433
- if (vcpu->arch.irq_type)
1468
+ if (vcpu->arch.irq_type != KVMPPC_IRQ_DEFAULT)
14341469 return -EBUSY;
14351470
14361471 r = kvmppc_xics_create_icp(vcpu, xcpu);