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/pci-ecam.h>
44 #include <linux/delay.h>
45 #include <linux/semaphore.h>
46 #include <linux/irq.h>
47 #include <linux/msi.h>
48 #include <linux/hyperv.h>
49 #include <linux/refcount.h>
50 #include <linux/irqdomain.h>
51 #include <linux/acpi.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 */
66 PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */
67 PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4), /* WS2022 */
70 #define CPU_AFFINITY_ALL -1ULL
73 * Supported protocol versions in the order of probing - highest go
76 static enum pci_protocol_version_t pci_protocol_versions[] = {
77 PCI_PROTOCOL_VERSION_1_4,
78 PCI_PROTOCOL_VERSION_1_3,
79 PCI_PROTOCOL_VERSION_1_2,
80 PCI_PROTOCOL_VERSION_1_1,
83 #define PCI_CONFIG_MMIO_LENGTH 0x2000
84 #define CFG_PAGE_OFFSET 0x1000
85 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
87 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
89 #define STATUS_REVISION_MISMATCH 0xC0000059
91 /* space for 32bit serial number as string */
92 #define SLOT_NAME_SIZE 11
98 enum pci_message_type {
102 PCI_MESSAGE_BASE = 0x42490000,
103 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
104 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
105 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
106 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
107 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
108 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
109 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
110 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
111 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
112 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
113 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
114 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
115 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
116 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
117 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
118 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
119 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
120 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
121 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
122 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
123 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
124 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
125 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
126 PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
127 PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A,
128 PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B,
133 * Structures defining the virtual PCI Express protocol.
145 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
146 * which is all this driver does. This representation is the one used in
147 * Windows, which is what is expected when sending this back and forth with
148 * the Hyper-V parent partition.
150 union win_slot_encoding {
160 * Pretty much as defined in the PCI Specifications.
162 struct pci_function_description {
163 u16 v_id; /* vendor ID */
164 u16 d_id; /* device ID */
170 union win_slot_encoding win_slot;
171 u32 ser; /* serial number */
174 enum pci_device_description_flags {
175 HV_PCI_DEVICE_FLAG_NONE = 0x0,
176 HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
179 struct pci_function_description2 {
180 u16 v_id; /* vendor ID */
181 u16 d_id; /* device ID */
187 union win_slot_encoding win_slot;
188 u32 ser; /* serial number */
190 u16 virtual_numa_node;
197 * @delivery_mode: As defined in Intel's Programmer's
198 * Reference Manual, Volume 3, Chapter 8.
199 * @vector_count: Number of contiguous entries in the
200 * Interrupt Descriptor Table that are
201 * occupied by this Message-Signaled
202 * Interrupt. For "MSI", as first defined
203 * in PCI 2.2, this can be between 1 and
204 * 32. For "MSI-X," as first defined in PCI
205 * 3.0, this must be 1, as each MSI-X table
206 * entry would have its own descriptor.
207 * @reserved: Empty space
208 * @cpu_mask: All the target virtual processors.
219 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
221 * @delivery_mode: As defined in Intel's Programmer's
222 * Reference Manual, Volume 3, Chapter 8.
223 * @vector_count: Number of contiguous entries in the
224 * Interrupt Descriptor Table that are
225 * occupied by this Message-Signaled
226 * Interrupt. For "MSI", as first defined
227 * in PCI 2.2, this can be between 1 and
228 * 32. For "MSI-X," as first defined in PCI
229 * 3.0, this must be 1, as each MSI-X table
230 * entry would have its own descriptor.
231 * @processor_count: number of bits enabled in array.
232 * @processor_array: All the target virtual processors.
234 struct hv_msi_desc2 {
239 u16 processor_array[32];
243 * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
244 * Everything is the same as in 'hv_msi_desc2' except that the size of the
245 * 'vector' field is larger to support bigger vector values. For ex: LPI
248 struct hv_msi_desc3 {
254 u16 processor_array[32];
258 * struct tran_int_desc
259 * @reserved: unused, padding
260 * @vector_count: same as in hv_msi_desc
261 * @data: This is the "data payload" value that is
262 * written by the device when it generates
263 * a message-signaled interrupt, either MSI
265 * @address: This is the address to which the data
266 * payload is written on interrupt
269 struct tran_int_desc {
277 * A generic message format for virtual PCI.
278 * Specific message formats are defined later in the file.
285 struct pci_child_message {
286 struct pci_message message_type;
287 union win_slot_encoding wslot;
290 struct pci_incoming_message {
291 struct vmpacket_descriptor hdr;
292 struct pci_message message_type;
295 struct pci_response {
296 struct vmpacket_descriptor hdr;
297 s32 status; /* negative values are failures */
301 void (*completion_func)(void *context, struct pci_response *resp,
302 int resp_packet_size);
305 struct pci_message message[];
309 * Specific message types supporting the PCI protocol.
313 * Version negotiation message. Sent from the guest to the host.
314 * The guest is free to try different versions until the host
315 * accepts the version.
317 * pci_version: The protocol version requested.
318 * is_last_attempt: If TRUE, this is the last version guest will request.
319 * reservedz: Reserved field, set to zero.
322 struct pci_version_request {
323 struct pci_message message_type;
324 u32 protocol_version;
328 * Bus D0 Entry. This is sent from the guest to the host when the virtual
329 * bus (PCI Express port) is ready for action.
332 struct pci_bus_d0_entry {
333 struct pci_message message_type;
338 struct pci_bus_relations {
339 struct pci_incoming_message incoming;
341 struct pci_function_description func[];
344 struct pci_bus_relations2 {
345 struct pci_incoming_message incoming;
347 struct pci_function_description2 func[];
350 struct pci_q_res_req_response {
351 struct vmpacket_descriptor hdr;
352 s32 status; /* negative values are failures */
353 u32 probed_bar[PCI_STD_NUM_BARS];
356 struct pci_set_power {
357 struct pci_message message_type;
358 union win_slot_encoding wslot;
359 u32 power_state; /* In Windows terms */
363 struct pci_set_power_response {
364 struct vmpacket_descriptor hdr;
365 s32 status; /* negative values are failures */
366 union win_slot_encoding wslot;
367 u32 resultant_state; /* In Windows terms */
371 struct pci_resources_assigned {
372 struct pci_message message_type;
373 union win_slot_encoding wslot;
374 u8 memory_range[0x14][6]; /* not used here */
379 struct pci_resources_assigned2 {
380 struct pci_message message_type;
381 union win_slot_encoding wslot;
382 u8 memory_range[0x14][6]; /* not used here */
383 u32 msi_descriptor_count;
387 struct pci_create_interrupt {
388 struct pci_message message_type;
389 union win_slot_encoding wslot;
390 struct hv_msi_desc int_desc;
393 struct pci_create_int_response {
394 struct pci_response response;
396 struct tran_int_desc int_desc;
399 struct pci_create_interrupt2 {
400 struct pci_message message_type;
401 union win_slot_encoding wslot;
402 struct hv_msi_desc2 int_desc;
405 struct pci_create_interrupt3 {
406 struct pci_message message_type;
407 union win_slot_encoding wslot;
408 struct hv_msi_desc3 int_desc;
411 struct pci_delete_interrupt {
412 struct pci_message message_type;
413 union win_slot_encoding wslot;
414 struct tran_int_desc int_desc;
418 * Note: the VM must pass a valid block id, wslot and bytes_requested.
420 struct pci_read_block {
421 struct pci_message message_type;
423 union win_slot_encoding wslot;
427 struct pci_read_block_response {
428 struct vmpacket_descriptor hdr;
430 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
434 * Note: the VM must pass a valid block id, wslot and byte_count.
436 struct pci_write_block {
437 struct pci_message message_type;
439 union win_slot_encoding wslot;
441 u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
444 struct pci_dev_inval_block {
445 struct pci_incoming_message incoming;
446 union win_slot_encoding wslot;
450 struct pci_dev_incoming {
451 struct pci_incoming_message incoming;
452 union win_slot_encoding wslot;
455 struct pci_eject_response {
456 struct pci_message message_type;
457 union win_slot_encoding wslot;
461 static int pci_ring_size = (4 * PAGE_SIZE);
464 * Driver specific state.
467 enum hv_pcibus_state {
475 struct hv_pcibus_device {
477 struct pci_sysdata sysdata;
478 #elif defined(CONFIG_ARM64)
479 struct pci_config_window sysdata;
481 struct pci_host_bridge *bridge;
482 struct fwnode_handle *fwnode;
483 /* Protocol version negotiated with the host */
484 enum pci_protocol_version_t protocol_version;
485 enum hv_pcibus_state state;
486 struct hv_device *hdev;
487 resource_size_t low_mmio_space;
488 resource_size_t high_mmio_space;
489 struct resource *mem_config;
490 struct resource *low_mmio_res;
491 struct resource *high_mmio_res;
492 struct completion *survey_event;
493 struct pci_bus *pci_bus;
494 spinlock_t config_lock; /* Avoid two threads writing index page */
495 spinlock_t device_list_lock; /* Protect lists below */
496 void __iomem *cfg_addr;
498 struct list_head children;
499 struct list_head dr_list;
501 struct msi_domain_info msi_info;
502 struct irq_domain *irq_domain;
504 spinlock_t retarget_msi_interrupt_lock;
506 struct workqueue_struct *wq;
508 /* Highest slot of child device with resources allocated */
509 int wslot_res_allocated;
511 /* hypercall arg, must not cross page boundary */
512 struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
515 * Don't put anything here: retarget_msi_interrupt_params must be last
520 * Tracks "Device Relations" messages from the host, which must be both
521 * processed in order and deferred so that they don't run in the context
522 * of the incoming packet callback.
525 struct work_struct wrk;
526 struct hv_pcibus_device *bus;
529 struct hv_pcidev_description {
530 u16 v_id; /* vendor ID */
531 u16 d_id; /* device ID */
537 union win_slot_encoding win_slot;
538 u32 ser; /* serial number */
540 u16 virtual_numa_node;
544 struct list_head list_entry;
546 struct hv_pcidev_description func[];
549 enum hv_pcichild_state {
550 hv_pcichild_init = 0,
551 hv_pcichild_requirements,
552 hv_pcichild_resourced,
553 hv_pcichild_ejecting,
558 /* List protected by pci_rescan_remove_lock */
559 struct list_head list_entry;
561 enum hv_pcichild_state state;
562 struct pci_slot *pci_slot;
563 struct hv_pcidev_description desc;
564 bool reported_missing;
565 struct hv_pcibus_device *hbus;
566 struct work_struct wrk;
568 void (*block_invalidate)(void *context, u64 block_mask);
569 void *invalidate_context;
572 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
573 * read it back, for each of the BAR offsets within config space.
575 u32 probed_bar[PCI_STD_NUM_BARS];
578 struct hv_pci_compl {
579 struct completion host_event;
580 s32 completion_status;
583 static void hv_pci_onchannelcallback(void *context);
586 #define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
587 #define FLOW_HANDLER handle_edge_irq
588 #define FLOW_NAME "edge"
590 static int hv_pci_irqchip_init(void)
595 static struct irq_domain *hv_pci_get_root_domain(void)
597 return x86_vector_domain;
600 static unsigned int hv_msi_get_int_vector(struct irq_data *data)
602 struct irq_cfg *cfg = irqd_cfg(data);
607 static void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
608 struct msi_desc *msi_desc)
610 msi_entry->address.as_uint32 = msi_desc->msg.address_lo;
611 msi_entry->data.as_uint32 = msi_desc->msg.data;
614 static int hv_msi_prepare(struct irq_domain *domain, struct device *dev,
615 int nvec, msi_alloc_info_t *info)
617 return pci_msi_prepare(domain, dev, nvec, info);
619 #elif defined(CONFIG_ARM64)
621 * SPI vectors to use for vPCI; arch SPIs range is [32, 1019], but leaving a bit
622 * of room at the start to allow for SPIs to be specified through ACPI and
623 * starting with a power of two to satisfy power of 2 multi-MSI requirement.
625 #define HV_PCI_MSI_SPI_START 64
626 #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
627 #define DELIVERY_MODE 0
628 #define FLOW_HANDLER NULL
629 #define FLOW_NAME NULL
630 #define hv_msi_prepare NULL
632 struct hv_pci_chip_data {
633 DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
634 struct mutex map_lock;
637 /* Hyper-V vPCI MSI GIC IRQ domain */
638 static struct irq_domain *hv_msi_gic_irq_domain;
640 /* Hyper-V PCI MSI IRQ chip */
641 static struct irq_chip hv_arm64_msi_irq_chip = {
643 .irq_set_affinity = irq_chip_set_affinity_parent,
644 .irq_eoi = irq_chip_eoi_parent,
645 .irq_mask = irq_chip_mask_parent,
646 .irq_unmask = irq_chip_unmask_parent
649 static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
651 return irqd->parent_data->hwirq;
654 static void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
655 struct msi_desc *msi_desc)
657 msi_entry->address = ((u64)msi_desc->msg.address_hi << 32) |
658 msi_desc->msg.address_lo;
659 msi_entry->data = msi_desc->msg.data;
663 * @nr_bm_irqs: Indicates the number of IRQs that were allocated from
665 * @nr_dom_irqs: Indicates the number of IRQs that were allocated from
668 static void hv_pci_vec_irq_free(struct irq_domain *domain,
670 unsigned int nr_bm_irqs,
671 unsigned int nr_dom_irqs)
673 struct hv_pci_chip_data *chip_data = domain->host_data;
674 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
675 int first = d->hwirq - HV_PCI_MSI_SPI_START;
678 mutex_lock(&chip_data->map_lock);
679 bitmap_release_region(chip_data->spi_map,
681 get_count_order(nr_bm_irqs));
682 mutex_unlock(&chip_data->map_lock);
683 for (i = 0; i < nr_dom_irqs; i++) {
685 d = irq_domain_get_irq_data(domain, virq + i);
686 irq_domain_reset_irq_data(d);
689 irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
692 static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
694 unsigned int nr_irqs)
696 hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
699 static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
700 unsigned int nr_irqs,
701 irq_hw_number_t *hwirq)
703 struct hv_pci_chip_data *chip_data = domain->host_data;
706 /* Find and allocate region from the SPI bitmap */
707 mutex_lock(&chip_data->map_lock);
708 index = bitmap_find_free_region(chip_data->spi_map,
710 get_count_order(nr_irqs));
711 mutex_unlock(&chip_data->map_lock);
715 *hwirq = index + HV_PCI_MSI_SPI_START;
720 static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
722 irq_hw_number_t hwirq)
724 struct irq_fwspec fwspec;
728 fwspec.fwnode = domain->parent->fwnode;
729 fwspec.param_count = 2;
730 fwspec.param[0] = hwirq;
731 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
733 ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
738 * Since the interrupt specifier is not coming from ACPI or DT, the
739 * trigger type will need to be set explicitly. Otherwise, it will be
740 * set to whatever is in the GIC configuration.
742 d = irq_domain_get_irq_data(domain->parent, virq);
744 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
747 static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
748 unsigned int virq, unsigned int nr_irqs,
751 irq_hw_number_t hwirq;
755 ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
759 for (i = 0; i < nr_irqs; i++) {
760 ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
763 hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
767 irq_domain_set_hwirq_and_chip(domain, virq + i,
769 &hv_arm64_msi_irq_chip,
771 pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
778 * Pick the first cpu as the irq affinity that can be temporarily used for
779 * composing MSI from the hypervisor. GIC will eventually set the right
780 * affinity for the irq and the 'unmask' will retarget the interrupt to that
783 static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
784 struct irq_data *irqd, bool reserve)
786 int cpu = cpumask_first(cpu_present_mask);
788 irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
793 static const struct irq_domain_ops hv_pci_domain_ops = {
794 .alloc = hv_pci_vec_irq_domain_alloc,
795 .free = hv_pci_vec_irq_domain_free,
796 .activate = hv_pci_vec_irq_domain_activate,
799 static int hv_pci_irqchip_init(void)
801 static struct hv_pci_chip_data *chip_data;
802 struct fwnode_handle *fn = NULL;
805 chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
809 mutex_init(&chip_data->map_lock);
810 fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
815 * IRQ domain once enabled, should not be removed since there is no
816 * way to ensure that all the corresponding devices are also gone and
817 * no interrupts will be generated.
819 hv_msi_gic_irq_domain = acpi_irq_create_hierarchy(0, HV_PCI_MSI_SPI_NR,
820 fn, &hv_pci_domain_ops,
823 if (!hv_msi_gic_irq_domain) {
824 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
833 irq_domain_free_fwnode(fn);
838 static struct irq_domain *hv_pci_get_root_domain(void)
840 return hv_msi_gic_irq_domain;
842 #endif /* CONFIG_ARM64 */
845 * hv_pci_generic_compl() - Invoked for a completion packet
846 * @context: Set up by the sender of the packet.
847 * @resp: The response packet
848 * @resp_packet_size: Size in bytes of the packet
850 * This function is used to trigger an event and report status
851 * for any message for which the completion packet contains a
852 * status and nothing else.
854 static void hv_pci_generic_compl(void *context, struct pci_response *resp,
855 int resp_packet_size)
857 struct hv_pci_compl *comp_pkt = context;
859 if (resp_packet_size >= offsetofend(struct pci_response, status))
860 comp_pkt->completion_status = resp->status;
862 comp_pkt->completion_status = -1;
864 complete(&comp_pkt->host_event);
867 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
870 static void get_pcichild(struct hv_pci_dev *hpdev)
872 refcount_inc(&hpdev->refs);
875 static void put_pcichild(struct hv_pci_dev *hpdev)
877 if (refcount_dec_and_test(&hpdev->refs))
882 * There is no good way to get notified from vmbus_onoffer_rescind(),
883 * so let's use polling here, since this is not a hot path.
885 static int wait_for_response(struct hv_device *hdev,
886 struct completion *comp)
889 if (hdev->channel->rescind) {
890 dev_warn_once(&hdev->device, "The device is gone.\n");
894 if (wait_for_completion_timeout(comp, HZ / 10))
902 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
903 * @devfn: The Linux representation of PCI slot
905 * Windows uses a slightly different representation of PCI slot.
907 * Return: The Windows representation
909 static u32 devfn_to_wslot(int devfn)
911 union win_slot_encoding wslot;
914 wslot.bits.dev = PCI_SLOT(devfn);
915 wslot.bits.func = PCI_FUNC(devfn);
921 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
922 * @wslot: The Windows representation of PCI slot
924 * Windows uses a slightly different representation of PCI slot.
926 * Return: The Linux representation
928 static int wslot_to_devfn(u32 wslot)
930 union win_slot_encoding slot_no;
932 slot_no.slot = wslot;
933 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
937 * PCI Configuration Space for these root PCI buses is implemented as a pair
938 * of pages in memory-mapped I/O space. Writing to the first page chooses
939 * the PCI function being written or read. Once the first page has been
940 * written to, the following page maps in the entire configuration space of
945 * _hv_pcifront_read_config() - Internal PCI config read
946 * @hpdev: The PCI driver's representation of the device
947 * @where: Offset within config space
948 * @size: Size of the transfer
949 * @val: Pointer to the buffer receiving the data
951 static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
955 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
958 * If the attempt is to read the IDs or the ROM BAR, simulate that.
960 if (where + size <= PCI_COMMAND) {
961 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
962 } else if (where >= PCI_CLASS_REVISION && where + size <=
963 PCI_CACHE_LINE_SIZE) {
964 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
965 PCI_CLASS_REVISION, size);
966 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
968 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
969 PCI_SUBSYSTEM_VENDOR_ID, size);
970 } else if (where >= PCI_ROM_ADDRESS && where + size <=
971 PCI_CAPABILITY_LIST) {
972 /* ROM BARs are unimplemented */
974 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
977 * Interrupt Line and Interrupt PIN are hard-wired to zero
978 * because this front-end only supports message-signaled
982 } else if (where + size <= CFG_PAGE_SIZE) {
983 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
984 /* Choose the function to be read. (See comment above) */
985 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
986 /* Make sure the function was chosen before we start reading. */
988 /* Read from that function's config space. */
1001 * Make sure the read was done before we release the spinlock
1002 * allowing consecutive reads/writes.
1005 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1007 dev_err(&hpdev->hbus->hdev->device,
1008 "Attempt to read beyond a function's config space.\n");
1012 static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
1015 unsigned long flags;
1016 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
1019 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1021 /* Choose the function to be read. (See comment above) */
1022 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
1023 /* Make sure the function was chosen before we start reading. */
1025 /* Read from that function's config space. */
1028 * mb() is not required here, because the spin_unlock_irqrestore()
1032 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1038 * _hv_pcifront_write_config() - Internal PCI config write
1039 * @hpdev: The PCI driver's representation of the device
1040 * @where: Offset within config space
1041 * @size: Size of the transfer
1042 * @val: The data being transferred
1044 static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
1047 unsigned long flags;
1048 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
1050 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
1051 where + size <= PCI_CAPABILITY_LIST) {
1052 /* SSIDs and ROM BARs are read-only */
1053 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
1054 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
1055 /* Choose the function to be written. (See comment above) */
1056 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
1057 /* Make sure the function was chosen before we start writing. */
1059 /* Write to that function's config space. */
1072 * Make sure the write was done before we release the spinlock
1073 * allowing consecutive reads/writes.
1076 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
1078 dev_err(&hpdev->hbus->hdev->device,
1079 "Attempt to write beyond a function's config space.\n");
1084 * hv_pcifront_read_config() - Read configuration space
1085 * @bus: PCI Bus structure
1086 * @devfn: Device/function
1087 * @where: Offset from base
1088 * @size: Byte/word/dword
1089 * @val: Value to be read
1091 * Return: PCIBIOS_SUCCESSFUL on success
1092 * PCIBIOS_DEVICE_NOT_FOUND on failure
1094 static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
1095 int where, int size, u32 *val)
1097 struct hv_pcibus_device *hbus =
1098 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
1099 struct hv_pci_dev *hpdev;
1101 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
1103 return PCIBIOS_DEVICE_NOT_FOUND;
1105 _hv_pcifront_read_config(hpdev, where, size, val);
1107 put_pcichild(hpdev);
1108 return PCIBIOS_SUCCESSFUL;
1112 * hv_pcifront_write_config() - Write configuration space
1113 * @bus: PCI Bus structure
1114 * @devfn: Device/function
1115 * @where: Offset from base
1116 * @size: Byte/word/dword
1117 * @val: Value to be written to device
1119 * Return: PCIBIOS_SUCCESSFUL on success
1120 * PCIBIOS_DEVICE_NOT_FOUND on failure
1122 static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
1123 int where, int size, u32 val)
1125 struct hv_pcibus_device *hbus =
1126 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
1127 struct hv_pci_dev *hpdev;
1129 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
1131 return PCIBIOS_DEVICE_NOT_FOUND;
1133 _hv_pcifront_write_config(hpdev, where, size, val);
1135 put_pcichild(hpdev);
1136 return PCIBIOS_SUCCESSFUL;
1139 /* PCIe operations */
1140 static struct pci_ops hv_pcifront_ops = {
1141 .read = hv_pcifront_read_config,
1142 .write = hv_pcifront_write_config,
1146 * Paravirtual backchannel
1148 * Hyper-V SR-IOV provides a backchannel mechanism in software for
1149 * communication between a VF driver and a PF driver. These
1150 * "configuration blocks" are similar in concept to PCI configuration space,
1151 * but instead of doing reads and writes in 32-bit chunks through a very slow
1152 * path, packets of up to 128 bytes can be sent or received asynchronously.
1154 * Nearly every SR-IOV device contains just such a communications channel in
1155 * hardware, so using this one in software is usually optional. Using the
1156 * software channel, however, allows driver implementers to leverage software
1157 * tools that fuzz the communications channel looking for vulnerabilities.
1159 * The usage model for these packets puts the responsibility for reading or
1160 * writing on the VF driver. The VF driver sends a read or a write packet,
1161 * indicating which "block" is being referred to by number.
1163 * If the PF driver wishes to initiate communication, it can "invalidate" one or
1164 * more of the first 64 blocks. This invalidation is delivered via a callback
1165 * supplied by the VF driver by this driver.
1167 * No protocol is implied, except that supplied by the PF and VF drivers.
1170 struct hv_read_config_compl {
1171 struct hv_pci_compl comp_pkt;
1174 unsigned int bytes_returned;
1178 * hv_pci_read_config_compl() - Invoked when a response packet
1179 * for a read config block operation arrives.
1180 * @context: Identifies the read config operation
1181 * @resp: The response packet itself
1182 * @resp_packet_size: Size in bytes of the response packet
1184 static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
1185 int resp_packet_size)
1187 struct hv_read_config_compl *comp = context;
1188 struct pci_read_block_response *read_resp =
1189 (struct pci_read_block_response *)resp;
1190 unsigned int data_len, hdr_len;
1192 hdr_len = offsetof(struct pci_read_block_response, bytes);
1193 if (resp_packet_size < hdr_len) {
1194 comp->comp_pkt.completion_status = -1;
1198 data_len = resp_packet_size - hdr_len;
1199 if (data_len > 0 && read_resp->status == 0) {
1200 comp->bytes_returned = min(comp->len, data_len);
1201 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
1203 comp->bytes_returned = 0;
1206 comp->comp_pkt.completion_status = read_resp->status;
1208 complete(&comp->comp_pkt.host_event);
1212 * hv_read_config_block() - Sends a read config block request to
1213 * the back-end driver running in the Hyper-V parent partition.
1214 * @pdev: The PCI driver's representation for this device.
1215 * @buf: Buffer into which the config block will be copied.
1216 * @len: Size in bytes of buf.
1217 * @block_id: Identifies the config block which has been requested.
1218 * @bytes_returned: Size which came back from the back-end driver.
1220 * Return: 0 on success, -errno on failure
1222 static int hv_read_config_block(struct pci_dev *pdev, void *buf,
1223 unsigned int len, unsigned int block_id,
1224 unsigned int *bytes_returned)
1226 struct hv_pcibus_device *hbus =
1227 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1230 struct pci_packet pkt;
1231 char buf[sizeof(struct pci_read_block)];
1233 struct hv_read_config_compl comp_pkt;
1234 struct pci_read_block *read_blk;
1237 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1240 init_completion(&comp_pkt.comp_pkt.host_event);
1244 memset(&pkt, 0, sizeof(pkt));
1245 pkt.pkt.completion_func = hv_pci_read_config_compl;
1246 pkt.pkt.compl_ctxt = &comp_pkt;
1247 read_blk = (struct pci_read_block *)&pkt.pkt.message;
1248 read_blk->message_type.type = PCI_READ_BLOCK;
1249 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1250 read_blk->block_id = block_id;
1251 read_blk->bytes_requested = len;
1253 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
1254 sizeof(*read_blk), (unsigned long)&pkt.pkt,
1256 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1260 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
1264 if (comp_pkt.comp_pkt.completion_status != 0 ||
1265 comp_pkt.bytes_returned == 0) {
1266 dev_err(&hbus->hdev->device,
1267 "Read Config Block failed: 0x%x, bytes_returned=%d\n",
1268 comp_pkt.comp_pkt.completion_status,
1269 comp_pkt.bytes_returned);
1273 *bytes_returned = comp_pkt.bytes_returned;
1278 * hv_pci_write_config_compl() - Invoked when a response packet for a write
1279 * config block operation arrives.
1280 * @context: Identifies the write config operation
1281 * @resp: The response packet itself
1282 * @resp_packet_size: Size in bytes of the response packet
1284 static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
1285 int resp_packet_size)
1287 struct hv_pci_compl *comp_pkt = context;
1289 comp_pkt->completion_status = resp->status;
1290 complete(&comp_pkt->host_event);
1294 * hv_write_config_block() - Sends a write config block request to the
1295 * back-end driver running in the Hyper-V parent partition.
1296 * @pdev: The PCI driver's representation for this device.
1297 * @buf: Buffer from which the config block will be copied.
1298 * @len: Size in bytes of buf.
1299 * @block_id: Identifies the config block which is being written.
1301 * Return: 0 on success, -errno on failure
1303 static int hv_write_config_block(struct pci_dev *pdev, void *buf,
1304 unsigned int len, unsigned int block_id)
1306 struct hv_pcibus_device *hbus =
1307 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1310 struct pci_packet pkt;
1311 char buf[sizeof(struct pci_write_block)];
1314 struct hv_pci_compl comp_pkt;
1315 struct pci_write_block *write_blk;
1319 if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
1322 init_completion(&comp_pkt.host_event);
1324 memset(&pkt, 0, sizeof(pkt));
1325 pkt.pkt.completion_func = hv_pci_write_config_compl;
1326 pkt.pkt.compl_ctxt = &comp_pkt;
1327 write_blk = (struct pci_write_block *)&pkt.pkt.message;
1328 write_blk->message_type.type = PCI_WRITE_BLOCK;
1329 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
1330 write_blk->block_id = block_id;
1331 write_blk->byte_count = len;
1332 memcpy(write_blk->bytes, buf, len);
1333 pkt_size = offsetof(struct pci_write_block, bytes) + len;
1335 * This quirk is required on some hosts shipped around 2018, because
1336 * these hosts don't check the pkt_size correctly (new hosts have been
1337 * fixed since early 2019). The quirk is also safe on very old hosts
1338 * and new hosts, because, on them, what really matters is the length
1339 * specified in write_blk->byte_count.
1341 pkt_size += sizeof(pkt.reserved);
1343 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
1344 (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
1345 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1349 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
1353 if (comp_pkt.completion_status != 0) {
1354 dev_err(&hbus->hdev->device,
1355 "Write Config Block failed: 0x%x\n",
1356 comp_pkt.completion_status);
1364 * hv_register_block_invalidate() - Invoked when a config block invalidation
1365 * arrives from the back-end driver.
1366 * @pdev: The PCI driver's representation for this device.
1367 * @context: Identifies the device.
1368 * @block_invalidate: Identifies all of the blocks being invalidated.
1370 * Return: 0 on success, -errno on failure
1372 static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
1373 void (*block_invalidate)(void *context,
1376 struct hv_pcibus_device *hbus =
1377 container_of(pdev->bus->sysdata, struct hv_pcibus_device,
1379 struct hv_pci_dev *hpdev;
1381 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1385 hpdev->block_invalidate = block_invalidate;
1386 hpdev->invalidate_context = context;
1388 put_pcichild(hpdev);
1393 /* Interrupt management hooks */
1394 static void hv_int_desc_free(struct hv_pci_dev *hpdev,
1395 struct tran_int_desc *int_desc)
1397 struct pci_delete_interrupt *int_pkt;
1399 struct pci_packet pkt;
1400 u8 buffer[sizeof(struct pci_delete_interrupt)];
1403 memset(&ctxt, 0, sizeof(ctxt));
1404 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
1405 int_pkt->message_type.type =
1406 PCI_DELETE_INTERRUPT_MESSAGE;
1407 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1408 int_pkt->int_desc = *int_desc;
1409 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
1410 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
1415 * hv_msi_free() - Free the MSI.
1416 * @domain: The interrupt domain pointer
1417 * @info: Extra MSI-related context
1418 * @irq: Identifies the IRQ.
1420 * The Hyper-V parent partition and hypervisor are tracking the
1421 * messages that are in use, keeping the interrupt redirection
1422 * table up to date. This callback sends a message that frees
1423 * the IRT entry and related tracking nonsense.
1425 static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
1428 struct hv_pcibus_device *hbus;
1429 struct hv_pci_dev *hpdev;
1430 struct pci_dev *pdev;
1431 struct tran_int_desc *int_desc;
1432 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
1433 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
1435 pdev = msi_desc_to_pci_dev(msi);
1437 int_desc = irq_data_get_irq_chip_data(irq_data);
1441 irq_data->chip_data = NULL;
1442 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1448 hv_int_desc_free(hpdev, int_desc);
1449 put_pcichild(hpdev);
1452 static void hv_irq_mask(struct irq_data *data)
1454 pci_msi_mask_irq(data);
1455 if (data->parent_data->chip->irq_mask)
1456 irq_chip_mask_parent(data);
1460 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
1462 * @data: Describes the IRQ
1464 * Build new a destination for the MSI and make a hypercall to
1465 * update the Interrupt Redirection Table. "Device Logical ID"
1466 * is built out of this PCI bus's instance GUID and the function
1467 * number of the device.
1469 static void hv_irq_unmask(struct irq_data *data)
1471 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
1472 struct hv_retarget_device_interrupt *params;
1473 struct hv_pcibus_device *hbus;
1474 struct cpumask *dest;
1476 struct pci_bus *pbus;
1477 struct pci_dev *pdev;
1478 unsigned long flags;
1483 dest = irq_data_get_effective_affinity_mask(data);
1484 pdev = msi_desc_to_pci_dev(msi_desc);
1486 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1488 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
1490 params = &hbus->retarget_msi_interrupt_params;
1491 memset(params, 0, sizeof(*params));
1492 params->partition_id = HV_PARTITION_ID_SELF;
1493 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
1494 hv_set_msi_entry_from_desc(¶ms->int_entry.msi_entry, msi_desc);
1495 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
1496 (hbus->hdev->dev_instance.b[4] << 16) |
1497 (hbus->hdev->dev_instance.b[7] << 8) |
1498 (hbus->hdev->dev_instance.b[6] & 0xf8) |
1499 PCI_FUNC(pdev->devfn);
1500 params->int_target.vector = hv_msi_get_int_vector(data);
1503 * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by
1504 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
1505 * spurious interrupt storm. Not doing so does not seem to have a
1506 * negative effect (yet?).
1509 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
1511 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
1512 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
1513 * with >64 VP support.
1514 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
1515 * is not sufficient for this hypercall.
1517 params->int_target.flags |=
1518 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
1520 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
1525 cpumask_and(tmp, dest, cpu_online_mask);
1526 nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp);
1527 free_cpumask_var(tmp);
1535 * var-sized hypercall, var-size starts after vp_mask (thus
1536 * vp_set.format does not count, but vp_set.valid_bank_mask
1539 var_size = 1 + nr_bank;
1541 for_each_cpu_and(cpu, dest, cpu_online_mask) {
1542 params->int_target.vp_mask |=
1543 (1ULL << hv_cpu_number_to_vp_number(cpu));
1547 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
1551 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
1554 * During hibernation, when a CPU is offlined, the kernel tries
1555 * to move the interrupt to the remaining CPUs that haven't
1556 * been offlined yet. In this case, the below hv_do_hypercall()
1557 * always fails since the vmbus channel has been closed:
1558 * refer to cpu_disable_common() -> fixup_irqs() ->
1559 * irq_migrate_all_off_this_cpu() -> migrate_one_irq().
1561 * Suppress the error message for hibernation because the failure
1562 * during hibernation does not matter (at this time all the devices
1563 * have been frozen). Note: the correct affinity info is still updated
1564 * into the irqdata data structure in migrate_one_irq() ->
1565 * irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
1566 * resumes, hv_pci_restore_msi_state() is able to correctly restore
1567 * the interrupt with the correct affinity.
1569 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
1570 dev_err(&hbus->hdev->device,
1571 "%s() failed: %#llx", __func__, res);
1573 if (data->parent_data->chip->irq_unmask)
1574 irq_chip_unmask_parent(data);
1575 pci_msi_unmask_irq(data);
1578 struct compose_comp_ctxt {
1579 struct hv_pci_compl comp_pkt;
1580 struct tran_int_desc int_desc;
1583 static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1584 int resp_packet_size)
1586 struct compose_comp_ctxt *comp_pkt = context;
1587 struct pci_create_int_response *int_resp =
1588 (struct pci_create_int_response *)resp;
1590 comp_pkt->comp_pkt.completion_status = resp->status;
1591 comp_pkt->int_desc = int_resp->int_desc;
1592 complete(&comp_pkt->comp_pkt.host_event);
1595 static u32 hv_compose_msi_req_v1(
1596 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1597 u32 slot, u8 vector)
1599 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1600 int_pkt->wslot.slot = slot;
1601 int_pkt->int_desc.vector = vector;
1602 int_pkt->int_desc.vector_count = 1;
1603 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
1606 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1609 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1611 return sizeof(*int_pkt);
1615 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1616 * by subsequent retarget in hv_irq_unmask().
1618 static int hv_compose_msi_req_get_cpu(struct cpumask *affinity)
1620 return cpumask_first_and(affinity, cpu_online_mask);
1623 static u32 hv_compose_msi_req_v2(
1624 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1625 u32 slot, u8 vector)
1629 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1630 int_pkt->wslot.slot = slot;
1631 int_pkt->int_desc.vector = vector;
1632 int_pkt->int_desc.vector_count = 1;
1633 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
1634 cpu = hv_compose_msi_req_get_cpu(affinity);
1635 int_pkt->int_desc.processor_array[0] =
1636 hv_cpu_number_to_vp_number(cpu);
1637 int_pkt->int_desc.processor_count = 1;
1639 return sizeof(*int_pkt);
1642 static u32 hv_compose_msi_req_v3(
1643 struct pci_create_interrupt3 *int_pkt, struct cpumask *affinity,
1644 u32 slot, u32 vector)
1648 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
1649 int_pkt->wslot.slot = slot;
1650 int_pkt->int_desc.vector = vector;
1651 int_pkt->int_desc.reserved = 0;
1652 int_pkt->int_desc.vector_count = 1;
1653 int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
1654 cpu = hv_compose_msi_req_get_cpu(affinity);
1655 int_pkt->int_desc.processor_array[0] =
1656 hv_cpu_number_to_vp_number(cpu);
1657 int_pkt->int_desc.processor_count = 1;
1659 return sizeof(*int_pkt);
1663 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1664 * @data: Everything about this MSI
1665 * @msg: Buffer that is filled in by this function
1667 * This function unpacks the IRQ looking for target CPU set, IDT
1668 * vector and mode and sends a message to the parent partition
1669 * asking for a mapping for that tuple in this partition. The
1670 * response supplies a data value and address to which that data
1671 * should be written to trigger that interrupt.
1673 static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1675 struct hv_pcibus_device *hbus;
1676 struct vmbus_channel *channel;
1677 struct hv_pci_dev *hpdev;
1678 struct pci_bus *pbus;
1679 struct pci_dev *pdev;
1680 struct cpumask *dest;
1681 struct compose_comp_ctxt comp;
1682 struct tran_int_desc *int_desc;
1684 struct pci_packet pci_pkt;
1686 struct pci_create_interrupt v1;
1687 struct pci_create_interrupt2 v2;
1688 struct pci_create_interrupt3 v3;
1695 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
1696 dest = irq_data_get_effective_affinity_mask(data);
1698 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1699 channel = hbus->hdev->channel;
1700 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1702 goto return_null_message;
1704 /* Free any previous message that might have already been composed. */
1705 if (data->chip_data) {
1706 int_desc = data->chip_data;
1707 data->chip_data = NULL;
1708 hv_int_desc_free(hpdev, int_desc);
1711 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
1713 goto drop_reference;
1715 memset(&ctxt, 0, sizeof(ctxt));
1716 init_completion(&comp.comp_pkt.host_event);
1717 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1718 ctxt.pci_pkt.compl_ctxt = ∁
1720 switch (hbus->protocol_version) {
1721 case PCI_PROTOCOL_VERSION_1_1:
1722 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
1724 hpdev->desc.win_slot.slot,
1725 hv_msi_get_int_vector(data));
1728 case PCI_PROTOCOL_VERSION_1_2:
1729 case PCI_PROTOCOL_VERSION_1_3:
1730 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
1732 hpdev->desc.win_slot.slot,
1733 hv_msi_get_int_vector(data));
1736 case PCI_PROTOCOL_VERSION_1_4:
1737 size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
1739 hpdev->desc.win_slot.slot,
1740 hv_msi_get_int_vector(data));
1744 /* As we only negotiate protocol versions known to this driver,
1745 * this path should never hit. However, this is it not a hot
1746 * path so we print a message to aid future updates.
1748 dev_err(&hbus->hdev->device,
1749 "Unexpected vPCI protocol, update driver.");
1753 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1754 size, (unsigned long)&ctxt.pci_pkt,
1756 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1758 dev_err(&hbus->hdev->device,
1759 "Sending request for interrupt failed: 0x%x",
1760 comp.comp_pkt.completion_status);
1765 * Prevents hv_pci_onchannelcallback() from running concurrently
1768 tasklet_disable_in_atomic(&channel->callback_event);
1771 * Since this function is called with IRQ locks held, can't
1772 * do normal wait for completion; instead poll.
1774 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1775 unsigned long flags;
1777 /* 0xFFFF means an invalid PCI VENDOR ID. */
1778 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1779 dev_err_once(&hbus->hdev->device,
1780 "the device has gone\n");
1781 goto enable_tasklet;
1785 * Make sure that the ring buffer data structure doesn't get
1786 * freed while we dereference the ring buffer pointer. Test
1787 * for the channel's onchannel_callback being NULL within a
1788 * sched_lock critical section. See also the inline comments
1789 * in vmbus_reset_channel_cb().
1791 spin_lock_irqsave(&channel->sched_lock, flags);
1792 if (unlikely(channel->onchannel_callback == NULL)) {
1793 spin_unlock_irqrestore(&channel->sched_lock, flags);
1794 goto enable_tasklet;
1796 hv_pci_onchannelcallback(hbus);
1797 spin_unlock_irqrestore(&channel->sched_lock, flags);
1799 if (hpdev->state == hv_pcichild_ejecting) {
1800 dev_err_once(&hbus->hdev->device,
1801 "the device is being ejected\n");
1802 goto enable_tasklet;
1808 tasklet_enable(&channel->callback_event);
1810 if (comp.comp_pkt.completion_status < 0) {
1811 dev_err(&hbus->hdev->device,
1812 "Request for interrupt failed: 0x%x",
1813 comp.comp_pkt.completion_status);
1818 * Record the assignment so that this can be unwound later. Using
1819 * irq_set_chip_data() here would be appropriate, but the lock it takes
1822 *int_desc = comp.int_desc;
1823 data->chip_data = int_desc;
1825 /* Pass up the result. */
1826 msg->address_hi = comp.int_desc.address >> 32;
1827 msg->address_lo = comp.int_desc.address & 0xffffffff;
1828 msg->data = comp.int_desc.data;
1830 put_pcichild(hpdev);
1834 tasklet_enable(&channel->callback_event);
1838 put_pcichild(hpdev);
1839 return_null_message:
1840 msg->address_hi = 0;
1841 msg->address_lo = 0;
1845 /* HW Interrupt Chip Descriptor */
1846 static struct irq_chip hv_msi_irq_chip = {
1847 .name = "Hyper-V PCIe MSI",
1848 .irq_compose_msi_msg = hv_compose_msi_msg,
1849 .irq_set_affinity = irq_chip_set_affinity_parent,
1851 .irq_ack = irq_chip_ack_parent,
1852 #elif defined(CONFIG_ARM64)
1853 .irq_eoi = irq_chip_eoi_parent,
1855 .irq_mask = hv_irq_mask,
1856 .irq_unmask = hv_irq_unmask,
1859 static struct msi_domain_ops hv_msi_ops = {
1860 .msi_prepare = hv_msi_prepare,
1861 .msi_free = hv_msi_free,
1865 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1866 * @hbus: The root PCI bus
1868 * This function creates an IRQ domain which will be used for
1869 * interrupts from devices that have been passed through. These
1870 * devices only support MSI and MSI-X, not line-based interrupts
1871 * or simulations of line-based interrupts through PCIe's
1872 * fabric-layer messages. Because interrupts are remapped, we
1873 * can support multi-message MSI here.
1875 * Return: '0' on success and error value on failure
1877 static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1879 hbus->msi_info.chip = &hv_msi_irq_chip;
1880 hbus->msi_info.ops = &hv_msi_ops;
1881 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1882 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1884 hbus->msi_info.handler = FLOW_HANDLER;
1885 hbus->msi_info.handler_name = FLOW_NAME;
1886 hbus->msi_info.data = hbus;
1887 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode,
1889 hv_pci_get_root_domain());
1890 if (!hbus->irq_domain) {
1891 dev_err(&hbus->hdev->device,
1892 "Failed to build an MSI IRQ domain\n");
1896 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
1902 * get_bar_size() - Get the address space consumed by a BAR
1903 * @bar_val: Value that a BAR returned after -1 was written
1906 * This function returns the size of the BAR, rounded up to 1
1907 * page. It has to be rounded up because the hypervisor's page
1908 * table entry that maps the BAR into the VM can't specify an
1909 * offset within a page. The invariant is that the hypervisor
1910 * must place any BARs of smaller than page length at the
1911 * beginning of a page.
1913 * Return: Size in bytes of the consumed MMIO space.
1915 static u64 get_bar_size(u64 bar_val)
1917 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1922 * survey_child_resources() - Total all MMIO requirements
1923 * @hbus: Root PCI bus, as understood by this driver
1925 static void survey_child_resources(struct hv_pcibus_device *hbus)
1927 struct hv_pci_dev *hpdev;
1928 resource_size_t bar_size = 0;
1929 unsigned long flags;
1930 struct completion *event;
1934 /* If nobody is waiting on the answer, don't compute it. */
1935 event = xchg(&hbus->survey_event, NULL);
1939 /* If the answer has already been computed, go with it. */
1940 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1945 spin_lock_irqsave(&hbus->device_list_lock, flags);
1948 * Due to an interesting quirk of the PCI spec, all memory regions
1949 * for a child device are a power of 2 in size and aligned in memory,
1950 * so it's sufficient to just add them up without tracking alignment.
1952 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1953 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1954 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1955 dev_err(&hbus->hdev->device,
1956 "There's an I/O BAR in this list!\n");
1958 if (hpdev->probed_bar[i] != 0) {
1960 * A probed BAR has all the upper bits set that
1964 bar_val = hpdev->probed_bar[i];
1965 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1967 ((u64)hpdev->probed_bar[++i] << 32);
1969 bar_val |= 0xffffffff00000000ULL;
1971 bar_size = get_bar_size(bar_val);
1973 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1974 hbus->high_mmio_space += bar_size;
1976 hbus->low_mmio_space += bar_size;
1981 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1986 * prepopulate_bars() - Fill in BARs with defaults
1987 * @hbus: Root PCI bus, as understood by this driver
1989 * The core PCI driver code seems much, much happier if the BARs
1990 * for a device have values upon first scan. So fill them in.
1991 * The algorithm below works down from large sizes to small,
1992 * attempting to pack the assignments optimally. The assumption,
1993 * enforced in other parts of the code, is that the beginning of
1994 * the memory-mapped I/O space will be aligned on the largest
1997 static void prepopulate_bars(struct hv_pcibus_device *hbus)
1999 resource_size_t high_size = 0;
2000 resource_size_t low_size = 0;
2001 resource_size_t high_base = 0;
2002 resource_size_t low_base = 0;
2003 resource_size_t bar_size;
2004 struct hv_pci_dev *hpdev;
2005 unsigned long flags;
2011 if (hbus->low_mmio_space) {
2012 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2013 low_base = hbus->low_mmio_res->start;
2016 if (hbus->high_mmio_space) {
2018 (63 - __builtin_clzll(hbus->high_mmio_space));
2019 high_base = hbus->high_mmio_res->start;
2022 spin_lock_irqsave(&hbus->device_list_lock, flags);
2025 * Clear the memory enable bit, in case it's already set. This occurs
2026 * in the suspend path of hibernation, where the device is suspended,
2027 * resumed and suspended again: see hibernation_snapshot() and
2028 * hibernation_platform_enter().
2030 * If the memory enable bit is already set, Hyper-V silently ignores
2031 * the below BAR updates, and the related PCI device driver can not
2032 * work, because reading from the device register(s) always returns
2033 * 0xFFFFFFFF (PCI_ERROR_RESPONSE).
2035 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2036 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
2037 command &= ~PCI_COMMAND_MEMORY;
2038 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
2041 /* Pick addresses for the BARs. */
2043 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2044 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
2045 bar_val = hpdev->probed_bar[i];
2048 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
2051 ((u64)hpdev->probed_bar[i + 1]
2054 bar_val |= 0xffffffffULL << 32;
2056 bar_size = get_bar_size(bar_val);
2058 if (high_size != bar_size) {
2062 _hv_pcifront_write_config(hpdev,
2063 PCI_BASE_ADDRESS_0 + (4 * i),
2065 (u32)(high_base & 0xffffff00));
2067 _hv_pcifront_write_config(hpdev,
2068 PCI_BASE_ADDRESS_0 + (4 * i),
2069 4, (u32)(high_base >> 32));
2070 high_base += bar_size;
2072 if (low_size != bar_size)
2074 _hv_pcifront_write_config(hpdev,
2075 PCI_BASE_ADDRESS_0 + (4 * i),
2077 (u32)(low_base & 0xffffff00));
2078 low_base += bar_size;
2081 if (high_size <= 1 && low_size <= 1) {
2082 /* Set the memory enable bit. */
2083 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
2085 command |= PCI_COMMAND_MEMORY;
2086 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
2094 } while (high_size || low_size);
2096 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2100 * Assign entries in sysfs pci slot directory.
2102 * Note that this function does not need to lock the children list
2103 * because it is called from pci_devices_present_work which
2104 * is serialized with hv_eject_device_work because they are on the
2105 * same ordered workqueue. Therefore hbus->children list will not change
2106 * even when pci_create_slot sleeps.
2108 static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
2110 struct hv_pci_dev *hpdev;
2111 char name[SLOT_NAME_SIZE];
2114 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2115 if (hpdev->pci_slot)
2118 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
2119 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
2120 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
2122 if (IS_ERR(hpdev->pci_slot)) {
2123 pr_warn("pci_create slot %s failed\n", name);
2124 hpdev->pci_slot = NULL;
2130 * Remove entries in sysfs pci slot directory.
2132 static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
2134 struct hv_pci_dev *hpdev;
2136 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2137 if (!hpdev->pci_slot)
2139 pci_destroy_slot(hpdev->pci_slot);
2140 hpdev->pci_slot = NULL;
2145 * Set NUMA node for the devices on the bus
2147 static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
2149 struct pci_dev *dev;
2150 struct pci_bus *bus = hbus->bridge->bus;
2151 struct hv_pci_dev *hv_dev;
2153 list_for_each_entry(dev, &bus->devices, bus_list) {
2154 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
2158 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY)
2159 set_dev_node(&dev->dev, hv_dev->desc.virtual_numa_node);
2161 put_pcichild(hv_dev);
2166 * create_root_hv_pci_bus() - Expose a new root PCI bus
2167 * @hbus: Root PCI bus, as understood by this driver
2169 * Return: 0 on success, -errno on failure
2171 static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
2174 struct pci_host_bridge *bridge = hbus->bridge;
2176 bridge->dev.parent = &hbus->hdev->device;
2177 bridge->sysdata = &hbus->sysdata;
2178 bridge->ops = &hv_pcifront_ops;
2180 error = pci_scan_root_bus_bridge(bridge);
2184 pci_lock_rescan_remove();
2185 hv_pci_assign_numa_node(hbus);
2186 pci_bus_assign_resources(bridge->bus);
2187 hv_pci_assign_slots(hbus);
2188 pci_bus_add_devices(bridge->bus);
2189 pci_unlock_rescan_remove();
2190 hbus->state = hv_pcibus_installed;
2194 struct q_res_req_compl {
2195 struct completion host_event;
2196 struct hv_pci_dev *hpdev;
2200 * q_resource_requirements() - Query Resource Requirements
2201 * @context: The completion context.
2202 * @resp: The response that came from the host.
2203 * @resp_packet_size: The size in bytes of resp.
2205 * This function is invoked on completion of a Query Resource
2206 * Requirements packet.
2208 static void q_resource_requirements(void *context, struct pci_response *resp,
2209 int resp_packet_size)
2211 struct q_res_req_compl *completion = context;
2212 struct pci_q_res_req_response *q_res_req =
2213 (struct pci_q_res_req_response *)resp;
2216 if (resp->status < 0) {
2217 dev_err(&completion->hpdev->hbus->hdev->device,
2218 "query resource requirements failed: %x\n",
2221 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
2222 completion->hpdev->probed_bar[i] =
2223 q_res_req->probed_bar[i];
2227 complete(&completion->host_event);
2231 * new_pcichild_device() - Create a new child device
2232 * @hbus: The internal struct tracking this root PCI bus.
2233 * @desc: The information supplied so far from the host
2236 * This function creates the tracking structure for a new child
2237 * device and kicks off the process of figuring out what it is.
2239 * Return: Pointer to the new tracking struct
2241 static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
2242 struct hv_pcidev_description *desc)
2244 struct hv_pci_dev *hpdev;
2245 struct pci_child_message *res_req;
2246 struct q_res_req_compl comp_pkt;
2248 struct pci_packet init_packet;
2249 u8 buffer[sizeof(struct pci_child_message)];
2251 unsigned long flags;
2254 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
2260 memset(&pkt, 0, sizeof(pkt));
2261 init_completion(&comp_pkt.host_event);
2262 comp_pkt.hpdev = hpdev;
2263 pkt.init_packet.compl_ctxt = &comp_pkt;
2264 pkt.init_packet.completion_func = q_resource_requirements;
2265 res_req = (struct pci_child_message *)&pkt.init_packet.message;
2266 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
2267 res_req->wslot.slot = desc->win_slot.slot;
2269 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
2270 sizeof(struct pci_child_message),
2271 (unsigned long)&pkt.init_packet,
2273 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2277 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
2280 hpdev->desc = *desc;
2281 refcount_set(&hpdev->refs, 1);
2282 get_pcichild(hpdev);
2283 spin_lock_irqsave(&hbus->device_list_lock, flags);
2285 list_add_tail(&hpdev->list_entry, &hbus->children);
2286 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2295 * get_pcichild_wslot() - Find device from slot
2296 * @hbus: Root PCI bus, as understood by this driver
2297 * @wslot: Location on the bus
2299 * This function looks up a PCI device and returns the internal
2300 * representation of it. It acquires a reference on it, so that
2301 * the device won't be deleted while somebody is using it. The
2302 * caller is responsible for calling put_pcichild() to release
2305 * Return: Internal representation of a PCI device
2307 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
2310 unsigned long flags;
2311 struct hv_pci_dev *iter, *hpdev = NULL;
2313 spin_lock_irqsave(&hbus->device_list_lock, flags);
2314 list_for_each_entry(iter, &hbus->children, list_entry) {
2315 if (iter->desc.win_slot.slot == wslot) {
2317 get_pcichild(hpdev);
2321 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2327 * pci_devices_present_work() - Handle new list of child devices
2328 * @work: Work struct embedded in struct hv_dr_work
2330 * "Bus Relations" is the Windows term for "children of this
2331 * bus." The terminology is preserved here for people trying to
2332 * debug the interaction between Hyper-V and Linux. This
2333 * function is called when the parent partition reports a list
2334 * of functions that should be observed under this PCI Express
2337 * This function updates the list, and must tolerate being
2338 * called multiple times with the same information. The typical
2339 * number of child devices is one, with very atypical cases
2340 * involving three or four, so the algorithms used here can be
2341 * simple and inefficient.
2343 * It must also treat the omission of a previously observed device as
2344 * notification that the device no longer exists.
2346 * Note that this function is serialized with hv_eject_device_work(),
2347 * because both are pushed to the ordered workqueue hbus->wq.
2349 static void pci_devices_present_work(struct work_struct *work)
2353 struct hv_pcidev_description *new_desc;
2354 struct hv_pci_dev *hpdev;
2355 struct hv_pcibus_device *hbus;
2356 struct list_head removed;
2357 struct hv_dr_work *dr_wrk;
2358 struct hv_dr_state *dr = NULL;
2359 unsigned long flags;
2361 dr_wrk = container_of(work, struct hv_dr_work, wrk);
2365 INIT_LIST_HEAD(&removed);
2367 /* Pull this off the queue and process it if it was the last one. */
2368 spin_lock_irqsave(&hbus->device_list_lock, flags);
2369 while (!list_empty(&hbus->dr_list)) {
2370 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
2372 list_del(&dr->list_entry);
2374 /* Throw this away if the list still has stuff in it. */
2375 if (!list_empty(&hbus->dr_list)) {
2380 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2385 /* First, mark all existing children as reported missing. */
2386 spin_lock_irqsave(&hbus->device_list_lock, flags);
2387 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2388 hpdev->reported_missing = true;
2390 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2392 /* Next, add back any reported devices. */
2393 for (child_no = 0; child_no < dr->device_count; child_no++) {
2395 new_desc = &dr->func[child_no];
2397 spin_lock_irqsave(&hbus->device_list_lock, flags);
2398 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2399 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
2400 (hpdev->desc.v_id == new_desc->v_id) &&
2401 (hpdev->desc.d_id == new_desc->d_id) &&
2402 (hpdev->desc.ser == new_desc->ser)) {
2403 hpdev->reported_missing = false;
2407 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2410 hpdev = new_pcichild_device(hbus, new_desc);
2412 dev_err(&hbus->hdev->device,
2413 "couldn't record a child device.\n");
2417 /* Move missing children to a list on the stack. */
2418 spin_lock_irqsave(&hbus->device_list_lock, flags);
2421 list_for_each_entry(hpdev, &hbus->children, list_entry) {
2422 if (hpdev->reported_missing) {
2424 put_pcichild(hpdev);
2425 list_move_tail(&hpdev->list_entry, &removed);
2430 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2432 /* Delete everything that should no longer exist. */
2433 while (!list_empty(&removed)) {
2434 hpdev = list_first_entry(&removed, struct hv_pci_dev,
2436 list_del(&hpdev->list_entry);
2438 if (hpdev->pci_slot)
2439 pci_destroy_slot(hpdev->pci_slot);
2441 put_pcichild(hpdev);
2444 switch (hbus->state) {
2445 case hv_pcibus_installed:
2447 * Tell the core to rescan bus
2448 * because there may have been changes.
2450 pci_lock_rescan_remove();
2451 pci_scan_child_bus(hbus->bridge->bus);
2452 hv_pci_assign_numa_node(hbus);
2453 hv_pci_assign_slots(hbus);
2454 pci_unlock_rescan_remove();
2457 case hv_pcibus_init:
2458 case hv_pcibus_probed:
2459 survey_child_resources(hbus);
2470 * hv_pci_start_relations_work() - Queue work to start device discovery
2471 * @hbus: Root PCI bus, as understood by this driver
2472 * @dr: The list of children returned from host
2474 * Return: 0 on success, -errno on failure
2476 static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
2477 struct hv_dr_state *dr)
2479 struct hv_dr_work *dr_wrk;
2480 unsigned long flags;
2483 if (hbus->state == hv_pcibus_removing) {
2484 dev_info(&hbus->hdev->device,
2485 "PCI VMBus BUS_RELATIONS: ignored\n");
2489 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
2493 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
2496 spin_lock_irqsave(&hbus->device_list_lock, flags);
2498 * If pending_dr is true, we have already queued a work,
2499 * which will see the new dr. Otherwise, we need to
2502 pending_dr = !list_empty(&hbus->dr_list);
2503 list_add_tail(&dr->list_entry, &hbus->dr_list);
2504 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2509 queue_work(hbus->wq, &dr_wrk->wrk);
2515 * hv_pci_devices_present() - Handle list of new children
2516 * @hbus: Root PCI bus, as understood by this driver
2517 * @relations: Packet from host listing children
2519 * Process a new list of devices on the bus. The list of devices is
2520 * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
2521 * whenever a new list of devices for this bus appears.
2523 static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
2524 struct pci_bus_relations *relations)
2526 struct hv_dr_state *dr;
2529 dr = kzalloc(struct_size(dr, func, relations->device_count),
2534 dr->device_count = relations->device_count;
2535 for (i = 0; i < dr->device_count; i++) {
2536 dr->func[i].v_id = relations->func[i].v_id;
2537 dr->func[i].d_id = relations->func[i].d_id;
2538 dr->func[i].rev = relations->func[i].rev;
2539 dr->func[i].prog_intf = relations->func[i].prog_intf;
2540 dr->func[i].subclass = relations->func[i].subclass;
2541 dr->func[i].base_class = relations->func[i].base_class;
2542 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2543 dr->func[i].win_slot = relations->func[i].win_slot;
2544 dr->func[i].ser = relations->func[i].ser;
2547 if (hv_pci_start_relations_work(hbus, dr))
2552 * hv_pci_devices_present2() - Handle list of new children
2553 * @hbus: Root PCI bus, as understood by this driver
2554 * @relations: Packet from host listing children
2556 * This function is the v2 version of hv_pci_devices_present()
2558 static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
2559 struct pci_bus_relations2 *relations)
2561 struct hv_dr_state *dr;
2564 dr = kzalloc(struct_size(dr, func, relations->device_count),
2569 dr->device_count = relations->device_count;
2570 for (i = 0; i < dr->device_count; i++) {
2571 dr->func[i].v_id = relations->func[i].v_id;
2572 dr->func[i].d_id = relations->func[i].d_id;
2573 dr->func[i].rev = relations->func[i].rev;
2574 dr->func[i].prog_intf = relations->func[i].prog_intf;
2575 dr->func[i].subclass = relations->func[i].subclass;
2576 dr->func[i].base_class = relations->func[i].base_class;
2577 dr->func[i].subsystem_id = relations->func[i].subsystem_id;
2578 dr->func[i].win_slot = relations->func[i].win_slot;
2579 dr->func[i].ser = relations->func[i].ser;
2580 dr->func[i].flags = relations->func[i].flags;
2581 dr->func[i].virtual_numa_node =
2582 relations->func[i].virtual_numa_node;
2585 if (hv_pci_start_relations_work(hbus, dr))
2590 * hv_eject_device_work() - Asynchronously handles ejection
2591 * @work: Work struct embedded in internal device struct
2593 * This function handles ejecting a device. Windows will
2594 * attempt to gracefully eject a device, waiting 60 seconds to
2595 * hear back from the guest OS that this completed successfully.
2596 * If this timer expires, the device will be forcibly removed.
2598 static void hv_eject_device_work(struct work_struct *work)
2600 struct pci_eject_response *ejct_pkt;
2601 struct hv_pcibus_device *hbus;
2602 struct hv_pci_dev *hpdev;
2603 struct pci_dev *pdev;
2604 unsigned long flags;
2607 struct pci_packet pkt;
2608 u8 buffer[sizeof(struct pci_eject_response)];
2611 hpdev = container_of(work, struct hv_pci_dev, wrk);
2614 WARN_ON(hpdev->state != hv_pcichild_ejecting);
2617 * Ejection can come before or after the PCI bus has been set up, so
2618 * attempt to find it and tear down the bus state, if it exists. This
2619 * must be done without constructs like pci_domain_nr(hbus->bridge->bus)
2620 * because hbus->bridge->bus may not exist yet.
2622 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
2623 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
2625 pci_lock_rescan_remove();
2626 pci_stop_and_remove_bus_device(pdev);
2628 pci_unlock_rescan_remove();
2631 spin_lock_irqsave(&hbus->device_list_lock, flags);
2632 list_del(&hpdev->list_entry);
2633 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
2635 if (hpdev->pci_slot)
2636 pci_destroy_slot(hpdev->pci_slot);
2638 memset(&ctxt, 0, sizeof(ctxt));
2639 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
2640 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
2641 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
2642 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
2643 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
2644 VM_PKT_DATA_INBAND, 0);
2646 /* For the get_pcichild() in hv_pci_eject_device() */
2647 put_pcichild(hpdev);
2648 /* For the two refs got in new_pcichild_device() */
2649 put_pcichild(hpdev);
2650 put_pcichild(hpdev);
2651 /* hpdev has been freed. Do not use it any more. */
2655 * hv_pci_eject_device() - Handles device ejection
2656 * @hpdev: Internal device tracking struct
2658 * This function is invoked when an ejection packet arrives. It
2659 * just schedules work so that we don't re-enter the packet
2660 * delivery code handling the ejection.
2662 static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
2664 struct hv_pcibus_device *hbus = hpdev->hbus;
2665 struct hv_device *hdev = hbus->hdev;
2667 if (hbus->state == hv_pcibus_removing) {
2668 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
2672 hpdev->state = hv_pcichild_ejecting;
2673 get_pcichild(hpdev);
2674 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
2675 queue_work(hbus->wq, &hpdev->wrk);
2679 * hv_pci_onchannelcallback() - Handles incoming packets
2680 * @context: Internal bus tracking struct
2682 * This function is invoked whenever the host sends a packet to
2683 * this channel (which is private to this root PCI bus).
2685 static void hv_pci_onchannelcallback(void *context)
2687 const int packet_size = 0x100;
2689 struct hv_pcibus_device *hbus = context;
2692 struct vmpacket_descriptor *desc;
2693 unsigned char *buffer;
2694 int bufferlen = packet_size;
2695 struct pci_packet *comp_packet;
2696 struct pci_response *response;
2697 struct pci_incoming_message *new_message;
2698 struct pci_bus_relations *bus_rel;
2699 struct pci_bus_relations2 *bus_rel2;
2700 struct pci_dev_inval_block *inval;
2701 struct pci_dev_incoming *dev_message;
2702 struct hv_pci_dev *hpdev;
2704 buffer = kmalloc(bufferlen, GFP_ATOMIC);
2709 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
2710 bufferlen, &bytes_recvd, &req_id);
2712 if (ret == -ENOBUFS) {
2714 /* Handle large packet */
2715 bufferlen = bytes_recvd;
2716 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
2722 /* Zero length indicates there are no more packets. */
2723 if (ret || !bytes_recvd)
2727 * All incoming packets must be at least as large as a
2730 if (bytes_recvd <= sizeof(struct pci_response))
2732 desc = (struct vmpacket_descriptor *)buffer;
2734 switch (desc->type) {
2738 * The host is trusted, and thus it's safe to interpret
2739 * this transaction ID as a pointer.
2741 comp_packet = (struct pci_packet *)req_id;
2742 response = (struct pci_response *)buffer;
2743 comp_packet->completion_func(comp_packet->compl_ctxt,
2748 case VM_PKT_DATA_INBAND:
2750 new_message = (struct pci_incoming_message *)buffer;
2751 switch (new_message->message_type.type) {
2752 case PCI_BUS_RELATIONS:
2754 bus_rel = (struct pci_bus_relations *)buffer;
2756 struct_size(bus_rel, func,
2757 bus_rel->device_count)) {
2758 dev_err(&hbus->hdev->device,
2759 "bus relations too small\n");
2763 hv_pci_devices_present(hbus, bus_rel);
2766 case PCI_BUS_RELATIONS2:
2768 bus_rel2 = (struct pci_bus_relations2 *)buffer;
2770 struct_size(bus_rel2, func,
2771 bus_rel2->device_count)) {
2772 dev_err(&hbus->hdev->device,
2773 "bus relations v2 too small\n");
2777 hv_pci_devices_present2(hbus, bus_rel2);
2782 dev_message = (struct pci_dev_incoming *)buffer;
2783 hpdev = get_pcichild_wslot(hbus,
2784 dev_message->wslot.slot);
2786 hv_pci_eject_device(hpdev);
2787 put_pcichild(hpdev);
2791 case PCI_INVALIDATE_BLOCK:
2793 inval = (struct pci_dev_inval_block *)buffer;
2794 hpdev = get_pcichild_wslot(hbus,
2797 if (hpdev->block_invalidate) {
2798 hpdev->block_invalidate(
2799 hpdev->invalidate_context,
2802 put_pcichild(hpdev);
2807 dev_warn(&hbus->hdev->device,
2808 "Unimplemented protocol message %x\n",
2809 new_message->message_type.type);
2815 dev_err(&hbus->hdev->device,
2816 "unhandled packet type %d, tid %llx len %d\n",
2817 desc->type, req_id, bytes_recvd);
2826 * hv_pci_protocol_negotiation() - Set up protocol
2827 * @hdev: VMBus's tracking struct for this root PCI bus.
2828 * @version: Array of supported channel protocol versions in
2829 * the order of probing - highest go first.
2830 * @num_version: Number of elements in the version array.
2832 * This driver is intended to support running on Windows 10
2833 * (server) and later versions. It will not run on earlier
2834 * versions, as they assume that many of the operations which
2835 * Linux needs accomplished with a spinlock held were done via
2836 * asynchronous messaging via VMBus. Windows 10 increases the
2837 * surface area of PCI emulation so that these actions can take
2838 * place by suspending a virtual processor for their duration.
2840 * This function negotiates the channel protocol version,
2841 * failing if the host doesn't support the necessary protocol
2844 static int hv_pci_protocol_negotiation(struct hv_device *hdev,
2845 enum pci_protocol_version_t version[],
2848 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2849 struct pci_version_request *version_req;
2850 struct hv_pci_compl comp_pkt;
2851 struct pci_packet *pkt;
2856 * Initiate the handshake with the host and negotiate
2857 * a version that the host can support. We start with the
2858 * highest version number and go down if the host cannot
2861 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2865 init_completion(&comp_pkt.host_event);
2866 pkt->completion_func = hv_pci_generic_compl;
2867 pkt->compl_ctxt = &comp_pkt;
2868 version_req = (struct pci_version_request *)&pkt->message;
2869 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
2871 for (i = 0; i < num_version; i++) {
2872 version_req->protocol_version = version[i];
2873 ret = vmbus_sendpacket(hdev->channel, version_req,
2874 sizeof(struct pci_version_request),
2875 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2876 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2878 ret = wait_for_response(hdev, &comp_pkt.host_event);
2881 dev_err(&hdev->device,
2882 "PCI Pass-through VSP failed to request version: %d",
2887 if (comp_pkt.completion_status >= 0) {
2888 hbus->protocol_version = version[i];
2889 dev_info(&hdev->device,
2890 "PCI VMBus probing: Using version %#x\n",
2891 hbus->protocol_version);
2895 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2896 dev_err(&hdev->device,
2897 "PCI Pass-through VSP failed version request: %#x",
2898 comp_pkt.completion_status);
2903 reinit_completion(&comp_pkt.host_event);
2906 dev_err(&hdev->device,
2907 "PCI pass-through VSP failed to find supported version");
2916 * hv_pci_free_bridge_windows() - Release memory regions for the
2918 * @hbus: Root PCI bus, as understood by this driver
2920 static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2923 * Set the resources back to the way they looked when they
2924 * were allocated by setting IORESOURCE_BUSY again.
2927 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2928 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
2929 vmbus_free_mmio(hbus->low_mmio_res->start,
2930 resource_size(hbus->low_mmio_res));
2933 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2934 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
2935 vmbus_free_mmio(hbus->high_mmio_res->start,
2936 resource_size(hbus->high_mmio_res));
2941 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2943 * @hbus: Root PCI bus, as understood by this driver
2945 * This function calls vmbus_allocate_mmio(), which is itself a
2946 * bit of a compromise. Ideally, we might change the pnp layer
2947 * in the kernel such that it comprehends either PCI devices
2948 * which are "grandchildren of ACPI," with some intermediate bus
2949 * node (in this case, VMBus) or change it such that it
2950 * understands VMBus. The pnp layer, however, has been declared
2951 * deprecated, and not subject to change.
2953 * The workaround, implemented here, is to ask VMBus to allocate
2954 * MMIO space for this bus. VMBus itself knows which ranges are
2955 * appropriate by looking at its own ACPI objects. Then, after
2956 * these ranges are claimed, they're modified to look like they
2957 * would have looked if the ACPI and pnp code had allocated
2958 * bridge windows. These descriptors have to exist in this form
2959 * in order to satisfy the code which will get invoked when the
2960 * endpoint PCI function driver calls request_mem_region() or
2961 * request_mem_region_exclusive().
2963 * Return: 0 on success, -errno on failure
2965 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2967 resource_size_t align;
2970 if (hbus->low_mmio_space) {
2971 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2972 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2973 (u64)(u32)0xffffffff,
2974 hbus->low_mmio_space,
2977 dev_err(&hbus->hdev->device,
2978 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2979 hbus->low_mmio_space);
2983 /* Modify this resource to become a bridge window. */
2984 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2985 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2986 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
2989 if (hbus->high_mmio_space) {
2990 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2991 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2993 hbus->high_mmio_space, align,
2996 dev_err(&hbus->hdev->device,
2997 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2998 hbus->high_mmio_space);
2999 goto release_low_mmio;
3002 /* Modify this resource to become a bridge window. */
3003 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
3004 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
3005 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
3011 if (hbus->low_mmio_res) {
3012 vmbus_free_mmio(hbus->low_mmio_res->start,
3013 resource_size(hbus->low_mmio_res));
3020 * hv_allocate_config_window() - Find MMIO space for PCI Config
3021 * @hbus: Root PCI bus, as understood by this driver
3023 * This function claims memory-mapped I/O space for accessing
3024 * configuration space for the functions on this bus.
3026 * Return: 0 on success, -errno on failure
3028 static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
3033 * Set up a region of MMIO space to use for accessing configuration
3036 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
3037 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
3042 * vmbus_allocate_mmio() gets used for allocating both device endpoint
3043 * resource claims (those which cannot be overlapped) and the ranges
3044 * which are valid for the children of this bus, which are intended
3045 * to be overlapped by those children. Set the flag on this claim
3046 * meaning that this region can't be overlapped.
3049 hbus->mem_config->flags |= IORESOURCE_BUSY;
3054 static void hv_free_config_window(struct hv_pcibus_device *hbus)
3056 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
3059 static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
3062 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
3063 * @hdev: VMBus's tracking struct for this root PCI bus
3065 * Return: 0 on success, -errno on failure
3067 static int hv_pci_enter_d0(struct hv_device *hdev)
3069 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3070 struct pci_bus_d0_entry *d0_entry;
3071 struct hv_pci_compl comp_pkt;
3072 struct pci_packet *pkt;
3076 * Tell the host that the bus is ready to use, and moved into the
3077 * powered-on state. This includes telling the host which region
3078 * of memory-mapped I/O space has been chosen for configuration space
3081 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
3085 init_completion(&comp_pkt.host_event);
3086 pkt->completion_func = hv_pci_generic_compl;
3087 pkt->compl_ctxt = &comp_pkt;
3088 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
3089 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
3090 d0_entry->mmio_base = hbus->mem_config->start;
3092 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
3093 (unsigned long)pkt, VM_PKT_DATA_INBAND,
3094 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3096 ret = wait_for_response(hdev, &comp_pkt.host_event);
3101 if (comp_pkt.completion_status < 0) {
3102 dev_err(&hdev->device,
3103 "PCI Pass-through VSP failed D0 Entry with status %x\n",
3104 comp_pkt.completion_status);
3117 * hv_pci_query_relations() - Ask host to send list of child
3119 * @hdev: VMBus's tracking struct for this root PCI bus
3121 * Return: 0 on success, -errno on failure
3123 static int hv_pci_query_relations(struct hv_device *hdev)
3125 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3126 struct pci_message message;
3127 struct completion comp;
3130 /* Ask the host to send along the list of child devices */
3131 init_completion(&comp);
3132 if (cmpxchg(&hbus->survey_event, NULL, &comp))
3135 memset(&message, 0, sizeof(message));
3136 message.type = PCI_QUERY_BUS_RELATIONS;
3138 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
3139 0, VM_PKT_DATA_INBAND, 0);
3141 ret = wait_for_response(hdev, &comp);
3147 * hv_send_resources_allocated() - Report local resource choices
3148 * @hdev: VMBus's tracking struct for this root PCI bus
3150 * The host OS is expecting to be sent a request as a message
3151 * which contains all the resources that the device will use.
3152 * The response contains those same resources, "translated"
3153 * which is to say, the values which should be used by the
3154 * hardware, when it delivers an interrupt. (MMIO resources are
3155 * used in local terms.) This is nice for Windows, and lines up
3156 * with the FDO/PDO split, which doesn't exist in Linux. Linux
3157 * is deeply expecting to scan an emulated PCI configuration
3158 * space. So this message is sent here only to drive the state
3159 * machine on the host forward.
3161 * Return: 0 on success, -errno on failure
3163 static int hv_send_resources_allocated(struct hv_device *hdev)
3165 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3166 struct pci_resources_assigned *res_assigned;
3167 struct pci_resources_assigned2 *res_assigned2;
3168 struct hv_pci_compl comp_pkt;
3169 struct hv_pci_dev *hpdev;
3170 struct pci_packet *pkt;
3175 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
3176 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
3178 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
3184 for (wslot = 0; wslot < 256; wslot++) {
3185 hpdev = get_pcichild_wslot(hbus, wslot);
3189 memset(pkt, 0, sizeof(*pkt) + size_res);
3190 init_completion(&comp_pkt.host_event);
3191 pkt->completion_func = hv_pci_generic_compl;
3192 pkt->compl_ctxt = &comp_pkt;
3194 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
3196 (struct pci_resources_assigned *)&pkt->message;
3197 res_assigned->message_type.type =
3198 PCI_RESOURCES_ASSIGNED;
3199 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
3202 (struct pci_resources_assigned2 *)&pkt->message;
3203 res_assigned2->message_type.type =
3204 PCI_RESOURCES_ASSIGNED2;
3205 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
3207 put_pcichild(hpdev);
3209 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
3210 size_res, (unsigned long)pkt,
3212 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3214 ret = wait_for_response(hdev, &comp_pkt.host_event);
3218 if (comp_pkt.completion_status < 0) {
3220 dev_err(&hdev->device,
3221 "resource allocated returned 0x%x",
3222 comp_pkt.completion_status);
3226 hbus->wslot_res_allocated = wslot;
3234 * hv_send_resources_released() - Report local resources
3236 * @hdev: VMBus's tracking struct for this root PCI bus
3238 * Return: 0 on success, -errno on failure
3240 static int hv_send_resources_released(struct hv_device *hdev)
3242 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3243 struct pci_child_message pkt;
3244 struct hv_pci_dev *hpdev;
3248 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
3249 hpdev = get_pcichild_wslot(hbus, wslot);
3253 memset(&pkt, 0, sizeof(pkt));
3254 pkt.message_type.type = PCI_RESOURCES_RELEASED;
3255 pkt.wslot.slot = hpdev->desc.win_slot.slot;
3257 put_pcichild(hpdev);
3259 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
3260 VM_PKT_DATA_INBAND, 0);
3264 hbus->wslot_res_allocated = wslot - 1;
3267 hbus->wslot_res_allocated = -1;
3272 #define HVPCI_DOM_MAP_SIZE (64 * 1024)
3273 static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
3276 * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
3277 * as invalid for passthrough PCI devices of this driver.
3279 #define HVPCI_DOM_INVALID 0
3282 * hv_get_dom_num() - Get a valid PCI domain number
3283 * Check if the PCI domain number is in use, and return another number if
3286 * @dom: Requested domain number
3288 * return: domain number on success, HVPCI_DOM_INVALID on failure
3290 static u16 hv_get_dom_num(u16 dom)
3294 if (test_and_set_bit(dom, hvpci_dom_map) == 0)
3297 for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
3298 if (test_and_set_bit(i, hvpci_dom_map) == 0)
3302 return HVPCI_DOM_INVALID;
3306 * hv_put_dom_num() - Mark the PCI domain number as free
3307 * @dom: Domain number to be freed
3309 static void hv_put_dom_num(u16 dom)
3311 clear_bit(dom, hvpci_dom_map);
3315 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3316 * @hdev: VMBus's tracking struct for this root PCI bus
3317 * @dev_id: Identifies the device itself
3319 * Return: 0 on success, -errno on failure
3321 static int hv_pci_probe(struct hv_device *hdev,
3322 const struct hv_vmbus_device_id *dev_id)
3324 struct pci_host_bridge *bridge;
3325 struct hv_pcibus_device *hbus;
3328 bool enter_d0_retry = true;
3332 * hv_pcibus_device contains the hypercall arguments for retargeting in
3333 * hv_irq_unmask(). Those must not cross a page boundary.
3335 BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
3337 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
3342 * With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
3343 * alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
3344 * a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
3345 * alignment of hbus is important because hbus's field
3346 * retarget_msi_interrupt_params must not cross a 4KB page boundary.
3348 * Here we prefer kzalloc to get_zeroed_page(), because a buffer
3349 * allocated by the latter is not tracked and scanned by kmemleak, and
3350 * hence kmemleak reports the pointer contained in the hbus buffer
3351 * (i.e. the hpdev struct, which is created in new_pcichild_device() and
3352 * is tracked by hbus->children) as memory leak (false positive).
3354 * If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
3355 * used to allocate the hbus buffer and we can avoid the kmemleak false
3356 * positive by using kmemleak_alloc() and kmemleak_free() to ask
3357 * kmemleak to track and scan the hbus buffer.
3359 hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
3363 hbus->bridge = bridge;
3364 hbus->state = hv_pcibus_init;
3365 hbus->wslot_res_allocated = -1;
3368 * The PCI bus "domain" is what is called "segment" in ACPI and other
3369 * specs. Pull it from the instance ID, to get something usually
3370 * unique. In rare cases of collision, we will find out another number
3373 * Note that, since this code only runs in a Hyper-V VM, Hyper-V
3374 * together with this guest driver can guarantee that (1) The only
3375 * domain used by Gen1 VMs for something that looks like a physical
3376 * PCI bus (which is actually emulated by the hypervisor) is domain 0.
3377 * (2) There will be no overlap between domains (after fixing possible
3378 * collisions) in the same VM.
3380 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
3381 dom = hv_get_dom_num(dom_req);
3383 if (dom == HVPCI_DOM_INVALID) {
3384 dev_err(&hdev->device,
3385 "Unable to use dom# 0x%x or other numbers", dom_req);
3391 dev_info(&hdev->device,
3392 "PCI dom# 0x%x has collision, using 0x%x",
3395 hbus->bridge->domain_nr = dom;
3397 hbus->sysdata.domain = dom;
3401 INIT_LIST_HEAD(&hbus->children);
3402 INIT_LIST_HEAD(&hbus->dr_list);
3403 spin_lock_init(&hbus->config_lock);
3404 spin_lock_init(&hbus->device_list_lock);
3405 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
3406 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
3407 hbus->bridge->domain_nr);
3413 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3414 hv_pci_onchannelcallback, hbus);
3418 hv_set_drvdata(hdev, hbus);
3420 ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
3421 ARRAY_SIZE(pci_protocol_versions));
3425 ret = hv_allocate_config_window(hbus);
3429 hbus->cfg_addr = ioremap(hbus->mem_config->start,
3430 PCI_CONFIG_MMIO_LENGTH);
3431 if (!hbus->cfg_addr) {
3432 dev_err(&hdev->device,
3433 "Unable to map a virtual address for config space\n");
3438 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
3444 hbus->fwnode = irq_domain_alloc_named_fwnode(name);
3446 if (!hbus->fwnode) {
3451 ret = hv_pcie_init_irq_domain(hbus);
3456 ret = hv_pci_query_relations(hdev);
3458 goto free_irq_domain;
3460 ret = hv_pci_enter_d0(hdev);
3462 * In certain case (Kdump) the pci device of interest was
3463 * not cleanly shut down and resource is still held on host
3464 * side, the host could return invalid device status.
3465 * We need to explicitly request host to release the resource
3466 * and try to enter D0 again.
3467 * Since the hv_pci_bus_exit() call releases structures
3468 * of all its child devices, we need to start the retry from
3469 * hv_pci_query_relations() call, requesting host to send
3470 * the synchronous child device relations message before this
3471 * information is needed in hv_send_resources_allocated()
3474 if (ret == -EPROTO && enter_d0_retry) {
3475 enter_d0_retry = false;
3477 dev_err(&hdev->device, "Retrying D0 Entry\n");
3480 * Hv_pci_bus_exit() calls hv_send_resources_released()
3481 * to free up resources of its child devices.
3482 * In the kdump kernel we need to set the
3483 * wslot_res_allocated to 255 so it scans all child
3484 * devices to release resources allocated in the
3485 * normal kernel before panic happened.
3487 hbus->wslot_res_allocated = 255;
3488 ret = hv_pci_bus_exit(hdev, true);
3493 dev_err(&hdev->device,
3494 "Retrying D0 failed with ret %d\n", ret);
3497 goto free_irq_domain;
3499 ret = hv_pci_allocate_bridge_windows(hbus);
3503 ret = hv_send_resources_allocated(hdev);
3507 prepopulate_bars(hbus);
3509 hbus->state = hv_pcibus_probed;
3511 ret = create_root_hv_pci_bus(hbus);
3518 hv_pci_free_bridge_windows(hbus);
3520 (void) hv_pci_bus_exit(hdev, true);
3522 irq_domain_remove(hbus->irq_domain);
3524 irq_domain_free_fwnode(hbus->fwnode);
3526 iounmap(hbus->cfg_addr);
3528 hv_free_config_window(hbus);
3530 vmbus_close(hdev->channel);
3532 destroy_workqueue(hbus->wq);
3534 hv_put_dom_num(hbus->bridge->domain_nr);
3540 static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
3542 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3544 struct pci_packet teardown_packet;
3545 u8 buffer[sizeof(struct pci_message)];
3547 struct hv_pci_compl comp_pkt;
3548 struct hv_pci_dev *hpdev, *tmp;
3549 unsigned long flags;
3553 * After the host sends the RESCIND_CHANNEL message, it doesn't
3554 * access the per-channel ringbuffer any longer.
3556 if (hdev->channel->rescind)
3560 struct list_head removed;
3562 /* Move all present children to the list on stack */
3563 INIT_LIST_HEAD(&removed);
3564 spin_lock_irqsave(&hbus->device_list_lock, flags);
3565 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
3566 list_move_tail(&hpdev->list_entry, &removed);
3567 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
3569 /* Remove all children in the list */
3570 list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
3571 list_del(&hpdev->list_entry);
3572 if (hpdev->pci_slot)
3573 pci_destroy_slot(hpdev->pci_slot);
3574 /* For the two refs got in new_pcichild_device() */
3575 put_pcichild(hpdev);
3576 put_pcichild(hpdev);
3580 ret = hv_send_resources_released(hdev);
3582 dev_err(&hdev->device,
3583 "Couldn't send resources released packet(s)\n");
3587 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
3588 init_completion(&comp_pkt.host_event);
3589 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
3590 pkt.teardown_packet.compl_ctxt = &comp_pkt;
3591 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
3593 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
3594 sizeof(struct pci_message),
3595 (unsigned long)&pkt.teardown_packet,
3597 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
3601 if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0)
3608 * hv_pci_remove() - Remove routine for this VMBus channel
3609 * @hdev: VMBus's tracking struct for this root PCI bus
3611 * Return: 0 on success, -errno on failure
3613 static int hv_pci_remove(struct hv_device *hdev)
3615 struct hv_pcibus_device *hbus;
3618 hbus = hv_get_drvdata(hdev);
3619 if (hbus->state == hv_pcibus_installed) {
3620 tasklet_disable(&hdev->channel->callback_event);
3621 hbus->state = hv_pcibus_removing;
3622 tasklet_enable(&hdev->channel->callback_event);
3623 destroy_workqueue(hbus->wq);
3626 * At this point, no work is running or can be scheduled
3627 * on hbus-wq. We can't race with hv_pci_devices_present()
3628 * or hv_pci_eject_device(), it's safe to proceed.
3631 /* Remove the bus from PCI's point of view. */
3632 pci_lock_rescan_remove();
3633 pci_stop_root_bus(hbus->bridge->bus);
3634 hv_pci_remove_slots(hbus);
3635 pci_remove_root_bus(hbus->bridge->bus);
3636 pci_unlock_rescan_remove();
3639 ret = hv_pci_bus_exit(hdev, false);
3641 vmbus_close(hdev->channel);
3643 iounmap(hbus->cfg_addr);
3644 hv_free_config_window(hbus);
3645 hv_pci_free_bridge_windows(hbus);
3646 irq_domain_remove(hbus->irq_domain);
3647 irq_domain_free_fwnode(hbus->fwnode);
3649 hv_put_dom_num(hbus->bridge->domain_nr);
3655 static int hv_pci_suspend(struct hv_device *hdev)
3657 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3658 enum hv_pcibus_state old_state;
3662 * hv_pci_suspend() must make sure there are no pending work items
3663 * before calling vmbus_close(), since it runs in a process context
3664 * as a callback in dpm_suspend(). When it starts to run, the channel
3665 * callback hv_pci_onchannelcallback(), which runs in a tasklet
3666 * context, can be still running concurrently and scheduling new work
3667 * items onto hbus->wq in hv_pci_devices_present() and
3668 * hv_pci_eject_device(), and the work item handlers can access the
3669 * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
3670 * the work item handler pci_devices_present_work() ->
3671 * new_pcichild_device() writes to the vmbus channel.
3673 * To eliminate the race, hv_pci_suspend() disables the channel
3674 * callback tasklet, sets hbus->state to hv_pcibus_removing, and
3675 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
3676 * it knows that no new work item can be scheduled, and then it flushes
3677 * hbus->wq and safely closes the vmbus channel.
3679 tasklet_disable(&hdev->channel->callback_event);
3681 /* Change the hbus state to prevent new work items. */
3682 old_state = hbus->state;
3683 if (hbus->state == hv_pcibus_installed)
3684 hbus->state = hv_pcibus_removing;
3686 tasklet_enable(&hdev->channel->callback_event);
3688 if (old_state != hv_pcibus_installed)
3691 flush_workqueue(hbus->wq);
3693 ret = hv_pci_bus_exit(hdev, true);
3697 vmbus_close(hdev->channel);
3702 static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
3704 struct msi_desc *entry;
3705 struct irq_data *irq_data;
3707 for_each_pci_msi_entry(entry, pdev) {
3708 irq_data = irq_get_irq_data(entry->irq);
3709 if (WARN_ON_ONCE(!irq_data))
3712 hv_compose_msi_msg(irq_data, &entry->msg);
3719 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
3720 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
3721 * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
3722 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
3725 static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
3727 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
3730 static int hv_pci_resume(struct hv_device *hdev)
3732 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
3733 enum pci_protocol_version_t version[1];
3736 hbus->state = hv_pcibus_init;
3738 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
3739 hv_pci_onchannelcallback, hbus);
3743 /* Only use the version that was in use before hibernation. */
3744 version[0] = hbus->protocol_version;
3745 ret = hv_pci_protocol_negotiation(hdev, version, 1);
3749 ret = hv_pci_query_relations(hdev);
3753 ret = hv_pci_enter_d0(hdev);
3757 ret = hv_send_resources_allocated(hdev);
3761 prepopulate_bars(hbus);
3763 hv_pci_restore_msi_state(hbus);
3765 hbus->state = hv_pcibus_installed;
3768 vmbus_close(hdev->channel);
3772 static const struct hv_vmbus_device_id hv_pci_id_table[] = {
3773 /* PCI Pass-through Class ID */
3774 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
3779 MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
3781 static struct hv_driver hv_pci_drv = {
3783 .id_table = hv_pci_id_table,
3784 .probe = hv_pci_probe,
3785 .remove = hv_pci_remove,
3786 .suspend = hv_pci_suspend,
3787 .resume = hv_pci_resume,
3790 static void __exit exit_hv_pci_drv(void)
3792 vmbus_driver_unregister(&hv_pci_drv);
3794 hvpci_block_ops.read_block = NULL;
3795 hvpci_block_ops.write_block = NULL;
3796 hvpci_block_ops.reg_blk_invalidate = NULL;
3799 static int __init init_hv_pci_drv(void)
3803 if (!hv_is_hyperv_initialized())
3806 ret = hv_pci_irqchip_init();
3810 /* Set the invalid domain number's bit, so it will not be used */
3811 set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
3813 /* Initialize PCI block r/w interface */
3814 hvpci_block_ops.read_block = hv_read_config_block;
3815 hvpci_block_ops.write_block = hv_write_config_block;
3816 hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
3818 return vmbus_driver_register(&hv_pci_drv);
3821 module_init(init_hv_pci_drv);
3822 module_exit(exit_hv_pci_drv);
3824 MODULE_DESCRIPTION("Hyper-V PCI");
3825 MODULE_LICENSE("GPL v2");