xref: /openbmc/linux/arch/x86/kvm/ioapic.h (revision 901181b7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_IO_APIC_H
3 #define __KVM_IO_APIC_H
4 
5 #include <linux/kvm_host.h>
6 #include <kvm/iodev.h>
7 #include "irq.h"
8 
9 struct kvm;
10 struct kvm_vcpu;
11 
12 #define IOAPIC_NUM_PINS  KVM_IOAPIC_NUM_PINS
13 #define MAX_NR_RESERVED_IOAPIC_PINS KVM_MAX_IRQ_ROUTES
14 #define IOAPIC_VERSION_ID 0x11	/* IOAPIC version */
15 #define IOAPIC_EDGE_TRIG  0
16 #define IOAPIC_LEVEL_TRIG 1
17 
18 #define IOAPIC_DEFAULT_BASE_ADDRESS  0xfec00000
19 #define IOAPIC_MEM_LENGTH            0x100
20 
21 /* Direct registers. */
22 #define IOAPIC_REG_SELECT  0x00
23 #define IOAPIC_REG_WINDOW  0x10
24 
25 /* Indirect registers. */
26 #define IOAPIC_REG_APIC_ID 0x00	/* x86 IOAPIC only */
27 #define IOAPIC_REG_VERSION 0x01
28 #define IOAPIC_REG_ARB_ID  0x02	/* x86 IOAPIC only */
29 
30 /*ioapic delivery mode*/
31 #define	IOAPIC_FIXED			0x0
32 #define	IOAPIC_LOWEST_PRIORITY		0x1
33 #define	IOAPIC_PMI			0x2
34 #define	IOAPIC_NMI			0x4
35 #define	IOAPIC_INIT			0x5
36 #define	IOAPIC_EXTINT			0x7
37 
38 #define RTC_GSI 8
39 
40 struct dest_map {
41 	/* vcpu bitmap where IRQ has been sent */
42 	DECLARE_BITMAP(map, KVM_MAX_VCPU_IDS);
43 
44 	/*
45 	 * Vector sent to a given vcpu, only valid when
46 	 * the vcpu's bit in map is set
47 	 */
48 	u8 vectors[KVM_MAX_VCPU_IDS];
49 };
50 
51 
52 struct rtc_status {
53 	int pending_eoi;
54 	struct dest_map dest_map;
55 };
56 
57 union kvm_ioapic_redirect_entry {
58 	u64 bits;
59 	struct {
60 		u8 vector;
61 		u8 delivery_mode:3;
62 		u8 dest_mode:1;
63 		u8 delivery_status:1;
64 		u8 polarity:1;
65 		u8 remote_irr:1;
66 		u8 trig_mode:1;
67 		u8 mask:1;
68 		u8 reserve:7;
69 		u8 reserved[4];
70 		u8 dest_id;
71 	} fields;
72 };
73 
74 struct kvm_ioapic {
75 	u64 base_address;
76 	u32 ioregsel;
77 	u32 id;
78 	u32 irr;
79 	u32 pad;
80 	union kvm_ioapic_redirect_entry redirtbl[IOAPIC_NUM_PINS];
81 	unsigned long irq_states[IOAPIC_NUM_PINS];
82 	struct kvm_io_device dev;
83 	struct kvm *kvm;
84 	void (*ack_notifier)(void *opaque, int irq);
85 	spinlock_t lock;
86 	struct rtc_status rtc_status;
87 	struct delayed_work eoi_inject;
88 	u32 irq_eoi[IOAPIC_NUM_PINS];
89 	u32 irr_delivered;
90 };
91 
92 #ifdef DEBUG
93 #define ASSERT(x)  							\
94 do {									\
95 	if (!(x)) {							\
96 		printk(KERN_EMERG "assertion failed %s: %d: %s\n",	\
97 		       __FILE__, __LINE__, #x);				\
98 		BUG();							\
99 	}								\
100 } while (0)
101 #else
102 #define ASSERT(x) do { } while (0)
103 #endif
104 
105 static inline int ioapic_in_kernel(struct kvm *kvm)
106 {
107 	return irqchip_kernel(kvm);
108 }
109 
110 void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu);
111 void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector,
112 			int trigger_mode);
113 int kvm_ioapic_init(struct kvm *kvm);
114 void kvm_ioapic_destroy(struct kvm *kvm);
115 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
116 		       int level, bool line_status);
117 void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id);
118 void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state);
119 void kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state);
120 void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu,
121 			   ulong *ioapic_handled_vectors);
122 void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
123 			    ulong *ioapic_handled_vectors);
124 #endif
125