1 /*-
2  * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
3  * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
4  * Copyright (c) 2017-2022 Intel Corporation.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef VIOAPIC_H
31 #define VIOAPIC_H
32 
33 /**
34  * @file vioapic.h
35  *
36  * @brief public APIs for virtual I/O APIC
37  */
38 
39 #include <asm/apicreg.h>
40 #include <asm/ioapic.h>
41 #include <util.h>
42 
43 #define	VIOAPIC_BASE	0xFEC00000UL
44 #define	VIOAPIC_SIZE	4096UL
45 
46 #define REDIR_ENTRIES_HW	120U /* Service VM align with native ioapic */
47 #define STATE_BITMAP_SIZE	INT_DIV_ROUNDUP(REDIR_ENTRIES_HW, 64U)
48 
49 #define IOAPIC_RTE_LOW_INTVEC	((uint32_t)IOAPIC_RTE_INTVEC)
50 
51 /*
52  * id field is used to emulate the IOAPIC_ID register of vIOAPIC
53  */
54 
55 struct acrn_single_vioapic {
56 	spinlock_t	lock;
57 	struct acrn_vm  *vm;
58 	struct ioapic_info chipinfo;
59 	uint32_t	ioregsel;
60 	union ioapic_rte rtbl[REDIR_ENTRIES_HW];
61 	/* pin_state status bitmap: 1 - high, 0 - low */
62 	uint64_t pin_state[STATE_BITMAP_SIZE];
63 };
64 
65 /*
66  * ioapic_num represents the number of IO-APICs emulated for the VM.
67  * nr_gsi represents the maximum number of GSI emulated for the VM.
68  */
69 struct acrn_vioapics {
70 	uint8_t ioapic_num;
71 	uint32_t nr_gsi;
72 	struct acrn_single_vioapic vioapic_array[CONFIG_MAX_IOAPIC_NUM];
73 };
74 
75 void dump_vioapic(struct acrn_vm *vm);
76 void vioapic_init(struct acrn_vm *vm);
77 void reset_vioapics(const struct acrn_vm *vm);
78 
79 
80 /**
81  * @brief virtual I/O APIC
82  *
83  * @addtogroup acrn_vioapic ACRN vIOAPIC
84  * @{
85  */
86 
87 /**
88  * @brief Set vIOAPIC IRQ line status.
89  *
90  * @param[in] vm        Pointer to target VM
91  * @param[in] vgsi	GSI for the virtual interrupt
92  * @param[in] operation Action options: GSI_SET_HIGH/GSI_SET_LOW/
93  *			GSI_RAISING_PULSE/GSI_FALLING_PULSE
94  *
95  * @pre irqline < vioapic_pincount(vm)
96  */
97 void	vioapic_set_irqline_lock(const struct acrn_vm *vm, uint32_t vgsi, uint32_t operation);
98 
99 /**
100  * @brief Set vIOAPIC IRQ line status.
101  *
102  * Similar with vioapic_set_irqline_lock(),but would not make sure
103  * operation be done with ioapic lock.
104  *
105  * @param[in] vm        Pointer to target VM
106  * @param[in] vgsi      GSI for the virtual interrupt
107  * @param[in] operation Action options: GSI_SET_HIGH/GSI_SET_LOW/
108  *			GSI_RAISING_PULSE/GSI_FALLING_PULSE
109  *
110  * @pre irqline < vioapic_pincount(vm)
111  */
112 void	vioapic_set_irqline_nolock(const struct acrn_vm *vm, uint32_t vgsi, uint32_t operation);
113 
114 uint32_t get_vm_gsicount(const struct acrn_vm *vm);
115 void	vioapic_broadcast_eoi(const struct acrn_vm *vm, uint32_t vector);
116 void	vioapic_get_rte(const struct acrn_vm *vm, uint32_t vgsi, union ioapic_rte *rte);
117 int32_t	vioapic_mmio_access_handler(struct io_request *io_req, void *handler_private_data);
118 struct acrn_single_vioapic *vgsi_to_vioapic_and_vpin(const struct acrn_vm *vm, uint32_t vgsi, uint32_t *vpin);
119 
120 /**
121  * @}
122  */
123 /* End of acrn_vioapic */
124 #endif /* VIOAPIC_H */
125