1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) Microsoft Corporation.
6 * Jake Oshins <jakeo@microsoft.com>
8 * This driver acts as a paravirtual front-end for PCI Express root buses.
9 * When a PCI Express function (either an entire device or an SR-IOV
10 * Virtual Function) is being passed through to the VM, this driver exposes
11 * a new bus to the guest VM. This is modeled as a root PCI bus because
12 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
13 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
14 * until a device as been exposed using this driver.
16 * Each root PCI bus has its own PCI domain, which is called "Segment" in
17 * the PCI Firmware Specifications. Thus while each device passed through
18 * to the VM using this front-end will appear at "device 0", the domain will
19 * be unique. Typically, each bus will have one PCI function on it, though
20 * this driver does support more than one.
22 * In order to map the interrupts from the device through to the guest VM,
23 * this driver also implements an IRQ Domain, which handles interrupts (either
24 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
25 * set up, torn down, or reaffined, this driver communicates with the
26 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
27 * interrupt will be delivered to the correct virtual processor at the right
28 * vector. This driver does not support level-triggered (line-based)
29 * interrupts, and will report that the Interrupt Line register in the
30 * function's configuration space is zero.
32 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
33 * facilities. For instance, the configuration space of a function exposed
34 * by Hyper-V is mapped into a single page of memory space, and the
35 * read and write handlers for config space must be aware of this mechanism.
36 * Similarly, device setup and teardown involves messages sent to and from
37 * the PCI back-end driver in Hyper-V.
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/semaphore.h>
45 #include <linux/irqdomain.h>
46 #include <asm/irqdomain.h>
48 #include <linux/irq.h>
49 #include <linux/msi.h>
50 #include <linux/hyperv.h>
51 #include <linux/refcount.h>
52 #include <asm/mshyperv.h>
55 * Protocol versions. The low word is the minor version, the high word the
59 #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
60 #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
61 #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
63 enum pci_protocol_version_t
{
64 PCI_PROTOCOL_VERSION_1_1
= PCI_MAKE_VERSION(1, 1), /* Win10 */
65 PCI_PROTOCOL_VERSION_1_2
= PCI_MAKE_VERSION(1, 2), /* RS1 */
68 #define CPU_AFFINITY_ALL -1ULL
71 * Supported protocol versions in the order of probing - highest go
74 static enum pci_protocol_version_t pci_protocol_versions
[] = {
75 PCI_PROTOCOL_VERSION_1_2
,
76 PCI_PROTOCOL_VERSION_1_1
,
80 * Protocol version negotiated by hv_pci_protocol_negotiation().
82 static enum pci_protocol_version_t pci_protocol_version
;
84 #define PCI_CONFIG_MMIO_LENGTH 0x2000
85 #define CFG_PAGE_OFFSET 0x1000
86 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
88 #define MAX_SUPPORTED_MSI_MESSAGES 0x400
90 #define STATUS_REVISION_MISMATCH 0xC0000059
96 enum pci_message_type
{
100 PCI_MESSAGE_BASE
= 0x42490000,
101 PCI_BUS_RELATIONS
= PCI_MESSAGE_BASE
+ 0,
102 PCI_QUERY_BUS_RELATIONS
= PCI_MESSAGE_BASE
+ 1,
103 PCI_POWER_STATE_CHANGE
= PCI_MESSAGE_BASE
+ 4,
104 PCI_QUERY_RESOURCE_REQUIREMENTS
= PCI_MESSAGE_BASE
+ 5,
105 PCI_QUERY_RESOURCE_RESOURCES
= PCI_MESSAGE_BASE
+ 6,
106 PCI_BUS_D0ENTRY
= PCI_MESSAGE_BASE
+ 7,
107 PCI_BUS_D0EXIT
= PCI_MESSAGE_BASE
+ 8,
108 PCI_READ_BLOCK
= PCI_MESSAGE_BASE
+ 9,
109 PCI_WRITE_BLOCK
= PCI_MESSAGE_BASE
+ 0xA,
110 PCI_EJECT
= PCI_MESSAGE_BASE
+ 0xB,
111 PCI_QUERY_STOP
= PCI_MESSAGE_BASE
+ 0xC,
112 PCI_REENABLE
= PCI_MESSAGE_BASE
+ 0xD,
113 PCI_QUERY_STOP_FAILED
= PCI_MESSAGE_BASE
+ 0xE,
114 PCI_EJECTION_COMPLETE
= PCI_MESSAGE_BASE
+ 0xF,
115 PCI_RESOURCES_ASSIGNED
= PCI_MESSAGE_BASE
+ 0x10,
116 PCI_RESOURCES_RELEASED
= PCI_MESSAGE_BASE
+ 0x11,
117 PCI_INVALIDATE_BLOCK
= PCI_MESSAGE_BASE
+ 0x12,
118 PCI_QUERY_PROTOCOL_VERSION
= PCI_MESSAGE_BASE
+ 0x13,
119 PCI_CREATE_INTERRUPT_MESSAGE
= PCI_MESSAGE_BASE
+ 0x14,
120 PCI_DELETE_INTERRUPT_MESSAGE
= PCI_MESSAGE_BASE
+ 0x15,
121 PCI_RESOURCES_ASSIGNED2
= PCI_MESSAGE_BASE
+ 0x16,
122 PCI_CREATE_INTERRUPT_MESSAGE2
= PCI_MESSAGE_BASE
+ 0x17,
123 PCI_DELETE_INTERRUPT_MESSAGE2
= PCI_MESSAGE_BASE
+ 0x18, /* unused */
128 * Structures defining the virtual PCI Express protocol.
140 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
141 * which is all this driver does. This representation is the one used in
142 * Windows, which is what is expected when sending this back and forth with
143 * the Hyper-V parent partition.
145 union win_slot_encoding
{
155 * Pretty much as defined in the PCI Specifications.
157 struct pci_function_description
{
158 u16 v_id
; /* vendor ID */
159 u16 d_id
; /* device ID */
165 union win_slot_encoding win_slot
;
166 u32 ser
; /* serial number */
172 * @delivery_mode: As defined in Intel's Programmer's
173 * Reference Manual, Volume 3, Chapter 8.
174 * @vector_count: Number of contiguous entries in the
175 * Interrupt Descriptor Table that are
176 * occupied by this Message-Signaled
177 * Interrupt. For "MSI", as first defined
178 * in PCI 2.2, this can be between 1 and
179 * 32. For "MSI-X," as first defined in PCI
180 * 3.0, this must be 1, as each MSI-X table
181 * entry would have its own descriptor.
182 * @reserved: Empty space
183 * @cpu_mask: All the target virtual processors.
194 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
196 * @delivery_mode: As defined in Intel's Programmer's
197 * Reference Manual, Volume 3, Chapter 8.
198 * @vector_count: Number of contiguous entries in the
199 * Interrupt Descriptor Table that are
200 * occupied by this Message-Signaled
201 * Interrupt. For "MSI", as first defined
202 * in PCI 2.2, this can be between 1 and
203 * 32. For "MSI-X," as first defined in PCI
204 * 3.0, this must be 1, as each MSI-X table
205 * entry would have its own descriptor.
206 * @processor_count: number of bits enabled in array.
207 * @processor_array: All the target virtual processors.
209 struct hv_msi_desc2
{
214 u16 processor_array
[32];
218 * struct tran_int_desc
219 * @reserved: unused, padding
220 * @vector_count: same as in hv_msi_desc
221 * @data: This is the "data payload" value that is
222 * written by the device when it generates
223 * a message-signaled interrupt, either MSI
225 * @address: This is the address to which the data
226 * payload is written on interrupt
229 struct tran_int_desc
{
237 * A generic message format for virtual PCI.
238 * Specific message formats are defined later in the file.
245 struct pci_child_message
{
246 struct pci_message message_type
;
247 union win_slot_encoding wslot
;
250 struct pci_incoming_message
{
251 struct vmpacket_descriptor hdr
;
252 struct pci_message message_type
;
255 struct pci_response
{
256 struct vmpacket_descriptor hdr
;
257 s32 status
; /* negative values are failures */
261 void (*completion_func
)(void *context
, struct pci_response
*resp
,
262 int resp_packet_size
);
265 struct pci_message message
[0];
269 * Specific message types supporting the PCI protocol.
273 * Version negotiation message. Sent from the guest to the host.
274 * The guest is free to try different versions until the host
275 * accepts the version.
277 * pci_version: The protocol version requested.
278 * is_last_attempt: If TRUE, this is the last version guest will request.
279 * reservedz: Reserved field, set to zero.
282 struct pci_version_request
{
283 struct pci_message message_type
;
284 u32 protocol_version
;
288 * Bus D0 Entry. This is sent from the guest to the host when the virtual
289 * bus (PCI Express port) is ready for action.
292 struct pci_bus_d0_entry
{
293 struct pci_message message_type
;
298 struct pci_bus_relations
{
299 struct pci_incoming_message incoming
;
301 struct pci_function_description func
[0];
304 struct pci_q_res_req_response
{
305 struct vmpacket_descriptor hdr
;
306 s32 status
; /* negative values are failures */
310 struct pci_set_power
{
311 struct pci_message message_type
;
312 union win_slot_encoding wslot
;
313 u32 power_state
; /* In Windows terms */
317 struct pci_set_power_response
{
318 struct vmpacket_descriptor hdr
;
319 s32 status
; /* negative values are failures */
320 union win_slot_encoding wslot
;
321 u32 resultant_state
; /* In Windows terms */
325 struct pci_resources_assigned
{
326 struct pci_message message_type
;
327 union win_slot_encoding wslot
;
328 u8 memory_range
[0x14][6]; /* not used here */
333 struct pci_resources_assigned2
{
334 struct pci_message message_type
;
335 union win_slot_encoding wslot
;
336 u8 memory_range
[0x14][6]; /* not used here */
337 u32 msi_descriptor_count
;
341 struct pci_create_interrupt
{
342 struct pci_message message_type
;
343 union win_slot_encoding wslot
;
344 struct hv_msi_desc int_desc
;
347 struct pci_create_int_response
{
348 struct pci_response response
;
350 struct tran_int_desc int_desc
;
353 struct pci_create_interrupt2
{
354 struct pci_message message_type
;
355 union win_slot_encoding wslot
;
356 struct hv_msi_desc2 int_desc
;
359 struct pci_delete_interrupt
{
360 struct pci_message message_type
;
361 union win_slot_encoding wslot
;
362 struct tran_int_desc int_desc
;
365 struct pci_dev_incoming
{
366 struct pci_incoming_message incoming
;
367 union win_slot_encoding wslot
;
370 struct pci_eject_response
{
371 struct pci_message message_type
;
372 union win_slot_encoding wslot
;
376 static int pci_ring_size
= (4 * PAGE_SIZE
);
379 * Definitions or interrupt steering hypercall.
381 #define HV_PARTITION_ID_SELF ((u64)-1)
382 #define HVCALL_RETARGET_INTERRUPT 0x7e
384 struct hv_interrupt_entry
{
385 u32 source
; /* 1 for MSI(-X) */
391 #define HV_VP_SET_BANK_COUNT_MAX 5 /* current implementation limit */
394 u64 format
; /* 0 (HvGenericSetSparse4k) */
396 u64 masks
[HV_VP_SET_BANK_COUNT_MAX
];
400 * flags for hv_device_interrupt_target.flags
402 #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
403 #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
405 struct hv_device_interrupt_target
{
410 struct hv_vp_set vp_set
;
414 struct retarget_msi_interrupt
{
415 u64 partition_id
; /* use "self" */
417 struct hv_interrupt_entry int_entry
;
419 struct hv_device_interrupt_target int_target
;
423 * Driver specific state.
426 enum hv_pcibus_state
{
434 struct hv_pcibus_device
{
435 struct pci_sysdata sysdata
;
436 enum hv_pcibus_state state
;
437 refcount_t remove_lock
;
438 struct hv_device
*hdev
;
439 resource_size_t low_mmio_space
;
440 resource_size_t high_mmio_space
;
441 struct resource
*mem_config
;
442 struct resource
*low_mmio_res
;
443 struct resource
*high_mmio_res
;
444 struct completion
*survey_event
;
445 struct completion remove_event
;
446 struct pci_bus
*pci_bus
;
447 spinlock_t config_lock
; /* Avoid two threads writing index page */
448 spinlock_t device_list_lock
; /* Protect lists below */
449 void __iomem
*cfg_addr
;
451 struct list_head resources_for_children
;
453 struct list_head children
;
454 struct list_head dr_list
;
456 struct msi_domain_info msi_info
;
457 struct msi_controller msi_chip
;
458 struct irq_domain
*irq_domain
;
460 /* hypercall arg, must not cross page boundary */
461 struct retarget_msi_interrupt retarget_msi_interrupt_params
;
463 spinlock_t retarget_msi_interrupt_lock
;
465 struct workqueue_struct
*wq
;
469 * Tracks "Device Relations" messages from the host, which must be both
470 * processed in order and deferred so that they don't run in the context
471 * of the incoming packet callback.
474 struct work_struct wrk
;
475 struct hv_pcibus_device
*bus
;
479 struct list_head list_entry
;
481 struct pci_function_description func
[0];
484 enum hv_pcichild_state
{
485 hv_pcichild_init
= 0,
486 hv_pcichild_requirements
,
487 hv_pcichild_resourced
,
488 hv_pcichild_ejecting
,
493 /* List protected by pci_rescan_remove_lock */
494 struct list_head list_entry
;
496 enum hv_pcichild_state state
;
497 struct pci_function_description desc
;
498 bool reported_missing
;
499 struct hv_pcibus_device
*hbus
;
500 struct work_struct wrk
;
503 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
504 * read it back, for each of the BAR offsets within config space.
509 struct hv_pci_compl
{
510 struct completion host_event
;
511 s32 completion_status
;
514 static void hv_pci_onchannelcallback(void *context
);
517 * hv_pci_generic_compl() - Invoked for a completion packet
518 * @context: Set up by the sender of the packet.
519 * @resp: The response packet
520 * @resp_packet_size: Size in bytes of the packet
522 * This function is used to trigger an event and report status
523 * for any message for which the completion packet contains a
524 * status and nothing else.
526 static void hv_pci_generic_compl(void *context
, struct pci_response
*resp
,
527 int resp_packet_size
)
529 struct hv_pci_compl
*comp_pkt
= context
;
531 if (resp_packet_size
>= offsetofend(struct pci_response
, status
))
532 comp_pkt
->completion_status
= resp
->status
;
534 comp_pkt
->completion_status
= -1;
536 complete(&comp_pkt
->host_event
);
539 static struct hv_pci_dev
*get_pcichild_wslot(struct hv_pcibus_device
*hbus
,
542 static void get_pcichild(struct hv_pci_dev
*hpdev
)
544 refcount_inc(&hpdev
->refs
);
547 static void put_pcichild(struct hv_pci_dev
*hpdev
)
549 if (refcount_dec_and_test(&hpdev
->refs
))
553 static void get_hvpcibus(struct hv_pcibus_device
*hv_pcibus
);
554 static void put_hvpcibus(struct hv_pcibus_device
*hv_pcibus
);
557 * There is no good way to get notified from vmbus_onoffer_rescind(),
558 * so let's use polling here, since this is not a hot path.
560 static int wait_for_response(struct hv_device
*hdev
,
561 struct completion
*comp
)
564 if (hdev
->channel
->rescind
) {
565 dev_warn_once(&hdev
->device
, "The device is gone.\n");
569 if (wait_for_completion_timeout(comp
, HZ
/ 10))
577 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
578 * @devfn: The Linux representation of PCI slot
580 * Windows uses a slightly different representation of PCI slot.
582 * Return: The Windows representation
584 static u32
devfn_to_wslot(int devfn
)
586 union win_slot_encoding wslot
;
589 wslot
.bits
.dev
= PCI_SLOT(devfn
);
590 wslot
.bits
.func
= PCI_FUNC(devfn
);
596 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
597 * @wslot: The Windows representation of PCI slot
599 * Windows uses a slightly different representation of PCI slot.
601 * Return: The Linux representation
603 static int wslot_to_devfn(u32 wslot
)
605 union win_slot_encoding slot_no
;
607 slot_no
.slot
= wslot
;
608 return PCI_DEVFN(slot_no
.bits
.dev
, slot_no
.bits
.func
);
612 * PCI Configuration Space for these root PCI buses is implemented as a pair
613 * of pages in memory-mapped I/O space. Writing to the first page chooses
614 * the PCI function being written or read. Once the first page has been
615 * written to, the following page maps in the entire configuration space of
620 * _hv_pcifront_read_config() - Internal PCI config read
621 * @hpdev: The PCI driver's representation of the device
622 * @where: Offset within config space
623 * @size: Size of the transfer
624 * @val: Pointer to the buffer receiving the data
626 static void _hv_pcifront_read_config(struct hv_pci_dev
*hpdev
, int where
,
630 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+ where
;
633 * If the attempt is to read the IDs or the ROM BAR, simulate that.
635 if (where
+ size
<= PCI_COMMAND
) {
636 memcpy(val
, ((u8
*)&hpdev
->desc
.v_id
) + where
, size
);
637 } else if (where
>= PCI_CLASS_REVISION
&& where
+ size
<=
638 PCI_CACHE_LINE_SIZE
) {
639 memcpy(val
, ((u8
*)&hpdev
->desc
.rev
) + where
-
640 PCI_CLASS_REVISION
, size
);
641 } else if (where
>= PCI_SUBSYSTEM_VENDOR_ID
&& where
+ size
<=
643 memcpy(val
, (u8
*)&hpdev
->desc
.subsystem_id
+ where
-
644 PCI_SUBSYSTEM_VENDOR_ID
, size
);
645 } else if (where
>= PCI_ROM_ADDRESS
&& where
+ size
<=
646 PCI_CAPABILITY_LIST
) {
647 /* ROM BARs are unimplemented */
649 } else if (where
>= PCI_INTERRUPT_LINE
&& where
+ size
<=
652 * Interrupt Line and Interrupt PIN are hard-wired to zero
653 * because this front-end only supports message-signaled
657 } else if (where
+ size
<= CFG_PAGE_SIZE
) {
658 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
659 /* Choose the function to be read. (See comment above) */
660 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
661 /* Make sure the function was chosen before we start reading. */
663 /* Read from that function's config space. */
676 * Make sure the read was done before we release the spinlock
677 * allowing consecutive reads/writes.
680 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
682 dev_err(&hpdev
->hbus
->hdev
->device
,
683 "Attempt to read beyond a function's config space.\n");
687 static u16
hv_pcifront_get_vendor_id(struct hv_pci_dev
*hpdev
)
691 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+
694 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
696 /* Choose the function to be read. (See comment above) */
697 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
698 /* Make sure the function was chosen before we start reading. */
700 /* Read from that function's config space. */
703 * mb() is not required here, because the spin_unlock_irqrestore()
707 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
713 * _hv_pcifront_write_config() - Internal PCI config write
714 * @hpdev: The PCI driver's representation of the device
715 * @where: Offset within config space
716 * @size: Size of the transfer
717 * @val: The data being transferred
719 static void _hv_pcifront_write_config(struct hv_pci_dev
*hpdev
, int where
,
723 void __iomem
*addr
= hpdev
->hbus
->cfg_addr
+ CFG_PAGE_OFFSET
+ where
;
725 if (where
>= PCI_SUBSYSTEM_VENDOR_ID
&&
726 where
+ size
<= PCI_CAPABILITY_LIST
) {
727 /* SSIDs and ROM BARs are read-only */
728 } else if (where
>= PCI_COMMAND
&& where
+ size
<= CFG_PAGE_SIZE
) {
729 spin_lock_irqsave(&hpdev
->hbus
->config_lock
, flags
);
730 /* Choose the function to be written. (See comment above) */
731 writel(hpdev
->desc
.win_slot
.slot
, hpdev
->hbus
->cfg_addr
);
732 /* Make sure the function was chosen before we start writing. */
734 /* Write to that function's config space. */
747 * Make sure the write was done before we release the spinlock
748 * allowing consecutive reads/writes.
751 spin_unlock_irqrestore(&hpdev
->hbus
->config_lock
, flags
);
753 dev_err(&hpdev
->hbus
->hdev
->device
,
754 "Attempt to write beyond a function's config space.\n");
759 * hv_pcifront_read_config() - Read configuration space
760 * @bus: PCI Bus structure
761 * @devfn: Device/function
762 * @where: Offset from base
763 * @size: Byte/word/dword
764 * @val: Value to be read
766 * Return: PCIBIOS_SUCCESSFUL on success
767 * PCIBIOS_DEVICE_NOT_FOUND on failure
769 static int hv_pcifront_read_config(struct pci_bus
*bus
, unsigned int devfn
,
770 int where
, int size
, u32
*val
)
772 struct hv_pcibus_device
*hbus
=
773 container_of(bus
->sysdata
, struct hv_pcibus_device
, sysdata
);
774 struct hv_pci_dev
*hpdev
;
776 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(devfn
));
778 return PCIBIOS_DEVICE_NOT_FOUND
;
780 _hv_pcifront_read_config(hpdev
, where
, size
, val
);
783 return PCIBIOS_SUCCESSFUL
;
787 * hv_pcifront_write_config() - Write configuration space
788 * @bus: PCI Bus structure
789 * @devfn: Device/function
790 * @where: Offset from base
791 * @size: Byte/word/dword
792 * @val: Value to be written to device
794 * Return: PCIBIOS_SUCCESSFUL on success
795 * PCIBIOS_DEVICE_NOT_FOUND on failure
797 static int hv_pcifront_write_config(struct pci_bus
*bus
, unsigned int devfn
,
798 int where
, int size
, u32 val
)
800 struct hv_pcibus_device
*hbus
=
801 container_of(bus
->sysdata
, struct hv_pcibus_device
, sysdata
);
802 struct hv_pci_dev
*hpdev
;
804 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(devfn
));
806 return PCIBIOS_DEVICE_NOT_FOUND
;
808 _hv_pcifront_write_config(hpdev
, where
, size
, val
);
811 return PCIBIOS_SUCCESSFUL
;
814 /* PCIe operations */
815 static struct pci_ops hv_pcifront_ops
= {
816 .read
= hv_pcifront_read_config
,
817 .write
= hv_pcifront_write_config
,
820 /* Interrupt management hooks */
821 static void hv_int_desc_free(struct hv_pci_dev
*hpdev
,
822 struct tran_int_desc
*int_desc
)
824 struct pci_delete_interrupt
*int_pkt
;
826 struct pci_packet pkt
;
827 u8 buffer
[sizeof(struct pci_delete_interrupt
)];
830 memset(&ctxt
, 0, sizeof(ctxt
));
831 int_pkt
= (struct pci_delete_interrupt
*)&ctxt
.pkt
.message
;
832 int_pkt
->message_type
.type
=
833 PCI_DELETE_INTERRUPT_MESSAGE
;
834 int_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
835 int_pkt
->int_desc
= *int_desc
;
836 vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, int_pkt
, sizeof(*int_pkt
),
837 (unsigned long)&ctxt
.pkt
, VM_PKT_DATA_INBAND
, 0);
842 * hv_msi_free() - Free the MSI.
843 * @domain: The interrupt domain pointer
844 * @info: Extra MSI-related context
845 * @irq: Identifies the IRQ.
847 * The Hyper-V parent partition and hypervisor are tracking the
848 * messages that are in use, keeping the interrupt redirection
849 * table up to date. This callback sends a message that frees
850 * the IRT entry and related tracking nonsense.
852 static void hv_msi_free(struct irq_domain
*domain
, struct msi_domain_info
*info
,
855 struct hv_pcibus_device
*hbus
;
856 struct hv_pci_dev
*hpdev
;
857 struct pci_dev
*pdev
;
858 struct tran_int_desc
*int_desc
;
859 struct irq_data
*irq_data
= irq_domain_get_irq_data(domain
, irq
);
860 struct msi_desc
*msi
= irq_data_get_msi_desc(irq_data
);
862 pdev
= msi_desc_to_pci_dev(msi
);
864 int_desc
= irq_data_get_irq_chip_data(irq_data
);
868 irq_data
->chip_data
= NULL
;
869 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(pdev
->devfn
));
875 hv_int_desc_free(hpdev
, int_desc
);
879 static int hv_set_affinity(struct irq_data
*data
, const struct cpumask
*dest
,
882 struct irq_data
*parent
= data
->parent_data
;
884 return parent
->chip
->irq_set_affinity(parent
, dest
, force
);
887 static void hv_irq_mask(struct irq_data
*data
)
889 pci_msi_mask_irq(data
);
893 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
895 * @data: Describes the IRQ
897 * Build new a destination for the MSI and make a hypercall to
898 * update the Interrupt Redirection Table. "Device Logical ID"
899 * is built out of this PCI bus's instance GUID and the function
900 * number of the device.
902 static void hv_irq_unmask(struct irq_data
*data
)
904 struct msi_desc
*msi_desc
= irq_data_get_msi_desc(data
);
905 struct irq_cfg
*cfg
= irqd_cfg(data
);
906 struct retarget_msi_interrupt
*params
;
907 struct hv_pcibus_device
*hbus
;
908 struct cpumask
*dest
;
909 struct pci_bus
*pbus
;
910 struct pci_dev
*pdev
;
917 dest
= irq_data_get_effective_affinity_mask(data
);
918 pdev
= msi_desc_to_pci_dev(msi_desc
);
920 hbus
= container_of(pbus
->sysdata
, struct hv_pcibus_device
, sysdata
);
922 spin_lock_irqsave(&hbus
->retarget_msi_interrupt_lock
, flags
);
924 params
= &hbus
->retarget_msi_interrupt_params
;
925 memset(params
, 0, sizeof(*params
));
926 params
->partition_id
= HV_PARTITION_ID_SELF
;
927 params
->int_entry
.source
= 1; /* MSI(-X) */
928 params
->int_entry
.address
= msi_desc
->msg
.address_lo
;
929 params
->int_entry
.data
= msi_desc
->msg
.data
;
930 params
->device_id
= (hbus
->hdev
->dev_instance
.b
[5] << 24) |
931 (hbus
->hdev
->dev_instance
.b
[4] << 16) |
932 (hbus
->hdev
->dev_instance
.b
[7] << 8) |
933 (hbus
->hdev
->dev_instance
.b
[6] & 0xf8) |
934 PCI_FUNC(pdev
->devfn
);
935 params
->int_target
.vector
= cfg
->vector
;
938 * Honoring apic->irq_delivery_mode set to dest_Fixed by
939 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
940 * spurious interrupt storm. Not doing so does not seem to have a
941 * negative effect (yet?).
944 if (pci_protocol_version
>= PCI_PROTOCOL_VERSION_1_2
) {
946 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
947 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
948 * with >64 VP support.
949 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
950 * is not sufficient for this hypercall.
952 params
->int_target
.flags
|=
953 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET
;
954 params
->int_target
.vp_set
.valid_banks
=
955 (1ull << HV_VP_SET_BANK_COUNT_MAX
) - 1;
958 * var-sized hypercall, var-size starts after vp_mask (thus
959 * vp_set.format does not count, but vp_set.valid_banks does).
961 var_size
= 1 + HV_VP_SET_BANK_COUNT_MAX
;
963 for_each_cpu_and(cpu
, dest
, cpu_online_mask
) {
964 cpu_vmbus
= hv_cpu_number_to_vp_number(cpu
);
966 if (cpu_vmbus
>= HV_VP_SET_BANK_COUNT_MAX
* 64) {
967 dev_err(&hbus
->hdev
->device
,
968 "too high CPU %d", cpu_vmbus
);
973 params
->int_target
.vp_set
.masks
[cpu_vmbus
/ 64] |=
974 (1ULL << (cpu_vmbus
& 63));
977 for_each_cpu_and(cpu
, dest
, cpu_online_mask
) {
978 params
->int_target
.vp_mask
|=
979 (1ULL << hv_cpu_number_to_vp_number(cpu
));
983 res
= hv_do_hypercall(HVCALL_RETARGET_INTERRUPT
| (var_size
<< 17),
987 spin_unlock_irqrestore(&hbus
->retarget_msi_interrupt_lock
, flags
);
990 dev_err(&hbus
->hdev
->device
,
991 "%s() failed: %#llx", __func__
, res
);
995 pci_msi_unmask_irq(data
);
998 struct compose_comp_ctxt
{
999 struct hv_pci_compl comp_pkt
;
1000 struct tran_int_desc int_desc
;
1003 static void hv_pci_compose_compl(void *context
, struct pci_response
*resp
,
1004 int resp_packet_size
)
1006 struct compose_comp_ctxt
*comp_pkt
= context
;
1007 struct pci_create_int_response
*int_resp
=
1008 (struct pci_create_int_response
*)resp
;
1010 comp_pkt
->comp_pkt
.completion_status
= resp
->status
;
1011 comp_pkt
->int_desc
= int_resp
->int_desc
;
1012 complete(&comp_pkt
->comp_pkt
.host_event
);
1015 static u32
hv_compose_msi_req_v1(
1016 struct pci_create_interrupt
*int_pkt
, struct cpumask
*affinity
,
1017 u32 slot
, u8 vector
)
1019 int_pkt
->message_type
.type
= PCI_CREATE_INTERRUPT_MESSAGE
;
1020 int_pkt
->wslot
.slot
= slot
;
1021 int_pkt
->int_desc
.vector
= vector
;
1022 int_pkt
->int_desc
.vector_count
= 1;
1023 int_pkt
->int_desc
.delivery_mode
= dest_Fixed
;
1026 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1029 int_pkt
->int_desc
.cpu_mask
= CPU_AFFINITY_ALL
;
1031 return sizeof(*int_pkt
);
1034 static u32
hv_compose_msi_req_v2(
1035 struct pci_create_interrupt2
*int_pkt
, struct cpumask
*affinity
,
1036 u32 slot
, u8 vector
)
1040 int_pkt
->message_type
.type
= PCI_CREATE_INTERRUPT_MESSAGE2
;
1041 int_pkt
->wslot
.slot
= slot
;
1042 int_pkt
->int_desc
.vector
= vector
;
1043 int_pkt
->int_desc
.vector_count
= 1;
1044 int_pkt
->int_desc
.delivery_mode
= dest_Fixed
;
1047 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1048 * by subsequent retarget in hv_irq_unmask().
1050 cpu
= cpumask_first_and(affinity
, cpu_online_mask
);
1051 int_pkt
->int_desc
.processor_array
[0] =
1052 hv_cpu_number_to_vp_number(cpu
);
1053 int_pkt
->int_desc
.processor_count
= 1;
1055 return sizeof(*int_pkt
);
1059 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1060 * @data: Everything about this MSI
1061 * @msg: Buffer that is filled in by this function
1063 * This function unpacks the IRQ looking for target CPU set, IDT
1064 * vector and mode and sends a message to the parent partition
1065 * asking for a mapping for that tuple in this partition. The
1066 * response supplies a data value and address to which that data
1067 * should be written to trigger that interrupt.
1069 static void hv_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
1071 struct irq_cfg
*cfg
= irqd_cfg(data
);
1072 struct hv_pcibus_device
*hbus
;
1073 struct hv_pci_dev
*hpdev
;
1074 struct pci_bus
*pbus
;
1075 struct pci_dev
*pdev
;
1076 struct cpumask
*dest
;
1077 unsigned long flags
;
1078 struct compose_comp_ctxt comp
;
1079 struct tran_int_desc
*int_desc
;
1081 struct pci_packet pci_pkt
;
1083 struct pci_create_interrupt v1
;
1084 struct pci_create_interrupt2 v2
;
1091 pdev
= msi_desc_to_pci_dev(irq_data_get_msi_desc(data
));
1092 dest
= irq_data_get_effective_affinity_mask(data
);
1094 hbus
= container_of(pbus
->sysdata
, struct hv_pcibus_device
, sysdata
);
1095 hpdev
= get_pcichild_wslot(hbus
, devfn_to_wslot(pdev
->devfn
));
1097 goto return_null_message
;
1099 /* Free any previous message that might have already been composed. */
1100 if (data
->chip_data
) {
1101 int_desc
= data
->chip_data
;
1102 data
->chip_data
= NULL
;
1103 hv_int_desc_free(hpdev
, int_desc
);
1106 int_desc
= kzalloc(sizeof(*int_desc
), GFP_ATOMIC
);
1108 goto drop_reference
;
1110 memset(&ctxt
, 0, sizeof(ctxt
));
1111 init_completion(&comp
.comp_pkt
.host_event
);
1112 ctxt
.pci_pkt
.completion_func
= hv_pci_compose_compl
;
1113 ctxt
.pci_pkt
.compl_ctxt
= &comp
;
1115 switch (pci_protocol_version
) {
1116 case PCI_PROTOCOL_VERSION_1_1
:
1117 size
= hv_compose_msi_req_v1(&ctxt
.int_pkts
.v1
,
1119 hpdev
->desc
.win_slot
.slot
,
1123 case PCI_PROTOCOL_VERSION_1_2
:
1124 size
= hv_compose_msi_req_v2(&ctxt
.int_pkts
.v2
,
1126 hpdev
->desc
.win_slot
.slot
,
1131 /* As we only negotiate protocol versions known to this driver,
1132 * this path should never hit. However, this is it not a hot
1133 * path so we print a message to aid future updates.
1135 dev_err(&hbus
->hdev
->device
,
1136 "Unexpected vPCI protocol, update driver.");
1140 ret
= vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, &ctxt
.int_pkts
,
1141 size
, (unsigned long)&ctxt
.pci_pkt
,
1143 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1145 dev_err(&hbus
->hdev
->device
,
1146 "Sending request for interrupt failed: 0x%x",
1147 comp
.comp_pkt
.completion_status
);
1152 * Since this function is called with IRQ locks held, can't
1153 * do normal wait for completion; instead poll.
1155 while (!try_wait_for_completion(&comp
.comp_pkt
.host_event
)) {
1156 /* 0xFFFF means an invalid PCI VENDOR ID. */
1157 if (hv_pcifront_get_vendor_id(hpdev
) == 0xFFFF) {
1158 dev_err_once(&hbus
->hdev
->device
,
1159 "the device has gone\n");
1164 * When the higher level interrupt code calls us with
1165 * interrupt disabled, we must poll the channel by calling
1166 * the channel callback directly when channel->target_cpu is
1167 * the current CPU. When the higher level interrupt code
1168 * calls us with interrupt enabled, let's add the
1169 * local_irq_save()/restore() to avoid race:
1170 * hv_pci_onchannelcallback() can also run in tasklet.
1172 local_irq_save(flags
);
1174 if (hbus
->hdev
->channel
->target_cpu
== smp_processor_id())
1175 hv_pci_onchannelcallback(hbus
);
1177 local_irq_restore(flags
);
1179 if (hpdev
->state
== hv_pcichild_ejecting
) {
1180 dev_err_once(&hbus
->hdev
->device
,
1181 "the device is being ejected\n");
1188 if (comp
.comp_pkt
.completion_status
< 0) {
1189 dev_err(&hbus
->hdev
->device
,
1190 "Request for interrupt failed: 0x%x",
1191 comp
.comp_pkt
.completion_status
);
1196 * Record the assignment so that this can be unwound later. Using
1197 * irq_set_chip_data() here would be appropriate, but the lock it takes
1200 *int_desc
= comp
.int_desc
;
1201 data
->chip_data
= int_desc
;
1203 /* Pass up the result. */
1204 msg
->address_hi
= comp
.int_desc
.address
>> 32;
1205 msg
->address_lo
= comp
.int_desc
.address
& 0xffffffff;
1206 msg
->data
= comp
.int_desc
.data
;
1208 put_pcichild(hpdev
);
1214 put_pcichild(hpdev
);
1215 return_null_message
:
1216 msg
->address_hi
= 0;
1217 msg
->address_lo
= 0;
1221 /* HW Interrupt Chip Descriptor */
1222 static struct irq_chip hv_msi_irq_chip
= {
1223 .name
= "Hyper-V PCIe MSI",
1224 .irq_compose_msi_msg
= hv_compose_msi_msg
,
1225 .irq_set_affinity
= hv_set_affinity
,
1226 .irq_ack
= irq_chip_ack_parent
,
1227 .irq_mask
= hv_irq_mask
,
1228 .irq_unmask
= hv_irq_unmask
,
1231 static irq_hw_number_t
hv_msi_domain_ops_get_hwirq(struct msi_domain_info
*info
,
1232 msi_alloc_info_t
*arg
)
1234 return arg
->msi_hwirq
;
1237 static struct msi_domain_ops hv_msi_ops
= {
1238 .get_hwirq
= hv_msi_domain_ops_get_hwirq
,
1239 .msi_prepare
= pci_msi_prepare
,
1240 .set_desc
= pci_msi_set_desc
,
1241 .msi_free
= hv_msi_free
,
1245 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1246 * @hbus: The root PCI bus
1248 * This function creates an IRQ domain which will be used for
1249 * interrupts from devices that have been passed through. These
1250 * devices only support MSI and MSI-X, not line-based interrupts
1251 * or simulations of line-based interrupts through PCIe's
1252 * fabric-layer messages. Because interrupts are remapped, we
1253 * can support multi-message MSI here.
1255 * Return: '0' on success and error value on failure
1257 static int hv_pcie_init_irq_domain(struct hv_pcibus_device
*hbus
)
1259 hbus
->msi_info
.chip
= &hv_msi_irq_chip
;
1260 hbus
->msi_info
.ops
= &hv_msi_ops
;
1261 hbus
->msi_info
.flags
= (MSI_FLAG_USE_DEF_DOM_OPS
|
1262 MSI_FLAG_USE_DEF_CHIP_OPS
| MSI_FLAG_MULTI_PCI_MSI
|
1264 hbus
->msi_info
.handler
= handle_edge_irq
;
1265 hbus
->msi_info
.handler_name
= "edge";
1266 hbus
->msi_info
.data
= hbus
;
1267 hbus
->irq_domain
= pci_msi_create_irq_domain(hbus
->sysdata
.fwnode
,
1270 if (!hbus
->irq_domain
) {
1271 dev_err(&hbus
->hdev
->device
,
1272 "Failed to build an MSI IRQ domain\n");
1280 * get_bar_size() - Get the address space consumed by a BAR
1281 * @bar_val: Value that a BAR returned after -1 was written
1284 * This function returns the size of the BAR, rounded up to 1
1285 * page. It has to be rounded up because the hypervisor's page
1286 * table entry that maps the BAR into the VM can't specify an
1287 * offset within a page. The invariant is that the hypervisor
1288 * must place any BARs of smaller than page length at the
1289 * beginning of a page.
1291 * Return: Size in bytes of the consumed MMIO space.
1293 static u64
get_bar_size(u64 bar_val
)
1295 return round_up((1 + ~(bar_val
& PCI_BASE_ADDRESS_MEM_MASK
)),
1300 * survey_child_resources() - Total all MMIO requirements
1301 * @hbus: Root PCI bus, as understood by this driver
1303 static void survey_child_resources(struct hv_pcibus_device
*hbus
)
1305 struct hv_pci_dev
*hpdev
;
1306 resource_size_t bar_size
= 0;
1307 unsigned long flags
;
1308 struct completion
*event
;
1312 /* If nobody is waiting on the answer, don't compute it. */
1313 event
= xchg(&hbus
->survey_event
, NULL
);
1317 /* If the answer has already been computed, go with it. */
1318 if (hbus
->low_mmio_space
|| hbus
->high_mmio_space
) {
1323 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1326 * Due to an interesting quirk of the PCI spec, all memory regions
1327 * for a child device are a power of 2 in size and aligned in memory,
1328 * so it's sufficient to just add them up without tracking alignment.
1330 list_for_each_entry(hpdev
, &hbus
->children
, list_entry
) {
1331 for (i
= 0; i
< 6; i
++) {
1332 if (hpdev
->probed_bar
[i
] & PCI_BASE_ADDRESS_SPACE_IO
)
1333 dev_err(&hbus
->hdev
->device
,
1334 "There's an I/O BAR in this list!\n");
1336 if (hpdev
->probed_bar
[i
] != 0) {
1338 * A probed BAR has all the upper bits set that
1342 bar_val
= hpdev
->probed_bar
[i
];
1343 if (bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
1345 ((u64
)hpdev
->probed_bar
[++i
] << 32);
1347 bar_val
|= 0xffffffff00000000ULL
;
1349 bar_size
= get_bar_size(bar_val
);
1351 if (bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
)
1352 hbus
->high_mmio_space
+= bar_size
;
1354 hbus
->low_mmio_space
+= bar_size
;
1359 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1364 * prepopulate_bars() - Fill in BARs with defaults
1365 * @hbus: Root PCI bus, as understood by this driver
1367 * The core PCI driver code seems much, much happier if the BARs
1368 * for a device have values upon first scan. So fill them in.
1369 * The algorithm below works down from large sizes to small,
1370 * attempting to pack the assignments optimally. The assumption,
1371 * enforced in other parts of the code, is that the beginning of
1372 * the memory-mapped I/O space will be aligned on the largest
1375 static void prepopulate_bars(struct hv_pcibus_device
*hbus
)
1377 resource_size_t high_size
= 0;
1378 resource_size_t low_size
= 0;
1379 resource_size_t high_base
= 0;
1380 resource_size_t low_base
= 0;
1381 resource_size_t bar_size
;
1382 struct hv_pci_dev
*hpdev
;
1383 unsigned long flags
;
1389 if (hbus
->low_mmio_space
) {
1390 low_size
= 1ULL << (63 - __builtin_clzll(hbus
->low_mmio_space
));
1391 low_base
= hbus
->low_mmio_res
->start
;
1394 if (hbus
->high_mmio_space
) {
1396 (63 - __builtin_clzll(hbus
->high_mmio_space
));
1397 high_base
= hbus
->high_mmio_res
->start
;
1400 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1402 /* Pick addresses for the BARs. */
1404 list_for_each_entry(hpdev
, &hbus
->children
, list_entry
) {
1405 for (i
= 0; i
< 6; i
++) {
1406 bar_val
= hpdev
->probed_bar
[i
];
1409 high
= bar_val
& PCI_BASE_ADDRESS_MEM_TYPE_64
;
1412 ((u64
)hpdev
->probed_bar
[i
+ 1]
1415 bar_val
|= 0xffffffffULL
<< 32;
1417 bar_size
= get_bar_size(bar_val
);
1419 if (high_size
!= bar_size
) {
1423 _hv_pcifront_write_config(hpdev
,
1424 PCI_BASE_ADDRESS_0
+ (4 * i
),
1426 (u32
)(high_base
& 0xffffff00));
1428 _hv_pcifront_write_config(hpdev
,
1429 PCI_BASE_ADDRESS_0
+ (4 * i
),
1430 4, (u32
)(high_base
>> 32));
1431 high_base
+= bar_size
;
1433 if (low_size
!= bar_size
)
1435 _hv_pcifront_write_config(hpdev
,
1436 PCI_BASE_ADDRESS_0
+ (4 * i
),
1438 (u32
)(low_base
& 0xffffff00));
1439 low_base
+= bar_size
;
1442 if (high_size
<= 1 && low_size
<= 1) {
1443 /* Set the memory enable bit. */
1444 _hv_pcifront_read_config(hpdev
, PCI_COMMAND
, 2,
1446 command
|= PCI_COMMAND_MEMORY
;
1447 _hv_pcifront_write_config(hpdev
, PCI_COMMAND
, 2,
1455 } while (high_size
|| low_size
);
1457 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1461 * create_root_hv_pci_bus() - Expose a new root PCI bus
1462 * @hbus: Root PCI bus, as understood by this driver
1464 * Return: 0 on success, -errno on failure
1466 static int create_root_hv_pci_bus(struct hv_pcibus_device
*hbus
)
1468 /* Register the device */
1469 hbus
->pci_bus
= pci_create_root_bus(&hbus
->hdev
->device
,
1470 0, /* bus number is always zero */
1473 &hbus
->resources_for_children
);
1477 hbus
->pci_bus
->msi
= &hbus
->msi_chip
;
1478 hbus
->pci_bus
->msi
->dev
= &hbus
->hdev
->device
;
1480 pci_lock_rescan_remove();
1481 pci_scan_child_bus(hbus
->pci_bus
);
1482 pci_bus_assign_resources(hbus
->pci_bus
);
1483 pci_bus_add_devices(hbus
->pci_bus
);
1484 pci_unlock_rescan_remove();
1485 hbus
->state
= hv_pcibus_installed
;
1489 struct q_res_req_compl
{
1490 struct completion host_event
;
1491 struct hv_pci_dev
*hpdev
;
1495 * q_resource_requirements() - Query Resource Requirements
1496 * @context: The completion context.
1497 * @resp: The response that came from the host.
1498 * @resp_packet_size: The size in bytes of resp.
1500 * This function is invoked on completion of a Query Resource
1501 * Requirements packet.
1503 static void q_resource_requirements(void *context
, struct pci_response
*resp
,
1504 int resp_packet_size
)
1506 struct q_res_req_compl
*completion
= context
;
1507 struct pci_q_res_req_response
*q_res_req
=
1508 (struct pci_q_res_req_response
*)resp
;
1511 if (resp
->status
< 0) {
1512 dev_err(&completion
->hpdev
->hbus
->hdev
->device
,
1513 "query resource requirements failed: %x\n",
1516 for (i
= 0; i
< 6; i
++) {
1517 completion
->hpdev
->probed_bar
[i
] =
1518 q_res_req
->probed_bar
[i
];
1522 complete(&completion
->host_event
);
1526 * new_pcichild_device() - Create a new child device
1527 * @hbus: The internal struct tracking this root PCI bus.
1528 * @desc: The information supplied so far from the host
1531 * This function creates the tracking structure for a new child
1532 * device and kicks off the process of figuring out what it is.
1534 * Return: Pointer to the new tracking struct
1536 static struct hv_pci_dev
*new_pcichild_device(struct hv_pcibus_device
*hbus
,
1537 struct pci_function_description
*desc
)
1539 struct hv_pci_dev
*hpdev
;
1540 struct pci_child_message
*res_req
;
1541 struct q_res_req_compl comp_pkt
;
1543 struct pci_packet init_packet
;
1544 u8 buffer
[sizeof(struct pci_child_message
)];
1546 unsigned long flags
;
1549 hpdev
= kzalloc(sizeof(*hpdev
), GFP_KERNEL
);
1555 memset(&pkt
, 0, sizeof(pkt
));
1556 init_completion(&comp_pkt
.host_event
);
1557 comp_pkt
.hpdev
= hpdev
;
1558 pkt
.init_packet
.compl_ctxt
= &comp_pkt
;
1559 pkt
.init_packet
.completion_func
= q_resource_requirements
;
1560 res_req
= (struct pci_child_message
*)&pkt
.init_packet
.message
;
1561 res_req
->message_type
.type
= PCI_QUERY_RESOURCE_REQUIREMENTS
;
1562 res_req
->wslot
.slot
= desc
->win_slot
.slot
;
1564 ret
= vmbus_sendpacket(hbus
->hdev
->channel
, res_req
,
1565 sizeof(struct pci_child_message
),
1566 (unsigned long)&pkt
.init_packet
,
1568 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1572 if (wait_for_response(hbus
->hdev
, &comp_pkt
.host_event
))
1575 hpdev
->desc
= *desc
;
1576 refcount_set(&hpdev
->refs
, 1);
1577 get_pcichild(hpdev
);
1578 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1580 list_add_tail(&hpdev
->list_entry
, &hbus
->children
);
1581 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1590 * get_pcichild_wslot() - Find device from slot
1591 * @hbus: Root PCI bus, as understood by this driver
1592 * @wslot: Location on the bus
1594 * This function looks up a PCI device and returns the internal
1595 * representation of it. It acquires a reference on it, so that
1596 * the device won't be deleted while somebody is using it. The
1597 * caller is responsible for calling put_pcichild() to release
1600 * Return: Internal representation of a PCI device
1602 static struct hv_pci_dev
*get_pcichild_wslot(struct hv_pcibus_device
*hbus
,
1605 unsigned long flags
;
1606 struct hv_pci_dev
*iter
, *hpdev
= NULL
;
1608 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1609 list_for_each_entry(iter
, &hbus
->children
, list_entry
) {
1610 if (iter
->desc
.win_slot
.slot
== wslot
) {
1612 get_pcichild(hpdev
);
1616 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1622 * pci_devices_present_work() - Handle new list of child devices
1623 * @work: Work struct embedded in struct hv_dr_work
1625 * "Bus Relations" is the Windows term for "children of this
1626 * bus." The terminology is preserved here for people trying to
1627 * debug the interaction between Hyper-V and Linux. This
1628 * function is called when the parent partition reports a list
1629 * of functions that should be observed under this PCI Express
1632 * This function updates the list, and must tolerate being
1633 * called multiple times with the same information. The typical
1634 * number of child devices is one, with very atypical cases
1635 * involving three or four, so the algorithms used here can be
1636 * simple and inefficient.
1638 * It must also treat the omission of a previously observed device as
1639 * notification that the device no longer exists.
1641 * Note that this function is serialized with hv_eject_device_work(),
1642 * because both are pushed to the ordered workqueue hbus->wq.
1644 static void pci_devices_present_work(struct work_struct
*work
)
1648 struct pci_function_description
*new_desc
;
1649 struct hv_pci_dev
*hpdev
;
1650 struct hv_pcibus_device
*hbus
;
1651 struct list_head removed
;
1652 struct hv_dr_work
*dr_wrk
;
1653 struct hv_dr_state
*dr
= NULL
;
1654 unsigned long flags
;
1656 dr_wrk
= container_of(work
, struct hv_dr_work
, wrk
);
1660 INIT_LIST_HEAD(&removed
);
1662 /* Pull this off the queue and process it if it was the last one. */
1663 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1664 while (!list_empty(&hbus
->dr_list
)) {
1665 dr
= list_first_entry(&hbus
->dr_list
, struct hv_dr_state
,
1667 list_del(&dr
->list_entry
);
1669 /* Throw this away if the list still has stuff in it. */
1670 if (!list_empty(&hbus
->dr_list
)) {
1675 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1682 /* First, mark all existing children as reported missing. */
1683 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1684 list_for_each_entry(hpdev
, &hbus
->children
, list_entry
) {
1685 hpdev
->reported_missing
= true;
1687 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1689 /* Next, add back any reported devices. */
1690 for (child_no
= 0; child_no
< dr
->device_count
; child_no
++) {
1692 new_desc
= &dr
->func
[child_no
];
1694 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1695 list_for_each_entry(hpdev
, &hbus
->children
, list_entry
) {
1696 if ((hpdev
->desc
.win_slot
.slot
== new_desc
->win_slot
.slot
) &&
1697 (hpdev
->desc
.v_id
== new_desc
->v_id
) &&
1698 (hpdev
->desc
.d_id
== new_desc
->d_id
) &&
1699 (hpdev
->desc
.ser
== new_desc
->ser
)) {
1700 hpdev
->reported_missing
= false;
1704 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1707 hpdev
= new_pcichild_device(hbus
, new_desc
);
1709 dev_err(&hbus
->hdev
->device
,
1710 "couldn't record a child device.\n");
1714 /* Move missing children to a list on the stack. */
1715 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1718 list_for_each_entry(hpdev
, &hbus
->children
, list_entry
) {
1719 if (hpdev
->reported_missing
) {
1721 put_pcichild(hpdev
);
1722 list_move_tail(&hpdev
->list_entry
, &removed
);
1727 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1729 /* Delete everything that should no longer exist. */
1730 while (!list_empty(&removed
)) {
1731 hpdev
= list_first_entry(&removed
, struct hv_pci_dev
,
1733 list_del(&hpdev
->list_entry
);
1734 put_pcichild(hpdev
);
1737 switch (hbus
->state
) {
1738 case hv_pcibus_installed
:
1740 * Tell the core to rescan bus
1741 * because there may have been changes.
1743 pci_lock_rescan_remove();
1744 pci_scan_child_bus(hbus
->pci_bus
);
1745 pci_unlock_rescan_remove();
1748 case hv_pcibus_init
:
1749 case hv_pcibus_probed
:
1750 survey_child_resources(hbus
);
1762 * hv_pci_devices_present() - Handles list of new children
1763 * @hbus: Root PCI bus, as understood by this driver
1764 * @relations: Packet from host listing children
1766 * This function is invoked whenever a new list of devices for
1769 static void hv_pci_devices_present(struct hv_pcibus_device
*hbus
,
1770 struct pci_bus_relations
*relations
)
1772 struct hv_dr_state
*dr
;
1773 struct hv_dr_work
*dr_wrk
;
1774 unsigned long flags
;
1777 dr_wrk
= kzalloc(sizeof(*dr_wrk
), GFP_NOWAIT
);
1781 dr
= kzalloc(offsetof(struct hv_dr_state
, func
) +
1782 (sizeof(struct pci_function_description
) *
1783 (relations
->device_count
)), GFP_NOWAIT
);
1789 INIT_WORK(&dr_wrk
->wrk
, pci_devices_present_work
);
1791 dr
->device_count
= relations
->device_count
;
1792 if (dr
->device_count
!= 0) {
1793 memcpy(dr
->func
, relations
->func
,
1794 sizeof(struct pci_function_description
) *
1798 spin_lock_irqsave(&hbus
->device_list_lock
, flags
);
1800 * If pending_dr is true, we have already queued a work,
1801 * which will see the new dr. Otherwise, we need to
1804 pending_dr
= !list_empty(&hbus
->dr_list
);
1805 list_add_tail(&dr
->list_entry
, &hbus
->dr_list
);
1806 spin_unlock_irqrestore(&hbus
->device_list_lock
, flags
);
1812 queue_work(hbus
->wq
, &dr_wrk
->wrk
);
1817 * hv_eject_device_work() - Asynchronously handles ejection
1818 * @work: Work struct embedded in internal device struct
1820 * This function handles ejecting a device. Windows will
1821 * attempt to gracefully eject a device, waiting 60 seconds to
1822 * hear back from the guest OS that this completed successfully.
1823 * If this timer expires, the device will be forcibly removed.
1825 static void hv_eject_device_work(struct work_struct
*work
)
1827 struct pci_eject_response
*ejct_pkt
;
1828 struct hv_pci_dev
*hpdev
;
1829 struct pci_dev
*pdev
;
1830 unsigned long flags
;
1833 struct pci_packet pkt
;
1834 u8 buffer
[sizeof(struct pci_eject_response
)];
1837 hpdev
= container_of(work
, struct hv_pci_dev
, wrk
);
1839 WARN_ON(hpdev
->state
!= hv_pcichild_ejecting
);
1842 * Ejection can come before or after the PCI bus has been set up, so
1843 * attempt to find it and tear down the bus state, if it exists. This
1844 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1845 * because hbus->pci_bus may not exist yet.
1847 wslot
= wslot_to_devfn(hpdev
->desc
.win_slot
.slot
);
1848 pdev
= pci_get_domain_bus_and_slot(hpdev
->hbus
->sysdata
.domain
, 0,
1851 pci_lock_rescan_remove();
1852 pci_stop_and_remove_bus_device(pdev
);
1854 pci_unlock_rescan_remove();
1857 spin_lock_irqsave(&hpdev
->hbus
->device_list_lock
, flags
);
1858 list_del(&hpdev
->list_entry
);
1859 spin_unlock_irqrestore(&hpdev
->hbus
->device_list_lock
, flags
);
1861 memset(&ctxt
, 0, sizeof(ctxt
));
1862 ejct_pkt
= (struct pci_eject_response
*)&ctxt
.pkt
.message
;
1863 ejct_pkt
->message_type
.type
= PCI_EJECTION_COMPLETE
;
1864 ejct_pkt
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
1865 vmbus_sendpacket(hpdev
->hbus
->hdev
->channel
, ejct_pkt
,
1866 sizeof(*ejct_pkt
), (unsigned long)&ctxt
.pkt
,
1867 VM_PKT_DATA_INBAND
, 0);
1869 put_pcichild(hpdev
);
1870 put_pcichild(hpdev
);
1871 put_hvpcibus(hpdev
->hbus
);
1875 * hv_pci_eject_device() - Handles device ejection
1876 * @hpdev: Internal device tracking struct
1878 * This function is invoked when an ejection packet arrives. It
1879 * just schedules work so that we don't re-enter the packet
1880 * delivery code handling the ejection.
1882 static void hv_pci_eject_device(struct hv_pci_dev
*hpdev
)
1884 hpdev
->state
= hv_pcichild_ejecting
;
1885 get_pcichild(hpdev
);
1886 INIT_WORK(&hpdev
->wrk
, hv_eject_device_work
);
1887 get_hvpcibus(hpdev
->hbus
);
1888 queue_work(hpdev
->hbus
->wq
, &hpdev
->wrk
);
1892 * hv_pci_onchannelcallback() - Handles incoming packets
1893 * @context: Internal bus tracking struct
1895 * This function is invoked whenever the host sends a packet to
1896 * this channel (which is private to this root PCI bus).
1898 static void hv_pci_onchannelcallback(void *context
)
1900 const int packet_size
= 0x100;
1902 struct hv_pcibus_device
*hbus
= context
;
1905 struct vmpacket_descriptor
*desc
;
1906 unsigned char *buffer
;
1907 int bufferlen
= packet_size
;
1908 struct pci_packet
*comp_packet
;
1909 struct pci_response
*response
;
1910 struct pci_incoming_message
*new_message
;
1911 struct pci_bus_relations
*bus_rel
;
1912 struct pci_dev_incoming
*dev_message
;
1913 struct hv_pci_dev
*hpdev
;
1915 buffer
= kmalloc(bufferlen
, GFP_ATOMIC
);
1920 ret
= vmbus_recvpacket_raw(hbus
->hdev
->channel
, buffer
,
1921 bufferlen
, &bytes_recvd
, &req_id
);
1923 if (ret
== -ENOBUFS
) {
1925 /* Handle large packet */
1926 bufferlen
= bytes_recvd
;
1927 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1933 /* Zero length indicates there are no more packets. */
1934 if (ret
|| !bytes_recvd
)
1938 * All incoming packets must be at least as large as a
1941 if (bytes_recvd
<= sizeof(struct pci_response
))
1943 desc
= (struct vmpacket_descriptor
*)buffer
;
1945 switch (desc
->type
) {
1949 * The host is trusted, and thus it's safe to interpret
1950 * this transaction ID as a pointer.
1952 comp_packet
= (struct pci_packet
*)req_id
;
1953 response
= (struct pci_response
*)buffer
;
1954 comp_packet
->completion_func(comp_packet
->compl_ctxt
,
1959 case VM_PKT_DATA_INBAND
:
1961 new_message
= (struct pci_incoming_message
*)buffer
;
1962 switch (new_message
->message_type
.type
) {
1963 case PCI_BUS_RELATIONS
:
1965 bus_rel
= (struct pci_bus_relations
*)buffer
;
1967 offsetof(struct pci_bus_relations
, func
) +
1968 (sizeof(struct pci_function_description
) *
1969 (bus_rel
->device_count
))) {
1970 dev_err(&hbus
->hdev
->device
,
1971 "bus relations too small\n");
1975 hv_pci_devices_present(hbus
, bus_rel
);
1980 dev_message
= (struct pci_dev_incoming
*)buffer
;
1981 hpdev
= get_pcichild_wslot(hbus
,
1982 dev_message
->wslot
.slot
);
1984 hv_pci_eject_device(hpdev
);
1985 put_pcichild(hpdev
);
1990 dev_warn(&hbus
->hdev
->device
,
1991 "Unimplemented protocol message %x\n",
1992 new_message
->message_type
.type
);
1998 dev_err(&hbus
->hdev
->device
,
1999 "unhandled packet type %d, tid %llx len %d\n",
2000 desc
->type
, req_id
, bytes_recvd
);
2009 * hv_pci_protocol_negotiation() - Set up protocol
2010 * @hdev: VMBus's tracking struct for this root PCI bus
2012 * This driver is intended to support running on Windows 10
2013 * (server) and later versions. It will not run on earlier
2014 * versions, as they assume that many of the operations which
2015 * Linux needs accomplished with a spinlock held were done via
2016 * asynchronous messaging via VMBus. Windows 10 increases the
2017 * surface area of PCI emulation so that these actions can take
2018 * place by suspending a virtual processor for their duration.
2020 * This function negotiates the channel protocol version,
2021 * failing if the host doesn't support the necessary protocol
2024 static int hv_pci_protocol_negotiation(struct hv_device
*hdev
)
2026 struct pci_version_request
*version_req
;
2027 struct hv_pci_compl comp_pkt
;
2028 struct pci_packet
*pkt
;
2033 * Initiate the handshake with the host and negotiate
2034 * a version that the host can support. We start with the
2035 * highest version number and go down if the host cannot
2038 pkt
= kzalloc(sizeof(*pkt
) + sizeof(*version_req
), GFP_KERNEL
);
2042 init_completion(&comp_pkt
.host_event
);
2043 pkt
->completion_func
= hv_pci_generic_compl
;
2044 pkt
->compl_ctxt
= &comp_pkt
;
2045 version_req
= (struct pci_version_request
*)&pkt
->message
;
2046 version_req
->message_type
.type
= PCI_QUERY_PROTOCOL_VERSION
;
2048 for (i
= 0; i
< ARRAY_SIZE(pci_protocol_versions
); i
++) {
2049 version_req
->protocol_version
= pci_protocol_versions
[i
];
2050 ret
= vmbus_sendpacket(hdev
->channel
, version_req
,
2051 sizeof(struct pci_version_request
),
2052 (unsigned long)pkt
, VM_PKT_DATA_INBAND
,
2053 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2055 ret
= wait_for_response(hdev
, &comp_pkt
.host_event
);
2058 dev_err(&hdev
->device
,
2059 "PCI Pass-through VSP failed to request version: %d",
2064 if (comp_pkt
.completion_status
>= 0) {
2065 pci_protocol_version
= pci_protocol_versions
[i
];
2066 dev_info(&hdev
->device
,
2067 "PCI VMBus probing: Using version %#x\n",
2068 pci_protocol_version
);
2072 if (comp_pkt
.completion_status
!= STATUS_REVISION_MISMATCH
) {
2073 dev_err(&hdev
->device
,
2074 "PCI Pass-through VSP failed version request: %#x",
2075 comp_pkt
.completion_status
);
2080 reinit_completion(&comp_pkt
.host_event
);
2083 dev_err(&hdev
->device
,
2084 "PCI pass-through VSP failed to find supported version");
2093 * hv_pci_free_bridge_windows() - Release memory regions for the
2095 * @hbus: Root PCI bus, as understood by this driver
2097 static void hv_pci_free_bridge_windows(struct hv_pcibus_device
*hbus
)
2100 * Set the resources back to the way they looked when they
2101 * were allocated by setting IORESOURCE_BUSY again.
2104 if (hbus
->low_mmio_space
&& hbus
->low_mmio_res
) {
2105 hbus
->low_mmio_res
->flags
|= IORESOURCE_BUSY
;
2106 vmbus_free_mmio(hbus
->low_mmio_res
->start
,
2107 resource_size(hbus
->low_mmio_res
));
2110 if (hbus
->high_mmio_space
&& hbus
->high_mmio_res
) {
2111 hbus
->high_mmio_res
->flags
|= IORESOURCE_BUSY
;
2112 vmbus_free_mmio(hbus
->high_mmio_res
->start
,
2113 resource_size(hbus
->high_mmio_res
));
2118 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2120 * @hbus: Root PCI bus, as understood by this driver
2122 * This function calls vmbus_allocate_mmio(), which is itself a
2123 * bit of a compromise. Ideally, we might change the pnp layer
2124 * in the kernel such that it comprehends either PCI devices
2125 * which are "grandchildren of ACPI," with some intermediate bus
2126 * node (in this case, VMBus) or change it such that it
2127 * understands VMBus. The pnp layer, however, has been declared
2128 * deprecated, and not subject to change.
2130 * The workaround, implemented here, is to ask VMBus to allocate
2131 * MMIO space for this bus. VMBus itself knows which ranges are
2132 * appropriate by looking at its own ACPI objects. Then, after
2133 * these ranges are claimed, they're modified to look like they
2134 * would have looked if the ACPI and pnp code had allocated
2135 * bridge windows. These descriptors have to exist in this form
2136 * in order to satisfy the code which will get invoked when the
2137 * endpoint PCI function driver calls request_mem_region() or
2138 * request_mem_region_exclusive().
2140 * Return: 0 on success, -errno on failure
2142 static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device
*hbus
)
2144 resource_size_t align
;
2147 if (hbus
->low_mmio_space
) {
2148 align
= 1ULL << (63 - __builtin_clzll(hbus
->low_mmio_space
));
2149 ret
= vmbus_allocate_mmio(&hbus
->low_mmio_res
, hbus
->hdev
, 0,
2150 (u64
)(u32
)0xffffffff,
2151 hbus
->low_mmio_space
,
2154 dev_err(&hbus
->hdev
->device
,
2155 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2156 hbus
->low_mmio_space
);
2160 /* Modify this resource to become a bridge window. */
2161 hbus
->low_mmio_res
->flags
|= IORESOURCE_WINDOW
;
2162 hbus
->low_mmio_res
->flags
&= ~IORESOURCE_BUSY
;
2163 pci_add_resource(&hbus
->resources_for_children
,
2164 hbus
->low_mmio_res
);
2167 if (hbus
->high_mmio_space
) {
2168 align
= 1ULL << (63 - __builtin_clzll(hbus
->high_mmio_space
));
2169 ret
= vmbus_allocate_mmio(&hbus
->high_mmio_res
, hbus
->hdev
,
2171 hbus
->high_mmio_space
, align
,
2174 dev_err(&hbus
->hdev
->device
,
2175 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2176 hbus
->high_mmio_space
);
2177 goto release_low_mmio
;
2180 /* Modify this resource to become a bridge window. */
2181 hbus
->high_mmio_res
->flags
|= IORESOURCE_WINDOW
;
2182 hbus
->high_mmio_res
->flags
&= ~IORESOURCE_BUSY
;
2183 pci_add_resource(&hbus
->resources_for_children
,
2184 hbus
->high_mmio_res
);
2190 if (hbus
->low_mmio_res
) {
2191 vmbus_free_mmio(hbus
->low_mmio_res
->start
,
2192 resource_size(hbus
->low_mmio_res
));
2199 * hv_allocate_config_window() - Find MMIO space for PCI Config
2200 * @hbus: Root PCI bus, as understood by this driver
2202 * This function claims memory-mapped I/O space for accessing
2203 * configuration space for the functions on this bus.
2205 * Return: 0 on success, -errno on failure
2207 static int hv_allocate_config_window(struct hv_pcibus_device
*hbus
)
2212 * Set up a region of MMIO space to use for accessing configuration
2215 ret
= vmbus_allocate_mmio(&hbus
->mem_config
, hbus
->hdev
, 0, -1,
2216 PCI_CONFIG_MMIO_LENGTH
, 0x1000, false);
2221 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2222 * resource claims (those which cannot be overlapped) and the ranges
2223 * which are valid for the children of this bus, which are intended
2224 * to be overlapped by those children. Set the flag on this claim
2225 * meaning that this region can't be overlapped.
2228 hbus
->mem_config
->flags
|= IORESOURCE_BUSY
;
2233 static void hv_free_config_window(struct hv_pcibus_device
*hbus
)
2235 vmbus_free_mmio(hbus
->mem_config
->start
, PCI_CONFIG_MMIO_LENGTH
);
2239 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2240 * @hdev: VMBus's tracking struct for this root PCI bus
2242 * Return: 0 on success, -errno on failure
2244 static int hv_pci_enter_d0(struct hv_device
*hdev
)
2246 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2247 struct pci_bus_d0_entry
*d0_entry
;
2248 struct hv_pci_compl comp_pkt
;
2249 struct pci_packet
*pkt
;
2253 * Tell the host that the bus is ready to use, and moved into the
2254 * powered-on state. This includes telling the host which region
2255 * of memory-mapped I/O space has been chosen for configuration space
2258 pkt
= kzalloc(sizeof(*pkt
) + sizeof(*d0_entry
), GFP_KERNEL
);
2262 init_completion(&comp_pkt
.host_event
);
2263 pkt
->completion_func
= hv_pci_generic_compl
;
2264 pkt
->compl_ctxt
= &comp_pkt
;
2265 d0_entry
= (struct pci_bus_d0_entry
*)&pkt
->message
;
2266 d0_entry
->message_type
.type
= PCI_BUS_D0ENTRY
;
2267 d0_entry
->mmio_base
= hbus
->mem_config
->start
;
2269 ret
= vmbus_sendpacket(hdev
->channel
, d0_entry
, sizeof(*d0_entry
),
2270 (unsigned long)pkt
, VM_PKT_DATA_INBAND
,
2271 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2273 ret
= wait_for_response(hdev
, &comp_pkt
.host_event
);
2278 if (comp_pkt
.completion_status
< 0) {
2279 dev_err(&hdev
->device
,
2280 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2281 comp_pkt
.completion_status
);
2294 * hv_pci_query_relations() - Ask host to send list of child
2296 * @hdev: VMBus's tracking struct for this root PCI bus
2298 * Return: 0 on success, -errno on failure
2300 static int hv_pci_query_relations(struct hv_device
*hdev
)
2302 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2303 struct pci_message message
;
2304 struct completion comp
;
2307 /* Ask the host to send along the list of child devices */
2308 init_completion(&comp
);
2309 if (cmpxchg(&hbus
->survey_event
, NULL
, &comp
))
2312 memset(&message
, 0, sizeof(message
));
2313 message
.type
= PCI_QUERY_BUS_RELATIONS
;
2315 ret
= vmbus_sendpacket(hdev
->channel
, &message
, sizeof(message
),
2316 0, VM_PKT_DATA_INBAND
, 0);
2318 ret
= wait_for_response(hdev
, &comp
);
2324 * hv_send_resources_allocated() - Report local resource choices
2325 * @hdev: VMBus's tracking struct for this root PCI bus
2327 * The host OS is expecting to be sent a request as a message
2328 * which contains all the resources that the device will use.
2329 * The response contains those same resources, "translated"
2330 * which is to say, the values which should be used by the
2331 * hardware, when it delivers an interrupt. (MMIO resources are
2332 * used in local terms.) This is nice for Windows, and lines up
2333 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2334 * is deeply expecting to scan an emulated PCI configuration
2335 * space. So this message is sent here only to drive the state
2336 * machine on the host forward.
2338 * Return: 0 on success, -errno on failure
2340 static int hv_send_resources_allocated(struct hv_device
*hdev
)
2342 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2343 struct pci_resources_assigned
*res_assigned
;
2344 struct pci_resources_assigned2
*res_assigned2
;
2345 struct hv_pci_compl comp_pkt
;
2346 struct hv_pci_dev
*hpdev
;
2347 struct pci_packet
*pkt
;
2352 size_res
= (pci_protocol_version
< PCI_PROTOCOL_VERSION_1_2
)
2353 ? sizeof(*res_assigned
) : sizeof(*res_assigned2
);
2355 pkt
= kmalloc(sizeof(*pkt
) + size_res
, GFP_KERNEL
);
2361 for (wslot
= 0; wslot
< 256; wslot
++) {
2362 hpdev
= get_pcichild_wslot(hbus
, wslot
);
2366 memset(pkt
, 0, sizeof(*pkt
) + size_res
);
2367 init_completion(&comp_pkt
.host_event
);
2368 pkt
->completion_func
= hv_pci_generic_compl
;
2369 pkt
->compl_ctxt
= &comp_pkt
;
2371 if (pci_protocol_version
< PCI_PROTOCOL_VERSION_1_2
) {
2373 (struct pci_resources_assigned
*)&pkt
->message
;
2374 res_assigned
->message_type
.type
=
2375 PCI_RESOURCES_ASSIGNED
;
2376 res_assigned
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2379 (struct pci_resources_assigned2
*)&pkt
->message
;
2380 res_assigned2
->message_type
.type
=
2381 PCI_RESOURCES_ASSIGNED2
;
2382 res_assigned2
->wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2384 put_pcichild(hpdev
);
2386 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
->message
,
2387 size_res
, (unsigned long)pkt
,
2389 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2391 ret
= wait_for_response(hdev
, &comp_pkt
.host_event
);
2395 if (comp_pkt
.completion_status
< 0) {
2397 dev_err(&hdev
->device
,
2398 "resource allocated returned 0x%x",
2399 comp_pkt
.completion_status
);
2409 * hv_send_resources_released() - Report local resources
2411 * @hdev: VMBus's tracking struct for this root PCI bus
2413 * Return: 0 on success, -errno on failure
2415 static int hv_send_resources_released(struct hv_device
*hdev
)
2417 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2418 struct pci_child_message pkt
;
2419 struct hv_pci_dev
*hpdev
;
2423 for (wslot
= 0; wslot
< 256; wslot
++) {
2424 hpdev
= get_pcichild_wslot(hbus
, wslot
);
2428 memset(&pkt
, 0, sizeof(pkt
));
2429 pkt
.message_type
.type
= PCI_RESOURCES_RELEASED
;
2430 pkt
.wslot
.slot
= hpdev
->desc
.win_slot
.slot
;
2432 put_pcichild(hpdev
);
2434 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
, sizeof(pkt
), 0,
2435 VM_PKT_DATA_INBAND
, 0);
2443 static void get_hvpcibus(struct hv_pcibus_device
*hbus
)
2445 refcount_inc(&hbus
->remove_lock
);
2448 static void put_hvpcibus(struct hv_pcibus_device
*hbus
)
2450 if (refcount_dec_and_test(&hbus
->remove_lock
))
2451 complete(&hbus
->remove_event
);
2455 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2456 * @hdev: VMBus's tracking struct for this root PCI bus
2457 * @dev_id: Identifies the device itself
2459 * Return: 0 on success, -errno on failure
2461 static int hv_pci_probe(struct hv_device
*hdev
,
2462 const struct hv_vmbus_device_id
*dev_id
)
2464 struct hv_pcibus_device
*hbus
;
2468 * hv_pcibus_device contains the hypercall arguments for retargeting in
2469 * hv_irq_unmask(). Those must not cross a page boundary.
2471 BUILD_BUG_ON(sizeof(*hbus
) > PAGE_SIZE
);
2473 hbus
= (struct hv_pcibus_device
*)get_zeroed_page(GFP_KERNEL
);
2476 hbus
->state
= hv_pcibus_init
;
2479 * The PCI bus "domain" is what is called "segment" in ACPI and
2480 * other specs. Pull it from the instance ID, to get something
2481 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2482 * do the same thing for consistency. Note that, since this code
2483 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2484 * that (1) the only domain in use for something that looks like
2485 * a physical PCI bus (which is actually emulated by the
2486 * hypervisor) is domain 0 and (2) there will be no overlap
2487 * between domains derived from these instance IDs in the same
2490 hbus
->sysdata
.domain
= hdev
->dev_instance
.b
[9] |
2491 hdev
->dev_instance
.b
[8] << 8;
2494 refcount_set(&hbus
->remove_lock
, 1);
2495 INIT_LIST_HEAD(&hbus
->children
);
2496 INIT_LIST_HEAD(&hbus
->dr_list
);
2497 INIT_LIST_HEAD(&hbus
->resources_for_children
);
2498 spin_lock_init(&hbus
->config_lock
);
2499 spin_lock_init(&hbus
->device_list_lock
);
2500 spin_lock_init(&hbus
->retarget_msi_interrupt_lock
);
2501 init_completion(&hbus
->remove_event
);
2502 hbus
->wq
= alloc_ordered_workqueue("hv_pci_%x", 0,
2503 hbus
->sysdata
.domain
);
2509 ret
= vmbus_open(hdev
->channel
, pci_ring_size
, pci_ring_size
, NULL
, 0,
2510 hv_pci_onchannelcallback
, hbus
);
2514 hv_set_drvdata(hdev
, hbus
);
2516 ret
= hv_pci_protocol_negotiation(hdev
);
2520 ret
= hv_allocate_config_window(hbus
);
2524 hbus
->cfg_addr
= ioremap(hbus
->mem_config
->start
,
2525 PCI_CONFIG_MMIO_LENGTH
);
2526 if (!hbus
->cfg_addr
) {
2527 dev_err(&hdev
->device
,
2528 "Unable to map a virtual address for config space\n");
2533 hbus
->sysdata
.fwnode
= irq_domain_alloc_fwnode(hbus
);
2534 if (!hbus
->sysdata
.fwnode
) {
2539 ret
= hv_pcie_init_irq_domain(hbus
);
2543 ret
= hv_pci_query_relations(hdev
);
2545 goto free_irq_domain
;
2547 ret
= hv_pci_enter_d0(hdev
);
2549 goto free_irq_domain
;
2551 ret
= hv_pci_allocate_bridge_windows(hbus
);
2553 goto free_irq_domain
;
2555 ret
= hv_send_resources_allocated(hdev
);
2559 prepopulate_bars(hbus
);
2561 hbus
->state
= hv_pcibus_probed
;
2563 ret
= create_root_hv_pci_bus(hbus
);
2570 hv_pci_free_bridge_windows(hbus
);
2572 irq_domain_remove(hbus
->irq_domain
);
2574 irq_domain_free_fwnode(hbus
->sysdata
.fwnode
);
2576 iounmap(hbus
->cfg_addr
);
2578 hv_free_config_window(hbus
);
2580 vmbus_close(hdev
->channel
);
2582 destroy_workqueue(hbus
->wq
);
2584 free_page((unsigned long)hbus
);
2588 static void hv_pci_bus_exit(struct hv_device
*hdev
)
2590 struct hv_pcibus_device
*hbus
= hv_get_drvdata(hdev
);
2592 struct pci_packet teardown_packet
;
2593 u8 buffer
[sizeof(struct pci_message
)];
2595 struct pci_bus_relations relations
;
2596 struct hv_pci_compl comp_pkt
;
2600 * After the host sends the RESCIND_CHANNEL message, it doesn't
2601 * access the per-channel ringbuffer any longer.
2603 if (hdev
->channel
->rescind
)
2606 /* Delete any children which might still exist. */
2607 memset(&relations
, 0, sizeof(relations
));
2608 hv_pci_devices_present(hbus
, &relations
);
2610 ret
= hv_send_resources_released(hdev
);
2612 dev_err(&hdev
->device
,
2613 "Couldn't send resources released packet(s)\n");
2615 memset(&pkt
.teardown_packet
, 0, sizeof(pkt
.teardown_packet
));
2616 init_completion(&comp_pkt
.host_event
);
2617 pkt
.teardown_packet
.completion_func
= hv_pci_generic_compl
;
2618 pkt
.teardown_packet
.compl_ctxt
= &comp_pkt
;
2619 pkt
.teardown_packet
.message
[0].type
= PCI_BUS_D0EXIT
;
2621 ret
= vmbus_sendpacket(hdev
->channel
, &pkt
.teardown_packet
.message
,
2622 sizeof(struct pci_message
),
2623 (unsigned long)&pkt
.teardown_packet
,
2625 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
2627 wait_for_completion_timeout(&comp_pkt
.host_event
, 10 * HZ
);
2631 * hv_pci_remove() - Remove routine for this VMBus channel
2632 * @hdev: VMBus's tracking struct for this root PCI bus
2634 * Return: 0 on success, -errno on failure
2636 static int hv_pci_remove(struct hv_device
*hdev
)
2638 struct hv_pcibus_device
*hbus
;
2640 hbus
= hv_get_drvdata(hdev
);
2641 if (hbus
->state
== hv_pcibus_installed
) {
2642 /* Remove the bus from PCI's point of view. */
2643 pci_lock_rescan_remove();
2644 pci_stop_root_bus(hbus
->pci_bus
);
2645 pci_remove_root_bus(hbus
->pci_bus
);
2646 pci_unlock_rescan_remove();
2647 hbus
->state
= hv_pcibus_removed
;
2650 hv_pci_bus_exit(hdev
);
2652 vmbus_close(hdev
->channel
);
2654 iounmap(hbus
->cfg_addr
);
2655 hv_free_config_window(hbus
);
2656 pci_free_resource_list(&hbus
->resources_for_children
);
2657 hv_pci_free_bridge_windows(hbus
);
2658 irq_domain_remove(hbus
->irq_domain
);
2659 irq_domain_free_fwnode(hbus
->sysdata
.fwnode
);
2661 wait_for_completion(&hbus
->remove_event
);
2662 destroy_workqueue(hbus
->wq
);
2663 free_page((unsigned long)hbus
);
2667 static const struct hv_vmbus_device_id hv_pci_id_table
[] = {
2668 /* PCI Pass-through Class ID */
2669 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2674 MODULE_DEVICE_TABLE(vmbus
, hv_pci_id_table
);
2676 static struct hv_driver hv_pci_drv
= {
2678 .id_table
= hv_pci_id_table
,
2679 .probe
= hv_pci_probe
,
2680 .remove
= hv_pci_remove
,
2683 static void __exit
exit_hv_pci_drv(void)
2685 vmbus_driver_unregister(&hv_pci_drv
);
2688 static int __init
init_hv_pci_drv(void)
2690 return vmbus_driver_register(&hv_pci_drv
);
2693 module_init(init_hv_pci_drv
);
2694 module_exit(exit_hv_pci_drv
);
2696 MODULE_DESCRIPTION("Hyper-V PCI");
2697 MODULE_LICENSE("GPL v2");