vgic.c 24.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2015, 2016 ARM Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/kvm.h>
#include <linux/kvm_host.h>
19
#include <linux/list_sort.h>
20 21
#include <linux/interrupt.h>
#include <linux/irq.h>
22
#include <asm/kvm_hyp.h>
23 24 25

#include "vgic.h"

26
#define CREATE_TRACE_POINTS
27
#include "trace.h"
28 29 30 31 32 33 34

#ifdef CONFIG_DEBUG_SPINLOCK
#define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
#else
#define DEBUG_SPINLOCK_BUG_ON(p)
#endif

35 36 37
struct vgic_global kvm_vgic_global_state __ro_after_init = {
	.gicv3_cpuif = STATIC_KEY_FALSE_INIT,
};
38

39 40
/*
 * Locking order is always:
41 42 43 44 45 46
 * kvm->lock (mutex)
 *   its->cmd_lock (mutex)
 *     its->its_lock (mutex)
 *       vgic_cpu->ap_list_lock
 *         kvm->lpi_list_lock
 *           vgic_irq->irq_lock
47
 *
48 49 50 51 52
 * If you need to take multiple locks, always take the upper lock first,
 * then the lower ones, e.g. first take the its_lock, then the irq_lock.
 * If you are already holding a lock and need to take a higher one, you
 * have to drop the lower ranking lock first and re-aquire it after having
 * taken the upper one.
53 54 55 56 57 58
 *
 * When taking more than one ap_list_lock at the same time, always take the
 * lowest numbered VCPU's ap_list_lock first, so:
 *   vcpuX->vcpu_id < vcpuY->vcpu_id:
 *     spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
 *     spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
59 60 61 62
 *
 * Since the VGIC must support injecting virtual interrupts from ISRs, we have
 * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
 * spinlocks for any lock that may be taken while injecting an interrupt.
63 64
 */

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*
 * Iterate over the VM's list of mapped LPIs to find the one with a
 * matching interrupt ID and return a reference to the IRQ structure.
 */
static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
{
	struct vgic_dist *dist = &kvm->arch.vgic;
	struct vgic_irq *irq = NULL;

	spin_lock(&dist->lpi_list_lock);

	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
		if (irq->intid != intid)
			continue;

		/*
		 * This increases the refcount, the caller is expected to
		 * call vgic_put_irq() later once it's finished with the IRQ.
		 */
84
		vgic_get_irq_kref(irq);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		goto out_unlock;
	}
	irq = NULL;

out_unlock:
	spin_unlock(&dist->lpi_list_lock);

	return irq;
}

/*
 * This looks up the virtual interrupt ID to get the corresponding
 * struct vgic_irq. It also increases the refcount, so any caller is expected
 * to call vgic_put_irq() once it's finished with this IRQ.
 */
100 101 102 103 104 105 106 107 108 109 110
struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
			      u32 intid)
{
	/* SGIs and PPIs */
	if (intid <= VGIC_MAX_PRIVATE)
		return &vcpu->arch.vgic_cpu.private_irqs[intid];

	/* SPIs */
	if (intid <= VGIC_MAX_SPI)
		return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];

111
	/* LPIs */
112
	if (intid >= VGIC_MIN_LPI)
113
		return vgic_get_lpi(kvm, intid);
114 115 116 117

	WARN(1, "Looking up struct vgic_irq for reserved INTID");
	return NULL;
}
118

119 120 121 122 123
/*
 * We can't do anything in here, because we lack the kvm pointer to
 * lock and remove the item from the lpi_list. So we keep this function
 * empty and use the return value of kref_put() to trigger the freeing.
 */
124 125 126 127 128 129
static void vgic_irq_release(struct kref *ref)
{
}

