2018-06-14 18:06:26 +08:00
|
|
|
/*
|
2022-04-05 18:59:24 +08:00
|
|
|
* Copyright (C) 2018 Intel Corporation.
|
2018-06-14 18:06:26 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PTDEV_H
|
|
|
|
#define PTDEV_H
|
2019-02-03 15:07:13 +08:00
|
|
|
#include <list.h>
|
2021-04-23 15:50:57 +08:00
|
|
|
#include <asm/lib/spinlock.h>
|
2021-04-12 13:40:00 +08:00
|
|
|
#include <timer.h>
|
2021-09-03 15:48:25 +08:00
|
|
|
#include <vacpi.h>
|
2018-06-14 18:06:26 +08:00
|
|
|
|
2020-03-02 22:14:19 +08:00
|
|
|
|
|
|
|
enum intx_ctlr {
|
|
|
|
INTX_CTLR_IOAPIC = 0U,
|
|
|
|
INTX_CTLR_PIC
|
|
|
|
};
|
|
|
|
|
2018-08-20 10:47:35 +08:00
|
|
|
#define PTDEV_INTR_MSI (1U << 0U)
|
|
|
|
#define PTDEV_INTR_INTX (1U << 1U)
|
2018-06-14 18:06:26 +08:00
|
|
|
|
2021-08-12 17:30:05 +08:00
|
|
|
#define GPU_OPREGION_SIZE 0x5000U
|
2021-09-03 15:48:25 +08:00
|
|
|
#define GPU_OPREGION_GPA (VIRT_ACPI_DATA_ADDR - GPU_OPREGION_SIZE)
|
2021-08-12 17:30:05 +08:00
|
|
|
#define PCIR_ASLS_CTL 0xfcU /* register offset in PCIe configuration space for Opregion base address */
|
|
|
|
#define PCIM_ASLS_OPREGION_MASK 0xfffff000U /* opregion need 4KB aligned */
|
|
|
|
|
2018-11-19 11:19:10 +08:00
|
|
|
#define INVALID_PTDEV_ENTRY_ID 0xffffU
|
|
|
|
|
2018-08-20 10:47:35 +08:00
|
|
|
#define DEFINE_MSI_SID(name, a, b) \
|
2018-09-05 10:55:29 +08:00
|
|
|
union source_id (name) = {.msi_id = {.bdf = (a), .entry_nr = (b)} }
|
2018-08-20 10:47:35 +08:00
|
|
|
|
2020-03-02 22:14:19 +08:00
|
|
|
#define DEFINE_INTX_SID(name, a, b) \
|
2020-03-12 04:03:43 +08:00
|
|
|
union source_id (name) = {.intx_id = {.gsi = (a), .ctlr = (b)} }
|
2018-08-20 10:47:35 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
union irte_index {
|
|
|
|
uint16_t index;
|
|
|
|
struct {
|
|
|
|
uint16_t index_low:15;
|
|
|
|
uint16_t index_high:1;
|
|
|
|
} bits __packed;
|
|
|
|
};
|
|
|
|
|
2020-03-02 22:14:19 +08:00
|
|
|
|
2018-08-20 10:47:35 +08:00
|
|
|
union source_id {
|
2019-01-10 10:57:49 +08:00
|
|
|
uint64_t value;
|
2018-08-20 10:47:35 +08:00
|
|
|
struct {
|
|
|
|
uint16_t bdf;
|
|
|
|
uint16_t entry_nr;
|
2019-01-10 10:57:49 +08:00
|
|
|
uint32_t reserved;
|
2018-08-20 10:47:35 +08:00
|
|
|
} msi_id;
|
2020-03-02 22:14:19 +08:00
|
|
|
/*
|
|
|
|
* ctlr indicates if the source of interrupt is IO-APIC or PIC
|
|
|
|
* pin indicates the pin number of interrupt controller determined by ctlr
|
|
|
|
*/
|
2018-08-20 10:47:35 +08:00
|
|
|
struct {
|
2020-03-02 22:14:19 +08:00
|
|
|
enum intx_ctlr ctlr;
|
2020-03-12 04:03:43 +08:00
|
|
|
uint32_t gsi;
|
2018-08-20 10:47:35 +08:00
|
|
|
} intx_id;
|
|
|
|
};
|
|
|
|
|
2019-01-26 17:31:14 +08:00
|
|
|
/*
|
|
|
|
* Macros for bits in union msi_addr_reg
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MSI_ADDR_BASE 0xfeeUL /* Base address for MSI messages */
|
|
|
|
#define MSI_ADDR_RH 0x1U /* Redirection Hint */
|
|
|
|
#define MSI_ADDR_DESTMODE_LOGICAL 0x1U /* Destination Mode: Logical*/
|
|
|
|
#define MSI_ADDR_DESTMODE_PHYS 0x0U /* Destination Mode: Physical*/
|
|
|
|
|
|
|
|
union msi_addr_reg {
|
|
|
|
uint64_t full;
|
|
|
|
struct {
|
|
|
|
uint32_t rsvd_1:2;
|
|
|
|
uint32_t dest_mode:1;
|
|
|
|
uint32_t rh:1;
|
|
|
|
uint32_t rsvd_2:8;
|
|
|
|
uint32_t dest_field:8;
|
|
|
|
uint32_t addr_base:12;
|
|
|
|
uint32_t hi_32;
|
|
|
|
} bits __packed;
|
2019-01-16 04:51:25 +08:00
|
|
|
struct {
|
|
|
|
uint32_t rsvd_1:2;
|
|
|
|
uint32_t intr_index_high:1;
|
|
|
|
uint32_t shv:1;
|
|
|
|
uint32_t intr_format:1;
|
|
|
|
uint32_t intr_index_low:15;
|
|
|
|
uint32_t constant:12;
|
|
|
|
uint32_t hi_32;
|
|
|
|
} ir_bits __packed;
|
|
|
|
|
2019-01-26 17:31:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Macros for bits in union msi_data_reg
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MSI_DATA_DELMODE_FIXED 0x0U /* Delivery Mode: Fixed */
|
|
|
|
#define MSI_DATA_DELMODE_LOPRI 0x1U /* Delivery Mode: Low Priority */
|
|
|
|
#define MSI_DATA_TRGRMODE_EDGE 0x0U /* Trigger Mode: Edge */
|
|
|
|
#define MSI_DATA_TRGRMODE_LEVEL 0x1U /* Trigger Mode: Level */
|
|
|
|
|
|
|
|
union msi_data_reg {
|
|
|
|
uint32_t full;
|
|
|
|
struct {
|
|
|
|
uint32_t vector:8;
|
|
|
|
uint32_t delivery_mode:3;
|
|
|
|
uint32_t rsvd_1:3;
|
|
|
|
uint32_t level:1;
|
|
|
|
uint32_t trigger_mode:1;
|
|
|
|
uint32_t rsvd_2:16;
|
|
|
|
} bits __packed;
|
|
|
|
};
|
|
|
|
|
2020-04-30 11:06:02 +08:00
|
|
|
struct msi_info {
|
|
|
|
union msi_addr_reg addr;
|
|
|
|
union msi_data_reg data;
|
2018-06-14 18:06:26 +08:00
|
|
|
};
|
|
|
|
|
2019-04-23 00:28:35 +08:00
|
|
|
struct ptirq_remapping_info;
|
|
|
|
typedef void (*ptirq_arch_release_fn_t)(const struct ptirq_remapping_info *entry);
|
|
|
|
|
2018-06-14 18:06:26 +08:00
|
|
|
/* entry per each allocated irq/vector
|
|
|
|
* it represents a pass-thru device's remapping data entry which collecting
|
|
|
|
* information related with its vm and msi/intx mapping & interaction nodes
|
|
|
|
* with interrupt handler and softirq.
|
|
|
|
*/
|
2018-11-29 15:26:27 +08:00
|
|
|
struct ptirq_remapping_info {
|
2020-05-14 13:31:21 +08:00
|
|
|
struct hlist_node phys_link;
|
|
|
|
struct hlist_node virt_link;
|
2018-11-19 11:19:10 +08:00
|
|
|
uint16_t ptdev_entry_id;
|
2018-08-20 10:47:35 +08:00
|
|
|
uint32_t intr_type;
|
|
|
|
union source_id phys_sid;
|
|
|
|
union source_id virt_sid;
|
2018-11-05 13:28:23 +08:00
|
|
|
struct acrn_vm *vm;
|
2019-07-02 21:37:08 +08:00
|
|
|
bool active; /* true=active, false=inactive*/
|
2018-08-07 14:33:02 +08:00
|
|
|
uint32_t allocated_pirq;
|
2018-09-28 15:20:02 +08:00
|
|
|
uint32_t polarity; /* 0=active high, 1=active low*/
|
2018-06-14 18:06:26 +08:00
|
|
|
struct list_head softirq_node;
|
2020-04-30 11:06:02 +08:00
|
|
|
struct msi_info vmsi;
|
|
|
|
struct msi_info pmsi;
|
2020-05-10 17:29:53 +08:00
|
|
|
uint16_t irte_idx;
|
2018-09-13 16:31:17 +08:00
|
|
|
|
|
|
|
uint64_t intr_count;
|
|
|
|
struct hv_timer intr_delay_timer; /* used for delay intr injection */
|
2019-04-23 00:28:35 +08:00
|
|
|
ptirq_arch_release_fn_t release_cb;
|
2018-06-14 18:06:26 +08:00
|
|
|
};
|
|
|
|
|
2019-07-02 21:37:08 +08:00
|
|
|
static inline bool is_entry_active(const struct ptirq_remapping_info *entry)
|
|
|
|
{
|
|
|
|
return entry->active;
|
|
|
|
}
|
|
|
|
|
2018-12-18 22:22:10 +08:00
|
|
|
extern struct ptirq_remapping_info ptirq_entries[CONFIG_MAX_PT_IRQ_ENTRIES];
|
2018-06-14 18:06:26 +08:00
|
|
|
extern spinlock_t ptdev_lock;
|
|
|
|
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @file ptdev.h
|
|
|
|
*
|
|
|
|
* @brief public APIs for ptdev
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ptdev
|
|
|
|
*
|
|
|
|
* @addtogroup acrn_passthrough
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-05-14 13:31:21 +08:00
|
|
|
/*
|
|
|
|
* @brief Find a ptdev entry by sid
|
|
|
|
*
|
|
|
|
* param[in] intr_type interrupt type of the ptirq entry
|
|
|
|
* param[in] sid source id of the ptirq entry
|
|
|
|
* param[in] vm vm pointer of the ptirq entry if find the ptdev entry by virtual sid
|
|
|
|
*
|
2020-10-07 06:31:11 +08:00
|
|
|
* @retval NULL when no ptirq entry match the sid
|
|
|
|
* @retval ptirq entry when there is available ptirq entry match the sid
|
2020-05-14 13:31:21 +08:00
|
|
|
*
|
|
|
|
* @pre: vm must be NULL when lookup by physical sid, otherwise,
|
|
|
|
* vm must not be NULL when lookup by virtual sid.
|
|
|
|
*/
|
|
|
|
struct ptirq_remapping_info *find_ptirq_entry(uint32_t intr_type,
|
|
|
|
const union source_id *sid, const struct acrn_vm *vm);
|
|
|
|
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Handler of softirq for passthrough device.
|
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* When hypervisor receive a physical interrupt from passthrough device, it
|
2019-10-18 16:20:44 +08:00
|
|
|
* will enqueue a ptirq entry and raise softirq SOFTIRQ_PTDEV. This function
|
|
|
|
* is the handler of the softirq, it handles the interrupt and injects the
|
|
|
|
* virtual into VM.
|
2019-10-18 16:21:23 +08:00
|
|
|
* The handler is registered by calling @ref ptdev_init during hypervisor
|
|
|
|
* initialization.
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* @param[in] pcpu_id physical cpu id of the soft irq
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-11-29 16:42:45 +08:00
|
|
|
void ptirq_softirq(uint16_t pcpu_id);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Passthrough device global data structure initialization.
|
|
|
|
*
|
|
|
|
* During the hypervisor cpu initialization stage, this function:
|
|
|
|
* - init global spinlock for ptdev (on BSP)
|
|
|
|
* - register SOFTIRQ_PTDEV handler (on BSP)
|
|
|
|
* - init the softirq entry list for each CPU
|
|
|
|
*
|
|
|
|
*/
|
2018-06-14 18:06:26 +08:00
|
|
|
void ptdev_init(void);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
2019-10-18 16:21:23 +08:00
|
|
|
* @brief Deactivate and release all ptirq entries for a VM.
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* This function deactivates and releases all ptirq entries for a VM. The function
|
2019-10-18 16:20:44 +08:00
|
|
|
* should only be called after the VM is already down.
|
|
|
|
*
|
|
|
|
* @param[in] vm acrn_vm on which the ptirq entries will be released
|
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* @pre VM is already down
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-11-05 13:28:23 +08:00
|
|
|
void ptdev_release_all_entries(const struct acrn_vm *vm);
|
2018-06-14 18:06:26 +08:00
|
|
|
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Dequeue an entry from per cpu ptdev softirq queue.
|
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* Dequeue an entry from the ptdev softirq queue on the specific physical cpu.
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* @param[in] pcpu_id physical cpu id
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
2020-10-07 06:31:11 +08:00
|
|
|
* @retval NULL when the queue is empty
|
|
|
|
* @retval !NULL when there is available ptirq_remapping_info entry in the queue
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
|
|
|
*/
|
2019-09-03 11:50:01 +08:00
|
|
|
struct ptirq_remapping_info *ptirq_dequeue_softirq(uint16_t pcpu_id);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Allocate a ptirq_remapping_info entry.
|
|
|
|
*
|
|
|
|
* Allocate a ptirq_remapping_info entry for hypervisor to store the remapping information.
|
|
|
|
* The total number of the entries is statically defined as CONFIG_MAX_PT_IRQ_ENTRIES.
|
|
|
|
* Appropriate number should be configured on different platforms.
|
|
|
|
*
|
|
|
|
* @param[in] vm acrn_vm that the entry allocated for.
|
2020-03-05 09:36:16 +08:00
|
|
|
* @param[in] intr_type interrupt type: PTDEV_INTR_MSI or PTDEV_INTR_INTX
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
2020-10-07 06:31:11 +08:00
|
|
|
* @retval NULL when the number of entries allocated is CONFIG_MAX_PT_IRQ_ENTRIES
|
|
|
|
* @retval !NULL when the number of entries allocated is less than CONFIG_MAX_PT_IRQ_ENTRIES
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-11-29 16:42:45 +08:00
|
|
|
struct ptirq_remapping_info *ptirq_alloc_entry(struct acrn_vm *vm, uint32_t intr_type);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Release a ptirq_remapping_info entry.
|
|
|
|
*
|
|
|
|
* @param[in] entry the ptirq_remapping_info entry to release.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-29 16:42:45 +08:00
|
|
|
void ptirq_release_entry(struct ptirq_remapping_info *entry);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Activate a irq for the associated passthrough device.
|
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* After activating the ptirq entry, the physical interrupt irq of passthrough device will be handled
|
2019-10-18 16:20:44 +08:00
|
|
|
* by the handler ptirq_interrupt_handler.
|
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* @param[in] entry the ptirq_remapping_info entry that will be associated with the physical irq.
|
|
|
|
* @param[in] phys_irq physical interrupt irq for the entry
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
2020-10-07 06:31:11 +08:00
|
|
|
* @retval success when return value >=0
|
|
|
|
* @retval failure when return value < 0
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-11-29 16:42:45 +08:00
|
|
|
int32_t ptirq_activate_entry(struct ptirq_remapping_info *entry, uint32_t phys_irq);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief De-activate a irq for the associated passthrough device.
|
|
|
|
*
|
|
|
|
* @param[in] entry the ptirq_remapping_info entry that will be de-activated.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-29 16:42:45 +08:00
|
|
|
void ptirq_deactivate_entry(struct ptirq_remapping_info *entry);
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the interrupt information and store to the buffer provided.
|
|
|
|
*
|
|
|
|
* @param[in] target_vm the VM to get the interrupt information.
|
2020-10-07 06:31:11 +08:00
|
|
|
* @param[out] buffer where interrupt information is stored.
|
2019-10-18 16:20:44 +08:00
|
|
|
* @param[in] buffer_cnt the size of the buffer.
|
|
|
|
*
|
2019-10-18 16:21:23 +08:00
|
|
|
* @retval the actual size the buffer filled with the interrupt information
|
2019-10-18 16:20:44 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-11-29 16:42:45 +08:00
|
|
|
uint32_t ptirq_get_intr_data(const struct acrn_vm *target_vm, uint64_t *buffer, uint32_t buffer_cnt);
|
2018-09-13 16:31:17 +08:00
|
|
|
|
2019-10-18 16:20:44 +08:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2018-06-14 18:06:26 +08:00
|
|
|
#endif /* PTDEV_H */
|