Lines Matching +full:ecam +full:- +full:based

1 // SPDX-License-Identifier: GPL-2.0
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
13 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
18 * to the VM using this front-end will appear at "device 0", the domain will
24 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
28 * vector. This driver does not support level-triggered (line-based)
32 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
34 * by Hyper-V is mapped into a single page of memory space, and the
37 * the PCI back-end driver in Hyper-V.
43 #include <linux/pci-ecam.h>
70 #define CPU_AFFINITY_ALL -1ULL
73 * Supported protocol versions in the order of probing - highest go
85 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
95 * Size of requestor for VMbus; the value is based on the observation
152 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
155 * the Hyper-V parent partition.
208 * occupied by this Message-Signaled
211 * 32. For "MSI-X," as first defined in PCI
212 * 3.0, this must be 1, as each MSI-X table
226 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
232 * occupied by this Message-Signaled
235 * 32. For "MSI-X," as first defined in PCI
236 * 3.0, this must be 1, as each MSI-X table
250 * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
270 * a message-signaled interrupt, either MSI
271 * or MSI-X.
597 return cfg->vector; in hv_msi_get_int_vector()
603 * hv_arch_irq_unmask() - "Unmask" the IRQ by setting its current
629 pbus = pdev->bus; in hv_arch_irq_unmask()
630 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_arch_irq_unmask()
631 int_desc = data->chip_data; in hv_arch_irq_unmask()
633 dev_warn(&hbus->hdev->device, "%s() can not unmask irq %u\n", in hv_arch_irq_unmask()
634 __func__, data->irq); in hv_arch_irq_unmask()
642 params->partition_id = HV_PARTITION_ID_SELF; in hv_arch_irq_unmask()
643 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI; in hv_arch_irq_unmask()
644 params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff; in hv_arch_irq_unmask()
645 params->int_entry.msi_entry.data.as_uint32 = int_desc->data; in hv_arch_irq_unmask()
646 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) | in hv_arch_irq_unmask()
647 (hbus->hdev->dev_instance.b[4] << 16) | in hv_arch_irq_unmask()
648 (hbus->hdev->dev_instance.b[7] << 8) | in hv_arch_irq_unmask()
649 (hbus->hdev->dev_instance.b[6] & 0xf8) | in hv_arch_irq_unmask()
650 PCI_FUNC(pdev->devfn); in hv_arch_irq_unmask()
651 params->int_target.vector = hv_msi_get_int_vector(data); in hv_arch_irq_unmask()
653 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) { in hv_arch_irq_unmask()
661 params->int_target.flags |= in hv_arch_irq_unmask()
670 nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp); in hv_arch_irq_unmask()
679 * var-sized hypercall, var-size starts after vp_mask (thus in hv_arch_irq_unmask()
686 params->int_target.vp_mask |= in hv_arch_irq_unmask()
702 * refer to cpu_disable_common() -> fixup_irqs() -> in hv_arch_irq_unmask()
703 * irq_migrate_all_off_this_cpu() -> migrate_one_irq(). in hv_arch_irq_unmask()
708 * into the irqdata data structure in migrate_one_irq() -> in hv_arch_irq_unmask()
713 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing) in hv_arch_irq_unmask()
714 dev_err(&hbus->hdev->device, in hv_arch_irq_unmask()
721 * starting with a power of two to satisfy power of 2 multi-MSI requirement.
724 #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
735 /* Hyper-V vPCI MSI GIC IRQ domain */
738 /* Hyper-V PCI MSI IRQ chip */
749 return irqd->parent_data->hwirq; in hv_msi_get_int_vector()
763 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_irq_free()
765 int first = d->hwirq - HV_PCI_MSI_SPI_START; in hv_pci_vec_irq_free()
768 mutex_lock(&chip_data->map_lock); in hv_pci_vec_irq_free()
769 bitmap_release_region(chip_data->spi_map, in hv_pci_vec_irq_free()
772 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_irq_free()
793 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_alloc_device_irq()
797 mutex_lock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
798 index = bitmap_find_free_region(chip_data->spi_map, in hv_pci_vec_alloc_device_irq()
801 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
803 return -ENOSPC; in hv_pci_vec_alloc_device_irq()
818 fwspec.fwnode = domain->parent->fwnode; in hv_pci_vec_irq_gic_domain_alloc()
832 d = irq_domain_get_irq_data(domain->parent, virq); in hv_pci_vec_irq_gic_domain_alloc()
834 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING); in hv_pci_vec_irq_gic_domain_alloc()
860 domain->host_data); in hv_pci_vec_irq_domain_alloc()
893 int ret = -ENOMEM; in hv_pci_irqchip_init()
899 mutex_init(&chip_data->map_lock); in hv_pci_irqchip_init()
914 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n"); in hv_pci_irqchip_init()
935 * registers which Hyper-V already supports, so no hypercall needed.
941 * hv_pci_generic_compl() - Invoked for a completion packet
955 comp_pkt->completion_status = resp->status; in hv_pci_generic_compl()
956 complete(&comp_pkt->host_event); in hv_pci_generic_compl()
964 refcount_inc(&hpdev->refs); in get_pcichild()
969 if (refcount_dec_and_test(&hpdev->refs)) in put_pcichild()
981 if (hdev->channel->rescind) { in wait_for_response()
982 dev_warn_once(&hdev->device, "The device is gone.\n"); in wait_for_response()
983 return -ENODEV; in wait_for_response()
994 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
1013 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
1036 * to use the per-cpu input argument page. Use it for in hv_pci_read_mmio()
1041 in->gpa = gpa; in hv_pci_read_mmio()
1042 in->size = size; in hv_pci_read_mmio()
1048 *val = *(u8 *)(out->data); in hv_pci_read_mmio()
1051 *val = *(u16 *)(out->data); in hv_pci_read_mmio()
1054 *val = *(u32 *)(out->data); in hv_pci_read_mmio()
1069 * to use the per-cpu input argument memory. in hv_pci_write_mmio()
1072 in->gpa = gpa; in hv_pci_write_mmio()
1073 in->size = size; in hv_pci_write_mmio()
1076 *(u8 *)(in->data) = val; in hv_pci_write_mmio()
1079 *(u16 *)(in->data) = val; in hv_pci_write_mmio()
1082 *(u32 *)(in->data) = val; in hv_pci_write_mmio()
1094 * of pages in memory-mapped I/O space. Writing to the first page chooses
1101 * _hv_pcifront_read_config() - Internal PCI config read
1110 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_read_config()
1111 struct device *dev = &hbus->hdev->device; in _hv_pcifront_read_config()
1119 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size); in _hv_pcifront_read_config()
1122 memcpy(val, ((u8 *)&hpdev->desc.rev) + where - in _hv_pcifront_read_config()
1126 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where - in _hv_pcifront_read_config()
1135 * Interrupt Line and Interrupt PIN are hard-wired to zero in _hv_pcifront_read_config()
1136 * because this front-end only supports message-signaled in _hv_pcifront_read_config()
1142 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1143 if (hbus->use_calls) { in _hv_pcifront_read_config()
1144 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_read_config()
1146 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_read_config()
1147 hpdev->desc.win_slot.slot); in _hv_pcifront_read_config()
1150 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_read_config()
1153 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_read_config()
1174 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1182 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pcifront_get_vendor_id()
1183 struct device *dev = &hbus->hdev->device; in hv_pcifront_get_vendor_id()
1188 spin_lock_irqsave(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1190 if (hbus->use_calls) { in hv_pcifront_get_vendor_id()
1191 phys_addr_t addr = hbus->mem_config->start + in hv_pcifront_get_vendor_id()
1194 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in hv_pcifront_get_vendor_id()
1195 hpdev->desc.win_slot.slot); in hv_pcifront_get_vendor_id()
1199 void __iomem *addr = hbus->cfg_addr + CFG_PAGE_OFFSET + in hv_pcifront_get_vendor_id()
1202 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in hv_pcifront_get_vendor_id()
1213 spin_unlock_irqrestore(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1219 * _hv_pcifront_write_config() - Internal PCI config write
1228 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_write_config()
1229 struct device *dev = &hbus->hdev->device; in _hv_pcifront_write_config()
1235 /* SSIDs and ROM BARs are read-only */ in _hv_pcifront_write_config()
1237 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1239 if (hbus->use_calls) { in _hv_pcifront_write_config()
1240 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_write_config()
1242 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_write_config()
1243 hpdev->desc.win_slot.slot); in _hv_pcifront_write_config()
1246 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_write_config()
1249 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_write_config()
1270 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1277 * hv_pcifront_read_config() - Read configuration space
1291 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_read_config()
1305 * hv_pcifront_write_config() - Write configuration space
1319 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_write_config()
1341 * Hyper-V SR-IOV provides a backchannel mechanism in software for
1344 * but instead of doing reads and writes in 32-bit chunks through a very slow
1347 * Nearly every SR-IOV device contains just such a communications channel in
1371 * hv_pci_read_config_compl() - Invoked when a response packet
1387 comp->comp_pkt.completion_status = -1; in hv_pci_read_config_compl()
1391 data_len = resp_packet_size - hdr_len; in hv_pci_read_config_compl()
1392 if (data_len > 0 && read_resp->status == 0) { in hv_pci_read_config_compl()
1393 comp->bytes_returned = min(comp->len, data_len); in hv_pci_read_config_compl()
1394 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned); in hv_pci_read_config_compl()
1396 comp->bytes_returned = 0; in hv_pci_read_config_compl()
1399 comp->comp_pkt.completion_status = read_resp->status; in hv_pci_read_config_compl()
1401 complete(&comp->comp_pkt.host_event); in hv_pci_read_config_compl()
1405 * hv_read_config_block() - Sends a read config block request to
1406 * the back-end driver running in the Hyper-V parent partition.
1411 * @bytes_returned: Size which came back from the back-end driver.
1413 * Return: 0 on success, -errno on failure
1420 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_read_config_block()
1431 return -EINVAL; in hv_read_config_block()
1441 read_blk->message_type.type = PCI_READ_BLOCK; in hv_read_config_block()
1442 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_read_config_block()
1443 read_blk->block_id = block_id; in hv_read_config_block()
1444 read_blk->bytes_requested = len; in hv_read_config_block()
1446 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk, in hv_read_config_block()
1453 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event); in hv_read_config_block()
1459 dev_err(&hbus->hdev->device, in hv_read_config_block()
1463 return -EIO; in hv_read_config_block()
1471 * hv_pci_write_config_compl() - Invoked when a response packet for a write
1482 comp_pkt->completion_status = resp->status; in hv_pci_write_config_compl()
1483 complete(&comp_pkt->host_event); in hv_pci_write_config_compl()
1487 * hv_write_config_block() - Sends a write config block request to the
1488 * back-end driver running in the Hyper-V parent partition.
1494 * Return: 0 on success, -errno on failure
1500 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_write_config_block()
1513 return -EINVAL; in hv_write_config_block()
1521 write_blk->message_type.type = PCI_WRITE_BLOCK; in hv_write_config_block()
1522 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_write_config_block()
1523 write_blk->block_id = block_id; in hv_write_config_block()
1524 write_blk->byte_count = len; in hv_write_config_block()
1525 memcpy(write_blk->bytes, buf, len); in hv_write_config_block()
1532 * specified in write_blk->byte_count. in hv_write_config_block()
1536 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size, in hv_write_config_block()
1542 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event); in hv_write_config_block()
1547 dev_err(&hbus->hdev->device, in hv_write_config_block()
1550 return -EIO; in hv_write_config_block()
1557 * hv_register_block_invalidate() - Invoked when a config block invalidation
1558 * arrives from the back-end driver.
1563 * Return: 0 on success, -errno on failure
1570 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_register_block_invalidate()
1574 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_register_block_invalidate()
1576 return -ENODEV; in hv_register_block_invalidate()
1578 hpdev->block_invalidate = block_invalidate; in hv_register_block_invalidate()
1579 hpdev->invalidate_context = context; in hv_register_block_invalidate()
1596 if (!int_desc->vector_count) { in hv_int_desc_free()
1602 int_pkt->message_type.type = in hv_int_desc_free()
1604 int_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_int_desc_free()
1605 int_pkt->int_desc = *int_desc; in hv_int_desc_free()
1606 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), in hv_int_desc_free()
1612 * hv_msi_free() - Free the MSI.
1614 * @info: Extra MSI-related context
1617 * The Hyper-V parent partition and hypervisor are tracking the
1633 hbus = info->data; in hv_msi_free()
1638 irq_data->chip_data = NULL; in hv_msi_free()
1639 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_msi_free()
1652 if (data->parent_data->chip->irq_mask) in hv_irq_mask()
1660 if (data->parent_data->chip->irq_unmask) in hv_irq_unmask()
1678 comp_pkt->comp_pkt.completion_status = -1; in hv_pci_compose_compl()
1681 comp_pkt->comp_pkt.completion_status = resp->status; in hv_pci_compose_compl()
1682 comp_pkt->int_desc = int_resp->int_desc; in hv_pci_compose_compl()
1684 complete(&comp_pkt->comp_pkt.host_event); in hv_pci_compose_compl()
1691 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE; in hv_compose_msi_req_v1()
1692 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v1()
1693 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v1()
1694 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v1()
1695 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v1()
1701 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL; in hv_compose_msi_req_v1()
1709 * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
1711 * not irrelevant because Hyper-V chooses the physical CPU to handle the
1712 * interrupts based on the vCPU specified in message sent to the vPCI VSP in
1713 * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
1715 * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
1718 * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
1720 * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
1721 * new pCPU for the interrupt. But for the multi-MSI case, the second call to
1723 * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
1724 * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
1728 * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
1729 * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
1731 * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
1745 * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
1751 /* -1 means starting with CPU 0 */ in hv_compose_multi_msi_req_get_cpu()
1752 static int cpu_next = -1; in hv_compose_multi_msi_req_get_cpu()
1772 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2; in hv_compose_msi_req_v2()
1773 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v2()
1774 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v2()
1775 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v2()
1776 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v2()
1777 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v2()
1779 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v2()
1788 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3; in hv_compose_msi_req_v3()
1789 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v3()
1790 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v3()
1791 int_pkt->int_desc.reserved = 0; in hv_compose_msi_req_v3()
1792 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v3()
1793 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v3()
1794 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v3()
1796 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v3()
1802 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1844 multi_msi = !msi_desc->pci.msi_attrib.is_msix && in hv_compose_msi_msg()
1845 msi_desc->nvec_used > 1; in hv_compose_msi_msg()
1848 if (data->chip_data && multi_msi) { in hv_compose_msi_msg()
1849 int_desc = data->chip_data; in hv_compose_msi_msg()
1850 msg->address_hi = int_desc->address >> 32; in hv_compose_msi_msg()
1851 msg->address_lo = int_desc->address & 0xffffffff; in hv_compose_msi_msg()
1852 msg->data = int_desc->data; in hv_compose_msi_msg()
1858 pbus = pdev->bus; in hv_compose_msi_msg()
1859 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_compose_msi_msg()
1860 channel = hbus->hdev->channel; in hv_compose_msi_msg()
1861 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_compose_msi_msg()
1866 if (data->chip_data && !multi_msi) { in hv_compose_msi_msg()
1867 int_desc = data->chip_data; in hv_compose_msi_msg()
1868 data->chip_data = NULL; in hv_compose_msi_msg()
1881 if (msi_desc->irq != data->irq) { in hv_compose_msi_msg()
1882 data->chip_data = int_desc; in hv_compose_msi_msg()
1883 int_desc->address = msi_desc->msg.address_lo | in hv_compose_msi_msg()
1884 (u64)msi_desc->msg.address_hi << 32; in hv_compose_msi_msg()
1885 int_desc->data = msi_desc->msg.data + in hv_compose_msi_msg()
1886 (data->irq - msi_desc->irq); in hv_compose_msi_msg()
1887 msg->address_hi = msi_desc->msg.address_hi; in hv_compose_msi_msg()
1888 msg->address_lo = msi_desc->msg.address_lo; in hv_compose_msi_msg()
1889 msg->data = int_desc->data; in hv_compose_msi_msg()
1896 * to be aligned with the count, and also not zero. Multi-msi in hv_compose_msi_msg()
1900 vector_count = msi_desc->nvec_used; in hv_compose_msi_msg()
1918 switch (hbus->protocol_version) { in hv_compose_msi_msg()
1921 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1930 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1938 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1948 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
1953 ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts, in hv_compose_msi_msg()
1958 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
1968 tasklet_disable_in_atomic(&channel->callback_event); in hv_compose_msi_msg()
1979 dev_err_once(&hbus->hdev->device, in hv_compose_msi_msg()
1991 spin_lock_irqsave(&channel->sched_lock, flags); in hv_compose_msi_msg()
1992 if (unlikely(channel->onchannel_callback == NULL)) { in hv_compose_msi_msg()
1993 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
1997 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2002 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2005 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
2017 data->chip_data = int_desc; in hv_compose_msi_msg()
2020 msg->address_hi = comp.int_desc.address >> 32; in hv_compose_msi_msg()
2021 msg->address_lo = comp.int_desc.address & 0xffffffff; in hv_compose_msi_msg()
2022 msg->data = comp.int_desc.data; in hv_compose_msi_msg()
2028 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2033 * been 're-used', i.e., already removed and (re-)mapped.) in hv_compose_msi_msg()
2043 msg->address_hi = 0; in hv_compose_msi_msg()
2044 msg->address_lo = 0; in hv_compose_msi_msg()
2045 msg->data = 0; in hv_compose_msi_msg()
2050 .name = "Hyper-V PCIe MSI",
2068 * hv_pcie_init_irq_domain() - Initialize IRQ domain
2073 * devices only support MSI and MSI-X, not line-based interrupts
2074 * or simulations of line-based interrupts through PCIe's
2075 * fabric-layer messages. Because interrupts are remapped, we
2076 * can support multi-message MSI here.
2082 hbus->msi_info.chip = &hv_msi_irq_chip; in hv_pcie_init_irq_domain()
2083 hbus->msi_info.ops = &hv_msi_ops; in hv_pcie_init_irq_domain()
2084 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS | in hv_pcie_init_irq_domain()
2087 hbus->msi_info.handler = FLOW_HANDLER; in hv_pcie_init_irq_domain()
2088 hbus->msi_info.handler_name = FLOW_NAME; in hv_pcie_init_irq_domain()
2089 hbus->msi_info.data = hbus; in hv_pcie_init_irq_domain()
2090 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode, in hv_pcie_init_irq_domain()
2091 &hbus->msi_info, in hv_pcie_init_irq_domain()
2093 if (!hbus->irq_domain) { in hv_pcie_init_irq_domain()
2094 dev_err(&hbus->hdev->device, in hv_pcie_init_irq_domain()
2096 return -ENODEV; in hv_pcie_init_irq_domain()
2099 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain); in hv_pcie_init_irq_domain()
2105 * get_bar_size() - Get the address space consumed by a BAR
2106 * @bar_val: Value that a BAR returned after -1 was written
2125 * survey_child_resources() - Total all MMIO requirements
2138 event = xchg(&hbus->survey_event, NULL); in survey_child_resources()
2143 if (hbus->low_mmio_space || hbus->high_mmio_space) { in survey_child_resources()
2148 spin_lock_irqsave(&hbus->device_list_lock, flags); in survey_child_resources()
2155 list_for_each_entry(hpdev, &hbus->children, list_entry) { in survey_child_resources()
2157 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO) in survey_child_resources()
2158 dev_err(&hbus->hdev->device, in survey_child_resources()
2161 if (hpdev->probed_bar[i] != 0) { in survey_child_resources()
2167 bar_val = hpdev->probed_bar[i]; in survey_child_resources()
2170 ((u64)hpdev->probed_bar[++i] << 32); in survey_child_resources()
2177 hbus->high_mmio_space += bar_size; in survey_child_resources()
2179 hbus->low_mmio_space += bar_size; in survey_child_resources()
2184 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in survey_child_resources()
2189 * prepopulate_bars() - Fill in BARs with defaults
2197 * the memory-mapped I/O space will be aligned on the largest
2214 if (hbus->low_mmio_space) { in prepopulate_bars()
2215 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in prepopulate_bars()
2216 low_base = hbus->low_mmio_res->start; in prepopulate_bars()
2219 if (hbus->high_mmio_space) { in prepopulate_bars()
2221 (63 - __builtin_clzll(hbus->high_mmio_space)); in prepopulate_bars()
2222 high_base = hbus->high_mmio_res->start; in prepopulate_bars()
2225 spin_lock_irqsave(&hbus->device_list_lock, flags); in prepopulate_bars()
2233 * If the memory enable bit is already set, Hyper-V silently ignores in prepopulate_bars()
2238 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2246 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2248 bar_val = hpdev->probed_bar[i]; in prepopulate_bars()
2254 ((u64)hpdev->probed_bar[i + 1] in prepopulate_bars()
2288 * to be pre-set. Actually here we intentionally in prepopulate_bars()
2290 * in the core PCI driver doesn't cause Hyper-V in prepopulate_bars()
2304 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in prepopulate_bars()
2313 * same ordered workqueue. Therefore hbus->children list will not change
2322 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_assign_slots()
2323 if (hpdev->pci_slot) in hv_pci_assign_slots()
2326 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot)); in hv_pci_assign_slots()
2327 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser); in hv_pci_assign_slots()
2328 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr, in hv_pci_assign_slots()
2330 if (IS_ERR(hpdev->pci_slot)) { in hv_pci_assign_slots()
2332 hpdev->pci_slot = NULL; in hv_pci_assign_slots()
2344 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_remove_slots()
2345 if (!hpdev->pci_slot) in hv_pci_remove_slots()
2347 pci_destroy_slot(hpdev->pci_slot); in hv_pci_remove_slots()
2348 hpdev->pci_slot = NULL; in hv_pci_remove_slots()
2358 struct pci_bus *bus = hbus->bridge->bus; in hv_pci_assign_numa_node()
2361 list_for_each_entry(dev, &bus->devices, bus_list) { in hv_pci_assign_numa_node()
2362 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn)); in hv_pci_assign_numa_node()
2366 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY && in hv_pci_assign_numa_node()
2367 hv_dev->desc.virtual_numa_node < num_possible_nodes()) in hv_pci_assign_numa_node()
2374 set_dev_node(&dev->dev, in hv_pci_assign_numa_node()
2376 hv_dev->desc.virtual_numa_node)); in hv_pci_assign_numa_node()
2383 * create_root_hv_pci_bus() - Expose a new root PCI bus
2386 * Return: 0 on success, -errno on failure
2391 struct pci_host_bridge *bridge = hbus->bridge; in create_root_hv_pci_bus()
2393 bridge->dev.parent = &hbus->hdev->device; in create_root_hv_pci_bus()
2394 bridge->sysdata = &hbus->sysdata; in create_root_hv_pci_bus()
2395 bridge->ops = &hv_pcifront_ops; in create_root_hv_pci_bus()
2403 pci_bus_assign_resources(bridge->bus); in create_root_hv_pci_bus()
2405 pci_bus_add_devices(bridge->bus); in create_root_hv_pci_bus()
2407 hbus->state = hv_pcibus_installed; in create_root_hv_pci_bus()
2417 * q_resource_requirements() - Query Resource Requirements
2434 status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status; in q_resource_requirements()
2436 dev_err(&completion->hpdev->hbus->hdev->device, in q_resource_requirements()
2441 completion->hpdev->probed_bar[i] = in q_resource_requirements()
2442 q_res_req->probed_bar[i]; in q_resource_requirements()
2446 complete(&completion->host_event); in q_resource_requirements()
2450 * new_pcichild_device() - Create a new child device
2477 hpdev->hbus = hbus; in new_pcichild_device()
2485 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS; in new_pcichild_device()
2486 res_req->wslot.slot = desc->win_slot.slot; in new_pcichild_device()
2488 ret = vmbus_sendpacket(hbus->hdev->channel, res_req, in new_pcichild_device()
2496 if (wait_for_response(hbus->hdev, &comp_pkt.host_event)) in new_pcichild_device()
2499 hpdev->desc = *desc; in new_pcichild_device()
2500 refcount_set(&hpdev->refs, 1); in new_pcichild_device()
2502 spin_lock_irqsave(&hbus->device_list_lock, flags); in new_pcichild_device()
2504 list_add_tail(&hpdev->list_entry, &hbus->children); in new_pcichild_device()
2505 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in new_pcichild_device()
2514 * get_pcichild_wslot() - Find device from slot
2532 spin_lock_irqsave(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2533 list_for_each_entry(iter, &hbus->children, list_entry) { in get_pcichild_wslot()
2534 if (iter->desc.win_slot.slot == wslot) { in get_pcichild_wslot()
2540 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2546 * pci_devices_present_work() - Handle new list of child devices
2551 * debug the interaction between Hyper-V and Linux. This
2566 * because both are pushed to the ordered workqueue hbus->wq.
2581 hbus = dr_wrk->bus; in pci_devices_present_work()
2587 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2588 while (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2589 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state, in pci_devices_present_work()
2591 list_del(&dr->list_entry); in pci_devices_present_work()
2594 if (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2599 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2604 mutex_lock(&hbus->state_lock); in pci_devices_present_work()
2607 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2608 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2609 hpdev->reported_missing = true; in pci_devices_present_work()
2611 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2614 for (child_no = 0; child_no < dr->device_count; child_no++) { in pci_devices_present_work()
2616 new_desc = &dr->func[child_no]; in pci_devices_present_work()
2618 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2619 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2620 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) && in pci_devices_present_work()
2621 (hpdev->desc.v_id == new_desc->v_id) && in pci_devices_present_work()
2622 (hpdev->desc.d_id == new_desc->d_id) && in pci_devices_present_work()
2623 (hpdev->desc.ser == new_desc->ser)) { in pci_devices_present_work()
2624 hpdev->reported_missing = false; in pci_devices_present_work()
2628 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2633 dev_err(&hbus->hdev->device, in pci_devices_present_work()
2639 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2642 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2643 if (hpdev->reported_missing) { in pci_devices_present_work()
2646 list_move_tail(&hpdev->list_entry, &removed); in pci_devices_present_work()
2651 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2657 list_del(&hpdev->list_entry); in pci_devices_present_work()
2659 if (hpdev->pci_slot) in pci_devices_present_work()
2660 pci_destroy_slot(hpdev->pci_slot); in pci_devices_present_work()
2665 switch (hbus->state) { in pci_devices_present_work()
2672 pci_scan_child_bus(hbus->bridge->bus); in pci_devices_present_work()
2687 mutex_unlock(&hbus->state_lock); in pci_devices_present_work()
2693 * hv_pci_start_relations_work() - Queue work to start device discovery
2697 * Return: 0 on success, -errno on failure
2706 if (hbus->state == hv_pcibus_removing) { in hv_pci_start_relations_work()
2707 dev_info(&hbus->hdev->device, in hv_pci_start_relations_work()
2709 return -ENOENT; in hv_pci_start_relations_work()
2714 return -ENOMEM; in hv_pci_start_relations_work()
2716 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work); in hv_pci_start_relations_work()
2717 dr_wrk->bus = hbus; in hv_pci_start_relations_work()
2719 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2725 pending_dr = !list_empty(&hbus->dr_list); in hv_pci_start_relations_work()
2726 list_add_tail(&dr->list_entry, &hbus->dr_list); in hv_pci_start_relations_work()
2727 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2732 queue_work(hbus->wq, &dr_wrk->wrk); in hv_pci_start_relations_work()
2738 * hv_pci_devices_present() - Handle list of new children
2752 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present()
2757 dr->device_count = relations->device_count; in hv_pci_devices_present()
2758 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present()
2759 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present()
2760 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present()
2761 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present()
2762 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present()
2763 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present()
2764 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present()
2765 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present()
2766 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present()
2767 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present()
2775 * hv_pci_devices_present2() - Handle list of new children
2787 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present2()
2792 dr->device_count = relations->device_count; in hv_pci_devices_present2()
2793 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present2()
2794 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present2()
2795 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present2()
2796 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present2()
2797 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present2()
2798 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present2()
2799 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present2()
2800 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present2()
2801 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present2()
2802 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present2()
2803 dr->func[i].flags = relations->func[i].flags; in hv_pci_devices_present2()
2804 dr->func[i].virtual_numa_node = in hv_pci_devices_present2()
2805 relations->func[i].virtual_numa_node; in hv_pci_devices_present2()
2813 * hv_eject_device_work() - Asynchronously handles ejection
2835 hbus = hpdev->hbus; in hv_eject_device_work()
2837 mutex_lock(&hbus->state_lock); in hv_eject_device_work()
2842 * must be done without constructs like pci_domain_nr(hbus->bridge->bus) in hv_eject_device_work()
2843 * because hbus->bridge->bus may not exist yet. in hv_eject_device_work()
2845 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot); in hv_eject_device_work()
2846 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot); in hv_eject_device_work()
2854 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_eject_device_work()
2855 list_del(&hpdev->list_entry); in hv_eject_device_work()
2856 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_eject_device_work()
2858 if (hpdev->pci_slot) in hv_eject_device_work()
2859 pci_destroy_slot(hpdev->pci_slot); in hv_eject_device_work()
2863 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; in hv_eject_device_work()
2864 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_eject_device_work()
2865 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt, in hv_eject_device_work()
2876 mutex_unlock(&hbus->state_lock); in hv_eject_device_work()
2880 * hv_pci_eject_device() - Handles device ejection
2884 * just schedules work so that we don't re-enter the packet
2889 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pci_eject_device()
2890 struct hv_device *hdev = hbus->hdev; in hv_pci_eject_device()
2892 if (hbus->state == hv_pcibus_removing) { in hv_pci_eject_device()
2893 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n"); in hv_pci_eject_device()
2898 INIT_WORK(&hpdev->wrk, hv_eject_device_work); in hv_pci_eject_device()
2899 queue_work(hbus->wq, &hpdev->wrk); in hv_pci_eject_device()
2903 * hv_pci_onchannelcallback() - Handles incoming packets
2914 struct vmbus_channel *chan = hbus->hdev->channel; in hv_pci_onchannelcallback()
2938 if (ret == -ENOBUFS) { in hv_pci_onchannelcallback()
2960 switch (desc->type) { in hv_pci_onchannelcallback()
2968 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
2976 * Call ->completion_func() within the critical section to make in hv_pci_onchannelcallback()
2982 comp_packet->completion_func(comp_packet->compl_ctxt, in hv_pci_onchannelcallback()
2991 switch (new_message->message_type.type) { in hv_pci_onchannelcallback()
2998 bus_rel->device_count)) { in hv_pci_onchannelcallback()
2999 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3013 bus_rel2->device_count)) { in hv_pci_onchannelcallback()
3014 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3026 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3031 dev_message->wslot.slot); in hv_pci_onchannelcallback()
3042 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3047 inval->wslot.slot); in hv_pci_onchannelcallback()
3049 if (hpdev->block_invalidate) { in hv_pci_onchannelcallback()
3050 hpdev->block_invalidate( in hv_pci_onchannelcallback()
3051 hpdev->invalidate_context, in hv_pci_onchannelcallback()
3052 inval->block_mask); in hv_pci_onchannelcallback()
3059 dev_warn(&hbus->hdev->device, in hv_pci_onchannelcallback()
3061 new_message->message_type.type); in hv_pci_onchannelcallback()
3067 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3069 desc->type, req_id, bytes_recvd); in hv_pci_onchannelcallback()
3078 * hv_pci_protocol_negotiation() - Set up protocol
3081 * the order of probing - highest go first.
3115 return -ENOMEM; in hv_pci_protocol_negotiation()
3118 pkt->completion_func = hv_pci_generic_compl; in hv_pci_protocol_negotiation()
3119 pkt->compl_ctxt = &comp_pkt; in hv_pci_protocol_negotiation()
3120 version_req = (struct pci_version_request *)&pkt->message; in hv_pci_protocol_negotiation()
3121 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION; in hv_pci_protocol_negotiation()
3124 version_req->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3125 ret = vmbus_sendpacket(hdev->channel, version_req, in hv_pci_protocol_negotiation()
3133 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3134 "PCI Pass-through VSP failed to request version: %d", in hv_pci_protocol_negotiation()
3140 hbus->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3141 dev_info(&hdev->device, in hv_pci_protocol_negotiation()
3143 hbus->protocol_version); in hv_pci_protocol_negotiation()
3148 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3149 "PCI Pass-through VSP failed version request: %#x", in hv_pci_protocol_negotiation()
3151 ret = -EPROTO; in hv_pci_protocol_negotiation()
3158 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3159 "PCI pass-through VSP failed to find supported version"); in hv_pci_protocol_negotiation()
3160 ret = -EPROTO; in hv_pci_protocol_negotiation()
3168 * hv_pci_free_bridge_windows() - Release memory regions for the
3179 if (hbus->low_mmio_space && hbus->low_mmio_res) { in hv_pci_free_bridge_windows()
3180 hbus->low_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3181 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_free_bridge_windows()
3182 resource_size(hbus->low_mmio_res)); in hv_pci_free_bridge_windows()
3185 if (hbus->high_mmio_space && hbus->high_mmio_res) { in hv_pci_free_bridge_windows()
3186 hbus->high_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3187 vmbus_free_mmio(hbus->high_mmio_res->start, in hv_pci_free_bridge_windows()
3188 resource_size(hbus->high_mmio_res)); in hv_pci_free_bridge_windows()
3193 * hv_pci_allocate_bridge_windows() - Allocate memory regions
3215 * Return: 0 on success, -errno on failure
3222 if (hbus->low_mmio_space) { in hv_pci_allocate_bridge_windows()
3223 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in hv_pci_allocate_bridge_windows()
3224 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0, in hv_pci_allocate_bridge_windows()
3226 hbus->low_mmio_space, in hv_pci_allocate_bridge_windows()
3229 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3231 hbus->low_mmio_space); in hv_pci_allocate_bridge_windows()
3236 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3237 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3238 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res); in hv_pci_allocate_bridge_windows()
3241 if (hbus->high_mmio_space) { in hv_pci_allocate_bridge_windows()
3242 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space)); in hv_pci_allocate_bridge_windows()
3243 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev, in hv_pci_allocate_bridge_windows()
3244 0x100000000, -1, in hv_pci_allocate_bridge_windows()
3245 hbus->high_mmio_space, align, in hv_pci_allocate_bridge_windows()
3248 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3250 hbus->high_mmio_space); in hv_pci_allocate_bridge_windows()
3255 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3256 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3257 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res); in hv_pci_allocate_bridge_windows()
3263 if (hbus->low_mmio_res) { in hv_pci_allocate_bridge_windows()
3264 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_allocate_bridge_windows()
3265 resource_size(hbus->low_mmio_res)); in hv_pci_allocate_bridge_windows()
3272 * hv_allocate_config_window() - Find MMIO space for PCI Config
3275 * This function claims memory-mapped I/O space for accessing
3278 * Return: 0 on success, -errno on failure
3288 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1, in hv_allocate_config_window()
3301 hbus->mem_config->flags |= IORESOURCE_BUSY; in hv_allocate_config_window()
3308 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH); in hv_free_config_window()
3314 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
3317 * Return: 0 on success, -errno on failure
3331 * powered-on state. This includes telling the host which region in hv_pci_enter_d0()
3332 * of memory-mapped I/O space has been chosen for configuration space in hv_pci_enter_d0()
3337 return -ENOMEM; in hv_pci_enter_d0()
3340 pkt->completion_func = hv_pci_generic_compl; in hv_pci_enter_d0()
3341 pkt->compl_ctxt = &comp_pkt; in hv_pci_enter_d0()
3342 d0_entry = (struct pci_bus_d0_entry *)&pkt->message; in hv_pci_enter_d0()
3343 d0_entry->message_type.type = PCI_BUS_D0ENTRY; in hv_pci_enter_d0()
3344 d0_entry->mmio_base = hbus->mem_config->start; in hv_pci_enter_d0()
3346 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry), in hv_pci_enter_d0()
3365 dev_err(&hdev->device, "Retrying D0 Entry\n"); in hv_pci_enter_d0()
3375 hbus->wslot_res_allocated = 255; in hv_pci_enter_d0()
3383 dev_err(&hdev->device, in hv_pci_enter_d0()
3388 dev_err(&hdev->device, in hv_pci_enter_d0()
3389 "PCI Pass-through VSP failed D0 Entry with status %x\n", in hv_pci_enter_d0()
3391 ret = -EPROTO; in hv_pci_enter_d0()
3403 * hv_pci_query_relations() - Ask host to send list of child
3407 * Return: 0 on success, -errno on failure
3418 if (cmpxchg(&hbus->survey_event, NULL, &comp)) in hv_pci_query_relations()
3419 return -ENOTEMPTY; in hv_pci_query_relations()
3424 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message), in hv_pci_query_relations()
3431 * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we in hv_pci_query_relations()
3433 * channel callback already scheduled a work to hbus->wq, which can be in hv_pci_query_relations()
3434 * running pci_devices_present_work() -> survey_child_resources() -> in hv_pci_query_relations()
3435 * complete(&hbus->survey_event), even after hv_pci_query_relations() in hv_pci_query_relations()
3438 * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from in hv_pci_query_relations()
3440 * -ENODEV, there can't be any more work item scheduled to hbus->wq in hv_pci_query_relations()
3441 * after the flush_workqueue(): see vmbus_onoffer_rescind() -> in hv_pci_query_relations()
3442 * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() -> in hv_pci_query_relations()
3443 * channel->rescind = true. in hv_pci_query_relations()
3445 flush_workqueue(hbus->wq); in hv_pci_query_relations()
3451 * hv_send_resources_allocated() - Report local resource choices
3465 * Return: 0 on success, -errno on failure
3479 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) in hv_send_resources_allocated()
3484 return -ENOMEM; in hv_send_resources_allocated()
3495 pkt->completion_func = hv_pci_generic_compl; in hv_send_resources_allocated()
3496 pkt->compl_ctxt = &comp_pkt; in hv_send_resources_allocated()
3498 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) { in hv_send_resources_allocated()
3500 (struct pci_resources_assigned *)&pkt->message; in hv_send_resources_allocated()
3501 res_assigned->message_type.type = in hv_send_resources_allocated()
3503 res_assigned->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3506 (struct pci_resources_assigned2 *)&pkt->message; in hv_send_resources_allocated()
3507 res_assigned2->message_type.type = in hv_send_resources_allocated()
3509 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3513 ret = vmbus_sendpacket(hdev->channel, &pkt->message, in hv_send_resources_allocated()
3523 ret = -EPROTO; in hv_send_resources_allocated()
3524 dev_err(&hdev->device, in hv_send_resources_allocated()
3530 hbus->wslot_res_allocated = wslot; in hv_send_resources_allocated()
3538 * hv_send_resources_released() - Report local resources
3542 * Return: 0 on success, -errno on failure
3552 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) { in hv_send_resources_released()
3559 pkt.wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_released()
3563 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0, in hv_send_resources_released()
3568 hbus->wslot_res_allocated = wslot - 1; in hv_send_resources_released()
3571 hbus->wslot_res_allocated = -1; in hv_send_resources_released()
3586 * hv_get_dom_num() - Get a valid PCI domain number
3610 * hv_put_dom_num() - Mark the PCI domain number as free
3619 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3623 * Return: 0 on success, -errno on failure
3634 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0); in hv_pci_probe()
3636 return -ENOMEM; in hv_pci_probe()
3640 return -ENOMEM; in hv_pci_probe()
3642 hbus->bridge = bridge; in hv_pci_probe()
3643 mutex_init(&hbus->state_lock); in hv_pci_probe()
3644 hbus->state = hv_pcibus_init; in hv_pci_probe()
3645 hbus->wslot_res_allocated = -1; in hv_pci_probe()
3653 * Note that, since this code only runs in a Hyper-V VM, Hyper-V in hv_pci_probe()
3660 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4]; in hv_pci_probe()
3664 dev_err(&hdev->device, in hv_pci_probe()
3666 ret = -EINVAL; in hv_pci_probe()
3671 dev_info(&hdev->device, in hv_pci_probe()
3675 hbus->bridge->domain_nr = dom; in hv_pci_probe()
3677 hbus->sysdata.domain = dom; in hv_pci_probe()
3678 hbus->use_calls = !!(ms_hyperv.hints & HV_X64_USE_MMIO_HYPERCALLS); in hv_pci_probe()
3687 hbus->sysdata.parent = hdev->device.parent; in hv_pci_probe()
3688 hbus->use_calls = false; in hv_pci_probe()
3691 hbus->hdev = hdev; in hv_pci_probe()
3692 INIT_LIST_HEAD(&hbus->children); in hv_pci_probe()
3693 INIT_LIST_HEAD(&hbus->dr_list); in hv_pci_probe()
3694 spin_lock_init(&hbus->config_lock); in hv_pci_probe()
3695 spin_lock_init(&hbus->device_list_lock); in hv_pci_probe()
3696 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0, in hv_pci_probe()
3697 hbus->bridge->domain_nr); in hv_pci_probe()
3698 if (!hbus->wq) { in hv_pci_probe()
3699 ret = -ENOMEM; in hv_pci_probe()
3703 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_probe()
3704 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_probe()
3705 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_probe()
3707 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_probe()
3723 hbus->cfg_addr = ioremap(hbus->mem_config->start, in hv_pci_probe()
3725 if (!hbus->cfg_addr) { in hv_pci_probe()
3726 dev_err(&hdev->device, in hv_pci_probe()
3728 ret = -ENOMEM; in hv_pci_probe()
3732 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance); in hv_pci_probe()
3734 ret = -ENOMEM; in hv_pci_probe()
3738 hbus->fwnode = irq_domain_alloc_named_fwnode(name); in hv_pci_probe()
3740 if (!hbus->fwnode) { in hv_pci_probe()
3741 ret = -ENOMEM; in hv_pci_probe()
3753 mutex_lock(&hbus->state_lock); in hv_pci_probe()
3769 hbus->state = hv_pcibus_probed; in hv_pci_probe()
3775 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3783 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3785 irq_domain_remove(hbus->irq_domain); in hv_pci_probe()
3787 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_probe()
3789 iounmap(hbus->cfg_addr); in hv_pci_probe()
3793 vmbus_close(hdev->channel); in hv_pci_probe()
3795 destroy_workqueue(hbus->wq); in hv_pci_probe()
3797 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_probe()
3806 struct vmbus_channel *chan = hdev->channel; in hv_pci_bus_exit()
3819 * access the per-channel ringbuffer any longer. in hv_pci_bus_exit()
3821 if (chan->rescind) in hv_pci_bus_exit()
3829 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3830 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) in hv_pci_bus_exit()
3831 list_move_tail(&hpdev->list_entry, &removed); in hv_pci_bus_exit()
3832 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3836 list_del(&hpdev->list_entry); in hv_pci_bus_exit()
3837 if (hpdev->pci_slot) in hv_pci_bus_exit()
3838 pci_destroy_slot(hpdev->pci_slot); in hv_pci_bus_exit()
3847 dev_err(&hdev->device, in hv_pci_bus_exit()
3876 return -ETIMEDOUT; in hv_pci_bus_exit()
3883 * hv_pci_remove() - Remove routine for this VMBus channel
3891 if (hbus->state == hv_pcibus_installed) { in hv_pci_remove()
3892 tasklet_disable(&hdev->channel->callback_event); in hv_pci_remove()
3893 hbus->state = hv_pcibus_removing; in hv_pci_remove()
3894 tasklet_enable(&hdev->channel->callback_event); in hv_pci_remove()
3895 destroy_workqueue(hbus->wq); in hv_pci_remove()
3896 hbus->wq = NULL; in hv_pci_remove()
3899 * on hbus-wq. We can't race with hv_pci_devices_present() in hv_pci_remove()
3905 pci_stop_root_bus(hbus->bridge->bus); in hv_pci_remove()
3907 pci_remove_root_bus(hbus->bridge->bus); in hv_pci_remove()
3913 vmbus_close(hdev->channel); in hv_pci_remove()
3915 iounmap(hbus->cfg_addr); in hv_pci_remove()
3918 irq_domain_remove(hbus->irq_domain); in hv_pci_remove()
3919 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_remove()
3921 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_remove()
3938 * items onto hbus->wq in hv_pci_devices_present() and in hv_pci_suspend()
3941 * the work item handler pci_devices_present_work() -> in hv_pci_suspend()
3945 * callback tasklet, sets hbus->state to hv_pcibus_removing, and in hv_pci_suspend()
3946 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds, in hv_pci_suspend()
3948 * hbus->wq and safely closes the vmbus channel. in hv_pci_suspend()
3950 tasklet_disable(&hdev->channel->callback_event); in hv_pci_suspend()
3953 old_state = hbus->state; in hv_pci_suspend()
3954 if (hbus->state == hv_pcibus_installed) in hv_pci_suspend()
3955 hbus->state = hv_pcibus_removing; in hv_pci_suspend()
3957 tasklet_enable(&hdev->channel->callback_event); in hv_pci_suspend()
3960 return -EINVAL; in hv_pci_suspend()
3962 flush_workqueue(hbus->wq); in hv_pci_suspend()
3968 vmbus_close(hdev->channel); in hv_pci_suspend()
3979 if (!pdev->msi_enabled && !pdev->msix_enabled) in hv_pci_restore_msi_msg()
3982 msi_lock_descs(&pdev->dev); in hv_pci_restore_msi_msg()
3983 msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { in hv_pci_restore_msi_msg()
3984 irq_data = irq_get_irq_data(entry->irq); in hv_pci_restore_msi_msg()
3986 ret = -EINVAL; in hv_pci_restore_msi_msg()
3990 hv_compose_msi_msg(irq_data, &entry->msg); in hv_pci_restore_msi_msg()
3992 msi_unlock_descs(&pdev->dev); in hv_pci_restore_msi_msg()
3998 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
3999 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
4001 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
4006 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL); in hv_pci_restore_msi_state()
4015 hbus->state = hv_pcibus_init; in hv_pci_resume()
4017 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_resume()
4018 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_resume()
4019 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_resume()
4021 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_resume()
4027 version[0] = hbus->protocol_version; in hv_pci_resume()
4036 mutex_lock(&hbus->state_lock); in hv_pci_resume()
4050 hbus->state = hv_pcibus_installed; in hv_pci_resume()
4051 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4055 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4057 vmbus_close(hdev->channel); in hv_pci_resume()
4062 /* PCI Pass-through Class ID */
4063 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
4093 return -ENODEV; in init_hv_pci_drv()
4113 MODULE_DESCRIPTION("Hyper-V PCI");