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>
47 #include <linux/irqchip/irq-msi-lib.h>
73 #define CPU_AFFINITY_ALL -1ULL
76 * Supported protocol versions in the order of probing - highest go
88 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
98 * Size of requestor for VMbus; the value is based on the observation
155 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
158 * the Hyper-V parent partition.
211 * occupied by this Message-Signaled
214 * 32. For "MSI-X," as first defined in PCI
215 * 3.0, this must be 1, as each MSI-X table
229 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
235 * occupied by this Message-Signaled
238 * 32. For "MSI-X," as first defined in PCI
239 * 3.0, this must be 1, as each MSI-X table
253 * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
273 * a message-signaled interrupt, either MSI
274 * or MSI-X.
596 return cfg->vector; in hv_msi_get_int_vector()
602 * hv_irq_retarget_interrupt() - "Unmask" the IRQ by setting its current
628 pbus = pdev->bus; in hv_irq_retarget_interrupt()
629 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_irq_retarget_interrupt()
630 int_desc = data->chip_data; in hv_irq_retarget_interrupt()
632 dev_warn(&hbus->hdev->device, "%s() can not unmask irq %u\n", in hv_irq_retarget_interrupt()
633 __func__, data->irq); in hv_irq_retarget_interrupt()
641 params->partition_id = HV_PARTITION_ID_SELF; in hv_irq_retarget_interrupt()
642 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI; in hv_irq_retarget_interrupt()
643 params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff; in hv_irq_retarget_interrupt()
644 params->int_entry.msi_entry.data.as_uint32 = int_desc->data; in hv_irq_retarget_interrupt()
645 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) | in hv_irq_retarget_interrupt()
646 (hbus->hdev->dev_instance.b[4] << 16) | in hv_irq_retarget_interrupt()
647 (hbus->hdev->dev_instance.b[7] << 8) | in hv_irq_retarget_interrupt()
648 (hbus->hdev->dev_instance.b[6] & 0xf8) | in hv_irq_retarget_interrupt()
649 PCI_FUNC(pdev->devfn); in hv_irq_retarget_interrupt()
650 params->int_target.vector = hv_msi_get_int_vector(data); in hv_irq_retarget_interrupt()
652 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) { in hv_irq_retarget_interrupt()
660 params->int_target.flags |= in hv_irq_retarget_interrupt()
669 nr_bank = cpumask_to_vpset(¶ms->int_target.vp_set, tmp); in hv_irq_retarget_interrupt()
678 * var-sized hypercall, var-size starts after vp_mask (thus in hv_irq_retarget_interrupt()
685 params->int_target.vp_mask |= in hv_irq_retarget_interrupt()
701 * refer to cpu_disable_common() -> fixup_irqs() -> in hv_irq_retarget_interrupt()
702 * irq_migrate_all_off_this_cpu() -> migrate_one_irq(). in hv_irq_retarget_interrupt()
707 * into the irqdata data structure in migrate_one_irq() -> in hv_irq_retarget_interrupt()
712 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing) in hv_irq_retarget_interrupt()
713 dev_err(&hbus->hdev->device, in hv_irq_retarget_interrupt()
734 * starting with a power of two to satisfy power of 2 multi-MSI requirement.
737 #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
747 /* Hyper-V vPCI MSI GIC IRQ domain */
750 /* Hyper-V PCI MSI IRQ chip */
761 return irqd->parent_data->hwirq; in hv_msi_get_int_vector()
775 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_irq_free()
777 int first = d->hwirq - HV_PCI_MSI_SPI_START; in hv_pci_vec_irq_free()
780 mutex_lock(&chip_data->map_lock); in hv_pci_vec_irq_free()
781 bitmap_release_region(chip_data->spi_map, in hv_pci_vec_irq_free()
784 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_irq_free()
805 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_alloc_device_irq()
809 mutex_lock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
810 index = bitmap_find_free_region(chip_data->spi_map, in hv_pci_vec_alloc_device_irq()
813 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
815 return -ENOSPC; in hv_pci_vec_alloc_device_irq()
830 fwspec.fwnode = domain->parent->fwnode; in hv_pci_vec_irq_gic_domain_alloc()
835 fwspec.param[1] = hwirq - 32; in hv_pci_vec_irq_gic_domain_alloc()
852 d = irq_domain_get_irq_data(domain->parent, virq); in hv_pci_vec_irq_gic_domain_alloc()
854 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING); in hv_pci_vec_irq_gic_domain_alloc()
880 domain->host_data); in hv_pci_vec_irq_domain_alloc()
916 parent = of_irq_find_parent(hv_get_vmbus_root_device()->of_node); in hv_pci_of_irq_domain_parent()
947 int ret = -ENOMEM; in hv_pci_irqchip_init()
953 mutex_init(&chip_data->map_lock); in hv_pci_irqchip_init()
973 ret = -EINVAL; in hv_pci_irqchip_init()
983 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n"); in hv_pci_irqchip_init()
1004 * registers which Hyper-V already supports, so no hypercall needed.
1010 * hv_pci_generic_compl() - Invoked for a completion packet
1024 comp_pkt->completion_status = resp->status; in hv_pci_generic_compl()
1025 complete(&comp_pkt->host_event); in hv_pci_generic_compl()
1033 refcount_inc(&hpdev->refs); in get_pcichild()
1038 if (refcount_dec_and_test(&hpdev->refs)) in put_pcichild()
1050 if (hdev->channel->rescind) { in wait_for_response()
1051 dev_warn_once(&hdev->device, "The device is gone.\n"); in wait_for_response()
1052 return -ENODEV; in wait_for_response()
1063 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
1082 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
1105 * to use the per-cpu input argument page. Use it for in hv_pci_read_mmio()
1110 in->gpa = gpa; in hv_pci_read_mmio()
1111 in->size = size; in hv_pci_read_mmio()
1117 *val = *(u8 *)(out->data); in hv_pci_read_mmio()
1120 *val = *(u16 *)(out->data); in hv_pci_read_mmio()
1123 *val = *(u32 *)(out->data); in hv_pci_read_mmio()
1138 * to use the per-cpu input argument memory. in hv_pci_write_mmio()
1141 in->gpa = gpa; in hv_pci_write_mmio()
1142 in->size = size; in hv_pci_write_mmio()
1145 *(u8 *)(in->data) = val; in hv_pci_write_mmio()
1148 *(u16 *)(in->data) = val; in hv_pci_write_mmio()
1151 *(u32 *)(in->data) = val; in hv_pci_write_mmio()
1163 * of pages in memory-mapped I/O space. Writing to the first page chooses
1170 * _hv_pcifront_read_config() - Internal PCI config read
1179 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_read_config()
1180 struct device *dev = &hbus->hdev->device; in _hv_pcifront_read_config()
1188 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size); in _hv_pcifront_read_config()
1191 memcpy(val, ((u8 *)&hpdev->desc.rev) + where - in _hv_pcifront_read_config()
1195 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where - in _hv_pcifront_read_config()
1204 * Interrupt Line and Interrupt PIN are hard-wired to zero in _hv_pcifront_read_config()
1205 * because this front-end only supports message-signaled in _hv_pcifront_read_config()
1211 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1212 if (hbus->use_calls) { in _hv_pcifront_read_config()
1213 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_read_config()
1215 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_read_config()
1216 hpdev->desc.win_slot.slot); in _hv_pcifront_read_config()
1219 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_read_config()
1222 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_read_config()
1243 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1251 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pcifront_get_vendor_id()
1252 struct device *dev = &hbus->hdev->device; in hv_pcifront_get_vendor_id()
1257 spin_lock_irqsave(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1259 if (hbus->use_calls) { in hv_pcifront_get_vendor_id()
1260 phys_addr_t addr = hbus->mem_config->start + in hv_pcifront_get_vendor_id()
1263 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in hv_pcifront_get_vendor_id()
1264 hpdev->desc.win_slot.slot); in hv_pcifront_get_vendor_id()
1268 void __iomem *addr = hbus->cfg_addr + CFG_PAGE_OFFSET + in hv_pcifront_get_vendor_id()
1271 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in hv_pcifront_get_vendor_id()
1282 spin_unlock_irqrestore(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1288 * _hv_pcifront_write_config() - Internal PCI config write
1297 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_write_config()
1298 struct device *dev = &hbus->hdev->device; in _hv_pcifront_write_config()
1304 /* SSIDs and ROM BARs are read-only */ in _hv_pcifront_write_config()
1306 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1308 if (hbus->use_calls) { in _hv_pcifront_write_config()
1309 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_write_config()
1311 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_write_config()
1312 hpdev->desc.win_slot.slot); in _hv_pcifront_write_config()
1315 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_write_config()
1318 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_write_config()
1339 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1346 * hv_pcifront_read_config() - Read configuration space
1360 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_read_config()
1374 * hv_pcifront_write_config() - Write configuration space
1388 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_write_config()
1410 * Hyper-V SR-IOV provides a backchannel mechanism in software for
1413 * but instead of doing reads and writes in 32-bit chunks through a very slow
1416 * Nearly every SR-IOV device contains just such a communications channel in
1440 * hv_pci_read_config_compl() - Invoked when a response packet
1456 comp->comp_pkt.completion_status = -1; in hv_pci_read_config_compl()
1460 data_len = resp_packet_size - hdr_len; in hv_pci_read_config_compl()
1461 if (data_len > 0 && read_resp->status == 0) { in hv_pci_read_config_compl()
1462 comp->bytes_returned = min(comp->len, data_len); in hv_pci_read_config_compl()
1463 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned); in hv_pci_read_config_compl()
1465 comp->bytes_returned = 0; in hv_pci_read_config_compl()
1468 comp->comp_pkt.completion_status = read_resp->status; in hv_pci_read_config_compl()
1470 complete(&comp->comp_pkt.host_event); in hv_pci_read_config_compl()
1474 * hv_read_config_block() - Sends a read config block request to
1475 * the back-end driver running in the Hyper-V parent partition.
1480 * @bytes_returned: Size which came back from the back-end driver.
1482 * Return: 0 on success, -errno on failure
1489 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_read_config_block()
1500 return -EINVAL; in hv_read_config_block()
1510 read_blk->message_type.type = PCI_READ_BLOCK; in hv_read_config_block()
1511 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_read_config_block()
1512 read_blk->block_id = block_id; in hv_read_config_block()
1513 read_blk->bytes_requested = len; in hv_read_config_block()
1515 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk, in hv_read_config_block()
1522 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event); in hv_read_config_block()
1528 dev_err(&hbus->hdev->device, in hv_read_config_block()
1532 return -EIO; in hv_read_config_block()
1540 * hv_pci_write_config_compl() - Invoked when a response packet for a write
1551 comp_pkt->completion_status = resp->status; in hv_pci_write_config_compl()
1552 complete(&comp_pkt->host_event); in hv_pci_write_config_compl()
1556 * hv_write_config_block() - Sends a write config block request to the
1557 * back-end driver running in the Hyper-V parent partition.
1563 * Return: 0 on success, -errno on failure
1569 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_write_config_block()
1582 return -EINVAL; in hv_write_config_block()
1590 write_blk->message_type.type = PCI_WRITE_BLOCK; in hv_write_config_block()
1591 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_write_config_block()
1592 write_blk->block_id = block_id; in hv_write_config_block()
1593 write_blk->byte_count = len; in hv_write_config_block()
1594 memcpy(write_blk->bytes, buf, len); in hv_write_config_block()
1601 * specified in write_blk->byte_count. in hv_write_config_block()
1605 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size, in hv_write_config_block()
1611 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event); in hv_write_config_block()
1616 dev_err(&hbus->hdev->device, in hv_write_config_block()
1619 return -EIO; in hv_write_config_block()
1626 * hv_register_block_invalidate() - Invoked when a config block invalidation
1627 * arrives from the back-end driver.
1632 * Return: 0 on success, -errno on failure
1639 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_register_block_invalidate()
1643 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_register_block_invalidate()
1645 return -ENODEV; in hv_register_block_invalidate()
1647 hpdev->block_invalidate = block_invalidate; in hv_register_block_invalidate()
1648 hpdev->invalidate_context = context; in hv_register_block_invalidate()
1665 if (!int_desc->vector_count) { in hv_int_desc_free()
1671 int_pkt->message_type.type = in hv_int_desc_free()
1673 int_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_int_desc_free()
1674 int_pkt->int_desc = *int_desc; in hv_int_desc_free()
1675 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), in hv_int_desc_free()
1681 * hv_msi_free() - Free the MSI.
1685 * The Hyper-V parent partition and hypervisor are tracking the
1700 hbus = domain->host_data; in hv_msi_free()
1705 irq_data->chip_data = NULL; in hv_msi_free()
1706 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_msi_free()
1718 if (data->parent_data->chip->irq_mask) in hv_irq_mask()
1726 if (data->parent_data->chip->irq_unmask) in hv_irq_unmask()
1743 comp_pkt->comp_pkt.completion_status = -1; in hv_pci_compose_compl()
1746 comp_pkt->comp_pkt.completion_status = resp->status; in hv_pci_compose_compl()
1747 comp_pkt->int_desc = int_resp->int_desc; in hv_pci_compose_compl()
1749 complete(&comp_pkt->comp_pkt.host_event); in hv_pci_compose_compl()
1756 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE; in hv_compose_msi_req_v1()
1757 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v1()
1758 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v1()
1759 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v1()
1760 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v1()
1766 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL; in hv_compose_msi_req_v1()
1774 * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
1776 * not irrelevant because Hyper-V chooses the physical CPU to handle the
1777 * interrupts based on the vCPU specified in message sent to the vPCI VSP in
1778 * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
1780 * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
1783 * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
1785 * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
1786 * new pCPU for the interrupt. But for the multi-MSI case, the second call to
1788 * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
1789 * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
1793 * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
1794 * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
1796 * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
1810 * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
1816 /* -1 means starting with CPU 0 */ in hv_compose_multi_msi_req_get_cpu()
1817 static int cpu_next = -1; in hv_compose_multi_msi_req_get_cpu()
1836 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2; in hv_compose_msi_req_v2()
1837 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v2()
1838 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v2()
1839 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v2()
1840 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v2()
1841 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v2()
1843 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v2()
1852 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3; in hv_compose_msi_req_v3()
1853 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v3()
1854 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v3()
1855 int_pkt->int_desc.reserved = 0; in hv_compose_msi_req_v3()
1856 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v3()
1857 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v3()
1858 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v3()
1860 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v3()
1866 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1908 multi_msi = !msi_desc->pci.msi_attrib.is_msix && in hv_compose_msi_msg()
1909 msi_desc->nvec_used > 1; in hv_compose_msi_msg()
1912 if (data->chip_data && multi_msi) { in hv_compose_msi_msg()
1913 int_desc = data->chip_data; in hv_compose_msi_msg()
1914 msg->address_hi = int_desc->address >> 32; in hv_compose_msi_msg()
1915 msg->address_lo = int_desc->address & 0xffffffff; in hv_compose_msi_msg()
1916 msg->data = int_desc->data; in hv_compose_msi_msg()
1922 pbus = pdev->bus; in hv_compose_msi_msg()
1923 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_compose_msi_msg()
1924 channel = hbus->hdev->channel; in hv_compose_msi_msg()
1925 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_compose_msi_msg()
1930 if (data->chip_data && !multi_msi) { in hv_compose_msi_msg()
1931 int_desc = data->chip_data; in hv_compose_msi_msg()
1932 data->chip_data = NULL; in hv_compose_msi_msg()
1945 if (msi_desc->irq != data->irq) { in hv_compose_msi_msg()
1946 data->chip_data = int_desc; in hv_compose_msi_msg()
1947 int_desc->address = msi_desc->msg.address_lo | in hv_compose_msi_msg()
1948 (u64)msi_desc->msg.address_hi << 32; in hv_compose_msi_msg()
1949 int_desc->data = msi_desc->msg.data + in hv_compose_msi_msg()
1950 (data->irq - msi_desc->irq); in hv_compose_msi_msg()
1951 msg->address_hi = msi_desc->msg.address_hi; in hv_compose_msi_msg()
1952 msg->address_lo = msi_desc->msg.address_lo; in hv_compose_msi_msg()
1953 msg->data = int_desc->data; in hv_compose_msi_msg()
1960 * to be aligned with the count, and also not zero. Multi-msi in hv_compose_msi_msg()
1964 vector_count = msi_desc->nvec_used; in hv_compose_msi_msg()
1982 switch (hbus->protocol_version) { in hv_compose_msi_msg()
1985 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1994 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
2002 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
2012 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
2017 ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts, in hv_compose_msi_msg()
2022 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
2032 tasklet_disable_in_atomic(&channel->callback_event); in hv_compose_msi_msg()
2043 dev_err_once(&hbus->hdev->device, in hv_compose_msi_msg()
2055 spin_lock_irqsave(&channel->sched_lock, flags); in hv_compose_msi_msg()
2056 if (unlikely(channel->onchannel_callback == NULL)) { in hv_compose_msi_msg()
2057 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2061 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2066 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2069 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
2081 data->chip_data = int_desc; in hv_compose_msi_msg()
2084 msg->address_hi = comp.int_desc.address >> 32; in hv_compose_msi_msg()
2085 msg->address_lo = comp.int_desc.address & 0xffffffff; in hv_compose_msi_msg()
2086 msg->data = comp.int_desc.data; in hv_compose_msi_msg()
2092 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2097 * been 're-used', i.e., already removed and (re-)mapped.) in hv_compose_msi_msg()
2107 msg->address_hi = 0; in hv_compose_msi_msg()
2108 msg->address_lo = 0; in hv_compose_msi_msg()
2109 msg->data = 0; in hv_compose_msi_msg()
2115 struct irq_chip *chip = info->chip; in hv_pcie_init_dev_msi_info()
2120 info->ops->msi_prepare = hv_msi_prepare; in hv_pcie_init_dev_msi_info()
2122 chip->irq_set_affinity = irq_chip_set_affinity_parent; in hv_pcie_init_dev_msi_info()
2125 chip->flags |= IRQCHIP_MOVE_DEFERRED; in hv_pcie_init_dev_msi_info()
2143 .prefix = "HV-",
2149 .name = "Hyper-V PCIe MSI",
2194 * hv_pcie_init_irq_domain() - Initialize IRQ domain
2199 * devices only support MSI and MSI-X, not line-based interrupts
2200 * or simulations of line-based interrupts through PCIe's
2201 * fabric-layer messages. Because interrupts are remapped, we
2202 * can support multi-message MSI here.
2209 .fwnode = hbus->fwnode, in hv_pcie_init_irq_domain()
2215 hbus->irq_domain = msi_create_parent_irq_domain(&info, &hv_pcie_msi_parent_ops); in hv_pcie_init_irq_domain()
2216 if (!hbus->irq_domain) { in hv_pcie_init_irq_domain()
2217 dev_err(&hbus->hdev->device, in hv_pcie_init_irq_domain()
2219 return -ENODEV; in hv_pcie_init_irq_domain()
2222 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain); in hv_pcie_init_irq_domain()
2228 * get_bar_size() - Get the address space consumed by a BAR
2229 * @bar_val: Value that a BAR returned after -1 was written
2248 * survey_child_resources() - Total all MMIO requirements
2261 event = xchg(&hbus->survey_event, NULL); in survey_child_resources()
2266 if (hbus->low_mmio_space || hbus->high_mmio_space) { in survey_child_resources()
2271 spin_lock_irqsave(&hbus->device_list_lock, flags); in survey_child_resources()
2278 list_for_each_entry(hpdev, &hbus->children, list_entry) { in survey_child_resources()
2280 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO) in survey_child_resources()
2281 dev_err(&hbus->hdev->device, in survey_child_resources()
2284 if (hpdev->probed_bar[i] != 0) { in survey_child_resources()
2290 bar_val = hpdev->probed_bar[i]; in survey_child_resources()
2293 ((u64)hpdev->probed_bar[++i] << 32); in survey_child_resources()
2300 hbus->high_mmio_space += bar_size; in survey_child_resources()
2302 hbus->low_mmio_space += bar_size; in survey_child_resources()
2307 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in survey_child_resources()
2312 * prepopulate_bars() - Fill in BARs with defaults
2320 * the memory-mapped I/O space will be aligned on the largest
2337 if (hbus->low_mmio_space) { in prepopulate_bars()
2338 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in prepopulate_bars()
2339 low_base = hbus->low_mmio_res->start; in prepopulate_bars()
2342 if (hbus->high_mmio_space) { in prepopulate_bars()
2344 (63 - __builtin_clzll(hbus->high_mmio_space)); in prepopulate_bars()
2345 high_base = hbus->high_mmio_res->start; in prepopulate_bars()
2348 spin_lock_irqsave(&hbus->device_list_lock, flags); in prepopulate_bars()
2356 * If the memory enable bit is already set, Hyper-V silently ignores in prepopulate_bars()
2361 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2369 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2371 bar_val = hpdev->probed_bar[i]; in prepopulate_bars()
2377 ((u64)hpdev->probed_bar[i + 1] in prepopulate_bars()
2411 * to be pre-set. Actually here we intentionally in prepopulate_bars()
2413 * in the core PCI driver doesn't cause Hyper-V in prepopulate_bars()
2427 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in prepopulate_bars()
2436 * same ordered workqueue. Therefore hbus->children list will not change
2445 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_assign_slots()
2446 if (hpdev->pci_slot) in hv_pci_assign_slots()
2449 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot)); in hv_pci_assign_slots()
2450 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser); in hv_pci_assign_slots()
2451 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr, in hv_pci_assign_slots()
2453 if (IS_ERR(hpdev->pci_slot)) { in hv_pci_assign_slots()
2455 hpdev->pci_slot = NULL; in hv_pci_assign_slots()
2467 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_remove_slots()
2468 if (!hpdev->pci_slot) in hv_pci_remove_slots()
2470 pci_destroy_slot(hpdev->pci_slot); in hv_pci_remove_slots()
2471 hpdev->pci_slot = NULL; in hv_pci_remove_slots()
2481 struct pci_bus *bus = hbus->bridge->bus; in hv_pci_assign_numa_node()
2484 list_for_each_entry(dev, &bus->devices, bus_list) { in hv_pci_assign_numa_node()
2485 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn)); in hv_pci_assign_numa_node()
2489 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY && in hv_pci_assign_numa_node()
2490 hv_dev->desc.virtual_numa_node < num_possible_nodes()) in hv_pci_assign_numa_node()
2497 set_dev_node(&dev->dev, in hv_pci_assign_numa_node()
2499 hv_dev->desc.virtual_numa_node)); in hv_pci_assign_numa_node()
2506 * create_root_hv_pci_bus() - Expose a new root PCI bus
2509 * Return: 0 on success, -errno on failure
2514 struct pci_host_bridge *bridge = hbus->bridge; in create_root_hv_pci_bus()
2516 bridge->dev.parent = &hbus->hdev->device; in create_root_hv_pci_bus()
2517 bridge->sysdata = &hbus->sysdata; in create_root_hv_pci_bus()
2518 bridge->ops = &hv_pcifront_ops; in create_root_hv_pci_bus()
2526 pci_bus_assign_resources(bridge->bus); in create_root_hv_pci_bus()
2528 pci_bus_add_devices(bridge->bus); in create_root_hv_pci_bus()
2530 hbus->state = hv_pcibus_installed; in create_root_hv_pci_bus()
2540 * q_resource_requirements() - Query Resource Requirements
2557 status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status; in q_resource_requirements()
2559 dev_err(&completion->hpdev->hbus->hdev->device, in q_resource_requirements()
2564 completion->hpdev->probed_bar[i] = in q_resource_requirements()
2565 q_res_req->probed_bar[i]; in q_resource_requirements()
2569 complete(&completion->host_event); in q_resource_requirements()
2573 * new_pcichild_device() - Create a new child device
2600 hpdev->hbus = hbus; in new_pcichild_device()
2608 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS; in new_pcichild_device()
2609 res_req->wslot.slot = desc->win_slot.slot; in new_pcichild_device()
2611 ret = vmbus_sendpacket(hbus->hdev->channel, res_req, in new_pcichild_device()
2619 if (wait_for_response(hbus->hdev, &comp_pkt.host_event)) in new_pcichild_device()
2622 hpdev->desc = *desc; in new_pcichild_device()
2623 refcount_set(&hpdev->refs, 1); in new_pcichild_device()
2625 spin_lock_irqsave(&hbus->device_list_lock, flags); in new_pcichild_device()
2627 list_add_tail(&hpdev->list_entry, &hbus->children); in new_pcichild_device()
2628 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in new_pcichild_device()
2637 * get_pcichild_wslot() - Find device from slot
2655 spin_lock_irqsave(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2656 list_for_each_entry(iter, &hbus->children, list_entry) { in get_pcichild_wslot()
2657 if (iter->desc.win_slot.slot == wslot) { in get_pcichild_wslot()
2663 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2669 * pci_devices_present_work() - Handle new list of child devices
2674 * debug the interaction between Hyper-V and Linux. This
2689 * because both are pushed to the ordered workqueue hbus->wq.
2704 hbus = dr_wrk->bus; in pci_devices_present_work()
2710 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2711 while (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2712 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state, in pci_devices_present_work()
2714 list_del(&dr->list_entry); in pci_devices_present_work()
2717 if (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2722 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2727 mutex_lock(&hbus->state_lock); in pci_devices_present_work()
2730 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2731 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2732 hpdev->reported_missing = true; in pci_devices_present_work()
2734 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2737 for (child_no = 0; child_no < dr->device_count; child_no++) { in pci_devices_present_work()
2739 new_desc = &dr->func[child_no]; in pci_devices_present_work()
2741 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2742 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2743 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) && in pci_devices_present_work()
2744 (hpdev->desc.v_id == new_desc->v_id) && in pci_devices_present_work()
2745 (hpdev->desc.d_id == new_desc->d_id) && in pci_devices_present_work()
2746 (hpdev->desc.ser == new_desc->ser)) { in pci_devices_present_work()
2747 hpdev->reported_missing = false; in pci_devices_present_work()
2751 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2756 dev_err(&hbus->hdev->device, in pci_devices_present_work()
2762 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2765 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2766 if (hpdev->reported_missing) { in pci_devices_present_work()
2769 list_move_tail(&hpdev->list_entry, &removed); in pci_devices_present_work()
2774 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2780 list_del(&hpdev->list_entry); in pci_devices_present_work()
2782 if (hpdev->pci_slot) in pci_devices_present_work()
2783 pci_destroy_slot(hpdev->pci_slot); in pci_devices_present_work()
2788 switch (hbus->state) { in pci_devices_present_work()
2795 pci_scan_child_bus(hbus->bridge->bus); in pci_devices_present_work()
2810 mutex_unlock(&hbus->state_lock); in pci_devices_present_work()
2816 * hv_pci_start_relations_work() - Queue work to start device discovery
2820 * Return: 0 on success, -errno on failure
2829 if (hbus->state == hv_pcibus_removing) { in hv_pci_start_relations_work()
2830 dev_info(&hbus->hdev->device, in hv_pci_start_relations_work()
2832 return -ENOENT; in hv_pci_start_relations_work()
2837 return -ENOMEM; in hv_pci_start_relations_work()
2839 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work); in hv_pci_start_relations_work()
2840 dr_wrk->bus = hbus; in hv_pci_start_relations_work()
2842 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2848 pending_dr = !list_empty(&hbus->dr_list); in hv_pci_start_relations_work()
2849 list_add_tail(&dr->list_entry, &hbus->dr_list); in hv_pci_start_relations_work()
2850 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2855 queue_work(hbus->wq, &dr_wrk->wrk); in hv_pci_start_relations_work()
2861 * hv_pci_devices_present() - Handle list of new children
2875 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present()
2880 dr->device_count = relations->device_count; in hv_pci_devices_present()
2881 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present()
2882 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present()
2883 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present()
2884 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present()
2885 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present()
2886 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present()
2887 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present()
2888 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present()
2889 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present()
2890 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present()
2898 * hv_pci_devices_present2() - Handle list of new children
2910 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present2()
2915 dr->device_count = relations->device_count; in hv_pci_devices_present2()
2916 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present2()
2917 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present2()
2918 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present2()
2919 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present2()
2920 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present2()
2921 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present2()
2922 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present2()
2923 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present2()
2924 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present2()
2925 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present2()
2926 dr->func[i].flags = relations->func[i].flags; in hv_pci_devices_present2()
2927 dr->func[i].virtual_numa_node = in hv_pci_devices_present2()
2928 relations->func[i].virtual_numa_node; in hv_pci_devices_present2()
2936 * hv_eject_device_work() - Asynchronously handles ejection
2958 hbus = hpdev->hbus; in hv_eject_device_work()
2960 mutex_lock(&hbus->state_lock); in hv_eject_device_work()
2965 * must be done without constructs like pci_domain_nr(hbus->bridge->bus) in hv_eject_device_work()
2966 * because hbus->bridge->bus may not exist yet. in hv_eject_device_work()
2968 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot); in hv_eject_device_work()
2969 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot); in hv_eject_device_work()
2977 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_eject_device_work()
2978 list_del(&hpdev->list_entry); in hv_eject_device_work()
2979 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_eject_device_work()
2981 if (hpdev->pci_slot) in hv_eject_device_work()
2982 pci_destroy_slot(hpdev->pci_slot); in hv_eject_device_work()
2986 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; in hv_eject_device_work()
2987 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_eject_device_work()
2988 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt, in hv_eject_device_work()
2999 mutex_unlock(&hbus->state_lock); in hv_eject_device_work()
3003 * hv_pci_eject_device() - Handles device ejection
3007 * just schedules work so that we don't re-enter the packet
3012 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pci_eject_device()
3013 struct hv_device *hdev = hbus->hdev; in hv_pci_eject_device()
3015 if (hbus->state == hv_pcibus_removing) { in hv_pci_eject_device()
3016 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n"); in hv_pci_eject_device()
3021 INIT_WORK(&hpdev->wrk, hv_eject_device_work); in hv_pci_eject_device()
3022 queue_work(hbus->wq, &hpdev->wrk); in hv_pci_eject_device()
3026 * hv_pci_onchannelcallback() - Handles incoming packets
3037 struct vmbus_channel *chan = hbus->hdev->channel; in hv_pci_onchannelcallback()
3061 if (ret == -ENOBUFS) { in hv_pci_onchannelcallback()
3083 switch (desc->type) { in hv_pci_onchannelcallback()
3091 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3099 * Call ->completion_func() within the critical section to make in hv_pci_onchannelcallback()
3105 comp_packet->completion_func(comp_packet->compl_ctxt, in hv_pci_onchannelcallback()
3114 switch (new_message->message_type.type) { in hv_pci_onchannelcallback()
3121 bus_rel->device_count)) { in hv_pci_onchannelcallback()
3122 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3136 bus_rel2->device_count)) { in hv_pci_onchannelcallback()
3137 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3149 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3154 dev_message->wslot.slot); in hv_pci_onchannelcallback()
3165 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3170 inval->wslot.slot); in hv_pci_onchannelcallback()
3172 if (hpdev->block_invalidate) { in hv_pci_onchannelcallback()
3173 hpdev->block_invalidate( in hv_pci_onchannelcallback()
3174 hpdev->invalidate_context, in hv_pci_onchannelcallback()
3175 inval->block_mask); in hv_pci_onchannelcallback()
3182 dev_warn(&hbus->hdev->device, in hv_pci_onchannelcallback()
3184 new_message->message_type.type); in hv_pci_onchannelcallback()
3190 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3192 desc->type, req_id, bytes_recvd); in hv_pci_onchannelcallback()
3201 * hv_pci_protocol_negotiation() - Set up protocol
3204 * the order of probing - highest go first.
3238 return -ENOMEM; in hv_pci_protocol_negotiation()
3241 pkt->completion_func = hv_pci_generic_compl; in hv_pci_protocol_negotiation()
3242 pkt->compl_ctxt = &comp_pkt; in hv_pci_protocol_negotiation()
3244 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION; in hv_pci_protocol_negotiation()
3247 version_req->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3248 ret = vmbus_sendpacket(hdev->channel, version_req, in hv_pci_protocol_negotiation()
3256 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3257 "PCI Pass-through VSP failed to request version: %d", in hv_pci_protocol_negotiation()
3263 hbus->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3264 dev_info(&hdev->device, in hv_pci_protocol_negotiation()
3266 hbus->protocol_version); in hv_pci_protocol_negotiation()
3271 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3272 "PCI Pass-through VSP failed version request: %#x", in hv_pci_protocol_negotiation()
3274 ret = -EPROTO; in hv_pci_protocol_negotiation()
3281 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3282 "PCI pass-through VSP failed to find supported version"); in hv_pci_protocol_negotiation()
3283 ret = -EPROTO; in hv_pci_protocol_negotiation()
3291 * hv_pci_free_bridge_windows() - Release memory regions for the
3302 if (hbus->low_mmio_space && hbus->low_mmio_res) { in hv_pci_free_bridge_windows()
3303 hbus->low_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3304 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_free_bridge_windows()
3305 resource_size(hbus->low_mmio_res)); in hv_pci_free_bridge_windows()
3308 if (hbus->high_mmio_space && hbus->high_mmio_res) { in hv_pci_free_bridge_windows()
3309 hbus->high_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3310 vmbus_free_mmio(hbus->high_mmio_res->start, in hv_pci_free_bridge_windows()
3311 resource_size(hbus->high_mmio_res)); in hv_pci_free_bridge_windows()
3316 * hv_pci_allocate_bridge_windows() - Allocate memory regions
3338 * Return: 0 on success, -errno on failure
3345 if (hbus->low_mmio_space) { in hv_pci_allocate_bridge_windows()
3346 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in hv_pci_allocate_bridge_windows()
3347 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0, in hv_pci_allocate_bridge_windows()
3349 hbus->low_mmio_space, in hv_pci_allocate_bridge_windows()
3352 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3354 hbus->low_mmio_space); in hv_pci_allocate_bridge_windows()
3359 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3360 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3361 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res); in hv_pci_allocate_bridge_windows()
3364 if (hbus->high_mmio_space) { in hv_pci_allocate_bridge_windows()
3365 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space)); in hv_pci_allocate_bridge_windows()
3366 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev, in hv_pci_allocate_bridge_windows()
3367 0x100000000, -1, in hv_pci_allocate_bridge_windows()
3368 hbus->high_mmio_space, align, in hv_pci_allocate_bridge_windows()
3371 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3373 hbus->high_mmio_space); in hv_pci_allocate_bridge_windows()
3378 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3379 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3380 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res); in hv_pci_allocate_bridge_windows()
3386 if (hbus->low_mmio_res) { in hv_pci_allocate_bridge_windows()
3387 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_allocate_bridge_windows()
3388 resource_size(hbus->low_mmio_res)); in hv_pci_allocate_bridge_windows()
3395 * hv_allocate_config_window() - Find MMIO space for PCI Config
3398 * This function claims memory-mapped I/O space for accessing
3401 * Return: 0 on success, -errno on failure
3411 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1, in hv_allocate_config_window()
3424 hbus->mem_config->flags |= IORESOURCE_BUSY; in hv_allocate_config_window()
3431 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH); in hv_free_config_window()
3437 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
3440 * Return: 0 on success, -errno on failure
3454 * powered-on state. This includes telling the host which region in hv_pci_enter_d0()
3455 * of memory-mapped I/O space has been chosen for configuration space in hv_pci_enter_d0()
3460 return -ENOMEM; in hv_pci_enter_d0()
3463 pkt->completion_func = hv_pci_generic_compl; in hv_pci_enter_d0()
3464 pkt->compl_ctxt = &comp_pkt; in hv_pci_enter_d0()
3466 d0_entry->message_type.type = PCI_BUS_D0ENTRY; in hv_pci_enter_d0()
3467 d0_entry->mmio_base = hbus->mem_config->start; in hv_pci_enter_d0()
3469 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry), in hv_pci_enter_d0()
3488 dev_err(&hdev->device, "Retrying D0 Entry\n"); in hv_pci_enter_d0()
3498 hbus->wslot_res_allocated = 255; in hv_pci_enter_d0()
3506 dev_err(&hdev->device, in hv_pci_enter_d0()
3511 dev_err(&hdev->device, in hv_pci_enter_d0()
3512 "PCI Pass-through VSP failed D0 Entry with status %x\n", in hv_pci_enter_d0()
3514 ret = -EPROTO; in hv_pci_enter_d0()
3526 * hv_pci_query_relations() - Ask host to send list of child
3530 * Return: 0 on success, -errno on failure
3541 if (cmpxchg(&hbus->survey_event, NULL, &comp)) in hv_pci_query_relations()
3542 return -ENOTEMPTY; in hv_pci_query_relations()
3547 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message), in hv_pci_query_relations()
3554 * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we in hv_pci_query_relations()
3556 * channel callback already scheduled a work to hbus->wq, which can be in hv_pci_query_relations()
3557 * running pci_devices_present_work() -> survey_child_resources() -> in hv_pci_query_relations()
3558 * complete(&hbus->survey_event), even after hv_pci_query_relations() in hv_pci_query_relations()
3561 * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from in hv_pci_query_relations()
3563 * -ENODEV, there can't be any more work item scheduled to hbus->wq in hv_pci_query_relations()
3564 * after the flush_workqueue(): see vmbus_onoffer_rescind() -> in hv_pci_query_relations()
3565 * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() -> in hv_pci_query_relations()
3566 * channel->rescind = true. in hv_pci_query_relations()
3568 flush_workqueue(hbus->wq); in hv_pci_query_relations()
3574 * hv_send_resources_allocated() - Report local resource choices
3588 * Return: 0 on success, -errno on failure
3602 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) in hv_send_resources_allocated()
3607 return -ENOMEM; in hv_send_resources_allocated()
3618 pkt->completion_func = hv_pci_generic_compl; in hv_send_resources_allocated()
3619 pkt->compl_ctxt = &comp_pkt; in hv_send_resources_allocated()
3621 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) { in hv_send_resources_allocated()
3624 res_assigned->message_type.type = in hv_send_resources_allocated()
3626 res_assigned->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3630 res_assigned2->message_type.type = in hv_send_resources_allocated()
3632 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3636 ret = vmbus_sendpacket(hdev->channel, pkt + 1, in hv_send_resources_allocated()
3646 ret = -EPROTO; in hv_send_resources_allocated()
3647 dev_err(&hdev->device, in hv_send_resources_allocated()
3653 hbus->wslot_res_allocated = wslot; in hv_send_resources_allocated()
3661 * hv_send_resources_released() - Report local resources
3665 * Return: 0 on success, -errno on failure
3675 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) { in hv_send_resources_released()
3682 pkt.wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_released()
3686 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0, in hv_send_resources_released()
3691 hbus->wslot_res_allocated = wslot - 1; in hv_send_resources_released()
3694 hbus->wslot_res_allocated = -1; in hv_send_resources_released()
3709 * hv_get_dom_num() - Get a valid PCI domain number
3733 * hv_put_dom_num() - Mark the PCI domain number as free
3742 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3746 * Return: 0 on success, -errno on failure
3757 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0); in hv_pci_probe()
3759 return -ENOMEM; in hv_pci_probe()
3763 return -ENOMEM; in hv_pci_probe()
3765 hbus->bridge = bridge; in hv_pci_probe()
3766 mutex_init(&hbus->state_lock); in hv_pci_probe()
3767 hbus->state = hv_pcibus_init; in hv_pci_probe()
3768 hbus->wslot_res_allocated = -1; in hv_pci_probe()
3776 * Note that, since this code only runs in a Hyper-V VM, Hyper-V in hv_pci_probe()
3783 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4]; in hv_pci_probe()
3787 dev_err(&hdev->device, in hv_pci_probe()
3789 ret = -EINVAL; in hv_pci_probe()
3794 dev_info(&hdev->device, in hv_pci_probe()
3798 hbus->bridge->domain_nr = dom; in hv_pci_probe()
3800 hbus->sysdata.domain = dom; in hv_pci_probe()
3801 hbus->use_calls = !!(ms_hyperv.hints & HV_X64_USE_MMIO_HYPERCALLS); in hv_pci_probe()
3810 hbus->sysdata.parent = hdev->device.parent; in hv_pci_probe()
3811 hbus->use_calls = false; in hv_pci_probe()
3814 hbus->hdev = hdev; in hv_pci_probe()
3815 INIT_LIST_HEAD(&hbus->children); in hv_pci_probe()
3816 INIT_LIST_HEAD(&hbus->dr_list); in hv_pci_probe()
3817 spin_lock_init(&hbus->config_lock); in hv_pci_probe()
3818 spin_lock_init(&hbus->device_list_lock); in hv_pci_probe()
3819 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0, in hv_pci_probe()
3820 hbus->bridge->domain_nr); in hv_pci_probe()
3821 if (!hbus->wq) { in hv_pci_probe()
3822 ret = -ENOMEM; in hv_pci_probe()
3826 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_probe()
3827 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_probe()
3828 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_probe()
3830 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_probe()
3846 hbus->cfg_addr = ioremap(hbus->mem_config->start, in hv_pci_probe()
3848 if (!hbus->cfg_addr) { in hv_pci_probe()
3849 dev_err(&hdev->device, in hv_pci_probe()
3851 ret = -ENOMEM; in hv_pci_probe()
3855 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance); in hv_pci_probe()
3857 ret = -ENOMEM; in hv_pci_probe()
3861 hbus->fwnode = irq_domain_alloc_named_fwnode(name); in hv_pci_probe()
3863 if (!hbus->fwnode) { in hv_pci_probe()
3864 ret = -ENOMEM; in hv_pci_probe()
3876 mutex_lock(&hbus->state_lock); in hv_pci_probe()
3892 hbus->state = hv_pcibus_probed; in hv_pci_probe()
3898 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3906 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3908 irq_domain_remove(hbus->irq_domain); in hv_pci_probe()
3910 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_probe()
3912 iounmap(hbus->cfg_addr); in hv_pci_probe()
3916 vmbus_close(hdev->channel); in hv_pci_probe()
3918 destroy_workqueue(hbus->wq); in hv_pci_probe()
3920 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_probe()
3929 struct vmbus_channel *chan = hdev->channel; in hv_pci_bus_exit()
3943 * access the per-channel ringbuffer any longer. in hv_pci_bus_exit()
3945 if (chan->rescind) in hv_pci_bus_exit()
3953 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3954 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) in hv_pci_bus_exit()
3955 list_move_tail(&hpdev->list_entry, &removed); in hv_pci_bus_exit()
3956 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3960 list_del(&hpdev->list_entry); in hv_pci_bus_exit()
3961 if (hpdev->pci_slot) in hv_pci_bus_exit()
3962 pci_destroy_slot(hpdev->pci_slot); in hv_pci_bus_exit()
3971 dev_err(&hdev->device, in hv_pci_bus_exit()
3981 msg->type = PCI_BUS_D0EXIT; in hv_pci_bus_exit()
4000 return -ETIMEDOUT; in hv_pci_bus_exit()
4007 * hv_pci_remove() - Remove routine for this VMBus channel
4015 if (hbus->state == hv_pcibus_installed) { in hv_pci_remove()
4016 tasklet_disable(&hdev->channel->callback_event); in hv_pci_remove()
4017 hbus->state = hv_pcibus_removing; in hv_pci_remove()
4018 tasklet_enable(&hdev->channel->callback_event); in hv_pci_remove()
4019 destroy_workqueue(hbus->wq); in hv_pci_remove()
4020 hbus->wq = NULL; in hv_pci_remove()
4023 * on hbus-wq. We can't race with hv_pci_devices_present() in hv_pci_remove()
4029 pci_stop_root_bus(hbus->bridge->bus); in hv_pci_remove()
4031 pci_remove_root_bus(hbus->bridge->bus); in hv_pci_remove()
4037 vmbus_close(hdev->channel); in hv_pci_remove()
4039 iounmap(hbus->cfg_addr); in hv_pci_remove()
4042 irq_domain_remove(hbus->irq_domain); in hv_pci_remove()
4043 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_remove()
4045 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_remove()
4062 * items onto hbus->wq in hv_pci_devices_present() and in hv_pci_suspend()
4065 * the work item handler pci_devices_present_work() -> in hv_pci_suspend()
4069 * callback tasklet, sets hbus->state to hv_pcibus_removing, and in hv_pci_suspend()
4070 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds, in hv_pci_suspend()
4072 * hbus->wq and safely closes the vmbus channel. in hv_pci_suspend()
4074 tasklet_disable(&hdev->channel->callback_event); in hv_pci_suspend()
4077 old_state = hbus->state; in hv_pci_suspend()
4078 if (hbus->state == hv_pcibus_installed) in hv_pci_suspend()
4079 hbus->state = hv_pcibus_removing; in hv_pci_suspend()
4081 tasklet_enable(&hdev->channel->callback_event); in hv_pci_suspend()
4084 return -EINVAL; in hv_pci_suspend()
4086 flush_workqueue(hbus->wq); in hv_pci_suspend()
4092 vmbus_close(hdev->channel); in hv_pci_suspend()
4102 if (!pdev->msi_enabled && !pdev->msix_enabled) in hv_pci_restore_msi_msg()
4105 guard(msi_descs_lock)(&pdev->dev); in hv_pci_restore_msi_msg()
4106 msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { in hv_pci_restore_msi_msg()
4107 irq_data = irq_get_irq_data(entry->irq); in hv_pci_restore_msi_msg()
4109 return -EINVAL; in hv_pci_restore_msi_msg()
4110 hv_compose_msi_msg(irq_data, &entry->msg); in hv_pci_restore_msi_msg()
4116 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
4117 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
4119 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
4124 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL); in hv_pci_restore_msi_state()
4133 hbus->state = hv_pcibus_init; in hv_pci_resume()
4135 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_resume()
4136 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_resume()
4137 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_resume()
4139 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_resume()
4145 version[0] = hbus->protocol_version; in hv_pci_resume()
4154 mutex_lock(&hbus->state_lock); in hv_pci_resume()
4168 hbus->state = hv_pcibus_installed; in hv_pci_resume()
4169 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4173 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4175 vmbus_close(hdev->channel); in hv_pci_resume()
4180 /* PCI Pass-through Class ID */
4181 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
4211 return -ENODEV; in init_hv_pci_drv()
4214 return -ENODEV; in init_hv_pci_drv()
4234 MODULE_DESCRIPTION("Hyper-V PCI");