1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) Microsoft Corporation.
6 * Jake Oshins <jakeo@microsoft.com>
8 * This driver acts as a paravirtual front-end for PCI Express root buses.
9 * When a PCI Express function (either an entire device or an SR-IOV
10 * Virtual Function) is being passed through to the VM, this driver exposes
11 * a new bus to the guest VM. This is modeled as a root PCI bus because
12 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
13 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
14 * until a device as been exposed using this driver.
16 * Each root PCI bus has its own PCI domain, which is called "Segment" in
17 * the PCI Firmware Specifications. Thus while each device passed through
18 * to the VM using this front-end will appear at "device 0", the domain will
19 * be unique. Typically, each bus will have one PCI function on it, though
20 * this driver does support more than one.
22 * In order to map the interrupts from the device through to the guest VM,
23 * this driver also implements an IRQ Domain, which handles interrupts (either
24 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
25 * set up, torn down, or reaffined, this driver communicates with the
26 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
27 * interrupt will be delivered to the correct virtual processor at the right
28 * vector. This driver does not support level-triggered (line-based)
29 * interrupts, and will report that the Interrupt Line register in the
30 * function's configuration space is zero.
32 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
33 * facilities. For instance, the configuration space of a function exposed
34 * by Hyper-V is mapped into a single page of memory space, and the
35 * read and write handlers for config space must be aware of this mechanism.
36 * Similarly, device setup and teardown involves messages sent to and from
37 * the PCI back-end driver in Hyper-V.
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/semaphore.h>
45 #include <linux/irqdomain.h>
46 #include <asm/irqdomain.h>
48 #include <linux/irq.h>
49 #include <linux/msi.h>
50 #include <linux/hyperv.h>
51 #include <linux/refcount.h>
52 #include <asm/mshyperv.h>
55 * Protocol versions. The low word is the minor version, the high word the
59 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
60 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
61 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
63 enum pci_protocol_version_t {
64 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
65 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
68 #define CPU_AFFINITY_ALL -1ULL
71 * Supported protocol versions in the order of probing - highest go
74 static enum pci_protocol_version_t pci_protocol_versions[] = {
75 PCI_PROTOCOL_VERSION_1_2,
76 PCI_PROTOCOL_VERSION_1_1,
80 * Protocol version negotiated by hv_pci_protocol_negotiation().
82 static enum pci_protocol_version_t pci_protocol_version;
84 #define PCI_CONFIG_MMIO_LENGTH 0x2000
85 #define CFG_PAGE_OFFSET 0x1000
86 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
88 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
90 #define STATUS_REVISION_MISMATCH 0xC0000059
92 /* space for 32bit serial number as string */
93 #define SLOT_NAME_SIZE 11
99 enum pci_message_type {
103 PCI_MESSAGE_BASE = 0x42490000,
104 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
105 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
106 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
107 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
108 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
109 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
110 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
111 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
112 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
113 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
114 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
115 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
116 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
117 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
118 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
119 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
120 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
121 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
122 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
123 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
124 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
125 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
126 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
131 * Structures defining the virtual PCI Express protocol.
143 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
144 * which is all this driver does. This representation is the one used in
145 * Windows, which is what is expected when sending this back and forth with
146 * the Hyper-V parent partition.
148 union win_slot_encoding {
158 * Pretty much as defined in the PCI Specifications.
160 struct pci_function_description {
161 u16 v_id; /* vendor ID */
162 u16 d_id; /* device ID */
168 union win_slot_encoding win_slot;
169 u32 ser; /* serial number */
175 * @delivery_mode: As defined in Intel's Programmer's
176 * Reference Manual, Volume 3, Chapter 8.
177 * @vector_count: Number of contiguous entries in the
178 * Interrupt Descriptor Table that are
179 * occupied by this Message-Signaled
180 * Interrupt. For "MSI", as first defined
181 * in PCI 2.2, this can be between 1 and
182 * 32. For "MSI-X," as first defined in PCI
183 * 3.0, this must be 1, as each MSI-X table
184 * entry would have its own descriptor.
185 * @reserved: Empty space
186 * @cpu_mask: All the target virtual processors.
197 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
199 * @delivery_mode: As defined in Intel's Programmer's
200 * Reference Manual, Volume 3, Chapter 8.
201 * @vector_count: Number of contiguous entries in the
202 * Interrupt Descriptor Table that are
203 * occupied by this Message-Signaled
204 * Interrupt. For "MSI", as first defined
205 * in PCI 2.2, this can be between 1 and
206 * 32. For "MSI-X," as first defined in PCI
207 * 3.0, this must be 1, as each MSI-X table
208 * entry would have its own descriptor.
209 * @processor_count: number of bits enabled in array.
210 * @processor_array: All the target virtual processors.
212 struct hv_msi_desc2 {
217 u16 processor_array[32];
221 * struct tran_int_desc
222 * @reserved: unused, padding
223 * @vector_count: same as in hv_msi_desc
224 * @data: This is the "data payload" value that is
225 * written by the device when it generates
226 * a message-signaled interrupt, either MSI
228 * @address: This is the address to which the data
229 * payload is written on interrupt
232 struct tran_int_desc {
240 * A generic message format for virtual PCI.
241 * Specific message formats are defined later in the file.
248 struct pci_child_message {
249 struct pci_message message_type;
250 union win_slot_encoding wslot;
253 struct pci_incoming_message {
254 struct vmpacket_descriptor hdr;
255 struct pci_message message_type;
258 struct pci_response {
259 struct vmpacket_descriptor hdr;
260 s32 status; /* negative values are failures */
264 void (*completion_func)(void *context, struct pci_response *resp,
265 int resp_packet_size);
268 struct pci_message message[0];
272 * Specific message types supporting the PCI protocol.
276 * Version negotiation message. Sent from the guest to the host.
277 * The guest is free to try different versions until the host
278 * accepts the version.
280 * pci_version: The protocol version requested.
281 * is_last_attempt: If TRUE, this is the last version guest will request.
282 * reservedz: Reserved field, set to zero.
285 struct pci_version_request {
286 struct pci_message message_type;
287 u32 protocol_version;
291 * Bus D0 Entry. This is sent from the guest to the host when the virtual
292 * bus (PCI Express port) is ready for action.
295 struct pci_bus_d0_entry {
296 struct pci_message message_type;
301 struct pci_bus_relations {
302 struct pci_incoming_message incoming;
304 struct pci_function_description func[0];
307 struct pci_q_res_req_response {
308 struct vmpacket_descriptor hdr;
309 s32 status; /* negative values are failures */
313 struct pci_set_power {
314 struct pci_message message_type;
315 union win_slot_encoding wslot;
316 u32 power_state; /* In Windows terms */
320 struct pci_set_power_response {
321 struct vmpacket_descriptor hdr;
322 s32 status; /* negative values are failures */
323 union win_slot_encoding wslot;
324 u32 resultant_state; /* In Windows terms */
328 struct pci_resources_assigned {
329 struct pci_message message_type;
330 union win_slot_encoding wslot;
331 u8 memory_range[0x14][6]; /* not used here */
336 struct pci_resources_assigned2 {
337 struct pci_message message_type;
338 union win_slot_encoding wslot;
339 u8 memory_range[0x14][6]; /* not used here */
340 u32 msi_descriptor_count;
344 struct pci_create_interrupt {
345 struct pci_message message_type;
346 union win_slot_encoding wslot;
347 struct hv_msi_desc int_desc;
350 struct pci_create_int_response {
351 struct pci_response response;
353 struct tran_int_desc int_desc;
356 struct pci_create_interrupt2 {
357 struct pci_message message_type;
358 union win_slot_encoding wslot;
359 struct hv_msi_desc2 int_desc;
362 struct pci_delete_interrupt {
363 struct pci_message message_type;
364 union win_slot_encoding wslot;
365 struct tran_int_desc int_desc;
368 struct pci_dev_incoming {
369 struct pci_incoming_message incoming;
370 union win_slot_encoding wslot;
373 struct pci_eject_response {
374 struct pci_message message_type;
375 union win_slot_encoding wslot;
379 static int pci_ring_size = (4 * PAGE_SIZE);
382 * Definitions or interrupt steering hypercall.
384 #define HV_PARTITION_ID_SELF ((u64)-1)
385 #define HVCALL_RETARGET_INTERRUPT 0x7e
387 struct hv_interrupt_entry {
388 u32 source; /* 1 for MSI(-X) */
394 #define HV_VP_SET_BANK_COUNT_MAX 5 /* current implementation limit */
397 u64 format; /* 0 (HvGenericSetSparse4k) */
399 u64 masks[HV_VP_SET_BANK_COUNT_MAX];
403 * flags for hv_device_interrupt_target.flags
405 #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
406 #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
408 struct hv_device_interrupt_target {
413 struct hv_vp_set vp_set;
417 struct retarget_msi_interrupt {
418 u64 partition_id; /* use "self" */
420 struct hv_interrupt_entry int_entry;
422 struct hv_device_interrupt_target int_target;
426 * Driver specific state.
429 enum hv_pcibus_state {
437 struct hv_pcibus_device {
438 struct pci_sysdata sysdata;
439 enum hv_pcibus_state state;
440 refcount_t remove_lock;
441 struct hv_device *hdev;
442 resource_size_t low_mmio_space;
443 resource_size_t high_mmio_space;
444 struct resource *mem_config;
445 struct resource *low_mmio_res;
446 struct resource *high_mmio_res;
447 struct completion *survey_event;
448 struct completion remove_event;
449 struct pci_bus *pci_bus;
450 spinlock_t config_lock; /* Avoid two threads writing index page */
451 spinlock_t device_list_lock; /* Protect lists below */
452 void __iomem *cfg_addr;
454 struct list_head resources_for_children;
456 struct list_head children;
457 struct list_head dr_list;
459 struct msi_domain_info msi_info;
460 struct msi_controller msi_chip;
461 struct irq_domain *irq_domain;
463 /* hypercall arg, must not cross page boundary */
464 struct retarget_msi_interrupt retarget_msi_interrupt_params;
466 spinlock_t retarget_msi_interrupt_lock;
468 struct workqueue_struct *wq;
472 * Tracks "Device Relations" messages from the host, which must be both
473 * processed in order and deferred so that they don't run in the context
474 * of the incoming packet callback.
477 struct work_struct wrk;
478 struct hv_pcibus_device *bus;
482 struct list_head list_entry;
484 struct pci_function_description func[0];
487 enum hv_pcichild_state {
488 hv_pcichild_init = 0,
489 hv_pcichild_requirements,
490 hv_pcichild_resourced,
491 hv_pcichild_ejecting,
496 /* List protected by pci_rescan_remove_lock */
497 struct list_head list_entry;
499 enum hv_pcichild_state state;
500 struct pci_slot *pci_slot;
501 struct pci_function_description desc;
502 bool reported_missing;
503 struct hv_pcibus_device *hbus;
504 struct work_struct wrk;
507 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
508 * read it back, for each of the BAR offsets within config space.
513 struct hv_pci_compl {
514 struct completion host_event;
515 s32 completion_status;
518 static void hv_pci_onchannelcallback(void *context);
521 * hv_pci_generic_compl() - Invoked for a completion packet
522 * @context: Set up by the sender of the packet.
523 * @resp: The response packet
524 * @resp_packet_size: Size in bytes of the packet
526 * This function is used to trigger an event and report status
527 * for any message for which the completion packet contains a
528 * status and nothing else.
530 static void hv_pci_generic_compl(void *context, struct pci_response *resp,
531 int resp_packet_size)
533 struct hv_pci_compl *comp_pkt = context;
535 if (resp_packet_size >= offsetofend(struct pci_response, status))
536 comp_pkt->completion_status = resp->status;
538 comp_pkt->completion_status = -1;
540 complete(&comp_pkt->host_event);
543 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
546 static void get_pcichild(struct hv_pci_dev *hpdev)
548 refcount_inc(&hpdev->refs);
551 static void put_pcichild(struct hv_pci_dev *hpdev)
553 if (refcount_dec_and_test(&hpdev->refs))
557 static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
558 static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
561 * There is no good way to get notified from vmbus_onoffer_rescind(),
562 * so let's use polling here, since this is not a hot path.
564 static int wait_for_response(struct hv_device *hdev,
565 struct completion *comp)
568 if (hdev->channel->rescind) {
569 dev_warn_once(&hdev->device, "The device is gone.\n");
573 if (wait_for_completion_timeout(comp, HZ / 10))
581 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
582 * @devfn: The Linux representation of PCI slot
584 * Windows uses a slightly different representation of PCI slot.
586 * Return: The Windows representation
588 static u32 devfn_to_wslot(int devfn)
590 union win_slot_encoding wslot;
593 wslot.bits.dev = PCI_SLOT(devfn);
594 wslot.bits.func = PCI_FUNC(devfn);
600 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
601 * @wslot: The Windows representation of PCI slot
603 * Windows uses a slightly different representation of PCI slot.
605 * Return: The Linux representation
607 static int wslot_to_devfn(u32 wslot)
609 union win_slot_encoding slot_no;
611 slot_no.slot = wslot;
612 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
616 * PCI Configuration Space for these root PCI buses is implemented as a pair
617 * of pages in memory-mapped I/O space. Writing to the first page chooses
618 * the PCI function being written or read. Once the first page has been
619 * written to, the following page maps in the entire configuration space of
624 * _hv_pcifront_read_config() - Internal PCI config read
625 * @hpdev: The PCI driver's representation of the device
626 * @where: Offset within config space
627 * @size: Size of the transfer
628 * @val: Pointer to the buffer receiving the data
630 static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
634 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
637 * If the attempt is to read the IDs or the ROM BAR, simulate that.
639 if (where + size <= PCI_COMMAND) {
640 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
641 } else if (where >= PCI_CLASS_REVISION && where + size <=
642 PCI_CACHE_LINE_SIZE) {
643 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
644 PCI_CLASS_REVISION, size);
645 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
647 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
648 PCI_SUBSYSTEM_VENDOR_ID, size);
649 } else if (where >= PCI_ROM_ADDRESS && where + size <=
650 PCI_CAPABILITY_LIST) {
651 /* ROM BARs are unimplemented */
653 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
656 * Interrupt Line and Interrupt PIN are hard-wired to zero
657 * because this front-end only supports message-signaled
661 } else if (where + size <= CFG_PAGE_SIZE) {
662 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
663 /* Choose the function to be read. (See comment above) */
664 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
665 /* Make sure the function was chosen before we start reading. */
667 /* Read from that function's config space. */
680 * Make sure the read was done before we release the spinlock
681 * allowing consecutive reads/writes.
684 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
686 dev_err(&hpdev->hbus->hdev->device,
687 "Attempt to read beyond a function's config space.\n");
691 static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
695 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
698 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
700 /* Choose the function to be read. (See comment above) */
701 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
702 /* Make sure the function was chosen before we start reading. */
704 /* Read from that function's config space. */
707 * mb() is not required here, because the spin_unlock_irqrestore()
711 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
717 * _hv_pcifront_write_config() - Internal PCI config write
718 * @hpdev: The PCI driver's representation of the device
719 * @where: Offset within config space
720 * @size: Size of the transfer
721 * @val: The data being transferred
723 static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
727 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
729 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
730 where + size <= PCI_CAPABILITY_LIST) {
731 /* SSIDs and ROM BARs are read-only */
732 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
733 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
734 /* Choose the function to be written. (See comment above) */
735 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
736 /* Make sure the function was chosen before we start writing. */
738 /* Write to that function's config space. */
751 * Make sure the write was done before we release the spinlock
752 * allowing consecutive reads/writes.
755 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
757 dev_err(&hpdev->hbus->hdev->device,
758 "Attempt to write beyond a function's config space.\n");
763 * hv_pcifront_read_config() - Read configuration space
764 * @bus: PCI Bus structure
765 * @devfn: Device/function
766 * @where: Offset from base
767 * @size: Byte/word/dword
768 * @val: Value to be read
770 * Return: PCIBIOS_SUCCESSFUL on success
771 * PCIBIOS_DEVICE_NOT_FOUND on failure
773 static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
774 int where, int size, u32 *val)
776 struct hv_pcibus_device *hbus =
777 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
778 struct hv_pci_dev *hpdev;
780 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
782 return PCIBIOS_DEVICE_NOT_FOUND;
784 _hv_pcifront_read_config(hpdev, where, size, val);
787 return PCIBIOS_SUCCESSFUL;
791 * hv_pcifront_write_config() - Write configuration space
792 * @bus: PCI Bus structure
793 * @devfn: Device/function
794 * @where: Offset from base
795 * @size: Byte/word/dword
796 * @val: Value to be written to device
798 * Return: PCIBIOS_SUCCESSFUL on success
799 * PCIBIOS_DEVICE_NOT_FOUND on failure
801 static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
802 int where, int size, u32 val)
804 struct hv_pcibus_device *hbus =
805 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
806 struct hv_pci_dev *hpdev;
808 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
810 return PCIBIOS_DEVICE_NOT_FOUND;
812 _hv_pcifront_write_config(hpdev, where, size, val);
815 return PCIBIOS_SUCCESSFUL;
818 /* PCIe operations */
819 static struct pci_ops hv_pcifront_ops = {
820 .read = hv_pcifront_read_config,
821 .write = hv_pcifront_write_config,
824 /* Interrupt management hooks */
825 static void hv_int_desc_free(struct hv_pci_dev *hpdev,
826 struct tran_int_desc *int_desc)
828 struct pci_delete_interrupt *int_pkt;
830 struct pci_packet pkt;
831 u8 buffer[sizeof(struct pci_delete_interrupt)];
834 if (!int_desc->vector_count) {
838 memset(&ctxt, 0, sizeof(ctxt));
839 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
840 int_pkt->message_type.type =
841 PCI_DELETE_INTERRUPT_MESSAGE;
842 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
843 int_pkt->int_desc = *int_desc;
844 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
845 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
850 * hv_msi_free() - Free the MSI.
851 * @domain: The interrupt domain pointer
852 * @info: Extra MSI-related context
853 * @irq: Identifies the IRQ.
855 * The Hyper-V parent partition and hypervisor are tracking the
856 * messages that are in use, keeping the interrupt redirection
857 * table up to date. This callback sends a message that frees
858 * the IRT entry and related tracking nonsense.
860 static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
863 struct hv_pcibus_device *hbus;
864 struct hv_pci_dev *hpdev;
865 struct pci_dev *pdev;
866 struct tran_int_desc *int_desc;
867 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
868 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
870 pdev = msi_desc_to_pci_dev(msi);
872 int_desc = irq_data_get_irq_chip_data(irq_data);
876 irq_data->chip_data = NULL;
877 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
883 hv_int_desc_free(hpdev, int_desc);
887 static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
890 struct irq_data *parent = data->parent_data;
892 return parent->chip->irq_set_affinity(parent, dest, force);
895 static void hv_irq_mask(struct irq_data *data)
897 pci_msi_mask_irq(data);
900 static unsigned int hv_msi_get_int_vector(struct irq_data *data)
902 struct irq_cfg *cfg = irqd_cfg(data);
907 static int hv_msi_prepare(struct irq_domain *domain, struct device *dev,
908 int nvec, msi_alloc_info_t *info)
910 int ret = pci_msi_prepare(domain, dev, nvec, info);
913 * By using the interrupt remapper in the hypervisor IOMMU, contiguous
914 * CPU vectors is not needed for multi-MSI
916 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
917 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
923 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
925 * @data: Describes the IRQ
927 * Build new a destination for the MSI and make a hypercall to
928 * update the Interrupt Redirection Table. "Device Logical ID"
929 * is built out of this PCI bus's instance GUID and the function
930 * number of the device.
932 static void hv_irq_unmask(struct irq_data *data)
934 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
935 struct irq_cfg *cfg = irqd_cfg(data);
936 struct retarget_msi_interrupt *params;
937 struct tran_int_desc *int_desc;
938 struct hv_pcibus_device *hbus;
939 struct cpumask *dest;
940 struct pci_bus *pbus;
941 struct pci_dev *pdev;
948 dest = irq_data_get_effective_affinity_mask(data);
949 pdev = msi_desc_to_pci_dev(msi_desc);
951 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
952 int_desc = data->chip_data;
954 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
956 params = &hbus->retarget_msi_interrupt_params;
957 memset(params, 0, sizeof(*params));
958 params->partition_id = HV_PARTITION_ID_SELF;
959 params->int_entry.source = 1; /* MSI(-X) */
960 params->int_entry.address = int_desc->address & 0xffffffff;
961 params->int_entry.data = int_desc->data;
962 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
963 (hbus->hdev->dev_instance.b[4] << 16) |
964 (hbus->hdev->dev_instance.b[7] << 8) |
965 (hbus->hdev->dev_instance.b[6] & 0xf8) |
966 PCI_FUNC(pdev->devfn);
967 params->int_target.vector = cfg->vector;
970 * Honoring apic->irq_delivery_mode set to dest_Fixed by
971 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
972 * spurious interrupt storm. Not doing so does not seem to have a
973 * negative effect (yet?).
976 if (pci_protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
978 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
979 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
980 * with >64 VP support.
981 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
982 * is not sufficient for this hypercall.
984 params->int_target.flags |=
985 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
986 params->int_target.vp_set.valid_banks =
987 (1ull << HV_VP_SET_BANK_COUNT_MAX) - 1;
990 * var-sized hypercall, var-size starts after vp_mask (thus
991 * vp_set.format does not count, but vp_set.valid_banks does).
993 var_size = 1 + HV_VP_SET_BANK_COUNT_MAX;
995 for_each_cpu_and(cpu, dest, cpu_online_mask) {
996 cpu_vmbus = hv_cpu_number_to_vp_number(cpu);
998 if (cpu_vmbus >= HV_VP_SET_BANK_COUNT_MAX * 64) {
999 dev_err(&hbus->hdev->device,
1000 "too high CPU %d", cpu_vmbus);
1005 params->int_target.vp_set.masks[cpu_vmbus / 64] |=
1006 (1ULL << (cpu_vmbus & 63));
1009 for_each_cpu_and(cpu, dest, cpu_online_mask) {
1010 params->int_target.vp_mask |=
1011 (1ULL << hv_cpu_number_to_vp_number(cpu));
1015 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
1019 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
1022 dev_err(&hbus->hdev->device,
1023 "%s() failed: %#llx", __func__, res);
1027 pci_msi_unmask_irq(data);
1030 struct compose_comp_ctxt {
1031 struct hv_pci_compl comp_pkt;
1032 struct tran_int_desc int_desc;
1035 static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1036 int resp_packet_size)
1038 struct compose_comp_ctxt *comp_pkt = context;
1039 struct pci_create_int_response *int_resp =
1040 (struct pci_create_int_response *)resp;
1042 comp_pkt->comp_pkt.completion_status = resp->status;
1043 comp_pkt->int_desc = int_resp->int_desc;
1044 complete(&comp_pkt->comp_pkt.host_event);
1047 static u32 hv_compose_msi_req_v1(
1048 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1049 u32 slot, u8 vector, u8 vector_count)
1051 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1052 int_pkt->wslot.slot = slot;
1053 int_pkt->int_desc.vector = vector;
1054 int_pkt->int_desc.vector_count = vector_count;
1055 int_pkt->int_desc.delivery_mode = dest_Fixed;
1058 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1061 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1063 return sizeof(*int_pkt);
1066 static u32 hv_compose_msi_req_v2(
1067 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1068 u32 slot, u8 vector, u8 vector_count)
1072 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1073 int_pkt->wslot.slot = slot;
1074 int_pkt->int_desc.vector = vector;
1075 int_pkt->int_desc.vector_count = vector_count;
1076 int_pkt->int_desc.delivery_mode = dest_Fixed;
1079 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1080 * by subsequent retarget in hv_irq_unmask().
1082 cpu = cpumask_first_and(affinity, cpu_online_mask);
1083 int_pkt->int_desc.processor_array[0] =
1084 hv_cpu_number_to_vp_number(cpu);
1085 int_pkt->int_desc.processor_count = 1;
1087 return sizeof(*int_pkt);
1091 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1092 * @data: Everything about this MSI
1093 * @msg: Buffer that is filled in by this function
1095 * This function unpacks the IRQ looking for target CPU set, IDT
1096 * vector and mode and sends a message to the parent partition
1097 * asking for a mapping for that tuple in this partition. The
1098 * response supplies a data value and address to which that data
1099 * should be written to trigger that interrupt.
1101 static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1103 struct hv_pcibus_device *hbus;
1104 struct hv_pci_dev *hpdev;
1105 struct pci_bus *pbus;
1106 struct pci_dev *pdev;
1107 struct cpumask *dest;
1108 unsigned long flags;
1109 struct compose_comp_ctxt comp;
1110 struct tran_int_desc *int_desc;
1111 struct msi_desc *msi_desc;
1112 u8 vector, vector_count;
1114 struct pci_packet pci_pkt;
1116 struct pci_create_interrupt v1;
1117 struct pci_create_interrupt2 v2;
1124 /* Reuse the previous allocation */
1125 if (data->chip_data) {
1126 int_desc = data->chip_data;
1127 msg->address_hi = int_desc->address >> 32;
1128 msg->address_lo = int_desc->address & 0xffffffff;
1129 msg->data = int_desc->data;
1133 msi_desc = irq_data_get_msi_desc(data);
1134 pdev = msi_desc_to_pci_dev(msi_desc);
1135 dest = irq_data_get_effective_affinity_mask(data);
1137 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1138 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1140 goto return_null_message;
1142 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
1144 goto drop_reference;
1146 if (!msi_desc->msi_attrib.is_msix && msi_desc->nvec_used > 1) {
1148 * If this is not the first MSI of Multi MSI, we already have
1149 * a mapping. Can exit early.
1151 if (msi_desc->irq != data->irq) {
1152 data->chip_data = int_desc;
1153 int_desc->address = msi_desc->msg.address_lo |
1154 (u64)msi_desc->msg.address_hi << 32;
1155 int_desc->data = msi_desc->msg.data +
1156 (data->irq - msi_desc->irq);
1157 msg->address_hi = msi_desc->msg.address_hi;
1158 msg->address_lo = msi_desc->msg.address_lo;
1159 msg->data = int_desc->data;
1160 put_pcichild(hpdev);
1164 * The vector we select here is a dummy value. The correct
1165 * value gets sent to the hypervisor in unmask(). This needs
1166 * to be aligned with the count, and also not zero. Multi-msi
1167 * is powers of 2 up to 32, so 32 will always work here.
1170 vector_count = msi_desc->nvec_used;
1172 vector = hv_msi_get_int_vector(data);
1176 memset(&ctxt, 0, sizeof(ctxt));
1177 init_completion(&comp.comp_pkt.host_event);
1178 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1179 ctxt.pci_pkt.compl_ctxt = ∁
1181 switch (pci_protocol_version) {
1182 case PCI_PROTOCOL_VERSION_1_1:
1183 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
1185 hpdev->desc.win_slot.slot,
1190 case PCI_PROTOCOL_VERSION_1_2:
1191 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
1193 hpdev->desc.win_slot.slot,
1199 /* As we only negotiate protocol versions known to this driver,
1200 * this path should never hit. However, this is it not a hot
1201 * path so we print a message to aid future updates.
1203 dev_err(&hbus->hdev->device,
1204 "Unexpected vPCI protocol, update driver.");
1208 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1209 size, (unsigned long)&ctxt.pci_pkt,
1211 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1213 dev_err(&hbus->hdev->device,
1214 "Sending request for interrupt failed: 0x%x",
1215 comp.comp_pkt.completion_status);
1220 * Since this function is called with IRQ locks held, can't
1221 * do normal wait for completion; instead poll.
1223 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1224 /* 0xFFFF means an invalid PCI VENDOR ID. */
1225 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1226 dev_err_once(&hbus->hdev->device,
1227 "the device has gone\n");
1232 * When the higher level interrupt code calls us with
1233 * interrupt disabled, we must poll the channel by calling
1234 * the channel callback directly when channel->target_cpu is
1235 * the current CPU. When the higher level interrupt code
1236 * calls us with interrupt enabled, let's add the
1237 * local_irq_save()/restore() to avoid race:
1238 * hv_pci_onchannelcallback() can also run in tasklet.
1240 local_irq_save(flags);
1242 if (hbus->hdev->channel->target_cpu == smp_processor_id())
1243 hv_pci_onchannelcallback(hbus);
1245 local_irq_restore(flags);
1247 if (hpdev->state == hv_pcichild_ejecting) {
1248 dev_err_once(&hbus->hdev->device,
1249 "the device is being ejected\n");
1256 if (comp.comp_pkt.completion_status < 0) {
1257 dev_err(&hbus->hdev->device,
1258 "Request for interrupt failed: 0x%x",
1259 comp.comp_pkt.completion_status);
1264 * Record the assignment so that this can be unwound later. Using
1265 * irq_set_chip_data() here would be appropriate, but the lock it takes
1268 *int_desc = comp.int_desc;
1269 data->chip_data = int_desc;
1271 /* Pass up the result. */
1272 msg->address_hi = comp.int_desc.address >> 32;
1273 msg->address_lo = comp.int_desc.address & 0xffffffff;
1274 msg->data = comp.int_desc.data;
1276 put_pcichild(hpdev);
1282 put_pcichild(hpdev);
1283 return_null_message:
1284 msg->address_hi = 0;
1285 msg->address_lo = 0;
1289 /* HW Interrupt Chip Descriptor */
1290 static struct irq_chip hv_msi_irq_chip = {
1291 .name = "Hyper-V PCIe MSI",
1292 .irq_compose_msi_msg = hv_compose_msi_msg,
1293 .irq_set_affinity = hv_set_affinity,
1294 .irq_ack = irq_chip_ack_parent,
1295 .irq_mask = hv_irq_mask,
1296 .irq_unmask = hv_irq_unmask,
1299 static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
1300 msi_alloc_info_t *arg)
1302 return arg->msi_hwirq;
1305 static struct msi_domain_ops hv_msi_ops = {
1306 .get_hwirq = hv_msi_domain_ops_get_hwirq,
1307 .msi_prepare = hv_msi_prepare,
1308 .set_desc = pci_msi_set_desc,
1309 .msi_free = hv_msi_free,
1313 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1314 * @hbus: The root PCI bus
1316 * This function creates an IRQ domain which will be used for
1317 * interrupts from devices that have been passed through. These
1318 * devices only support MSI and MSI-X, not line-based interrupts
1319 * or simulations of line-based interrupts through PCIe's
1320 * fabric-layer messages. Because interrupts are remapped, we
1321 * can support multi-message MSI here.
1323 * Return: '0' on success and error value on failure
1325 static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1327 hbus->msi_info.chip = &hv_msi_irq_chip;
1328 hbus->msi_info.ops = &hv_msi_ops;
1329 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1330 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1332 hbus->msi_info.handler = handle_edge_irq;
1333 hbus->msi_info.handler_name = "edge";
1334 hbus->msi_info.data = hbus;
1335 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
1338 if (!hbus->irq_domain) {
1339 dev_err(&hbus->hdev->device,
1340 "Failed to build an MSI IRQ domain\n");
1348 * get_bar_size() - Get the address space consumed by a BAR
1349 * @bar_val: Value that a BAR returned after -1 was written
1352 * This function returns the size of the BAR, rounded up to 1
1353 * page. It has to be rounded up because the hypervisor's page
1354 * table entry that maps the BAR into the VM can't specify an
1355 * offset within a page. The invariant is that the hypervisor
1356 * must place any BARs of smaller than page length at the
1357 * beginning of a page.
1359 * Return: Size in bytes of the consumed MMIO space.
1361 static u64 get_bar_size(u64 bar_val)
1363 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1368 * survey_child_resources() - Total all MMIO requirements
1369 * @hbus: Root PCI bus, as understood by this driver
1371 static void survey_child_resources(struct hv_pcibus_device *hbus)
1373 struct hv_pci_dev *hpdev;
1374 resource_size_t bar_size = 0;
1375 unsigned long flags;
1376 struct completion *event;
1380 /* If nobody is waiting on the answer, don't compute it. */
1381 event = xchg(&hbus->survey_event, NULL);
1385 /* If the answer has already been computed, go with it. */
1386 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1391 spin_lock_irqsave(&hbus->device_list_lock, flags);
1394 * Due to an interesting quirk of the PCI spec, all memory regions
1395 * for a child device are a power of 2 in size and aligned in memory,
1396 * so it's sufficient to just add them up without tracking alignment.
1398 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1399 for (i = 0; i < 6; i++) {
1400 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1401 dev_err(&hbus->hdev->device,
1402 "There's an I/O BAR in this list!\n");
1404 if (hpdev->probed_bar[i] != 0) {
1406 * A probed BAR has all the upper bits set that
1410 bar_val = hpdev->probed_bar[i];
1411 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1413 ((u64)hpdev->probed_bar[++i] << 32);
1415 bar_val |= 0xffffffff00000000ULL;
1417 bar_size = get_bar_size(bar_val);
1419 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1420 hbus->high_mmio_space += bar_size;
1422 hbus->low_mmio_space += bar_size;
1427 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1432 * prepopulate_bars() - Fill in BARs with defaults
1433 * @hbus: Root PCI bus, as understood by this driver
1435 * The core PCI driver code seems much, much happier if the BARs
1436 * for a device have values upon first scan. So fill them in.
1437 * The algorithm below works down from large sizes to small,
1438 * attempting to pack the assignments optimally. The assumption,
1439 * enforced in other parts of the code, is that the beginning of
1440 * the memory-mapped I/O space will be aligned on the largest
1443 static void prepopulate_bars(struct hv_pcibus_device *hbus)
1445 resource_size_t high_size = 0;
1446 resource_size_t low_size = 0;
1447 resource_size_t high_base = 0;
1448 resource_size_t low_base = 0;
1449 resource_size_t bar_size;
1450 struct hv_pci_dev *hpdev;
1451 unsigned long flags;
1457 if (hbus->low_mmio_space) {
1458 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1459 low_base = hbus->low_mmio_res->start;
1462 if (hbus->high_mmio_space) {
1464 (63 - __builtin_clzll(hbus->high_mmio_space));
1465 high_base = hbus->high_mmio_res->start;
1468 spin_lock_irqsave(&hbus->device_list_lock, flags);
1470 /* Pick addresses for the BARs. */
1472 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1473 for (i = 0; i < 6; i++) {
1474 bar_val = hpdev->probed_bar[i];
1477 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1480 ((u64)hpdev->probed_bar[i + 1]
1483 bar_val |= 0xffffffffULL << 32;
1485 bar_size = get_bar_size(bar_val);
1487 if (high_size != bar_size) {
1491 _hv_pcifront_write_config(hpdev,
1492 PCI_BASE_ADDRESS_0 + (4 * i),
1494 (u32)(high_base & 0xffffff00));
1496 _hv_pcifront_write_config(hpdev,
1497 PCI_BASE_ADDRESS_0 + (4 * i),
1498 4, (u32)(high_base >> 32));
1499 high_base += bar_size;
1501 if (low_size != bar_size)
1503 _hv_pcifront_write_config(hpdev,
1504 PCI_BASE_ADDRESS_0 + (4 * i),
1506 (u32)(low_base & 0xffffff00));
1507 low_base += bar_size;
1510 if (high_size <= 1 && low_size <= 1) {
1511 /* Set the memory enable bit. */
1512 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1514 command |= PCI_COMMAND_MEMORY;
1515 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1523 } while (high_size || low_size);
1525 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1529 * Assign entries in sysfs pci slot directory.
1531 * Note that this function does not need to lock the children list
1532 * because it is called from pci_devices_present_work which
1533 * is serialized with hv_eject_device_work because they are on the
1534 * same ordered workqueue. Therefore hbus->children list will not change
1535 * even when pci_create_slot sleeps.
1537 static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
1539 struct hv_pci_dev *hpdev;
1540 char name[SLOT_NAME_SIZE];
1543 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1544 if (hpdev->pci_slot)
1547 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
1548 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
1549 hpdev->pci_slot = pci_create_slot(hbus->pci_bus, slot_nr,
1551 if (IS_ERR(hpdev->pci_slot)) {
1552 pr_warn("pci_create slot %s failed\n", name);
1553 hpdev->pci_slot = NULL;
1559 * Remove entries in sysfs pci slot directory.
1561 static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
1563 struct hv_pci_dev *hpdev;
1565 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1566 if (!hpdev->pci_slot)
1568 pci_destroy_slot(hpdev->pci_slot);
1569 hpdev->pci_slot = NULL;
1574 * create_root_hv_pci_bus() - Expose a new root PCI bus
1575 * @hbus: Root PCI bus, as understood by this driver
1577 * Return: 0 on success, -errno on failure
1579 static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1581 /* Register the device */
1582 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1583 0, /* bus number is always zero */
1586 &hbus->resources_for_children);
1590 hbus->pci_bus->msi = &hbus->msi_chip;
1591 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1593 pci_lock_rescan_remove();
1594 pci_scan_child_bus(hbus->pci_bus);
1595 pci_bus_assign_resources(hbus->pci_bus);
1596 hv_pci_assign_slots(hbus);
1597 pci_bus_add_devices(hbus->pci_bus);
1598 pci_unlock_rescan_remove();
1599 hbus->state = hv_pcibus_installed;
1603 struct q_res_req_compl {
1604 struct completion host_event;
1605 struct hv_pci_dev *hpdev;
1609 * q_resource_requirements() - Query Resource Requirements
1610 * @context: The completion context.
1611 * @resp: The response that came from the host.
1612 * @resp_packet_size: The size in bytes of resp.
1614 * This function is invoked on completion of a Query Resource
1615 * Requirements packet.
1617 static void q_resource_requirements(void *context, struct pci_response *resp,
1618 int resp_packet_size)
1620 struct q_res_req_compl *completion = context;
1621 struct pci_q_res_req_response *q_res_req =
1622 (struct pci_q_res_req_response *)resp;
1625 if (resp->status < 0) {
1626 dev_err(&completion->hpdev->hbus->hdev->device,
1627 "query resource requirements failed: %x\n",
1630 for (i = 0; i < 6; i++) {
1631 completion->hpdev->probed_bar[i] =
1632 q_res_req->probed_bar[i];
1636 complete(&completion->host_event);
1640 * new_pcichild_device() - Create a new child device
1641 * @hbus: The internal struct tracking this root PCI bus.
1642 * @desc: The information supplied so far from the host
1645 * This function creates the tracking structure for a new child
1646 * device and kicks off the process of figuring out what it is.
1648 * Return: Pointer to the new tracking struct
1650 static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1651 struct pci_function_description *desc)
1653 struct hv_pci_dev *hpdev;
1654 struct pci_child_message *res_req;
1655 struct q_res_req_compl comp_pkt;
1657 struct pci_packet init_packet;
1658 u8 buffer[sizeof(struct pci_child_message)];
1660 unsigned long flags;
1663 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
1669 memset(&pkt, 0, sizeof(pkt));
1670 init_completion(&comp_pkt.host_event);
1671 comp_pkt.hpdev = hpdev;
1672 pkt.init_packet.compl_ctxt = &comp_pkt;
1673 pkt.init_packet.completion_func = q_resource_requirements;
1674 res_req = (struct pci_child_message *)&pkt.init_packet.message;
1675 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
1676 res_req->wslot.slot = desc->win_slot.slot;
1678 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1679 sizeof(struct pci_child_message),
1680 (unsigned long)&pkt.init_packet,
1682 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1686 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
1689 hpdev->desc = *desc;
1690 refcount_set(&hpdev->refs, 1);
1691 get_pcichild(hpdev);
1692 spin_lock_irqsave(&hbus->device_list_lock, flags);
1694 list_add_tail(&hpdev->list_entry, &hbus->children);
1695 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1704 * get_pcichild_wslot() - Find device from slot
1705 * @hbus: Root PCI bus, as understood by this driver
1706 * @wslot: Location on the bus
1708 * This function looks up a PCI device and returns the internal
1709 * representation of it. It acquires a reference on it, so that
1710 * the device won't be deleted while somebody is using it. The
1711 * caller is responsible for calling put_pcichild() to release
1714 * Return: Internal representation of a PCI device
1716 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1719 unsigned long flags;
1720 struct hv_pci_dev *iter, *hpdev = NULL;
1722 spin_lock_irqsave(&hbus->device_list_lock, flags);
1723 list_for_each_entry(iter, &hbus->children, list_entry) {
1724 if (iter->desc.win_slot.slot == wslot) {
1726 get_pcichild(hpdev);
1730 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1736 * pci_devices_present_work() - Handle new list of child devices
1737 * @work: Work struct embedded in struct hv_dr_work
1739 * "Bus Relations" is the Windows term for "children of this
1740 * bus." The terminology is preserved here for people trying to
1741 * debug the interaction between Hyper-V and Linux. This
1742 * function is called when the parent partition reports a list
1743 * of functions that should be observed under this PCI Express
1746 * This function updates the list, and must tolerate being
1747 * called multiple times with the same information. The typical
1748 * number of child devices is one, with very atypical cases
1749 * involving three or four, so the algorithms used here can be
1750 * simple and inefficient.
1752 * It must also treat the omission of a previously observed device as
1753 * notification that the device no longer exists.
1755 * Note that this function is serialized with hv_eject_device_work(),
1756 * because both are pushed to the ordered workqueue hbus->wq.
1758 static void pci_devices_present_work(struct work_struct *work)
1762 struct pci_function_description *new_desc;
1763 struct hv_pci_dev *hpdev;
1764 struct hv_pcibus_device *hbus;
1765 struct list_head removed;
1766 struct hv_dr_work *dr_wrk;
1767 struct hv_dr_state *dr = NULL;
1768 unsigned long flags;
1770 dr_wrk = container_of(work, struct hv_dr_work, wrk);
1774 INIT_LIST_HEAD(&removed);
1776 /* Pull this off the queue and process it if it was the last one. */
1777 spin_lock_irqsave(&hbus->device_list_lock, flags);
1778 while (!list_empty(&hbus->dr_list)) {
1779 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
1781 list_del(&dr->list_entry);
1783 /* Throw this away if the list still has stuff in it. */
1784 if (!list_empty(&hbus->dr_list)) {
1789 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1796 /* First, mark all existing children as reported missing. */
1797 spin_lock_irqsave(&hbus->device_list_lock, flags);
1798 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1799 hpdev->reported_missing = true;
1801 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1803 /* Next, add back any reported devices. */
1804 for (child_no = 0; child_no < dr->device_count; child_no++) {
1806 new_desc = &dr->func[child_no];
1808 spin_lock_irqsave(&hbus->device_list_lock, flags);
1809 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1810 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
1811 (hpdev->desc.v_id == new_desc->v_id) &&
1812 (hpdev->desc.d_id == new_desc->d_id) &&
1813 (hpdev->desc.ser == new_desc->ser)) {
1814 hpdev->reported_missing = false;
1818 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1821 hpdev = new_pcichild_device(hbus, new_desc);
1823 dev_err(&hbus->hdev->device,
1824 "couldn't record a child device.\n");
1828 /* Move missing children to a list on the stack. */
1829 spin_lock_irqsave(&hbus->device_list_lock, flags);
1832 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1833 if (hpdev->reported_missing) {
1835 put_pcichild(hpdev);
1836 list_move_tail(&hpdev->list_entry, &removed);
1841 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1843 /* Delete everything that should no longer exist. */
1844 while (!list_empty(&removed)) {
1845 hpdev = list_first_entry(&removed, struct hv_pci_dev,
1847 list_del(&hpdev->list_entry);
1849 if (hpdev->pci_slot)
1850 pci_destroy_slot(hpdev->pci_slot);
1852 put_pcichild(hpdev);
1855 switch (hbus->state) {
1856 case hv_pcibus_installed:
1858 * Tell the core to rescan bus
1859 * because there may have been changes.
1861 pci_lock_rescan_remove();
1862 pci_scan_child_bus(hbus->pci_bus);
1863 hv_pci_assign_slots(hbus);
1864 pci_unlock_rescan_remove();
1867 case hv_pcibus_init:
1868 case hv_pcibus_probed:
1869 survey_child_resources(hbus);
1881 * hv_pci_devices_present() - Handles list of new children
1882 * @hbus: Root PCI bus, as understood by this driver
1883 * @relations: Packet from host listing children
1885 * This function is invoked whenever a new list of devices for
1888 static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
1889 struct pci_bus_relations *relations)
1891 struct hv_dr_state *dr;
1892 struct hv_dr_work *dr_wrk;
1893 unsigned long flags;
1896 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
1900 dr = kzalloc(offsetof(struct hv_dr_state, func) +
1901 (sizeof(struct pci_function_description) *
1902 (relations->device_count)), GFP_NOWAIT);
1908 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
1910 dr->device_count = relations->device_count;
1911 if (dr->device_count != 0) {
1912 memcpy(dr->func, relations->func,
1913 sizeof(struct pci_function_description) *
1917 spin_lock_irqsave(&hbus->device_list_lock, flags);
1919 * If pending_dr is true, we have already queued a work,
1920 * which will see the new dr. Otherwise, we need to
1923 pending_dr = !list_empty(&hbus->dr_list);
1924 list_add_tail(&dr->list_entry, &hbus->dr_list);
1925 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1931 queue_work(hbus->wq, &dr_wrk->wrk);
1936 * hv_eject_device_work() - Asynchronously handles ejection
1937 * @work: Work struct embedded in internal device struct
1939 * This function handles ejecting a device. Windows will
1940 * attempt to gracefully eject a device, waiting 60 seconds to
1941 * hear back from the guest OS that this completed successfully.
1942 * If this timer expires, the device will be forcibly removed.
1944 static void hv_eject_device_work(struct work_struct *work)
1946 struct pci_eject_response *ejct_pkt;
1947 struct hv_pcibus_device *hbus;
1948 struct hv_pci_dev *hpdev;
1949 struct pci_dev *pdev;
1950 unsigned long flags;
1953 struct pci_packet pkt;
1954 u8 buffer[sizeof(struct pci_eject_response)];
1957 hpdev = container_of(work, struct hv_pci_dev, wrk);
1960 WARN_ON(hpdev->state != hv_pcichild_ejecting);
1963 * Ejection can come before or after the PCI bus has been set up, so
1964 * attempt to find it and tear down the bus state, if it exists. This
1965 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1966 * because hbus->pci_bus may not exist yet.
1968 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
1969 pdev = pci_get_domain_bus_and_slot(hbus->sysdata.domain, 0, wslot);
1971 pci_lock_rescan_remove();
1972 pci_stop_and_remove_bus_device(pdev);
1974 pci_unlock_rescan_remove();
1977 spin_lock_irqsave(&hbus->device_list_lock, flags);
1978 list_del(&hpdev->list_entry);
1979 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1981 if (hpdev->pci_slot)
1982 pci_destroy_slot(hpdev->pci_slot);
1984 memset(&ctxt, 0, sizeof(ctxt));
1985 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
1986 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
1987 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1988 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
1989 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
1990 VM_PKT_DATA_INBAND, 0);
1992 /* For the get_pcichild() in hv_pci_eject_device() */
1993 put_pcichild(hpdev);
1994 /* For the two refs got in new_pcichild_device() */
1995 put_pcichild(hpdev);
1996 put_pcichild(hpdev);
1997 /* hpdev has been freed. Do not use it any more. */
2003 * hv_pci_eject_device() - Handles device ejection
2004 * @hpdev: Internal device tracking struct
2006 * This function is invoked when an ejection packet arrives. It
2007 * just schedules work so that we don't re-enter the packet
2008 * delivery code handling the ejection.
2010 static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
2012 hpdev->state = hv_pcichild_ejecting;
2013 get_pcichild(hpdev);
2014 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
2015 get_hvpcibus(hpdev->hbus);
2016 queue_work(hpdev->hbus->wq, &hpdev->wrk);
2020 * hv_pci_onchannelcallback() - Handles incoming packets
2021 * @context: Internal bus tracking struct
2023 * This function is invoked whenever the host sends a packet to
2024 * this channel (which is private to this root PCI bus).
2026 static void hv_pci_onchannelcallback(void *context)
2028 const int packet_size = 0x100;
2030 struct hv_pcibus_device *hbus = context;
2033 struct vmpacket_descriptor *desc;
2034 unsigned char *buffer;
2035 int bufferlen = packet_size;
2036 struct pci_packet *comp_packet;
2037 struct pci_response *response;
2038 struct pci_incoming_message *new_message;
2039 struct pci_bus_relations *bus_rel;
2040 struct pci_dev_incoming *dev_message;
2041 struct hv_pci_dev *hpdev;
2043 buffer = kmalloc(bufferlen, GFP_ATOMIC);
2048 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
2049 bufferlen, &bytes_recvd, &req_id);
2051 if (ret == -ENOBUFS) {
2053 /* Handle large packet */
2054 bufferlen = bytes_recvd;
2055 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
2061 /* Zero length indicates there are no more packets. */
2062 if (ret || !bytes_recvd)
2066 * All incoming packets must be at least as large as a
2069 if (bytes_recvd <= sizeof(struct pci_response))
2071 desc = (struct vmpacket_descriptor *)buffer;
2073 switch (desc->type) {
2077 * The host is trusted, and thus it's safe to interpret
2078 * this transaction ID as a pointer.
2080 comp_packet = (struct pci_packet *)req_id;
2081 response = (struct pci_response *)buffer;
2082 comp_packet->completion_func(comp_packet->compl_ctxt,
2087 case VM_PKT_DATA_INBAND:
2089 new_message = (struct pci_incoming_message *)buffer;
2090 switch (new_message->message_type.type) {
2091 case PCI_BUS_RELATIONS:
2093 bus_rel = (struct pci_bus_relations *)buffer;
2095 offsetof(struct pci_bus_relations, func) +
2096 (sizeof(struct pci_function_description) *
2097 (bus_rel->device_count))) {
2098 dev_err(&hbus->hdev->device,
2099 "bus relations too small\n");
2103 hv_pci_devices_present(hbus, bus_rel);
2108 dev_message = (struct pci_dev_incoming *)buffer;
2109 hpdev = get_pcichild_wslot(hbus,
2110 dev_message->wslot.slot);
2112 hv_pci_eject_device(hpdev);
2113 put_pcichild(hpdev);
2118 dev_warn(&hbus->hdev->device,
2119 "Unimplemented protocol message %x\n",
2120 new_message->message_type.type);
2126 dev_err(&hbus->hdev->device,
2127 "unhandled packet type %d, tid %llx len %d\n",
2128 desc->type, req_id, bytes_recvd);
2137 * hv_pci_protocol_negotiation() - Set up protocol
2138 * @hdev: VMBus's tracking struct for this root PCI bus
2140 * This driver is intended to support running on Windows 10
2141 * (server) and later versions. It will not run on earlier
2142 * versions, as they assume that many of the operations which
2143 * Linux needs accomplished with a spinlock held were done via
2144 * asynchronous messaging via VMBus. Windows 10 increases the
2145 * surface area of PCI emulation so that these actions can take
2146 * place by suspending a virtual processor for their duration.
2148 * This function negotiates the channel protocol version,
2149 * failing if the host doesn't support the necessary protocol
2152 static int hv_pci_protocol_negotiation(struct hv_device *hdev)
2154 struct pci_version_request *version_req;
2155 struct hv_pci_compl comp_pkt;
2156 struct pci_packet *pkt;
2161 * Initiate the handshake with the host and negotiate
2162 * a version that the host can support. We start with the
2163 * highest version number and go down if the host cannot
2166 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2170 init_completion(&comp_pkt.host_event);
2171 pkt->completion_func = hv_pci_generic_compl;
2172 pkt->compl_ctxt = &comp_pkt;
2173 version_req = (struct pci_version_request *)&pkt->message;
2174 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
2176 for (i = 0; i < ARRAY_SIZE(pci_protocol_versions); i++) {
2177 version_req->protocol_version = pci_protocol_versions[i];
2178 ret = vmbus_sendpacket(hdev->channel, version_req,
2179 sizeof(struct pci_version_request),
2180 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2181 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2183 ret = wait_for_response(hdev, &comp_pkt.host_event);
2186 dev_err(&hdev->device,
2187 "PCI Pass-through VSP failed to request version: %d",
2192 if (comp_pkt.completion_status >= 0) {
2193 pci_protocol_version = pci_protocol_versions[i];
2194 dev_info(&hdev->device,
2195 "PCI VMBus probing: Using version %#x\n",
2196 pci_protocol_version);
2200 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2201 dev_err(&hdev->device,
2202 "PCI Pass-through VSP failed version request: %#x",
2203 comp_pkt.completion_status);
2208 reinit_completion(&comp_pkt.host_event);
2211 dev_err(&hdev->device,
2212 "PCI pass-through VSP failed to find supported version");
2221 * hv_pci_free_bridge_windows() - Release memory regions for the
2223 * @hbus: Root PCI bus, as understood by this driver
2225 static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2228 * Set the resources back to the way they looked when they
2229 * were allocated by setting IORESOURCE_BUSY again.
2232 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2233 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
2234 vmbus_free_mmio(hbus->low_mmio_res->start,
2235 resource_size(hbus->low_mmio_res));
2238 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2239 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
2240 vmbus_free_mmio(hbus->high_mmio_res->start,
2241 resource_size(hbus->high_mmio_res));
2246 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2248 * @hbus: Root PCI bus, as understood by this driver
2250 * This function calls vmbus_allocate_mmio(), which is itself a
2251 * bit of a compromise. Ideally, we might change the pnp layer
2252 * in the kernel such that it comprehends either PCI devices
2253 * which are "grandchildren of ACPI," with some intermediate bus
2254 * node (in this case, VMBus) or change it such that it
2255 * understands VMBus. The pnp layer, however, has been declared
2256 * deprecated, and not subject to change.
2258 * The workaround, implemented here, is to ask VMBus to allocate
2259 * MMIO space for this bus. VMBus itself knows which ranges are
2260 * appropriate by looking at its own ACPI objects. Then, after
2261 * these ranges are claimed, they're modified to look like they
2262 * would have looked if the ACPI and pnp code had allocated
2263 * bridge windows. These descriptors have to exist in this form
2264 * in order to satisfy the code which will get invoked when the
2265 * endpoint PCI function driver calls request_mem_region() or
2266 * request_mem_region_exclusive().
2268 * Return: 0 on success, -errno on failure
2270 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2272 resource_size_t align;
2275 if (hbus->low_mmio_space) {
2276 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2277 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2278 (u64)(u32)0xffffffff,
2279 hbus->low_mmio_space,
2282 dev_err(&hbus->hdev->device,
2283 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2284 hbus->low_mmio_space);
2288 /* Modify this resource to become a bridge window. */
2289 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2290 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2291 pci_add_resource(&hbus->resources_for_children,
2292 hbus->low_mmio_res);
2295 if (hbus->high_mmio_space) {
2296 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2297 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2299 hbus->high_mmio_space, align,
2302 dev_err(&hbus->hdev->device,
2303 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2304 hbus->high_mmio_space);
2305 goto release_low_mmio;
2308 /* Modify this resource to become a bridge window. */
2309 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2310 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2311 pci_add_resource(&hbus->resources_for_children,
2312 hbus->high_mmio_res);
2318 if (hbus->low_mmio_res) {
2319 vmbus_free_mmio(hbus->low_mmio_res->start,
2320 resource_size(hbus->low_mmio_res));
2327 * hv_allocate_config_window() - Find MMIO space for PCI Config
2328 * @hbus: Root PCI bus, as understood by this driver
2330 * This function claims memory-mapped I/O space for accessing
2331 * configuration space for the functions on this bus.
2333 * Return: 0 on success, -errno on failure
2335 static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2340 * Set up a region of MMIO space to use for accessing configuration
2343 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2344 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2349 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2350 * resource claims (those which cannot be overlapped) and the ranges
2351 * which are valid for the children of this bus, which are intended
2352 * to be overlapped by those children. Set the flag on this claim
2353 * meaning that this region can't be overlapped.
2356 hbus->mem_config->flags |= IORESOURCE_BUSY;
2361 static void hv_free_config_window(struct hv_pcibus_device *hbus)
2363 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
2367 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2368 * @hdev: VMBus's tracking struct for this root PCI bus
2370 * Return: 0 on success, -errno on failure
2372 static int hv_pci_enter_d0(struct hv_device *hdev)
2374 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2375 struct pci_bus_d0_entry *d0_entry;
2376 struct hv_pci_compl comp_pkt;
2377 struct pci_packet *pkt;
2381 * Tell the host that the bus is ready to use, and moved into the
2382 * powered-on state. This includes telling the host which region
2383 * of memory-mapped I/O space has been chosen for configuration space
2386 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2390 init_completion(&comp_pkt.host_event);
2391 pkt->completion_func = hv_pci_generic_compl;
2392 pkt->compl_ctxt = &comp_pkt;
2393 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
2394 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
2395 d0_entry->mmio_base = hbus->mem_config->start;
2397 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2398 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2399 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2401 ret = wait_for_response(hdev, &comp_pkt.host_event);
2406 if (comp_pkt.completion_status < 0) {
2407 dev_err(&hdev->device,
2408 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2409 comp_pkt.completion_status);
2422 * hv_pci_query_relations() - Ask host to send list of child
2424 * @hdev: VMBus's tracking struct for this root PCI bus
2426 * Return: 0 on success, -errno on failure
2428 static int hv_pci_query_relations(struct hv_device *hdev)
2430 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2431 struct pci_message message;
2432 struct completion comp;
2435 /* Ask the host to send along the list of child devices */
2436 init_completion(&comp);
2437 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2440 memset(&message, 0, sizeof(message));
2441 message.type = PCI_QUERY_BUS_RELATIONS;
2443 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2444 0, VM_PKT_DATA_INBAND, 0);
2446 ret = wait_for_response(hdev, &comp);
2449 * In the case of fast device addition/removal, it's possible that
2450 * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we
2451 * already got a PCI_BUS_RELATIONS* message from the host and the
2452 * channel callback already scheduled a work to hbus->wq, which can be
2453 * running pci_devices_present_work() -> survey_child_resources() ->
2454 * complete(&hbus->survey_event), even after hv_pci_query_relations()
2455 * exits and the stack variable 'comp' is no longer valid; as a result,
2456 * a hang or a page fault may happen when the complete() calls
2457 * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from
2458 * hv_pci_query_relations() to avoid the issues. Note: if 'ret' is
2459 * -ENODEV, there can't be any more work item scheduled to hbus->wq
2460 * after the flush_workqueue(): see vmbus_onoffer_rescind() ->
2461 * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() ->
2462 * channel->rescind = true.
2464 flush_workqueue(hbus->wq);
2470 * hv_send_resources_allocated() - Report local resource choices
2471 * @hdev: VMBus's tracking struct for this root PCI bus
2473 * The host OS is expecting to be sent a request as a message
2474 * which contains all the resources that the device will use.
2475 * The response contains those same resources, "translated"
2476 * which is to say, the values which should be used by the
2477 * hardware, when it delivers an interrupt. (MMIO resources are
2478 * used in local terms.) This is nice for Windows, and lines up
2479 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2480 * is deeply expecting to scan an emulated PCI configuration
2481 * space. So this message is sent here only to drive the state
2482 * machine on the host forward.
2484 * Return: 0 on success, -errno on failure
2486 static int hv_send_resources_allocated(struct hv_device *hdev)
2488 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2489 struct pci_resources_assigned *res_assigned;
2490 struct pci_resources_assigned2 *res_assigned2;
2491 struct hv_pci_compl comp_pkt;
2492 struct hv_pci_dev *hpdev;
2493 struct pci_packet *pkt;
2498 size_res = (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2)
2499 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2501 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
2507 for (wslot = 0; wslot < 256; wslot++) {
2508 hpdev = get_pcichild_wslot(hbus, wslot);
2512 memset(pkt, 0, sizeof(*pkt) + size_res);
2513 init_completion(&comp_pkt.host_event);
2514 pkt->completion_func = hv_pci_generic_compl;
2515 pkt->compl_ctxt = &comp_pkt;
2517 if (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2) {
2519 (struct pci_resources_assigned *)&pkt->message;
2520 res_assigned->message_type.type =
2521 PCI_RESOURCES_ASSIGNED;
2522 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2525 (struct pci_resources_assigned2 *)&pkt->message;
2526 res_assigned2->message_type.type =
2527 PCI_RESOURCES_ASSIGNED2;
2528 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2530 put_pcichild(hpdev);
2532 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2533 size_res, (unsigned long)pkt,
2535 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2537 ret = wait_for_response(hdev, &comp_pkt.host_event);
2541 if (comp_pkt.completion_status < 0) {
2543 dev_err(&hdev->device,
2544 "resource allocated returned 0x%x",
2545 comp_pkt.completion_status);
2555 * hv_send_resources_released() - Report local resources
2557 * @hdev: VMBus's tracking struct for this root PCI bus
2559 * Return: 0 on success, -errno on failure
2561 static int hv_send_resources_released(struct hv_device *hdev)
2563 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2564 struct pci_child_message pkt;
2565 struct hv_pci_dev *hpdev;
2569 for (wslot = 0; wslot < 256; wslot++) {
2570 hpdev = get_pcichild_wslot(hbus, wslot);
2574 memset(&pkt, 0, sizeof(pkt));
2575 pkt.message_type.type = PCI_RESOURCES_RELEASED;
2576 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2578 put_pcichild(hpdev);
2580 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2581 VM_PKT_DATA_INBAND, 0);
2589 static void get_hvpcibus(struct hv_pcibus_device *hbus)
2591 refcount_inc(&hbus->remove_lock);
2594 static void put_hvpcibus(struct hv_pcibus_device *hbus)
2596 if (refcount_dec_and_test(&hbus->remove_lock))
2597 complete(&hbus->remove_event);
2601 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2602 * @hdev: VMBus's tracking struct for this root PCI bus
2603 * @dev_id: Identifies the device itself
2605 * Return: 0 on success, -errno on failure
2607 static int hv_pci_probe(struct hv_device *hdev,
2608 const struct hv_vmbus_device_id *dev_id)
2610 struct hv_pcibus_device *hbus;
2614 * hv_pcibus_device contains the hypercall arguments for retargeting in
2615 * hv_irq_unmask(). Those must not cross a page boundary.
2617 BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
2619 hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
2622 hbus->state = hv_pcibus_init;
2625 * The PCI bus "domain" is what is called "segment" in ACPI and
2626 * other specs. Pull it from the instance ID, to get something
2627 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2628 * do the same thing for consistency. Note that, since this code
2629 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2630 * that (1) the only domain in use for something that looks like
2631 * a physical PCI bus (which is actually emulated by the
2632 * hypervisor) is domain 0 and (2) there will be no overlap
2633 * between domains derived from these instance IDs in the same
2636 hbus->sysdata.domain = hdev->dev_instance.b[9] |
2637 hdev->dev_instance.b[8] << 8;
2640 refcount_set(&hbus->remove_lock, 1);
2641 INIT_LIST_HEAD(&hbus->children);
2642 INIT_LIST_HEAD(&hbus->dr_list);
2643 INIT_LIST_HEAD(&hbus->resources_for_children);
2644 spin_lock_init(&hbus->config_lock);
2645 spin_lock_init(&hbus->device_list_lock);
2646 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
2647 init_completion(&hbus->remove_event);
2648 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
2649 hbus->sysdata.domain);
2655 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
2656 hv_pci_onchannelcallback, hbus);
2660 hv_set_drvdata(hdev, hbus);
2662 ret = hv_pci_protocol_negotiation(hdev);
2666 ret = hv_allocate_config_window(hbus);
2670 hbus->cfg_addr = ioremap(hbus->mem_config->start,
2671 PCI_CONFIG_MMIO_LENGTH);
2672 if (!hbus->cfg_addr) {
2673 dev_err(&hdev->device,
2674 "Unable to map a virtual address for config space\n");
2679 hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
2680 if (!hbus->sysdata.fwnode) {
2685 ret = hv_pcie_init_irq_domain(hbus);
2689 ret = hv_pci_query_relations(hdev);
2691 goto free_irq_domain;
2693 ret = hv_pci_enter_d0(hdev);
2695 goto free_irq_domain;
2697 ret = hv_pci_allocate_bridge_windows(hbus);
2699 goto free_irq_domain;
2701 ret = hv_send_resources_allocated(hdev);
2705 prepopulate_bars(hbus);
2707 hbus->state = hv_pcibus_probed;
2709 ret = create_root_hv_pci_bus(hbus);
2716 hv_pci_free_bridge_windows(hbus);
2718 irq_domain_remove(hbus->irq_domain);
2720 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2722 iounmap(hbus->cfg_addr);
2724 hv_free_config_window(hbus);
2726 vmbus_close(hdev->channel);
2728 destroy_workqueue(hbus->wq);
2730 free_page((unsigned long)hbus);
2734 static void hv_pci_bus_exit(struct hv_device *hdev)
2736 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2738 struct pci_packet teardown_packet;
2739 u8 buffer[sizeof(struct pci_message)];
2741 struct pci_bus_relations relations;
2742 struct hv_pci_compl comp_pkt;
2746 * After the host sends the RESCIND_CHANNEL message, it doesn't
2747 * access the per-channel ringbuffer any longer.
2749 if (hdev->channel->rescind)
2752 /* Delete any children which might still exist. */
2753 memset(&relations, 0, sizeof(relations));
2754 hv_pci_devices_present(hbus, &relations);
2756 ret = hv_send_resources_released(hdev);
2758 dev_err(&hdev->device,
2759 "Couldn't send resources released packet(s)\n");
2761 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
2762 init_completion(&comp_pkt.host_event);
2763 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
2764 pkt.teardown_packet.compl_ctxt = &comp_pkt;
2765 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
2767 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
2768 sizeof(struct pci_message),
2769 (unsigned long)&pkt.teardown_packet,
2771 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2773 wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
2777 * hv_pci_remove() - Remove routine for this VMBus channel
2778 * @hdev: VMBus's tracking struct for this root PCI bus
2780 * Return: 0 on success, -errno on failure
2782 static int hv_pci_remove(struct hv_device *hdev)
2784 struct hv_pcibus_device *hbus;
2786 hbus = hv_get_drvdata(hdev);
2787 if (hbus->state == hv_pcibus_installed) {
2788 /* Remove the bus from PCI's point of view. */
2789 pci_lock_rescan_remove();
2790 pci_stop_root_bus(hbus->pci_bus);
2791 hv_pci_remove_slots(hbus);
2792 pci_remove_root_bus(hbus->pci_bus);
2793 pci_unlock_rescan_remove();
2794 hbus->state = hv_pcibus_removed;
2797 hv_pci_bus_exit(hdev);
2799 vmbus_close(hdev->channel);
2801 iounmap(hbus->cfg_addr);
2802 hv_free_config_window(hbus);
2803 pci_free_resource_list(&hbus->resources_for_children);
2804 hv_pci_free_bridge_windows(hbus);
2805 irq_domain_remove(hbus->irq_domain);
2806 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2808 wait_for_completion(&hbus->remove_event);
2809 destroy_workqueue(hbus->wq);
2810 free_page((unsigned long)hbus);
2814 static const struct hv_vmbus_device_id hv_pci_id_table[] = {
2815 /* PCI Pass-through Class ID */
2816 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2821 MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
2823 static struct hv_driver hv_pci_drv = {
2825 .id_table = hv_pci_id_table,
2826 .probe = hv_pci_probe,
2827 .remove = hv_pci_remove,
2830 static void __exit exit_hv_pci_drv(void)
2832 vmbus_driver_unregister(&hv_pci_drv);
2835 static int __init init_hv_pci_drv(void)
2837 return vmbus_driver_register(&hv_pci_drv);
2840 module_init(init_hv_pci_drv);
2841 module_exit(exit_hv_pci_drv);
2843 MODULE_DESCRIPTION("Hyper-V PCI");
2844 MODULE_LICENSE("GPL v2");