Bjorn Helgaas
2020-Sep-24 15:22 UTC
[PATCH v3 5/6] iommu/virtio: Support topology description in config space
On Fri, Aug 21, 2020 at 03:15:39PM +0200, Jean-Philippe Brucker wrote:> Platforms without device-tree nor ACPI can provide a topology > description embedded into the virtio config space. Parse it. > > Use PCI FIXUP to probe the config space early, because we need to > discover the topology before any DMA configuration takes place, and the > virtio driver may be loaded much later. Since we discover the topology > description when probing the PCI hierarchy, the virtual IOMMU cannot > manage other platform devices discovered earlier.> +struct viommu_cap_config { > + u8 bar; > + u32 length; /* structure size */ > + u32 offset; /* structure offset within the bar */s/the bar/the BAR/ (to match comment below).> +static void viommu_pci_parse_topology(struct pci_dev *dev) > +{ > + int ret; > + u32 features; > + void __iomem *regs, *common_regs; > + struct viommu_cap_config cap = {0}; > + struct virtio_pci_common_cfg __iomem *common_cfg; > + > + /* > + * The virtio infrastructure might not be loaded at this point. We need > + * to access the BARs ourselves. > + */ > + ret = viommu_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG, &cap); > + if (!ret) { > + pci_warn(dev, "common capability not found\n");Is the lack of this capability really an error, i.e., is this pci_warn() or pci_info()? The "device doesn't have topology description" below is only pci_dbg(), which suggests that we can live without this. Maybe a hint about what "common capability" means?> + return; > + } > + > + if (pci_enable_device_mem(dev)) > + return; > + > + common_regs = pci_iomap(dev, cap.bar, 0); > + if (!common_regs) > + return; > + > + common_cfg = common_regs + cap.offset; > + > + /* Perform the init sequence before we can read the config */ > + ret = viommu_pci_reset(common_cfg);I guess this is some special device-specific reset, not any kind of standard PCI reset?> + if (ret < 0) { > + pci_warn(dev, "unable to reset device\n"); > + goto out_unmap_common; > + } > + > + iowrite8(VIRTIO_CONFIG_S_ACKNOWLEDGE, &common_cfg->device_status); > + iowrite8(VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER, > + &common_cfg->device_status); > + > + /* Find out if the device supports topology description */ > + iowrite32(0, &common_cfg->device_feature_select); > + features = ioread32(&common_cfg->device_feature); > + > + if (!(features & BIT(VIRTIO_IOMMU_F_TOPOLOGY))) { > + pci_dbg(dev, "device doesn't have topology description"); > + goto out_reset; > + } > + > + ret = viommu_pci_find_capability(dev, VIRTIO_PCI_CAP_DEVICE_CFG, &cap); > + if (!ret) { > + pci_warn(dev, "device config capability not found\n"); > + goto out_reset; > + } > + > + regs = pci_iomap(dev, cap.bar, 0); > + if (!regs) > + goto out_reset; > + > + pci_info(dev, "parsing virtio-iommu topology\n"); > + ret = viommu_parse_topology(&dev->dev, regs + cap.offset, > + pci_resource_len(dev, 0) - cap.offset); > + if (ret) > + pci_warn(dev, "failed to parse topology: %d\n", ret); > + > + pci_iounmap(dev, regs); > +out_reset: > + ret = viommu_pci_reset(common_cfg); > + if (ret) > + pci_warn(dev, "unable to reset device\n"); > +out_unmap_common: > + pci_iounmap(dev, common_regs); > +} > + > +/* > + * Catch a PCI virtio-iommu implementation early to get the topology description > + * before we start probing other endpoints. > + */ > +DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_REDHAT_QUMRANET, 0x1040 + VIRTIO_ID_IOMMU, > + viommu_pci_parse_topology); > -- > 2.28.0 >
Jean-Philippe Brucker
2020-Sep-25 08:12 UTC
[PATCH v3 5/6] iommu/virtio: Support topology description in config space
On Thu, Sep 24, 2020 at 10:22:03AM -0500, Bjorn Helgaas wrote:> On Fri, Aug 21, 2020 at 03:15:39PM +0200, Jean-Philippe Brucker wrote: > > Platforms without device-tree nor ACPI can provide a topology > > description embedded into the virtio config space. Parse it. > > > > Use PCI FIXUP to probe the config space early, because we need to > > discover the topology before any DMA configuration takes place, and the > > virtio driver may be loaded much later. Since we discover the topology > > description when probing the PCI hierarchy, the virtual IOMMU cannot > > manage other platform devices discovered earlier. > > > +struct viommu_cap_config { > > + u8 bar; > > + u32 length; /* structure size */ > > + u32 offset; /* structure offset within the bar */ > > s/the bar/the BAR/ (to match comment below). > > > +static void viommu_pci_parse_topology(struct pci_dev *dev) > > +{ > > + int ret; > > + u32 features; > > + void __iomem *regs, *common_regs; > > + struct viommu_cap_config cap = {0}; > > + struct virtio_pci_common_cfg __iomem *common_cfg; > > + > > + /* > > + * The virtio infrastructure might not be loaded at this point. We need > > + * to access the BARs ourselves. > > + */ > > + ret = viommu_pci_find_capability(dev, VIRTIO_PCI_CAP_COMMON_CFG, &cap); > > + if (!ret) { > > + pci_warn(dev, "common capability not found\n"); > > Is the lack of this capability really an error, i.e., is this > pci_warn() or pci_info()? The "device doesn't have topology > description" below is only pci_dbg(), which suggests that we can live > without this.At this point we know that this is a (modern) virtio-pci device which, according to the virtio 1.0 specification, must have this capability. So this is definitely an error, but the topology description is an optional feature.> > Maybe a hint about what "common capability" means?Yes, "virtio-pci common configuration capability" would be more appropriate> > > + return; > > + } > > + > > + if (pci_enable_device_mem(dev)) > > + return; > > + > > + common_regs = pci_iomap(dev, cap.bar, 0); > > + if (!common_regs) > > + return; > > + > > + common_cfg = common_regs + cap.offset; > > + > > + /* Perform the init sequence before we can read the config */ > > + ret = viommu_pci_reset(common_cfg); > > I guess this is some special device-specific reset, not any kind of > standard PCI reset?Yes it's the virtio reset - writing 0 to the status register in the BAR. Thanks, Jean
Bjorn Helgaas
2020-Sep-25 15:34 UTC
[PATCH v3 5/6] iommu/virtio: Support topology description in config space
On Fri, Sep 25, 2020 at 10:12:43AM +0200, Jean-Philippe Brucker wrote:> On Thu, Sep 24, 2020 at 10:22:03AM -0500, Bjorn Helgaas wrote: > > On Fri, Aug 21, 2020 at 03:15:39PM +0200, Jean-Philippe Brucker wrote:> > > + /* Perform the init sequence before we can read the config */ > > > + ret = viommu_pci_reset(common_cfg); > > > > I guess this is some special device-specific reset, not any kind of > > standard PCI reset? > > Yes it's the virtio reset - writing 0 to the status register in the BAR.I wonder if this should be named something like viommu_virtio_reset(), so there's no confusion with PCI resets and all the timing restrictions, config space restoration, etc. associated with them. Bjorn
Apparently Analagous Threads
- [PATCH v3 5/6] iommu/virtio: Support topology description in config space
- [PATCH v3 5/6] iommu/virtio: Support topology description in config space
- [PATCH v3 5/6] iommu/virtio: Support topology description in config space
- [PATCH v3 5/6] iommu/virtio: Support topology description in config space
- [PATCH v2 1/3] iommu/virtio: Add topology description to virtio-iommu config space