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>
71 #define CPU_AFFINITY_ALL -1ULL
74 * Supported protocol versions in the order of probing - highest go
86 #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
96 * Size of requestor for VMbus; the value is based on the observation
153 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
156 * the Hyper-V parent partition.
209 * occupied by this Message-Signaled
212 * 32. For "MSI-X," as first defined in PCI
213 * 3.0, this must be 1, as each MSI-X table
227 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
233 * occupied by this Message-Signaled
236 * 32. For "MSI-X," as first defined in PCI
237 * 3.0, this must be 1, as each MSI-X table
251 * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
271 * a message-signaled interrupt, either MSI
272 * or MSI-X.
598 return cfg->vector; in hv_msi_get_int_vector()
604 * hv_arch_irq_unmask() - "Unmask" the IRQ by setting its current
630 pbus = pdev->bus; in hv_arch_irq_unmask()
631 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_arch_irq_unmask()
632 int_desc = data->chip_data; in hv_arch_irq_unmask()
634 dev_warn(&hbus->hdev->device, "%s() can not unmask irq %u\n", in hv_arch_irq_unmask()
635 __func__, data->irq); in hv_arch_irq_unmask()
643 params->partition_id = HV_PARTITION_ID_SELF; in hv_arch_irq_unmask()
644 params->int_entry.source = HV_INTERRUPT_SOURCE_MSI; in hv_arch_irq_unmask()
645 params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff; in hv_arch_irq_unmask()
646 params->int_entry.msi_entry.data.as_uint32 = int_desc->data; in hv_arch_irq_unmask()
647 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) | in hv_arch_irq_unmask()
648 (hbus->hdev->dev_instance.b[4] << 16) | in hv_arch_irq_unmask()
649 (hbus->hdev->dev_instance.b[7] << 8) | in hv_arch_irq_unmask()
650 (hbus->hdev->dev_instance.b[6] & 0xf8) | in hv_arch_irq_unmask()
651 PCI_FUNC(pdev->devfn); in hv_arch_irq_unmask()
652 params->int_target.vector = hv_msi_get_int_vector(data); in hv_arch_irq_unmask()
655 * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by in hv_arch_irq_unmask()
661 if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) { in hv_arch_irq_unmask()
669 params->int_target.flags |= in hv_arch_irq_unmask()
678 nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp); in hv_arch_irq_unmask()
687 * var-sized hypercall, var-size starts after vp_mask (thus in hv_arch_irq_unmask()
694 params->int_target.vp_mask |= in hv_arch_irq_unmask()
710 * refer to cpu_disable_common() -> fixup_irqs() -> in hv_arch_irq_unmask()
711 * irq_migrate_all_off_this_cpu() -> migrate_one_irq(). in hv_arch_irq_unmask()
716 * into the irqdata data structure in migrate_one_irq() -> in hv_arch_irq_unmask()
721 if (!hv_result_success(res) && hbus->state != hv_pcibus_removing) in hv_arch_irq_unmask()
722 dev_err(&hbus->hdev->device, in hv_arch_irq_unmask()
729 * starting with a power of two to satisfy power of 2 multi-MSI requirement.
732 #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
743 /* Hyper-V vPCI MSI GIC IRQ domain */
746 /* Hyper-V PCI MSI IRQ chip */
757 return irqd->parent_data->hwirq; in hv_msi_get_int_vector()
771 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_irq_free()
773 int first = d->hwirq - HV_PCI_MSI_SPI_START; in hv_pci_vec_irq_free()
776 mutex_lock(&chip_data->map_lock); in hv_pci_vec_irq_free()
777 bitmap_release_region(chip_data->spi_map, in hv_pci_vec_irq_free()
780 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_irq_free()
801 struct hv_pci_chip_data *chip_data = domain->host_data; in hv_pci_vec_alloc_device_irq()
805 mutex_lock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
806 index = bitmap_find_free_region(chip_data->spi_map, in hv_pci_vec_alloc_device_irq()
809 mutex_unlock(&chip_data->map_lock); in hv_pci_vec_alloc_device_irq()
811 return -ENOSPC; in hv_pci_vec_alloc_device_irq()
826 fwspec.fwnode = domain->parent->fwnode; in hv_pci_vec_irq_gic_domain_alloc()
840 d = irq_domain_get_irq_data(domain->parent, virq); in hv_pci_vec_irq_gic_domain_alloc()
842 return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING); in hv_pci_vec_irq_gic_domain_alloc()
868 domain->host_data); in hv_pci_vec_irq_domain_alloc()
901 int ret = -ENOMEM; in hv_pci_irqchip_init()
907 mutex_init(&chip_data->map_lock); in hv_pci_irqchip_init()
922 pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n"); in hv_pci_irqchip_init()
943 * registers which Hyper-V already supports, so no hypercall needed.
949 * hv_pci_generic_compl() - Invoked for a completion packet
963 comp_pkt->completion_status = resp->status; in hv_pci_generic_compl()
964 complete(&comp_pkt->host_event); in hv_pci_generic_compl()
972 refcount_inc(&hpdev->refs); in get_pcichild()
977 if (refcount_dec_and_test(&hpdev->refs)) in put_pcichild()
989 if (hdev->channel->rescind) { in wait_for_response()
990 dev_warn_once(&hdev->device, "The device is gone.\n"); in wait_for_response()
991 return -ENODEV; in wait_for_response()
1002 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
1021 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
1044 * to use the per-cpu input argument page. Use it for in hv_pci_read_mmio()
1049 in->gpa = gpa; in hv_pci_read_mmio()
1050 in->size = size; in hv_pci_read_mmio()
1056 *val = *(u8 *)(out->data); in hv_pci_read_mmio()
1059 *val = *(u16 *)(out->data); in hv_pci_read_mmio()
1062 *val = *(u32 *)(out->data); in hv_pci_read_mmio()
1077 * to use the per-cpu input argument memory. in hv_pci_write_mmio()
1080 in->gpa = gpa; in hv_pci_write_mmio()
1081 in->size = size; in hv_pci_write_mmio()
1084 *(u8 *)(in->data) = val; in hv_pci_write_mmio()
1087 *(u16 *)(in->data) = val; in hv_pci_write_mmio()
1090 *(u32 *)(in->data) = val; in hv_pci_write_mmio()
1102 * of pages in memory-mapped I/O space. Writing to the first page chooses
1109 * _hv_pcifront_read_config() - Internal PCI config read
1118 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_read_config()
1119 struct device *dev = &hbus->hdev->device; in _hv_pcifront_read_config()
1127 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size); in _hv_pcifront_read_config()
1130 memcpy(val, ((u8 *)&hpdev->desc.rev) + where - in _hv_pcifront_read_config()
1134 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where - in _hv_pcifront_read_config()
1143 * Interrupt Line and Interrupt PIN are hard-wired to zero in _hv_pcifront_read_config()
1144 * because this front-end only supports message-signaled in _hv_pcifront_read_config()
1150 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1151 if (hbus->use_calls) { in _hv_pcifront_read_config()
1152 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_read_config()
1154 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_read_config()
1155 hpdev->desc.win_slot.slot); in _hv_pcifront_read_config()
1158 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_read_config()
1161 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_read_config()
1182 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_read_config()
1190 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pcifront_get_vendor_id()
1191 struct device *dev = &hbus->hdev->device; in hv_pcifront_get_vendor_id()
1196 spin_lock_irqsave(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1198 if (hbus->use_calls) { in hv_pcifront_get_vendor_id()
1199 phys_addr_t addr = hbus->mem_config->start + in hv_pcifront_get_vendor_id()
1202 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in hv_pcifront_get_vendor_id()
1203 hpdev->desc.win_slot.slot); in hv_pcifront_get_vendor_id()
1207 void __iomem *addr = hbus->cfg_addr + CFG_PAGE_OFFSET + in hv_pcifront_get_vendor_id()
1210 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in hv_pcifront_get_vendor_id()
1221 spin_unlock_irqrestore(&hbus->config_lock, flags); in hv_pcifront_get_vendor_id()
1227 * _hv_pcifront_write_config() - Internal PCI config write
1236 struct hv_pcibus_device *hbus = hpdev->hbus; in _hv_pcifront_write_config()
1237 struct device *dev = &hbus->hdev->device; in _hv_pcifront_write_config()
1243 /* SSIDs and ROM BARs are read-only */ in _hv_pcifront_write_config()
1245 spin_lock_irqsave(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1247 if (hbus->use_calls) { in _hv_pcifront_write_config()
1248 phys_addr_t addr = hbus->mem_config->start + offset; in _hv_pcifront_write_config()
1250 hv_pci_write_mmio(dev, hbus->mem_config->start, 4, in _hv_pcifront_write_config()
1251 hpdev->desc.win_slot.slot); in _hv_pcifront_write_config()
1254 void __iomem *addr = hbus->cfg_addr + offset; in _hv_pcifront_write_config()
1257 writel(hpdev->desc.win_slot.slot, hbus->cfg_addr); in _hv_pcifront_write_config()
1278 spin_unlock_irqrestore(&hbus->config_lock, flags); in _hv_pcifront_write_config()
1285 * hv_pcifront_read_config() - Read configuration space
1299 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_read_config()
1313 * hv_pcifront_write_config() - Write configuration space
1327 container_of(bus->sysdata, struct hv_pcibus_device, sysdata); in hv_pcifront_write_config()
1349 * Hyper-V SR-IOV provides a backchannel mechanism in software for
1352 * but instead of doing reads and writes in 32-bit chunks through a very slow
1355 * Nearly every SR-IOV device contains just such a communications channel in
1379 * hv_pci_read_config_compl() - Invoked when a response packet
1395 comp->comp_pkt.completion_status = -1; in hv_pci_read_config_compl()
1399 data_len = resp_packet_size - hdr_len; in hv_pci_read_config_compl()
1400 if (data_len > 0 && read_resp->status == 0) { in hv_pci_read_config_compl()
1401 comp->bytes_returned = min(comp->len, data_len); in hv_pci_read_config_compl()
1402 memcpy(comp->buf, read_resp->bytes, comp->bytes_returned); in hv_pci_read_config_compl()
1404 comp->bytes_returned = 0; in hv_pci_read_config_compl()
1407 comp->comp_pkt.completion_status = read_resp->status; in hv_pci_read_config_compl()
1409 complete(&comp->comp_pkt.host_event); in hv_pci_read_config_compl()
1413 * hv_read_config_block() - Sends a read config block request to
1414 * the back-end driver running in the Hyper-V parent partition.
1419 * @bytes_returned: Size which came back from the back-end driver.
1421 * Return: 0 on success, -errno on failure
1428 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_read_config_block()
1439 return -EINVAL; in hv_read_config_block()
1449 read_blk->message_type.type = PCI_READ_BLOCK; in hv_read_config_block()
1450 read_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_read_config_block()
1451 read_blk->block_id = block_id; in hv_read_config_block()
1452 read_blk->bytes_requested = len; in hv_read_config_block()
1454 ret = vmbus_sendpacket(hbus->hdev->channel, read_blk, in hv_read_config_block()
1461 ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event); in hv_read_config_block()
1467 dev_err(&hbus->hdev->device, in hv_read_config_block()
1471 return -EIO; in hv_read_config_block()
1479 * hv_pci_write_config_compl() - Invoked when a response packet for a write
1490 comp_pkt->completion_status = resp->status; in hv_pci_write_config_compl()
1491 complete(&comp_pkt->host_event); in hv_pci_write_config_compl()
1495 * hv_write_config_block() - Sends a write config block request to the
1496 * back-end driver running in the Hyper-V parent partition.
1502 * Return: 0 on success, -errno on failure
1508 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_write_config_block()
1521 return -EINVAL; in hv_write_config_block()
1529 write_blk->message_type.type = PCI_WRITE_BLOCK; in hv_write_config_block()
1530 write_blk->wslot.slot = devfn_to_wslot(pdev->devfn); in hv_write_config_block()
1531 write_blk->block_id = block_id; in hv_write_config_block()
1532 write_blk->byte_count = len; in hv_write_config_block()
1533 memcpy(write_blk->bytes, buf, len); in hv_write_config_block()
1540 * specified in write_blk->byte_count. in hv_write_config_block()
1544 ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size, in hv_write_config_block()
1550 ret = wait_for_response(hbus->hdev, &comp_pkt.host_event); in hv_write_config_block()
1555 dev_err(&hbus->hdev->device, in hv_write_config_block()
1558 return -EIO; in hv_write_config_block()
1565 * hv_register_block_invalidate() - Invoked when a config block invalidation
1566 * arrives from the back-end driver.
1571 * Return: 0 on success, -errno on failure
1578 container_of(pdev->bus->sysdata, struct hv_pcibus_device, in hv_register_block_invalidate()
1582 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_register_block_invalidate()
1584 return -ENODEV; in hv_register_block_invalidate()
1586 hpdev->block_invalidate = block_invalidate; in hv_register_block_invalidate()
1587 hpdev->invalidate_context = context; in hv_register_block_invalidate()
1604 if (!int_desc->vector_count) { in hv_int_desc_free()
1610 int_pkt->message_type.type = in hv_int_desc_free()
1612 int_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_int_desc_free()
1613 int_pkt->int_desc = *int_desc; in hv_int_desc_free()
1614 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt), in hv_int_desc_free()
1620 * hv_msi_free() - Free the MSI.
1622 * @info: Extra MSI-related context
1625 * The Hyper-V parent partition and hypervisor are tracking the
1641 hbus = info->data; in hv_msi_free()
1646 irq_data->chip_data = NULL; in hv_msi_free()
1647 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_msi_free()
1660 if (data->parent_data->chip->irq_mask) in hv_irq_mask()
1668 if (data->parent_data->chip->irq_unmask) in hv_irq_unmask()
1686 comp_pkt->comp_pkt.completion_status = -1; in hv_pci_compose_compl()
1689 comp_pkt->comp_pkt.completion_status = resp->status; in hv_pci_compose_compl()
1690 comp_pkt->int_desc = int_resp->int_desc; in hv_pci_compose_compl()
1692 complete(&comp_pkt->comp_pkt.host_event); in hv_pci_compose_compl()
1699 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE; in hv_compose_msi_req_v1()
1700 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v1()
1701 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v1()
1702 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v1()
1703 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v1()
1709 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL; in hv_compose_msi_req_v1()
1717 * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
1719 * not irrelevant because Hyper-V chooses the physical CPU to handle the
1720 * interrupts based on the vCPU specified in message sent to the vPCI VSP in
1721 * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
1723 * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
1726 * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
1728 * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
1729 * new pCPU for the interrupt. But for the multi-MSI case, the second call to
1731 * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
1732 * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
1736 * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
1737 * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
1739 * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
1753 * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
1759 /* -1 means starting with CPU 0 */ in hv_compose_multi_msi_req_get_cpu()
1760 static int cpu_next = -1; in hv_compose_multi_msi_req_get_cpu()
1780 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2; in hv_compose_msi_req_v2()
1781 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v2()
1782 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v2()
1783 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v2()
1784 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v2()
1785 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v2()
1787 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v2()
1796 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3; in hv_compose_msi_req_v3()
1797 int_pkt->wslot.slot = slot; in hv_compose_msi_req_v3()
1798 int_pkt->int_desc.vector = vector; in hv_compose_msi_req_v3()
1799 int_pkt->int_desc.reserved = 0; in hv_compose_msi_req_v3()
1800 int_pkt->int_desc.vector_count = vector_count; in hv_compose_msi_req_v3()
1801 int_pkt->int_desc.delivery_mode = DELIVERY_MODE; in hv_compose_msi_req_v3()
1802 int_pkt->int_desc.processor_array[0] = in hv_compose_msi_req_v3()
1804 int_pkt->int_desc.processor_count = 1; in hv_compose_msi_req_v3()
1810 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1852 multi_msi = !msi_desc->pci.msi_attrib.is_msix && in hv_compose_msi_msg()
1853 msi_desc->nvec_used > 1; in hv_compose_msi_msg()
1856 if (data->chip_data && multi_msi) { in hv_compose_msi_msg()
1857 int_desc = data->chip_data; in hv_compose_msi_msg()
1858 msg->address_hi = int_desc->address >> 32; in hv_compose_msi_msg()
1859 msg->address_lo = int_desc->address & 0xffffffff; in hv_compose_msi_msg()
1860 msg->data = int_desc->data; in hv_compose_msi_msg()
1866 pbus = pdev->bus; in hv_compose_msi_msg()
1867 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); in hv_compose_msi_msg()
1868 channel = hbus->hdev->channel; in hv_compose_msi_msg()
1869 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn)); in hv_compose_msi_msg()
1874 if (data->chip_data && !multi_msi) { in hv_compose_msi_msg()
1875 int_desc = data->chip_data; in hv_compose_msi_msg()
1876 data->chip_data = NULL; in hv_compose_msi_msg()
1889 if (msi_desc->irq != data->irq) { in hv_compose_msi_msg()
1890 data->chip_data = int_desc; in hv_compose_msi_msg()
1891 int_desc->address = msi_desc->msg.address_lo | in hv_compose_msi_msg()
1892 (u64)msi_desc->msg.address_hi << 32; in hv_compose_msi_msg()
1893 int_desc->data = msi_desc->msg.data + in hv_compose_msi_msg()
1894 (data->irq - msi_desc->irq); in hv_compose_msi_msg()
1895 msg->address_hi = msi_desc->msg.address_hi; in hv_compose_msi_msg()
1896 msg->address_lo = msi_desc->msg.address_lo; in hv_compose_msi_msg()
1897 msg->data = int_desc->data; in hv_compose_msi_msg()
1904 * to be aligned with the count, and also not zero. Multi-msi in hv_compose_msi_msg()
1908 vector_count = msi_desc->nvec_used; in hv_compose_msi_msg()
1926 switch (hbus->protocol_version) { in hv_compose_msi_msg()
1929 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1938 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1946 hpdev->desc.win_slot.slot, in hv_compose_msi_msg()
1956 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
1961 ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts, in hv_compose_msi_msg()
1966 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
1976 tasklet_disable_in_atomic(&channel->callback_event); in hv_compose_msi_msg()
1987 dev_err_once(&hbus->hdev->device, in hv_compose_msi_msg()
1999 spin_lock_irqsave(&channel->sched_lock, flags); in hv_compose_msi_msg()
2000 if (unlikely(channel->onchannel_callback == NULL)) { in hv_compose_msi_msg()
2001 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2005 spin_unlock_irqrestore(&channel->sched_lock, flags); in hv_compose_msi_msg()
2010 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2013 dev_err(&hbus->hdev->device, in hv_compose_msi_msg()
2025 data->chip_data = int_desc; in hv_compose_msi_msg()
2028 msg->address_hi = comp.int_desc.address >> 32; in hv_compose_msi_msg()
2029 msg->address_lo = comp.int_desc.address & 0xffffffff; in hv_compose_msi_msg()
2030 msg->data = comp.int_desc.data; in hv_compose_msi_msg()
2036 tasklet_enable(&channel->callback_event); in hv_compose_msi_msg()
2041 * been 're-used', i.e., already removed and (re-)mapped.) in hv_compose_msi_msg()
2051 msg->address_hi = 0; in hv_compose_msi_msg()
2052 msg->address_lo = 0; in hv_compose_msi_msg()
2053 msg->data = 0; in hv_compose_msi_msg()
2058 .name = "Hyper-V PCIe MSI",
2076 * hv_pcie_init_irq_domain() - Initialize IRQ domain
2081 * devices only support MSI and MSI-X, not line-based interrupts
2082 * or simulations of line-based interrupts through PCIe's
2083 * fabric-layer messages. Because interrupts are remapped, we
2084 * can support multi-message MSI here.
2090 hbus->msi_info.chip = &hv_msi_irq_chip; in hv_pcie_init_irq_domain()
2091 hbus->msi_info.ops = &hv_msi_ops; in hv_pcie_init_irq_domain()
2092 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS | in hv_pcie_init_irq_domain()
2095 hbus->msi_info.handler = FLOW_HANDLER; in hv_pcie_init_irq_domain()
2096 hbus->msi_info.handler_name = FLOW_NAME; in hv_pcie_init_irq_domain()
2097 hbus->msi_info.data = hbus; in hv_pcie_init_irq_domain()
2098 hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode, in hv_pcie_init_irq_domain()
2099 &hbus->msi_info, in hv_pcie_init_irq_domain()
2101 if (!hbus->irq_domain) { in hv_pcie_init_irq_domain()
2102 dev_err(&hbus->hdev->device, in hv_pcie_init_irq_domain()
2104 return -ENODEV; in hv_pcie_init_irq_domain()
2107 dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain); in hv_pcie_init_irq_domain()
2113 * get_bar_size() - Get the address space consumed by a BAR
2114 * @bar_val: Value that a BAR returned after -1 was written
2133 * survey_child_resources() - Total all MMIO requirements
2146 event = xchg(&hbus->survey_event, NULL); in survey_child_resources()
2151 if (hbus->low_mmio_space || hbus->high_mmio_space) { in survey_child_resources()
2156 spin_lock_irqsave(&hbus->device_list_lock, flags); in survey_child_resources()
2163 list_for_each_entry(hpdev, &hbus->children, list_entry) { in survey_child_resources()
2165 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO) in survey_child_resources()
2166 dev_err(&hbus->hdev->device, in survey_child_resources()
2169 if (hpdev->probed_bar[i] != 0) { in survey_child_resources()
2175 bar_val = hpdev->probed_bar[i]; in survey_child_resources()
2178 ((u64)hpdev->probed_bar[++i] << 32); in survey_child_resources()
2185 hbus->high_mmio_space += bar_size; in survey_child_resources()
2187 hbus->low_mmio_space += bar_size; in survey_child_resources()
2192 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in survey_child_resources()
2197 * prepopulate_bars() - Fill in BARs with defaults
2205 * the memory-mapped I/O space will be aligned on the largest
2222 if (hbus->low_mmio_space) { in prepopulate_bars()
2223 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in prepopulate_bars()
2224 low_base = hbus->low_mmio_res->start; in prepopulate_bars()
2227 if (hbus->high_mmio_space) { in prepopulate_bars()
2229 (63 - __builtin_clzll(hbus->high_mmio_space)); in prepopulate_bars()
2230 high_base = hbus->high_mmio_res->start; in prepopulate_bars()
2233 spin_lock_irqsave(&hbus->device_list_lock, flags); in prepopulate_bars()
2241 * If the memory enable bit is already set, Hyper-V silently ignores in prepopulate_bars()
2246 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2254 list_for_each_entry(hpdev, &hbus->children, list_entry) { in prepopulate_bars()
2256 bar_val = hpdev->probed_bar[i]; in prepopulate_bars()
2262 ((u64)hpdev->probed_bar[i + 1] in prepopulate_bars()
2296 * to be pre-set. Actually here we intentionally in prepopulate_bars()
2298 * in the core PCI driver doesn't cause Hyper-V in prepopulate_bars()
2312 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in prepopulate_bars()
2321 * same ordered workqueue. Therefore hbus->children list will not change
2330 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_assign_slots()
2331 if (hpdev->pci_slot) in hv_pci_assign_slots()
2334 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot)); in hv_pci_assign_slots()
2335 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser); in hv_pci_assign_slots()
2336 hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr, in hv_pci_assign_slots()
2338 if (IS_ERR(hpdev->pci_slot)) { in hv_pci_assign_slots()
2340 hpdev->pci_slot = NULL; in hv_pci_assign_slots()
2352 list_for_each_entry(hpdev, &hbus->children, list_entry) { in hv_pci_remove_slots()
2353 if (!hpdev->pci_slot) in hv_pci_remove_slots()
2355 pci_destroy_slot(hpdev->pci_slot); in hv_pci_remove_slots()
2356 hpdev->pci_slot = NULL; in hv_pci_remove_slots()
2366 struct pci_bus *bus = hbus->bridge->bus; in hv_pci_assign_numa_node()
2369 list_for_each_entry(dev, &bus->devices, bus_list) { in hv_pci_assign_numa_node()
2370 hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn)); in hv_pci_assign_numa_node()
2374 if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY && in hv_pci_assign_numa_node()
2375 hv_dev->desc.virtual_numa_node < num_possible_nodes()) in hv_pci_assign_numa_node()
2382 set_dev_node(&dev->dev, in hv_pci_assign_numa_node()
2384 hv_dev->desc.virtual_numa_node)); in hv_pci_assign_numa_node()
2391 * create_root_hv_pci_bus() - Expose a new root PCI bus
2394 * Return: 0 on success, -errno on failure
2399 struct pci_host_bridge *bridge = hbus->bridge; in create_root_hv_pci_bus()
2401 bridge->dev.parent = &hbus->hdev->device; in create_root_hv_pci_bus()
2402 bridge->sysdata = &hbus->sysdata; in create_root_hv_pci_bus()
2403 bridge->ops = &hv_pcifront_ops; in create_root_hv_pci_bus()
2411 pci_bus_assign_resources(bridge->bus); in create_root_hv_pci_bus()
2413 pci_bus_add_devices(bridge->bus); in create_root_hv_pci_bus()
2415 hbus->state = hv_pcibus_installed; in create_root_hv_pci_bus()
2425 * q_resource_requirements() - Query Resource Requirements
2442 status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status; in q_resource_requirements()
2444 dev_err(&completion->hpdev->hbus->hdev->device, in q_resource_requirements()
2449 completion->hpdev->probed_bar[i] = in q_resource_requirements()
2450 q_res_req->probed_bar[i]; in q_resource_requirements()
2454 complete(&completion->host_event); in q_resource_requirements()
2458 * new_pcichild_device() - Create a new child device
2485 hpdev->hbus = hbus; in new_pcichild_device()
2493 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS; in new_pcichild_device()
2494 res_req->wslot.slot = desc->win_slot.slot; in new_pcichild_device()
2496 ret = vmbus_sendpacket(hbus->hdev->channel, res_req, in new_pcichild_device()
2504 if (wait_for_response(hbus->hdev, &comp_pkt.host_event)) in new_pcichild_device()
2507 hpdev->desc = *desc; in new_pcichild_device()
2508 refcount_set(&hpdev->refs, 1); in new_pcichild_device()
2510 spin_lock_irqsave(&hbus->device_list_lock, flags); in new_pcichild_device()
2512 list_add_tail(&hpdev->list_entry, &hbus->children); in new_pcichild_device()
2513 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in new_pcichild_device()
2522 * get_pcichild_wslot() - Find device from slot
2540 spin_lock_irqsave(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2541 list_for_each_entry(iter, &hbus->children, list_entry) { in get_pcichild_wslot()
2542 if (iter->desc.win_slot.slot == wslot) { in get_pcichild_wslot()
2548 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in get_pcichild_wslot()
2554 * pci_devices_present_work() - Handle new list of child devices
2559 * debug the interaction between Hyper-V and Linux. This
2574 * because both are pushed to the ordered workqueue hbus->wq.
2589 hbus = dr_wrk->bus; in pci_devices_present_work()
2595 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2596 while (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2597 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state, in pci_devices_present_work()
2599 list_del(&dr->list_entry); in pci_devices_present_work()
2602 if (!list_empty(&hbus->dr_list)) { in pci_devices_present_work()
2607 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2612 mutex_lock(&hbus->state_lock); in pci_devices_present_work()
2615 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2616 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2617 hpdev->reported_missing = true; in pci_devices_present_work()
2619 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2622 for (child_no = 0; child_no < dr->device_count; child_no++) { in pci_devices_present_work()
2624 new_desc = &dr->func[child_no]; in pci_devices_present_work()
2626 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2627 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2628 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) && in pci_devices_present_work()
2629 (hpdev->desc.v_id == new_desc->v_id) && in pci_devices_present_work()
2630 (hpdev->desc.d_id == new_desc->d_id) && in pci_devices_present_work()
2631 (hpdev->desc.ser == new_desc->ser)) { in pci_devices_present_work()
2632 hpdev->reported_missing = false; in pci_devices_present_work()
2636 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2641 dev_err(&hbus->hdev->device, in pci_devices_present_work()
2647 spin_lock_irqsave(&hbus->device_list_lock, flags); in pci_devices_present_work()
2650 list_for_each_entry(hpdev, &hbus->children, list_entry) { in pci_devices_present_work()
2651 if (hpdev->reported_missing) { in pci_devices_present_work()
2654 list_move_tail(&hpdev->list_entry, &removed); in pci_devices_present_work()
2659 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in pci_devices_present_work()
2665 list_del(&hpdev->list_entry); in pci_devices_present_work()
2667 if (hpdev->pci_slot) in pci_devices_present_work()
2668 pci_destroy_slot(hpdev->pci_slot); in pci_devices_present_work()
2673 switch (hbus->state) { in pci_devices_present_work()
2680 pci_scan_child_bus(hbus->bridge->bus); in pci_devices_present_work()
2695 mutex_unlock(&hbus->state_lock); in pci_devices_present_work()
2701 * hv_pci_start_relations_work() - Queue work to start device discovery
2705 * Return: 0 on success, -errno on failure
2714 if (hbus->state == hv_pcibus_removing) { in hv_pci_start_relations_work()
2715 dev_info(&hbus->hdev->device, in hv_pci_start_relations_work()
2717 return -ENOENT; in hv_pci_start_relations_work()
2722 return -ENOMEM; in hv_pci_start_relations_work()
2724 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work); in hv_pci_start_relations_work()
2725 dr_wrk->bus = hbus; in hv_pci_start_relations_work()
2727 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2733 pending_dr = !list_empty(&hbus->dr_list); in hv_pci_start_relations_work()
2734 list_add_tail(&dr->list_entry, &hbus->dr_list); in hv_pci_start_relations_work()
2735 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_start_relations_work()
2740 queue_work(hbus->wq, &dr_wrk->wrk); in hv_pci_start_relations_work()
2746 * hv_pci_devices_present() - Handle list of new children
2760 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present()
2765 dr->device_count = relations->device_count; in hv_pci_devices_present()
2766 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present()
2767 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present()
2768 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present()
2769 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present()
2770 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present()
2771 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present()
2772 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present()
2773 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present()
2774 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present()
2775 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present()
2783 * hv_pci_devices_present2() - Handle list of new children
2795 dr = kzalloc(struct_size(dr, func, relations->device_count), in hv_pci_devices_present2()
2800 dr->device_count = relations->device_count; in hv_pci_devices_present2()
2801 for (i = 0; i < dr->device_count; i++) { in hv_pci_devices_present2()
2802 dr->func[i].v_id = relations->func[i].v_id; in hv_pci_devices_present2()
2803 dr->func[i].d_id = relations->func[i].d_id; in hv_pci_devices_present2()
2804 dr->func[i].rev = relations->func[i].rev; in hv_pci_devices_present2()
2805 dr->func[i].prog_intf = relations->func[i].prog_intf; in hv_pci_devices_present2()
2806 dr->func[i].subclass = relations->func[i].subclass; in hv_pci_devices_present2()
2807 dr->func[i].base_class = relations->func[i].base_class; in hv_pci_devices_present2()
2808 dr->func[i].subsystem_id = relations->func[i].subsystem_id; in hv_pci_devices_present2()
2809 dr->func[i].win_slot = relations->func[i].win_slot; in hv_pci_devices_present2()
2810 dr->func[i].ser = relations->func[i].ser; in hv_pci_devices_present2()
2811 dr->func[i].flags = relations->func[i].flags; in hv_pci_devices_present2()
2812 dr->func[i].virtual_numa_node = in hv_pci_devices_present2()
2813 relations->func[i].virtual_numa_node; in hv_pci_devices_present2()
2821 * hv_eject_device_work() - Asynchronously handles ejection
2843 hbus = hpdev->hbus; in hv_eject_device_work()
2845 mutex_lock(&hbus->state_lock); in hv_eject_device_work()
2850 * must be done without constructs like pci_domain_nr(hbus->bridge->bus) in hv_eject_device_work()
2851 * because hbus->bridge->bus may not exist yet. in hv_eject_device_work()
2853 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot); in hv_eject_device_work()
2854 pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot); in hv_eject_device_work()
2862 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_eject_device_work()
2863 list_del(&hpdev->list_entry); in hv_eject_device_work()
2864 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_eject_device_work()
2866 if (hpdev->pci_slot) in hv_eject_device_work()
2867 pci_destroy_slot(hpdev->pci_slot); in hv_eject_device_work()
2871 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE; in hv_eject_device_work()
2872 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot; in hv_eject_device_work()
2873 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt, in hv_eject_device_work()
2884 mutex_unlock(&hbus->state_lock); in hv_eject_device_work()
2888 * hv_pci_eject_device() - Handles device ejection
2892 * just schedules work so that we don't re-enter the packet
2897 struct hv_pcibus_device *hbus = hpdev->hbus; in hv_pci_eject_device()
2898 struct hv_device *hdev = hbus->hdev; in hv_pci_eject_device()
2900 if (hbus->state == hv_pcibus_removing) { in hv_pci_eject_device()
2901 dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n"); in hv_pci_eject_device()
2906 INIT_WORK(&hpdev->wrk, hv_eject_device_work); in hv_pci_eject_device()
2907 queue_work(hbus->wq, &hpdev->wrk); in hv_pci_eject_device()
2911 * hv_pci_onchannelcallback() - Handles incoming packets
2922 struct vmbus_channel *chan = hbus->hdev->channel; in hv_pci_onchannelcallback()
2946 if (ret == -ENOBUFS) { in hv_pci_onchannelcallback()
2968 switch (desc->type) { in hv_pci_onchannelcallback()
2976 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
2984 * Call ->completion_func() within the critical section to make in hv_pci_onchannelcallback()
2990 comp_packet->completion_func(comp_packet->compl_ctxt, in hv_pci_onchannelcallback()
2999 switch (new_message->message_type.type) { in hv_pci_onchannelcallback()
3006 bus_rel->device_count)) { in hv_pci_onchannelcallback()
3007 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3021 bus_rel2->device_count)) { in hv_pci_onchannelcallback()
3022 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3034 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3039 dev_message->wslot.slot); in hv_pci_onchannelcallback()
3050 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3055 inval->wslot.slot); in hv_pci_onchannelcallback()
3057 if (hpdev->block_invalidate) { in hv_pci_onchannelcallback()
3058 hpdev->block_invalidate( in hv_pci_onchannelcallback()
3059 hpdev->invalidate_context, in hv_pci_onchannelcallback()
3060 inval->block_mask); in hv_pci_onchannelcallback()
3067 dev_warn(&hbus->hdev->device, in hv_pci_onchannelcallback()
3069 new_message->message_type.type); in hv_pci_onchannelcallback()
3075 dev_err(&hbus->hdev->device, in hv_pci_onchannelcallback()
3077 desc->type, req_id, bytes_recvd); in hv_pci_onchannelcallback()
3086 * hv_pci_protocol_negotiation() - Set up protocol
3089 * the order of probing - highest go first.
3123 return -ENOMEM; in hv_pci_protocol_negotiation()
3126 pkt->completion_func = hv_pci_generic_compl; in hv_pci_protocol_negotiation()
3127 pkt->compl_ctxt = &comp_pkt; in hv_pci_protocol_negotiation()
3128 version_req = (struct pci_version_request *)&pkt->message; in hv_pci_protocol_negotiation()
3129 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION; in hv_pci_protocol_negotiation()
3132 version_req->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3133 ret = vmbus_sendpacket(hdev->channel, version_req, in hv_pci_protocol_negotiation()
3141 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3142 "PCI Pass-through VSP failed to request version: %d", in hv_pci_protocol_negotiation()
3148 hbus->protocol_version = version[i]; in hv_pci_protocol_negotiation()
3149 dev_info(&hdev->device, in hv_pci_protocol_negotiation()
3151 hbus->protocol_version); in hv_pci_protocol_negotiation()
3156 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3157 "PCI Pass-through VSP failed version request: %#x", in hv_pci_protocol_negotiation()
3159 ret = -EPROTO; in hv_pci_protocol_negotiation()
3166 dev_err(&hdev->device, in hv_pci_protocol_negotiation()
3167 "PCI pass-through VSP failed to find supported version"); in hv_pci_protocol_negotiation()
3168 ret = -EPROTO; in hv_pci_protocol_negotiation()
3176 * hv_pci_free_bridge_windows() - Release memory regions for the
3187 if (hbus->low_mmio_space && hbus->low_mmio_res) { in hv_pci_free_bridge_windows()
3188 hbus->low_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3189 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_free_bridge_windows()
3190 resource_size(hbus->low_mmio_res)); in hv_pci_free_bridge_windows()
3193 if (hbus->high_mmio_space && hbus->high_mmio_res) { in hv_pci_free_bridge_windows()
3194 hbus->high_mmio_res->flags |= IORESOURCE_BUSY; in hv_pci_free_bridge_windows()
3195 vmbus_free_mmio(hbus->high_mmio_res->start, in hv_pci_free_bridge_windows()
3196 resource_size(hbus->high_mmio_res)); in hv_pci_free_bridge_windows()
3201 * hv_pci_allocate_bridge_windows() - Allocate memory regions
3223 * Return: 0 on success, -errno on failure
3230 if (hbus->low_mmio_space) { in hv_pci_allocate_bridge_windows()
3231 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space)); in hv_pci_allocate_bridge_windows()
3232 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0, in hv_pci_allocate_bridge_windows()
3234 hbus->low_mmio_space, in hv_pci_allocate_bridge_windows()
3237 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3239 hbus->low_mmio_space); in hv_pci_allocate_bridge_windows()
3244 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3245 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3246 pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res); in hv_pci_allocate_bridge_windows()
3249 if (hbus->high_mmio_space) { in hv_pci_allocate_bridge_windows()
3250 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space)); in hv_pci_allocate_bridge_windows()
3251 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev, in hv_pci_allocate_bridge_windows()
3252 0x100000000, -1, in hv_pci_allocate_bridge_windows()
3253 hbus->high_mmio_space, align, in hv_pci_allocate_bridge_windows()
3256 dev_err(&hbus->hdev->device, in hv_pci_allocate_bridge_windows()
3258 hbus->high_mmio_space); in hv_pci_allocate_bridge_windows()
3263 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW; in hv_pci_allocate_bridge_windows()
3264 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY; in hv_pci_allocate_bridge_windows()
3265 pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res); in hv_pci_allocate_bridge_windows()
3271 if (hbus->low_mmio_res) { in hv_pci_allocate_bridge_windows()
3272 vmbus_free_mmio(hbus->low_mmio_res->start, in hv_pci_allocate_bridge_windows()
3273 resource_size(hbus->low_mmio_res)); in hv_pci_allocate_bridge_windows()
3280 * hv_allocate_config_window() - Find MMIO space for PCI Config
3283 * This function claims memory-mapped I/O space for accessing
3286 * Return: 0 on success, -errno on failure
3296 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1, in hv_allocate_config_window()
3309 hbus->mem_config->flags |= IORESOURCE_BUSY; in hv_allocate_config_window()
3316 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH); in hv_free_config_window()
3322 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
3325 * Return: 0 on success, -errno on failure
3339 * powered-on state. This includes telling the host which region in hv_pci_enter_d0()
3340 * of memory-mapped I/O space has been chosen for configuration space in hv_pci_enter_d0()
3345 return -ENOMEM; in hv_pci_enter_d0()
3348 pkt->completion_func = hv_pci_generic_compl; in hv_pci_enter_d0()
3349 pkt->compl_ctxt = &comp_pkt; in hv_pci_enter_d0()
3350 d0_entry = (struct pci_bus_d0_entry *)&pkt->message; in hv_pci_enter_d0()
3351 d0_entry->message_type.type = PCI_BUS_D0ENTRY; in hv_pci_enter_d0()
3352 d0_entry->mmio_base = hbus->mem_config->start; in hv_pci_enter_d0()
3354 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry), in hv_pci_enter_d0()
3373 dev_err(&hdev->device, "Retrying D0 Entry\n"); in hv_pci_enter_d0()
3383 hbus->wslot_res_allocated = 255; in hv_pci_enter_d0()
3391 dev_err(&hdev->device, in hv_pci_enter_d0()
3396 dev_err(&hdev->device, in hv_pci_enter_d0()
3397 "PCI Pass-through VSP failed D0 Entry with status %x\n", in hv_pci_enter_d0()
3399 ret = -EPROTO; in hv_pci_enter_d0()
3411 * hv_pci_query_relations() - Ask host to send list of child
3415 * Return: 0 on success, -errno on failure
3426 if (cmpxchg(&hbus->survey_event, NULL, &comp)) in hv_pci_query_relations()
3427 return -ENOTEMPTY; in hv_pci_query_relations()
3432 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message), in hv_pci_query_relations()
3439 * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we in hv_pci_query_relations()
3441 * channel callback already scheduled a work to hbus->wq, which can be in hv_pci_query_relations()
3442 * running pci_devices_present_work() -> survey_child_resources() -> in hv_pci_query_relations()
3443 * complete(&hbus->survey_event), even after hv_pci_query_relations() in hv_pci_query_relations()
3446 * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from in hv_pci_query_relations()
3448 * -ENODEV, there can't be any more work item scheduled to hbus->wq in hv_pci_query_relations()
3449 * after the flush_workqueue(): see vmbus_onoffer_rescind() -> in hv_pci_query_relations()
3450 * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() -> in hv_pci_query_relations()
3451 * channel->rescind = true. in hv_pci_query_relations()
3453 flush_workqueue(hbus->wq); in hv_pci_query_relations()
3459 * hv_send_resources_allocated() - Report local resource choices
3473 * Return: 0 on success, -errno on failure
3487 size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) in hv_send_resources_allocated()
3492 return -ENOMEM; in hv_send_resources_allocated()
3503 pkt->completion_func = hv_pci_generic_compl; in hv_send_resources_allocated()
3504 pkt->compl_ctxt = &comp_pkt; in hv_send_resources_allocated()
3506 if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) { in hv_send_resources_allocated()
3508 (struct pci_resources_assigned *)&pkt->message; in hv_send_resources_allocated()
3509 res_assigned->message_type.type = in hv_send_resources_allocated()
3511 res_assigned->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3514 (struct pci_resources_assigned2 *)&pkt->message; in hv_send_resources_allocated()
3515 res_assigned2->message_type.type = in hv_send_resources_allocated()
3517 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_allocated()
3521 ret = vmbus_sendpacket(hdev->channel, &pkt->message, in hv_send_resources_allocated()
3531 ret = -EPROTO; in hv_send_resources_allocated()
3532 dev_err(&hdev->device, in hv_send_resources_allocated()
3538 hbus->wslot_res_allocated = wslot; in hv_send_resources_allocated()
3546 * hv_send_resources_released() - Report local resources
3550 * Return: 0 on success, -errno on failure
3560 for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) { in hv_send_resources_released()
3567 pkt.wslot.slot = hpdev->desc.win_slot.slot; in hv_send_resources_released()
3571 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0, in hv_send_resources_released()
3576 hbus->wslot_res_allocated = wslot - 1; in hv_send_resources_released()
3579 hbus->wslot_res_allocated = -1; in hv_send_resources_released()
3594 * hv_get_dom_num() - Get a valid PCI domain number
3618 * hv_put_dom_num() - Mark the PCI domain number as free
3627 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
3631 * Return: 0 on success, -errno on failure
3642 bridge = devm_pci_alloc_host_bridge(&hdev->device, 0); in hv_pci_probe()
3644 return -ENOMEM; in hv_pci_probe()
3648 return -ENOMEM; in hv_pci_probe()
3650 hbus->bridge = bridge; in hv_pci_probe()
3651 mutex_init(&hbus->state_lock); in hv_pci_probe()
3652 hbus->state = hv_pcibus_init; in hv_pci_probe()
3653 hbus->wslot_res_allocated = -1; in hv_pci_probe()
3661 * Note that, since this code only runs in a Hyper-V VM, Hyper-V in hv_pci_probe()
3668 dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4]; in hv_pci_probe()
3672 dev_err(&hdev->device, in hv_pci_probe()
3674 ret = -EINVAL; in hv_pci_probe()
3679 dev_info(&hdev->device, in hv_pci_probe()
3683 hbus->bridge->domain_nr = dom; in hv_pci_probe()
3685 hbus->sysdata.domain = dom; in hv_pci_probe()
3686 hbus->use_calls = !!(ms_hyperv.hints & HV_X64_USE_MMIO_HYPERCALLS); in hv_pci_probe()
3695 hbus->sysdata.parent = hdev->device.parent; in hv_pci_probe()
3696 hbus->use_calls = false; in hv_pci_probe()
3699 hbus->hdev = hdev; in hv_pci_probe()
3700 INIT_LIST_HEAD(&hbus->children); in hv_pci_probe()
3701 INIT_LIST_HEAD(&hbus->dr_list); in hv_pci_probe()
3702 spin_lock_init(&hbus->config_lock); in hv_pci_probe()
3703 spin_lock_init(&hbus->device_list_lock); in hv_pci_probe()
3704 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0, in hv_pci_probe()
3705 hbus->bridge->domain_nr); in hv_pci_probe()
3706 if (!hbus->wq) { in hv_pci_probe()
3707 ret = -ENOMEM; in hv_pci_probe()
3711 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_probe()
3712 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_probe()
3713 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_probe()
3715 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_probe()
3731 hbus->cfg_addr = ioremap(hbus->mem_config->start, in hv_pci_probe()
3733 if (!hbus->cfg_addr) { in hv_pci_probe()
3734 dev_err(&hdev->device, in hv_pci_probe()
3736 ret = -ENOMEM; in hv_pci_probe()
3740 name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance); in hv_pci_probe()
3742 ret = -ENOMEM; in hv_pci_probe()
3746 hbus->fwnode = irq_domain_alloc_named_fwnode(name); in hv_pci_probe()
3748 if (!hbus->fwnode) { in hv_pci_probe()
3749 ret = -ENOMEM; in hv_pci_probe()
3761 mutex_lock(&hbus->state_lock); in hv_pci_probe()
3777 hbus->state = hv_pcibus_probed; in hv_pci_probe()
3783 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3791 mutex_unlock(&hbus->state_lock); in hv_pci_probe()
3793 irq_domain_remove(hbus->irq_domain); in hv_pci_probe()
3795 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_probe()
3797 iounmap(hbus->cfg_addr); in hv_pci_probe()
3801 vmbus_close(hdev->channel); in hv_pci_probe()
3803 destroy_workqueue(hbus->wq); in hv_pci_probe()
3805 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_probe()
3814 struct vmbus_channel *chan = hdev->channel; in hv_pci_bus_exit()
3827 * access the per-channel ringbuffer any longer. in hv_pci_bus_exit()
3829 if (chan->rescind) in hv_pci_bus_exit()
3837 spin_lock_irqsave(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3838 list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) in hv_pci_bus_exit()
3839 list_move_tail(&hpdev->list_entry, &removed); in hv_pci_bus_exit()
3840 spin_unlock_irqrestore(&hbus->device_list_lock, flags); in hv_pci_bus_exit()
3844 list_del(&hpdev->list_entry); in hv_pci_bus_exit()
3845 if (hpdev->pci_slot) in hv_pci_bus_exit()
3846 pci_destroy_slot(hpdev->pci_slot); in hv_pci_bus_exit()
3855 dev_err(&hdev->device, in hv_pci_bus_exit()
3884 return -ETIMEDOUT; in hv_pci_bus_exit()
3891 * hv_pci_remove() - Remove routine for this VMBus channel
3899 if (hbus->state == hv_pcibus_installed) { in hv_pci_remove()
3900 tasklet_disable(&hdev->channel->callback_event); in hv_pci_remove()
3901 hbus->state = hv_pcibus_removing; in hv_pci_remove()
3902 tasklet_enable(&hdev->channel->callback_event); in hv_pci_remove()
3903 destroy_workqueue(hbus->wq); in hv_pci_remove()
3904 hbus->wq = NULL; in hv_pci_remove()
3907 * on hbus-wq. We can't race with hv_pci_devices_present() in hv_pci_remove()
3913 pci_stop_root_bus(hbus->bridge->bus); in hv_pci_remove()
3915 pci_remove_root_bus(hbus->bridge->bus); in hv_pci_remove()
3921 vmbus_close(hdev->channel); in hv_pci_remove()
3923 iounmap(hbus->cfg_addr); in hv_pci_remove()
3926 irq_domain_remove(hbus->irq_domain); in hv_pci_remove()
3927 irq_domain_free_fwnode(hbus->fwnode); in hv_pci_remove()
3929 hv_put_dom_num(hbus->bridge->domain_nr); in hv_pci_remove()
3946 * items onto hbus->wq in hv_pci_devices_present() and in hv_pci_suspend()
3949 * the work item handler pci_devices_present_work() -> in hv_pci_suspend()
3953 * callback tasklet, sets hbus->state to hv_pcibus_removing, and in hv_pci_suspend()
3954 * re-enables the tasklet. This way, when hv_pci_suspend() proceeds, in hv_pci_suspend()
3956 * hbus->wq and safely closes the vmbus channel. in hv_pci_suspend()
3958 tasklet_disable(&hdev->channel->callback_event); in hv_pci_suspend()
3961 old_state = hbus->state; in hv_pci_suspend()
3962 if (hbus->state == hv_pcibus_installed) in hv_pci_suspend()
3963 hbus->state = hv_pcibus_removing; in hv_pci_suspend()
3965 tasklet_enable(&hdev->channel->callback_event); in hv_pci_suspend()
3968 return -EINVAL; in hv_pci_suspend()
3970 flush_workqueue(hbus->wq); in hv_pci_suspend()
3976 vmbus_close(hdev->channel); in hv_pci_suspend()
3987 if (!pdev->msi_enabled && !pdev->msix_enabled) in hv_pci_restore_msi_msg()
3990 msi_lock_descs(&pdev->dev); in hv_pci_restore_msi_msg()
3991 msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { in hv_pci_restore_msi_msg()
3992 irq_data = irq_get_irq_data(entry->irq); in hv_pci_restore_msi_msg()
3994 ret = -EINVAL; in hv_pci_restore_msi_msg()
3998 hv_compose_msi_msg(irq_data, &entry->msg); in hv_pci_restore_msi_msg()
4000 msi_unlock_descs(&pdev->dev); in hv_pci_restore_msi_msg()
4006 * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
4007 * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
4009 * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
4014 pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL); in hv_pci_restore_msi_state()
4023 hbus->state = hv_pcibus_init; in hv_pci_resume()
4025 hdev->channel->next_request_id_callback = vmbus_next_request_id; in hv_pci_resume()
4026 hdev->channel->request_addr_callback = vmbus_request_addr; in hv_pci_resume()
4027 hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE; in hv_pci_resume()
4029 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, in hv_pci_resume()
4035 version[0] = hbus->protocol_version; in hv_pci_resume()
4044 mutex_lock(&hbus->state_lock); in hv_pci_resume()
4058 hbus->state = hv_pcibus_installed; in hv_pci_resume()
4059 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4063 mutex_unlock(&hbus->state_lock); in hv_pci_resume()
4065 vmbus_close(hdev->channel); in hv_pci_resume()
4070 /* PCI Pass-through Class ID */
4071 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
4101 return -ENODEV; in init_hv_pci_drv()
4121 MODULE_DESCRIPTION("Hyper-V PCI");