void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
{
130
	struct vgic_dist *dist = &kvm->arch.vgic;
131

132 133 134
	if (irq->intid < VGIC_MIN_LPI)
		return;

135 136 137
	spin_lock(&dist->lpi_list_lock);
	if (!kref_put(&irq->refcount, vgic_irq_release)) {
		spin_unlock(&dist->lpi_list_lock);
138
		return;
139
	};
140 141 142 143 144 145

	list_del(&irq->lpi_list);
	dist->lpi_list_count--;
	spin_unlock(&dist->lpi_list_lock);

	kfree(irq);
146 147
}

148 149 150 151 152 153 154
void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
{
	WARN_ON(irq_set_irqchip_state(irq->host_irq,
				      IRQCHIP_STATE_PENDING,
				      pending));
}

155 156 157 158 159 160
bool vgic_get_phys_line_level(struct vgic_irq *irq)
{
	bool line_level;

	BUG_ON(!irq->hw);

161 162 163
	if (irq->get_input_level)
		return irq->get_input_level(irq->intid);

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	WARN_ON(irq_get_irqchip_state(irq->host_irq,
				      IRQCHIP_STATE_PENDING,
				      &line_level));
	return line_level;
}

/* Set/Clear the physical active state */
void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
{

	BUG_ON(!irq->hw);
	WARN_ON(irq_set_irqchip_state(irq->host_irq,
				      IRQCHIP_STATE_ACTIVE,
				      active));
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/**
 * kvm_vgic_target_oracle - compute the target vcpu for an irq
 *
 * @irq:	The irq to route. Must be already locked.
 *
 * Based on the current state of the interrupt (enabled, pending,
 * active, vcpu and target_vcpu), compute the next vcpu this should be
 * given to. Return NULL if this shouldn't be injected at all.
 *
 * Requires the IRQ lock to be held.
 */
static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
{
	DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));

	/* If the interrupt is active, it must stay on the current vcpu */
	if (irq->active)
		return irq->vcpu ? : irq->target_vcpu;

	/*
	 * If the IRQ is not active but enabled and pending, we should direct
	 * it to its configured target VCPU.
	 * If the distributor is disabled, pending interrupts shouldn't be
	 * forwarded.
	 */
205
	if (irq->enabled && irq_is_pending(irq)) {
206 207 208 209 210 211 212 213 214 215 216 217 218
		if (unlikely(irq->target_vcpu &&
			     !irq->target_vcpu->kvm->arch.vgic.enabled))
			return NULL;

		return irq->target_vcpu;
	}

	/* If neither active nor pending and enabled, then this IRQ should not
	 * be queued to any VCPU.
	 */
	return NULL;
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/*
 * The order of items in the ap_lists defines how we'll pack things in LRs as
 * well, the first items in the list being the first things populated in the
 * LRs.
 *
 * A hard rule is that active interrupts can never be pushed out of the LRs
 * (and therefore take priority) since we cannot reliably trap on deactivation
 * of IRQs and therefore they have to be present in the LRs.
 *
 * Otherwise things should be sorted by the priority field and the GIC
 * hardware support will take care of preemption of priority groups etc.
 *
 * Return negative if "a" sorts before "b", 0 to preserve order, and positive
 * to sort "b" before "a".
 */
static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
{
	struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
	struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
	bool penda, pendb;
	int ret;

	spin_lock(&irqa->irq_lock);
	spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);

	if (irqa->active || irqb->active) {
		ret = (int)irqb->active - (int)irqa->active;
		goto out;
	}

249 250
	penda = irqa->enabled && irq_is_pending(irqa);
	pendb = irqb->enabled && irq_is_pending(irqb);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

	if (!penda || !pendb) {
		ret = (int)pendb - (int)penda;
		goto out;
	}

	/* Both pending and enabled, sort by priority */
	ret = irqa->priority - irqb->priority;
out:
	spin_unlock(&irqb->irq_lock);
	spin_unlock(&irqa->irq_lock);
	return ret;
}

/* Must be called with the ap_list_lock held */
static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
{
	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;

	DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));

	list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
}

275 276
/*
 * Only valid injection if changing level for level-triggered IRQs or for a
277 278
 * rising edge, and in-kernel connected IRQ lines can only be controlled by
 * their owner.
279
 */
