summaryrefslogtreecommitdiffstats
path: root/virt
diff options
context:
space:
mode:
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/arm/arch_timer.c29
-rw-r--r--virt/kvm/arm/vgic-v2.c16
-rw-r--r--virt/kvm/arm/vgic-v3.c21
-rw-r--r--virt/kvm/arm/vgic.c427
-rw-r--r--virt/kvm/irqchip.c8
-rw-r--r--virt/kvm/kvm_main.c62
6 files changed, 508 insertions, 55 deletions
diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 98c95f2fcba4..76e38d231e99 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -64,10 +64,10 @@ static void kvm_timer_inject_irq(struct kvm_vcpu *vcpu)
64 int ret; 64 int ret;
65 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 65 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
66 66
67 timer->cntv_ctl |= ARCH_TIMER_CTRL_IT_MASK; 67 kvm_vgic_set_phys_irq_active(timer->map, true);
68 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, 68 ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
69 timer->irq->irq, 69 timer->map,
70 timer->irq->level); 70 timer->irq->level);
71 WARN_ON(ret); 71 WARN_ON(ret);
72} 72}
73 73
@@ -117,7 +117,8 @@ bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
117 cycle_t cval, now; 117 cycle_t cval, now;
118 118
119 if ((timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) || 119 if ((timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) ||
120 !(timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE)) 120 !(timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE) ||
121 kvm_vgic_get_phys_irq_active(timer->map))
121 return false; 122 return false;
122 123
123 cval = timer->cntv_cval; 124 cval = timer->cntv_cval;
@@ -184,10 +185,11 @@ void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
184 timer_arm(timer, ns); 185 timer_arm(timer, ns);
185} 186}
186 187
187void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, 188int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
188 const struct kvm_irq_level *irq) 189 const struct kvm_irq_level *irq)
189{ 190{
190 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 191 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
192 struct irq_phys_map *map;
191 193
192 /* 194 /*
193 * The vcpu timer irq number cannot be determined in 195 * The vcpu timer irq number cannot be determined in
@@ -196,6 +198,17 @@ void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
196 * vcpu timer irq number when the vcpu is reset. 198 * vcpu timer irq number when the vcpu is reset.
197 */ 199 */
198 timer->irq = irq; 200 timer->irq = irq;
201
202 /*
203 * Tell the VGIC that the virtual interrupt is tied to a
204 * physical interrupt. We do that once per VCPU.
205 */
206 map = kvm_vgic_map_phys_irq(vcpu, irq->irq, host_vtimer_irq);
207 if (WARN_ON(IS_ERR(map)))
208 return PTR_ERR(map);
209
210 timer->map = map;
211 return 0;
199} 212}
200 213
201void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) 214void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
@@ -335,6 +348,8 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
335 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 348 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
336 349
337 timer_disarm(timer); 350 timer_disarm(timer);
351 if (timer->map)
352 kvm_vgic_unmap_phys_irq(vcpu, timer->map);
338} 353}
339 354
340void kvm_timer_enable(struct kvm *kvm) 355void kvm_timer_enable(struct kvm *kvm)
diff --git a/virt/kvm/arm/vgic-v2.c b/virt/kvm/arm/vgic-v2.c
index f9b9c7c51372..8d7b04db8471 100644
--- a/virt/kvm/arm/vgic-v2.c
+++ b/virt/kvm/arm/vgic-v2.c
@@ -48,6 +48,10 @@ static struct vgic_lr vgic_v2_get_lr(const struct kvm_vcpu *vcpu, int lr)
48 lr_desc.state |= LR_STATE_ACTIVE; 48 lr_desc.state |= LR_STATE_ACTIVE;
49 if (val & GICH_LR_EOI) 49 if (val & GICH_LR_EOI)
50 lr_desc.state |= LR_EOI_INT; 50 lr_desc.state |= LR_EOI_INT;
51 if (val & GICH_LR_HW) {
52 lr_desc.state |= LR_HW;
53 lr_desc.hwirq = (val & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT;
54 }
51 55
52 return lr_desc; 56 return lr_desc;
53} 57}
@@ -55,7 +59,9 @@ static struct vgic_lr vgic_v2_get_lr(const struct kvm_vcpu *vcpu, int lr)
55static void vgic_v2_set_lr(struct kvm_vcpu *vcpu, int lr, 59static void vgic_v2_set_lr(struct kvm_vcpu *vcpu, int lr,
56 struct vgic_lr lr_desc) 60 struct vgic_lr lr_desc)
57{ 61{
58 u32 lr_val = (lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT) | lr_desc.irq; 62 u32 lr_val;
63
64 lr_val = lr_desc.irq;
59 65
60 if (lr_desc.state & LR_STATE_PENDING) 66 if (lr_desc.state & LR_STATE_PENDING)
61 lr_val |= GICH_LR_PENDING_BIT; 67 lr_val |= GICH_LR_PENDING_BIT;
@@ -64,6 +70,14 @@ static void vgic_v2_set_lr(struct kvm_vcpu *vcpu, int lr,
64 if (lr_desc.state & LR_EOI_INT) 70 if (lr_desc.state & LR_EOI_INT)
65 lr_val |= GICH_LR_EOI; 71 lr_val |= GICH_LR_EOI;
66 72
73 if (lr_desc.state & LR_HW) {
74 lr_val |= GICH_LR_HW;
75 lr_val |= (u32)lr_desc.hwirq << GICH_LR_PHYSID_CPUID_SHIFT;
76 }
77
78 if (lr_desc.irq < VGIC_NR_SGIS)
79 lr_val |= (lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT);
80
67 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = lr_val; 81 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = lr_val;
68} 82}
69 83
diff --git a/virt/kvm/arm/vgic-v3.c b/virt/kvm/arm/vgic-v3.c
index dff06021e748..afbf925b00f4 100644
--- a/virt/kvm/arm/vgic-v3.c
+++ b/virt/kvm/arm/vgic-v3.c
@@ -67,6 +67,10 @@ static struct vgic_lr vgic_v3_get_lr(const struct kvm_vcpu *vcpu, int lr)
67 lr_desc.state |= LR_STATE_ACTIVE; 67 lr_desc.state |= LR_STATE_ACTIVE;
68 if (val & ICH_LR_EOI) 68 if (val & ICH_LR_EOI)
69 lr_desc.state |= LR_EOI_INT; 69 lr_desc.state |= LR_EOI_INT;
70 if (val & ICH_LR_HW) {
71 lr_desc.state |= LR_HW;
72 lr_desc.hwirq = (val >> ICH_LR_PHYS_ID_SHIFT) & GENMASK(9, 0);
73 }
70 74
71 return lr_desc; 75 return lr_desc;
72} 76}
@@ -84,10 +88,17 @@ static void vgic_v3_set_lr(struct kvm_vcpu *vcpu, int lr,
84 * Eventually we want to make this configurable, so we may revisit 88 * Eventually we want to make this configurable, so we may revisit
85 * this in the future. 89 * this in the future.
86 */ 90 */
87 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) 91 switch (vcpu->kvm->arch.vgic.vgic_model) {
92 case KVM_DEV_TYPE_ARM_VGIC_V3:
88 lr_val |= ICH_LR_GROUP; 93 lr_val |= ICH_LR_GROUP;
89 else 94 break;
90 lr_val |= (u32)lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT; 95 case KVM_DEV_TYPE_ARM_VGIC_V2:
96 if (lr_desc.irq < VGIC_NR_SGIS)
97 lr_val |= (u32)lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT;
98 break;
99 default:
100 BUG();
101 }
91 102
92 if (lr_desc.state & LR_STATE_PENDING) 103 if (lr_desc.state & LR_STATE_PENDING)
93 lr_val |= ICH_LR_PENDING_BIT; 104 lr_val |= ICH_LR_PENDING_BIT;
@@ -95,6 +106,10 @@ static void vgic_v3_set_lr(struct kvm_vcpu *vcpu, int lr,
95 lr_val |= ICH_LR_ACTIVE_BIT; 106 lr_val |= ICH_LR_ACTIVE_BIT;
96 if (lr_desc.state & LR_EOI_INT) 107 if (lr_desc.state & LR_EOI_INT)
97 lr_val |= ICH_LR_EOI; 108 lr_val |= ICH_LR_EOI;
109 if (lr_desc.state & LR_HW) {
110 lr_val |= ICH_LR_HW;
111 lr_val |= ((u64)lr_desc.hwirq) << ICH_LR_PHYS_ID_SHIFT;
112 }
98 113
99 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)] = lr_val; 114 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)] = lr_val;
100} 115}
diff --git a/virt/kvm/arm/vgic.c b/virt/kvm/arm/vgic.c
index bc40137a022d..9eb489a2c94c 100644
--- a/virt/kvm/arm/vgic.c
+++ b/virt/kvm/arm/vgic.c
@@ -24,6 +24,7 @@
24#include <linux/of.h> 24#include <linux/of.h>
25#include <linux/of_address.h> 25#include <linux/of_address.h>
26#include <linux/of_irq.h> 26#include <linux/of_irq.h>
27#include <linux/rculist.h>
27#include <linux/uaccess.h> 28#include <linux/uaccess.h>
28 29
29#include <asm/kvm_emulate.h> 30#include <asm/kvm_emulate.h>
@@ -74,6 +75,28 @@
74 * cause the interrupt to become inactive in such a situation. 75 * cause the interrupt to become inactive in such a situation.
75 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become 76 * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
76 * inactive as long as the external input line is held high. 77 * inactive as long as the external input line is held high.
78 *
79 *
80 * Initialization rules: there are multiple stages to the vgic
81 * initialization, both for the distributor and the CPU interfaces.
82 *
83 * Distributor:
84 *
85 * - kvm_vgic_early_init(): initialization of static data that doesn't
86 * depend on any sizing information or emulation type. No allocation
87 * is allowed there.
88 *
89 * - vgic_init(): allocation and initialization of the generic data
90 * structures that depend on sizing information (number of CPUs,
91 * number of interrupts). Also initializes the vcpu specific data
92 * structures. Can be executed lazily for GICv2.
93 * [to be renamed to kvm_vgic_init??]
94 *
95 * CPU Interface:
96 *
97 * - kvm_vgic_cpu_early_init(): initialization of static data that
98 * doesn't depend on any sizing information or emulation type. No
99 * allocation is allowed there.
77 */ 100 */
78 101
79#include "vgic.h" 102#include "vgic.h"
@@ -82,6 +105,8 @@ static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
82static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu); 105static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
83static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr); 106static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
84static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc); 107static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
108static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
109 int virt_irq);
85 110
86static const struct vgic_ops *vgic_ops; 111static const struct vgic_ops *vgic_ops;
87static const struct vgic_params *vgic; 112static const struct vgic_params *vgic;
@@ -375,7 +400,7 @@ void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
375 400
376static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq) 401static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
377{ 402{
378 return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq); 403 return !vgic_irq_is_queued(vcpu, irq);
379} 404}
380 405
381/** 406/**
@@ -1115,6 +1140,39 @@ static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1115 if (!vgic_irq_is_edge(vcpu, irq)) 1140 if (!vgic_irq_is_edge(vcpu, irq))
1116 vlr.state |= LR_EOI_INT; 1141 vlr.state |= LR_EOI_INT;
1117 1142
1143 if (vlr.irq >= VGIC_NR_SGIS) {
1144 struct irq_phys_map *map;
1145 map = vgic_irq_map_search(vcpu, irq);
1146
1147 /*
1148 * If we have a mapping, and the virtual interrupt is
1149 * being injected, then we must set the state to
1150 * active in the physical world. Otherwise the
1151 * physical interrupt will fire and the guest will
1152 * exit before processing the virtual interrupt.
1153 */
1154 if (map) {
1155 int ret;
1156
1157 BUG_ON(!map->active);
1158 vlr.hwirq = map->phys_irq;
1159 vlr.state |= LR_HW;
1160 vlr.state &= ~LR_EOI_INT;
1161
1162 ret = irq_set_irqchip_state(map->irq,
1163 IRQCHIP_STATE_ACTIVE,
1164 true);
1165 WARN_ON(ret);
1166
1167 /*
1168 * Make sure we're not going to sample this
1169 * again, as a HW-backed interrupt cannot be
1170 * in the PENDING_ACTIVE stage.
1171 */
1172 vgic_irq_set_queued(vcpu, irq);
1173 }
1174 }
1175
1118 vgic_set_lr(vcpu, lr_nr, vlr); 1176 vgic_set_lr(vcpu, lr_nr, vlr);
1119 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr); 1177 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1120} 1178}
@@ -1339,6 +1397,39 @@ static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1339 return level_pending; 1397 return level_pending;
1340} 1398}
1341 1399
1400/*
1401 * Save the physical active state, and reset it to inactive.
1402 *
1403 * Return 1 if HW interrupt went from active to inactive, and 0 otherwise.
1404 */
1405static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr)
1406{
1407 struct irq_phys_map *map;
1408 int ret;
1409
1410 if (!(vlr.state & LR_HW))
1411 return 0;
1412
1413 map = vgic_irq_map_search(vcpu, vlr.irq);
1414 BUG_ON(!map || !map->active);
1415
1416 ret = irq_get_irqchip_state(map->irq,
1417 IRQCHIP_STATE_ACTIVE,
1418 &map->active);
1419
1420 WARN_ON(ret);
1421
1422 if (map->active) {
1423 ret = irq_set_irqchip_state(map->irq,
1424 IRQCHIP_STATE_ACTIVE,
1425 false);
1426 WARN_ON(ret);
1427 return 0;
1428 }
1429
1430 return 1;
1431}
1432
1342/* Sync back the VGIC state after a guest run */ 1433/* Sync back the VGIC state after a guest run */
1343static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) 1434static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1344{ 1435{
@@ -1353,14 +1444,31 @@ static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1353 elrsr = vgic_get_elrsr(vcpu); 1444 elrsr = vgic_get_elrsr(vcpu);
1354 elrsr_ptr = u64_to_bitmask(&elrsr); 1445 elrsr_ptr = u64_to_bitmask(&elrsr);
1355 1446
1356 /* Clear mappings for empty LRs */ 1447 /* Deal with HW interrupts, and clear mappings for empty LRs */
1357 for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) { 1448 for (lr = 0; lr < vgic->nr_lr; lr++) {
1358 struct vgic_lr vlr; 1449 struct vgic_lr vlr;
1359 1450
1360 if (!test_and_clear_bit(lr, vgic_cpu->lr_used)) 1451 if (!test_bit(lr, vgic_cpu->lr_used))
1361 continue; 1452 continue;
1362 1453
1363 vlr = vgic_get_lr(vcpu, lr); 1454 vlr = vgic_get_lr(vcpu, lr);
1455 if (vgic_sync_hwirq(vcpu, vlr)) {
1456 /*
1457 * So this is a HW interrupt that the guest
1458 * EOI-ed. Clean the LR state and allow the
1459 * interrupt to be sampled again.
1460 */
1461 vlr.state = 0;
1462 vlr.hwirq = 0;
1463 vgic_set_lr(vcpu, lr, vlr);
1464 vgic_irq_clear_queued(vcpu, vlr.irq);
1465 set_bit(lr, elrsr_ptr);
1466 }
1467
1468 if (!test_bit(lr, elrsr_ptr))
1469 continue;
1470
1471 clear_bit(lr, vgic_cpu->lr_used);
1364 1472
1365 BUG_ON(vlr.irq >= dist->nr_irqs); 1473 BUG_ON(vlr.irq >= dist->nr_irqs);
1366 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY; 1474 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
@@ -1447,7 +1555,8 @@ static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
1447} 1555}
1448 1556
1449static int vgic_update_irq_pending(struct kvm *kvm, int cpuid, 1557static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1450 unsigned int irq_num, bool level) 1558 struct irq_phys_map *map,
1559 unsigned int irq_num, bool level)
1451{ 1560{
1452 struct vgic_dist *dist = &kvm->arch.vgic; 1561 struct vgic_dist *dist = &kvm->arch.vgic;
1453 struct kvm_vcpu *vcpu; 1562 struct kvm_vcpu *vcpu;
@@ -1455,6 +1564,9 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1455 int enabled; 1564 int enabled;
1456 bool ret = true, can_inject = true; 1565 bool ret = true, can_inject = true;
1457 1566
1567 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
1568 return -EINVAL;
1569
1458 spin_lock(&dist->lock); 1570 spin_lock(&dist->lock);
1459 1571
1460 vcpu = kvm_get_vcpu(kvm, cpuid); 1572 vcpu = kvm_get_vcpu(kvm, cpuid);
@@ -1517,18 +1629,46 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1517out: 1629out:
1518 spin_unlock(&dist->lock); 1630 spin_unlock(&dist->lock);
1519 1631
1520 return ret ? cpuid : -EINVAL; 1632 if (ret) {
1633 /* kick the specified vcpu */
1634 kvm_vcpu_kick(kvm_get_vcpu(kvm, cpuid));
1635 }
1636
1637 return 0;
1638}
1639
1640static int vgic_lazy_init(struct kvm *kvm)
1641{
1642 int ret = 0;
1643
1644 if (unlikely(!vgic_initialized(kvm))) {
1645 /*
1646 * We only provide the automatic initialization of the VGIC
1647 * for the legacy case of a GICv2. Any other type must
1648 * be explicitly initialized once setup with the respective
1649 * KVM device call.
1650 */
1651 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
1652 return -EBUSY;
1653
1654 mutex_lock(&kvm->lock);
1655 ret = vgic_init(kvm);
1656 mutex_unlock(&kvm->lock);
1657 }
1658
1659 return ret;
1521} 1660}
1522 1661
1523/** 1662/**
1524 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic 1663 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
1525 * @kvm: The VM structure pointer 1664 * @kvm: The VM structure pointer
1526 * @cpuid: The CPU for PPIs 1665 * @cpuid: The CPU for PPIs
1527 * @irq_num: The IRQ number that is assigned to the device 1666 * @irq_num: The IRQ number that is assigned to the device. This IRQ
1667 * must not be mapped to a HW interrupt.
1528 * @level: Edge-triggered: true: to trigger the interrupt 1668 * @level: Edge-triggered: true: to trigger the interrupt
1529 * false: to ignore the call 1669 * false: to ignore the call
1530 * Level-sensitive true: activates an interrupt 1670 * Level-sensitive true: raise the input signal
1531 * false: deactivates an interrupt 1671 * false: lower the input signal
1532 * 1672 *
1533 * The GIC is not concerned with devices being active-LOW or active-HIGH for 1673 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1534 * level-sensitive interrupts. You can think of the level parameter as 1 1674 * level-sensitive interrupts. You can think of the level parameter as 1
@@ -1537,39 +1677,44 @@ out:
1537int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num, 1677int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
1538 bool level) 1678 bool level)
1539{ 1679{
1540 int ret = 0; 1680 struct irq_phys_map *map;
1541 int vcpu_id; 1681 int ret;
1542
1543 if (unlikely(!vgic_initialized(kvm))) {
1544 /*
1545 * We only provide the automatic initialization of the VGIC
1546 * for the legacy case of a GICv2. Any other type must
1547 * be explicitly initialized once setup with the respective
1548 * KVM device call.
1549 */
1550 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
1551 ret = -EBUSY;
1552 goto out;
1553 }
1554 mutex_lock(&kvm->lock);
1555 ret = vgic_init(kvm);
1556 mutex_unlock(&kvm->lock);
1557 1682
1558 if (ret) 1683 ret = vgic_lazy_init(kvm);
1559 goto out; 1684 if (ret)
1560 } 1685 return ret;
1561 1686
1562 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020)) 1687 map = vgic_irq_map_search(kvm_get_vcpu(kvm, cpuid), irq_num);
1688 if (map)
1563 return -EINVAL; 1689 return -EINVAL;
1564 1690
1565 vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level); 1691 return vgic_update_irq_pending(kvm, cpuid, NULL, irq_num, level);
1566 if (vcpu_id >= 0) { 1692}
1567 /* kick the specified vcpu */
1568 kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
1569 }
1570 1693
1571out: 1694/**
1572 return ret; 1695 * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic
1696 * @kvm: The VM structure pointer
1697 * @cpuid: The CPU for PPIs
1698 * @map: Pointer to a irq_phys_map structure describing the mapping
1699 * @level: Edge-triggered: true: to trigger the interrupt
1700 * false: to ignore the call
1701 * Level-sensitive true: raise the input signal
1702 * false: lower the input signal
1703 *
1704 * The GIC is not concerned with devices being active-LOW or active-HIGH for
1705 * level-sensitive interrupts. You can think of the level parameter as 1
1706 * being HIGH and 0 being LOW and all devices being active-HIGH.
1707 */
1708int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,
1709 struct irq_phys_map *map, bool level)
1710{
1711 int ret;
1712
1713 ret = vgic_lazy_init(kvm);
1714 if (ret)
1715 return ret;
1716
1717 return vgic_update_irq_pending(kvm, cpuid, map, map->virt_irq, level);
1573} 1718}
1574 1719
1575static irqreturn_t vgic_maintenance_handler(int irq, void *data) 1720static irqreturn_t vgic_maintenance_handler(int irq, void *data)
@@ -1583,6 +1728,188 @@ static irqreturn_t vgic_maintenance_handler(int irq, void *data)
1583 return IRQ_HANDLED; 1728 return IRQ_HANDLED;
1584} 1729}
1585 1730
1731static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,
1732 int virt_irq)
1733{
1734 if (virt_irq < VGIC_NR_PRIVATE_IRQS)
1735 return &vcpu->arch.vgic_cpu.irq_phys_map_list;
1736 else
1737 return &vcpu->kvm->arch.vgic.irq_phys_map_list;
1738}
1739
1740/**
1741 * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ
1742 * @vcpu: The VCPU pointer
1743 * @virt_irq: The virtual irq number
1744 * @irq: The Linux IRQ number
1745 *
1746 * Establish a mapping between a guest visible irq (@virt_irq) and a
1747 * Linux irq (@irq). On injection, @virt_irq will be associated with
1748 * the physical interrupt represented by @irq. This mapping can be
1749 * established multiple times as long as the parameters are the same.
1750 *
1751 * Returns a valid pointer on success, and an error pointer otherwise
1752 */
1753struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
1754 int virt_irq, int irq)
1755{
1756 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1757 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1758 struct irq_phys_map *map;
1759 struct irq_phys_map_entry *entry;
1760 struct irq_desc *desc;
1761 struct irq_data *data;
1762 int phys_irq;
1763
1764 desc = irq_to_desc(irq);
1765 if (!desc) {
1766 kvm_err("%s: no interrupt descriptor\n", __func__);
1767 return ERR_PTR(-EINVAL);
1768 }
1769
1770 data = irq_desc_get_irq_data(desc);
1771 while (data->parent_data)
1772 data = data->parent_data;
1773
1774 phys_irq = data->hwirq;
1775
1776 /* Create a new mapping */
1777 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1778 if (!entry)
1779 return ERR_PTR(-ENOMEM);
1780
1781 spin_lock(&dist->irq_phys_map_lock);
1782
1783 /* Try to match an existing mapping */
1784 map = vgic_irq_map_search(vcpu, virt_irq);
1785 if (map) {
1786 /* Make sure this mapping matches */
1787 if (map->phys_irq != phys_irq ||
1788 map->irq != irq)
1789 map = ERR_PTR(-EINVAL);
1790
1791 /* Found an existing, valid mapping */
1792 goto out;
1793 }
1794
1795 map = &entry->map;
1796 map->virt_irq = virt_irq;
1797 map->phys_irq = phys_irq;
1798 map->irq = irq;
1799
1800 list_add_tail_rcu(&entry->entry, root);
1801
1802out:
1803 spin_unlock(&dist->irq_phys_map_lock);
1804 /* If we've found a hit in the existing list, free the useless
1805 * entry */
1806 if (IS_ERR(map) || map != &entry->map)
1807 kfree(entry);
1808 return map;
1809}
1810
1811static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
1812 int virt_irq)
1813{
1814 struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq);
1815 struct irq_phys_map_entry *entry;
1816 struct irq_phys_map *map;
1817
1818 rcu_read_lock();
1819
1820 list_for_each_entry_rcu(entry, root, entry) {
1821 map = &entry->map;
1822 if (map->virt_irq == virt_irq) {
1823 rcu_read_unlock();
1824 return map;
1825 }
1826 }
1827
1828 rcu_read_unlock();
1829
1830 return NULL;
1831}
1832
1833static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1834{
1835 struct irq_phys_map_entry *entry;
1836
1837 entry = container_of(rcu, struct irq_phys_map_entry, rcu);
1838 kfree(entry);
1839}
1840
1841/**
1842 * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1843 *
1844 * Return the logical active state of a mapped interrupt. This doesn't
1845 * necessarily reflects the current HW state.
1846 */
1847bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map)
1848{
1849 BUG_ON(!map);
1850 return map->active;
1851}
1852
1853/**
1854 * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1855 *
1856 * Set the logical active state of a mapped interrupt. This doesn't
1857 * immediately affects the HW state.
1858 */
1859void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active)
1860{
1861 BUG_ON(!map);
1862 map->active = active;
1863}
1864
1865/**
1866 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1867 * @vcpu: The VCPU pointer
1868 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
1869 *
1870 * Remove an existing mapping between virtual and physical interrupts.
1871 */
1872int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map)
1873{
1874 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1875 struct irq_phys_map_entry *entry;
1876 struct list_head *root;
1877
1878 if (!map)
1879 return -EINVAL;
1880
1881 root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq);
1882
1883 spin_lock(&dist->irq_phys_map_lock);
1884
1885 list_for_each_entry(entry, root, entry) {
1886 if (&entry->map == map) {
1887 list_del_rcu(&entry->entry);
1888 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1889 break;
1890 }
1891 }
1892
1893 spin_unlock(&dist->irq_phys_map_lock);
1894
1895 return 0;
1896}
1897
1898static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root)
1899{
1900 struct vgic_dist *dist = &kvm->arch.vgic;
1901 struct irq_phys_map_entry *entry;
1902
1903 spin_lock(&dist->irq_phys_map_lock);
1904
1905 list_for_each_entry(entry, root, entry) {
1906 list_del_rcu(&entry->entry);
1907 call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu);
1908 }
1909
1910 spin_unlock(&dist->irq_phys_map_lock);
1911}
1912
1586void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) 1913void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1587{ 1914{
1588 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 1915 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
@@ -1591,6 +1918,7 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1591 kfree(vgic_cpu->active_shared); 1918 kfree(vgic_cpu->active_shared);
1592 kfree(vgic_cpu->pend_act_shared); 1919 kfree(vgic_cpu->pend_act_shared);
1593 kfree(vgic_cpu->vgic_irq_lr_map); 1920 kfree(vgic_cpu->vgic_irq_lr_map);
1921 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
1594 vgic_cpu->pending_shared = NULL; 1922 vgic_cpu->pending_shared = NULL;
1595 vgic_cpu->active_shared = NULL; 1923 vgic_cpu->active_shared = NULL;
1596 vgic_cpu->pend_act_shared = NULL; 1924 vgic_cpu->pend_act_shared = NULL;
@@ -1628,6 +1956,17 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1628} 1956}
1629 1957
1630/** 1958/**
1959 * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage
1960 *
1961 * No memory allocation should be performed here, only static init.
1962 */
1963void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
1964{
1965 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1966 INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list);
1967}
1968
1969/**
1631 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW 1970 * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
1632 * 1971 *
1633 * The host's GIC naturally limits the maximum amount of VCPUs a guest 1972 * The host's GIC naturally limits the maximum amount of VCPUs a guest
@@ -1664,6 +2003,7 @@ void kvm_vgic_destroy(struct kvm *kvm)
1664 kfree(dist->irq_spi_target); 2003 kfree(dist->irq_spi_target);
1665 kfree(dist->irq_pending_on_cpu); 2004 kfree(dist->irq_pending_on_cpu);
1666 kfree(dist->irq_active_on_cpu); 2005 kfree(dist->irq_active_on_cpu);
2006 vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list);
1667 dist->irq_sgi_sources = NULL; 2007 dist->irq_sgi_sources = NULL;
1668 dist->irq_spi_cpu = NULL; 2008 dist->irq_spi_cpu = NULL;
1669 dist->irq_spi_target = NULL; 2009 dist->irq_spi_target = NULL;
@@ -1787,6 +2127,18 @@ static int init_vgic_model(struct kvm *kvm, int type)
1787 return 0; 2127 return 0;
1788} 2128}
1789 2129
2130/**
2131 * kvm_vgic_early_init - Earliest possible vgic initialization stage
2132 *
2133 * No memory allocation should be performed here, only static init.
2134 */
2135void kvm_vgic_early_init(struct kvm *kvm)
2136{
2137 spin_lock_init(&kvm->arch.vgic.lock);
2138 spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock);
2139 INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list);
2140}
2141
1790int kvm_vgic_create(struct kvm *kvm, u32 type) 2142int kvm_vgic_create(struct kvm *kvm, u32 type)
1791{ 2143{
1792 int i, vcpu_lock_idx = -1, ret; 2144 int i, vcpu_lock_idx = -1, ret;
@@ -1832,7 +2184,6 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
1832 if (ret) 2184 if (ret)
1833 goto out_unlock; 2185 goto out_unlock;
1834 2186
1835 spin_lock_init(&kvm->arch.vgic.lock);
1836 kvm->arch.vgic.in_kernel = true; 2187 kvm->arch.vgic.in_kernel = true;
1837 kvm->arch.vgic.vgic_model = type; 2188 kvm->arch.vgic.vgic_model = type;
1838 kvm->arch.vgic.vctrl_base = vgic->vctrl_base; 2189 kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
diff --git a/virt/kvm/irqchip.c b/virt/kvm/irqchip.c
index 21c14244f4c4..d7ea8e20dae4 100644
--- a/virt/kvm/irqchip.c
+++ b/virt/kvm/irqchip.c
@@ -213,11 +213,15 @@ int kvm_set_irq_routing(struct kvm *kvm,
213 goto out; 213 goto out;
214 214
215 r = -EINVAL; 215 r = -EINVAL;
216 if (ue->flags) 216 if (ue->flags) {
217 kfree(e);
217 goto out; 218 goto out;
219 }
218 r = setup_routing_entry(new, e, ue); 220 r = setup_routing_entry(new, e, ue);
219 if (r) 221 if (r) {
222 kfree(e);
220 goto out; 223 goto out;
224 }
221 ++ue; 225 ++ue;
222 } 226 }
223 227
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 268fc0a5a932..a25a73147f71 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -66,9 +66,18 @@
66MODULE_AUTHOR("Qumranet"); 66MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL"); 67MODULE_LICENSE("GPL");
68 68
69static unsigned int halt_poll_ns; 69/* halt polling only reduces halt latency by 5-7 us, 500us is enough */
70static unsigned int halt_poll_ns = 500000;
70module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR); 71module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR);
71 72
73/* Default doubles per-vcpu halt_poll_ns. */
74static unsigned int halt_poll_ns_grow = 2;
75module_param(halt_poll_ns_grow, int, S_IRUGO);
76
77/* Default resets per-vcpu halt_poll_ns . */
78static unsigned int halt_poll_ns_shrink;
79module_param(halt_poll_ns_shrink, int, S_IRUGO);
80
72/* 81/*
73 * Ordering of locks: 82 * Ordering of locks:
74 * 83 *
@@ -217,6 +226,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
217 vcpu->kvm = kvm; 226 vcpu->kvm = kvm;
218 vcpu->vcpu_id = id; 227 vcpu->vcpu_id = id;
219 vcpu->pid = NULL; 228 vcpu->pid = NULL;
229 vcpu->halt_poll_ns = 0;
220 init_waitqueue_head(&vcpu->wq); 230 init_waitqueue_head(&vcpu->wq);
221 kvm_async_pf_vcpu_init(vcpu); 231 kvm_async_pf_vcpu_init(vcpu);
222 232
@@ -1937,6 +1947,35 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
1937} 1947}
1938EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); 1948EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
1939 1949
1950static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
1951{
1952 int old, val;
1953
1954 old = val = vcpu->halt_poll_ns;
1955 /* 10us base */
1956 if (val == 0 && halt_poll_ns_grow)
1957 val = 10000;
1958 else
1959 val *= halt_poll_ns_grow;
1960
1961 vcpu->halt_poll_ns = val;
1962 trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
1963}
1964
1965static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
1966{
1967 int old, val;
1968
1969 old = val = vcpu->halt_poll_ns;
1970 if (halt_poll_ns_shrink == 0)
1971 val = 0;
1972 else
1973 val /= halt_poll_ns_shrink;
1974
1975 vcpu->halt_poll_ns = val;
1976 trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
1977}
1978
1940static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) 1979static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
1941{ 1980{
1942 if (kvm_arch_vcpu_runnable(vcpu)) { 1981 if (kvm_arch_vcpu_runnable(vcpu)) {
@@ -1959,10 +1998,11 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1959 ktime_t start, cur; 1998 ktime_t start, cur;
1960 DEFINE_WAIT(wait); 1999 DEFINE_WAIT(wait);
1961 bool waited = false; 2000 bool waited = false;
2001 u64 block_ns;
1962 2002
1963 start = cur = ktime_get(); 2003 start = cur = ktime_get();
1964 if (halt_poll_ns) { 2004 if (vcpu->halt_poll_ns) {
1965 ktime_t stop = ktime_add_ns(ktime_get(), halt_poll_ns); 2005 ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
1966 2006
1967 do { 2007 do {
1968 /* 2008 /*
@@ -1991,7 +2031,21 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
1991 cur = ktime_get(); 2031 cur = ktime_get();
1992 2032
1993out: 2033out:
1994 trace_kvm_vcpu_wakeup(ktime_to_ns(cur) - ktime_to_ns(start), waited); 2034 block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
2035
2036 if (halt_poll_ns) {
2037 if (block_ns <= vcpu->halt_poll_ns)
2038 ;
2039 /* we had a long block, shrink polling */
2040 else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns)
2041 shrink_halt_poll_ns(vcpu);
2042 /* we had a short halt and our poll time is too small */
2043 else if (vcpu->halt_poll_ns < halt_poll_ns &&
2044 block_ns < halt_poll_ns)
2045 grow_halt_poll_ns(vcpu);
2046 }
2047
2048 trace_kvm_vcpu_wakeup(block_ns, waited);
1995} 2049}
1996EXPORT_SYMBOL_GPL(kvm_vcpu_block); 2050EXPORT_SYMBOL_GPL(kvm_vcpu_block);
1997 2051