2 * Copyright (c) Microsoft Corporation.
5 * Jake Oshins <jakeo@microsoft.com>
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/delay.h>
54 #include <linux/semaphore.h>
55 #include <linux/irqdomain.h>
56 #include <asm/irqdomain.h>
58 #include <linux/msi.h>
59 #include <linux/hyperv.h>
60 #include <linux/refcount.h>
61 #include <asm/mshyperv.h>
64 * Protocol versions. The low word is the minor version, the high word the
68 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
69 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
70 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
72 enum pci_protocol_version_t
{
73 PCI_PROTOCOL_VERSION_1_1
= PCI_MAKE_VERSION(1, 1), /* Win10 */
74 PCI_PROTOCOL_VERSION_1_2
= PCI_MAKE_VERSION(1, 2), /* RS1 */
77 #define CPU_AFFINITY_ALL -1ULL
80 * Supported protocol versions in the order of probing - highest go
83 static enum pci_protocol_version_t pci_protocol_versions
[] = {
84 PCI_PROTOCOL_VERSION_1_2
,
85 PCI_PROTOCOL_VERSION_1_1
,
89 * Protocol version negotiated by hv_pci_protocol_negotiation().
91 static enum pci_protocol_version_t pci_protocol_version
;
93 #define PCI_CONFIG_MMIO_LENGTH 0x2000
94 #define CFG_PAGE_OFFSET 0x1000
95 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
97 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
99 #define STATUS_REVISION_MISMATCH 0xC0000059
105 enum pci_message_type
{
109 PCI_MESSAGE_BASE
= 0x42490000,
110 PCI_BUS_RELATIONS
= PCI_MESSAGE_BASE
+ 0,
111 PCI_QUERY_BUS_RELATIONS
= PCI_MESSAGE_BASE
+ 1,
112 PCI_POWER_STATE_CHANGE
= PCI_MESSAGE_BASE
+ 4,
113 PCI_QUERY_RESOURCE_REQUIREMENTS
= PCI_MESSAGE_BASE
+ 5,
114 PCI_QUERY_RESOURCE_RESOURCES
= PCI_MESSAGE_BASE
+ 6,
115 PCI_BUS_D0ENTRY
= PCI_MESSAGE_BASE
+ 7,
116 PCI_BUS_D0EXIT
= PCI_MESSAGE_BASE
+ 8,
117 PCI_READ_BLOCK
= PCI_MESSAGE_BASE
+ 9,
118 PCI_WRITE_BLOCK
= PCI_MESSAGE_BASE
+ 0xA,
119 PCI_EJECT
= PCI_MESSAGE_BASE
+ 0xB,
120 PCI_QUERY_STOP
= PCI_MESSAGE_BASE
+ 0xC,
121 PCI_REENABLE
= PCI_MESSAGE_BASE
+ 0xD,
122 PCI_QUERY_STOP_FAILED
= PCI_MESSAGE_BASE
+ 0xE,
123 PCI_EJECTION_COMPLETE
= PCI_MESSAGE_BASE
+ 0xF,
124 PCI_RESOURCES_ASSIGNED
= PCI_MESSAGE_BASE
+ 0x10,
125 PCI_RESOURCES_RELEASED
= PCI_MESSAGE_BASE
+ 0x11,
126 PCI_INVALIDATE_BLOCK
= PCI_MESSAGE_BASE
+ 0x12,
127 PCI_QUERY_PROTOCOL_VERSION
= PCI_MESSAGE_BASE
+ 0x13,
128 PCI_CREATE_INTERRUPT_MESSAGE
= PCI_MESSAGE_BASE
+ 0x14,
129 PCI_DELETE_INTERRUPT_MESSAGE
= PCI_MESSAGE_BASE
+ 0x15,
130 PCI_RESOURCES_ASSIGNED2
= PCI_MESSAGE_BASE
+ 0x16,
131 PCI_CREATE_INTERRUPT_MESSAGE2
= PCI_MESSAGE_BASE
+ 0x17,
132 PCI_DELETE_INTERRUPT_MESSAGE2
= PCI_MESSAGE_BASE
+ 0x18, /* unused */
137 * Structures defining the virtual PCI Express protocol.
149 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
150 * which is all this driver does. This representation is the one used in
151 * Windows, which is what is expected when sending this back and forth with
152 * the Hyper-V parent partition.
154 union win_slot_encoding
{
164 * Pretty much as defined in the PCI Specifications.
166 struct pci_function_description
{
167 u16 v_id
; /* vendor ID */
168 u16 d_id
; /* device ID */
174 union win_slot_encoding win_slot
;
175 u32 ser
; /* serial number */
181 * @delivery_mode: As defined in Intel's Programmer's
182 * Reference Manual, Volume 3, Chapter 8.
183 * @vector_count: Number of contiguous entries in the
184 * Interrupt Descriptor Table that are
185 * occupied by this Message-Signaled
186 * Interrupt. For "MSI", as first defined
187 * in PCI 2.2, this can be between 1 and
188 * 32. For "MSI-X," as first defined in PCI
189 * 3.0, this must be 1, as each MSI-X table
190 * entry would have its own descriptor.
191 * @reserved: Empty space
192 * @cpu_mask: All the target virtual processors.
203 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
205 * @delivery_mode: As defined in Intel's Programmer's
206 * Reference Manual, Volume 3, Chapter 8.
207 * @vector_count: Number of contiguous entries in the
208 * Interrupt Descriptor Table that are
209 * occupied by this Message-Signaled
210 * Interrupt. For "MSI", as first defined
211 * in PCI 2.2, this can be between 1 and
212 * 32. For "MSI-X," as first defined in PCI
213 * 3.0, this must be 1, as each MSI-X table
214 * entry would have its own descriptor.
215 * @processor_count: number of bits enabled in array.
216 * @processor_array: All the target virtual processors.
218 struct hv_msi_desc2
{
223 u16 processor_array
[32];
227 * struct tran_int_desc
228 * @reserved: unused, padding
229 * @vector_count: same as in hv_msi_desc
230 * @data: This is the "data payload" value that is
231 * written by the device when it generates
232 * a message-signaled interrupt, either MSI
234 * @address: This is the address to which the data
235 * payload is written on interrupt
238 struct tran_int_desc
{
246 * A generic message format for virtual PCI.
247 * Specific message formats are defined later in the file.
254 struct pci_child_message
{
255 struct pci_message message_type
;
256 union win_slot_encoding wslot
;
259 struct pci_incoming_message
{
260 struct vmpacket_descriptor hdr
;
261 struct pci_message message_type
;
264 struct pci_response
{
265 struct vmpacket_descriptor hdr
;
266 s32 status
; /* negative values are failures */
270 void (*completion_func
)(void *context
, struct pci_response
*resp
,
271 int resp_packet_size
);
274 struct pci_message message
[0];
278 * Specific message types supporting the PCI protocol.
282 * Version negotiation message. Sent from the guest to the host.
283 * The guest is free to try different versions until the host
284 * accepts the version.
286 * pci_version: The protocol version requested.
287 * is_last_attempt: If TRUE, this is the last version guest will request.
288 * reservedz: Reserved field, set to zero.
291 struct pci_version_request
{
292 struct pci_message message_type
;
293 u32 protocol_version
;
297 * Bus D0 Entry. This is sent from the guest to the host when the virtual
298 * bus (PCI Express port) is ready for action.
301 struct pci_bus_d0_entry
{
302 struct pci_message message_type
;
307 struct pci_bus_relations
{
308 struct pci_incoming_message incoming
;
310 struct pci_function_description func
[0];
313 struct pci_q_res_req_response
{
314 struct vmpacket_descriptor hdr
;
315 s32 status
; /* negative values are failures */
319 struct pci_set_power
{
320 struct pci_message message_type
;
321 union win_slot_encoding wslot
;
322 u32 power_state
; /* In Windows terms */
326 struct pci_set_power_response
{
327 struct vmpacket_descriptor hdr
;
328 s32 status
; /* negative values are failures */
329 union win_slot_encoding wslot
;
330 u32 resultant_state
; /* In Windows terms */
334 struct pci_resources_assigned
{
335 struct pci_message message_type
;
336 union win_slot_encoding wslot
;
337 u8 memory_range
[0x14][6]; /* not used here */
342 struct pci_resources_assigned2
{
343 struct pci_message message_type
;
344 union win_slot_encoding wslot
;
345 u8 memory_range
[0x14][6]; /* not used here */
346 u32 msi_descriptor_count
;
350 struct pci_create_interrupt
{
351 struct pci_message message_type
;
352 union win_slot_encoding wslot
;
353 struct hv_msi_desc int_desc
;
356 struct pci_create_int_response
{
357 struct pci_response response
;
359 struct tran_int_desc int_desc
;
362 struct pci_create_interrupt2
{
363 struct pci_message message_type
;
364 union win_slot_encoding wslot
;
365 struct hv_msi_desc2 int_desc
;
368 struct pci_delete_interrupt
{
369 struct pci_message message_type
;
370 union win_slot_encoding wslot
;
371 struct tran_int_desc int_desc
;
374 struct pci_dev_incoming
{
375 struct pci_incoming_message incoming
;
376 union win_slot_encoding wslot
;
379 struct pci_eject_response
{
380 struct pci_message message_type
;
381 union win_slot_encoding wslot
;
385 static int pci_ring_size
= (4 * PAGE_SIZE
);
388 * Definitions or interrupt steering hypercall.
390 #define HV_PARTITION_ID_SELF ((u64)-1)
391 #define HVCALL_RETARGET_INTERRUPT 0x7e
393 struct hv_interrupt_entry
{
394 u32 source
; /* 1 for MSI(-X) */
400 #define HV_VP_SET_BANK_COUNT_MAX 5 /* current implementation limit */
403 u64 format
; /* 0 (HvGenericSetSparse4k) */
405 u64 masks
[HV_VP_SET_BANK_COUNT_MAX
];
409 * flags for hv_device_interrupt_target.flags
411 #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
412 #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
414 struct hv_device_interrupt_target
{
419 struct hv_vp_set vp_set
;
423 struct retarget_msi_interrupt
{
424 u64 partition_id
; /* use "self" */
426 struct hv_interrupt_entry int_entry
;
428 struct hv_device_interrupt_target int_target
;
432 * Driver specific state.
435 enum hv_pcibus_state
{
443 struct hv_pcibus_device
{
444 struct pci_sysdata sysdata
;
445 enum hv_pcibus_state state
;
446 atomic_t remove_lock
;
447 struct hv_device
*hdev
;
448 resource_size_t low_mmio_space
;
449 resource_size_t high_mmio_space
;
450 struct resource
*mem_config
;
451 struct resource
*low_mmio_res
;
452 struct resource
*high_mmio_res
;
453 struct completion
*survey_event
;
454 struct completion remove_event
;
455 struct pci_bus
*pci_bus
;
456 spinlock_t config_lock
; /* Avoid two threads writing index page */
457 spinlock_t device_list_lock
; /* Protect lists below */
458 void __iomem
*cfg_addr
;
460 struct semaphore enum_sem
;
461 struct list_head resources_for_children
;
463 struct list_head children
;
464 struct list_head dr_list
;
466 struct msi_domain_info msi_info
;
467 struct msi_controller msi_chip
;
468 struct irq_domain
*irq_domain
;
470 /* hypercall arg, must not cross page boundary */
471 struct retarget_msi_interrupt retarget_msi_interrupt_params
;
473 spinlock_t retarget_msi_interrupt_lock
;
477 * Tracks "Device Relations" messages from the host, which must be both
478 * processed in order and deferred so that they don't run in the context
479 * of the incoming packet callback.
482 struct work_struct wrk
;
483 struct hv_pcibus_device
*bus
;
487 struct list_head list_entry
;
489 struct pci_function_description func
[0];
492 enum hv_pcichild_state
{
493 hv_pcichild_init
= 0,
494 hv_pcichild_requirements
,
495 hv_pcichild_resourced
,
496 hv_pcichild_ejecting
,
500 enum hv_pcidev_ref_reason
{
501 hv_pcidev_ref_invalid
= 0,
502 hv_pcidev_ref_initial
,
503 hv_pcidev_ref_by_slot
,
504 hv_pcidev_ref_packet
,
506 hv_pcidev_ref_childlist
,
512 /* List protected by pci_rescan_remove_lock */
513 struct list_head list_entry
;
515 enum hv_pcichild_state state
;
516 struct pci_function_description desc
;
517 bool reported_missing
;
518 struct hv_pcibus_device
*hbus
;
519 struct work_struct wrk
;
522 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
523 * read it back, for each of the BAR offsets within config space.
528 struct hv_pci_compl
{
529 struct completion host_event
;
530 s32 completion_status
;
534 * hv_pci_generic_compl() - Invoked for a completion packet
535 * @context: Set up by the sender of the packet.
536 * @resp: The response packet
537 * @resp_packet_size: Size in bytes of the packet
539 * This function is used to trigger an event and report status
540 * for any message for which the completion packet contains a
541 * status and nothing else.
543 static void hv_pci_generic_compl(void *context
, struct pci_response
*resp
,
544 int resp_packet_size
)
546 struct hv_pci_compl
*comp_pkt
= context
;
548 if (resp_packet_size
>= offsetofend(struct pci_response
, status
))
549 comp_pkt
->completion_status
= resp
->status
;
551 comp_pkt
->completion_status
= -1;
553 complete(&comp_pkt
->host_event
);
556 static struct hv_pci_dev
*get_pcichild_wslot(struct hv_pcibus_device
*hbus
,
558 static void get_pcichild(struct hv_pci_dev
*hv_pcidev
,
559 enum hv_pcidev_ref_reason reason
);
560 static void put_pcichild(struct hv_pci_dev
*hv_pcidev
,
561 enum hv_pcidev_ref_reason reason
);
563 static void get_hvpcibus(struct hv_pcibus_device
*hv_pcibus
);
564 static void put_hvpcibus(struct hv_pcibus_device
*hv_pcibus
);
567 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
568 * @devfn: The Linux representation of PCI slot
570 * Windows uses a slightly different representation of PCI slot.
572 * Return: The Windows representation
574 static u32
devfn_to_wslot(int devfn
)
576 union win_slot_encoding wslot
;
579 wslot
.bits
.dev
= PCI_SLOT(devfn
);
580 wslot
.bits
.func
= PCI_FUNC(devfn
);
586 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
587 * @wslot: The Windows representation of PCI slot
589 * Windows uses a slightly different representation of PCI slot.
591 * Return: The Linux representation
593 static int wslot_to_devfn(u32 wslot
)
595 union win_slot_encoding slot_no
;
597 slot_no
.slot
= wslot
;
598 return PCI_DEVFN(slot_no
.bits
.dev
, slot_no
.bits
.func
);
602 * PCI Configuration Space for these root PCI buses is implemented as a pair
603 * of pages in memory-mapped I/O space. Writing to the first page chooses
604 * the PCI function being written or read. Once the first page has been
605 * written to, the following page maps in the entire configuration space of
610 * _hv_pcifront_read_config() - Internal PCI config read
611 * @hpdev: The PCI driver's representation of the device
612 * @where: Offset within config space
613 * @size: Size of the transfer
614 * @val: Pointer to the buffer receiving the data
616 static void _hv_pcifront_read_config(struct hv_pci_dev
*hpdev
, int where
,
620 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+ where
;
623 * If the attempt is to read the IDs or the ROM BAR, simulate that.
625 if (where
+ size
<= PCI_COMMAND
) {
626 memcpy(val
, ((u8
*)&hpdev
->desc
.v_id
) + where
, size
);
627 } else if (where
>= PCI_CLASS_REVISION
&& where
+ size
<=
628 PCI_CACHE_LINE_SIZE
) {
629 memcpy(val
, ((u8
*)&hpdev
->desc
.rev
) + where
-
630 PCI_CLASS_REVISION
, size
);
631 } else if (where
>= PCI_SUBSYSTEM_VENDOR_ID
&& where
+ size
<=
633 memcpy(val
, (u8
*)&hpdev
->desc
.subsystem_id
+ where
-
634 PCI_SUBSYSTEM_VENDOR_ID
, size
);
635 } else if (where
>= PCI_ROM_ADDRESS
&& where
+ size
<=
636 PCI_CAPABILITY_LIST
) {
637 /* ROM BARs are unimplemented */
639 } else if (where
>= PCI_INTERRUPT_LINE
&& where
+ size
<=
642 * Interrupt Line and Interrupt PIN are hard-wired to zero
643 * because this front-end only supports message-signaled
647 } else if (where
+ size
<= CFG_PAGE_SIZE
) {
648 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
649 /* Choose the function to be read. (See comment above) */
650 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
651 /* Make sure the function was chosen before we start reading. */
653 /* Read from that function's config space. */
666 * Make sure the write was done before we release the spinlock
667 * allowing consecutive reads/writes.
670 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
672 dev_err(&hpdev
->hbus
->hdev
->device
,
673 "Attempt to read beyond a function's config space.\n");
678 * _hv_pcifront_write_config() - Internal PCI config write
679 * @hpdev: The PCI driver's representation of the device
680 * @where: Offset within config space
681 * @size: Size of the transfer
682 * @val: The data being transferred
684 static void _hv_pcifront_write_config(struct hv_pci_dev
*hpdev
, int where
,
688 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+ where
;
690 if (where
>= PCI_SUBSYSTEM_VENDOR_ID
&&
691 where
+ size
<= PCI_CAPABILITY_LIST
) {
692 /* SSIDs and ROM BARs are read-only */
693 } else if (where
>= PCI_COMMAND
&& where
+ size
<= CFG_PAGE_SIZE
) {
694 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
695 /* Choose the function to be written. (See comment above) */
696 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
697 /* Make sure the function was chosen before we start writing. */
699 /* Write to that function's config space. */
712 * Make sure the write was done before we release the spinlock
713 * allowing consecutive reads/writes.
716 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
718 dev_err(&hpdev
->hbus
->hdev
->device
,
719 "Attempt to write beyond a function's config space.\n");
724 * hv_pcifront_read_config() - Read configuration space
725 * @bus: PCI Bus structure
726 * @devfn: Device/function
727 * @where: Offset from base
728 * @size: Byte/word/dword
729 * @val: Value to be read
731 * Return: PCIBIOS_SUCCESSFUL on success
732 * PCIBIOS_DEVICE_NOT_FOUND on failure
734 static int hv_pcifront_read_config(struct pci_bus
*bus
, unsigned int devfn
,
735 int where
, int size
, u32
*val
)
737 struct hv_pcibus_device
*hbus
=
738 container_of(bus
->sysdata
, struct hv_pcibus_device
, sysdata
);
739 struct hv_pci_dev
*hpdev
;
741 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(devfn
));
743 return PCIBIOS_DEVICE_NOT_FOUND
;
745 _hv_pcifront_read_config(hpdev
, where
, size
, val
);
747 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
748 return PCIBIOS_SUCCESSFUL
;
752 * hv_pcifront_write_config() - Write configuration space
753 * @bus: PCI Bus structure
754 * @devfn: Device/function
755 * @where: Offset from base
756 * @size: Byte/word/dword
757 * @val: Value to be written to device
759 * Return: PCIBIOS_SUCCESSFUL on success
760 * PCIBIOS_DEVICE_NOT_FOUND on failure
762 static int hv_pcifront_write_config(struct pci_bus
*bus
, unsigned int devfn
,
763 int where
, int size
, u32 val
)
765 struct hv_pcibus_device
*hbus
=
766 container_of(bus
->sysdata
, struct hv_pcibus_device
, sysdata
);
767 struct hv_pci_dev
*hpdev
;
769 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(devfn
));
771 return PCIBIOS_DEVICE_NOT_FOUND
;
773 _hv_pcifront_write_config(hpdev
, where
, size
, val
);
775 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
776 return PCIBIOS_SUCCESSFUL
;
779 /* PCIe operations */
780 static struct pci_ops hv_pcifront_ops
= {
781 .read
= hv_pcifront_read_config
,
782 .write
= hv_pcifront_write_config
,
785 /* Interrupt management hooks */
786 static void hv_int_desc_free(struct hv_pci_dev
*hpdev
,
787 struct tran_int_desc
*int_desc
)
789 struct pci_delete_interrupt
*int_pkt
;
791 struct pci_packet pkt
;
792 u8 buffer
[sizeof(struct pci_delete_interrupt
)];
795 memset(&ctxt
, 0, sizeof(ctxt
));
796 int_pkt
= (struct pci_delete_interrupt
*)&ctxt
.pkt
.message
;
797 int_pkt
->message_type
.type
=
798 PCI_DELETE_INTERRUPT_MESSAGE
;
799 int_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
800 int_pkt
->int_desc
= *int_desc
;
801 vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, int_pkt
, sizeof(*int_pkt
),
802 (unsigned long)&ctxt
.pkt
, VM_PKT_DATA_INBAND
, 0);
807 * hv_msi_free() - Free the MSI.
808 * @domain: The interrupt domain pointer
809 * @info: Extra MSI-related context
810 * @irq: Identifies the IRQ.
812 * The Hyper-V parent partition and hypervisor are tracking the
813 * messages that are in use, keeping the interrupt redirection
814 * table up to date. This callback sends a message that frees
815 * the IRT entry and related tracking nonsense.
817 static void hv_msi_free(struct irq_domain
*domain
, struct msi_domain_info
*info
,
820 struct hv_pcibus_device
*hbus
;
821 struct hv_pci_dev
*hpdev
;
822 struct pci_dev
*pdev
;
823 struct tran_int_desc
*int_desc
;
824 struct irq_data
*irq_data
= irq_domain_get_irq_data(domain
, irq
);
825 struct msi_desc
*msi
= irq_data_get_msi_desc(irq_data
);
827 pdev
= msi_desc_to_pci_dev(msi
);
829 int_desc
= irq_data_get_irq_chip_data(irq_data
);
833 irq_data
->chip_data
= NULL
;
834 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(pdev
->devfn
));
840 hv_int_desc_free(hpdev
, int_desc
);
841 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
844 static int hv_set_affinity(struct irq_data
*data
, const struct cpumask
*dest
,
847 struct irq_data
*parent
= data
->parent_data
;
849 return parent
->chip
->irq_set_affinity(parent
, dest
, force
);
852 static void hv_irq_mask(struct irq_data
*data
)
854 pci_msi_mask_irq(data
);
858 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
860 * @data: Describes the IRQ
862 * Build new a destination for the MSI and make a hypercall to
863 * update the Interrupt Redirection Table. "Device Logical ID"
864 * is built out of this PCI bus's instance GUID and the function
865 * number of the device.
867 static void hv_irq_unmask(struct irq_data
*data
)
869 struct msi_desc
*msi_desc
= irq_data_get_msi_desc(data
);
870 struct irq_cfg
*cfg
= irqd_cfg(data
);
871 struct retarget_msi_interrupt
*params
;
872 struct hv_pcibus_device
*hbus
;
873 struct cpumask
*dest
;
874 struct pci_bus
*pbus
;
875 struct pci_dev
*pdev
;
882 dest
= irq_data_get_affinity_mask(data
);
883 pdev
= msi_desc_to_pci_dev(msi_desc
);
885 hbus
= container_of(pbus
->sysdata
, struct hv_pcibus_device
, sysdata
);
887 spin_lock_irqsave(&hbus
->retarget_msi_interrupt_lock
, flags
);
889 params
= &hbus
->retarget_msi_interrupt_params
;
890 memset(params
, 0, sizeof(*params
));
891 params
->partition_id
= HV_PARTITION_ID_SELF
;
892 params
->int_entry
.source
= 1; /* MSI(-X) */
893 params
->int_entry
.address
= msi_desc
->msg
.address_lo
;
894 params
->int_entry
.data
= msi_desc
->msg
.data
;
895 params
->device_id
= (hbus
->hdev
->dev_instance
.b
[5] << 24) |
896 (hbus
->hdev
->dev_instance
.b
[4] << 16) |
897 (hbus
->hdev
->dev_instance
.b
[7] << 8) |
898 (hbus
->hdev
->dev_instance
.b
[6] & 0xf8) |
899 PCI_FUNC(pdev
->devfn
);
900 params
->int_target
.vector
= cfg
->vector
;
903 * Honoring apic->irq_delivery_mode set to dest_Fixed by
904 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
905 * spurious interrupt storm. Not doing so does not seem to have a
906 * negative effect (yet?).
909 if (pci_protocol_version
>= PCI_PROTOCOL_VERSION_1_2
) {
911 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
912 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
913 * with >64 VP support.
914 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
915 * is not sufficient for this hypercall.
917 params
->int_target
.flags
|=
918 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET
;
919 params
->int_target
.vp_set
.valid_banks
=
920 (1ull << HV_VP_SET_BANK_COUNT_MAX
) - 1;
923 * var-sized hypercall, var-size starts after vp_mask (thus
924 * vp_set.format does not count, but vp_set.valid_banks does).
926 var_size
= 1 + HV_VP_SET_BANK_COUNT_MAX
;
928 for_each_cpu_and(cpu
, dest
, cpu_online_mask
) {
929 cpu_vmbus
= hv_cpu_number_to_vp_number(cpu
);
931 if (cpu_vmbus
>= HV_VP_SET_BANK_COUNT_MAX
* 64) {
932 dev_err(&hbus
->hdev
->device
,
933 "too high CPU %d", cpu_vmbus
);
938 params
->int_target
.vp_set
.masks
[cpu_vmbus
/ 64] |=
939 (1ULL << (cpu_vmbus
& 63));
942 for_each_cpu_and(cpu
, dest
, cpu_online_mask
) {
943 params
->int_target
.vp_mask
|=
944 (1ULL << hv_cpu_number_to_vp_number(cpu
));
948 res
= hv_do_hypercall(HVCALL_RETARGET_INTERRUPT
| (var_size
<< 17),
952 spin_unlock_irqrestore(&hbus
->retarget_msi_interrupt_lock
, flags
);
955 dev_err(&hbus
->hdev
->device
,
956 "%s() failed: %#llx", __func__
, res
);
960 pci_msi_unmask_irq(data
);
963 struct compose_comp_ctxt
{
964 struct hv_pci_compl comp_pkt
;
965 struct tran_int_desc int_desc
;
968 static void hv_pci_compose_compl(void *context
, struct pci_response
*resp
,
969 int resp_packet_size
)
971 struct compose_comp_ctxt
*comp_pkt
= context
;
972 struct pci_create_int_response
*int_resp
=
973 (struct pci_create_int_response
*)resp
;
975 comp_pkt
->comp_pkt
.completion_status
= resp
->status
;
976 comp_pkt
->int_desc
= int_resp
->int_desc
;
977 complete(&comp_pkt
->comp_pkt
.host_event
);
980 static u32
hv_compose_msi_req_v1(
981 struct pci_create_interrupt
*int_pkt
, struct cpumask
*affinity
,
984 int_pkt
->message_type
.type
= PCI_CREATE_INTERRUPT_MESSAGE
;
985 int_pkt
->wslot
.slot
= slot
;
986 int_pkt
->int_desc
.vector
= vector
;
987 int_pkt
->int_desc
.vector_count
= 1;
988 int_pkt
->int_desc
.delivery_mode
= dest_Fixed
;
991 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
994 int_pkt
->int_desc
.cpu_mask
= CPU_AFFINITY_ALL
;
996 return sizeof(*int_pkt
);
999 static u32
hv_compose_msi_req_v2(
1000 struct pci_create_interrupt2
*int_pkt
, struct cpumask
*affinity
,
1001 u32 slot
, u8 vector
)
1005 int_pkt
->message_type
.type
= PCI_CREATE_INTERRUPT_MESSAGE2
;
1006 int_pkt
->wslot
.slot
= slot
;
1007 int_pkt
->int_desc
.vector
= vector
;
1008 int_pkt
->int_desc
.vector_count
= 1;
1009 int_pkt
->int_desc
.delivery_mode
= dest_Fixed
;
1012 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1013 * by subsequent retarget in hv_irq_unmask().
1015 cpu
= cpumask_first_and(affinity
, cpu_online_mask
);
1016 int_pkt
->int_desc
.processor_array
[0] =
1017 hv_cpu_number_to_vp_number(cpu
);
1018 int_pkt
->int_desc
.processor_count
= 1;
1020 return sizeof(*int_pkt
);
1024 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1025 * @data: Everything about this MSI
1026 * @msg: Buffer that is filled in by this function
1028 * This function unpacks the IRQ looking for target CPU set, IDT
1029 * vector and mode and sends a message to the parent partition
1030 * asking for a mapping for that tuple in this partition. The
1031 * response supplies a data value and address to which that data
1032 * should be written to trigger that interrupt.
1034 static void hv_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
1036 struct irq_cfg
*cfg
= irqd_cfg(data
);
1037 struct hv_pcibus_device
*hbus
;
1038 struct hv_pci_dev
*hpdev
;
1039 struct pci_bus
*pbus
;
1040 struct pci_dev
*pdev
;
1041 struct compose_comp_ctxt comp
;
1042 struct tran_int_desc
*int_desc
;
1044 struct pci_packet pci_pkt
;
1046 struct pci_create_interrupt v1
;
1047 struct pci_create_interrupt2 v2
;
1054 pdev
= msi_desc_to_pci_dev(irq_data_get_msi_desc(data
));
1056 hbus
= container_of(pbus
->sysdata
, struct hv_pcibus_device
, sysdata
);
1057 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(pdev
->devfn
));
1059 goto return_null_message
;
1061 /* Free any previous message that might have already been composed. */
1062 if (data
->chip_data
) {
1063 int_desc
= data
->chip_data
;
1064 data
->chip_data
= NULL
;
1065 hv_int_desc_free(hpdev
, int_desc
);
1068 int_desc
= kzalloc(sizeof(*int_desc
), GFP_ATOMIC
);
1070 goto drop_reference
;
1072 memset(&ctxt
, 0, sizeof(ctxt
));
1073 init_completion(&comp
.comp_pkt
.host_event
);
1074 ctxt
.pci_pkt
.completion_func
= hv_pci_compose_compl
;
1075 ctxt
.pci_pkt
.compl_ctxt
= &comp
;
1077 switch (pci_protocol_version
) {
1078 case PCI_PROTOCOL_VERSION_1_1
:
1079 size
= hv_compose_msi_req_v1(&ctxt
.int_pkts
.v1
,
1080 irq_data_get_affinity_mask(data
),
1081 hpdev
->desc
.win_slot
.slot
,
1085 case PCI_PROTOCOL_VERSION_1_2
:
1086 size
= hv_compose_msi_req_v2(&ctxt
.int_pkts
.v2
,
1087 irq_data_get_affinity_mask(data
),
1088 hpdev
->desc
.win_slot
.slot
,
1093 /* As we only negotiate protocol versions known to this driver,
1094 * this path should never hit. However, this is it not a hot
1095 * path so we print a message to aid future updates.
1097 dev_err(&hbus
->hdev
->device
,
1098 "Unexpected vPCI protocol, update driver.");
1102 ret
= vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, &ctxt
.int_pkts
,
1103 size
, (unsigned long)&ctxt
.pci_pkt
,
1105 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1107 dev_err(&hbus
->hdev
->device
,
1108 "Sending request for interrupt failed: 0x%x",
1109 comp
.comp_pkt
.completion_status
);
1114 * Since this function is called with IRQ locks held, can't
1115 * do normal wait for completion; instead poll.
1117 while (!try_wait_for_completion(&comp
.comp_pkt
.host_event
))
1120 if (comp
.comp_pkt
.completion_status
< 0) {
1121 dev_err(&hbus
->hdev
->device
,
1122 "Request for interrupt failed: 0x%x",
1123 comp
.comp_pkt
.completion_status
);
1128 * Record the assignment so that this can be unwound later. Using
1129 * irq_set_chip_data() here would be appropriate, but the lock it takes
1132 *int_desc
= comp
.int_desc
;
1133 data
->chip_data
= int_desc
;
1135 /* Pass up the result. */
1136 msg
->address_hi
= comp
.int_desc
.address
>> 32;
1137 msg
->address_lo
= comp
.int_desc
.address
& 0xffffffff;
1138 msg
->data
= comp
.int_desc
.data
;
1140 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
1146 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
1147 return_null_message
:
1148 msg
->address_hi
= 0;
1149 msg
->address_lo
= 0;
1153 /* HW Interrupt Chip Descriptor */
1154 static struct irq_chip hv_msi_irq_chip
= {
1155 .name
= "Hyper-V PCIe MSI",
1156 .irq_compose_msi_msg
= hv_compose_msi_msg
,
1157 .irq_set_affinity
= hv_set_affinity
,
1158 .irq_ack
= irq_chip_ack_parent
,
1159 .irq_mask
= hv_irq_mask
,
1160 .irq_unmask
= hv_irq_unmask
,
1163 static irq_hw_number_t
hv_msi_domain_ops_get_hwirq(struct msi_domain_info
*info
,
1164 msi_alloc_info_t
*arg
)
1166 return arg
->msi_hwirq
;
1169 static struct msi_domain_ops hv_msi_ops
= {
1170 .get_hwirq
= hv_msi_domain_ops_get_hwirq
,
1171 .msi_prepare
= pci_msi_prepare
,
1172 .set_desc
= pci_msi_set_desc
,
1173 .msi_free
= hv_msi_free
,
1177 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1178 * @hbus: The root PCI bus
1180 * This function creates an IRQ domain which will be used for
1181 * interrupts from devices that have been passed through. These
1182 * devices only support MSI and MSI-X, not line-based interrupts
1183 * or simulations of line-based interrupts through PCIe's
1184 * fabric-layer messages. Because interrupts are remapped, we
1185 * can support multi-message MSI here.
1187 * Return: '0' on success and error value on failure
1189 static int hv_pcie_init_irq_domain(struct hv_pcibus_device
*hbus
)
1191 hbus
->msi_info
.chip
= &hv_msi_irq_chip
;
1192 hbus
->msi_info
.ops
= &hv_msi_ops
;
1193 hbus
->msi_info
.flags
= (MSI_FLAG_USE_DEF_DOM_OPS
|
1194 MSI_FLAG_USE_DEF_CHIP_OPS
| MSI_FLAG_MULTI_PCI_MSI
|
1196 hbus
->msi_info
.handler
= handle_edge_irq
;
1197 hbus
->msi_info
.handler_name
= "edge";
1198 hbus
->msi_info
.data
= hbus
;
1199 hbus
->irq_domain
= pci_msi_create_irq_domain(hbus
->sysdata
.fwnode
,
1202 if (!hbus
->irq_domain
) {
1203 dev_err(&hbus
->hdev
->device
,
1204 "Failed to build an MSI IRQ domain\n");
1212 * get_bar_size() - Get the address space consumed by a BAR
1213 * @bar_val: Value that a BAR returned after -1 was written
1216 * This function returns the size of the BAR, rounded up to 1
1217 * page. It has to be rounded up because the hypervisor's page
1218 * table entry that maps the BAR into the VM can't specify an
1219 * offset within a page. The invariant is that the hypervisor
1220 * must place any BARs of smaller than page length at the
1221 * beginning of a page.
1223 * Return: Size in bytes of the consumed MMIO space.
1225 static u64
get_bar_size(u64 bar_val
)
1227 return round_up((1 + ~(bar_val
& PCI_BASE_ADDRESS_MEM_MASK
)),
1232 * survey_child_resources() - Total all MMIO requirements
1233 * @hbus: Root PCI bus, as understood by this driver
1235 static void survey_child_resources(struct hv_pcibus_device
*hbus
)
1237 struct list_head
*iter
;
1238 struct hv_pci_dev
*hpdev
;
1239 resource_size_t bar_size
= 0;
1240 unsigned long flags
;
1241 struct completion
*event
;
1245 /* If nobody is waiting on the answer, don't compute it. */
1246 event
= xchg(&hbus
->survey_event
, NULL
);
1250 /* If the answer has already been computed, go with it. */
1251 if (hbus
->low_mmio_space
|| hbus
->high_mmio_space
) {
1256 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1259 * Due to an interesting quirk of the PCI spec, all memory regions
1260 * for a child device are a power of 2 in size and aligned in memory,
1261 * so it's sufficient to just add them up without tracking alignment.
1263 list_for_each(iter
, &hbus
->children
) {
1264 hpdev
= container_of(iter
, struct hv_pci_dev
, list_entry
);
1265 for (i
= 0; i
< 6; i
++) {
1266 if (hpdev
->probed_bar
[i
] & PCI_BASE_ADDRESS_SPACE_IO
)
1267 dev_err(&hbus
->hdev
->device
,
1268 "There's an I/O BAR in this list!\n");
1270 if (hpdev
->probed_bar
[i
] != 0) {
1272 * A probed BAR has all the upper bits set that
1276 bar_val
= hpdev
->probed_bar
[i
];
1277 if (bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
1279 ((u64
)hpdev
->probed_bar
[++i
] << 32);
1281 bar_val
|= 0xffffffff00000000ULL
;
1283 bar_size
= get_bar_size(bar_val
);
1285 if (bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
1286 hbus
->high_mmio_space
+= bar_size
;
1288 hbus
->low_mmio_space
+= bar_size
;
1293 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1298 * prepopulate_bars() - Fill in BARs with defaults
1299 * @hbus: Root PCI bus, as understood by this driver
1301 * The core PCI driver code seems much, much happier if the BARs
1302 * for a device have values upon first scan. So fill them in.
1303 * The algorithm below works down from large sizes to small,
1304 * attempting to pack the assignments optimally. The assumption,
1305 * enforced in other parts of the code, is that the beginning of
1306 * the memory-mapped I/O space will be aligned on the largest
1309 static void prepopulate_bars(struct hv_pcibus_device
*hbus
)
1311 resource_size_t high_size
= 0;
1312 resource_size_t low_size
= 0;
1313 resource_size_t high_base
= 0;
1314 resource_size_t low_base
= 0;
1315 resource_size_t bar_size
;
1316 struct hv_pci_dev
*hpdev
;
1317 struct list_head
*iter
;
1318 unsigned long flags
;
1324 if (hbus
->low_mmio_space
) {
1325 low_size
= 1ULL << (63 - __builtin_clzll(hbus
->low_mmio_space
));
1326 low_base
= hbus
->low_mmio_res
->start
;
1329 if (hbus
->high_mmio_space
) {
1331 (63 - __builtin_clzll(hbus
->high_mmio_space
));
1332 high_base
= hbus
->high_mmio_res
->start
;
1335 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1337 /* Pick addresses for the BARs. */
1339 list_for_each(iter
, &hbus
->children
) {
1340 hpdev
= container_of(iter
, struct hv_pci_dev
,
1342 for (i
= 0; i
< 6; i
++) {
1343 bar_val
= hpdev
->probed_bar
[i
];
1346 high
= bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
;
1349 ((u64
)hpdev
->probed_bar
[i
+ 1]
1352 bar_val
|= 0xffffffffULL
<< 32;
1354 bar_size
= get_bar_size(bar_val
);
1356 if (high_size
!= bar_size
) {
1360 _hv_pcifront_write_config(hpdev
,
1361 PCI_BASE_ADDRESS_0
+ (4 * i
),
1363 (u32
)(high_base
& 0xffffff00));
1365 _hv_pcifront_write_config(hpdev
,
1366 PCI_BASE_ADDRESS_0
+ (4 * i
),
1367 4, (u32
)(high_base
>> 32));
1368 high_base
+= bar_size
;
1370 if (low_size
!= bar_size
)
1372 _hv_pcifront_write_config(hpdev
,
1373 PCI_BASE_ADDRESS_0
+ (4 * i
),
1375 (u32
)(low_base
& 0xffffff00));
1376 low_base
+= bar_size
;
1379 if (high_size
<= 1 && low_size
<= 1) {
1380 /* Set the memory enable bit. */
1381 _hv_pcifront_read_config(hpdev
, PCI_COMMAND
, 2,
1383 command
|= PCI_COMMAND_MEMORY
;
1384 _hv_pcifront_write_config(hpdev
, PCI_COMMAND
, 2,
1392 } while (high_size
|| low_size
);
1394 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1398 * create_root_hv_pci_bus() - Expose a new root PCI bus
1399 * @hbus: Root PCI bus, as understood by this driver
1401 * Return: 0 on success, -errno on failure
1403 static int create_root_hv_pci_bus(struct hv_pcibus_device
*hbus
)
1405 /* Register the device */
1406 hbus
->pci_bus
= pci_create_root_bus(&hbus
->hdev
->device
,
1407 0, /* bus number is always zero */
1410 &hbus
->resources_for_children
);
1414 hbus
->pci_bus
->msi
= &hbus
->msi_chip
;
1415 hbus
->pci_bus
->msi
->dev
= &hbus
->hdev
->device
;
1417 pci_lock_rescan_remove();
1418 pci_scan_child_bus(hbus
->pci_bus
);
1419 pci_bus_assign_resources(hbus
->pci_bus
);
1420 pci_bus_add_devices(hbus
->pci_bus
);
1421 pci_unlock_rescan_remove();
1422 hbus
->state
= hv_pcibus_installed
;
1426 struct q_res_req_compl
{
1427 struct completion host_event
;
1428 struct hv_pci_dev
*hpdev
;
1432 * q_resource_requirements() - Query Resource Requirements
1433 * @context: The completion context.
1434 * @resp: The response that came from the host.
1435 * @resp_packet_size: The size in bytes of resp.
1437 * This function is invoked on completion of a Query Resource
1438 * Requirements packet.
1440 static void q_resource_requirements(void *context
, struct pci_response
*resp
,
1441 int resp_packet_size
)
1443 struct q_res_req_compl
*completion
= context
;
1444 struct pci_q_res_req_response
*q_res_req
=
1445 (struct pci_q_res_req_response
*)resp
;
1448 if (resp
->status
< 0) {
1449 dev_err(&completion
->hpdev
->hbus
->hdev
->device
,
1450 "query resource requirements failed: %x\n",
1453 for (i
= 0; i
< 6; i
++) {
1454 completion
->hpdev
->probed_bar
[i
] =
1455 q_res_req
->probed_bar
[i
];
1459 complete(&completion
->host_event
);
1462 static void get_pcichild(struct hv_pci_dev
*hpdev
,
1463 enum hv_pcidev_ref_reason reason
)
1465 refcount_inc(&hpdev
->refs
);
1468 static void put_pcichild(struct hv_pci_dev
*hpdev
,
1469 enum hv_pcidev_ref_reason reason
)
1471 if (refcount_dec_and_test(&hpdev
->refs
))
1476 * new_pcichild_device() - Create a new child device
1477 * @hbus: The internal struct tracking this root PCI bus.
1478 * @desc: The information supplied so far from the host
1481 * This function creates the tracking structure for a new child
1482 * device and kicks off the process of figuring out what it is.
1484 * Return: Pointer to the new tracking struct
1486 static struct hv_pci_dev
*new_pcichild_device(struct hv_pcibus_device
*hbus
,
1487 struct pci_function_description
*desc
)
1489 struct hv_pci_dev
*hpdev
;
1490 struct pci_child_message
*res_req
;
1491 struct q_res_req_compl comp_pkt
;
1493 struct pci_packet init_packet
;
1494 u8 buffer
[sizeof(struct pci_child_message
)];
1496 unsigned long flags
;
1499 hpdev
= kzalloc(sizeof(*hpdev
), GFP_ATOMIC
);
1505 memset(&pkt
, 0, sizeof(pkt
));
1506 init_completion(&comp_pkt
.host_event
);
1507 comp_pkt
.hpdev
= hpdev
;
1508 pkt
.init_packet
.compl_ctxt
= &comp_pkt
;
1509 pkt
.init_packet
.completion_func
= q_resource_requirements
;
1510 res_req
= (struct pci_child_message
*)&pkt
.init_packet
.message
;
1511 res_req
->message_type
.type
= PCI_QUERY_RESOURCE_REQUIREMENTS
;
1512 res_req
->wslot
.slot
= desc
->win_slot
.slot
;
1514 ret
= vmbus_sendpacket(hbus
->hdev
->channel
, res_req
,
1515 sizeof(struct pci_child_message
),
1516 (unsigned long)&pkt
.init_packet
,
1518 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1522 wait_for_completion(&comp_pkt
.host_event
);
1524 hpdev
->desc
= *desc
;
1525 refcount_set(&hpdev
->refs
, 1);
1526 get_pcichild(hpdev
, hv_pcidev_ref_childlist
);
1527 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1530 * When a device is being added to the bus, we set the PCI domain
1531 * number to be the device serial number, which is non-zero and
1532 * unique on the same VM. The serial numbers start with 1, and
1533 * increase by 1 for each device. So device names including this
1534 * can have shorter names than based on the bus instance UUID.
1535 * Only the first device serial number is used for domain, so the
1536 * domain number will not change after the first device is added.
1538 if (list_empty(&hbus
->children
))
1539 hbus
->sysdata
.domain
= desc
->ser
;
1540 list_add_tail(&hpdev
->list_entry
, &hbus
->children
);
1541 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1550 * get_pcichild_wslot() - Find device from slot
1551 * @hbus: Root PCI bus, as understood by this driver
1552 * @wslot: Location on the bus
1554 * This function looks up a PCI device and returns the internal
1555 * representation of it. It acquires a reference on it, so that
1556 * the device won't be deleted while somebody is using it. The
1557 * caller is responsible for calling put_pcichild() to release
1560 * Return: Internal representation of a PCI device
1562 static struct hv_pci_dev
*get_pcichild_wslot(struct hv_pcibus_device
*hbus
,
1565 unsigned long flags
;
1566 struct hv_pci_dev
*iter
, *hpdev
= NULL
;
1568 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1569 list_for_each_entry(iter
, &hbus
->children
, list_entry
) {
1570 if (iter
->desc
.win_slot
.slot
== wslot
) {
1572 get_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
1576 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1582 * pci_devices_present_work() - Handle new list of child devices
1583 * @work: Work struct embedded in struct hv_dr_work
1585 * "Bus Relations" is the Windows term for "children of this
1586 * bus." The terminology is preserved here for people trying to
1587 * debug the interaction between Hyper-V and Linux. This
1588 * function is called when the parent partition reports a list
1589 * of functions that should be observed under this PCI Express
1592 * This function updates the list, and must tolerate being
1593 * called multiple times with the same information. The typical
1594 * number of child devices is one, with very atypical cases
1595 * involving three or four, so the algorithms used here can be
1596 * simple and inefficient.
1598 * It must also treat the omission of a previously observed device as
1599 * notification that the device no longer exists.
1601 * Note that this function is a work item, and it may not be
1602 * invoked in the order that it was queued. Back to back
1603 * updates of the list of present devices may involve queuing
1604 * multiple work items, and this one may run before ones that
1605 * were sent later. As such, this function only does something
1606 * if is the last one in the queue.
1608 static void pci_devices_present_work(struct work_struct
*work
)
1612 struct list_head
*iter
;
1613 struct pci_function_description
*new_desc
;
1614 struct hv_pci_dev
*hpdev
;
1615 struct hv_pcibus_device
*hbus
;
1616 struct list_head removed
;
1617 struct hv_dr_work
*dr_wrk
;
1618 struct hv_dr_state
*dr
= NULL
;
1619 unsigned long flags
;
1621 dr_wrk
= container_of(work
, struct hv_dr_work
, wrk
);
1625 INIT_LIST_HEAD(&removed
);
1627 if (down_interruptible(&hbus
->enum_sem
)) {
1632 /* Pull this off the queue and process it if it was the last one. */
1633 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1634 while (!list_empty(&hbus
->dr_list
)) {
1635 dr
= list_first_entry(&hbus
->dr_list
, struct hv_dr_state
,
1637 list_del(&dr
->list_entry
);
1639 /* Throw this away if the list still has stuff in it. */
1640 if (!list_empty(&hbus
->dr_list
)) {
1645 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1648 up(&hbus
->enum_sem
);
1653 /* First, mark all existing children as reported missing. */
1654 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1655 list_for_each(iter
, &hbus
->children
) {
1656 hpdev
= container_of(iter
, struct hv_pci_dev
,
1658 hpdev
->reported_missing
= true;
1660 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1662 /* Next, add back any reported devices. */
1663 for (child_no
= 0; child_no
< dr
->device_count
; child_no
++) {
1665 new_desc
= &dr
->func
[child_no
];
1667 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1668 list_for_each(iter
, &hbus
->children
) {
1669 hpdev
= container_of(iter
, struct hv_pci_dev
,
1671 if ((hpdev
->desc
.win_slot
.slot
==
1672 new_desc
->win_slot
.slot
) &&
1673 (hpdev
->desc
.v_id
== new_desc
->v_id
) &&
1674 (hpdev
->desc
.d_id
== new_desc
->d_id
) &&
1675 (hpdev
->desc
.ser
== new_desc
->ser
)) {
1676 hpdev
->reported_missing
= false;
1680 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1683 hpdev
= new_pcichild_device(hbus
, new_desc
);
1685 dev_err(&hbus
->hdev
->device
,
1686 "couldn't record a child device.\n");
1690 /* Move missing children to a list on the stack. */
1691 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1694 list_for_each(iter
, &hbus
->children
) {
1695 hpdev
= container_of(iter
, struct hv_pci_dev
,
1697 if (hpdev
->reported_missing
) {
1699 put_pcichild(hpdev
, hv_pcidev_ref_childlist
);
1700 list_move_tail(&hpdev
->list_entry
, &removed
);
1705 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1707 /* Delete everything that should no longer exist. */
1708 while (!list_empty(&removed
)) {
1709 hpdev
= list_first_entry(&removed
, struct hv_pci_dev
,
1711 list_del(&hpdev
->list_entry
);
1712 put_pcichild(hpdev
, hv_pcidev_ref_initial
);
1715 switch (hbus
->state
) {
1716 case hv_pcibus_installed
:
1718 * Tell the core to rescan bus
1719 * because there may have been changes.
1721 pci_lock_rescan_remove();
1722 pci_scan_child_bus(hbus
->pci_bus
);
1723 pci_unlock_rescan_remove();
1726 case hv_pcibus_init
:
1727 case hv_pcibus_probed
:
1728 survey_child_resources(hbus
);
1735 up(&hbus
->enum_sem
);
1741 * hv_pci_devices_present() - Handles list of new children
1742 * @hbus: Root PCI bus, as understood by this driver
1743 * @relations: Packet from host listing children
1745 * This function is invoked whenever a new list of devices for
1748 static void hv_pci_devices_present(struct hv_pcibus_device
*hbus
,
1749 struct pci_bus_relations
*relations
)
1751 struct hv_dr_state
*dr
;
1752 struct hv_dr_work
*dr_wrk
;
1753 unsigned long flags
;
1755 dr_wrk
= kzalloc(sizeof(*dr_wrk
), GFP_NOWAIT
);
1759 dr
= kzalloc(offsetof(struct hv_dr_state
, func
) +
1760 (sizeof(struct pci_function_description
) *
1761 (relations
->device_count
)), GFP_NOWAIT
);
1767 INIT_WORK(&dr_wrk
->wrk
, pci_devices_present_work
);
1769 dr
->device_count
= relations
->device_count
;
1770 if (dr
->device_count
!= 0) {
1771 memcpy(dr
->func
, relations
->func
,
1772 sizeof(struct pci_function_description
) *
1776 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1777 list_add_tail(&dr
->list_entry
, &hbus
->dr_list
);
1778 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1781 schedule_work(&dr_wrk
->wrk
);
1785 * hv_eject_device_work() - Asynchronously handles ejection
1786 * @work: Work struct embedded in internal device struct
1788 * This function handles ejecting a device. Windows will
1789 * attempt to gracefully eject a device, waiting 60 seconds to
1790 * hear back from the guest OS that this completed successfully.
1791 * If this timer expires, the device will be forcibly removed.
1793 static void hv_eject_device_work(struct work_struct
*work
)
1795 struct pci_eject_response
*ejct_pkt
;
1796 struct hv_pci_dev
*hpdev
;
1797 struct pci_dev
*pdev
;
1798 unsigned long flags
;
1801 struct pci_packet pkt
;
1802 u8 buffer
[sizeof(struct pci_eject_response
)];
1805 hpdev
= container_of(work
, struct hv_pci_dev
, wrk
);
1807 if (hpdev
->state
!= hv_pcichild_ejecting
) {
1808 put_pcichild(hpdev
, hv_pcidev_ref_pnp
);
1813 * Ejection can come before or after the PCI bus has been set up, so
1814 * attempt to find it and tear down the bus state, if it exists. This
1815 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1816 * because hbus->pci_bus may not exist yet.
1818 wslot
= wslot_to_devfn(hpdev
->desc
.win_slot
.slot
);
1819 pdev
= pci_get_domain_bus_and_slot(hpdev
->hbus
->sysdata
.domain
, 0,
1822 pci_lock_rescan_remove();
1823 pci_stop_and_remove_bus_device(pdev
);
1825 pci_unlock_rescan_remove();
1828 spin_lock_irqsave(&hpdev
->hbus
->device_list_lock
, flags
);
1829 list_del(&hpdev
->list_entry
);
1830 spin_unlock_irqrestore(&hpdev
->hbus
->device_list_lock
, flags
);
1832 memset(&ctxt
, 0, sizeof(ctxt
));
1833 ejct_pkt
= (struct pci_eject_response
*)&ctxt
.pkt
.message
;
1834 ejct_pkt
->message_type
.type
= PCI_EJECTION_COMPLETE
;
1835 ejct_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
1836 vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, ejct_pkt
,
1837 sizeof(*ejct_pkt
), (unsigned long)&ctxt
.pkt
,
1838 VM_PKT_DATA_INBAND
, 0);
1840 put_pcichild(hpdev
, hv_pcidev_ref_childlist
);
1841 put_pcichild(hpdev
, hv_pcidev_ref_pnp
);
1842 put_hvpcibus(hpdev
->hbus
);
1846 * hv_pci_eject_device() - Handles device ejection
1847 * @hpdev: Internal device tracking struct
1849 * This function is invoked when an ejection packet arrives. It
1850 * just schedules work so that we don't re-enter the packet
1851 * delivery code handling the ejection.
1853 static void hv_pci_eject_device(struct hv_pci_dev
*hpdev
)
1855 hpdev
->state
= hv_pcichild_ejecting
;
1856 get_pcichild(hpdev
, hv_pcidev_ref_pnp
);
1857 INIT_WORK(&hpdev
->wrk
, hv_eject_device_work
);
1858 get_hvpcibus(hpdev
->hbus
);
1859 schedule_work(&hpdev
->wrk
);
1863 * hv_pci_onchannelcallback() - Handles incoming packets
1864 * @context: Internal bus tracking struct
1866 * This function is invoked whenever the host sends a packet to
1867 * this channel (which is private to this root PCI bus).
1869 static void hv_pci_onchannelcallback(void *context
)
1871 const int packet_size
= 0x100;
1873 struct hv_pcibus_device
*hbus
= context
;
1876 struct vmpacket_descriptor
*desc
;
1877 unsigned char *buffer
;
1878 int bufferlen
= packet_size
;
1879 struct pci_packet
*comp_packet
;
1880 struct pci_response
*response
;
1881 struct pci_incoming_message
*new_message
;
1882 struct pci_bus_relations
*bus_rel
;
1883 struct pci_dev_incoming
*dev_message
;
1884 struct hv_pci_dev
*hpdev
;
1886 buffer
= kmalloc(bufferlen
, GFP_ATOMIC
);
1891 ret
= vmbus_recvpacket_raw(hbus
->hdev
->channel
, buffer
,
1892 bufferlen
, &bytes_recvd
, &req_id
);
1894 if (ret
== -ENOBUFS
) {
1896 /* Handle large packet */
1897 bufferlen
= bytes_recvd
;
1898 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1904 /* Zero length indicates there are no more packets. */
1905 if (ret
|| !bytes_recvd
)
1909 * All incoming packets must be at least as large as a
1912 if (bytes_recvd
<= sizeof(struct pci_response
))
1914 desc
= (struct vmpacket_descriptor
*)buffer
;
1916 switch (desc
->type
) {
1920 * The host is trusted, and thus it's safe to interpret
1921 * this transaction ID as a pointer.
1923 comp_packet
= (struct pci_packet
*)req_id
;
1924 response
= (struct pci_response
*)buffer
;
1925 comp_packet
->completion_func(comp_packet
->compl_ctxt
,
1930 case VM_PKT_DATA_INBAND
:
1932 new_message
= (struct pci_incoming_message
*)buffer
;
1933 switch (new_message
->message_type
.type
) {
1934 case PCI_BUS_RELATIONS
:
1936 bus_rel
= (struct pci_bus_relations
*)buffer
;
1938 offsetof(struct pci_bus_relations
, func
) +
1939 (sizeof(struct pci_function_description
) *
1940 (bus_rel
->device_count
))) {
1941 dev_err(&hbus
->hdev
->device
,
1942 "bus relations too small\n");
1946 hv_pci_devices_present(hbus
, bus_rel
);
1951 dev_message
= (struct pci_dev_incoming
*)buffer
;
1952 hpdev
= get_pcichild_wslot(hbus
,
1953 dev_message
->wslot
.slot
);
1955 hv_pci_eject_device(hpdev
);
1957 hv_pcidev_ref_by_slot
);
1962 dev_warn(&hbus
->hdev
->device
,
1963 "Unimplemented protocol message %x\n",
1964 new_message
->message_type
.type
);
1970 dev_err(&hbus
->hdev
->device
,
1971 "unhandled packet type %d, tid %llx len %d\n",
1972 desc
->type
, req_id
, bytes_recvd
);
1981 * hv_pci_protocol_negotiation() - Set up protocol
1982 * @hdev: VMBus's tracking struct for this root PCI bus
1984 * This driver is intended to support running on Windows 10
1985 * (server) and later versions. It will not run on earlier
1986 * versions, as they assume that many of the operations which
1987 * Linux needs accomplished with a spinlock held were done via
1988 * asynchronous messaging via VMBus. Windows 10 increases the
1989 * surface area of PCI emulation so that these actions can take
1990 * place by suspending a virtual processor for their duration.
1992 * This function negotiates the channel protocol version,
1993 * failing if the host doesn't support the necessary protocol
1996 static int hv_pci_protocol_negotiation(struct hv_device
*hdev
)
1998 struct pci_version_request
*version_req
;
1999 struct hv_pci_compl comp_pkt
;
2000 struct pci_packet
*pkt
;
2005 * Initiate the handshake with the host and negotiate
2006 * a version that the host can support. We start with the
2007 * highest version number and go down if the host cannot
2010 pkt
= kzalloc(sizeof(*pkt
) + sizeof(*version_req
), GFP_KERNEL
);
2014 init_completion(&comp_pkt
.host_event
);
2015 pkt
->completion_func
= hv_pci_generic_compl
;
2016 pkt
->compl_ctxt
= &comp_pkt
;
2017 version_req
= (struct pci_version_request
*)&pkt
->message
;
2018 version_req
->message_type
.type
= PCI_QUERY_PROTOCOL_VERSION
;
2020 for (i
= 0; i
< ARRAY_SIZE(pci_protocol_versions
); i
++) {
2021 version_req
->protocol_version
= pci_protocol_versions
[i
];
2022 ret
= vmbus_sendpacket(hdev
->channel
, version_req
,
2023 sizeof(struct pci_version_request
),
2024 (unsigned long)pkt
, VM_PKT_DATA_INBAND
,
2025 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2027 dev_err(&hdev
->device
,
2028 "PCI Pass-through VSP failed sending version reqquest: %#x",
2033 wait_for_completion(&comp_pkt
.host_event
);
2035 if (comp_pkt
.completion_status
>= 0) {
2036 pci_protocol_version
= pci_protocol_versions
[i
];
2037 dev_info(&hdev
->device
,
2038 "PCI VMBus probing: Using version %#x\n",
2039 pci_protocol_version
);
2043 if (comp_pkt
.completion_status
!= STATUS_REVISION_MISMATCH
) {
2044 dev_err(&hdev
->device
,
2045 "PCI Pass-through VSP failed version request: %#x",
2046 comp_pkt
.completion_status
);
2051 reinit_completion(&comp_pkt
.host_event
);
2054 dev_err(&hdev
->device
,
2055 "PCI pass-through VSP failed to find supported version");
2064 * hv_pci_free_bridge_windows() - Release memory regions for the
2066 * @hbus: Root PCI bus, as understood by this driver
2068 static void hv_pci_free_bridge_windows(struct hv_pcibus_device
*hbus
)
2071 * Set the resources back to the way they looked when they
2072 * were allocated by setting IORESOURCE_BUSY again.
2075 if (hbus
->low_mmio_space
&& hbus
->low_mmio_res
) {
2076 hbus
->low_mmio_res
->flags
|= IORESOURCE_BUSY
;
2077 vmbus_free_mmio(hbus
->low_mmio_res
->start
,
2078 resource_size(hbus
->low_mmio_res
));
2081 if (hbus
->high_mmio_space
&& hbus
->high_mmio_res
) {
2082 hbus
->high_mmio_res
->flags
|= IORESOURCE_BUSY
;
2083 vmbus_free_mmio(hbus
->high_mmio_res
->start
,
2084 resource_size(hbus
->high_mmio_res
));
2089 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2091 * @hbus: Root PCI bus, as understood by this driver
2093 * This function calls vmbus_allocate_mmio(), which is itself a
2094 * bit of a compromise. Ideally, we might change the pnp layer
2095 * in the kernel such that it comprehends either PCI devices
2096 * which are "grandchildren of ACPI," with some intermediate bus
2097 * node (in this case, VMBus) or change it such that it
2098 * understands VMBus. The pnp layer, however, has been declared
2099 * deprecated, and not subject to change.
2101 * The workaround, implemented here, is to ask VMBus to allocate
2102 * MMIO space for this bus. VMBus itself knows which ranges are
2103 * appropriate by looking at its own ACPI objects. Then, after
2104 * these ranges are claimed, they're modified to look like they
2105 * would have looked if the ACPI and pnp code had allocated
2106 * bridge windows. These descriptors have to exist in this form
2107 * in order to satisfy the code which will get invoked when the
2108 * endpoint PCI function driver calls request_mem_region() or
2109 * request_mem_region_exclusive().
2111 * Return: 0 on success, -errno on failure
2113 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device
*hbus
)
2115 resource_size_t align
;
2118 if (hbus
->low_mmio_space
) {
2119 align
= 1ULL << (63 - __builtin_clzll(hbus
->low_mmio_space
));
2120 ret
= vmbus_allocate_mmio(&hbus
->low_mmio_res
, hbus
->hdev
, 0,
2121 (u64
)(u32
)0xffffffff,
2122 hbus
->low_mmio_space
,
2125 dev_err(&hbus
->hdev
->device
,
2126 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2127 hbus
->low_mmio_space
);
2131 /* Modify this resource to become a bridge window. */
2132 hbus
->low_mmio_res
->flags
|= IORESOURCE_WINDOW
;
2133 hbus
->low_mmio_res
->flags
&= ~IORESOURCE_BUSY
;
2134 pci_add_resource(&hbus
->resources_for_children
,
2135 hbus
->low_mmio_res
);
2138 if (hbus
->high_mmio_space
) {
2139 align
= 1ULL << (63 - __builtin_clzll(hbus
->high_mmio_space
));
2140 ret
= vmbus_allocate_mmio(&hbus
->high_mmio_res
, hbus
->hdev
,
2142 hbus
->high_mmio_space
, align
,
2145 dev_err(&hbus
->hdev
->device
,
2146 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2147 hbus
->high_mmio_space
);
2148 goto release_low_mmio
;
2151 /* Modify this resource to become a bridge window. */
2152 hbus
->high_mmio_res
->flags
|= IORESOURCE_WINDOW
;
2153 hbus
->high_mmio_res
->flags
&= ~IORESOURCE_BUSY
;
2154 pci_add_resource(&hbus
->resources_for_children
,
2155 hbus
->high_mmio_res
);
2161 if (hbus
->low_mmio_res
) {
2162 vmbus_free_mmio(hbus
->low_mmio_res
->start
,
2163 resource_size(hbus
->low_mmio_res
));
2170 * hv_allocate_config_window() - Find MMIO space for PCI Config
2171 * @hbus: Root PCI bus, as understood by this driver
2173 * This function claims memory-mapped I/O space for accessing
2174 * configuration space for the functions on this bus.
2176 * Return: 0 on success, -errno on failure
2178 static int hv_allocate_config_window(struct hv_pcibus_device
*hbus
)
2183 * Set up a region of MMIO space to use for accessing configuration
2186 ret
= vmbus_allocate_mmio(&hbus
->mem_config
, hbus
->hdev
, 0, -1,
2187 PCI_CONFIG_MMIO_LENGTH
, 0x1000, false);
2192 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2193 * resource claims (those which cannot be overlapped) and the ranges
2194 * which are valid for the children of this bus, which are intended
2195 * to be overlapped by those children. Set the flag on this claim
2196 * meaning that this region can't be overlapped.
2199 hbus
->mem_config
->flags
|= IORESOURCE_BUSY
;
2204 static void hv_free_config_window(struct hv_pcibus_device
*hbus
)
2206 vmbus_free_mmio(hbus
->mem_config
->start
, PCI_CONFIG_MMIO_LENGTH
);
2210 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2211 * @hdev: VMBus's tracking struct for this root PCI bus
2213 * Return: 0 on success, -errno on failure
2215 static int hv_pci_enter_d0(struct hv_device
*hdev
)
2217 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2218 struct pci_bus_d0_entry
*d0_entry
;
2219 struct hv_pci_compl comp_pkt
;
2220 struct pci_packet
*pkt
;
2224 * Tell the host that the bus is ready to use, and moved into the
2225 * powered-on state. This includes telling the host which region
2226 * of memory-mapped I/O space has been chosen for configuration space
2229 pkt
= kzalloc(sizeof(*pkt
) + sizeof(*d0_entry
), GFP_KERNEL
);
2233 init_completion(&comp_pkt
.host_event
);
2234 pkt
->completion_func
= hv_pci_generic_compl
;
2235 pkt
->compl_ctxt
= &comp_pkt
;
2236 d0_entry
= (struct pci_bus_d0_entry
*)&pkt
->message
;
2237 d0_entry
->message_type
.type
= PCI_BUS_D0ENTRY
;
2238 d0_entry
->mmio_base
= hbus
->mem_config
->start
;
2240 ret
= vmbus_sendpacket(hdev
->channel
, d0_entry
, sizeof(*d0_entry
),
2241 (unsigned long)pkt
, VM_PKT_DATA_INBAND
,
2242 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2246 wait_for_completion(&comp_pkt
.host_event
);
2248 if (comp_pkt
.completion_status
< 0) {
2249 dev_err(&hdev
->device
,
2250 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2251 comp_pkt
.completion_status
);
2264 * hv_pci_query_relations() - Ask host to send list of child
2266 * @hdev: VMBus's tracking struct for this root PCI bus
2268 * Return: 0 on success, -errno on failure
2270 static int hv_pci_query_relations(struct hv_device
*hdev
)
2272 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2273 struct pci_message message
;
2274 struct completion comp
;
2277 /* Ask the host to send along the list of child devices */
2278 init_completion(&comp
);
2279 if (cmpxchg(&hbus
->survey_event
, NULL
, &comp
))
2282 memset(&message
, 0, sizeof(message
));
2283 message
.type
= PCI_QUERY_BUS_RELATIONS
;
2285 ret
= vmbus_sendpacket(hdev
->channel
, &message
, sizeof(message
),
2286 0, VM_PKT_DATA_INBAND
, 0);
2290 wait_for_completion(&comp
);
2295 * hv_send_resources_allocated() - Report local resource choices
2296 * @hdev: VMBus's tracking struct for this root PCI bus
2298 * The host OS is expecting to be sent a request as a message
2299 * which contains all the resources that the device will use.
2300 * The response contains those same resources, "translated"
2301 * which is to say, the values which should be used by the
2302 * hardware, when it delivers an interrupt. (MMIO resources are
2303 * used in local terms.) This is nice for Windows, and lines up
2304 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2305 * is deeply expecting to scan an emulated PCI configuration
2306 * space. So this message is sent here only to drive the state
2307 * machine on the host forward.
2309 * Return: 0 on success, -errno on failure
2311 static int hv_send_resources_allocated(struct hv_device
*hdev
)
2313 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2314 struct pci_resources_assigned
*res_assigned
;
2315 struct pci_resources_assigned2
*res_assigned2
;
2316 struct hv_pci_compl comp_pkt
;
2317 struct hv_pci_dev
*hpdev
;
2318 struct pci_packet
*pkt
;
2323 size_res
= (pci_protocol_version
< PCI_PROTOCOL_VERSION_1_2
)
2324 ? sizeof(*res_assigned
) : sizeof(*res_assigned2
);
2326 pkt
= kmalloc(sizeof(*pkt
) + size_res
, GFP_KERNEL
);
2332 for (wslot
= 0; wslot
< 256; wslot
++) {
2333 hpdev
= get_pcichild_wslot(hbus
, wslot
);
2337 memset(pkt
, 0, sizeof(*pkt
) + size_res
);
2338 init_completion(&comp_pkt
.host_event
);
2339 pkt
->completion_func
= hv_pci_generic_compl
;
2340 pkt
->compl_ctxt
= &comp_pkt
;
2342 if (pci_protocol_version
< PCI_PROTOCOL_VERSION_1_2
) {
2344 (struct pci_resources_assigned
*)&pkt
->message
;
2345 res_assigned
->message_type
.type
=
2346 PCI_RESOURCES_ASSIGNED
;
2347 res_assigned
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2350 (struct pci_resources_assigned2
*)&pkt
->message
;
2351 res_assigned2
->message_type
.type
=
2352 PCI_RESOURCES_ASSIGNED2
;
2353 res_assigned2
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2355 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
2357 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
->message
,
2358 size_res
, (unsigned long)pkt
,
2360 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2364 wait_for_completion(&comp_pkt
.host_event
);
2366 if (comp_pkt
.completion_status
< 0) {
2368 dev_err(&hdev
->device
,
2369 "resource allocated returned 0x%x",
2370 comp_pkt
.completion_status
);
2380 * hv_send_resources_released() - Report local resources
2382 * @hdev: VMBus's tracking struct for this root PCI bus
2384 * Return: 0 on success, -errno on failure
2386 static int hv_send_resources_released(struct hv_device
*hdev
)
2388 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2389 struct pci_child_message pkt
;
2390 struct hv_pci_dev
*hpdev
;
2394 for (wslot
= 0; wslot
< 256; wslot
++) {
2395 hpdev
= get_pcichild_wslot(hbus
, wslot
);
2399 memset(&pkt
, 0, sizeof(pkt
));
2400 pkt
.message_type
.type
= PCI_RESOURCES_RELEASED
;
2401 pkt
.wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2403 put_pcichild(hpdev
, hv_pcidev_ref_by_slot
);
2405 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
, sizeof(pkt
), 0,
2406 VM_PKT_DATA_INBAND
, 0);
2414 static void get_hvpcibus(struct hv_pcibus_device
*hbus
)
2416 atomic_inc(&hbus
->remove_lock
);
2419 static void put_hvpcibus(struct hv_pcibus_device
*hbus
)
2421 if (atomic_dec_and_test(&hbus
->remove_lock
))
2422 complete(&hbus
->remove_event
);
2426 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2427 * @hdev: VMBus's tracking struct for this root PCI bus
2428 * @dev_id: Identifies the device itself
2430 * Return: 0 on success, -errno on failure
2432 static int hv_pci_probe(struct hv_device
*hdev
,
2433 const struct hv_vmbus_device_id
*dev_id
)
2435 struct hv_pcibus_device
*hbus
;
2439 * hv_pcibus_device contains the hypercall arguments for retargeting in
2440 * hv_irq_unmask(). Those must not cross a page boundary.
2442 BUILD_BUG_ON(sizeof(*hbus
) > PAGE_SIZE
);
2444 hbus
= (struct hv_pcibus_device
*)get_zeroed_page(GFP_KERNEL
);
2447 hbus
->state
= hv_pcibus_init
;
2450 * The PCI bus "domain" is what is called "segment" in ACPI and
2451 * other specs. Pull it from the instance ID, to get something
2452 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2453 * do the same thing for consistency. Note that, since this code
2454 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2455 * that (1) the only domain in use for something that looks like
2456 * a physical PCI bus (which is actually emulated by the
2457 * hypervisor) is domain 0 and (2) there will be no overlap
2458 * between domains derived from these instance IDs in the same
2461 hbus
->sysdata
.domain
= hdev
->dev_instance
.b
[9] |
2462 hdev
->dev_instance
.b
[8] << 8;
2465 atomic_inc(&hbus
->remove_lock
);
2466 INIT_LIST_HEAD(&hbus
->children
);
2467 INIT_LIST_HEAD(&hbus
->dr_list
);
2468 INIT_LIST_HEAD(&hbus
->resources_for_children
);
2469 spin_lock_init(&hbus
->config_lock
);
2470 spin_lock_init(&hbus
->device_list_lock
);
2471 spin_lock_init(&hbus
->retarget_msi_interrupt_lock
);
2472 sema_init(&hbus
->enum_sem
, 1);
2473 init_completion(&hbus
->remove_event
);
2475 ret
= vmbus_open(hdev
->channel
, pci_ring_size
, pci_ring_size
, NULL
, 0,
2476 hv_pci_onchannelcallback
, hbus
);
2480 hv_set_drvdata(hdev
, hbus
);
2482 ret
= hv_pci_protocol_negotiation(hdev
);
2486 ret
= hv_allocate_config_window(hbus
);
2490 hbus
->cfg_addr
= ioremap(hbus
->mem_config
->start
,
2491 PCI_CONFIG_MMIO_LENGTH
);
2492 if (!hbus
->cfg_addr
) {
2493 dev_err(&hdev
->device
,
2494 "Unable to map a virtual address for config space\n");
2499 hbus
->sysdata
.fwnode
= irq_domain_alloc_fwnode(hbus
);
2500 if (!hbus
->sysdata
.fwnode
) {
2505 ret
= hv_pcie_init_irq_domain(hbus
);
2509 ret
= hv_pci_query_relations(hdev
);
2511 goto free_irq_domain
;
2513 ret
= hv_pci_enter_d0(hdev
);
2515 goto free_irq_domain
;
2517 ret
= hv_pci_allocate_bridge_windows(hbus
);
2519 goto free_irq_domain
;
2521 ret
= hv_send_resources_allocated(hdev
);
2525 prepopulate_bars(hbus
);
2527 hbus
->state
= hv_pcibus_probed
;
2529 ret
= create_root_hv_pci_bus(hbus
);
2536 hv_pci_free_bridge_windows(hbus
);
2538 irq_domain_remove(hbus
->irq_domain
);
2540 irq_domain_free_fwnode(hbus
->sysdata
.fwnode
);
2542 iounmap(hbus
->cfg_addr
);
2544 hv_free_config_window(hbus
);
2546 vmbus_close(hdev
->channel
);
2548 free_page((unsigned long)hbus
);
2552 static void hv_pci_bus_exit(struct hv_device
*hdev
)
2554 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2556 struct pci_packet teardown_packet
;
2557 u8 buffer
[sizeof(struct pci_message
)];
2559 struct pci_bus_relations relations
;
2560 struct hv_pci_compl comp_pkt
;
2564 * After the host sends the RESCIND_CHANNEL message, it doesn't
2565 * access the per-channel ringbuffer any longer.
2567 if (hdev
->channel
->rescind
)
2570 /* Delete any children which might still exist. */
2571 memset(&relations
, 0, sizeof(relations
));
2572 hv_pci_devices_present(hbus
, &relations
);
2574 ret
= hv_send_resources_released(hdev
);
2576 dev_err(&hdev
->device
,
2577 "Couldn't send resources released packet(s)\n");
2579 memset(&pkt
.teardown_packet
, 0, sizeof(pkt
.teardown_packet
));
2580 init_completion(&comp_pkt
.host_event
);
2581 pkt
.teardown_packet
.completion_func
= hv_pci_generic_compl
;
2582 pkt
.teardown_packet
.compl_ctxt
= &comp_pkt
;
2583 pkt
.teardown_packet
.message
[0].type
= PCI_BUS_D0EXIT
;
2585 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
.teardown_packet
.message
,
2586 sizeof(struct pci_message
),
2587 (unsigned long)&pkt
.teardown_packet
,
2589 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2591 wait_for_completion_timeout(&comp_pkt
.host_event
, 10 * HZ
);
2595 * hv_pci_remove() - Remove routine for this VMBus channel
2596 * @hdev: VMBus's tracking struct for this root PCI bus
2598 * Return: 0 on success, -errno on failure
2600 static int hv_pci_remove(struct hv_device
*hdev
)
2602 struct hv_pcibus_device
*hbus
;
2604 hbus
= hv_get_drvdata(hdev
);
2605 if (hbus
->state
== hv_pcibus_installed
) {
2606 /* Remove the bus from PCI's point of view. */
2607 pci_lock_rescan_remove();
2608 pci_stop_root_bus(hbus
->pci_bus
);
2609 pci_remove_root_bus(hbus
->pci_bus
);
2610 pci_unlock_rescan_remove();
2611 hbus
->state
= hv_pcibus_removed
;
2614 hv_pci_bus_exit(hdev
);
2616 vmbus_close(hdev
->channel
);
2618 iounmap(hbus
->cfg_addr
);
2619 hv_free_config_window(hbus
);
2620 pci_free_resource_list(&hbus
->resources_for_children
);
2621 hv_pci_free_bridge_windows(hbus
);
2622 irq_domain_remove(hbus
->irq_domain
);
2623 irq_domain_free_fwnode(hbus
->sysdata
.fwnode
);
2625 wait_for_completion(&hbus
->remove_event
);
2626 free_page((unsigned long)hbus
);
2630 static const struct hv_vmbus_device_id hv_pci_id_table
[] = {
2631 /* PCI Pass-through Class ID */
2632 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2637 MODULE_DEVICE_TABLE(vmbus
, hv_pci_id_table
);
2639 static struct hv_driver hv_pci_drv
= {
2641 .id_table
= hv_pci_id_table
,
2642 .probe
= hv_pci_probe
,
2643 .remove
= hv_pci_remove
,
2646 static void __exit
exit_hv_pci_drv(void)
2648 vmbus_driver_unregister(&hv_pci_drv
);
2651 static int __init
init_hv_pci_drv(void)
2653 return vmbus_driver_register(&hv_pci_drv
);
2656 module_init(init_hv_pci_drv
);
2657 module_exit(exit_hv_pci_drv
);
2659 MODULE_DESCRIPTION("Hyper-V PCI");
2660 MODULE_LICENSE("GPL v2");