280
static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
281
{
282 283 284
	if (irq->owner != owner)
		return false;

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	switch (irq->config) {
	case VGIC_CONFIG_LEVEL:
		return irq->line_level != level;
	case VGIC_CONFIG_EDGE:
		return level;
	}

	return false;
}

/*
 * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
 * Do the queuing if necessary, taking the right locks in the right order.
 * Returns true when the IRQ was queued, false otherwise.
 *
 * Needs to be entered with the IRQ lock already held, but will return
 * with all locks dropped.
 */
303 304
bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
			   unsigned long flags)
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
{
	struct kvm_vcpu *vcpu;

	DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));

retry:
	vcpu = vgic_target_oracle(irq);
	if (irq->vcpu || !vcpu) {
		/*
		 * If this IRQ is already on a VCPU's ap_list, then it
		 * cannot be moved or modified and there is no more work for
		 * us to do.
		 *
		 * Otherwise, if the irq is not pending and enabled, it does
		 * not need to be inserted into an ap_list and there is also
		 * no more work for us to do.
		 */
322
		spin_unlock_irqrestore(&irq->irq_lock, flags);
323 324 325 326 327 328 329 330 331 332

		/*
		 * We have to kick the VCPU here, because we could be
		 * queueing an edge-triggered interrupt for which we
		 * get no EOI maintenance interrupt. In that case,
		 * while the IRQ is already on the VCPU's AP list, the
		 * VCPU could have EOI'ed the original interrupt and
		 * won't see this one until it exits for some other
		 * reason.
		 */
333 334
		if (vcpu) {
			kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
335
			kvm_vcpu_kick(vcpu);
336
		}
337 338 339 340 341 342 343
		return false;
	}

	/*
	 * We must unlock the irq lock to take the ap_list_lock where
	 * we are going to insert this new pending interrupt.
	 */
344
	spin_unlock_irqrestore(&irq->irq_lock, flags);
345 346 347

	/* someone can do stuff here, which we re-check below */

348
	spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	spin_lock(&irq->irq_lock);

	/*
	 * Did something change behind our backs?
	 *
	 * There are two cases:
	 * 1) The irq lost its pending state or was disabled behind our
	 *    backs and/or it was queued to another VCPU's ap_list.
	 * 2) Someone changed the affinity on this irq behind our
	 *    backs and we are now holding the wrong ap_list_lock.
	 *
	 * In both cases, drop the locks and retry.
	 */

	if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
		spin_unlock(&irq->irq_lock);
365
		spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
366

367
		spin_lock_irqsave(&irq->irq_lock, flags);
368 369 370
		goto retry;
	}

371 372 373 374 375
	/*
	 * Grab a reference to the irq to reflect the fact that it is
	 * now in the ap_list.
	 */
	vgic_get_irq_kref(irq);
376 377 378 379
	list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
	irq->vcpu = vcpu;

	spin_unlock(&irq->irq_lock);
380
	spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
381

382
	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
383 384 385 386 387
	kvm_vcpu_kick(vcpu);

	return true;
}

388 389 390 391 392 393 394 395 396
/**
 * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
 * @kvm:     The VM structure pointer
 * @cpuid:   The CPU for PPIs
 * @intid:   The INTID to inject a new state to.
 * @level:   Edge-triggered:  true:  to trigger the interrupt
 *			      false: to ignore the call
 *	     Level-sensitive  true:  raise the input signal
 *			      false: lower the input signal
397 398 399
 * @owner:   The opaque pointer to the owner of the IRQ being raised to verify
 *           that the caller is allowed to inject this IRQ.  Userspace
 *           injections will have owner == NULL.
400 401 402 403 404 405
 *
 * The VGIC is not concerned with devices being active-LOW or active-HIGH for
 * level-sensitive interrupts.  You can think of the level parameter as 1
 * being HIGH and 0 being LOW and all devices being active-HIGH.
 */
int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
406
			bool level, void *owner)
