4195 lines · 116.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * Copyright (c) Microsoft Corporation. * * Author: *   Jake Oshins <jakeo@microsoft.com> * * This driver acts as a paravirtual front-end for PCI Express root buses. * When a PCI Express function (either an entire device or an SR-IOV * Virtual Function) is being passed through to the VM, this driver exposes * a new bus to the guest VM.  This is modeled as a root PCI bus because * no bridges are being exposed to the VM.  In fact, with a "Generation 2" * VM within Hyper-V, there may seem to be no PCI bus at all in the VM * until a device as been exposed using this driver. * * Each root PCI bus has its own PCI domain, which is called "Segment" in * the PCI Firmware Specifications.  Thus while each device passed through * to the VM using this front-end will appear at "device 0", the domain will * be unique.  Typically, each bus will have one PCI function on it, though * this driver does support more than one. * * In order to map the interrupts from the device through to the guest VM, * this driver also implements an IRQ Domain, which handles interrupts (either * MSI or MSI-X) associated with the functions on the bus.  As interrupts are * set up, torn down, or reaffined, this driver communicates with the * underlying hypervisor to adjust the mappings in the I/O MMU so that each * interrupt will be delivered to the correct virtual processor at the right * vector.  This driver does not support level-triggered (line-based) * interrupts, and will report that the Interrupt Line register in the * function's configuration space is zero. * * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V * facilities.  For instance, the configuration space of a function exposed * by Hyper-V is mapped into a single page of memory space, and the * read and write handlers for config space must be aware of this mechanism. * Similarly, device setup and teardown involves messages sent to and from * the PCI back-end driver in Hyper-V. */ #include <linux/kernel.h>#include <linux/module.h>#include <linux/pci.h>#include <linux/pci-ecam.h>#include <linux/delay.h>#include <linux/semaphore.h>#include <linux/irq.h>#include <linux/irqchip/irq-msi-lib.h>#include <linux/msi.h>#include <linux/hyperv.h>#include <linux/refcount.h>#include <linux/irqdomain.h>#include <linux/acpi.h>#include <linux/sizes.h>#include <linux/of_irq.h>#include <asm/mshyperv.h> /* * Protocol versions. The low word is the minor version, the high word the * major version. */ #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))#define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)#define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff) enum pci_protocol_version_t {	PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),	/* Win10 */	PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2),	/* RS1 */	PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3),	/* Vibranium */	PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4),	/* WS2022 */}; #define CPU_AFFINITY_ALL	-1ULL /* * Supported protocol versions in the order of probing - highest go * first. */static enum pci_protocol_version_t pci_protocol_versions[] = {	PCI_PROTOCOL_VERSION_1_4,	PCI_PROTOCOL_VERSION_1_3,	PCI_PROTOCOL_VERSION_1_2,	PCI_PROTOCOL_VERSION_1_1,}; #define PCI_CONFIG_MMIO_LENGTH	0x2000#define CFG_PAGE_OFFSET 0x1000#define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET) #define MAX_SUPPORTED_MSI_MESSAGES 0x400 #define STATUS_REVISION_MISMATCH 0xC0000059 /* space for 32bit serial number as string */#define SLOT_NAME_SIZE 11 /* * Size of requestor for VMbus; the value is based on the observation * that having more than one request outstanding is 'rare', and so 64 * should be generous in ensuring that we don't ever run out. */#define HV_PCI_RQSTOR_SIZE 64 /* * Message Types */ enum pci_message_type {	/*	 * Version 1.1	 */	PCI_MESSAGE_BASE                = 0x42490000,	PCI_BUS_RELATIONS               = PCI_MESSAGE_BASE + 0,	PCI_QUERY_BUS_RELATIONS         = PCI_MESSAGE_BASE + 1,	PCI_POWER_STATE_CHANGE          = PCI_MESSAGE_BASE + 4,	PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,	PCI_QUERY_RESOURCE_RESOURCES    = PCI_MESSAGE_BASE + 6,	PCI_BUS_D0ENTRY                 = PCI_MESSAGE_BASE + 7,	PCI_BUS_D0EXIT                  = PCI_MESSAGE_BASE + 8,	PCI_READ_BLOCK                  = PCI_MESSAGE_BASE + 9,	PCI_WRITE_BLOCK                 = PCI_MESSAGE_BASE + 0xA,	PCI_EJECT                       = PCI_MESSAGE_BASE + 0xB,	PCI_QUERY_STOP                  = PCI_MESSAGE_BASE + 0xC,	PCI_REENABLE                    = PCI_MESSAGE_BASE + 0xD,	PCI_QUERY_STOP_FAILED           = PCI_MESSAGE_BASE + 0xE,	PCI_EJECTION_COMPLETE           = PCI_MESSAGE_BASE + 0xF,	PCI_RESOURCES_ASSIGNED          = PCI_MESSAGE_BASE + 0x10,	PCI_RESOURCES_RELEASED          = PCI_MESSAGE_BASE + 0x11,	PCI_INVALIDATE_BLOCK            = PCI_MESSAGE_BASE + 0x12,	PCI_QUERY_PROTOCOL_VERSION      = PCI_MESSAGE_BASE + 0x13,	PCI_CREATE_INTERRUPT_MESSAGE    = PCI_MESSAGE_BASE + 0x14,	PCI_DELETE_INTERRUPT_MESSAGE    = PCI_MESSAGE_BASE + 0x15,	PCI_RESOURCES_ASSIGNED2		= PCI_MESSAGE_BASE + 0x16,	PCI_CREATE_INTERRUPT_MESSAGE2	= PCI_MESSAGE_BASE + 0x17,	PCI_DELETE_INTERRUPT_MESSAGE2	= PCI_MESSAGE_BASE + 0x18, /* unused */	PCI_BUS_RELATIONS2		= PCI_MESSAGE_BASE + 0x19,	PCI_RESOURCES_ASSIGNED3         = PCI_MESSAGE_BASE + 0x1A,	PCI_CREATE_INTERRUPT_MESSAGE3   = PCI_MESSAGE_BASE + 0x1B,	PCI_MESSAGE_MAXIMUM}; /* * Structures defining the virtual PCI Express protocol. */ union pci_version {	struct {		u16 minor_version;		u16 major_version;	} parts;	u32 version;} __packed; /* * Function numbers are 8-bits wide on Express, as interpreted through ARI, * which is all this driver does.  This representation is the one used in * Windows, which is what is expected when sending this back and forth with * the Hyper-V parent partition. */union win_slot_encoding {	struct {		u32	dev:5;		u32	func:3;		u32	reserved:24;	} bits;	u32 slot;} __packed; /* * Pretty much as defined in the PCI Specifications. */struct pci_function_description {	u16	v_id;	/* vendor ID */	u16	d_id;	/* device ID */	u8	rev;	u8	prog_intf;	u8	subclass;	u8	base_class;	u32	subsystem_id;	union win_slot_encoding win_slot;	u32	ser;	/* serial number */} __packed; enum pci_device_description_flags {	HV_PCI_DEVICE_FLAG_NONE			= 0x0,	HV_PCI_DEVICE_FLAG_NUMA_AFFINITY	= 0x1,}; struct pci_function_description2 {	u16	v_id;	/* vendor ID */	u16	d_id;	/* device ID */	u8	rev;	u8	prog_intf;	u8	subclass;	u8	base_class;	u32	subsystem_id;	union	win_slot_encoding win_slot;	u32	ser;	/* serial number */	u32	flags;	u16	virtual_numa_node;	u16	reserved;} __packed; /** * struct hv_msi_desc * @vector:		IDT entry * @delivery_mode:	As defined in Intel's Programmer's *			Reference Manual, Volume 3, Chapter 8. * @vector_count:	Number of contiguous entries in the *			Interrupt Descriptor Table that are *			occupied by this Message-Signaled *			Interrupt. For "MSI", as first defined *			in PCI 2.2, this can be between 1 and *			32. For "MSI-X," as first defined in PCI *			3.0, this must be 1, as each MSI-X table *			entry would have its own descriptor. * @reserved:		Empty space * @cpu_mask:		All the target virtual processors. */struct hv_msi_desc {	u8	vector;	u8	delivery_mode;	u16	vector_count;	u32	reserved;	u64	cpu_mask;} __packed; /** * struct hv_msi_desc2 - 1.2 version of hv_msi_desc * @vector:		IDT entry * @delivery_mode:	As defined in Intel's Programmer's *			Reference Manual, Volume 3, Chapter 8. * @vector_count:	Number of contiguous entries in the *			Interrupt Descriptor Table that are *			occupied by this Message-Signaled *			Interrupt. For "MSI", as first defined *			in PCI 2.2, this can be between 1 and *			32. For "MSI-X," as first defined in PCI *			3.0, this must be 1, as each MSI-X table *			entry would have its own descriptor. * @processor_count:	number of bits enabled in array. * @processor_array:	All the target virtual processors. */struct hv_msi_desc2 {	u8	vector;	u8	delivery_mode;	u16	vector_count;	u16	processor_count;	u16	processor_array[32];} __packed; /* * struct hv_msi_desc3 - 1.3 version of hv_msi_desc *	Everything is the same as in 'hv_msi_desc2' except that the size of the *	'vector' field is larger to support bigger vector values. For ex: LPI *	vectors on ARM. */struct hv_msi_desc3 {	u32	vector;	u8	delivery_mode;	u8	reserved;	u16	vector_count;	u16	processor_count;	u16	processor_array[32];} __packed; /** * struct tran_int_desc * @reserved:		unused, padding * @vector_count:	same as in hv_msi_desc * @data:		This is the "data payload" value that is *			written by the device when it generates *			a message-signaled interrupt, either MSI *			or MSI-X. * @address:		This is the address to which the data *			payload is written on interrupt *			generation. */struct tran_int_desc {	u16	reserved;	u16	vector_count;	u32	data;	u64	address;} __packed; /* * A generic message format for virtual PCI. * Specific message formats are defined later in the file. */ struct pci_message {	u32 type;} __packed; struct pci_child_message {	struct pci_message message_type;	union win_slot_encoding wslot;} __packed; struct pci_incoming_message {	struct vmpacket_descriptor hdr;	struct pci_message message_type;} __packed; struct pci_response {	struct vmpacket_descriptor hdr;	s32 status;			/* negative values are failures */} __packed; struct pci_packet {	void (*completion_func)(void *context, struct pci_response *resp,				int resp_packet_size);	void *compl_ctxt;}; /* * Specific message types supporting the PCI protocol. */ /* * Version negotiation message. Sent from the guest to the host. * The guest is free to try different versions until the host * accepts the version. * * pci_version: The protocol version requested. * is_last_attempt: If TRUE, this is the last version guest will request. * reservedz: Reserved field, set to zero. */ struct pci_version_request {	struct pci_message message_type;	u32 protocol_version;} __packed; /* * Bus D0 Entry.  This is sent from the guest to the host when the virtual * bus (PCI Express port) is ready for action. */ struct pci_bus_d0_entry {	struct pci_message message_type;	u32 reserved;	u64 mmio_base;} __packed; struct pci_bus_relations {	struct pci_incoming_message incoming;	u32 device_count;	struct pci_function_description func[];} __packed; struct pci_bus_relations2 {	struct pci_incoming_message incoming;	u32 device_count;	struct pci_function_description2 func[];} __packed; struct pci_q_res_req_response {	struct vmpacket_descriptor hdr;	s32 status;			/* negative values are failures */	u32 probed_bar[PCI_STD_NUM_BARS];} __packed; struct pci_set_power {	struct pci_message message_type;	union win_slot_encoding wslot;	u32 power_state;		/* In Windows terms */	u32 reserved;} __packed; struct pci_set_power_response {	struct vmpacket_descriptor hdr;	s32 status;			/* negative values are failures */	union win_slot_encoding wslot;	u32 resultant_state;		/* In Windows terms */	u32 reserved;} __packed; struct pci_resources_assigned {	struct pci_message message_type;	union win_slot_encoding wslot;	u8 memory_range[0x14][6];	/* not used here */	u32 msi_descriptors;	u32 reserved[4];} __packed; struct pci_resources_assigned2 {	struct pci_message message_type;	union win_slot_encoding wslot;	u8 memory_range[0x14][6];	/* not used here */	u32 msi_descriptor_count;	u8 reserved[70];} __packed; struct pci_create_interrupt {	struct pci_message message_type;	union win_slot_encoding wslot;	struct hv_msi_desc int_desc;} __packed; struct pci_create_int_response {	struct pci_response response;	u32 reserved;	struct tran_int_desc int_desc;} __packed; struct pci_create_interrupt2 {	struct pci_message message_type;	union win_slot_encoding wslot;	struct hv_msi_desc2 int_desc;} __packed; struct pci_create_interrupt3 {	struct pci_message message_type;	union win_slot_encoding wslot;	struct hv_msi_desc3 int_desc;} __packed; struct pci_delete_interrupt {	struct pci_message message_type;	union win_slot_encoding wslot;	struct tran_int_desc int_desc;} __packed; /* * Note: the VM must pass a valid block id, wslot and bytes_requested. */struct pci_read_block {	struct pci_message message_type;	u32 block_id;	union win_slot_encoding wslot;	u32 bytes_requested;} __packed; struct pci_read_block_response {	struct vmpacket_descriptor hdr;	u32 status;	u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];} __packed; /* * Note: the VM must pass a valid block id, wslot and byte_count. */struct pci_write_block {	struct pci_message message_type;	u32 block_id;	union win_slot_encoding wslot;	u32 byte_count;	u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];} __packed; struct pci_dev_inval_block {	struct pci_incoming_message incoming;	union win_slot_encoding wslot;	u64 block_mask;} __packed; struct pci_dev_incoming {	struct pci_incoming_message incoming;	union win_slot_encoding wslot;} __packed; struct pci_eject_response {	struct pci_message message_type;	union win_slot_encoding wslot;	u32 status;} __packed; static int pci_ring_size = VMBUS_RING_SIZE(SZ_16K); /* * Driver specific state. */ enum hv_pcibus_state {	hv_pcibus_init = 0,	hv_pcibus_probed,	hv_pcibus_installed,	hv_pcibus_removing,	hv_pcibus_maximum}; struct hv_pcibus_device {#ifdef CONFIG_X86	struct pci_sysdata sysdata;#elif defined(CONFIG_ARM64)	struct pci_config_window sysdata;#endif	struct pci_host_bridge *bridge;	struct fwnode_handle *fwnode;	/* Protocol version negotiated with the host */	enum pci_protocol_version_t protocol_version; 	struct mutex state_lock;	enum hv_pcibus_state state; 	struct hv_device *hdev;	resource_size_t low_mmio_space;	resource_size_t high_mmio_space;	struct resource *mem_config;

Lines 1–500 of 4195.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t