• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kvm_host.h>
3 
4 #include <asm/irq_remapping.h>
5 #include <asm/cpu.h>
6 
7 #include "lapic.h"
8 #include "irq.h"
9 #include "posted_intr.h"
10 #include "trace.h"
11 #include "vmx.h"
12 
13 /*
14  * We maintain a per-CPU linked-list of vCPU, so in wakeup_handler() we
15  * can find which vCPU should be waken up.
16  */
17 static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
18 static DEFINE_PER_CPU(raw_spinlock_t, blocked_vcpu_on_cpu_lock);
19 
vcpu_to_pi_desc(struct kvm_vcpu * vcpu)20 static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
21 {
22 	return &(to_vmx(vcpu)->pi_desc);
23 }
24 
vmx_vcpu_pi_load(struct kvm_vcpu * vcpu,int cpu)25 void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
26 {
27 	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
28 	struct pi_desc old, new;
29 	unsigned int dest;
30 
31 	/*
32 	 * In case of hot-plug or hot-unplug, we may have to undo
33 	 * vmx_vcpu_pi_put even if there is no assigned device.  And we
34 	 * always keep PI.NDST up to date for simplicity: it makes the
35 	 * code easier, and CPU migration is not a fast path.
36 	 */
37 	if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
38 		return;
39 
40 	/*
41 	 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
42 	 * PI.NDST: pi_post_block is the one expected to change PID.NDST and the
43 	 * wakeup handler expects the vCPU to be on the blocked_vcpu_list that
44 	 * matches PI.NDST. Otherwise, a vcpu may not be able to be woken up
45 	 * correctly.
46 	 */
47 	if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
48 		pi_clear_sn(pi_desc);
49 		goto after_clear_sn;
50 	}
51 
52 	/* The full case.  */
53 	do {
54 		old.control = new.control = READ_ONCE(pi_desc->control);
55 
56 		dest = cpu_physical_id(cpu);
57 
58 		if (x2apic_mode)
59 			new.ndst = dest;
60 		else
61 			new.ndst = (dest << 8) & 0xFF00;
62 
63 		new.sn = 0;
64 	} while (cmpxchg64(&pi_desc->control, old.control,
65 			   new.control) != old.control);
66 
67 after_clear_sn:
68 
69 	/*
70 	 * Clear SN before reading the bitmap.  The VT-d firmware
71 	 * writes the bitmap and reads SN atomically (5.2.3 in the
72 	 * spec), so it doesn't really have a memory barrier that
73 	 * pairs with this, but we cannot do that and we need one.
74 	 */
75 	smp_mb__after_atomic();
76 
77 	if (!pi_is_pir_empty(pi_desc))
78 		pi_set_on(pi_desc);
79 }
80 
vmx_can_use_vtd_pi(struct kvm * kvm)81 static bool vmx_can_use_vtd_pi(struct kvm *kvm)
82 {
83 	return irqchip_in_kernel(kvm) && enable_apicv &&
84 		kvm_arch_has_assigned_device(kvm) &&
85 		irq_remapping_cap(IRQ_POSTING_CAP);
86 }
87 
vmx_vcpu_pi_put(struct kvm_vcpu * vcpu)88 void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
89 {
90 	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
91 
92 	if (!vmx_can_use_vtd_pi(vcpu->kvm))
93 		return;
94 
95 	/* Set SN when the vCPU is preempted */
96 	if (vcpu->preempted)
97 		pi_set_sn(pi_desc);
98 }
99 
__pi_post_block(struct kvm_vcpu * vcpu)100 static void __pi_post_block(struct kvm_vcpu *vcpu)
101 {
102 	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
103 	struct pi_desc old, new;
104 	unsigned int dest;
105 
106 	do {
107 		old.control = new.control = READ_ONCE(pi_desc->control);
108 		WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
109 		     "Wakeup handler not enabled while the VCPU is blocked\n");
110 
111 		dest = cpu_physical_id(vcpu->cpu);
112 
113 		if (x2apic_mode)
114 			new.ndst = dest;
115 		else
116 			new.ndst = (dest << 8) & 0xFF00;
117 
118 		/* set 'NV' to 'notification vector' */
119 		new.nv = POSTED_INTR_VECTOR;
120 	} while (cmpxchg64(&pi_desc->control, old.control,
121 			   new.control) != old.control);
122 
123 	if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
124 		raw_spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
125 		list_del(&vcpu->blocked_vcpu_list);
126 		raw_spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
127 		vcpu->pre_pcpu = -1;
128 	}
129 }
130 
131 /*
132  * This routine does the following things for vCPU which is going
133  * to be blocked if VT-d PI is enabled.
134  * - Store the vCPU to the wakeup list, so when interrupts happen
135  *   we can find the right vCPU to wake up.
136  * - Change the Posted-interrupt descriptor as below:
137  *      'NDST' <-- vcpu->pre_pcpu
138  *      'NV' <-- POSTED_INTR_WAKEUP_VECTOR
139  * - If 'ON' is set during this process, which means at least one
140  *   interrupt is posted for this vCPU, we cannot block it, in
141  *   this case, return 1, otherwise, return 0.
142  *
143  */
pi_pre_block(struct kvm_vcpu * vcpu)144 int pi_pre_block(struct kvm_vcpu *vcpu)
145 {
146 	unsigned int dest;
147 	struct pi_desc old, new;
148 	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
149 
150 	if (!vmx_can_use_vtd_pi(vcpu->kvm) ||
151 	    vmx_interrupt_blocked(vcpu))
152 		return 0;
153 
154 	WARN_ON(irqs_disabled());
155 	local_irq_disable();
156 	if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
157 		vcpu->pre_pcpu = vcpu->cpu;
158 		raw_spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
159 		list_add_tail(&vcpu->blocked_vcpu_list,
160 			      &per_cpu(blocked_vcpu_on_cpu,
161 				       vcpu->pre_pcpu));
162 		raw_spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
163 	}
164 
165 	do {
166 		old.control = new.control = READ_ONCE(pi_desc->control);
167 
168 		WARN((pi_desc->sn == 1),
169 		     "Warning: SN field of posted-interrupts "
170 		     "is set before blocking\n");
171 
172 		/*
173 		 * Since vCPU can be preempted during this process,
174 		 * vcpu->cpu could be different with pre_pcpu, we
175 		 * need to set pre_pcpu as the destination of wakeup
176 		 * notification event, then we can find the right vCPU
177 		 * to wakeup in wakeup handler if interrupts happen
178 		 * when the vCPU is in blocked state.
179 		 */
180 		dest = cpu_physical_id(vcpu->pre_pcpu);
181 
182 		if (x2apic_mode)
183 			new.ndst = dest;
184 		else
185 			new.ndst = (dest << 8) & 0xFF00;
186 
187 		/* set 'NV' to 'wakeup vector' */
188 		new.nv = POSTED_INTR_WAKEUP_VECTOR;
189 	} while (cmpxchg64(&pi_desc->control, old.control,
190 			   new.control) != old.control);
191 
192 	/* We should not block the vCPU if an interrupt is posted for it.  */
193 	if (pi_test_on(pi_desc) == 1)
194 		__pi_post_block(vcpu);
195 
196 	local_irq_enable();
197 	return (vcpu->pre_pcpu == -1);
198 }
199 
pi_post_block(struct kvm_vcpu * vcpu)200 void pi_post_block(struct kvm_vcpu *vcpu)
201 {
202 	if (vcpu->pre_pcpu == -1)
203 		return;
204 
205 	WARN_ON(irqs_disabled());
206 	local_irq_disable();
207 	__pi_post_block(vcpu);
208 	local_irq_enable();
209 }
210 
211 /*
212  * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
213  */
pi_wakeup_handler(void)214 void pi_wakeup_handler(void)
215 {
216 	struct kvm_vcpu *vcpu;
217 	int cpu = smp_processor_id();
218 
219 	raw_spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
220 	list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
221 			blocked_vcpu_list) {
222 		struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
223 
224 		if (pi_test_on(pi_desc) == 1)
225 			kvm_vcpu_kick(vcpu);
226 	}
227 	raw_spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
228 }
229 
pi_init_cpu(int cpu)230 void __init pi_init_cpu(int cpu)
231 {
232 	INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
233 	raw_spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
234 }
235 
pi_has_pending_interrupt(struct kvm_vcpu * vcpu)236 bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu)
237 {
238 	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
239 
240 	return pi_test_on(pi_desc) ||
241 		(pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
242 }
243 
244 
245 /*
246  * Bail out of the block loop if the VM has an assigned
247  * device, but the blocking vCPU didn't reconfigure the
248  * PI.NV to the wakeup vector, i.e. the assigned device
249  * came along after the initial check in pi_pre_block().
250  */
vmx_pi_start_assignment(struct kvm * kvm)251 void vmx_pi_start_assignment(struct kvm *kvm)
252 {
253 	if (!irq_remapping_cap(IRQ_POSTING_CAP))
254 		return;
255 
256 	kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK);
257 }
258 
259 /*
260  * pi_update_irte - set IRTE for Posted-Interrupts
261  *
262  * @kvm: kvm
263  * @host_irq: host irq of the interrupt
264  * @guest_irq: gsi of the interrupt
265  * @set: set or unset PI
266  * returns 0 on success, < 0 on failure
267  */
pi_update_irte(struct kvm * kvm,unsigned int host_irq,uint32_t guest_irq,bool set)268 int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,
269 		   bool set)
270 {
271 	struct kvm_kernel_irq_routing_entry *e;
272 	struct kvm_irq_routing_table *irq_rt;
273 	struct kvm_lapic_irq irq;
274 	struct kvm_vcpu *vcpu;
275 	struct vcpu_data vcpu_info;
276 	int idx, ret = 0;
277 
278 	if (!vmx_can_use_vtd_pi(kvm))
279 		return 0;
280 
281 	idx = srcu_read_lock(&kvm->irq_srcu);
282 	irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
283 	if (guest_irq >= irq_rt->nr_rt_entries ||
284 	    hlist_empty(&irq_rt->map[guest_irq])) {
285 		pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
286 			     guest_irq, irq_rt->nr_rt_entries);
287 		goto out;
288 	}
289 
290 	hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
291 		if (e->type != KVM_IRQ_ROUTING_MSI)
292 			continue;
293 		/*
294 		 * VT-d PI cannot support posting multicast/broadcast
295 		 * interrupts to a vCPU, we still use interrupt remapping
296 		 * for these kind of interrupts.
297 		 *
298 		 * For lowest-priority interrupts, we only support
299 		 * those with single CPU as the destination, e.g. user
300 		 * configures the interrupts via /proc/irq or uses
301 		 * irqbalance to make the interrupts single-CPU.
302 		 *
303 		 * We will support full lowest-priority interrupt later.
304 		 *
305 		 * In addition, we can only inject generic interrupts using
306 		 * the PI mechanism, refuse to route others through it.
307 		 */
308 
309 		kvm_set_msi_irq(kvm, e, &irq);
310 		if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
311 		    !kvm_irq_is_postable(&irq)) {
312 			/*
313 			 * Make sure the IRTE is in remapped mode if
314 			 * we don't handle it in posted mode.
315 			 */
316 			ret = irq_set_vcpu_affinity(host_irq, NULL);
317 			if (ret < 0) {
318 				printk(KERN_INFO
319 				   "failed to back to remapped mode, irq: %u\n",
320 				   host_irq);
321 				goto out;
322 			}
323 
324 			continue;
325 		}
326 
327 		vcpu_info.pi_desc_addr = __pa(&to_vmx(vcpu)->pi_desc);
328 		vcpu_info.vector = irq.vector;
329 
330 		trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
331 				vcpu_info.vector, vcpu_info.pi_desc_addr, set);
332 
333 		if (set)
334 			ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
335 		else
336 			ret = irq_set_vcpu_affinity(host_irq, NULL);
337 
338 		if (ret < 0) {
339 			printk(KERN_INFO "%s: failed to update PI IRTE\n",
340 					__func__);
341 			goto out;
342 		}
343 	}
344 
345 	ret = 0;
346 out:
347 	srcu_read_unlock(&kvm->irq_srcu, idx);
348 	return ret;
349 }
350