407 408 409
{
	struct kvm_vcpu *vcpu;
	struct vgic_irq *irq;
410
	unsigned long flags;
411 412 413 414
	int ret;

	trace_vgic_update_irq_pending(cpuid, intid, level);

415 416 417 418
	ret = vgic_lazy_init(kvm);
	if (ret)
		return ret;

419 420 421 422 423 424 425 426
	vcpu = kvm_get_vcpu(kvm, cpuid);
	if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
		return -EINVAL;

	irq = vgic_get_irq(kvm, vcpu, intid);
	if (!irq)
		return -EINVAL;

427
	spin_lock_irqsave(&irq->irq_lock, flags);
428

429
	if (!vgic_validate_injection(irq, level, owner)) {
430
		/* Nothing to see here, move along... */
431
		spin_unlock_irqrestore(&irq->irq_lock, flags);
432
		vgic_put_irq(kvm, irq);
433 434 435
		return 0;
	}

436
	if (irq->config == VGIC_CONFIG_LEVEL)
437
		irq->line_level = level;
438 439
	else
		irq->pending_latch = true;
440

441
	vgic_queue_irq_unlock(kvm, irq, flags);
442
	vgic_put_irq(kvm, irq);
443 444 445 446

	return 0;
}

447 448
/* @irq->irq_lock must be held */
static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
449 450
			    unsigned int host_irq,
			    bool (*get_input_level)(int vindid))
451
{
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	struct irq_desc *desc;
	struct irq_data *data;

	/*
	 * Find the physical IRQ number corresponding to @host_irq
	 */
	desc = irq_to_desc(host_irq);
	if (!desc) {
		kvm_err("%s: no interrupt descriptor\n", __func__);
		return -EINVAL;
	}
	data = irq_desc_get_irq_data(desc);
	while (data->parent_data)
		data = data->parent_data;

	irq->hw = true;
	irq->host_irq = host_irq;
	irq->hwintid = data->hwirq;
470
	irq->get_input_level = get_input_level;
471 472 473 474 475 476 477 478
	return 0;
}

/* @irq->irq_lock must be held */
static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
{
	irq->hw = false;
	irq->hwintid = 0;
479
	irq->get_input_level = NULL;
480 481 482
}

int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
483
			  u32 vintid, bool (*get_input_level)(int vindid))
