2 * Copyright (c) Microsoft Corporation.
7 * This driver acts as a paravirtual front-end for PCI Express root buses.
8 * When a PCI Express function (either an entire device or an SR-IOV
9 * Virtual Function) is being passed through to the VM, this driver exposes
10 * a new bus to the guest VM. This is modeled as a root PCI bus because
11 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
12 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
13 * until a device as been exposed using this driver.
15 * Each root PCI bus has its own PCI domain, which is called "Segment" in
16 * the PCI Firmware Specifications. Thus while each device passed through
17 * to the VM using this front-end will appear at "device 0", the domain will
18 * be unique. Typically, each bus will have one PCI function on it, though
19 * this driver does support more than one.
21 * In order to map the interrupts from the device through to the guest VM,
22 * this driver also implements an IRQ Domain, which handles interrupts (either
23 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
24 * set up, torn down, or reaffined, this driver communicates with the
25 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
26 * interrupt will be delivered to the correct virtual processor at the right
27 * vector. This driver does not support level-triggered (line-based)
28 * interrupts, and will report that the Interrupt Line register in the
29 * function's configuration space is zero.
31 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
32 * facilities. For instance, the configuration space of a function exposed
33 * by Hyper-V is mapped into a single page of memory space, and the
34 * read and write handlers for config space must be aware of this mechanism.
35 * Similarly, device setup and teardown involves messages sent to and from
36 * the PCI back-end driver in Hyper-V.
38 * This program is free software; you can redistribute it and/or modify it
39 * under the terms of the GNU General Public License version 2 as published
40 * by the Free Software Foundation.
42 * This program is distributed in the hope that it will be useful, but
43 * WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
45 * NON INFRINGEMENT. See the GNU General Public License for more
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/pci.h>
53 #include <linux/semaphore.h>
54 #include <linux/irqdomain.h>
55 #include <asm/irqdomain.h>
57 #include <linux/msi.h>
58 #include <linux/hyperv.h>
59 #include <asm/mshyperv.h>
62 * Protocol versions. The low word is the minor version, the high word the
66 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (major)))
67 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
68 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
71 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),
72 PCI_PROTOCOL_VERSION_CURRENT = PCI_PROTOCOL_VERSION_1_1
75 #define PCI_CONFIG_MMIO_LENGTH 0x2000
76 #define CFG_PAGE_OFFSET 0x1000
77 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
79 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
85 enum pci_message_type {
89 PCI_MESSAGE_BASE = 0x42490000,
90 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
91 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
92 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
93 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
94 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
95 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
96 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
97 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
98 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
99 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
100 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
101 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
102 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
103 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
104 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
105 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
106 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
107 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
108 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
109 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
114 * Structures defining the virtual PCI Express protocol.
126 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
127 * which is all this driver does. This representation is the one used in
128 * Windows, which is what is expected when sending this back and forth with
129 * the Hyper-V parent partition.
131 union win_slot_encoding {
140 * Pretty much as defined in the PCI Specifications.
142 struct pci_function_description {
143 u16 v_id; /* vendor ID */
144 u16 d_id; /* device ID */
150 union win_slot_encoding win_slot;
151 u32 ser; /* serial number */
157 * @delivery_mode: As defined in Intel's Programmer's
158 * Reference Manual, Volume 3, Chapter 8.
159 * @vector_count: Number of contiguous entries in the
160 * Interrupt Descriptor Table that are
161 * occupied by this Message-Signaled
162 * Interrupt. For "MSI", as first defined
163 * in PCI 2.2, this can be between 1 and
164 * 32. For "MSI-X," as first defined in PCI
165 * 3.0, this must be 1, as each MSI-X table
166 * entry would have its own descriptor.
167 * @reserved: Empty space
168 * @cpu_mask: All the target virtual processors.
179 * struct tran_int_desc
180 * @reserved: unused, padding
181 * @vector_count: same as in hv_msi_desc
182 * @data: This is the "data payload" value that is
183 * written by the device when it generates
184 * a message-signaled interrupt, either MSI
186 * @address: This is the address to which the data
187 * payload is written on interrupt
190 struct tran_int_desc {
198 * A generic message format for virtual PCI.
199 * Specific message formats are defined later in the file.
206 struct pci_child_message {
207 struct pci_message message_type;
208 union win_slot_encoding wslot;
211 struct pci_incoming_message {
212 struct vmpacket_descriptor hdr;
213 struct pci_message message_type;
216 struct pci_response {
217 struct vmpacket_descriptor hdr;
218 s32 status; /* negative values are failures */
222 void (*completion_func)(void *context, struct pci_response *resp,
223 int resp_packet_size);
226 struct pci_message message[0];
230 * Specific message types supporting the PCI protocol.
234 * Version negotiation message. Sent from the guest to the host.
235 * The guest is free to try different versions until the host
236 * accepts the version.
238 * pci_version: The protocol version requested.
239 * is_last_attempt: If TRUE, this is the last version guest will request.
240 * reservedz: Reserved field, set to zero.
243 struct pci_version_request {
244 struct pci_message message_type;
245 enum pci_message_type protocol_version;
249 * Bus D0 Entry. This is sent from the guest to the host when the virtual
250 * bus (PCI Express port) is ready for action.
253 struct pci_bus_d0_entry {
254 struct pci_message message_type;
259 struct pci_bus_relations {
260 struct pci_incoming_message incoming;
262 struct pci_function_description func[0];
265 struct pci_q_res_req_response {
266 struct vmpacket_descriptor hdr;
267 s32 status; /* negative values are failures */
271 struct pci_set_power {
272 struct pci_message message_type;
273 union win_slot_encoding wslot;
274 u32 power_state; /* In Windows terms */
278 struct pci_set_power_response {
279 struct vmpacket_descriptor hdr;
280 s32 status; /* negative values are failures */
281 union win_slot_encoding wslot;
282 u32 resultant_state; /* In Windows terms */
286 struct pci_resources_assigned {
287 struct pci_message message_type;
288 union win_slot_encoding wslot;
289 u8 memory_range[0x14][6]; /* not used here */
294 struct pci_create_interrupt {
295 struct pci_message message_type;
296 union win_slot_encoding wslot;
297 struct hv_msi_desc int_desc;
300 struct pci_create_int_response {
301 struct pci_response response;
303 struct tran_int_desc int_desc;
306 struct pci_delete_interrupt {
307 struct pci_message message_type;
308 union win_slot_encoding wslot;
309 struct tran_int_desc int_desc;
312 struct pci_dev_incoming {
313 struct pci_incoming_message incoming;
314 union win_slot_encoding wslot;
317 struct pci_eject_response {
318 struct pci_message message_type;
319 union win_slot_encoding wslot;
323 static int pci_ring_size = (4 * PAGE_SIZE);
326 * Definitions or interrupt steering hypercall.
328 #define HV_PARTITION_ID_SELF ((u64)-1)
329 #define HVCALL_RETARGET_INTERRUPT 0x7e
331 struct retarget_msi_interrupt {
332 u64 partition_id; /* use "self" */
334 u32 source; /* 1 for MSI(-X) */
345 * Driver specific state.
348 enum hv_pcibus_state {
355 struct hv_pcibus_device {
356 struct pci_sysdata sysdata;
357 enum hv_pcibus_state state;
358 atomic_t remove_lock;
359 struct hv_device *hdev;
360 resource_size_t low_mmio_space;
361 resource_size_t high_mmio_space;
362 struct resource *mem_config;
363 struct resource *low_mmio_res;
364 struct resource *high_mmio_res;
365 struct completion *survey_event;
366 struct completion remove_event;
367 struct pci_bus *pci_bus;
368 spinlock_t config_lock; /* Avoid two threads writing index page */
369 spinlock_t device_list_lock; /* Protect lists below */
370 void __iomem *cfg_addr;
372 struct semaphore enum_sem;
373 struct list_head resources_for_children;
375 struct list_head children;
376 struct list_head dr_list;
378 struct msi_domain_info msi_info;
379 struct msi_controller msi_chip;
380 struct irq_domain *irq_domain;
381 struct retarget_msi_interrupt retarget_msi_interrupt_params;
382 spinlock_t retarget_msi_interrupt_lock;
386 * Tracks "Device Relations" messages from the host, which must be both
387 * processed in order and deferred so that they don't run in the context
388 * of the incoming packet callback.
391 struct work_struct wrk;
392 struct hv_pcibus_device *bus;
396 struct list_head list_entry;
398 struct pci_function_description func[0];
401 enum hv_pcichild_state {
402 hv_pcichild_init = 0,
403 hv_pcichild_requirements,
404 hv_pcichild_resourced,
405 hv_pcichild_ejecting,
409 enum hv_pcidev_ref_reason {
410 hv_pcidev_ref_invalid = 0,
411 hv_pcidev_ref_initial,
412 hv_pcidev_ref_by_slot,
413 hv_pcidev_ref_packet,
415 hv_pcidev_ref_childlist,
421 /* List protected by pci_rescan_remove_lock */
422 struct list_head list_entry;
424 enum hv_pcichild_state state;
425 struct pci_function_description desc;
426 bool reported_missing;
427 struct hv_pcibus_device *hbus;
428 struct work_struct wrk;
431 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
432 * read it back, for each of the BAR offsets within config space.
437 struct hv_pci_compl {
438 struct completion host_event;
439 s32 completion_status;
443 * hv_pci_generic_compl() - Invoked for a completion packet
444 * @context: Set up by the sender of the packet.
445 * @resp: The response packet
446 * @resp_packet_size: Size in bytes of the packet
448 * This function is used to trigger an event and report status
449 * for any message for which the completion packet contains a
450 * status and nothing else.
452 static void hv_pci_generic_compl(void *context, struct pci_response *resp,
453 int resp_packet_size)
455 struct hv_pci_compl *comp_pkt = context;
457 if (resp_packet_size >= offsetofend(struct pci_response, status))
458 comp_pkt->completion_status = resp->status;
460 comp_pkt->completion_status = -1;
462 complete(&comp_pkt->host_event);
465 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
467 static void get_pcichild(struct hv_pci_dev *hv_pcidev,
468 enum hv_pcidev_ref_reason reason);
469 static void put_pcichild(struct hv_pci_dev *hv_pcidev,
470 enum hv_pcidev_ref_reason reason);
472 static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
473 static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
476 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
477 * @devfn: The Linux representation of PCI slot
479 * Windows uses a slightly different representation of PCI slot.
481 * Return: The Windows representation
483 static u32 devfn_to_wslot(int devfn)
485 union win_slot_encoding wslot;
488 wslot.bits.func = PCI_SLOT(devfn) | (PCI_FUNC(devfn) << 5);
494 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
495 * @wslot: The Windows representation of PCI slot
497 * Windows uses a slightly different representation of PCI slot.
499 * Return: The Linux representation
501 static int wslot_to_devfn(u32 wslot)
503 union win_slot_encoding slot_no;
505 slot_no.slot = wslot;
506 return PCI_DEVFN(0, slot_no.bits.func);
510 * PCI Configuration Space for these root PCI buses is implemented as a pair
511 * of pages in memory-mapped I/O space. Writing to the first page chooses
512 * the PCI function being written or read. Once the first page has been
513 * written to, the following page maps in the entire configuration space of
518 * _hv_pcifront_read_config() - Internal PCI config read
519 * @hpdev: The PCI driver's representation of the device
520 * @where: Offset within config space
521 * @size: Size of the transfer
522 * @val: Pointer to the buffer receiving the data
524 static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
528 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
531 * If the attempt is to read the IDs or the ROM BAR, simulate that.
533 if (where + size <= PCI_COMMAND) {
534 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
535 } else if (where >= PCI_CLASS_REVISION && where + size <=
536 PCI_CACHE_LINE_SIZE) {
537 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
538 PCI_CLASS_REVISION, size);
539 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
541 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
542 PCI_SUBSYSTEM_VENDOR_ID, size);
543 } else if (where >= PCI_ROM_ADDRESS && where + size <=
544 PCI_CAPABILITY_LIST) {
545 /* ROM BARs are unimplemented */
547 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
550 * Interrupt Line and Interrupt PIN are hard-wired to zero
551 * because this front-end only supports message-signaled
555 } else if (where + size <= CFG_PAGE_SIZE) {
556 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
557 /* Choose the function to be read. (See comment above) */
558 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
559 /* Make sure the function was chosen before we start reading. */
561 /* Read from that function's config space. */
574 * Make sure the write was done before we release the spinlock
575 * allowing consecutive reads/writes.
578 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
580 dev_err(&hpdev->hbus->hdev->device,
581 "Attempt to read beyond a function's config space.\n");
586 * _hv_pcifront_write_config() - Internal PCI config write
587 * @hpdev: The PCI driver's representation of the device
588 * @where: Offset within config space
589 * @size: Size of the transfer
590 * @val: The data being transferred
592 static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
596 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
598 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
599 where + size <= PCI_CAPABILITY_LIST) {
600 /* SSIDs and ROM BARs are read-only */
601 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
602 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
603 /* Choose the function to be written. (See comment above) */
604 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
605 /* Make sure the function was chosen before we start writing. */
607 /* Write to that function's config space. */
620 * Make sure the write was done before we release the spinlock
621 * allowing consecutive reads/writes.
624 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
626 dev_err(&hpdev->hbus->hdev->device,
627 "Attempt to write beyond a function's config space.\n");
632 * hv_pcifront_read_config() - Read configuration space
633 * @bus: PCI Bus structure
634 * @devfn: Device/function
635 * @where: Offset from base
636 * @size: Byte/word/dword
637 * @val: Value to be read
639 * Return: PCIBIOS_SUCCESSFUL on success
640 * PCIBIOS_DEVICE_NOT_FOUND on failure
642 static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
643 int where, int size, u32 *val)
645 struct hv_pcibus_device *hbus =
646 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
647 struct hv_pci_dev *hpdev;
649 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
651 return PCIBIOS_DEVICE_NOT_FOUND;
653 _hv_pcifront_read_config(hpdev, where, size, val);
655 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
656 return PCIBIOS_SUCCESSFUL;
660 * hv_pcifront_write_config() - Write configuration space
661 * @bus: PCI Bus structure
662 * @devfn: Device/function
663 * @where: Offset from base
664 * @size: Byte/word/dword
665 * @val: Value to be written to device
667 * Return: PCIBIOS_SUCCESSFUL on success
668 * PCIBIOS_DEVICE_NOT_FOUND on failure
670 static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
671 int where, int size, u32 val)
673 struct hv_pcibus_device *hbus =
674 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
675 struct hv_pci_dev *hpdev;
677 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
679 return PCIBIOS_DEVICE_NOT_FOUND;
681 _hv_pcifront_write_config(hpdev, where, size, val);
683 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
684 return PCIBIOS_SUCCESSFUL;
687 /* PCIe operations */
688 static struct pci_ops hv_pcifront_ops = {
689 .read = hv_pcifront_read_config,
690 .write = hv_pcifront_write_config,
693 /* Interrupt management hooks */
694 static void hv_int_desc_free(struct hv_pci_dev *hpdev,
695 struct tran_int_desc *int_desc)
697 struct pci_delete_interrupt *int_pkt;
699 struct pci_packet pkt;
700 u8 buffer[sizeof(struct pci_delete_interrupt)];
703 memset(&ctxt, 0, sizeof(ctxt));
704 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
705 int_pkt->message_type.type =
706 PCI_DELETE_INTERRUPT_MESSAGE;
707 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
708 int_pkt->int_desc = *int_desc;
709 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
710 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
715 * hv_msi_free() - Free the MSI.
716 * @domain: The interrupt domain pointer
717 * @info: Extra MSI-related context
718 * @irq: Identifies the IRQ.
720 * The Hyper-V parent partition and hypervisor are tracking the
721 * messages that are in use, keeping the interrupt redirection
722 * table up to date. This callback sends a message that frees
723 * the IRT entry and related tracking nonsense.
725 static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
728 struct hv_pcibus_device *hbus;
729 struct hv_pci_dev *hpdev;
730 struct pci_dev *pdev;
731 struct tran_int_desc *int_desc;
732 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
733 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
735 pdev = msi_desc_to_pci_dev(msi);
737 int_desc = irq_data_get_irq_chip_data(irq_data);
741 irq_data->chip_data = NULL;
742 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
748 hv_int_desc_free(hpdev, int_desc);
749 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
752 static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
755 struct irq_data *parent = data->parent_data;
757 return parent->chip->irq_set_affinity(parent, dest, force);
760 static void hv_irq_mask(struct irq_data *data)
762 pci_msi_mask_irq(data);
766 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
768 * @data: Describes the IRQ
770 * Build new a destination for the MSI and make a hypercall to
771 * update the Interrupt Redirection Table. "Device Logical ID"
772 * is built out of this PCI bus's instance GUID and the function
773 * number of the device.
775 static void hv_irq_unmask(struct irq_data *data)
777 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
778 struct irq_cfg *cfg = irqd_cfg(data);
779 struct retarget_msi_interrupt *params;
780 struct hv_pcibus_device *hbus;
781 struct cpumask *dest;
782 struct pci_bus *pbus;
783 struct pci_dev *pdev;
787 dest = irq_data_get_affinity_mask(data);
788 pdev = msi_desc_to_pci_dev(msi_desc);
790 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
792 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
794 params = &hbus->retarget_msi_interrupt_params;
795 memset(params, 0, sizeof(*params));
796 params->partition_id = HV_PARTITION_ID_SELF;
797 params->source = 1; /* MSI(-X) */
798 params->address = msi_desc->msg.address_lo;
799 params->data = msi_desc->msg.data;
800 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
801 (hbus->hdev->dev_instance.b[4] << 16) |
802 (hbus->hdev->dev_instance.b[7] << 8) |
803 (hbus->hdev->dev_instance.b[6] & 0xf8) |
804 PCI_FUNC(pdev->devfn);
805 params->vector = cfg->vector;
807 for_each_cpu_and(cpu, dest, cpu_online_mask)
808 params->vp_mask |= (1ULL << vmbus_cpu_number_to_vp_number(cpu));
810 hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, params, NULL);
812 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
814 pci_msi_unmask_irq(data);
817 struct compose_comp_ctxt {
818 struct hv_pci_compl comp_pkt;
819 struct tran_int_desc int_desc;
822 static void hv_pci_compose_compl(void *context, struct pci_response *resp,
823 int resp_packet_size)
825 struct compose_comp_ctxt *comp_pkt = context;
826 struct pci_create_int_response *int_resp =
827 (struct pci_create_int_response *)resp;
829 comp_pkt->comp_pkt.completion_status = resp->status;
830 comp_pkt->int_desc = int_resp->int_desc;
831 complete(&comp_pkt->comp_pkt.host_event);
835 * hv_compose_msi_msg() - Supplies a valid MSI address/data
836 * @data: Everything about this MSI
837 * @msg: Buffer that is filled in by this function
839 * This function unpacks the IRQ looking for target CPU set, IDT
840 * vector and mode and sends a message to the parent partition
841 * asking for a mapping for that tuple in this partition. The
842 * response supplies a data value and address to which that data
843 * should be written to trigger that interrupt.
845 static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
847 struct irq_cfg *cfg = irqd_cfg(data);
848 struct hv_pcibus_device *hbus;
849 struct hv_pci_dev *hpdev;
850 struct pci_bus *pbus;
851 struct pci_dev *pdev;
852 struct pci_create_interrupt *int_pkt;
853 struct compose_comp_ctxt comp;
854 struct tran_int_desc *int_desc;
855 struct cpumask *affinity;
857 struct pci_packet pkt;
858 u8 buffer[sizeof(struct pci_create_interrupt)];
863 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
865 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
866 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
868 goto return_null_message;
870 /* Free any previous message that might have already been composed. */
871 if (data->chip_data) {
872 int_desc = data->chip_data;
873 data->chip_data = NULL;
874 hv_int_desc_free(hpdev, int_desc);
877 int_desc = kzalloc(sizeof(*int_desc), GFP_KERNEL);
881 memset(&ctxt, 0, sizeof(ctxt));
882 init_completion(&comp.comp_pkt.host_event);
883 ctxt.pkt.completion_func = hv_pci_compose_compl;
884 ctxt.pkt.compl_ctxt = ∁
885 int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message;
886 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
887 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
888 int_pkt->int_desc.vector = cfg->vector;
889 int_pkt->int_desc.vector_count = 1;
890 int_pkt->int_desc.delivery_mode =
891 (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0;
894 * This bit doesn't have to work on machines with more than 64
895 * processors because Hyper-V only supports 64 in a guest.
897 affinity = irq_data_get_affinity_mask(data);
898 for_each_cpu_and(cpu, affinity, cpu_online_mask) {
899 int_pkt->int_desc.cpu_mask |=
900 (1ULL << vmbus_cpu_number_to_vp_number(cpu));
903 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt,
904 sizeof(*int_pkt), (unsigned long)&ctxt.pkt,
906 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
910 wait_for_completion(&comp.comp_pkt.host_event);
912 if (comp.comp_pkt.completion_status < 0) {
913 dev_err(&hbus->hdev->device,
914 "Request for interrupt failed: 0x%x",
915 comp.comp_pkt.completion_status);
920 * Record the assignment so that this can be unwound later. Using
921 * irq_set_chip_data() here would be appropriate, but the lock it takes
924 *int_desc = comp.int_desc;
925 data->chip_data = int_desc;
927 /* Pass up the result. */
928 msg->address_hi = comp.int_desc.address >> 32;
929 msg->address_lo = comp.int_desc.address & 0xffffffff;
930 msg->data = comp.int_desc.data;
932 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
938 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
945 /* HW Interrupt Chip Descriptor */
946 static struct irq_chip hv_msi_irq_chip = {
947 .name = "Hyper-V PCIe MSI",
948 .irq_compose_msi_msg = hv_compose_msi_msg,
949 .irq_set_affinity = hv_set_affinity,
950 .irq_ack = irq_chip_ack_parent,
951 .irq_mask = hv_irq_mask,
952 .irq_unmask = hv_irq_unmask,
955 static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
956 msi_alloc_info_t *arg)
958 return arg->msi_hwirq;
961 static struct msi_domain_ops hv_msi_ops = {
962 .get_hwirq = hv_msi_domain_ops_get_hwirq,
963 .msi_prepare = pci_msi_prepare,
964 .set_desc = pci_msi_set_desc,
965 .msi_free = hv_msi_free,
969 * hv_pcie_init_irq_domain() - Initialize IRQ domain
970 * @hbus: The root PCI bus
972 * This function creates an IRQ domain which will be used for
973 * interrupts from devices that have been passed through. These
974 * devices only support MSI and MSI-X, not line-based interrupts
975 * or simulations of line-based interrupts through PCIe's
976 * fabric-layer messages. Because interrupts are remapped, we
977 * can support multi-message MSI here.
979 * Return: '0' on success and error value on failure
981 static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
983 hbus->msi_info.chip = &hv_msi_irq_chip;
984 hbus->msi_info.ops = &hv_msi_ops;
985 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
986 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
988 hbus->msi_info.handler = handle_edge_irq;
989 hbus->msi_info.handler_name = "edge";
990 hbus->msi_info.data = hbus;
991 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
994 if (!hbus->irq_domain) {
995 dev_err(&hbus->hdev->device,
996 "Failed to build an MSI IRQ domain\n");
1004 * get_bar_size() - Get the address space consumed by a BAR
1005 * @bar_val: Value that a BAR returned after -1 was written
1008 * This function returns the size of the BAR, rounded up to 1
1009 * page. It has to be rounded up because the hypervisor's page
1010 * table entry that maps the BAR into the VM can't specify an
1011 * offset within a page. The invariant is that the hypervisor
1012 * must place any BARs of smaller than page length at the
1013 * beginning of a page.
1015 * Return: Size in bytes of the consumed MMIO space.
1017 static u64 get_bar_size(u64 bar_val)
1019 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1024 * survey_child_resources() - Total all MMIO requirements
1025 * @hbus: Root PCI bus, as understood by this driver
1027 static void survey_child_resources(struct hv_pcibus_device *hbus)
1029 struct list_head *iter;
1030 struct hv_pci_dev *hpdev;
1031 resource_size_t bar_size = 0;
1032 unsigned long flags;
1033 struct completion *event;
1037 /* If nobody is waiting on the answer, don't compute it. */
1038 event = xchg(&hbus->survey_event, NULL);
1042 /* If the answer has already been computed, go with it. */
1043 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1048 spin_lock_irqsave(&hbus->device_list_lock, flags);
1051 * Due to an interesting quirk of the PCI spec, all memory regions
1052 * for a child device are a power of 2 in size and aligned in memory,
1053 * so it's sufficient to just add them up without tracking alignment.
1055 list_for_each(iter, &hbus->children) {
1056 hpdev = container_of(iter, struct hv_pci_dev, list_entry);
1057 for (i = 0; i < 6; i++) {
1058 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1059 dev_err(&hbus->hdev->device,
1060 "There's an I/O BAR in this list!\n");
1062 if (hpdev->probed_bar[i] != 0) {
1064 * A probed BAR has all the upper bits set that
1068 bar_val = hpdev->probed_bar[i];
1069 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1071 ((u64)hpdev->probed_bar[++i] << 32);
1073 bar_val |= 0xffffffff00000000ULL;
1075 bar_size = get_bar_size(bar_val);
1077 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1078 hbus->high_mmio_space += bar_size;
1080 hbus->low_mmio_space += bar_size;
1085 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1090 * prepopulate_bars() - Fill in BARs with defaults
1091 * @hbus: Root PCI bus, as understood by this driver
1093 * The core PCI driver code seems much, much happier if the BARs
1094 * for a device have values upon first scan. So fill them in.
1095 * The algorithm below works down from large sizes to small,
1096 * attempting to pack the assignments optimally. The assumption,
1097 * enforced in other parts of the code, is that the beginning of
1098 * the memory-mapped I/O space will be aligned on the largest
1101 static void prepopulate_bars(struct hv_pcibus_device *hbus)
1103 resource_size_t high_size = 0;
1104 resource_size_t low_size = 0;
1105 resource_size_t high_base = 0;
1106 resource_size_t low_base = 0;
1107 resource_size_t bar_size;
1108 struct hv_pci_dev *hpdev;
1109 struct list_head *iter;
1110 unsigned long flags;
1116 if (hbus->low_mmio_space) {
1117 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1118 low_base = hbus->low_mmio_res->start;
1121 if (hbus->high_mmio_space) {
1123 (63 - __builtin_clzll(hbus->high_mmio_space));
1124 high_base = hbus->high_mmio_res->start;
1127 spin_lock_irqsave(&hbus->device_list_lock, flags);
1129 /* Pick addresses for the BARs. */
1131 list_for_each(iter, &hbus->children) {
1132 hpdev = container_of(iter, struct hv_pci_dev,
1134 for (i = 0; i < 6; i++) {
1135 bar_val = hpdev->probed_bar[i];
1138 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1141 ((u64)hpdev->probed_bar[i + 1]
1144 bar_val |= 0xffffffffULL << 32;
1146 bar_size = get_bar_size(bar_val);
1148 if (high_size != bar_size) {
1152 _hv_pcifront_write_config(hpdev,
1153 PCI_BASE_ADDRESS_0 + (4 * i),
1155 (u32)(high_base & 0xffffff00));
1157 _hv_pcifront_write_config(hpdev,
1158 PCI_BASE_ADDRESS_0 + (4 * i),
1159 4, (u32)(high_base >> 32));
1160 high_base += bar_size;
1162 if (low_size != bar_size)
1164 _hv_pcifront_write_config(hpdev,
1165 PCI_BASE_ADDRESS_0 + (4 * i),
1167 (u32)(low_base & 0xffffff00));
1168 low_base += bar_size;
1171 if (high_size <= 1 && low_size <= 1) {
1172 /* Set the memory enable bit. */
1173 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1175 command |= PCI_COMMAND_MEMORY;
1176 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1184 } while (high_size || low_size);
1186 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1190 * create_root_hv_pci_bus() - Expose a new root PCI bus
1191 * @hbus: Root PCI bus, as understood by this driver
1193 * Return: 0 on success, -errno on failure
1195 static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1197 /* Register the device */
1198 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1199 0, /* bus number is always zero */
1202 &hbus->resources_for_children);
1206 hbus->pci_bus->msi = &hbus->msi_chip;
1207 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1209 pci_scan_child_bus(hbus->pci_bus);
1210 pci_bus_assign_resources(hbus->pci_bus);
1211 pci_bus_add_devices(hbus->pci_bus);
1212 hbus->state = hv_pcibus_installed;
1216 struct q_res_req_compl {
1217 struct completion host_event;
1218 struct hv_pci_dev *hpdev;
1222 * q_resource_requirements() - Query Resource Requirements
1223 * @context: The completion context.
1224 * @resp: The response that came from the host.
1225 * @resp_packet_size: The size in bytes of resp.
1227 * This function is invoked on completion of a Query Resource
1228 * Requirements packet.
1230 static void q_resource_requirements(void *context, struct pci_response *resp,
1231 int resp_packet_size)
1233 struct q_res_req_compl *completion = context;
1234 struct pci_q_res_req_response *q_res_req =
1235 (struct pci_q_res_req_response *)resp;
1238 if (resp->status < 0) {
1239 dev_err(&completion->hpdev->hbus->hdev->device,
1240 "query resource requirements failed: %x\n",
1243 for (i = 0; i < 6; i++) {
1244 completion->hpdev->probed_bar[i] =
1245 q_res_req->probed_bar[i];
1249 complete(&completion->host_event);
1252 static void get_pcichild(struct hv_pci_dev *hpdev,
1253 enum hv_pcidev_ref_reason reason)
1255 atomic_inc(&hpdev->refs);
1258 static void put_pcichild(struct hv_pci_dev *hpdev,
1259 enum hv_pcidev_ref_reason reason)
1261 if (atomic_dec_and_test(&hpdev->refs))
1266 * new_pcichild_device() - Create a new child device
1267 * @hbus: The internal struct tracking this root PCI bus.
1268 * @desc: The information supplied so far from the host
1271 * This function creates the tracking structure for a new child
1272 * device and kicks off the process of figuring out what it is.
1274 * Return: Pointer to the new tracking struct
1276 static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1277 struct pci_function_description *desc)
1279 struct hv_pci_dev *hpdev;
1280 struct pci_child_message *res_req;
1281 struct q_res_req_compl comp_pkt;
1283 struct pci_packet init_packet;
1284 u8 buffer[sizeof(struct pci_child_message)];
1286 unsigned long flags;
1289 hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
1295 memset(&pkt, 0, sizeof(pkt));
1296 init_completion(&comp_pkt.host_event);
1297 comp_pkt.hpdev = hpdev;
1298 pkt.init_packet.compl_ctxt = &comp_pkt;
1299 pkt.init_packet.completion_func = q_resource_requirements;
1300 res_req = (struct pci_child_message *)&pkt.init_packet.message;
1301 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
1302 res_req->wslot.slot = desc->win_slot.slot;
1304 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1305 sizeof(struct pci_child_message),
1306 (unsigned long)&pkt.init_packet,
1308 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1312 wait_for_completion(&comp_pkt.host_event);
1314 hpdev->desc = *desc;
1315 get_pcichild(hpdev, hv_pcidev_ref_initial);
1316 get_pcichild(hpdev, hv_pcidev_ref_childlist);
1317 spin_lock_irqsave(&hbus->device_list_lock, flags);
1318 list_add_tail(&hpdev->list_entry, &hbus->children);
1319 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1328 * get_pcichild_wslot() - Find device from slot
1329 * @hbus: Root PCI bus, as understood by this driver
1330 * @wslot: Location on the bus
1332 * This function looks up a PCI device and returns the internal
1333 * representation of it. It acquires a reference on it, so that
1334 * the device won't be deleted while somebody is using it. The
1335 * caller is responsible for calling put_pcichild() to release
1338 * Return: Internal representation of a PCI device
1340 static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1343 unsigned long flags;
1344 struct hv_pci_dev *iter, *hpdev = NULL;
1346 spin_lock_irqsave(&hbus->device_list_lock, flags);
1347 list_for_each_entry(iter, &hbus->children, list_entry) {
1348 if (iter->desc.win_slot.slot == wslot) {
1350 get_pcichild(hpdev, hv_pcidev_ref_by_slot);
1354 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1360 * pci_devices_present_work() - Handle new list of child devices
1361 * @work: Work struct embedded in struct hv_dr_work
1363 * "Bus Relations" is the Windows term for "children of this
1364 * bus." The terminology is preserved here for people trying to
1365 * debug the interaction between Hyper-V and Linux. This
1366 * function is called when the parent partition reports a list
1367 * of functions that should be observed under this PCI Express
1370 * This function updates the list, and must tolerate being
1371 * called multiple times with the same information. The typical
1372 * number of child devices is one, with very atypical cases
1373 * involving three or four, so the algorithms used here can be
1374 * simple and inefficient.
1376 * It must also treat the omission of a previously observed device as
1377 * notification that the device no longer exists.
1379 * Note that this function is a work item, and it may not be
1380 * invoked in the order that it was queued. Back to back
1381 * updates of the list of present devices may involve queuing
1382 * multiple work items, and this one may run before ones that
1383 * were sent later. As such, this function only does something
1384 * if is the last one in the queue.
1386 static void pci_devices_present_work(struct work_struct *work)
1390 struct list_head *iter;
1391 struct pci_function_description *new_desc;
1392 struct hv_pci_dev *hpdev;
1393 struct hv_pcibus_device *hbus;
1394 struct list_head removed;
1395 struct hv_dr_work *dr_wrk;
1396 struct hv_dr_state *dr = NULL;
1397 unsigned long flags;
1399 dr_wrk = container_of(work, struct hv_dr_work, wrk);
1403 INIT_LIST_HEAD(&removed);
1405 if (down_interruptible(&hbus->enum_sem)) {
1410 /* Pull this off the queue and process it if it was the last one. */
1411 spin_lock_irqsave(&hbus->device_list_lock, flags);
1412 while (!list_empty(&hbus->dr_list)) {
1413 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
1415 list_del(&dr->list_entry);
1417 /* Throw this away if the list still has stuff in it. */
1418 if (!list_empty(&hbus->dr_list)) {
1423 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1426 up(&hbus->enum_sem);
1431 /* First, mark all existing children as reported missing. */
1432 spin_lock_irqsave(&hbus->device_list_lock, flags);
1433 list_for_each(iter, &hbus->children) {
1434 hpdev = container_of(iter, struct hv_pci_dev,
1436 hpdev->reported_missing = true;
1438 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1440 /* Next, add back any reported devices. */
1441 for (child_no = 0; child_no < dr->device_count; child_no++) {
1443 new_desc = &dr->func[child_no];
1445 spin_lock_irqsave(&hbus->device_list_lock, flags);
1446 list_for_each(iter, &hbus->children) {
1447 hpdev = container_of(iter, struct hv_pci_dev,
1449 if ((hpdev->desc.win_slot.slot ==
1450 new_desc->win_slot.slot) &&
1451 (hpdev->desc.v_id == new_desc->v_id) &&
1452 (hpdev->desc.d_id == new_desc->d_id) &&
1453 (hpdev->desc.ser == new_desc->ser)) {
1454 hpdev->reported_missing = false;
1458 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1461 hpdev = new_pcichild_device(hbus, new_desc);
1463 dev_err(&hbus->hdev->device,
1464 "couldn't record a child device.\n");
1468 /* Move missing children to a list on the stack. */
1469 spin_lock_irqsave(&hbus->device_list_lock, flags);
1472 list_for_each(iter, &hbus->children) {
1473 hpdev = container_of(iter, struct hv_pci_dev,
1475 if (hpdev->reported_missing) {
1477 put_pcichild(hpdev, hv_pcidev_ref_childlist);
1478 list_move_tail(&hpdev->list_entry, &removed);
1483 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1485 /* Delete everything that should no longer exist. */
1486 while (!list_empty(&removed)) {
1487 hpdev = list_first_entry(&removed, struct hv_pci_dev,
1489 list_del(&hpdev->list_entry);
1490 put_pcichild(hpdev, hv_pcidev_ref_initial);
1493 /* Tell the core to rescan bus because there may have been changes. */
1494 if (hbus->state == hv_pcibus_installed) {
1495 pci_lock_rescan_remove();
1496 pci_scan_child_bus(hbus->pci_bus);
1497 pci_unlock_rescan_remove();
1499 survey_child_resources(hbus);
1502 up(&hbus->enum_sem);
1508 * hv_pci_devices_present() - Handles list of new children
1509 * @hbus: Root PCI bus, as understood by this driver
1510 * @relations: Packet from host listing children
1512 * This function is invoked whenever a new list of devices for
1515 static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
1516 struct pci_bus_relations *relations)
1518 struct hv_dr_state *dr;
1519 struct hv_dr_work *dr_wrk;
1520 unsigned long flags;
1522 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
1526 dr = kzalloc(offsetof(struct hv_dr_state, func) +
1527 (sizeof(struct pci_function_description) *
1528 (relations->device_count)), GFP_NOWAIT);
1534 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
1536 dr->device_count = relations->device_count;
1537 if (dr->device_count != 0) {
1538 memcpy(dr->func, relations->func,
1539 sizeof(struct pci_function_description) *
1543 spin_lock_irqsave(&hbus->device_list_lock, flags);
1544 list_add_tail(&dr->list_entry, &hbus->dr_list);
1545 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1548 schedule_work(&dr_wrk->wrk);
1552 * hv_eject_device_work() - Asynchronously handles ejection
1553 * @work: Work struct embedded in internal device struct
1555 * This function handles ejecting a device. Windows will
1556 * attempt to gracefully eject a device, waiting 60 seconds to
1557 * hear back from the guest OS that this completed successfully.
1558 * If this timer expires, the device will be forcibly removed.
1560 static void hv_eject_device_work(struct work_struct *work)
1562 struct pci_eject_response *ejct_pkt;
1563 struct hv_pci_dev *hpdev;
1564 struct pci_dev *pdev;
1565 unsigned long flags;
1568 struct pci_packet pkt;
1569 u8 buffer[sizeof(struct pci_eject_response)];
1572 hpdev = container_of(work, struct hv_pci_dev, wrk);
1574 if (hpdev->state != hv_pcichild_ejecting) {
1575 put_pcichild(hpdev, hv_pcidev_ref_pnp);
1580 * Ejection can come before or after the PCI bus has been set up, so
1581 * attempt to find it and tear down the bus state, if it exists. This
1582 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1583 * because hbus->pci_bus may not exist yet.
1585 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
1586 pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
1589 pci_stop_and_remove_bus_device(pdev);
1593 spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
1594 list_del(&hpdev->list_entry);
1595 spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
1597 memset(&ctxt, 0, sizeof(ctxt));
1598 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
1599 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
1600 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1601 vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
1602 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
1603 VM_PKT_DATA_INBAND, 0);
1605 put_pcichild(hpdev, hv_pcidev_ref_childlist);
1606 put_pcichild(hpdev, hv_pcidev_ref_pnp);
1607 put_hvpcibus(hpdev->hbus);
1611 * hv_pci_eject_device() - Handles device ejection
1612 * @hpdev: Internal device tracking struct
1614 * This function is invoked when an ejection packet arrives. It
1615 * just schedules work so that we don't re-enter the packet
1616 * delivery code handling the ejection.
1618 static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
1620 hpdev->state = hv_pcichild_ejecting;
1621 get_pcichild(hpdev, hv_pcidev_ref_pnp);
1622 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
1623 get_hvpcibus(hpdev->hbus);
1624 schedule_work(&hpdev->wrk);
1628 * hv_pci_onchannelcallback() - Handles incoming packets
1629 * @context: Internal bus tracking struct
1631 * This function is invoked whenever the host sends a packet to
1632 * this channel (which is private to this root PCI bus).
1634 static void hv_pci_onchannelcallback(void *context)
1636 const int packet_size = 0x100;
1638 struct hv_pcibus_device *hbus = context;
1641 struct vmpacket_descriptor *desc;
1642 unsigned char *buffer;
1643 int bufferlen = packet_size;
1644 struct pci_packet *comp_packet;
1645 struct pci_response *response;
1646 struct pci_incoming_message *new_message;
1647 struct pci_bus_relations *bus_rel;
1648 struct pci_dev_incoming *dev_message;
1649 struct hv_pci_dev *hpdev;
1651 buffer = kmalloc(bufferlen, GFP_ATOMIC);
1656 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
1657 bufferlen, &bytes_recvd, &req_id);
1659 if (ret == -ENOBUFS) {
1661 /* Handle large packet */
1662 bufferlen = bytes_recvd;
1663 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1669 /* Zero length indicates there are no more packets. */
1670 if (ret || !bytes_recvd)
1674 * All incoming packets must be at least as large as a
1677 if (bytes_recvd <= sizeof(struct pci_response))
1679 desc = (struct vmpacket_descriptor *)buffer;
1681 switch (desc->type) {
1685 * The host is trusted, and thus it's safe to interpret
1686 * this transaction ID as a pointer.
1688 comp_packet = (struct pci_packet *)req_id;
1689 response = (struct pci_response *)buffer;
1690 comp_packet->completion_func(comp_packet->compl_ctxt,
1695 case VM_PKT_DATA_INBAND:
1697 new_message = (struct pci_incoming_message *)buffer;
1698 switch (new_message->message_type.type) {
1699 case PCI_BUS_RELATIONS:
1701 bus_rel = (struct pci_bus_relations *)buffer;
1703 offsetof(struct pci_bus_relations, func) +
1704 (sizeof(struct pci_function_description) *
1705 (bus_rel->device_count))) {
1706 dev_err(&hbus->hdev->device,
1707 "bus relations too small\n");
1711 hv_pci_devices_present(hbus, bus_rel);
1716 dev_message = (struct pci_dev_incoming *)buffer;
1717 hpdev = get_pcichild_wslot(hbus,
1718 dev_message->wslot.slot);
1720 hv_pci_eject_device(hpdev);
1722 hv_pcidev_ref_by_slot);
1727 dev_warn(&hbus->hdev->device,
1728 "Unimplemented protocol message %x\n",
1729 new_message->message_type.type);
1735 dev_err(&hbus->hdev->device,
1736 "unhandled packet type %d, tid %llx len %d\n",
1737 desc->type, req_id, bytes_recvd);
1746 * hv_pci_protocol_negotiation() - Set up protocol
1747 * @hdev: VMBus's tracking struct for this root PCI bus
1749 * This driver is intended to support running on Windows 10
1750 * (server) and later versions. It will not run on earlier
1751 * versions, as they assume that many of the operations which
1752 * Linux needs accomplished with a spinlock held were done via
1753 * asynchronous messaging via VMBus. Windows 10 increases the
1754 * surface area of PCI emulation so that these actions can take
1755 * place by suspending a virtual processor for their duration.
1757 * This function negotiates the channel protocol version,
1758 * failing if the host doesn't support the necessary protocol
1761 static int hv_pci_protocol_negotiation(struct hv_device *hdev)
1763 struct pci_version_request *version_req;
1764 struct hv_pci_compl comp_pkt;
1765 struct pci_packet *pkt;
1769 * Initiate the handshake with the host and negotiate
1770 * a version that the host can support. We start with the
1771 * highest version number and go down if the host cannot
1774 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
1778 init_completion(&comp_pkt.host_event);
1779 pkt->completion_func = hv_pci_generic_compl;
1780 pkt->compl_ctxt = &comp_pkt;
1781 version_req = (struct pci_version_request *)&pkt->message;
1782 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
1783 version_req->protocol_version = PCI_PROTOCOL_VERSION_CURRENT;
1785 ret = vmbus_sendpacket(hdev->channel, version_req,
1786 sizeof(struct pci_version_request),
1787 (unsigned long)pkt, VM_PKT_DATA_INBAND,
1788 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1792 wait_for_completion(&comp_pkt.host_event);
1794 if (comp_pkt.completion_status < 0) {
1795 dev_err(&hdev->device,
1796 "PCI Pass-through VSP failed version request %x\n",
1797 comp_pkt.completion_status);
1810 * hv_pci_free_bridge_windows() - Release memory regions for the
1812 * @hbus: Root PCI bus, as understood by this driver
1814 static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
1817 * Set the resources back to the way they looked when they
1818 * were allocated by setting IORESOURCE_BUSY again.
1821 if (hbus->low_mmio_space && hbus->low_mmio_res) {
1822 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
1823 vmbus_free_mmio(hbus->low_mmio_res->start,
1824 resource_size(hbus->low_mmio_res));
1827 if (hbus->high_mmio_space && hbus->high_mmio_res) {
1828 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
1829 vmbus_free_mmio(hbus->high_mmio_res->start,
1830 resource_size(hbus->high_mmio_res));
1835 * hv_pci_allocate_bridge_windows() - Allocate memory regions
1837 * @hbus: Root PCI bus, as understood by this driver
1839 * This function calls vmbus_allocate_mmio(), which is itself a
1840 * bit of a compromise. Ideally, we might change the pnp layer
1841 * in the kernel such that it comprehends either PCI devices
1842 * which are "grandchildren of ACPI," with some intermediate bus
1843 * node (in this case, VMBus) or change it such that it
1844 * understands VMBus. The pnp layer, however, has been declared
1845 * deprecated, and not subject to change.
1847 * The workaround, implemented here, is to ask VMBus to allocate
1848 * MMIO space for this bus. VMBus itself knows which ranges are
1849 * appropriate by looking at its own ACPI objects. Then, after
1850 * these ranges are claimed, they're modified to look like they
1851 * would have looked if the ACPI and pnp code had allocated
1852 * bridge windows. These descriptors have to exist in this form
1853 * in order to satisfy the code which will get invoked when the
1854 * endpoint PCI function driver calls request_mem_region() or
1855 * request_mem_region_exclusive().
1857 * Return: 0 on success, -errno on failure
1859 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
1861 resource_size_t align;
1864 if (hbus->low_mmio_space) {
1865 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1866 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
1867 (u64)(u32)0xffffffff,
1868 hbus->low_mmio_space,
1871 dev_err(&hbus->hdev->device,
1872 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
1873 hbus->low_mmio_space);
1877 /* Modify this resource to become a bridge window. */
1878 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
1879 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
1880 pci_add_resource(&hbus->resources_for_children,
1881 hbus->low_mmio_res);
1884 if (hbus->high_mmio_space) {
1885 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
1886 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
1888 hbus->high_mmio_space, align,
1891 dev_err(&hbus->hdev->device,
1892 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
1893 hbus->high_mmio_space);
1894 goto release_low_mmio;
1897 /* Modify this resource to become a bridge window. */
1898 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
1899 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
1900 pci_add_resource(&hbus->resources_for_children,
1901 hbus->high_mmio_res);
1907 if (hbus->low_mmio_res) {
1908 vmbus_free_mmio(hbus->low_mmio_res->start,
1909 resource_size(hbus->low_mmio_res));
1916 * hv_allocate_config_window() - Find MMIO space for PCI Config
1917 * @hbus: Root PCI bus, as understood by this driver
1919 * This function claims memory-mapped I/O space for accessing
1920 * configuration space for the functions on this bus.
1922 * Return: 0 on success, -errno on failure
1924 static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
1929 * Set up a region of MMIO space to use for accessing configuration
1932 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
1933 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
1938 * vmbus_allocate_mmio() gets used for allocating both device endpoint
1939 * resource claims (those which cannot be overlapped) and the ranges
1940 * which are valid for the children of this bus, which are intended
1941 * to be overlapped by those children. Set the flag on this claim
1942 * meaning that this region can't be overlapped.
1945 hbus->mem_config->flags |= IORESOURCE_BUSY;
1950 static void hv_free_config_window(struct hv_pcibus_device *hbus)
1952 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
1956 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
1957 * @hdev: VMBus's tracking struct for this root PCI bus
1959 * Return: 0 on success, -errno on failure
1961 static int hv_pci_enter_d0(struct hv_device *hdev)
1963 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
1964 struct pci_bus_d0_entry *d0_entry;
1965 struct hv_pci_compl comp_pkt;
1966 struct pci_packet *pkt;
1970 * Tell the host that the bus is ready to use, and moved into the
1971 * powered-on state. This includes telling the host which region
1972 * of memory-mapped I/O space has been chosen for configuration space
1975 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
1979 init_completion(&comp_pkt.host_event);
1980 pkt->completion_func = hv_pci_generic_compl;
1981 pkt->compl_ctxt = &comp_pkt;
1982 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
1983 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
1984 d0_entry->mmio_base = hbus->mem_config->start;
1986 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
1987 (unsigned long)pkt, VM_PKT_DATA_INBAND,
1988 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1992 wait_for_completion(&comp_pkt.host_event);
1994 if (comp_pkt.completion_status < 0) {
1995 dev_err(&hdev->device,
1996 "PCI Pass-through VSP failed D0 Entry with status %x\n",
1997 comp_pkt.completion_status);
2010 * hv_pci_query_relations() - Ask host to send list of child
2012 * @hdev: VMBus's tracking struct for this root PCI bus
2014 * Return: 0 on success, -errno on failure
2016 static int hv_pci_query_relations(struct hv_device *hdev)
2018 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2019 struct pci_message message;
2020 struct completion comp;
2023 /* Ask the host to send along the list of child devices */
2024 init_completion(&comp);
2025 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2028 memset(&message, 0, sizeof(message));
2029 message.type = PCI_QUERY_BUS_RELATIONS;
2031 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2032 0, VM_PKT_DATA_INBAND, 0);
2036 wait_for_completion(&comp);
2041 * hv_send_resources_allocated() - Report local resource choices
2042 * @hdev: VMBus's tracking struct for this root PCI bus
2044 * The host OS is expecting to be sent a request as a message
2045 * which contains all the resources that the device will use.
2046 * The response contains those same resources, "translated"
2047 * which is to say, the values which should be used by the
2048 * hardware, when it delivers an interrupt. (MMIO resources are
2049 * used in local terms.) This is nice for Windows, and lines up
2050 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2051 * is deeply expecting to scan an emulated PCI configuration
2052 * space. So this message is sent here only to drive the state
2053 * machine on the host forward.
2055 * Return: 0 on success, -errno on failure
2057 static int hv_send_resources_allocated(struct hv_device *hdev)
2059 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2060 struct pci_resources_assigned *res_assigned;
2061 struct hv_pci_compl comp_pkt;
2062 struct hv_pci_dev *hpdev;
2063 struct pci_packet *pkt;
2067 pkt = kmalloc(sizeof(*pkt) + sizeof(*res_assigned), GFP_KERNEL);
2073 for (wslot = 0; wslot < 256; wslot++) {
2074 hpdev = get_pcichild_wslot(hbus, wslot);
2078 memset(pkt, 0, sizeof(*pkt) + sizeof(*res_assigned));
2079 init_completion(&comp_pkt.host_event);
2080 pkt->completion_func = hv_pci_generic_compl;
2081 pkt->compl_ctxt = &comp_pkt;
2082 res_assigned = (struct pci_resources_assigned *)&pkt->message;
2083 res_assigned->message_type.type = PCI_RESOURCES_ASSIGNED;
2084 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2086 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
2088 ret = vmbus_sendpacket(
2089 hdev->channel, &pkt->message,
2090 sizeof(*res_assigned),
2093 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2097 wait_for_completion(&comp_pkt.host_event);
2099 if (comp_pkt.completion_status < 0) {
2101 dev_err(&hdev->device,
2102 "resource allocated returned 0x%x",
2103 comp_pkt.completion_status);
2113 * hv_send_resources_released() - Report local resources
2115 * @hdev: VMBus's tracking struct for this root PCI bus
2117 * Return: 0 on success, -errno on failure
2119 static int hv_send_resources_released(struct hv_device *hdev)
2121 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2122 struct pci_child_message pkt;
2123 struct hv_pci_dev *hpdev;
2127 for (wslot = 0; wslot < 256; wslot++) {
2128 hpdev = get_pcichild_wslot(hbus, wslot);
2132 memset(&pkt, 0, sizeof(pkt));
2133 pkt.message_type.type = PCI_RESOURCES_RELEASED;
2134 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2136 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
2138 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2139 VM_PKT_DATA_INBAND, 0);
2147 static void get_hvpcibus(struct hv_pcibus_device *hbus)
2149 atomic_inc(&hbus->remove_lock);
2152 static void put_hvpcibus(struct hv_pcibus_device *hbus)
2154 if (atomic_dec_and_test(&hbus->remove_lock))
2155 complete(&hbus->remove_event);
2159 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2160 * @hdev: VMBus's tracking struct for this root PCI bus
2161 * @dev_id: Identifies the device itself
2163 * Return: 0 on success, -errno on failure
2165 static int hv_pci_probe(struct hv_device *hdev,
2166 const struct hv_vmbus_device_id *dev_id)
2168 struct hv_pcibus_device *hbus;
2171 hbus = kzalloc(sizeof(*hbus), GFP_KERNEL);
2176 * The PCI bus "domain" is what is called "segment" in ACPI and
2177 * other specs. Pull it from the instance ID, to get something
2178 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2179 * do the same thing for consistency. Note that, since this code
2180 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2181 * that (1) the only domain in use for something that looks like
2182 * a physical PCI bus (which is actually emulated by the
2183 * hypervisor) is domain 0 and (2) there will be no overlap
2184 * between domains derived from these instance IDs in the same
2187 hbus->sysdata.domain = hdev->dev_instance.b[9] |
2188 hdev->dev_instance.b[8] << 8;
2191 atomic_inc(&hbus->remove_lock);
2192 INIT_LIST_HEAD(&hbus->children);
2193 INIT_LIST_HEAD(&hbus->dr_list);
2194 INIT_LIST_HEAD(&hbus->resources_for_children);
2195 spin_lock_init(&hbus->config_lock);
2196 spin_lock_init(&hbus->device_list_lock);
2197 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
2198 sema_init(&hbus->enum_sem, 1);
2199 init_completion(&hbus->remove_event);
2201 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
2202 hv_pci_onchannelcallback, hbus);
2206 hv_set_drvdata(hdev, hbus);
2208 ret = hv_pci_protocol_negotiation(hdev);
2212 ret = hv_allocate_config_window(hbus);
2216 hbus->cfg_addr = ioremap(hbus->mem_config->start,
2217 PCI_CONFIG_MMIO_LENGTH);
2218 if (!hbus->cfg_addr) {
2219 dev_err(&hdev->device,
2220 "Unable to map a virtual address for config space\n");
2225 hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
2226 if (!hbus->sysdata.fwnode) {
2231 ret = hv_pcie_init_irq_domain(hbus);
2235 ret = hv_pci_query_relations(hdev);
2237 goto free_irq_domain;
2239 ret = hv_pci_enter_d0(hdev);
2241 goto free_irq_domain;
2243 ret = hv_pci_allocate_bridge_windows(hbus);
2245 goto free_irq_domain;
2247 ret = hv_send_resources_allocated(hdev);
2251 prepopulate_bars(hbus);
2253 hbus->state = hv_pcibus_probed;
2255 ret = create_root_hv_pci_bus(hbus);
2262 hv_pci_free_bridge_windows(hbus);
2264 irq_domain_remove(hbus->irq_domain);
2266 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2268 iounmap(hbus->cfg_addr);
2270 hv_free_config_window(hbus);
2272 vmbus_close(hdev->channel);
2278 static void hv_pci_bus_exit(struct hv_device *hdev)
2280 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2282 struct pci_packet teardown_packet;
2283 u8 buffer[sizeof(struct pci_message)];
2285 struct pci_bus_relations relations;
2286 struct hv_pci_compl comp_pkt;
2290 * After the host sends the RESCIND_CHANNEL message, it doesn't
2291 * access the per-channel ringbuffer any longer.
2293 if (hdev->channel->rescind)
2296 /* Delete any children which might still exist. */
2297 memset(&relations, 0, sizeof(relations));
2298 hv_pci_devices_present(hbus, &relations);
2300 ret = hv_send_resources_released(hdev);
2302 dev_err(&hdev->device,
2303 "Couldn't send resources released packet(s)\n");
2305 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
2306 init_completion(&comp_pkt.host_event);
2307 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
2308 pkt.teardown_packet.compl_ctxt = &comp_pkt;
2309 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
2311 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
2312 sizeof(struct pci_message),
2313 (unsigned long)&pkt.teardown_packet,
2315 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2317 wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
2321 * hv_pci_remove() - Remove routine for this VMBus channel
2322 * @hdev: VMBus's tracking struct for this root PCI bus
2324 * Return: 0 on success, -errno on failure
2326 static int hv_pci_remove(struct hv_device *hdev)
2328 struct hv_pcibus_device *hbus;
2330 hbus = hv_get_drvdata(hdev);
2331 if (hbus->state == hv_pcibus_installed) {
2332 /* Remove the bus from PCI's point of view. */
2333 pci_lock_rescan_remove();
2334 pci_stop_root_bus(hbus->pci_bus);
2335 pci_remove_root_bus(hbus->pci_bus);
2336 pci_unlock_rescan_remove();
2339 hv_pci_bus_exit(hdev);
2341 vmbus_close(hdev->channel);
2343 iounmap(hbus->cfg_addr);
2344 hv_free_config_window(hbus);
2345 pci_free_resource_list(&hbus->resources_for_children);
2346 hv_pci_free_bridge_windows(hbus);
2347 irq_domain_remove(hbus->irq_domain);
2348 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2350 wait_for_completion(&hbus->remove_event);
2355 static const struct hv_vmbus_device_id hv_pci_id_table[] = {
2356 /* PCI Pass-through Class ID */
2357 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2362 MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
2364 static struct hv_driver hv_pci_drv = {
2366 .id_table = hv_pci_id_table,
2367 .probe = hv_pci_probe,
2368 .remove = hv_pci_remove,
2371 static void __exit exit_hv_pci_drv(void)
2373 vmbus_driver_unregister(&hv_pci_drv);
2376 static int __init init_hv_pci_drv(void)
2378 return vmbus_driver_register(&hv_pci_drv);
2381 module_init(init_hv_pci_drv);
2382 module_exit(exit_hv_pci_drv);
2384 MODULE_DESCRIPTION("Hyper-V PCI");
2385 MODULE_LICENSE("GPL v2");