xref: /linux/arch/x86/kvm/irq.h (revision 63307d015b91e626c97bb82e88054af3d0b74643)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * irq.h: in kernel interrupt controller related definitions
4  * Copyright (c) 2007, Intel Corporation.
5  *
6  * Authors:
7  *   Yaozu (Eddie) Dong <Eddie.dong@intel.com>
8  */
9 
10 #ifndef __IRQ_H
11 #define __IRQ_H
12 
13 #include <linux/mm_types.h>
14 #include <linux/hrtimer.h>
15 #include <linux/kvm_host.h>
16 #include <linux/spinlock.h>
17 
18 #include <kvm/iodev.h>
19 #include "ioapic.h"
20 #include "lapic.h"
21 
22 #define PIC_NUM_PINS 16
23 #define SELECT_PIC(irq) \
24 	((irq) < 8 ? KVM_IRQCHIP_PIC_MASTER : KVM_IRQCHIP_PIC_SLAVE)
25 
26 struct kvm;
27 struct kvm_vcpu;
28 
29 struct kvm_kpic_state {
30 	u8 last_irr;	/* edge detection */
31 	u8 irr;		/* interrupt request register */
32 	u8 imr;		/* interrupt mask register */
33 	u8 isr;		/* interrupt service register */
34 	u8 priority_add;	/* highest irq priority */
35 	u8 irq_base;
36 	u8 read_reg_select;
37 	u8 poll;
38 	u8 special_mask;
39 	u8 init_state;
40 	u8 auto_eoi;
41 	u8 rotate_on_auto_eoi;
42 	u8 special_fully_nested_mode;
43 	u8 init4;		/* true if 4 byte init */
44 	u8 elcr;		/* PIIX edge/trigger selection */
45 	u8 elcr_mask;
46 	u8 isr_ack;	/* interrupt ack detection */
47 	struct kvm_pic *pics_state;
48 };
49 
50 struct kvm_pic {
51 	spinlock_t lock;
52 	bool wakeup_needed;
53 	unsigned pending_acks;
54 	struct kvm *kvm;
55 	struct kvm_kpic_state pics[2]; /* 0 is master pic, 1 is slave pic */
56 	int output;		/* intr from master PIC */
57 	struct kvm_io_device dev_master;
58 	struct kvm_io_device dev_slave;
59 	struct kvm_io_device dev_eclr;
60 	void (*ack_notifier)(void *opaque, int irq);
61 	unsigned long irq_states[PIC_NUM_PINS];
62 };
63 
64 int kvm_pic_init(struct kvm *kvm);
65 void kvm_pic_destroy(struct kvm *kvm);
66 int kvm_pic_read_irq(struct kvm *kvm);
67 void kvm_pic_update_irq(struct kvm_pic *s);
68 
69 static inline int pic_in_kernel(struct kvm *kvm)
70 {
71 	int mode = kvm->arch.irqchip_mode;
72 
73 	/* Matches smp_wmb() when setting irqchip_mode */
74 	smp_rmb();
75 	return mode == KVM_IRQCHIP_KERNEL;
76 }
77 
78 static inline int irqchip_split(struct kvm *kvm)
79 {
80 	int mode = kvm->arch.irqchip_mode;
81 
82 	/* Matches smp_wmb() when setting irqchip_mode */
83 	smp_rmb();
84 	return mode == KVM_IRQCHIP_SPLIT;
85 }
86 
87 static inline int irqchip_kernel(struct kvm *kvm)
88 {
89 	int mode = kvm->arch.irqchip_mode;
90 
91 	/* Matches smp_wmb() when setting irqchip_mode */
92 	smp_rmb();
93 	return mode == KVM_IRQCHIP_KERNEL;
94 }
95 
96 static inline int irqchip_in_kernel(struct kvm *kvm)
97 {
98 	int mode = kvm->arch.irqchip_mode;
99 
100 	/* Matches smp_wmb() when setting irqchip_mode */
101 	smp_rmb();
102 	return mode != KVM_IRQCHIP_NONE;
103 }
104 
105 bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args);
106 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
107 void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu);
108 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu);
109 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu);
110 void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu);
111 void __kvm_migrate_timers(struct kvm_vcpu *vcpu);
112 
113 int apic_has_pending_timer(struct kvm_vcpu *vcpu);
114 
115 int kvm_setup_default_irq_routing(struct kvm *kvm);
116 int kvm_setup_empty_irq_routing(struct kvm *kvm);
117 
118 #endif
119