484 485
{
	struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
486
	unsigned long flags;
487
	int ret;
488 489 490

	BUG_ON(!irq);

491
	spin_lock_irqsave(&irq->irq_lock, flags);
492
	ret = kvm_vgic_map_irq(vcpu, irq, host_irq, get_input_level);
493
	spin_unlock_irqrestore(&irq->irq_lock, flags);
494
	vgic_put_irq(vcpu->kvm, irq);
495

496
	return ret;
497 498
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
/**
 * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
 * @vcpu: The VCPU pointer
 * @vintid: The INTID of the interrupt
 *
 * Reset the active and pending states of a mapped interrupt.  Kernel
 * subsystems injecting mapped interrupts should reset their interrupt lines
 * when we are doing a reset of the VM.
 */
void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
{
	struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
	unsigned long flags;

	if (!irq->hw)
		goto out;

	spin_lock_irqsave(&irq->irq_lock, flags);
	irq->active = false;
	irq->pending_latch = false;
	irq->line_level = false;
	spin_unlock_irqrestore(&irq->irq_lock, flags);
out:
	vgic_put_irq(vcpu->kvm, irq);
}

525
int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
526
{
527
	struct vgic_irq *irq;
528
	unsigned long flags;
529 530 531 532

	if (!vgic_initialized(vcpu->kvm))
		return -EAGAIN;

533
	irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
534 535
	BUG_ON(!irq);

536
	spin_lock_irqsave(&irq->irq_lock, flags);
537
	kvm_vgic_unmap_irq(irq);
538
	spin_unlock_irqrestore(&irq->irq_lock, flags);
539
	vgic_put_irq(vcpu->kvm, irq);
540 541 542 543

	return 0;
}

544 545 546 547 548 549 550 551 552 553 554 555 556
/**
 * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
 *
 * @vcpu:   Pointer to the VCPU (used for PPIs)
 * @intid:  The virtual INTID identifying the interrupt (PPI or SPI)
 * @owner:  Opaque pointer to the owner
 *
 * Returns 0 if intid is not already used by another in-kernel device and the
 * owner is set, otherwise returns an error code.
 */
int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
{
	struct vgic_irq *irq;
557
	unsigned long flags;
558 559 560 561 562 563 564 565 566 567
	int ret = 0;

	if (!vgic_initialized(vcpu->kvm))
		return -EAGAIN;

	/* SGIs and LPIs cannot be wired up to any device */
	if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
		return -EINVAL;

	irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
568
	spin_lock_irqsave(&irq->irq_lock, flags);
569 570 571 572
	if (irq->owner && irq->owner != owner)
		ret = -EEXIST;
	else
		irq->owner = owner;
573
	spin_unlock_irqrestore(&irq->irq_lock, flags);
574 575 576 577

	return ret;
}

578 579 580 581 582 583 584 585 586 587 588 589
/**
 * vgic_prune_ap_list - Remove non-relevant interrupts from the list
 *
 * @vcpu: The VCPU pointer
 *
 * Go over the list of "interesting" interrupts, and prune those that we
 * won't have to consider in the near future.
 */
static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
{
	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
	struct vgic_irq *irq, *tmp;
590
	unsigned long flags;
591 592

retry:
593
	spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
594 595 596

	list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
		struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
597
		bool target_vcpu_needs_kick = false;
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

		spin_lock(&irq->irq_lock);

		BUG_ON(vcpu != irq->vcpu);

		target_vcpu = vgic_target_oracle(irq);

		if (!target_vcpu) {
			/*
			 * We don't need to process this interrupt any
			 * further, move it off the list.
			 */
			list_del(&irq->ap_list);
			irq->vcpu = NULL;
			spin_unlock(&irq->irq_lock);
613 614 615 616 617 618 619 620 621

			/*
			 * This vgic_put_irq call matches the
			 * vgic_get_irq_kref in vgic_queue_irq_unlock,
			 * where we added the LPI to the ap_list. As
			 * we remove the irq from the list, we drop
			 * also drop the refcount.
			 */
			vgic_put_irq(vcpu->kvm, irq);
622 623 624 625 626 627 628 629 630 631 632 633
			continue;
		}

		if (target_vcpu == vcpu) {
			/* We're on the right CPU */
			spin_unlock(&irq->irq_lock);
			continue;
		}

		/* This interrupt looks like it has to be migrated. */

		spin_unlock(&irq->irq_lock);
634
		spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
635 636 637 638 639 640 641 642 643 644 645 646 647

		/*
		 * Ensure locking order by always locking the smallest
		 * ID first.
		 */
		if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
			vcpuA = vcpu;
			vcpuB = target_vcpu;
		} else {
			vcpuA = target_vcpu;
			vcpuB = vcpu;
		}

648
		spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
		spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
				 SINGLE_DEPTH_NESTING);
		spin_lock(&irq->irq_lock);

		/*
		 * If the affinity has been preserved, move the
		 * interrupt around. Otherwise, it means things have
		 * changed while the interrupt was unlocked, and we
		 * need to replay this.
		 *
		 * In all cases, we cannot trust the list not to have
		 * changed, so we restart from the beginning.
		 */
		if (target_vcpu == vgic_target_oracle(irq)) {
			struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;

			list_del(&irq->ap_list);
			irq->vcpu = target_vcpu;
			list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
668
			target_vcpu_needs_kick = true;
669 670 671 672
		}

		spin_unlock(&irq->irq_lock);
		spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
673
		spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
674 675 676 677 678 679

		if (target_vcpu_needs_kick) {
			kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
			kvm_vcpu_kick(target_vcpu);
		}

680 681 682
		goto retry;
	}

683
	spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
684 685 686 687
}

