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()
654 * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by in hv_arch_irq_unmask()
660 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) { in hv_arch_irq_unmask()
668 params->int_target.flags |= in hv_arch_irq_unmask()
677 nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp); in hv_arch_irq_unmask()
686 * var-sized hypercall, var-size starts after vp_mask (thus in hv_arch_irq_unmask()
693 params->int_target.vp_mask |= in hv_arch_irq_unmask()
709 * refer to cpu_disable_common() -> fixup_irqs() -> in hv_arch_irq_unmask()
710 * irq_migrate_all_off_this_cpu() -> migrate_one_irq(). in hv_arch_irq_unmask()
715 * into the irqdata data structure in migrate_one_irq() -> in hv_arch_irq_unmask()
720 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing) in hv_arch_irq_unmask()
721 dev_err(&hbus->hdev->device, in hv_arch_irq_unmask()
728 * starting with a power of two to satisfy power of 2 multi-MSI requirement.
731 #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
742 /* Hyper-V vPCI MSI GIC IRQ domain */
745 /* Hyper-V PCI MSI IRQ chip */
756 return irqd->parent_data->hwirq; in hv_msi_get_int_vector()
770 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_irq_free()
772 int first = d->hwirq - HV_PCI_MSI_SPI_START; in hv_pci_vec_irq_free()
775 mutex_lock(&chip_data->map_lock); in hv_pci_vec_irq_free()
776 bitmap_release_region(chip_data->spi_map, in hv_pci_vec_irq_free()
779 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_irq_free()
800 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_alloc_device_irq()
804 mutex_lock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
805 index = bitmap_find_free_region(chip_data->spi_map, in hv_pci_vec_alloc_device_irq()
808 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
810 return -ENOSPC; in hv_pci_vec_alloc_device_irq()
825 fwspec.fwnode = domain->parent->fwnode; in hv_pci_vec_irq_gic_domain_alloc()
839 d = irq_domain_get_irq_data(domain->parent, virq); in hv_pci_vec_irq_gic_domain_alloc()
841 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING); in hv_pci_vec_irq_gic_domain_alloc()
867 domain->host_data); in hv_pci_vec_irq_domain_alloc()
900 int ret = -ENOMEM; in hv_pci_irqchip_init()
906 mutex_init(&chip_data->map_lock); in hv_pci_irqchip_init()
921 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n"); in hv_pci_irqchip_init()
942 * registers which Hyper-V already supports, so no hypercall needed.
948 * hv_pci_generic_compl() - Invoked for a completion packet
962 comp_pkt->completion_status = resp->status; in hv_pci_generic_compl()
963 complete(&comp_pkt->host_event); in hv_pci_generic_compl()
971 refcount_inc(&hpdev->refs); in get_pcichild()
976 if (refcount_dec_and_test(&hpdev->refs)) in put_pcichild()
988 if (hdev->channel->rescind) { in wait_for_response()
989 dev_warn_once(&hdev->device, "The device is gone.\n"); in wait_for_response()
990 return -ENODEV; in wait_for_response()
1001 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
1020 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
1043 * to use the per-cpu input argument page. Use it for in hv_pci_read_mmio()
1048 in->gpa = gpa; in hv_pci_read_mmio()
1049 in->size = size; in hv_pci_read_mmio()
1055 *val = *(u8 *)(out->data); in hv_pci_read_mmio()
1058 *val = *(u16 *)(out->data); in hv_pci_read_mmio()
1061 *val = *(u32 *)(out->data); in hv_pci_read_mmio()
1076 * to use the per-cpu input argument memory. in hv_pci_write_mmio()
1079 in->gpa = gpa; in hv_pci_write_mmio()
1080 in->size = size; in hv_pci_write_mmio()
1083 *(u8 *)(in->data) = val; in hv_pci_write_mmio()
1086 *(u16 *)(in->data) = val; in hv_pci_write_mmio()
1089 *(u32 *)(in->data) = val; in hv_pci_write_mmio()
1101 * of pages in memory-mapped I/O space. Writing to the first page chooses
1108 * _hv_pcifront_read_config() - Internal PCI config read
1117 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_read_config()
1118 struct device *dev = &hbus->hdev->device; in _hv_pcifront_read_config()
1126 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size); in _hv_pcifront_read_config()
1129 memcpy(val, ((u8 *)&hpdev->desc.rev) + where - in _hv_pcifront_read_config()
1133 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where - in _hv_pcifront_read_config()
1142 * Interrupt Line and Interrupt PIN are hard-wired to zero in _hv_pcifront_read_config()
1143 * because this front-end only supports message-signaled in _hv_pcifront_read_config()
1149 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1150 if (hbus->use_calls) { in _hv_pcifront_read_config()
1151 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_read_config()
1153 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_read_config()
1154 hpdev->desc.win_slot.slot); in _hv_pcifront_read_config()
1157 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_read_config()
1160 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_read_config()
1181 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1189 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pcifront_get_vendor_id()
1190 struct device *dev = &hbus->hdev->device; in hv_pcifront_get_vendor_id()
1195 spin_lock_irqsave(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1197 if (hbus->use_calls) { in hv_pcifront_get_vendor_id()
1198 phys_addr_t addr = hbus->mem_config->start + in hv_pcifront_get_vendor_id()
1201 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in hv_pcifront_get_vendor_id()
1202 hpdev->desc.win_slot.slot); in hv_pcifront_get_vendor_id()
1206 void __iomem *addr = hbus->cfg_addr + CFG_PAGE_OFFSET + in hv_pcifront_get_vendor_id()
1209 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in hv_pcifront_get_vendor_id()
1220 spin_unlock_irqrestore(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1226 * _hv_pcifront_write_config() - Internal PCI config write
1235 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_write_config()
1236 struct device *dev = &hbus->hdev->device; in _hv_pcifront_write_config()
1242 /* SSIDs and ROM BARs are read-only */ in _hv_pcifront_write_config()
1244 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1246 if (hbus->use_calls) { in _hv_pcifront_write_config()
1247 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_write_config()
1249 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_write_config()
1250 hpdev->desc.win_slot.slot); in _hv_pcifront_write_config()
1253 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_write_config()
1256 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_write_config()
1277 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1284 * hv_pcifront_read_config() - Read configuration space
1298 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_read_config()
1312 * hv_pcifront_write_config() - Write configuration space
1326 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_write_config()
1348 * Hyper-V SR-IOV provides a backchannel mechanism in software for
1351 * but instead of doing reads and writes in 32-bit chunks through a very slow
1354 * Nearly every SR-IOV device contains just such a communications channel in
1378 * hv_pci_read_config_compl() - Invoked when a response packet
1394 comp->comp_pkt.completion_status = -1; in hv_pci_read_config_compl()
1398 data_len = resp_packet_size - hdr_len; in hv_pci_read_config_compl()
1399 if (data_len > 0 && read_resp->status == 0) { in hv_pci_read_config_compl()
1400 comp->bytes_returned = min(comp->len, data_len); in hv_pci_read_config_compl()
1401 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned); in hv_pci_read_config_compl()
1403 comp->bytes_returned = 0; in hv_pci_read_config_compl()
1406 comp->comp_pkt.completion_status = read_resp->status; in hv_pci_read_config_compl()
1408 complete(&comp->comp_pkt.host_event); in hv_pci_read_config_compl()
1412 * hv_read_config_block() - Sends a read config block request to
1413 * the back-end driver running in the Hyper-V parent partition.
1418 * @bytes_returned: Size which came back from the back-end driver.
1420 * Return: 0 on success, -errno on failure
1427 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_read_config_block()
1438 return -EINVAL; in hv_read_config_block()
1448 read_blk->message_type.type = PCI_READ_BLOCK; in hv_read_config_block()
1449 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_read_config_block()
1450 read_blk->block_id = block_id; in hv_read_config_block()
1451 read_blk->bytes_requested = len; in hv_read_config_block()
1453 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk, in hv_read_config_block()
1460 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event); in hv_read_config_block()
1466 dev_err(&hbus->hdev->device, in hv_read_config_block()
1470 return -EIO; in hv_read_config_block()
1478 * hv_pci_write_config_compl() - Invoked when a response packet for a write
1489 comp_pkt->completion_status = resp->status; in hv_pci_write_config_compl()
1490 complete(&comp_pkt->host_event); in hv_pci_write_config_compl()
1494 * hv_write_config_block() - Sends a write config block request to the
1495 * back-end driver running in the Hyper-V parent partition.
1501 * Return: 0 on success, -errno on failure
1507 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_write_config_block()
1520 return -EINVAL; in hv_write_config_block()
1528 write_blk->message_type.type = PCI_WRITE_BLOCK; in hv_write_config_block()
1529 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_write_config_block()
1530 write_blk->block_id = block_id; in hv_write_config_block()
1531 write_blk->byte_count = len; in hv_write_config_block()
1532 memcpy(write_blk->bytes, buf, len); in hv_write_config_block()
1539 * specified in write_blk->byte_count. in hv_write_config_block()
1543 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size, in hv_write_config_block()
1549 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event); in hv_write_config_block()
1554 dev_err(&hbus->hdev->device, in hv_write_config_block()
1557 return -EIO; in hv_write_config_block()
1564 * hv_register_block_invalidate() - Invoked when a config block invalidation
1565 * arrives from the back-end driver.
1570 * Return: 0 on success, -errno on failure
1577 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_register_block_invalidate()
1581 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_register_block_invalidate()
1583 return -ENODEV; in hv_register_block_invalidate()
1585 hpdev->block_invalidate = block_invalidate; in hv_register_block_invalidate()
1586 hpdev->invalidate_context = context; in hv_register_block_invalidate()
1603 if (!int_desc->vector_count) { in hv_int_desc_free()
1609 int_pkt->message_type.type = in hv_int_desc_free()
1611 int_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_int_desc_free()
1612 int_pkt->int_desc = *int_desc; in hv_int_desc_free()
1613 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), in hv_int_desc_free()
1619 * hv_msi_free() - Free the MSI.
1621 * @info: Extra MSI-related context
1624 * The Hyper-V parent partition and hypervisor are tracking the
1640 hbus = info->data; in hv_msi_free()
1645 irq_data->chip_data = NULL; in hv_msi_free()
1646 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_msi_free()
1659 if (data->parent_data->chip->irq_mask) in hv_irq_mask()
1667 if (data->parent_data->chip->irq_unmask) in hv_irq_unmask()
1685 comp_pkt->comp_pkt.completion_status = -1; in hv_pci_compose_compl()
1688 comp_pkt->comp_pkt.completion_status = resp->status; in hv_pci_compose_compl()
1689 comp_pkt->int_desc = int_resp->int_desc; in hv_pci_compose_compl()
1691 complete(&comp_pkt->comp_pkt.host_event); in hv_pci_compose_compl()
1698 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE; in hv_compose_msi_req_v1()
1699 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v1()
1700 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v1()
1701 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v1()
1702 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v1()
1708 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL; in hv_compose_msi_req_v1()
1716 * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
1718 * not irrelevant because Hyper-V chooses the physical CPU to handle the
1719 * interrupts based on the vCPU specified in message sent to the vPCI VSP in
1720 * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
1722 * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
1725 * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
1727 * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
1728 * new pCPU for the interrupt. But for the multi-MSI case, the second call to
1730 * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
1731 * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
1735 * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
1736 * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
1738 * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
1752 * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
1758 /* -1 means starting with CPU 0 */ in hv_compose_multi_msi_req_get_cpu()
1759 static int cpu_next = -1; in hv_compose_multi_msi_req_get_cpu()
1779 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2; in hv_compose_msi_req_v2()
1780 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v2()
1781 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v2()
1782 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v2()
1783 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v2()
1784 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v2()
1786 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v2()
1795 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3; in hv_compose_msi_req_v3()
1796 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v3()
1797 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v3()
1798 int_pkt->int_desc.reserved = 0; in hv_compose_msi_req_v3()
1799 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v3()
1800 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v3()
1801 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v3()
1803 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v3()
1809 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1851 multi_msi = !msi_desc->pci.msi_attrib.is_msix && in hv_compose_msi_msg()
1852 msi_desc->nvec_used > 1; in hv_compose_msi_msg()
1855 if (data->chip_data && multi_msi) { in hv_compose_msi_msg()
1856 int_desc = data->chip_data; in hv_compose_msi_msg()
1857 msg->address_hi = int_desc->address >> 32; in hv_compose_msi_msg()
1858 msg->address_lo = int_desc->address & 0xffffffff; in hv_compose_msi_msg()
1859 msg->data = int_desc->data; in hv_compose_msi_msg()
1865 pbus = pdev->bus; in hv_compose_msi_msg()
1866 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_compose_msi_msg()
1867 channel = hbus->hdev->channel; in hv_compose_msi_msg()
1868 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_compose_msi_msg()
1873 if (data->chip_data && !multi_msi) { in hv_compose_msi_msg()
1874 int_desc = data->chip_data; in hv_compose_msi_msg()
1875 data->chip_data = NULL; in hv_compose_msi_msg()
1888 if (msi_desc->irq != data->irq) { in hv_compose_msi_msg()
1889 data->chip_data = int_desc; in hv_compose_msi_msg()
1890 int_desc->address = msi_desc->msg.address_lo | in hv_compose_msi_msg()
1891 (u64)msi_desc->msg.address_hi << 32; in hv_compose_msi_msg()
1892 int_desc->data = msi_desc->msg.data + in hv_compose_msi_msg()
1893 (data->irq - msi_desc->irq); in hv_compose_msi_msg()
1894 msg->address_hi = msi_desc->msg.address_hi; in hv_compose_msi_msg()
1895 msg->address_lo = msi_desc->msg.address_lo; in hv_compose_msi_msg()
1896 msg->data = int_desc->data; in hv_compose_msi_msg()
1903 * to be aligned with the count, and also not zero. Multi-msi in hv_compose_msi_msg()
1907 vector_count = msi_desc->nvec_used; in hv_compose_msi_msg()
1925 switch (hbus->protocol_version) { in hv_compose_msi_msg()
1928 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1937 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1945 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1955 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
1960 ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts, in hv_compose_msi_msg()
1965 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
1975 tasklet_disable_in_atomic(&channel->callback_event); in hv_compose_msi_msg()
1986 dev_err_once(&hbus->hdev->device, in hv_compose_msi_msg()
1998 spin_lock_irqsave(&channel->sched_lock, flags); in hv_compose_msi_msg()
1999 if (unlikely(channel->onchannel_callback == NULL)) { in hv_compose_msi_msg()
2000 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2004 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2009 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2012 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
2024 data->chip_data = int_desc; in hv_compose_msi_msg()
2027 msg->address_hi = comp.int_desc.address >> 32; in hv_compose_msi_msg()
2028 msg->address_lo = comp.int_desc.address & 0xffffffff; in hv_compose_msi_msg()
2029 msg->data = comp.int_desc.data; in hv_compose_msi_msg()
2035 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2040 * been 're-used', i.e., already removed and (re-)mapped.) in hv_compose_msi_msg()
2050 msg->address_hi = 0; in hv_compose_msi_msg()
2051 msg->address_lo = 0; in hv_compose_msi_msg()
2052 msg->data = 0; in hv_compose_msi_msg()
2057 .name = "Hyper-V PCIe MSI",
2075 * hv_pcie_init_irq_domain() - Initialize IRQ domain
2080 * devices only support MSI and MSI-X, not line-based interrupts
2081 * or simulations of line-based interrupts through PCIe's
2082 * fabric-layer messages. Because interrupts are remapped, we
2083 * can support multi-message MSI here.
2089 hbus->msi_info.chip = &hv_msi_irq_chip; in hv_pcie_init_irq_domain()
2090 hbus->msi_info.ops = &hv_msi_ops; in hv_pcie_init_irq_domain()
2091 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS | in hv_pcie_init_irq_domain()
2094 hbus->msi_info.handler = FLOW_HANDLER; in hv_pcie_init_irq_domain()
2095 hbus->msi_info.handler_name = FLOW_NAME; in hv_pcie_init_irq_domain()
2096 hbus->msi_info.data = hbus; in hv_pcie_init_irq_domain()
2097 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode, in hv_pcie_init_irq_domain()
2098 &hbus->msi_info, in hv_pcie_init_irq_domain()
2100 if (!hbus->irq_domain) { in hv_pcie_init_irq_domain()
2101 dev_err(&hbus->hdev->device, in hv_pcie_init_irq_domain()
2103 return -ENODEV; in hv_pcie_init_irq_domain()
2106 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain); in hv_pcie_init_irq_domain()
2112 * get_bar_size() - Get the address space consumed by a BAR
2113 * @bar_val: Value that a BAR returned after -1 was written
2132 * survey_child_resources() - Total all MMIO requirements
2145 event = xchg(&hbus->survey_event, NULL); in survey_child_resources()
2150 if (hbus->low_mmio_space || hbus->high_mmio_space) { in survey_child_resources()
2155 spin_lock_irqsave(&hbus->device_list_lock, flags); in survey_child_resources()
2162 list_for_each_entry(hpdev, &hbus->children, list_entry) { in survey_child_resources()
2164 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO) in survey_child_resources()
2165 dev_err(&hbus->hdev->device, in survey_child_resources()
2168 if (hpdev->probed_bar[i] != 0) { in survey_child_resources()
2174 bar_val = hpdev->probed_bar[i]; in survey_child_resources()
2177 ((u64)hpdev->probed_bar[++i] << 32); in survey_child_resources()
2184 hbus->high_mmio_space += bar_size; in survey_child_resources()
2186 hbus->low_mmio_space += bar_size; in survey_child_resources()
2191 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in survey_child_resources()
2196 * prepopulate_bars() - Fill in BARs with defaults
2204 * the memory-mapped I/O space will be aligned on the largest
2221 if (hbus->low_mmio_space) { in prepopulate_bars()
2222 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in prepopulate_bars()
2223 low_base = hbus->low_mmio_res->start; in prepopulate_bars()
2226 if (hbus->high_mmio_space) { in prepopulate_bars()
2228 (63 - __builtin_clzll(hbus->high_mmio_space)); in prepopulate_bars()
2229 high_base = hbus->high_mmio_res->start; in prepopulate_bars()
2232 spin_lock_irqsave(&hbus->device_list_lock, flags); in prepopulate_bars()
2240 * If the memory enable bit is already set, Hyper-V silently ignores in prepopulate_bars()
2245 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2253 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2255 bar_val = hpdev->probed_bar[i]; in prepopulate_bars()
2261 ((u64)hpdev->probed_bar[i + 1] in prepopulate_bars()
2295 * to be pre-set. Actually here we intentionally in prepopulate_bars()
2297 * in the core PCI driver doesn't cause Hyper-V in prepopulate_bars()
2311 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in prepopulate_bars()
2320 * same ordered workqueue. Therefore hbus->children list will not change
2329 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_assign_slots()
2330 if (hpdev->pci_slot) in hv_pci_assign_slots()
2333 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot)); in hv_pci_assign_slots()
2334 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser); in hv_pci_assign_slots()
2335 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr, in hv_pci_assign_slots()
2337 if (IS_ERR(hpdev->pci_slot)) { in hv_pci_assign_slots()
2339 hpdev->pci_slot = NULL; in hv_pci_assign_slots()
2351 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_remove_slots()
2352 if (!hpdev->pci_slot) in hv_pci_remove_slots()
2354 pci_destroy_slot(hpdev->pci_slot); in hv_pci_remove_slots()
2355 hpdev->pci_slot = NULL; in hv_pci_remove_slots()
2365 struct pci_bus *bus = hbus->bridge->bus; in hv_pci_assign_numa_node()
2368 list_for_each_entry(dev, &bus->devices, bus_list) { in hv_pci_assign_numa_node()
2369 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn)); in hv_pci_assign_numa_node()
2373 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY && in hv_pci_assign_numa_node()
2374 hv_dev->desc.virtual_numa_node < num_possible_nodes()) in hv_pci_assign_numa_node()
2381 set_dev_node(&dev->dev, in hv_pci_assign_numa_node()
2383 hv_dev->desc.virtual_numa_node)); in hv_pci_assign_numa_node()
2390 * create_root_hv_pci_bus() - Expose a new root PCI bus
2393 * Return: 0 on success, -errno on failure
2398 struct pci_host_bridge *bridge = hbus->bridge; in create_root_hv_pci_bus()
2400 bridge->dev.parent = &hbus->hdev->device; in create_root_hv_pci_bus()
2401 bridge->sysdata = &hbus->sysdata; in create_root_hv_pci_bus()
2402 bridge->ops = &hv_pcifront_ops; in create_root_hv_pci_bus()
2410 pci_bus_assign_resources(bridge->bus); in create_root_hv_pci_bus()
2412 pci_bus_add_devices(bridge->bus); in create_root_hv_pci_bus()
2414 hbus->state = hv_pcibus_installed; in create_root_hv_pci_bus()
2424 * q_resource_requirements() - Query Resource Requirements
2441 status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status; in q_resource_requirements()
2443 dev_err(&completion->hpdev->hbus->hdev->device, in q_resource_requirements()
2448 completion->hpdev->probed_bar[i] = in q_resource_requirements()
2449 q_res_req->probed_bar[i]; in q_resource_requirements()
2453 complete(&completion->host_event); in q_resource_requirements()
2457 * new_pcichild_device() - Create a new child device
2484 hpdev->hbus = hbus; in new_pcichild_device()
2492 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS; in new_pcichild_device()
2493 res_req->wslot.slot = desc->win_slot.slot; in new_pcichild_device()
2495 ret = vmbus_sendpacket(hbus->hdev->channel, res_req, in new_pcichild_device()
2503 if (wait_for_response(hbus->hdev, &comp_pkt.host_event)) in new_pcichild_device()
2506 hpdev->desc = *desc; in new_pcichild_device()
2507 refcount_set(&hpdev->refs, 1); in new_pcichild_device()
2509 spin_lock_irqsave(&hbus->device_list_lock, flags); in new_pcichild_device()
2511 list_add_tail(&hpdev->list_entry, &hbus->children); in new_pcichild_device()
2512 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in new_pcichild_device()
2521 * get_pcichild_wslot() - Find device from slot
2539 spin_lock_irqsave(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2540 list_for_each_entry(iter, &hbus->children, list_entry) { in get_pcichild_wslot()
2541 if (iter->desc.win_slot.slot == wslot) { in get_pcichild_wslot()
2547 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2553 * pci_devices_present_work() - Handle new list of child devices
2558 * debug the interaction between Hyper-V and Linux. This
2573 * because both are pushed to the ordered workqueue hbus->wq.
2588 hbus = dr_wrk->bus; in pci_devices_present_work()
2594 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2595 while (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2596 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state, in pci_devices_present_work()
2598 list_del(&dr->list_entry); in pci_devices_present_work()
2601 if (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2606 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2611 mutex_lock(&hbus->state_lock); in pci_devices_present_work()
2614 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2615 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2616 hpdev->reported_missing = true; in pci_devices_present_work()
2618 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2621 for (child_no = 0; child_no < dr->device_count; child_no++) { in pci_devices_present_work()
2623 new_desc = &dr->func[child_no]; in pci_devices_present_work()
2625 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2626 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2627 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) && in pci_devices_present_work()
2628 (hpdev->desc.v_id == new_desc->v_id) && in pci_devices_present_work()
2629 (hpdev->desc.d_id == new_desc->d_id) && in pci_devices_present_work()
2630 (hpdev->desc.ser == new_desc->ser)) { in pci_devices_present_work()
2631 hpdev->reported_missing = false; in pci_devices_present_work()
2635 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2640 dev_err(&hbus->hdev->device, in pci_devices_present_work()
2646 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2649 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2650 if (hpdev->reported_missing) { in pci_devices_present_work()
2653 list_move_tail(&hpdev->list_entry, &removed); in pci_devices_present_work()
2658 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2664 list_del(&hpdev->list_entry); in pci_devices_present_work()
2666 if (hpdev->pci_slot) in pci_devices_present_work()
2667 pci_destroy_slot(hpdev->pci_slot); in pci_devices_present_work()
2672 switch (hbus->state) { in pci_devices_present_work()
2679 pci_scan_child_bus(hbus->bridge->bus); in pci_devices_present_work()
2694 mutex_unlock(&hbus->state_lock); in pci_devices_present_work()
2700 * hv_pci_start_relations_work() - Queue work to start device discovery
2704 * Return: 0 on success, -errno on failure
2713 if (hbus->state == hv_pcibus_removing) { in hv_pci_start_relations_work()
2714 dev_info(&hbus->hdev->device, in hv_pci_start_relations_work()
2716 return -ENOENT; in hv_pci_start_relations_work()
2721 return -ENOMEM; in hv_pci_start_relations_work()
2723 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work); in hv_pci_start_relations_work()
2724 dr_wrk->bus = hbus; in hv_pci_start_relations_work()
2726 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2732 pending_dr = !list_empty(&hbus->dr_list); in hv_pci_start_relations_work()
2733 list_add_tail(&dr->list_entry, &hbus->dr_list); in hv_pci_start_relations_work()
2734 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2739 queue_work(hbus->wq, &dr_wrk->wrk); in hv_pci_start_relations_work()
2745 * hv_pci_devices_present() - Handle list of new children
2759 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present()
2764 dr->device_count = relations->device_count; in hv_pci_devices_present()
2765 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present()
2766 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present()
2767 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present()
2768 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present()
2769 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present()
2770 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present()
2771 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present()
2772 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present()
2773 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present()
2774 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present()
2782 * hv_pci_devices_present2() - Handle list of new children
2794 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present2()
2799 dr->device_count = relations->device_count; in hv_pci_devices_present2()
2800 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present2()
2801 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present2()
2802 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present2()
2803 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present2()
2804 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present2()
2805 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present2()
2806 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present2()
2807 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present2()
2808 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present2()
2809 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present2()
2810 dr->func[i].flags = relations->func[i].flags; in hv_pci_devices_present2()
2811 dr->func[i].virtual_numa_node = in hv_pci_devices_present2()
2812 relations->func[i].virtual_numa_node; in hv_pci_devices_present2()
2820 * hv_eject_device_work() - Asynchronously handles ejection
2842 hbus = hpdev->hbus; in hv_eject_device_work()
2844 mutex_lock(&hbus->state_lock); in hv_eject_device_work()
2849 * must be done without constructs like pci_domain_nr(hbus->bridge->bus) in hv_eject_device_work()
2850 * because hbus->bridge->bus may not exist yet. in hv_eject_device_work()
2852 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot); in hv_eject_device_work()
2853 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot); in hv_eject_device_work()
2861 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_eject_device_work()
2862 list_del(&hpdev->list_entry); in hv_eject_device_work()
2863 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_eject_device_work()
2865 if (hpdev->pci_slot) in hv_eject_device_work()
2866 pci_destroy_slot(hpdev->pci_slot); in hv_eject_device_work()
2870 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; in hv_eject_device_work()
2871 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_eject_device_work()
2872 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt, in hv_eject_device_work()
2883 mutex_unlock(&hbus->state_lock); in hv_eject_device_work()
2887 * hv_pci_eject_device() - Handles device ejection
2891 * just schedules work so that we don't re-enter the packet
2896 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pci_eject_device()
2897 struct hv_device *hdev = hbus->hdev; in hv_pci_eject_device()
2899 if (hbus->state == hv_pcibus_removing) { in hv_pci_eject_device()
2900 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n"); in hv_pci_eject_device()
2905 INIT_WORK(&hpdev->wrk, hv_eject_device_work); in hv_pci_eject_device()
2906 queue_work(hbus->wq, &hpdev->wrk); in hv_pci_eject_device()
2910 * hv_pci_onchannelcallback() - Handles incoming packets
2921 struct vmbus_channel *chan = hbus->hdev->channel; in hv_pci_onchannelcallback()
2945 if (ret == -ENOBUFS) { in hv_pci_onchannelcallback()
2967 switch (desc->type) { in hv_pci_onchannelcallback()
2975 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
2983 * Call ->completion_func() within the critical section to make in hv_pci_onchannelcallback()
2989 comp_packet->completion_func(comp_packet->compl_ctxt, in hv_pci_onchannelcallback()
2998 switch (new_message->message_type.type) { in hv_pci_onchannelcallback()
3005 bus_rel->device_count)) { in hv_pci_onchannelcallback()
3006 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3020 bus_rel2->device_count)) { in hv_pci_onchannelcallback()
3021 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3033 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3038 dev_message->wslot.slot); in hv_pci_onchannelcallback()
3049 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3054 inval->wslot.slot); in hv_pci_onchannelcallback()
3056 if (hpdev->block_invalidate) { in hv_pci_onchannelcallback()
3057 hpdev->block_invalidate( in hv_pci_onchannelcallback()
3058 hpdev->invalidate_context, in hv_pci_onchannelcallback()
3059 inval->block_mask); in hv_pci_onchannelcallback()
3066 dev_warn(&hbus->hdev->device, in hv_pci_onchannelcallback()
3068 new_message->message_type.type); in hv_pci_onchannelcallback()
3074 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3076 desc->type, req_id, bytes_recvd); in hv_pci_onchannelcallback()
3085 * hv_pci_protocol_negotiation() - Set up protocol
3088 * the order of probing - highest go first.
3122 return -ENOMEM; in hv_pci_protocol_negotiation()
3125 pkt->completion_func = hv_pci_generic_compl; in hv_pci_protocol_negotiation()
3126 pkt->compl_ctxt = &comp_pkt; in hv_pci_protocol_negotiation()
3127 version_req = (struct pci_version_request *)&pkt->message; in hv_pci_protocol_negotiation()
3128 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION; in hv_pci_protocol_negotiation()
3131 version_req->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3132 ret = vmbus_sendpacket(hdev->channel, version_req, in hv_pci_protocol_negotiation()
3140 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3141 "PCI Pass-through VSP failed to request version: %d", in hv_pci_protocol_negotiation()
3147 hbus->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3148 dev_info(&hdev->device, in hv_pci_protocol_negotiation()
3150 hbus->protocol_version); in hv_pci_protocol_negotiation()
3155 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3156 "PCI Pass-through VSP failed version request: %#x", in hv_pci_protocol_negotiation()
3158 ret = -EPROTO; in hv_pci_protocol_negotiation()
3165 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3166 "PCI pass-through VSP failed to find supported version"); in hv_pci_protocol_negotiation()
3167 ret = -EPROTO; in hv_pci_protocol_negotiation()
3175 * hv_pci_free_bridge_windows() - Release memory regions for the
3186 if (hbus->low_mmio_space && hbus->low_mmio_res) { in hv_pci_free_bridge_windows()
3187 hbus->low_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3188 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_free_bridge_windows()
3189 resource_size(hbus->low_mmio_res)); in hv_pci_free_bridge_windows()
3192 if (hbus->high_mmio_space && hbus->high_mmio_res) { in hv_pci_free_bridge_windows()
3193 hbus->high_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3194 vmbus_free_mmio(hbus->high_mmio_res->start, in hv_pci_free_bridge_windows()
3195 resource_size(hbus->high_mmio_res)); in hv_pci_free_bridge_windows()
3200 * hv_pci_allocate_bridge_windows() - Allocate memory regions
3222 * Return: 0 on success, -errno on failure
3229 if (hbus->low_mmio_space) { in hv_pci_allocate_bridge_windows()
3230 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in hv_pci_allocate_bridge_windows()
3231 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0, in hv_pci_allocate_bridge_windows()
3233 hbus->low_mmio_space, in hv_pci_allocate_bridge_windows()
3236 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3238 hbus->low_mmio_space); in hv_pci_allocate_bridge_windows()
3243 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3244 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3245 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res); in hv_pci_allocate_bridge_windows()
3248 if (hbus->high_mmio_space) { in hv_pci_allocate_bridge_windows()
3249 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space)); in hv_pci_allocate_bridge_windows()
3250 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev, in hv_pci_allocate_bridge_windows()
3251 0x100000000, -1, in hv_pci_allocate_bridge_windows()
3252 hbus->high_mmio_space, align, in hv_pci_allocate_bridge_windows()
3255 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3257 hbus->high_mmio_space); in hv_pci_allocate_bridge_windows()
3262 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3263 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3264 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res); in hv_pci_allocate_bridge_windows()
3270 if (hbus->low_mmio_res) { in hv_pci_allocate_bridge_windows()
3271 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_allocate_bridge_windows()
3272 resource_size(hbus->low_mmio_res)); in hv_pci_allocate_bridge_windows()
3279 * hv_allocate_config_window() - Find MMIO space for PCI Config
3282 * This function claims memory-mapped I/O space for accessing
3285 * Return: 0 on success, -errno on failure
3295 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1, in hv_allocate_config_window()
3308 hbus->mem_config->flags |= IORESOURCE_BUSY; in hv_allocate_config_window()
3315 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH); in hv_free_config_window()
3321 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
3324 * Return: 0 on success, -errno on failure
3338 * powered-on state. This includes telling the host which region in hv_pci_enter_d0()
3339 * of memory-mapped I/O space has been chosen for configuration space in hv_pci_enter_d0()
3344 return -ENOMEM; in hv_pci_enter_d0()
3347 pkt->completion_func = hv_pci_generic_compl; in hv_pci_enter_d0()
3348 pkt->compl_ctxt = &comp_pkt; in hv_pci_enter_d0()
3349 d0_entry = (struct pci_bus_d0_entry *)&pkt->message; in hv_pci_enter_d0()
3350 d0_entry->message_type.type = PCI_BUS_D0ENTRY; in hv_pci_enter_d0()
3351 d0_entry->mmio_base = hbus->mem_config->start; in hv_pci_enter_d0()
3353 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry), in hv_pci_enter_d0()
3372 dev_err(&hdev->device, "Retrying D0 Entry\n"); in hv_pci_enter_d0()
3382 hbus->wslot_res_allocated = 255; in hv_pci_enter_d0()
3390 dev_err(&hdev->device, in hv_pci_enter_d0()
3395 dev_err(&hdev->device, in hv_pci_enter_d0()
3396 "PCI Pass-through VSP failed D0 Entry with status %x\n", in hv_pci_enter_d0()
3398 ret = -EPROTO; in hv_pci_enter_d0()
3410 * hv_pci_query_relations() - Ask host to send list of child
3414 * Return: 0 on success, -errno on failure
3425 if (cmpxchg(&hbus->survey_event, NULL, &comp)) in hv_pci_query_relations()
3426 return -ENOTEMPTY; in hv_pci_query_relations()
3431 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message), in hv_pci_query_relations()
3438 * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we in hv_pci_query_relations()
3440 * channel callback already scheduled a work to hbus->wq, which can be in hv_pci_query_relations()
3441 * running pci_devices_present_work() -> survey_child_resources() -> in hv_pci_query_relations()
3442 * complete(&hbus->survey_event), even after hv_pci_query_relations() in hv_pci_query_relations()
3445 * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from in hv_pci_query_relations()
3447 * -ENODEV, there can't be any more work item scheduled to hbus->wq in hv_pci_query_relations()
3448 * after the flush_workqueue(): see vmbus_onoffer_rescind() -> in hv_pci_query_relations()
3449 * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() -> in hv_pci_query_relations()
3450 * channel->rescind = true. in hv_pci_query_relations()
3452 flush_workqueue(hbus->wq); in hv_pci_query_relations()
3458 * hv_send_resources_allocated() - Report local resource choices
3472 * Return: 0 on success, -errno on failure
3486 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) in hv_send_resources_allocated()
3491 return -ENOMEM; in hv_send_resources_allocated()
3502 pkt->completion_func = hv_pci_generic_compl; in hv_send_resources_allocated()
3503 pkt->compl_ctxt = &comp_pkt; in hv_send_resources_allocated()
3505 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) { in hv_send_resources_allocated()
3507 (struct pci_resources_assigned *)&pkt->message; in hv_send_resources_allocated()
3508 res_assigned->message_type.type = in hv_send_resources_allocated()
3510 res_assigned->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3513 (struct pci_resources_assigned2 *)&pkt->message; in hv_send_resources_allocated()
3514 res_assigned2->message_type.type = in hv_send_resources_allocated()
3516 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3520 ret = vmbus_sendpacket(hdev->channel, &pkt->message, in hv_send_resources_allocated()
3530 ret = -EPROTO; in hv_send_resources_allocated()
3531 dev_err(&hdev->device, in hv_send_resources_allocated()
3537 hbus->wslot_res_allocated = wslot; in hv_send_resources_allocated()
3545 * hv_send_resources_released() - Report local resources
3549 * Return: 0 on success, -errno on failure
3559 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) { in hv_send_resources_released()
3566 pkt.wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_released()
3570 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0, in hv_send_resources_released()
3575 hbus->wslot_res_allocated = wslot - 1; in hv_send_resources_released()
3578 hbus->wslot_res_allocated = -1; in hv_send_resources_released()
3593 * hv_get_dom_num() - Get a valid PCI domain number
3617 * hv_put_dom_num() - Mark the PCI domain number as free
3626 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3630 * Return: 0 on success, -errno on failure
3641 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0); in hv_pci_probe()
3643 return -ENOMEM; in hv_pci_probe()
3647 return -ENOMEM; in hv_pci_probe()
3649 hbus->bridge = bridge; in hv_pci_probe()
3650 mutex_init(&hbus->state_lock); in hv_pci_probe()
3651 hbus->state = hv_pcibus_init; in hv_pci_probe()
3652 hbus->wslot_res_allocated = -1; in hv_pci_probe()
3660 * Note that, since this code only runs in a Hyper-V VM, Hyper-V in hv_pci_probe()
3667 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4]; in hv_pci_probe()
3671 dev_err(&hdev->device, in hv_pci_probe()
3673 ret = -EINVAL; in hv_pci_probe()
3678 dev_info(&hdev->device, in hv_pci_probe()
3682 hbus->bridge->domain_nr = dom; in hv_pci_probe()
3684 hbus->sysdata.domain = dom; in hv_pci_probe()
3685 hbus->use_calls = !!(ms_hyperv.hints & HV_X64_USE_MMIO_HYPERCALLS); in hv_pci_probe()
3694 hbus->sysdata.parent = hdev->device.parent; in hv_pci_probe()
3695 hbus->use_calls = false; in hv_pci_probe()
3698 hbus->hdev = hdev; in hv_pci_probe()
3699 INIT_LIST_HEAD(&hbus->children); in hv_pci_probe()
3700 INIT_LIST_HEAD(&hbus->dr_list); in hv_pci_probe()
3701 spin_lock_init(&hbus->config_lock); in hv_pci_probe()
3702 spin_lock_init(&hbus->device_list_lock); in hv_pci_probe()
3703 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0, in hv_pci_probe()
3704 hbus->bridge->domain_nr); in hv_pci_probe()
3705 if (!hbus->wq) { in hv_pci_probe()
3706 ret = -ENOMEM; in hv_pci_probe()
3710 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_probe()
3711 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_probe()
3712 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_probe()
3714 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_probe()
3730 hbus->cfg_addr = ioremap(hbus->mem_config->start, in hv_pci_probe()
3732 if (!hbus->cfg_addr) { in hv_pci_probe()
3733 dev_err(&hdev->device, in hv_pci_probe()
3735 ret = -ENOMEM; in hv_pci_probe()
3739 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance); in hv_pci_probe()
3741 ret = -ENOMEM; in hv_pci_probe()
3745 hbus->fwnode = irq_domain_alloc_named_fwnode(name); in hv_pci_probe()
3747 if (!hbus->fwnode) { in hv_pci_probe()
3748 ret = -ENOMEM; in hv_pci_probe()
3760 mutex_lock(&hbus->state_lock); in hv_pci_probe()
3776 hbus->state = hv_pcibus_probed; in hv_pci_probe()
3782 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3790 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3792 irq_domain_remove(hbus->irq_domain); in hv_pci_probe()
3794 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_probe()
3796 iounmap(hbus->cfg_addr); in hv_pci_probe()
3800 vmbus_close(hdev->channel); in hv_pci_probe()
3802 destroy_workqueue(hbus->wq); in hv_pci_probe()
3804 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_probe()
3813 struct vmbus_channel *chan = hdev->channel; in hv_pci_bus_exit()
3826 * access the per-channel ringbuffer any longer. in hv_pci_bus_exit()
3828 if (chan->rescind) in hv_pci_bus_exit()
3836 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3837 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) in hv_pci_bus_exit()
3838 list_move_tail(&hpdev->list_entry, &removed); in hv_pci_bus_exit()
3839 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3843 list_del(&hpdev->list_entry); in hv_pci_bus_exit()
3844 if (hpdev->pci_slot) in hv_pci_bus_exit()
3845 pci_destroy_slot(hpdev->pci_slot); in hv_pci_bus_exit()
3854 dev_err(&hdev->device, in hv_pci_bus_exit()
3883 return -ETIMEDOUT; in hv_pci_bus_exit()
3890 * hv_pci_remove() - Remove routine for this VMBus channel
3898 if (hbus->state == hv_pcibus_installed) { in hv_pci_remove()
3899 tasklet_disable(&hdev->channel->callback_event); in hv_pci_remove()
3900 hbus->state = hv_pcibus_removing; in hv_pci_remove()
3901 tasklet_enable(&hdev->channel->callback_event); in hv_pci_remove()
3902 destroy_workqueue(hbus->wq); in hv_pci_remove()
3903 hbus->wq = NULL; in hv_pci_remove()
3906 * on hbus-wq. We can't race with hv_pci_devices_present() in hv_pci_remove()
3912 pci_stop_root_bus(hbus->bridge->bus); in hv_pci_remove()
3914 pci_remove_root_bus(hbus->bridge->bus); in hv_pci_remove()
3920 vmbus_close(hdev->channel); in hv_pci_remove()
3922 iounmap(hbus->cfg_addr); in hv_pci_remove()
3925 irq_domain_remove(hbus->irq_domain); in hv_pci_remove()
3926 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_remove()
3928 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_remove()
3945 * items onto hbus->wq in hv_pci_devices_present() and in hv_pci_suspend()
3948 * the work item handler pci_devices_present_work() -> in hv_pci_suspend()
3952 * callback tasklet, sets hbus->state to hv_pcibus_removing, and in hv_pci_suspend()
3953 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds, in hv_pci_suspend()
3955 * hbus->wq and safely closes the vmbus channel. in hv_pci_suspend()
3957 tasklet_disable(&hdev->channel->callback_event); in hv_pci_suspend()
3960 old_state = hbus->state; in hv_pci_suspend()
3961 if (hbus->state == hv_pcibus_installed) in hv_pci_suspend()
3962 hbus->state = hv_pcibus_removing; in hv_pci_suspend()
3964 tasklet_enable(&hdev->channel->callback_event); in hv_pci_suspend()
3967 return -EINVAL; in hv_pci_suspend()
3969 flush_workqueue(hbus->wq); in hv_pci_suspend()
3975 vmbus_close(hdev->channel); in hv_pci_suspend()
3986 if (!pdev->msi_enabled && !pdev->msix_enabled) in hv_pci_restore_msi_msg()
3989 msi_lock_descs(&pdev->dev); in hv_pci_restore_msi_msg()
3990 msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { in hv_pci_restore_msi_msg()
3991 irq_data = irq_get_irq_data(entry->irq); in hv_pci_restore_msi_msg()
3993 ret = -EINVAL; in hv_pci_restore_msi_msg()
3997 hv_compose_msi_msg(irq_data, &entry->msg); in hv_pci_restore_msi_msg()
3999 msi_unlock_descs(&pdev->dev); in hv_pci_restore_msi_msg()
4005 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
4006 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
4008 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
4013 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL); in hv_pci_restore_msi_state()
4022 hbus->state = hv_pcibus_init; in hv_pci_resume()
4024 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_resume()
4025 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_resume()
4026 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_resume()
4028 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_resume()
4034 version[0] = hbus->protocol_version; in hv_pci_resume()
4043 mutex_lock(&hbus->state_lock); in hv_pci_resume()
4057 hbus->state = hv_pcibus_installed; in hv_pci_resume()
4058 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4062 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4064 vmbus_close(hdev->channel); in hv_pci_resume()
4069 /* PCI Pass-through Class ID */
4070 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
4100 return -ENODEV; in init_hv_pci_drv()
4120 MODULE_DESCRIPTION("Hyper-V PCI");