static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
{
688 689 690 691
	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_fold_lr_state(vcpu);
	else
		vgic_v3_fold_lr_state(vcpu);
692 693 694 695 696 697 698
}

/* Requires the irq_lock to be held. */
static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
				    struct vgic_irq *irq, int lr)
{
	DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
699

700 701 702 703
	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_populate_lr(vcpu, irq, lr);
	else
		vgic_v3_populate_lr(vcpu, irq, lr);
704 705 706 707
}

static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
{
708 709 710 711
	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_clear_lr(vcpu, lr);
	else
		vgic_v3_clear_lr(vcpu, lr);
712 713 714 715
}

static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
{
716 717 718 719
	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_set_underflow(vcpu);
	else
		vgic_v3_set_underflow(vcpu);
720 721
}

722 723 724 725 726 727 728 729
static inline void vgic_set_npie(struct kvm_vcpu *vcpu)
{
	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_set_npie(vcpu);
	else
		vgic_v3_set_npie(vcpu);
}

730
/* Requires the ap_list_lock to be held. */
731 732
static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
				 bool *multi_sgi)
733 734 735 736 737
{
	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
	struct vgic_irq *irq;
	int count = 0;

738 739
	*multi_sgi = false;

740 741 742 743 744
	DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));

	list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
		spin_lock(&irq->irq_lock);
		/* GICv2 SGIs can count for more than one... */
745 746 747 748 749 750
		if (vgic_irq_is_sgi(irq->intid) && irq->source) {
			int w = hweight8(irq->source);

			count += w;
			*multi_sgi |= (w > 1);
		} else {
751
			count++;
752
		}
753 754 755 756 757 758 759 760 761 762
		spin_unlock(&irq->irq_lock);
	}
	return count;
}

/* Requires the VCPU's ap_list_lock to be held. */
static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
{
	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
	struct vgic_irq *irq;
763 764 765 766
	int count;
	bool npie = false;
	bool multi_sgi;
	u8 prio = 0xff;
767 768 769

	DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));

770 771
	count = compute_ap_list_depth(vcpu, &multi_sgi);
	if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
772 773
		vgic_sort_ap_list(vcpu);

774 775
	count = 0;

776 777 778 779
	list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
		spin_lock(&irq->irq_lock);

		/*
780 781 782 783 784
		 * If we have multi-SGIs in the pipeline, we need to
		 * guarantee that they are all seen before any IRQ of
		 * lower priority. In that case, we need to filter out
		 * these interrupts by exiting early. This is easy as
		 * the AP list has been sorted already.
785
		 */
786 787 788 789 790 791
		if (multi_sgi && irq->priority > prio) {
			spin_unlock(&irq->irq_lock);
			break;
		}

		if (likely(vgic_target_oracle(irq) == vcpu)) {
792 793
			vgic_populate_lr(vcpu, irq, count++);

794 795 796 797 798 799
			if (irq->source) {
				npie = true;
				prio = irq->priority;
			}
		}

800 801
		spin_unlock(&irq->irq_lock);

802 803 804 805
		if (count == kvm_vgic_global_state.nr_lr) {
			if (!list_is_last(&irq->ap_list,
					  &vgic_cpu->ap_list_head))
				vgic_set_underflow(vcpu);
806
			break;
807
		}
808 809
	}

810 811 812
	if (npie)
		vgic_set_npie(vcpu);

813 814 815 816 817 818 819
	vcpu->arch.vgic_cpu.used_lrs = count;

	/* Nuke remaining LRs */
	for ( ; count < kvm_vgic_global_state.nr_lr; count++)
		vgic_clear_lr(vcpu, count);
}

820 821 822 823 824 825 826 827 828 829
static inline bool can_access_vgic_from_kernel(void)
{
	/*
	 * GICv2 can always be accessed from the kernel because it is
	 * memory-mapped, and VHE systems can access GICv3 EL2 system
	 * registers.
	 */
	return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
}

830 831 832 833
static inline void vgic_save_state(struct kvm_vcpu *vcpu)
{
	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
		vgic_v2_save_state(vcpu);
834 835
	else
		__vgic_v3_save_state(vcpu);
836 837
}

838 839 840
/* Sync back the hardware VGIC state into our emulation after a guest's run. */
void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
{
841 842
	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;

843 844
	WARN_ON(vgic_v4_sync_hwstate(vcpu));

845 846
	/* An empty ap_list_head implies used_lrs == 0 */
	if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
847 848
		return;

849 850 851
	if (can_access_vgic_from_kernel())
		vgic_save_state(vcpu);

852 853
	if (vgic_cpu->used_lrs)
		vgic_fold_lr_state(vcpu);
854 855 856
	vgic_prune_ap_list(vcpu);
}

857 858 859 860
static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
{
	if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
		vgic_v2_restore_state(vcpu);
861 862
	else
		__vgic_v3_restore_state(vcpu);
863 864
}

865 866 867
/* Flush our emulation state into the GIC hardware before entering the guest. */
void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
{
868 869
	WARN_ON(vgic_v4_flush_hwstate(vcpu));

870 871 872 873 874 875 876 877 878 879
	/*
	 * If there are no virtual interrupts active or pending for this
	 * VCPU, then there is no work to do and we can bail out without
	 * taking any lock.  There is a potential race with someone injecting
	 * interrupts to the VCPU, but it is a benign race as the VCPU will
	 * either observe the new interrupt before or after doing this check,
	 * and introducing additional synchronization mechanism doesn't change
	 * this.
	 */
	if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
880
		return;
881

882 883
	DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());

884 885 886
	spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
	vgic_flush_lr_state(vcpu);
	spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
887

888 889
	if (can_access_vgic_from_kernel())
		vgic_restore_state(vcpu);
890
}
891

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
void kvm_vgic_load(struct kvm_vcpu *vcpu)
{
	if (unlikely(!vgic_initialized(vcpu->kvm)))
		return;

	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_load(vcpu);
	else
		vgic_v3_load(vcpu);
}

void kvm_vgic_put(struct kvm_vcpu *vcpu)
{
	if (unlikely(!vgic_initialized(vcpu->kvm)))
		return;

	if (kvm_vgic_global_state.type == VGIC_V2)
		vgic_v2_put(vcpu);
	else
		vgic_v3_put(vcpu);
}

914 915 916 917 918
int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
{
	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
	struct vgic_irq *irq;
	bool pending = false;
919
	unsigned long flags;
920 921 922 923

	if (!vcpu->kvm->arch.vgic.enabled)
		return false;

924 925 926
	if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
		return true;

927
	spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
928 929 930

	list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
		spin_lock(&irq->irq_lock);
931
		pending = irq_is_pending(irq) && irq->enabled;
932 933 934 935 936 937
		spin_unlock(&irq->irq_lock);

		if (pending)
			break;
	}

938
	spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
939 940 941

	return pending;
}
942 943 944 945 946 947 948 949 950 951 952

void vgic_kick_vcpus(struct kvm *kvm)
{
	struct kvm_vcpu *vcpu;
	int c;

	/*
	 * We've injected an interrupt, time to find out who deserves
	 * a good kick...
	 */
	kvm_for_each_vcpu(c, vcpu, kvm) {
953 954
		if (kvm_vgic_vcpu_pending_irq(vcpu)) {
			kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
955
			kvm_vcpu_kick(vcpu);
956
		}
957 958
	}
}
959

960
bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
961
{
962
	struct vgic_irq *irq;
963
	bool map_is_active;
964
	unsigned long flags;
965

966 967 968
	if (!vgic_initialized(vcpu->kvm))
		return false;

969
	irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
970
	spin_lock_irqsave(&irq->irq_lock, flags);
971
	map_is_active = irq->hw && irq->active;
972
	spin_unlock_irqrestore(&irq->irq_lock, flags);
973
	vgic_put_irq(vcpu->kvm, irq);
974 975 976

	return map_is_active;
}
977