IOMMU Groups + VFIO
Subsystems (7)
cover IOMMU Groups + VFIO
This is the latest incantation of IOMMU Groups and matching VFIO code. After some discussions with BenH and David Gibson I think we have some agreement on moving forward with the IOMMU Group approach, which covers the first 6 patches in this series. The basic idea of this is that IOMMU groups need to be a more integrated part of the driver model. The current iommu_device_group interface requires the user to do all all the heavy lifting of finding devices reporting the same ID and putting them together into something useful. If an esoteric driver like VFIO is the only user of the groups, this works nicely, but if we want to use groups for common DMA paths, it's too much overhead. We therefore add an iommu_group pointer off the device so we can easily get to the group for fast paths like streaming DMA (GregKH, does this work for you?).
I include code to support IOMMU group on both amd_iommu and intel-iommu. These take into account device quirks, like the Ricoh device that David Woodhouse has noted on a few occasions. I'll leave it to the iommu owners to make use of these.
For VFIO itself, the core has had nearly a complete rewrite to support the IOMMU group interface and also to switch to a new model for allowing IOMMU domains to share context (merge is gone). To allow the most flexibility (and also to avoid ratholing on the exposed IOMMU interface), the VFIO IOMMU backend is completely modular, allowing the user to probe and initiate a specific backend for a group. I've poorly chosen the name "x86" for the current IOMMU backend, simply for lack of a good way to describe a non-window based, page table driven IOMMU implementation.
These patches can be found in git here:
git://github.com/awilliam/linux-vfio.git (iommu-group-vfio-20120511)
The matching qemu tree supporting VFIO device assignment is here:
git://github.com/awilliam/qemu-vfio.git (iommu-group-vfio)
Sorry, this is based on Qemu-1.0. Updating to 1.1 is next on my todo list.
I'd really like to move forward with this, so please provide comments and feedback. This touches iommu, x86 iommu drivers, pci, device, and adds a new driver, so I appreciate any kind of ack (or nak I suppose) to know if we've got end-to-end support. Thanks!
Alex
---
Alex Williamson (13):
vfio: Add PCI device driver
pci: Misc pci_reg additions
pci: Create common pcibios_err_to_errno
pci: export pci_user functions for use by other drivers
vfio: x86 IOMMU implementation
vfio: Add documentation
vfio: VFIO core
iommu: Make use of DMA quirking and ACS enabled check for groups
pci: New pci_acs_enabled()
pci: New pci_dma_quirk()
iommu: IOMMU groups for VT-d and AMD-Vi
iommu: IOMMU Groups
driver core: Add iommu_group tracking to struct device
Documentation/ioctl/ioctl-number.txt | 1 Documentation/vfio.txt | 315 +++++++ MAINTAINERS | 8 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/iommu/amd_iommu.c | 52 + drivers/iommu/intel-iommu.c | 72 +- drivers/iommu/iommu.c | 449 +++++++++- drivers/pci/access.c | 6 drivers/pci/pci.c | 43 + drivers/pci/pci.h | 8 drivers/pci/quirks.c | 22 drivers/vfio/Kconfig | 16 drivers/vfio/Makefile | 3 drivers/vfio/pci/Kconfig | 8 drivers/vfio/pci/Makefile | 4 drivers/vfio/pci/vfio_pci.c | 557 ++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 1527 ++++++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_intrs.c | 724 ++++++++++++++++ drivers/vfio/pci/vfio_pci_private.h | 91 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 267 ++++++ drivers/vfio/vfio.c | 1406 +++++++++++++++++++++++++++++++ drivers/vfio/vfio_iommu_x86.c | 743 +++++++++++++++++ drivers/xen/xen-pciback/conf_space.c | 6 include/linux/device.h | 2 include/linux/iommu.h | 84 ++ include/linux/pci.h | 37 + include/linux/pci_regs.h | 112 ++ include/linux/vfio.h | 442 ++++++++++ 29 files changed, 6892 insertions(+), 116 deletions(-) create mode 100644 Documentation/vfio.txt create mode 100644 drivers/vfio/Kconfig create mode 100644 drivers/vfio/Makefile create mode 100644 drivers/vfio/pci/Kconfig create mode 100644 drivers/vfio/pci/Makefile create mode 100644 drivers/vfio/pci/vfio_pci.c create mode 100644 drivers/vfio/pci/vfio_pci_config.c create mode 100644 drivers/vfio/pci/vfio_pci_intrs.c create mode 100644 drivers/vfio/pci/vfio_pci_private.h create mode 100644 drivers/vfio/pci/vfio_pci_rdwr.c create mode 100644 drivers/vfio/vfio.c create mode 100644 drivers/vfio/vfio_iommu_x86.c create mode 100644 include/linux/vfio.h
PATCH 1/13 driver core: Add iommu_group tracking to struct device
IOMMU groups allow IOMMU drivers to represent DMA visibility and isolation of devices. Multiple devices may be grouped together for the purposes of DMA. Placing a pointer on struct device enable easy access for things like streaming DMA programming and drivers like VFIO.
---
include/linux/device.h | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
include/linux/device.h +2 -0
@@ -35,6 +35,7 @@ struct subsys_private;struct bus_type ;struct device_node ;struct iommu_ops ; +struct iommu_group ;struct bus_attribute {struct attribute attr;
@@ -677,6 +678,7 @@ struct device {const struct attribute_group **groups;/* optional groups */ void (*release)(struct device *dev); +struct iommu_group *iommu_group; };/* Get the wakeup routines, which depend on struct device */
PATCH 2/13 iommu: IOMMU Groups
In reply to Alex WilliamsonIOMMU device groups are currently a rather vague associative notion with assembly required by the user or user level driver provider to do anything useful. This patch intends to grow the IOMMU group concept into something a bit more consumable.
To do this, we first create an object representing the group, struct iommu_group. This structure is allocated (iommu_group_alloc) and filled (iommu_group_add_device) by the iommu driver. The iommu driver is free to add devices to the group using it's own set of policies. This allows inclusion of devices based on physical hardware or topology limitations of the platform, as well as soft requirements, such as multi-function trust levels or peer-to-peer protection of the interconnects. Each device may only belong to a single iommu group, which is linked from struct device.iommu_group. IOMMU groups are maintained using kobject reference counting, allowing for automatic removal of empty, unreferenced groups. It is the responsibility of the iommu driver to remove devices from the group (iommu_group_remove_device).
IOMMU groups also include a userspace representation in sysfs under /sys/kernel/iommu_groups. When allocated, each group is given a dynamically assign ID (int). The ID is managed by the core IOMMU group code to support multiple heterogeneous iommu drivers, which could potentially collide in group naming/numbering. This also keeps group IDs to small, easily managed values. A directory is created under /sys/kernel/iommu_groups for each group. A further subdirectory named "devices" contains links to each device within the group. The iommu_group file in the device's sysfs directory, which formerly contained a group number when read, is now a link to the iommu group. Example:
$ ls -l /sys/kernel/iommu_groups/26/devices/ total 0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:00:1e.0 -> ../../../../devices/pci0000:00/0000:00:1e.0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:06:0d.0 -> ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:06:0d.1 -> ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.1
$ ls -l /sys/kernel/iommu_groups/26/devices/*/iommu_group [truncating perms/owner/timestamp] /sys/kernel/iommu_groups/26/devices/0000:00:1e.0/iommu_group -> ../../../kernel/iommu_groups/26 /sys/kernel/iommu_groups/26/devices/0000:06:0d.0/iommu_group -> ../../../../kernel/iommu_groups/26 /sys/kernel/iommu_groups/26/devices/0000:06:0d.1/iommu_group -> ../../../../kernel/iommu_groups/26
Groups also include several exported functions for use by user level driver providers, for example VFIO. These include:
iommu_group_get(): Acquires a reference to a group from a device iommu_group_put(): Releases reference iommu_group_for_each_dev(): Iterates over group devices using callback iommu_group_[un]register_notifier(): Allows notification of device add
and remove operations relevant to the groupiommu_group_id(): Return the group number
This patch also extends the IOMMU API to allow attaching groups to domains. This is currently a simple wrapper for iterating through devices within a group, but it's expected that the IOMMU API may eventually make groups a more integral part of domains.
Groups intentionally do not try to manage group ownership. A user level driver provider must independently acquire ownership for each device within a group before making use of the group as a whole. This may change in the future if group usage becomes more pervasive across both DMA and IOMMU ops.
Groups intentionally do not provide a mechanism for driver locking or otherwise manipulating driver matching/probing of devices within the group. Such interfaces are generic to devices and beyond the scope of IOMMU groups. If implemented, user level providers have ready access via iommu_group_for_each_dev and group notifiers.
Groups currently provide no storage for iommu context, but some kind of iommu_group_get/set_iommudata() interface is likely if groups become more pervasive in the dma layers.
iommu_device_group() is removed here as it has no users. The replacement is:
group = iommu_group_get(dev); id = iommu_group_id(group); iommu_group_put(group);
AMD-Vi & Intel VT-d support re-added in following patches.
---
drivers/iommu/amd_iommu.c | 21 -- drivers/iommu/intel-iommu.c | 49 ----- drivers/iommu/iommu.c | 449 ++++++++++++++++++++++++++++++++++++++++--- include/linux/iommu.h | 84 ++++++++ 4 files changed, 499 insertions(+), 104 deletions(-)
drivers/iommu/amd_iommu.c +0 -21
@@ -3193,26 +3193,6 @@ static int amd_iommu_domain_has_cap(struct iommu_domain *domain,return 0 ; } -static int amd_iommu_device_group (struct device *dev,unsigned int *groupid) -{ -struct iommu_dev_data *dev_data = dev->archdata.iommu; -struct pci_dev *pdev =to_pci_dev (dev); -u16 devid; - -if (!dev_data) -return -ENODEV ; - -if (pdev->is_virtfn || !iommu_group_mf) - devid = dev_data->devid; -else - devid =calc_devid (pdev->bus->number, -PCI_DEVFN (PCI_SLOT (pdev->devfn),0 )); - - *groupid = amd_iommu_alias_table[devid]; - -return 0 ; -} -static struct iommu_ops amd_iommu_ops = { .domain_init = amd_iommu_domain_init, .domain_destroy = amd_iommu_domain_destroy,
@@ -3222,7 +3202,6 @@ static struct iommu_ops amd_iommu_ops = { .unmap = amd_iommu_unmap, .iova_to_phys = amd_iommu_iova_to_phys, .domain_has_cap = amd_iommu_domain_has_cap, - .device_group = amd_iommu_device_group, .pgsize_bitmap = AMD_IOMMU_PGSIZES, };
drivers/iommu/intel-iommu.c +0 -49
61 lines not on this page · raw message
drivers/iommu/iommu.c +418 -31
495 lines not on this page · raw message
include/linux/iommu.h +81 -3
127 lines not on this page · raw message
PATCH 3/13 iommu: IOMMU groups for VT-d and AMD-Vi
In reply to Alex WilliamsonAdd back group support for AMD & Intel. amd_iommu already tracks devices and has init and uninit routines to manage groups. intel-iommu does this on the fly, so we make use of the notifier support built into iommu groups to create and remove groups.
---
drivers/iommu/amd_iommu.c | 28 +++++++++++++++++++++++++- drivers/iommu/intel-iommu.c | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletions(-)
drivers/iommu/amd_iommu.c +27 -1
@@ -256,9 +256,11 @@ static bool check_device(struct device *dev)static int iommu_init_device (struct device *dev) { -struct pci_dev *pdev =to_pci_dev (dev); +struct pci_dev *dma_pdev, *pdev =to_pci_dev (dev);struct iommu_dev_data *dev_data; +struct iommu_group *group;u16 alias; +int ret;if (dev->archdata.iommu)return 0 ;
@@ -279,8 +281,30 @@ static int iommu_init_device(struct device *dev)return -ENOTSUPP ; } dev_data->alias_data = alias_data; + + dma_pdev =pci_get_bus_and_slot (alias >>8 , alias &0xff ); + }else + dma_pdev = pdev; + +if (!pdev->is_virtfn &&PCI_FUNC (pdev->devfn) && iommu_group_mf && + pdev->hdr_type ==PCI_HEADER_TYPE_NORMAL ) + dma_pdev =pci_get_slot (pdev->bus, +PCI_DEVFN (PCI_SLOT (pdev->devfn),0 )); + + group =iommu_group_get (&dma_pdev->dev); +if (!group) { + group =iommu_group_alloc (); +if (IS_ERR (group)) +return PTR_ERR (group); } + ret =iommu_group_add_device (group, dev); + +iommu_group_put (group); + +if (ret) +return ret; +if (pci_iommuv2_capable (pdev)) {struct amd_iommu *iommu;
@@ -309,6 +333,8 @@ static void iommu_ignore_device(struct device *dev)static void iommu_uninit_device (struct device *dev) { +iommu_group_remove_device (dev); +/* * Nothing to do here - we keep dev_data around for unplugged devices * and reuse it when the device is re-plugged - not doing so would
drivers/iommu/intel-iommu.c +46 -0
57 lines not on this page · raw message
PATCH 4/13 pci: New pci_dma_quirk()
In reply to Alex WilliamsonIntegrating IOMMU groups more closely into the driver core allows us to more easily work around DMA quirks. The Ricoh multifunction controller is a favorite example of devices that are currently incompatible with IOMMU isolation as all the functions use the requestor ID of function 0 for DMA. Passing this device into pci_dma_quirk returns the PCI device to use for DMA. The IOMMU driver can then construct an IOMMU group including both devices.
---
drivers/pci/quirks.c | 22 ++++++++++++++++++++++ include/linux/pci.h | 2 ++ 2 files changed, 24 insertions(+), 0 deletions(-)
drivers/pci/quirks.c +22 -0
@@ -3109,3 +3109,25 @@ int pci_dev_specific_reset(struct pci_dev *dev, int probe)return -ENOTTY ; } + +struct pci_dev *pci_dma_quirk (struct pci_dev *dev) +{ +struct pci_dev *dma_dev = dev; + +/* +* https://bugzilla.redhat.com/show_bug.cgi?id=605888 +* +* Some Ricoh devices use the function 0 source ID for DMA on +* other functions of a multifunction device. The DMA devices +* is therefore function 0, which will have implications of the +* iommu grouping of these devices. +*/ +if (dev->vendor ==PCI_VENDOR_ID_RICOH && + (dev->device ==0xe822 || dev->device ==0xe230 || + dev->device ==0xe832 || dev->device ==0xe476 )) { + dma_dev =pci_get_slot (dev->bus, +PCI_DEVFN (PCI_SLOT (dev->devfn),0 )); + } + +return dma_dev; +}
include/linux/pci.h +2 -0
@@ -1479,9 +1479,11 @@ enum pci_fixup_pass {#ifdef CONFIG_PCI_QUIRKS void pci_fixup_device (enum pci_fixup_pass pass,struct pci_dev *dev); +struct pci_dev *pci_dma_quirk (struct pci_dev *dev);#else static inline void pci_fixup_device (enum pci_fixup_pass pass,struct pci_dev *dev) {} +struct pci_dev *pci_dma_quirk(struct pci_dev *dev) { return dev }#endif void __iomem *pcim_iomap (struct pci_dev *pdev,int bar,unsigned long maxlen);
PATCH 5/13 pci: New pci_acs_enabled()
In reply to Alex WilliamsonIn a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device, pci_acs_enabled returns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.
---
drivers/pci/pci.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 1 + 2 files changed, 44 insertions(+), 0 deletions(-)
drivers/pci/pci.c +43 -0
@@ -2358,6 +2358,49 @@ void pci_enable_acs(struct pci_dev *dev)pci_write_config_word (dev, pos +PCI_ACS_CTRL , ctrl); } +#define PCI_EXT_CAP_ACS_ENABLED (PCI_ACS_SV | PCI_ACS_RR | \ + PCI_ACS_CR | PCI_ACS_UF) + +/** +* pci_acs_enabled - test ACS support in downstream chain +* @dev: starting PCI device +* +* Returns the furthest downstream device with an unbroken ACS chain. If +* ACS is enabled throughout the chain, the returned device is the same as +* the one passed in. +*/ +struct pci_dev *pci_acs_enabled (struct pci_dev *dev) +{ +struct pci_dev *acs_dev; +int pos; +u16 ctrl; + +if (!pci_is_root_bus (dev->bus)) + acs_dev =pci_acs_enabled (dev->bus->self); +else +return dev; + +/* If the chain is already broken, pass on the device */ +if (acs_dev != dev->bus->self) +return acs_dev; + +if (!pci_is_pcie (dev) || (dev->class >>8 ) !=PCI_CLASS_BRIDGE_PCI ) +return dev; + +if (dev->pcie_type !=PCI_EXP_TYPE_DOWNSTREAM ) +return dev; + + pos =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_ACS ); +if (!pos) +return acs_dev; + +pci_read_config_word (dev, pos +PCI_ACS_CTRL , &ctrl); +if ((ctrl &PCI_EXT_CAP_ACS_ENABLED ) !=PCI_EXT_CAP_ACS_ENABLED ) +return acs_dev; + +return dev; +} +/** * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge * @dev: the PCI device
include/linux/pci.h +1 -0
@@ -1586,6 +1586,7 @@ static inline bool pci_is_pcie(struct pci_dev *dev) }void pci_request_acs (void ); +struct pci_dev *pci_acs_enabled (struct pci_dev *dev);#define PCI_VPD_LRDT 0x80/* Large Resource Data Type */
PATCH 6/13 iommu: Make use of DMA quirking and ACS enabled check for groups
In reply to Alex WilliamsonIncorporate DMA quirking and ACS checking into amd_iommu and intel-iommu. Note that IOMMU groups are not yet used for streaming DMA, so this doesn't immediately solve the problems with broken Ricoh devices.
---
drivers/iommu/amd_iommu.c | 3 +++ drivers/iommu/intel-iommu.c | 3 +++ drivers/pci/pci.h | 1 + 3 files changed, 7 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu.c +3 -0
@@ -291,6 +291,9 @@ static int iommu_init_device(struct device *dev) dma_pdev =pci_get_slot (pdev->bus,PCI_DEVFN (PCI_SLOT (pdev->devfn),0 )); + dma_pdev =pci_dma_quirk (dma_pdev); + dma_pdev =pci_acs_enabled (dma_pdev); + group =iommu_group_get (&dma_pdev->dev);if (!group) { group =iommu_group_alloc ();
drivers/iommu/intel-iommu.c +3 -0
@@ -4113,6 +4113,9 @@ static int intel_iommu_add_device(struct device *dev) dma_pdev =pci_get_slot (pdev->bus,PCI_DEVFN (PCI_SLOT (pdev->devfn),0 )); + dma_pdev =pci_dma_quirk (dma_pdev); + dma_pdev =pci_acs_enabled (dma_pdev); + group =iommu_group_get (&dma_pdev->dev);if (!group) { group =iommu_group_alloc ();
drivers/pci/pci.h +1 -0
@@ -310,6 +310,7 @@ static inline resource_size_t pci_resource_alignment(struct pci_dev *dev, }extern void pci_enable_acs (struct pci_dev *dev); +extern struct pci_dev *pci_acs_enabled (struct pci_dev *dev);struct pci_dev_reset_methods {u16 vendor;
PATCH 7/13 vfio: VFIO core
In reply to Alex WilliamsonVFIO is a secure user level driver for use with both virtual machines and user level drivers. VFIO makes use of IOMMU groups to ensure the isolation of devices in use, allowing unprivileged user access. It's intended that VFIO will replace KVM device assignment and UIO drivers (in cases where the target platform includes a sufficiently capable IOMMU).
New in this version of VFIO is support for IOMMU groups managed through the IOMMU core as well as a rework of the API, removing the group merge interface. We now go back to a model more similar to original VFIO with UIOMMU support where the file descriptor obtained from /dev/vfio/vfio allows access to the IOMMU, but only after a group is added, avoiding the previous privilege issues with this type of model. IOMMU support is also now fully modular as IOMMUs have vastly different interface requirements on different platforms. VFIO users are able to query and initialize the IOMMU model of their choice.
Please see the follow-on Documentation commit for further description and usage example.
---
Documentation/ioctl/ioctl-number.txt | 1 MAINTAINERS | 8 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/vfio/Kconfig | 8 drivers/vfio/Makefile | 1 drivers/vfio/vfio.c | 1399 ++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 364 +++++++++ 8 files changed, 1784 insertions(+), 0 deletions(-) create mode 100644 drivers/vfio/Kconfig create mode 100644 drivers/vfio/Makefile create mode 100644 drivers/vfio/vfio.c create mode 100644 include/linux/vfio.h
Documentation/ioctl/ioctl-number.txt +1 -0
@@ -88,6 +88,7 @@ Code Seq#(hex) Include File Comments and kernel/power/user.c '8' all SNP8023 advanced NIC card <mailto:[email protected]> +';' 64-6F linux/vfio.h '@' 00-0F linux/radeonfb.h conflict! '@' 00-0F drivers/video/aty/aty128fb.c conflict! 'A' 00-1F linux/apm_bios.h conflict!
MAINTAINERS +8 -0
@@ -7227,6 +7227,14 @@ S: Maintained F: Documentation/filesystems/vfat.txt F: fs/fat/ +VFIO DRIVER +M: Alex Williamson <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/vfio.txt +F: drivers/vfio/ +F: include/linux/vfio.h + VIDEOBUF2 FRAMEWORK M: Pawel Osciak <pawel-FA/[email protected]> M: Marek Szyprowski <[email protected]>
drivers/Kconfig +2 -0
@@ -112,6 +112,8 @@ source "drivers/auxdisplay/Kconfig" source "drivers/uio/Kconfig" +source "drivers/vfio/Kconfig" + source "drivers/vlynq/Kconfig" source "drivers/virtio/Kconfig"
drivers/Makefile +1 -0
@@ -59,6 +59,7 @@ obj-$(CONFIG_ATM) += atm/ obj-$(CONFIG_FUSION) += message/ obj-y += firewire/ obj-$(CONFIG_UIO) += uio/ +obj-$(CONFIG_VFIO) += vfio/ obj-y += cdrom/ obj-y += auxdisplay/ obj-$(CONFIG_PCCARD) += pcmcia/
drivers/vfio/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +menuconfig VFIO + tristate "VFIO Non-Privileged userspace driver framework" + depends on IOMMU_API + help + VFIO provides a framework for secure userspace device drivers. + See Documentation/vfio.txt for more details. + + If you don't know what to do here, say N.
drivers/vfio/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_VFIO) += vfio.o
drivers/vfio/vfio.c new file +1399 -0
1,399 lines not on this page · raw message
include/linux/vfio.h new file +364 -0
364 lines not on this page · raw message
PATCH 8/13 vfio: Add documentation
In reply to Alex Williamson---
Documentation/vfio.txt | 315 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 315 insertions(+), 0 deletions(-) create mode 100644 Documentation/vfio.txt
Documentation/vfio.txt new file +315 -0
@@ -0,0 +1,315 @@ +VFIO - "Virtual Function I/O"[1] +------------------------------------------------------------------------------- +Many modern system now provide DMA and interrupt remapping facilities +to help ensure I/O devices behave within the boundaries they've been +allotted. This includes x86 hardware with AMD-Vi and Intel VT-d, +POWER systems with Partitionable Endpoints (PEs) and embedded PowerPC +systems such as Freescale PAMU. The VFIO driver is an IOMMU/device +agnostic framework for exposing direct device access to userspace, in +a secure, IOMMU protected environment. In other words, this allows +safe[2], non-privileged, userspace drivers. + +Why do we want that? Virtual machines often make use of direct device +access ("device assignment") when configured for the highest possible +I/O performance. From a device and host perspective, this simply +turns the VM into a userspace driver, with the benefits of +significantly reduced latency, higher bandwidth, and direct use of +bare-metal device drivers[3]. + +Some applications, particularly in the high performance computing +field, also benefit from low-overhead, direct device access from +userspace. Examples include network adapters (often non-TCP/IP based) +and compute accelerators. Prior to VFIO, these drivers had to either +go through the full development cycle to become proper upstream +driver, be maintained out of tree, or make use of the UIO framework, +which has no notion of IOMMU protection, limited interrupt support, +and requires root privileges to access things like PCI configuration +space. + +The VFIO driver framework intends to unify these, replacing both the +KVM PCI specific device assignment code as well as provide a more +secure, more featureful userspace driver environment than UIO. + +Groups, Devices, and IOMMUs +------------------------------------------------------------------------------- + +Devices are the main target of any I/O driver. Devices typically +create a programming interface made up of I/O access, interrupts, +and DMA. Without going into the details of each of these, DMA is +by far the most critical aspect for maintaining a secure environment +as allowing a device read-write access to system memory imposes the +greatest risk to the overall system integrity. + +To help mitigate this risk, many modern IOMMUs now incorporate +isolation properties into what was, in many cases, an interface only +meant for translation (ie. solving the addressing problems of devices +with limited address spaces). With this, devices can now be isolated +from each other and from arbitrary memory access, thus allowing +things like secure direct assignment of devices into virtual machines. + +This isolation is not always at the granularity of a single device +though. Even when an IOMMU is capable of this, properties of devices, +interconnects, and IOMMU topologies can each reduce this isolation. +For instance, an individual device may be part of a larger multi- +function enclosure. While the IOMMU may be able to distinguish +between devices within the enclosure, the enclosure may not require +transactions between devices to reach the IOMMU. Examples of this +could be anything from a multi-function PCI device with backdoors +between functions to a non-PCI-ACS (Access Control Services) capable +bridge allowing redirection without reaching the IOMMU. Topology +can also play a factor in terms of hiding devices. A PCIe-to-PCI +bridge masks the devices behind it, making transaction appear as if +from the bridge itself. Obviously IOMMU design plays a major factor +as well. + +Therefore, while for the most part an IOMMU may have device level +granularity, any system is susceptible to reduced granularity. The +IOMMU API therefore supports a notion of IOMMU groups. A group is +a set of devices which is isolatable from all other devices in the +system. Groups are therefore the unit of ownership used by VFIO. + +While the group is the minimum granularity that must be used to +ensure secure user access, it's not necessarily the preferred +granularity. In IOMMUs which make use of page tables, it may be +possible to share a set of page tables between different groups, +reducing the overhead both to the platform (reduced TLB thrashing, +reduced duplicate page tables), and to the user (programming only +a single set of translations). For this reason, VFIO makes use of +a container class, which may hold one or more groups. A container +is created by simply opening the /dev/vfio/vfio character device. + +On it's own, the container provides little functionality, with all +but a couple version and extension query interfaces locked away. +The user needs to add a group into the container for the next level +of functionality. To do this, the user first needs to identify the +group associated with the desired device. This can be done using +the sysfs links described in the example below. By unbinding the +device from the host driver and binding it to a VFIO driver, a new +VFIO group will appear for the group as /dev/vfio/$GROUP, where +$GROUP is the IOMMU group number of which the device is a member. +If the IOMMU group contains multiple devices, each will need to +be bound to a VFIO driver before operations on the VFIO group +are allowed (it's also sufficient to only unbind the device from +host drivers if a VFIO driver is unavailable; this will make the +group available, but not that particular device). TBD - interface +for disabling driver probing/locking a device. + +Once the group is ready, it may be added to the container by opening +the VFIO group character device (/dev/vfio/$GROUP) and using the +VFIO_GROUP_SET_CONTAINER ioctl, passing the file descriptor of the +previously opened container file. If desired and if the IOMMU driver +supports sharing the IOMMU context between groups, multiple groups may +be set to the same container. If a group fails to set to a container +with existing groups, a new empty container will need to be used +instead. + +With a group (or groups) attached to a container, the remaining +ioctls become available, enabling access to the VFIO IOMMU interfaces. +Additionally, it now becomes possible to get file descriptors for each +device within a group using an ioctl on the VFIO group file descriptor. + +The VFIO device API includes ioctls for describing the device, the I/O +regions and their read/write/mmap offsets on the device descriptor, as +well as mechanisms for describing and registering interrupt +notifications. + +VFIO Usage Example +------------------------------------------------------------------------------- + +Assume user wants to access PCI device 0000:06:0d.0 + +$ readlink /sys/bus/pci/devices/0000:06:0d.0/iommu_group +../../../../kernel/iommu_groups/26 + +This device is therefore in IOMMU group 26. This device is on the +pci bus, therefore the user will make use of vfio-pci to manage the +group: + +# modprobe vfio-pci + +Binding this device to the vfio-pci driver creates the VFIO group +character devices for this group: + +$ lspci -n -s 0000:06:0d.0 +06:0d.0 0401: 1102:0002 (rev 08) +# echo 0000:06:0d.0 > /sys/bus/pci/devices/0000:06:0d.0/driver/unbind +# echo 1102 0002 > /sys/bus/pci/drivers/vfio/new_id +# echo 0000:06:0d.0 > /sys/bus/pci/drivers/vfio/bind + +Now we need to look at what other devices are in the group to free +it for use by VFIO: + +$ ls -l /sys/bus/pci/devices/0000:06:0d.0/iommu_group/devices +total 0 +lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:00:1e.0 -> + ../../../../devices/pci0000:00/0000:00:1e.0 +lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:06:0d.0 -> + ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.0 +lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:06:0d.1 -> + ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.1 + +This device is behind a PCIe-to-PCI bridge[4], therefore we also +need to add device 0000:06:0d.1 to the group following the same +procedure as above. Device 0000:00:1e.0 is a bridge that does +not currently have a host driver, therefore it's not required to +bind this device to the vfio-pci driver (vfio-pci does not currently +support PCI bridges). + +The final step is to provide the user with access to the group if +unprivileged operation is desired (note that /dev/vfio/vfio provides +no capabilities on it's own and is therefore expected to be set to +mode 0666 by the system). + +# chown user:user /dev/vfio/26 + +The user now has full access to all the devices and the iommu for this +group and can access them as follows: + + int container, group, device, i; + struct vfio_group_status group_status = + { .argsz = sizeof(group_status) }; + struct vfio_iommu_x86_info iommu_info = { .argsz = sizeof(iommu_info) }; + struct vfio_iommu_x86_dma_map dma_map = { .argsz = sizeof(dma_map) }; + struct vfio_device_info device_info = { .argsz = sizeof(device_info) }; + + /* Create a new container */ + container = open("/dev/vfio/vfio, O_RDWR); + + if (ioctl(container, VFIO_GET_API_VERSION) != VFIO_API_VERSION) + /* Unknown API version */ + + if (!ioctl(container, VFIO_CHECK_EXTENSION, VFIO_X86_IOMMU)) + /* Doesn't support the IOMMU driver we want. */ + + /* Open the group */ + group = open("/dev/vfio/26", O_RDWR); + + /* Test the group is viable and available */ + ioctl(group, VFIO_GROUP_GET_STATUS, &group_status); + + if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) + /* Group is not viable (ie, not all devices bound for vfio) */ + + /* Add the group to the container */ + ioctl(group, VFIO_GROUP_SET_CONTAINER, &container); + + /* Enable the IOMMU model we want */ + ioctl(container, VFIO_SET_IOMMU, VFIO_X86_IOMMU) + + /* Get addition IOMMU info */ + ioctl(container, VFIO_IOMMU_GET_INFO, &iommu_info); + + /* Allocate some space and setup a DMA mapping */ + dma_map.vaddr = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + dma_map.size = 1024 * 1024; + dma_map.iova = 0; /* 1MB starting at 0x0 from device view */ + dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE; + + ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map); + + /* Get a file descriptor for the device */ + device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:06:0d.0"); + + /* Test and setup the device */ + ioctl(device, VFIO_DEVICE_GET_INFO, &device_info); + + for (i = 0; i < device_info.num_regions; i++) { + struct vfio_region_info reg = { .argsz = sizeof(reg) }; + + reg.index = i; + + ioctl(device, VFIO_DEVICE_GET_REGION_INFO, ®); + + /* Setup mappings... read/write offsets, mmaps + * For PCI devices, config space is a region */ + } + + for (i = 0; i < device_info.num_irqs; i++) { + struct vfio_irq_info irq = { .argsz = sizeof(irq) }; + + irq.index = i; + + ioctl(device, VFIO_DEVICE_GET_IRQ_INFO, ®); + + /* Setup IRQs... eventfds, VFIO_DEVICE_SET_IRQS */ + } + + /* Gratuitous device reset and go... */ + ioctl(device, VFIO_DEVICE_RESET); + +VFIO User API +------------------------------------------------------------------------------- + +Please see include/linux/vfio.h for complete API documentation. + +VFIO bus driver API +------------------------------------------------------------------------------- + +VFIO bus drivers, such as vfio-pci make use of only a few interfaces +into VFIO core. When devices are bound and unbound to the driver, +the driver should call vfio_add_group_dev() and vfio_del_group_dev() +respectively: + +extern int vfio_add_group_dev(struct iommu_group *iommu_group, + struct device *dev, + const struct vfio_device_ops *ops, + void *device_data); + +extern void *vfio_del_group_dev(struct device *dev); + +vfio_add_group_dev() indicates to the core to begin tracking the +specified iommu_group and register the specified dev as owned by +a VFIO bus driver. The driver provides an ops structure for callbacks +similar to a file operations structure: + +struct vfio_device_ops { + int (*open)(void *device_data); + void (*release)(void *device_data); + ssize_t (*read)(void *device_data, char __user *buf, + size_t count, loff_t *ppos); + ssize_t (*write)(void *device_data, const char __user *buf, + size_t size, loff_t *ppos); + long (*ioctl)(void *device_data, unsigned int cmd, + unsigned long arg); + int (*mmap)(void *device_data, struct vm_area_struct *vma); +}; + +Each function is passed the device_data that was originally registered +in the vfio_add_group_dev() call above. This allows the bus driver +an easy place to store it's opaque, private data. The open/release +callbacks are issued when a new file descriptor is created for a +device (via VFIO_GROUP_GET_DEVICE_FD). The ioctl interface provides +a direct pass through for VFIO_DEVICE_* ioctls. The read/write/mmap +interfaces implement the device region access defined by the device's +own VFIO_DEVICE_GET_REGION_INFO ioctl. + +------------------------------------------------------------------------------- + +[1] VFIO was originally an acronym for "Virtual Function I/O" in it's +initial implementation by Tom Lyon while as Cisco. We've since +outgrown the acronym, but it's catchy. + +[2] "safe" also depends upon a device being "well behaved". It's +possible for multi-function devices to have backdoors between +functions and even for single function devices to have alternative +access to things like PCI config space through MMIO registers. To +guard against the former we can include additional precautions in the +IOMMU driver to group multi-function PCI devices together +(iommu=group_mf). The latter we can't prevent, but the IOMMU should +still provide isolation. For PCI, SR-IOV Virtual Functions are the +best indicator of "well behaved", as these are designed for +virtualization usage models. + +[3] As always there are trade-offs to virtual machine device +assignment that are beyond the scope of VFIO. It's expected that +future IOMMU technologies will reduce some, but maybe not all, of +these trade-offs. + +[4] In this case the device is below a PCI bridge, so transactions +from either function of the device are indistinguishable to the iommu: + +-[0000:00]-+-1e.0-[06]--+-0d.0 + \-0d.1 + +00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 90)
PATCH 9/13 vfio: x86 IOMMU implementation
In reply to Alex Williamsonx86 is probably the wrong name for this VFIO IOMMU driver, but x86 is the primary target for it. This driver support a very simple usage model using the existing IOMMU API. The IOMMU is expected to support the full host address space with no special IOVA windows, number of mappings restrictions, or unique processor target options.
---
Documentation/ioctl/ioctl-number.txt | 2 drivers/vfio/Kconfig | 6 drivers/vfio/Makefile | 2 drivers/vfio/vfio.c | 7 drivers/vfio/vfio_iommu_x86.c | 743 ++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 52 ++ 6 files changed, 811 insertions(+), 1 deletions(-) create mode 100644 drivers/vfio/vfio_iommu_x86.c
Documentation/ioctl/ioctl-number.txt +1 -1
@@ -88,7 +88,7 @@ Code Seq#(hex) Include File Comments and kernel/power/user.c '8' all SNP8023 advanced NIC card <mailto:[email protected]> -';' 64-6F linux/vfio.h +';' 64-72 linux/vfio.h '@' 00-0F linux/radeonfb.h conflict! '@' 00-0F drivers/video/aty/aty128fb.c conflict! 'A' 00-1F linux/apm_bios.h conflict!
drivers/vfio/Kconfig +6 -0
@@ -1,6 +1,12 @@ +config VFIO_IOMMU_X86 + tristate + depends on VFIO && X86 + default n + menuconfig VFIO tristate "VFIO Non-Privileged userspace driver framework" depends on IOMMU_API + select VFIO_IOMMU_X86 if X86 help VFIO provides a framework for secure userspace device drivers. See Documentation/vfio.txt for more details.
drivers/vfio/Makefile +2 -0
@@ -1 +1,3 @@ obj-$(CONFIG_VFIO) += vfio.o +obj-$(CONFIG_VFIO_IOMMU_X86) += vfio_iommu_x86.o +obj-$(CONFIG_VFIO_PCI) += pci/
drivers/vfio/vfio.c +7 -0
@@ -1362,6 +1362,13 @@ static int __init vfio_init(void)pr_info (DRIVER_DESC " version: " DRIVER_VERSION "\n" ); +/* +* Attempt to load known iommu-drivers. This gives us a working +* environment without the user needing to explicitly load iommu +* drivers. +*/ +request_module_nowait ("vfio_iommu_x86" ); +return 0 ; err_groups_cdev:
drivers/vfio/vfio_iommu_x86.c new file +743 -0
743 lines not on this page · raw message
include/linux/vfio.h +52 -0
56 lines not on this page · raw message
PATCH 10/13 pci: export pci_user functions for use by other drivers
In reply to Alex WilliamsonVFIO PCI support will make use of these for user initiated PCI config accesses.
---
drivers/pci/access.c | 6 ++++-- drivers/pci/pci.h | 7 ------- include/linux/pci.h | 8 ++++++++ 3 files changed, 12 insertions(+), 9 deletions(-)
drivers/pci/access.c +4 -2
@@ -162,7 +162,8 @@ int pci_user_read_config_##size \if (ret >0 ) \ ret = -EINVAL ; \return ret; \ -} +} \ +EXPORT_SYMBOL_GPL(pci_user_read_config_##size);/* Returns 0 on success, negative values indicate error. */ #define PCI_USER_WRITE_CONFIG (size,type) \
@@ -181,7 +182,8 @@ int pci_user_write_config_##size \if (ret >0 ) \ ret = -EINVAL ; \return ret; \ -} +} \ +EXPORT_SYMBOL_GPL(pci_user_write_config_##size); PCI_USER_READ_CONFIG(byte, u8)PCI_USER_READ_CONFIG (word, u16)
drivers/pci/pci.h +0 -7
@@ -86,13 +86,6 @@ static inline bool pci_is_bridge(struct pci_dev *pci_dev)return !!(pci_dev->subordinate); } -extern int pci_user_read_config_byte (struct pci_dev *dev,int where,u8 *val); -extern int pci_user_read_config_word (struct pci_dev *dev,int where,u16 *val); -extern int pci_user_read_config_dword (struct pci_dev *dev,int where,u32 *val); -extern int pci_user_write_config_byte (struct pci_dev *dev,int where,u8 val); -extern int pci_user_write_config_word (struct pci_dev *dev,int where,u16 val); -extern int pci_user_write_config_dword (struct pci_dev *dev,int where,u32 val); -struct pci_vpd_ops {ssize_t (*read)(struct pci_dev *dev,loff_t pos,size_t count,void *buf);ssize_t (*write)(struct pci_dev *dev,loff_t pos,size_t count,const void *buf);
include/linux/pci.h +8 -0
@@ -770,6 +770,14 @@ static inline int pci_write_config_dword(const struct pci_dev *dev, int where,return pci_bus_write_config_dword (dev->bus, dev->devfn, where, val); } +/* user-space driven config access */ +extern int pci_user_read_config_byte (struct pci_dev *dev,int where,u8 *val); +extern int pci_user_read_config_word (struct pci_dev *dev,int where,u16 *val); +extern int pci_user_read_config_dword (struct pci_dev *dev,int where,u32 *val); +extern int pci_user_write_config_byte (struct pci_dev *dev,int where,u8 val); +extern int pci_user_write_config_word (struct pci_dev *dev,int where,u16 val); +extern int pci_user_write_config_dword (struct pci_dev *dev,int where,u32 val); +int __must_check pci_enable_device (struct pci_dev *dev);int __must_check pci_enable_device_io (struct pci_dev *dev);int __must_check pci_enable_device_mem (struct pci_dev *dev);
PATCH 11/13 pci: Create common pcibios_err_to_errno
In reply to Alex WilliamsonFor returning errors out to non-PCI code. Re-name xen's version.
---
drivers/xen/xen-pciback/conf_space.c | 6 +++--- include/linux/pci.h | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-)
drivers/xen/xen-pciback/conf_space.c +3 -3
@@ -124,7 +124,7 @@ static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,return val; } -static int pcibios_err_to_errno (int err) +static int xen_pcibios_err_to_errno (int err) {switch (err) {case PCIBIOS_SUCCESSFUL :
@@ -202,7 +202,7 @@ out: pci_name(dev), size, offset, value); *ret_val = value; -return pcibios_err_to_errno (err); +return xen_pcibios_err_to_errno (err); }int xen_pcibk_config_write (struct pci_dev *dev,int offset,int size,u32 value)
@@ -290,7 +290,7 @@ int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value) } } -return pcibios_err_to_errno (err); +return xen_pcibios_err_to_errno (err); }void xen_pcibk_config_free_dyn_fields (struct pci_dev *dev)
include/linux/pci.h +26 -0
@@ -467,6 +467,32 @@ static inline bool pci_dev_msi_enabled(struct pci_dev *pci_dev) { return false;#define PCIBIOS_SET_FAILED 0x88#define PCIBIOS_BUFFER_TOO_SMALL 0x89 +/* +* Translate above to generic errno for passing back through non-pci. +*/ +static inline int pcibios_err_to_errno (int err) +{ +if (err <=PCIBIOS_SUCCESSFUL ) +return err;/* Assume already errno */ + +switch (err) { +case PCIBIOS_FUNC_NOT_SUPPORTED : +return -ENOENT ; +case PCIBIOS_BAD_VENDOR_ID : +return -EINVAL ; +case PCIBIOS_DEVICE_NOT_FOUND : +return -ENODEV ; +case PCIBIOS_BAD_REGISTER_NUMBER : +return -EFAULT ; +case PCIBIOS_SET_FAILED : +return -EIO ; +case PCIBIOS_BUFFER_TOO_SMALL : +return -ENOSPC ; + } + +return -ENOTTY ; +} +/* Low-level architecture-dependent routines */ struct pci_ops {
PATCH 12/13 pci: Misc pci_reg additions
In reply to Alex WilliamsonFill in many missing definitions and add sizeof fields for many sections allowing for more extensive config parsing.
---
include/linux/pci_regs.h | 112 +++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 100 insertions(+), 12 deletions(-)
include/linux/pci_regs.h +100 -12
@@ -26,6 +26,7 @@* Under PCI, each device has 256 bytes of configuration address space, * of which the first 64 bytes are standardized as follows: */ +#define PCI_STD_HEADER_SIZEOF 64#define PCI_VENDOR_ID 0x00/* 16 bits */ #define PCI_DEVICE_ID 0x02/* 16 bits */ #define PCI_COMMAND 0x04/* 16 bits */
@@ -209,9 +210,12 @@#define PCI_CAP_ID_SHPC 0x0C/* PCI Standard Hot-Plug Controller */ #define PCI_CAP_ID_SSVID 0x0D/* Bridge subsystem vendor/device ID */ #define PCI_CAP_ID_AGP3 0x0E/* AGP Target PCI-PCI bridge */ +#define PCI_CAP_ID_SECDEV 0x0F/* Secure Device */ #define PCI_CAP_ID_EXP 0x10/* PCI Express */ #define PCI_CAP_ID_MSIX 0x11/* MSI-X */ +#define PCI_CAP_ID_SATA 0x12/* SATA Data/Index Conf. */ #define PCI_CAP_ID_AF 0x13/* PCI Advanced Features */ +#define PCI_CAP_ID_MAX PCI_CAP_ID_AF#define PCI_CAP_LIST_NEXT 1/* Next capability in the list */ #define PCI_CAP_FLAGS 2/* Capability defined flags (16 bits) */ #define PCI_CAP_SIZEOF 4
@@ -276,6 +280,7 @@#define PCI_VPD_ADDR_MASK 0x7fff/* Address mask */ #define PCI_VPD_ADDR_F 0x8000/* Write 0, 1 indicates completion */ #define PCI_VPD_DATA 4/* 32-bits of data returned here */ +#define PCI_CAP_VPD_SIZEOF 8/* Slot Identification */
@@ -297,8 +302,10 @@#define PCI_MSI_ADDRESS_HI 8/* Upper 32 bits (if PCI_MSI_FLAGS_64BIT set) */ #define PCI_MSI_DATA_32 8/* 16 bits of data for 32-bit devices */ #define PCI_MSI_MASK_32 12/* Mask bits register for 32-bit devices */ +#define PCI_MSI_PENDING_32 16/* Pending intrs for 32-bit devices */ #define PCI_MSI_DATA_64 12/* 16 bits of data for 64-bit devices */ #define PCI_MSI_MASK_64 16/* Mask bits register for 64-bit devices */ +#define PCI_MSI_PENDING_64 20/* Pending intrs for 64-bit devices */ /* MSI-X registers */ #define PCI_MSIX_FLAGS 2
@@ -308,6 +315,7 @@#define PCI_MSIX_TABLE 4#define PCI_MSIX_PBA 8#define PCI_MSIX_FLAGS_BIRMASK (7 << 0) +#define PCI_CAP_MSIX_SIZEOF 12/* size of MSIX registers */ /* MSI-X entry's format */ #define PCI_MSIX_ENTRY_SIZE 16
@@ -338,6 +346,7 @@#define PCI_AF_CTRL_FLR 0x01#define PCI_AF_STATUS 5#define PCI_AF_STATUS_TP 0x01 +#define PCI_CAP_AF_SIZEOF 6/* size of AF registers */ /* PCI-X registers */
@@ -374,6 +383,9 @@#define PCI_X_STATUS_SPL_ERR 0x20000000/* Rcvd Split Completion Error Msg */ #define PCI_X_STATUS_266MHZ 0x40000000/* 266 MHz capable */ #define PCI_X_STATUS_533MHZ 0x80000000/* 533 MHz capable */ +#define PCI_X_ECC_CSR 8/* ECC control and status */ +#define PCI_CAP_PCIX_SIZEOF_V0 8/* size of registers for Version 0 */ +#define PCI_CAP_PCIX_SIZEOF_V12 24/* size for Version 1 & 2 */ /* PCI Bridge Subsystem ID registers */
@@ -462,6 +474,7 @@#define PCI_EXP_LNKSTA_DLLLA 0x2000/* Data Link Layer Link Active */ #define PCI_EXP_LNKSTA_LBMS 0x4000/* Link Bandwidth Management Status */ #define PCI_EXP_LNKSTA_LABS 0x8000/* Link Autonomous Bandwidth Status */ +#define PCI_CAP_EXP_ENDPOINT_SIZEOF_V1 20/* v1 endpoints end here */ #define PCI_EXP_SLTCAP 20/* Slot Capabilities */ #define PCI_EXP_SLTCAP_ABP 0x00000001/* Attention Button Present */ #define PCI_EXP_SLTCAP_PCP 0x00000002/* Power Controller Present */
@@ -521,6 +534,7 @@#define PCI_EXP_OBFF_MSGA_EN 0x2000/* OBFF enable with Message type A */ #define PCI_EXP_OBFF_MSGB_EN 0x4000/* OBFF enable with Message type B */ #define PCI_EXP_OBFF_WAKE_EN 0x6000/* OBFF using WAKE# signaling */ +#define PCI_CAP_EXP_ENDPOINT_SIZEOF_V2 44/* v2 endpoints end here */ #define PCI_EXP_LNKCTL2 48/* Link Control 2 */ #define PCI_EXP_SLTCTL2 56/* Slot Control 2 */
@@ -529,23 +543,43 @@#define PCI_EXT_CAP_VER (header) ((header >> 16) & 0xf)#define PCI_EXT_CAP_NEXT (header) ((header >> 20) & 0xffc) -#define PCI_EXT_CAP_ID_ERR 1 -#define PCI_EXT_CAP_ID_VC 2 -#define PCI_EXT_CAP_ID_DSN 3 -#define PCI_EXT_CAP_ID_PWR 4 -#define PCI_EXT_CAP_ID_VNDR 11 -#define PCI_EXT_CAP_ID_ACS 13 -#define PCI_EXT_CAP_ID_ARI 14 -#define PCI_EXT_CAP_ID_ATS 15 -#define PCI_EXT_CAP_ID_SRIOV 16 -#define PCI_EXT_CAP_ID_PRI 19 -#define PCI_EXT_CAP_ID_LTR 24 -#define PCI_EXT_CAP_ID_PASID 27 +#define PCI_EXT_CAP_ID_ERR 0x01/* Advanced Error Reporting */ +#define PCI_EXT_CAP_ID_VC 0x02/* Virtual Channel Capability */ +#define PCI_EXT_CAP_ID_DSN 0x03/* Device Serial Number */ +#define PCI_EXT_CAP_ID_PWR 0x04/* Power Budgeting */ +#define PCI_EXT_CAP_ID_RCLD 0x05/* Root Complex Link Declaration */ +#define PCI_EXT_CAP_ID_RCILC 0x06/* Root Complex Internal Link Control */ +#define PCI_EXT_CAP_ID_RCEC 0x07/* Root Complex Event Collector */ +#define PCI_EXT_CAP_ID_MFVC 0x08/* Multi-Function VC Capability */ +#define PCI_EXT_CAP_ID_VC9 0x09/* same as _VC */ +#define PCI_EXT_CAP_ID_RCRB 0x0A/* Root Complex RB? */ +#define PCI_EXT_CAP_ID_VNDR 0x0B/* Vendor Specific */ +#define PCI_EXT_CAP_ID_CAC 0x0C/* Config Access - obsolete */ +#define PCI_EXT_CAP_ID_ACS 0x0D/* Access Control Services */ +#define PCI_EXT_CAP_ID_ARI 0x0E/* Alternate Routing ID */ +#define PCI_EXT_CAP_ID_ATS 0x0F/* Address Translation Services */ +#define PCI_EXT_CAP_ID_SRIOV 0x10/* Single Root I/O Virtualization */ +#define PCI_EXT_CAP_ID_MRIOV 0x11/* Multi Root I/O Virtualization */ +#define PCI_EXT_CAP_ID_MCAST 0x12/* Multicast */ +#define PCI_EXT_CAP_ID_PRI 0x13/* Page Request Interface */ +#define PCI_EXT_CAP_ID_AMD_XXX 0x14/* reserved for AMD */ +#define PCI_EXT_CAP_ID_REBAR 0x15/* resizable BAR */ +#define PCI_EXT_CAP_ID_DPA 0x16/* dynamic power alloc */ +#define PCI_EXT_CAP_ID_TPH 0x17/* TPH request */ +#define PCI_EXT_CAP_ID_LTR 0x18/* latency tolerance reporting */ +#define PCI_EXT_CAP_ID_SECPCI 0x19/* Secondary PCIe */ +#define PCI_EXT_CAP_ID_PMUX 0x1A/* Protocol Multiplexing */ +#define PCI_EXT_CAP_ID_PASID 0x1B/* Process Address Space ID */ +#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID + +#define PCI_EXT_CAP_DSN_SIZEOF 12 +#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40/* Advanced Error Reporting */ #define PCI_ERR_UNCOR_STATUS 4/* Uncorrectable Error Status */ #define PCI_ERR_UNC_TRAIN 0x00000001/* Training */ #define PCI_ERR_UNC_DLP 0x00000010/* Data Link Protocol */ +#define PCI_ERR_UNC_SURPDN 0x00000020/* Surprise Down */ #define PCI_ERR_UNC_POISON_TLP 0x00001000/* Poisoned TLP */ #define PCI_ERR_UNC_FCP 0x00002000/* Flow Control Protocol */ #define PCI_ERR_UNC_COMP_TIME 0x00004000/* Completion Timeout */
@@ -555,6 +589,11 @@#define PCI_ERR_UNC_MALF_TLP 0x00040000/* Malformed TLP */ #define PCI_ERR_UNC_ECRC 0x00080000/* ECRC Error Status */ #define PCI_ERR_UNC_UNSUP 0x00100000/* Unsupported Request */ +#define PCI_ERR_UNC_ACSV 0x00200000/* ACS Violation */ +#define PCI_ERR_UNC_INTN 0x00400000/* internal error */ +#define PCI_ERR_UNC_MCBTLP 0x00800000/* MC blocked TLP */ +#define PCI_ERR_UNC_ATOMEG 0x01000000/* Atomic egress blocked */ +#define PCI_ERR_UNC_TLPPRE 0x02000000/* TLP prefix blocked */ #define PCI_ERR_UNCOR_MASK 8/* Uncorrectable Error Mask */ /* Same bits as above */ #define PCI_ERR_UNCOR_SEVER 12/* Uncorrectable Error Severity */
@@ -565,6 +604,9 @@#define PCI_ERR_COR_BAD_DLLP 0x00000080/* Bad DLLP Status */ #define PCI_ERR_COR_REP_ROLL 0x00000100/* REPLAY_NUM Rollover */ #define PCI_ERR_COR_REP_TIMER 0x00001000/* Replay Timer Timeout */ +#define PCI_ERR_COR_ADV_NFAT 0x00002000/* Advisory Non-Fatal */ +#define PCI_ERR_COR_INTERNAL 0x00004000/* Corrected Internal */ +#define PCI_ERR_COR_LOG_OVER 0x00008000/* Header Log Overflow */ #define PCI_ERR_COR_MASK 20/* Correctable Error Mask */ /* Same bits as above */ #define PCI_ERR_CAP 24/* Advanced Error Capabilities */
@@ -596,12 +638,18 @@/* Virtual Channel */ #define PCI_VC_PORT_REG1 4 +#define PCI_VC_REG1_EVCC 0x7/* extended vc count */ #define PCI_VC_PORT_REG2 8 +#define PCI_VC_REG2_32_PHASE 0x2 +#define PCI_VC_REG2_64_PHASE 0x4 +#define PCI_VC_REG2_128_PHASE 0x8#define PCI_VC_PORT_CTRL 12#define PCI_VC_PORT_STATUS 14#define PCI_VC_RES_CAP 16#define PCI_VC_RES_CTRL 20#define PCI_VC_RES_STATUS 26 +#define PCI_CAP_VC_BASE_SIZEOF 0x10 +#define PCI_CAP_VC_PER_VC_SIZEOF 0x0C/* Power Budgeting */ #define PCI_PWR_DSR 4/* Data Select Register */
@@ -614,6 +662,7 @@#define PCI_PWR_DATA_RAIL (x) (((x) >> 18) & 7)/* Power Rail */ #define PCI_PWR_CAP 12/* Capability */ #define PCI_PWR_CAP_BUDGET (x) ((x) & 1)/* Included in system budget */ +#define PCI_EXT_CAP_PWR_SIZEOF 16/* * Hypertransport sub capability types
@@ -646,6 +695,8 @@#define HT_CAPTYPE_ERROR_RETRY 0xC0/* Retry on error configuration */ #define HT_CAPTYPE_GEN3 0xD0/* Generation 3 hypertransport configuration */ #define HT_CAPTYPE_PM 0xE0/* Hypertransport powermanagement configuration */ +#define HT_CAP_SIZEOF_LONG 28/* slave & primary */ +#define HT_CAP_SIZEOF_SHORT 24/* host & secondary */ /* Alternative Routing-ID Interpretation */ #define PCI_ARI_CAP 0x04/* ARI Capability Register */
@@ -656,6 +707,7 @@#define PCI_ARI_CTRL_MFVC 0x0001/* MFVC Function Groups Enable */ #define PCI_ARI_CTRL_ACS 0x0002/* ACS Function Groups Enable */ #define PCI_ARI_CTRL_FG (x) (((x) >> 4) & 7)/* Function Group */ +#define PCI_EXT_CAP_ARI_SIZEOF 8/* Address Translation Service */ #define PCI_ATS_CAP 0x04/* ATS Capability Register */
@@ -665,6 +717,7 @@#define PCI_ATS_CTRL_ENABLE 0x8000/* ATS Enable */ #define PCI_ATS_CTRL_STU (x) ((x) & 0x1f)/* Smallest Translation Unit */ #define PCI_ATS_MIN_STU 12/* shift of minimum STU block */ +#define PCI_EXT_CAP_ATS_SIZEOF 8/* Page Request Interface */ #define PCI_PRI_CTRL 0x04/* PRI control register */
@@ -676,6 +729,7 @@#define PCI_PRI_STATUS_STOPPED 0x100/* PRI Stopped */ #define PCI_PRI_MAX_REQ 0x08/* PRI max reqs supported */ #define PCI_PRI_ALLOC_REQ 0x0c/* PRI max reqs allowed */ +#define PCI_EXT_CAP_PRI_SIZEOF 16/* PASID capability */ #define PCI_PASID_CAP 0x04/* PASID feature register */
@@ -685,6 +739,7 @@#define PCI_PASID_CTRL_ENABLE 0x01/* Enable bit */ #define PCI_PASID_CTRL_EXEC 0x02/* Exec permissions Enable */ #define PCI_PASID_CTRL_PRIV 0x04/* Priviledge Mode Enable */ +#define PCI_EXT_CAP_PASID_SIZEOF 8/* Single Root I/O Virtualization */ #define PCI_SRIOV_CAP 0x04/* SR-IOV Capabilities */
@@ -716,12 +771,14 @@#define PCI_SRIOV_VFM_MI 0x1/* Dormant.MigrateIn */ #define PCI_SRIOV_VFM_MO 0x2/* Active.MigrateOut */ #define PCI_SRIOV_VFM_AV 0x3/* Active.Available */ +#define PCI_EXT_CAP_SRIOV_SIZEOF 64#define PCI_LTR_MAX_SNOOP_LAT 0x4#define PCI_LTR_MAX_NOSNOOP_LAT 0x6#define PCI_LTR_VALUE_MASK 0x000003ff#define PCI_LTR_SCALE_MASK 0x00001c00#define PCI_LTR_SCALE_SHIFT 10 +#define PCI_EXT_CAP_LTR_SIZEOF 8/* Access Control Service */ #define PCI_ACS_CAP 0x04/* ACS Capability Register */
@@ -732,7 +789,38 @@#define PCI_ACS_UF 0x10/* Upstream Forwarding */ #define PCI_ACS_EC 0x20/* P2P Egress Control */ #define PCI_ACS_DT 0x40/* Direct Translated P2P */ +#define PCI_ACS_EGRESS_BITS 0x05/* ACS Egress Control Vector Size */ #define PCI_ACS_CTRL 0x06/* ACS Control Register */ #define PCI_ACS_EGRESS_CTL_V 0x08/* ACS Egress Control Vector */ +#define PCI_VSEC_HDR 4/* extended cap - vendor specific */ +#define PCI_VSEC_HDR_LEN_SHIFT 20/* shift for length field */ + +/* sata capability */ +#define PCI_SATA_REGS 4/* SATA REGs specifier */ +#define PCI_SATA_REGS_MASK 0xF/* location - BAR#/inline */ +#define PCI_SATA_REGS_INLINE 0xF/* REGS in config space */ +#define PCI_SATA_SIZEOF_SHORT 8 +#define PCI_SATA_SIZEOF_LONG 16 + +/* resizable BARs */ +#define PCI_REBAR_CTRL 8/* control register */ +#define PCI_REBAR_CTRL_NBAR_MASK (7 << 5)/* mask for # bars */ +#define PCI_REBAR_CTRL_NBAR_SHIFT 5/* shift for # bars */ + +/* dynamic power allocation */ +#define PCI_DPA_CAP 4/* capability register */ +#define PCI_DPA_CAP_SUBSTATE_MASK 0x1F/* # substates - 1 */ +#define PCI_DPA_BASE_SIZEOF 16/* size with 0 substates */ + +/* TPH Requester */ +#define PCI_TPH_CAP 4/* capability register */ +#define PCI_TPH_CAP_LOC_MASK 0x600/* location mask */ +#define PCI_TPH_LOC_NONE 0x000/* no location */ +#define PCI_TPH_LOC_CAP 0x200/* in capability */ +#define PCI_TPH_LOC_MSIX 0x400/* in MSI-X */ +#define PCI_TPH_CAP_ST_MASK 0x07FF0000/* st table mask */ +#define PCI_TPH_CAP_ST_SHIFT 16/* st table shift */ +#define PCI_TPH_BASE_SIZEOF 12/* size with no st table */ +#endif /* LINUX_PCI_REGS_H */
PATCH 13/13 vfio: Add PCI device driver
In reply to Alex WilliamsonAdd PCI device support for VFIO. PCI devices expose regions for accessing config space, I/O port space, and MMIO areas of the device. PCI config access is virtualized in the kernel, allowing us to ensure the integrity of the system, by preventing various accesses while reducing duplicate support across various userspace drivers. I/O port supports read/write access while MMIO also supports mmap of sufficiently sized regions. Support for INTx, MSI, and MSI-X interrupts are provided using eventfds to userspace.
---
drivers/vfio/Kconfig | 2 drivers/vfio/pci/Kconfig | 8 drivers/vfio/pci/Makefile | 4 drivers/vfio/pci/vfio_pci.c | 557 +++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 1527 +++++++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_intrs.c | 724 +++++++++++++++++ drivers/vfio/pci/vfio_pci_private.h | 91 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 267 ++++++ include/linux/vfio.h | 26 + 9 files changed, 3206 insertions(+), 0 deletions(-) create mode 100644 drivers/vfio/pci/Kconfig create mode 100644 drivers/vfio/pci/Makefile create mode 100644 drivers/vfio/pci/vfio_pci.c create mode 100644 drivers/vfio/pci/vfio_pci_config.c create mode 100644 drivers/vfio/pci/vfio_pci_intrs.c create mode 100644 drivers/vfio/pci/vfio_pci_private.h create mode 100644 drivers/vfio/pci/vfio_pci_rdwr.c
drivers/vfio/Kconfig +2 -0
@@ -12,3 +12,5 @@ menuconfig VFIO See Documentation/vfio.txt for more details. If you don't know what to do here, say N. + +source "drivers/vfio/pci/Kconfig"
drivers/vfio/pci/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +config VFIO_PCI + tristate "VFIO support for PCI devices" + depends on VFIO && PCI + help + Support for the PCI VFIO bus driver. This is required to make + use of PCI drivers using the VFIO framework. + + If you don't know what to do here, say N.
drivers/vfio/pci/Makefile new file +4 -0
@@ -0,0 +1,4 @@ + +vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o + +obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
drivers/vfio/pci/vfio_pci.c new file +557 -0
557 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_config.c new file +1527 -0
1,527 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_intrs.c new file +724 -0
724 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_private.h new file +91 -0
91 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_rdwr.c new file +267 -0
84 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
Greg KH
maintainerRe: [PATCH 01/13] driver core: Add iommu_group tracking to struct device
In reply to Alex WilliamsonQuoted 7 lines · Alex Williamson
IOMMU groups allow IOMMU drivers to represent DMA visibility and isolation of devices. Multiple devices may be grouped together for the purposes of DMA. Placing a pointer on struct device enable easy access for things like streaming DMA programming and drivers like VFIO.
Signed-off-by Alex Williamson <[email protected]>
Can't you get this today from the iommu_ops pointer that is on the bus that the device is associated with? Or can devices on a bus have different iommu_group pointers?
thanks,
greg k-h
Greg KH
maintainerRe: [PATCH 02/13] iommu: IOMMU Groups
In reply to Alex WilliamsonQuoted 30 lines · Alex Williamson
IOMMU device groups are currently a rather vague associative notion with assembly required by the user or user level driver provider to do anything useful. This patch intends to grow the IOMMU group concept into something a bit more consumable.
To do this, we first create an object representing the group, struct
iommu_group. This structure is allocated (iommu_group_alloc) and filled (iommu_group_add_device) by the iommu driver. The iommu driver is free to add devices to the group using it's own set of policies. This allows inclusion of devices based on physical hardware or topology limitations of the platform, as well as soft requirements, such as multi-function trust levels or peer-to-peer protection of the interconnects. Each device may only belong to a single iommu group, which is linked from struct device.iommu_group. IOMMU groups are maintained using kobject reference counting, allowing for automatic removal of empty, unreferenced groups. It is the responsibility of the iommu driver to remove devices from the group (iommu_group_remove_device).IOMMU groups also include a userspace representation in sysfs under
/sys/kernel/iommu_groups. When allocated, each group is given a dynamically assign ID (int). The ID is managed by the core IOMMU group code to support multiple heterogeneous iommu drivers, which could potentially collide in group naming/numbering. This also keeps group IDs to small, easily managed values. A directory is created under/sys/kernel/iommu_groupsfor each group. A further subdirectory named "devices" contains links to each device within the group. Theiommu_groupfile in the device's sysfs directory, which formerly contained a group number when read, is now a link to the iommu group. Example:
Quoted 1 line
$ ls -l /sys/kernel/iommu_groups/26/devices/
<snip>
As you are creating new sysfs files and directories, you need to also add the proper Documentation/ABI/ files at the same time.
thanks,
greg k-h
Re: [PATCH 01/13] driver core: Add iommu_group tracking to struct device
In reply to Greg KHQuoted 7 lines · Greg KH
Quoted 6 lines · Alex Williamson
IOMMU groups allow IOMMU drivers to represent DMA visibility and isolation of devices. Multiple devices may be grouped together for the purposes of DMA. Placing a pointer on struct device enable easy access for things like streaming DMA programming and drivers like VFIO.
Quoted 5 lines
Quoted 1 line
Signed-off-by Alex Williamson <[email protected]>Can't you get this today from the
iommu_opspointer that is on the bus that the device is associated with? Or can devices on a bus have differentiommu_grouppointers?
The latter, each device on a bus might be it's own group. This is often the case on x86 unless PCIe-to-PCI bridges obscure the device visibility. Thanks,
Alex
Re: [PATCH 02/13] iommu: IOMMU Groups
In reply to Greg KHQuoted 31 lines · Greg KH
Quoted 30 lines · Alex Williamson
IOMMU device groups are currently a rather vague associative notion with assembly required by the user or user level driver provider to do anything useful. This patch intends to grow the IOMMU group concept into something a bit more consumable.
To do this, we first create an object representing the group, struct
iommu_group. This structure is allocated (iommu_group_alloc) and filled (iommu_group_add_device) by the iommu driver. The iommu driver is free to add devices to the group using it's own set of policies. This allows inclusion of devices based on physical hardware or topology limitations of the platform, as well as soft requirements, such as multi-function trust levels or peer-to-peer protection of the interconnects. Each device may only belong to a single iommu group, which is linked from struct device.iommu_group. IOMMU groups are maintained using kobject reference counting, allowing for automatic removal of empty, unreferenced groups. It is the responsibility of the iommu driver to remove devices from the group (iommu_group_remove_device).IOMMU groups also include a userspace representation in sysfs under
/sys/kernel/iommu_groups. When allocated, each group is given a dynamically assign ID (int). The ID is managed by the core IOMMU group code to support multiple heterogeneous iommu drivers, which could potentially collide in group naming/numbering. This also keeps group IDs to small, easily managed values. A directory is created under/sys/kernel/iommu_groupsfor each group. A further subdirectory named "devices" contains links to each device within the group. Theiommu_groupfile in the device's sysfs directory, which formerly contained a group number when read, is now a link to the iommu group. Example:
Quoted 6 lines
Quoted 1 line
$ ls -l /sys/kernel/iommu_groups/26/devices/<snip>
As you are creating new sysfs files and directories, you need to also add the proper
Documentation/ABI/files at the same time.
I'll update. Thanks,
Alex
Re: [PATCH 01/13] driver core: Add iommu_group tracking to struct device
In reply to Alex WilliamsonQuoted 10 lines · Alex Williamson
Quoted 9 lines · Greg KH
Quoted 7 lines · Alex Williamson
IOMMU groups allow IOMMU drivers to represent DMA visibility and isolation of devices. Multiple devices may be grouped together for the purposes of DMA. Placing a pointer on struct device enable easy access for things like streaming DMA programming and drivers like VFIO.
Signed-off-by Alex Williamson <[email protected]>
Quoted 7 lines
Quoted 3 lines
Can't you get this today from the
iommu_opspointer that is on the bus that the device is associated with? Or can devices on a bus have differentiommu_grouppointers?The latter, each device on a bus might be it's own group. This is often the case on x86 unless PCIe-to-PCI bridges obscure the device visibility. Thanks,
Ah, ok, then I have no objection to add this to struct device:
Re: [PATCH 02/13] iommu: IOMMU Groups
In reply to Alex WilliamsonQuoted 79 lines · Alex Williamson
IOMMU device groups are currently a rather vague associative notion with assembly required by the user or user level driver provider to do anything useful. This patch intends to grow the IOMMU group concept into something a bit more consumable.
To do this, we first create an object representing the group, struct
iommu_group. This structure is allocated (iommu_group_alloc) and filled (iommu_group_add_device) by the iommu driver. The iommu driver is free to add devices to the group using it's own set of policies. This allows inclusion of devices based on physical hardware or topology limitations of the platform, as well as soft requirements, such as multi-function trust levels or peer-to-peer protection of the interconnects. Each device may only belong to a single iommu group, which is linked from struct device.iommu_group. IOMMU groups are maintained using kobject reference counting, allowing for automatic removal of empty, unreferenced groups. It is the responsibility of the iommu driver to remove devices from the group (iommu_group_remove_device).IOMMU groups also include a userspace representation in sysfs under
/sys/kernel/iommu_groups. When allocated, each group is given a dynamically assign ID (int). The ID is managed by the core IOMMU group code to support multiple heterogeneous iommu drivers, which could potentially collide in group naming/numbering. This also keeps group IDs to small, easily managed values. A directory is created under/sys/kernel/iommu_groupsfor each group. A further subdirectory named "devices" contains links to each device within the group. Theiommu_groupfile in the device's sysfs directory, which formerly contained a group number when read, is now a link to the iommu group. Example:$ ls -l /sys/kernel/iommu_groups/26/devices/ total 0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:00:1e.0 -> ../../../../devices/pci0000:00/0000:00:1e.0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:06:0d.0 -> ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:06:0d.1 -> ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.1$ ls -l /sys/kernel/iommu_groups/26/devices/*/iommu_group [truncating perms/owner/timestamp] /sys/kernel/iommu_groups/26/devices/0000:00:1e.0/iommu_group -> ../../../kernel/iommu_groups/26 /sys/kernel/iommu_groups/26/devices/0000:06:0d.0/iommu_group -> ../../../../kernel/iommu_groups/26 /sys/kernel/iommu_groups/26/devices/0000:06:0d.1/iommu_group -> ../../../../kernel/iommu_groups/26Groups also include several exported functions for use by user level driver providers, for example VFIO. These include:
iommu_group_get(): Acquires a reference to a group from a deviceiommu_group_put(): Releases referenceiommu_group_for_each_dev(): Iterates over group devices using callback iommu_group_[un]register_notifier(): Allows notification of device add
and remove operations relevant to the groupiommu_group_id(): Return the group numberThis patch also extends the IOMMU API to allow attaching groups to domains. This is currently a simple wrapper for iterating through devices within a group, but it's expected that the IOMMU API may eventually make groups a more integral part of domains.
Groups intentionally do not try to manage group ownership. A user level driver provider must independently acquire ownership for each device within a group before making use of the group as a whole. This may change in the future if group usage becomes more pervasive across both DMA and IOMMU ops.
Groups intentionally do not provide a mechanism for driver locking or otherwise manipulating driver matching/probing of devices within the group. Such interfaces are generic to devices and beyond the scope of IOMMU groups. If implemented, user level providers have ready access via
iommu_group_for_each_devand group notifiers.Groups currently provide no storage for iommu context, but some kind of iommu_group_get/set_iommudata() interface is likely if groups become more pervasive in the dma layers.
Quoted 6 lines
iommu_device_group()is removed here as it has no users. The replacement is:group = iommu_group_get(dev); id = iommu_group_id(group); iommu_group_put(group);
Looks reasonable to me, with a few nits, noted below.
[snip]
Quoted 12 lines
drivers/iommu/iommu.c +4
-0@@ -26,60 +26,404 @@#include <linux/slab.h> #include <linux/errno.h> #include <linux/iommu.h> +#include <linux/idr.h> +#include <linux/notifier.h> + +static struct kset *iommu_group_kset;
drivers/iommu/iommu.c line 42
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+static struct ida iommu_group_ida; +static struct mutex iommu_group_mutex; + +struct iommu_group { +struct kobject kobj; +struct kobject *devices_kobj; +struct list_head devices; +struct mutex mutex; +struct blocking_notifier_head notifier; +int id;
I think you should add some sort of name string to the group as well (supplied by the iommu driver creating the group). That would make it easier to connect the arbitrary assigned IDs to any platform-standard naming convention for these things.
[snip]
Quoted 22 lines
drivers/iommu/iommu.c +22
-0+/** +* iommu_group_add_device - add a device to an iommu group +* @group: the group into which to add the device (reference should be held) +* @dev: the device +* +* This function is called by an iommu driver to add a device into a +* group. Adding a device increments the group reference count. +*/ +int iommu_group_add_device (struct iommu_group *group,struct device *dev) +{ +int ret; +struct iommu_device *device; + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) +return -ENOMEM ; + + device->dev = dev; + + ret =sysfs_create_link (&dev->kobj, &group->kobj,"iommu_group" ); +if (ret) { +kfree (device);
drivers/iommu/iommu.c line 204
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+return ret; + } + + ret =sysfs_create_link (group->devices_kobj, &dev->kobj, +kobject_name (&dev->kobj)); +if (ret) { +sysfs_remove_link (&dev->kobj,"iommu_group" ); +kfree (device); +return ret; + }
So, it's not clear that the kobject_name() here has to be unique across all devices in the group. It might be better to use an arbitrary index here instead of a name to avoid that problem.
[snip]
Quoted 20 lines
drivers/iommu/iommu.c +20
-0+/** +* iommu_group_remove_device - remove a device from it's current group +* @dev: device to be removed +* +* This function is called by an iommu driver to remove the device from +* it's current group. This decrements the iommu group reference count. +*/ +void iommu_group_remove_device (struct device *dev) +{ +struct iommu_group *group = dev->iommu_group; +struct iommu_device *device; + +/* Pre-notify listeners that a device is being removed. */ +blocking_notifier_call_chain (&group->notifier, +IOMMU_GROUP_NOTIFY_DEL_DEVICE , dev); + +mutex_lock (&group->mutex); +list_for_each_entry (device, &group->devices, list) { +if (device->dev == dev) { +list_del (&device->list);
drivers/iommu/iommu.c line 249
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+kfree (device); +break ; + } + } +mutex_unlock (&group->mutex); + +sysfs_remove_link (group->devices_kobj,kobject_name (&dev->kobj)); +sysfs_remove_link (&dev->kobj,"iommu_group" ); + + dev->iommu_group =NULL ;
I suspect the dev -> group pointer should be cleared first, under the group lock, but I'm not certain about that.
[snip]
Quoted 9 lines
drivers/iommu/iommu.c +8
-0+/** +* iommu_group_for_each_dev - iterate over each device in the group +* @group: the group +* @data: caller opaque data to be passed to callback function +* @fn: caller supplied callback function +* +* This function is called by group users to iterate over group devices. +* Callers should hold a reference count to the group during callback.
Probably also worth noting in this doco that the group lock will be held across the callback.
[snip]
Quoted 11 lines
drivers/iommu/iommu.c +9
-0+static int iommu_bus_notifier (struct notifier_block *nb, +unsigned long action,void *data) {struct device *dev = data; +struct iommu_ops *ops = dev->bus->iommu_ops; +struct iommu_group *group; +unsigned long group_action =0 ; + +/* +* ADD/DEL call into iommu driver ops if provided, which may +* result in ADD/DEL notifiers to group->notifier
drivers/iommu/iommu.c line 386
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+*/ +if (action ==BUS_NOTIFY_ADD_DEVICE ) { +if (ops->add_device) +return ops->add_device (dev); + }else if (action ==BUS_NOTIFY_DEL_DEVICE ) { +if (ops->remove_device && dev->iommu_group) { + ops->remove_device (dev); +return 0 ; + } + }
So, there's still the question of how to assign grouping for devices on a subordinate bus behind a bridge which is iommu managed. The iommu driver for the top-level bus can't know about all possible subordinate bus types, but the subordinate devices will (in most cases, anyway) be iommu translated as if originating with the bus bridge.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH 02/13] iommu: IOMMU Groups
Quoted 109 lines · David Gibson
Quoted 79 lines · Alex Williamson
IOMMU device groups are currently a rather vague associative notion with assembly required by the user or user level driver provider to do anything useful. This patch intends to grow the IOMMU group concept into something a bit more consumable.
To do this, we first create an object representing the group, struct
iommu_group. This structure is allocated (iommu_group_alloc) and filled (iommu_group_add_device) by the iommu driver. The iommu driver is free to add devices to the group using it's own set of policies. This allows inclusion of devices based on physical hardware or topology limitations of the platform, as well as soft requirements, such as multi-function trust levels or peer-to-peer protection of the interconnects. Each device may only belong to a single iommu group, which is linked from struct device.iommu_group. IOMMU groups are maintained using kobject reference counting, allowing for automatic removal of empty, unreferenced groups. It is the responsibility of the iommu driver to remove devices from the group (iommu_group_remove_device).IOMMU groups also include a userspace representation in sysfs under
/sys/kernel/iommu_groups. When allocated, each group is given a dynamically assign ID (int). The ID is managed by the core IOMMU group code to support multiple heterogeneous iommu drivers, which could potentially collide in group naming/numbering. This also keeps group IDs to small, easily managed values. A directory is created under/sys/kernel/iommu_groupsfor each group. A further subdirectory named "devices" contains links to each device within the group. Theiommu_groupfile in the device's sysfs directory, which formerly contained a group number when read, is now a link to the iommu group. Example:$ ls -l /sys/kernel/iommu_groups/26/devices/ total 0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:00:1e.0 -> ../../../../devices/pci0000:00/0000:00:1e.0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:06:0d.0 -> ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.0 lrwxrwxrwx. 1 root root 0 Apr 17 12:57 0000:06:0d.1 -> ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.1$ ls -l /sys/kernel/iommu_groups/26/devices/*/iommu_group [truncating perms/owner/timestamp] /sys/kernel/iommu_groups/26/devices/0000:00:1e.0/iommu_group -> ../../../kernel/iommu_groups/26 /sys/kernel/iommu_groups/26/devices/0000:06:0d.0/iommu_group -> ../../../../kernel/iommu_groups/26 /sys/kernel/iommu_groups/26/devices/0000:06:0d.1/iommu_group -> ../../../../kernel/iommu_groups/26Groups also include several exported functions for use by user level driver providers, for example VFIO. These include:
iommu_group_get(): Acquires a reference to a group from a deviceiommu_group_put(): Releases referenceiommu_group_for_each_dev(): Iterates over group devices using callback iommu_group_[un]register_notifier(): Allows notification of device add
and remove operations relevant to the groupiommu_group_id(): Return the group numberThis patch also extends the IOMMU API to allow attaching groups to domains. This is currently a simple wrapper for iterating through devices within a group, but it's expected that the IOMMU API may eventually make groups a more integral part of domains.
Groups intentionally do not try to manage group ownership. A user level driver provider must independently acquire ownership for each device within a group before making use of the group as a whole. This may change in the future if group usage becomes more pervasive across both DMA and IOMMU ops.
Groups intentionally do not provide a mechanism for driver locking or otherwise manipulating driver matching/probing of devices within the group. Such interfaces are generic to devices and beyond the scope of IOMMU groups. If implemented, user level providers have ready access via
iommu_group_for_each_devand group notifiers.Groups currently provide no storage for iommu context, but some kind of iommu_group_get/set_iommudata() interface is likely if groups become more pervasive in the dma layers.
Quoted 6 lines
iommu_device_group()is removed here as it has no users. The replacement is:group = iommu_group_get(dev); id = iommu_group_id(group); iommu_group_put(group);Looks reasonable to me, with a few nits, noted below.
[snip]
Quoted 19 lines
drivers/iommu/iommu.c +11
-0@@ -26,60 +26,404 @@#include <linux/slab.h> #include <linux/errno.h> #include <linux/iommu.h> +#include <linux/idr.h> +#include <linux/notifier.h> + +static struct kset *iommu_group_kset; +static struct ida iommu_group_ida; +static struct mutex iommu_group_mutex; + +struct iommu_group { +struct kobject kobj; +struct kobject *devices_kobj; +struct list_head devices;
Quoted 8 lines
drivers/iommu/iommu.c line 42
Quoted 3 lines
drivers/iommu/iommu.c +3
-0+struct mutex mutex; +struct blocking_notifier_head notifier; +int id;I think you should add some sort of name string to the group as well (supplied by the iommu driver creating the group). That would make it easier to connect the arbitrary assigned IDs to any platform-standard naming convention for these things.
When would the name be used and how is it exposed?
Quoted 29 lines
[snip]
Quoted 28 lines
drivers/iommu/iommu.c +28
-0+/** +* iommu_group_add_device - add a device to an iommu group +* @group: the group into which to add the device (reference should be held) +* @dev: the device +* +* This function is called by an iommu driver to add a device into a +* group. Adding a device increments the group reference count. +*/ +int iommu_group_add_device (struct iommu_group *group,struct device *dev) +{ +int ret; +struct iommu_device *device; + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) +return -ENOMEM ; + + device->dev = dev; + + ret =sysfs_create_link (&dev->kobj, &group->kobj,"iommu_group" ); +if (ret) { +kfree (device); +return ret; + } + + ret =sysfs_create_link (group->devices_kobj, &dev->kobj, +kobject_name (&dev->kobj)); +if (ret) {
Quoted 8 lines
drivers/iommu/iommu.c line 204
Quoted 4 lines
drivers/iommu/iommu.c +4
-0+sysfs_remove_link (&dev->kobj,"iommu_group" ); +kfree (device); +return ret; + }So, it's not clear that the
kobject_name()here has to be unique across all devices in the group. It might be better to use an arbitrary index here instead of a name to avoid that problem.
Hmm, that loses useful convenience when they are unique, such as on PCI. I'll look and see if sysfs_create_link will fail on duplicate names and see about adding some kind of instance to it.
Quoted 26 lines
[snip]
Quoted 25 lines
drivers/iommu/iommu.c +25
-0+/** +* iommu_group_remove_device - remove a device from it's current group +* @dev: device to be removed +* +* This function is called by an iommu driver to remove the device from +* it's current group. This decrements the iommu group reference count. +*/ +void iommu_group_remove_device (struct device *dev) +{ +struct iommu_group *group = dev->iommu_group; +struct iommu_device *device; + +/* Pre-notify listeners that a device is being removed. */ +blocking_notifier_call_chain (&group->notifier, +IOMMU_GROUP_NOTIFY_DEL_DEVICE , dev); + +mutex_lock (&group->mutex); +list_for_each_entry (device, &group->devices, list) { +if (device->dev == dev) { +list_del (&device->list); +kfree (device); +break ; + } + } +mutex_unlock (&group->mutex);
Quoted 8 lines
drivers/iommu/iommu.c line 249
Quoted 5 lines
drivers/iommu/iommu.c +5
-0+ +sysfs_remove_link (group->devices_kobj,kobject_name (&dev->kobj)); +sysfs_remove_link (&dev->kobj,"iommu_group" ); + + dev->iommu_group =NULL ;I suspect the dev -> group pointer should be cleared first, under the group lock, but I'm not certain about that.
group->mutex is protecting the group's device list. I think my assumption is that when a device is being removed, there should be no references to it for anyone to race with iommu_group_get(dev), but I'm not sure how valid that is.
Quoted 11 lines
[snip]
Quoted 9 lines
+/**
+ *iommu_group_for_each_dev- iterate over each device in the group
+ * @group: the group
+ * @data: caller opaque data to be passed to callback function
+ * @fn: caller supplied callback function
+ *
+ * This function is called by group users to iterate over group devices.
+ * Callers should hold a reference count to the group during callback.
Quoted 2 lines
Probably also worth noting in this doco that the group lock will be held across the callback.
Yes; calling iommu_group_remove_device through this would be a bad idea.
Quoted 21 lines
[snip]
Quoted 20 lines
drivers/iommu/iommu.c +18
-0+static int iommu_bus_notifier (struct notifier_block *nb, +unsigned long action,void *data) {struct device *dev = data; +struct iommu_ops *ops = dev->bus->iommu_ops; +struct iommu_group *group; +unsigned long group_action =0 ; + +/* +* ADD/DEL call into iommu driver ops if provided, which may +* result in ADD/DEL notifiers to group->notifier +*/ +if (action ==BUS_NOTIFY_ADD_DEVICE ) { +if (ops->add_device) +return ops->add_device (dev); + }else if (action ==BUS_NOTIFY_DEL_DEVICE ) { +if (ops->remove_device && dev->iommu_group) { + ops->remove_device (dev); +return 0 ; + }
Quoted 8 lines
drivers/iommu/iommu.c
Quoted 1 line
drivers/iommu/iommu.c +1
-0+ }So, there's still the question of how to assign grouping for devices on a subordinate bus behind a bridge which is iommu managed. The iommu driver for the top-level bus can't know about all possible subordinate bus types, but the subordinate devices will (in most cases, anyway) be iommu translated as if originating with the bus bridge.
Not just any bridge, there has to be a different bus_type on the subordinate side. Things like PCI-to-PCI work as is, but a PCI-to-ISA would trigger this. In general, I don't know how to handle it and I'm open to suggestions. Perhaps we need to register notifiers for every bus_type and create notifiers for new bus_types. If we can catch the ADD_DEVICE for it, we can walk up to the first parent bus that supports an iommu_ops and add the device there. But then we need some kind of foreign bus support for the group since the iommu driver won't know what to do with the device if we call add_device with it. Other ideas? Thanks,
Alex
Re: [PATCH 10/13] pci: export pci_user functions for use by other drivers
In reply to Alex WilliamsonQuoted 5 lines
include/linux/pci.h +0
-0
include/linux/pci.h static inline int pci_write_config_dword(const struct pci_dev *dev, int where, · line 779
Quoted 10 lines
include/linux/pci.h +7
-0@@ -770,6 +770,14 @@ static inline int pci_write_config_dword(const struct pci_dev *dev, int where, return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val); } +/* user-space driven config access */ +extern int pci_user_read_config_byte (struct pci_dev *dev,int where,u8 *val); +extern int pci_user_read_config_word (struct pci_dev *dev,int where,u16 *val); +extern int pci_user_read_config_dword (struct pci_dev *dev,int where,u32 *val); +extern int pci_user_write_config_byte (struct pci_dev *dev,int where,u8 val); +extern int pci_user_write_config_word (struct pci_dev *dev,int where,u16 val); +extern int pci_user_write_config_dword (struct pci_dev *dev,int where,u32 val);
If you repost this, can you remove the externs when you move these declarations? I know the file's currently a random mix, but we might as well make a tiny improvement.
Looks fine to me otherwise, and if you don't have any other reason to update this series, don't worry about it.
Quoted 4 lines
include/linux/pci.h +0
-0int __must_check pci_enable_device(struct pci_dev *dev); int __must_check pci_enable_device_io(struct pci_dev *dev); int __must_check pci_enable_device_mem(struct pci_dev *dev);
Bjorn Helgaas
maintainerRe: [PATCH 05/13] pci: New pci_acs_enabled()
In reply to Alex WilliamsonQuoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.
The name "pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Quoted 74 lines
Signed-off-by Alex Williamson <[email protected]>---
drivers/pci/pci.c | 43 +++++++++++++++++++++++++++++++++++++++++++
include/linux/pci.h| 1 +2 files changed, 44 insertions(+), 0 deletions(-)drivers/pci/pci.c +43
-0@@ -2358,6 +2358,49 @@ void pci_enable_acs(struct pci_dev *dev) pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl); } +#define PCI_EXT_CAP_ACS_ENABLED (PCI_ACS_SV | PCI_ACS_RR | \ + PCI_ACS_CR | PCI_ACS_UF) + +/** +* pci_acs_enabled - test ACS support in downstream chain +* @dev: starting PCI device +* +* Returns the furthest downstream device with an unbroken ACS chain. If +* ACS is enabled throughout the chain, the returned device is the same as +* the one passed in. +*/ +struct pci_dev *pci_acs_enabled (struct pci_dev *dev) +{ + struct pci_dev *acs_dev; + int pos; + u16 ctrl; + + if (!pci_is_root_bus(dev->bus)) + acs_dev = pci_acs_enabled(dev->bus->self); + else + return dev; + + /* If the chain is already broken, pass on the device */ + if (acs_dev != dev->bus->self) + return acs_dev; + + if (!pci_is_pcie(dev) || (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) + return dev; + + if (dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) + return dev; + + pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS); + if (!pos) + return acs_dev; + + pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl); + if ((ctrl & PCI_EXT_CAP_ACS_ENABLED) != PCI_EXT_CAP_ACS_ENABLED) + return acs_dev; + + return dev; +} + /*** pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge * @dev: the PCI device include/linux/pci.h +1
-0@@ -1586,6 +1586,7 @@ static inline bool pci_is_pcie(struct pci_dev *dev) } void pci_request_acs(void); +struct pci_dev *pci_acs_enabled (struct pci_dev *dev); #define PCI_VPD_LRDT 0x80 /* Large Resource Data Type */
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.
Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.
Right, maybe this should be:
struct pci_dev *pci_find_upstream_acs(struct pci_dev *pdev);
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation, so we're looking for the furthest upstream device for which acs is enabled all the way up to the root bus. I'll fix the function name and comments/commit log if that makes it sufficiently clear. Thanks,
Alex
Quoted 78 lines
Quoted 67 lines
Signed-off-by Alex Williamson <[email protected]>---
drivers/pci/pci.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 1 + 2 files changed, 44 insertions(+), 0 deletions(-)drivers/pci/pci.c +43
-0@@ -2358,6 +2358,49 @@ void pci_enable_acs(struct pci_dev *dev)pci_write_config_word (dev, pos +PCI_ACS_CTRL , ctrl); } +#define PCI_EXT_CAP_ACS_ENABLED (PCI_ACS_SV | PCI_ACS_RR | \ + PCI_ACS_CR | PCI_ACS_UF) + +/** +* pci_acs_enabled - test ACS support in downstream chain +* @dev: starting PCI device +* +* Returns the furthest downstream device with an unbroken ACS chain. If +* ACS is enabled throughout the chain, the returned device is the same as +* the one passed in. +*/ +struct pci_dev *pci_acs_enabled (struct pci_dev *dev) +{ +struct pci_dev *acs_dev; +int pos; +u16 ctrl; + +if (!pci_is_root_bus (dev->bus)) + acs_dev =pci_acs_enabled (dev->bus->self); +else +return dev; + +/* If the chain is already broken, pass on the device */ +if (acs_dev != dev->bus->self) +return acs_dev; + +if (!pci_is_pcie (dev) || (dev->class >>8 ) !=PCI_CLASS_BRIDGE_PCI ) +return dev; + +if (dev->pcie_type !=PCI_EXP_TYPE_DOWNSTREAM ) +return dev; + + pos =pci_find_ext_capability (dev,PCI_EXT_CAP_ID_ACS ); +if (!pos) +return acs_dev; + +pci_read_config_word (dev, pos +PCI_ACS_CTRL , &ctrl); +if ((ctrl &PCI_EXT_CAP_ACS_ENABLED ) !=PCI_EXT_CAP_ACS_ENABLED ) +return acs_dev; + +return dev; +} +/** * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge * @dev: the PCI device include/linux/pci.h +0
-0include/linux/pci.h static inline bool pci_is_pcie(struct pci_dev *dev) · line 1592
Quoted 7 lines
include/linux/pci.h +1
-0@@ -1586,6 +1586,7 @@ static inline bool pci_is_pcie(struct pci_dev *dev) }void pci_request_acs (void ); +struct pci_dev *pci_acs_enabled (struct pci_dev *dev);#define PCI_VPD_LRDT 0x80/* Large Resource Data Type */ --
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 02/13] iommu: IOMMU Groups
In reply to Alex WilliamsonQuoted 2 lines · Alex Williamson
Quoted 1 line · David Gibson
On Fri, May 11, 2012 at 04:55:41PM -0600, Alex Williamson wrote:
[snip]
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/iommu/iommu.c +4
-0+struct iommu_group { +struct kobject kobj; +struct kobject *devices_kobj; +struct list_head devices;Quoted 8 lines
drivers/iommu/iommu.c line 42
Quoted 3 lines
drivers/iommu/iommu.c +3
-0+struct mutex mutex; +struct blocking_notifier_head notifier; +int id;I think you should add some sort of name string to the group as well (supplied by the iommu driver creating the group). That would make it easier to connect the arbitrary assigned IDs to any platform-standard naming convention for these things.
Quoted 1 line
When would the name be used and how is it exposed?
I'm thinking of this basically as a debugging aid. So I'd expect it to appear in a 'name' (or 'description') sysfs property on the group, and in printk messages regarding the group.
[snip]
Quoted 7 lines
Quoted 3 lines
So, it's not clear that the
kobject_name()here has to be unique across all devices in the group. It might be better to use an arbitrary index here instead of a name to avoid that problem.Hmm, that loses useful convenience when they are unique, such as on PCI. I'll look and see if
sysfs_create_linkwill fail on duplicate names and see about adding some kind of instance to it.
Ok. Is the name necessarily unique even for PCI, if the group crosses multiple domains?
[snip]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/iommu/iommu.c +9
-0+mutex_lock (&group->mutex); +list_for_each_entry (device, &group->devices, list) { +if (device->dev == dev) { +list_del (&device->list); +kfree (device); +break ; + } + } +mutex_unlock (&group->mutex);Quoted 8 lines
drivers/iommu/iommu.c line 249
Quoted 5 lines
drivers/iommu/iommu.c +5
-0+ +sysfs_remove_link (group->devices_kobj,kobject_name (&dev->kobj)); +sysfs_remove_link (&dev->kobj,"iommu_group" ); + + dev->iommu_group =NULL ;I suspect the dev -> group pointer should be cleared first, under the group lock, but I'm not certain about that.
Quoted 4 lines
group->mutexis protecting the group's device list. I think my assumption is that when a device is being removed, there should be no references to it for anyone to race withiommu_group_get(dev), but I'm not sure how valid that is.
What I'm concerned about here is someone grabbing the device by non-group-related means, grabbing a pointer to its group and that racing with remove_device(). It would then end up with a group pointer it thinks is right for the device, when the group no longer thinks it owns the device.
Doing it under the lock is so that on the other side, group aware code doesn't traverse the group member list and grab a reference to a device which no longer points back to the group.
Quoted 11 lines
Quoted 11 lines
[snip]
Quoted 9 lines
+/**
+ *iommu_group_for_each_dev- iterate over each device in the group
+ * @group: the group
+ * @data: caller opaque data to be passed to callback function
+ * @fn: caller supplied callback function
+ *
+ * This function is called by group users to iterate over group devices.
+ * Callers should hold a reference count to the group during callback.
Quoted 4 lines
Quoted 2 lines
Probably also worth noting in this doco that the group lock will be held across the callback.
Yes; calling
iommu_group_remove_devicethrough this would be a bad idea.
Or anything which blocks.
Quoted 30 lines
Quoted 21 lines
[snip]
Quoted 20 lines
drivers/iommu/iommu.c +18
-0+static int iommu_bus_notifier (struct notifier_block *nb, +unsigned long action,void *data) {struct device *dev = data; +struct iommu_ops *ops = dev->bus->iommu_ops; +struct iommu_group *group; +unsigned long group_action =0 ; + +/* +* ADD/DEL call into iommu driver ops if provided, which may +* result in ADD/DEL notifiers to group->notifier +*/ +if (action ==BUS_NOTIFY_ADD_DEVICE ) { +if (ops->add_device) +return ops->add_device (dev); + }else if (action ==BUS_NOTIFY_DEL_DEVICE ) { +if (ops->remove_device && dev->iommu_group) { + ops->remove_device (dev); +return 0 ; + }Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ }So, there's still the question of how to assign grouping for devices on a subordinate bus behind a bridge which is iommu managed. The iommu driver for the top-level bus can't know about all possible subordinate bus types, but the subordinate devices will (in most cases, anyway) be iommu translated as if originating with the bus bridge.
Quoted 3 lines
Not just any bridge, there has to be a different
bus_typeon the subordinate side. Things like PCI-to-PCI work as is, but a PCI-to-ISA would trigger this.
Right, although ISA-under-PCI is a bit of a special case anyway. I think PCI to Firewire/IEEE1394 would also have this issue, as would SoC-bus-to-PCI for a SoC which had an IOMMU at the SoC bus level. And virtual struct devices where the PCI driver is structured as a wrapper around a "vanilla" device driver, a pattern used in a number of drivers for chips with both PCI and non PCI variants.
Quoted 3 lines
In general, I don't know how to handle it and I'm
open to suggestions. Perhaps we need to register notifiers for everybus_typeand create notifiers for newbus_types.
I think we do need to do something like that and basically ensure that if a new device is not explicitly claimed by an IOMMU driver, it inherits its group from its parent bridge.
Quoted 5 lines
If we can catch the
ADD_DEVICEfor it, we can walk up to the first parent bus that supports aniommu_opsand add the device there. But then we need some kind of foreign bus support for the group since the iommu driver won't know what to do with the device if we calladd_devicewith it. Other ideas?
Hrm, yeah. We may need a distinction between primary group members (i.e. explicitly claimed by the IOMMU driver) and subordinate group members (devices which are descendents of a primary group member).
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH 02/13] iommu: IOMMU Groups
Quoted 17 lines · David Gibson
Quoted 2 lines · Alex Williamson
Quoted 1 line · David Gibson
On Fri, May 11, 2012 at 04:55:41PM -0600, Alex Williamson wrote:
[snip]
Quoted 13 lines
Quoted 12 lines
drivers/iommu/iommu.c line 42
Quoted 7 lines
drivers/iommu/iommu.c +7
-0+struct iommu_group { +struct kobject kobj; +struct kobject *devices_kobj; +struct list_head devices; +struct mutex mutex; +struct blocking_notifier_head notifier; +int id;I think you should add some sort of name string to the group as well (supplied by the iommu driver creating the group). That would make it easier to connect the arbitrary assigned IDs to any platform-standard naming convention for these things.
Quoted 5 lines
Quoted 1 line
When would the name be used and how is it exposed?
I'm thinking of this basically as a debugging aid. So I'd expect it to appear in a 'name' (or 'description') sysfs property on the group, and in printk messages regarding the group.
Ok, so long as it's only descriptive/debugging I don't have a problem adding something like that.
Quoted 11 lines
[snip]
Quoted 7 lines
Quoted 3 lines
So, it's not clear that the
kobject_name()here has to be unique across all devices in the group. It might be better to use an arbitrary index here instead of a name to avoid that problem.Hmm, that loses useful convenience when they are unique, such as on PCI. I'll look and see if
sysfs_create_linkwill fail on duplicate names and see about adding some kind of instance to it.Ok. Is the name necessarily unique even for PCI, if the group crosses multiple domains?
Yes, it includes the domain in the dddd:bb:dd.f form. I've found I can just use sysfs_create_link_nowarn and add a .# index when we have a name collision.
Quoted 30 lines
[snip]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/iommu/iommu.c +9
-0+mutex_lock (&group->mutex); +list_for_each_entry (device, &group->devices, list) { +if (device->dev == dev) { +list_del (&device->list); +kfree (device); +break ; + } + } +mutex_unlock (&group->mutex);Quoted 8 lines
drivers/iommu/iommu.c line 249
Quoted 5 lines
drivers/iommu/iommu.c +5
-0+ +sysfs_remove_link (group->devices_kobj,kobject_name (&dev->kobj)); +sysfs_remove_link (&dev->kobj,"iommu_group" ); + + dev->iommu_group =NULL ;I suspect the dev -> group pointer should be cleared first, under the group lock, but I'm not certain about that.
Quoted 4 lines
group->mutexis protecting the group's device list. I think my assumption is that when a device is being removed, there should be no references to it for anyone to race withiommu_group_get(dev), but I'm not sure how valid that is.What I'm concerned about here is someone grabbing the device by non-group-related means, grabbing a pointer to its group and that racing with
remove_device(). It would then end up with a group pointer it thinks is right for the device, when the group no longer thinks it owns the device.
Quoted 3 lines
Doing it under the lock is so that on the other side, group aware code doesn't traverse the group member list and grab a reference to a device which no longer points back to the group.
Our for_each function does grab the lock, as you noticed below, so removing it from the list under lock prevents that path. Where it gets fuzzy is if someone can call iommu_group_get(dev) to get a group reference in this gap. Whether we clear the iommu_group pointer under lock or not doesn't matter for that path since it doesn't retrieve it under lock. The assumption there is that the caller is going to have a reference to the device and therefore the device is not being removed. The asynchronous locking and reference counting is by far the hardest part of iommu_groups and vfio core, so appreciate any hard analysis of that.
Quoted 52 lines
Quoted 11 lines
Quoted 11 lines
[snip]
Quoted 9 lines
+/**
+ *iommu_group_for_each_dev- iterate over each device in the group
+ * @group: the group
+ * @data: caller opaque data to be passed to callback function
+ * @fn: caller supplied callback function
+ *
+ * This function is called by group users to iterate over group devices.
+ * Callers should hold a reference count to the group during callback.Quoted 4 lines
Quoted 2 lines
Probably also worth noting in this doco that the group lock will be held across the callback.
Yes; calling
iommu_group_remove_devicethrough this would be a bad idea.Or anything which blocks.
Quoted 30 lines
Quoted 21 lines
[snip]
Quoted 20 lines
drivers/iommu/iommu.c +18
-0+static int iommu_bus_notifier (struct notifier_block *nb, +unsigned long action,void *data) {struct device *dev = data; +struct iommu_ops *ops = dev->bus->iommu_ops; +struct iommu_group *group; +unsigned long group_action =0 ; + +/* +* ADD/DEL call into iommu driver ops if provided, which may +* result in ADD/DEL notifiers to group->notifier +*/ +if (action ==BUS_NOTIFY_ADD_DEVICE ) { +if (ops->add_device) +return ops->add_device (dev); + }else if (action ==BUS_NOTIFY_DEL_DEVICE ) { +if (ops->remove_device && dev->iommu_group) { + ops->remove_device (dev); +return 0 ; + }Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ }So, there's still the question of how to assign grouping for devices on a subordinate bus behind a bridge which is iommu managed. The iommu driver for the top-level bus can't know about all possible subordinate bus types, but the subordinate devices will (in most cases, anyway) be iommu translated as if originating with the bus bridge.
Quoted 3 lines
Not just any bridge, there has to be a different
bus_typeon the subordinate side. Things like PCI-to-PCI work as is, but a PCI-to-ISA would trigger this.
Quoted 6 lines
Right, although ISA-under-PCI is a bit of a special case anyway. I think PCI to Firewire/IEEE1394 would also have this issue, as would SoC-bus-to-PCI for a SoC which had an IOMMU at the SoC bus level. And virtual struct devices where the PCI driver is structured as a wrapper around a "vanilla" device driver, a pattern used in a number of drivers for chips with both PCI and non PCI variants.
Sorry, I jumped into reliving this issue without remembering how I decided to rationalize it for IOMMU groups. Let's step through it. Given DeviceA that's a member of GroupA and potentially sources a subordinate bus (A_bus_type) exposing DeviceA', what are the issues?
Quoted 1 line
From a VFIO perspective, GroupA isn't usable so long as DeviceA is
claimed by a non-VFIO driver. That same non-VFIO driver is the one causing DeviceA to source A_bus_type, so remove the driver and DeviceA' goes away and we can freely give GroupA to userspace. I believe this is always true; there are no subordinate buses to devices that meet the "viable" driver requirements of VFIO. I don't see any problems with the fact that userspace can then re-source A_bus_type and find DeviceA'. That's what should happen. If we want to assign just DeviceA' to userspace, well, it has no IOMMU group of it's own, so clearly it's not assignable on it's own.
For the more general IOMMU group case, I'm still struggling to figure out why this is an issue. If we were to do dma_ops via IOMMU groups, I don't think it's unreasonable that map_page would discover there's no iommu_ops on dev->bus (A_bus_type) and step up to dev->bus->self to find both iommu_group on DeviceA and iommu_ops on DeviceA->bus. Is there a practical reason why DeviceA' would need to be listed as a member of GroupA, or is it just an optimization? I know we had a number of discussions about these type of devices for isolation groups, but I think that trying to strictly represent these types of devices was also one of the downfalls of the isolation proposal.
This did make me think of one other generic quirk we might need. There's some funkiness with USB that makes me think that it's effectively a shared bus between 1.x and 2.0 controllers. So assigning the 1.x and 2.0 controllers to different groups potentially allows a fast and a slow path to the same set of devices. Is this true? If so, we probably need to quirk OHCI/UHCI and EHCI functions together when they're on the same PCI device. I think the PCI class code is sufficient to do this. Thanks,
Alex
Bjorn Helgaas
maintainerRe: [PATCH 05/13] pci: New pci_acs_enabled()
In reply to Alex WilliamsonQuoted 18 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^
|
+--------+-------+
| |
+------+-----+ +-----++-----+
| Downstream | | Downstream |
| Port | | Port |
| 06:05.0 | | 06:06.0 |
+------+-----+ +------+-----+
| |
+----v----+ +----v----+
| Endpoint| | Endpoint|
| 07:00.0 | | 08:00.0 |
+---------+ +---------+that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.
Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
I tried to work through some examples to develop some intuition about this:
|
+------------+----------+----------------------+
| | |
| |
+----------------|-------------------------------+
+----v----+ +----v----+ | +-----v----+
|
| 00:00.0 | | 00:01.0 | | | 00:02.0 |
|
| PCI | | PCIe-to | | | Upstream |
|
+---------+ | PCI | | +-----+----+
|
+----+----+ | |
|
| |
+---------+------+----------------+ |
+------+------+ | | |
| |
| | | +----v-----+ +----v-----+
+----v-----+ |
+----v----+ +----v----+ | | 02:00.0 | | 02:01.0 |
| 02:02.0 | |
| 01:00.0 | | 01:01.0 | | |Downstream| |Downstream|
|Downstream| |
| PCI | | PCI | | | w/o ACS | | w/ ACS |
| w/ ACS | |
+---------+ +---------+ | +-----+----+ +----+-----+
+----+-----+ |+-------|---------------|----------------|-------+
| | |
+----v----+ +----v----+
+----v----+
| 03:00.0 | | 04:00.0 |
| 05:00.0 |
| PCIe | | PCIe |
| PCIe |
+---------+ | w/o ACS |
| w/ ACS |
+---------+
+---------+pci_acs_enabled(00:00.0) = 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))pci_acs_enabled(00:01.0) = 00:01.0 (on root bus) pci_acs_enabled(01:00.0) = 01:00.0 (acs_dev = 00:01.0, 01:00.0 is not PCIe; seems wrong)pci_acs_enabled(00:02.0) = 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)pci_acs_enabled(02:00.0) = 00:02.0 (acs_dev = 00:02.0, 02:00.0 has no ACS cap) pci_acs_enabled(03:00.0) = 00:02.0 (acs_dev = 00:02.0) pci_acs_enabled(02:01.0) = 02:01.0 (acs_dev = 00:02.0, 02:01.0 has ACS enabled) pci_acs_enabled(04:00.0) = 04:00.0 (acs_dev = 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device) pci_acs_enabled(02:02.0) = 02:02.0 (acs_dev = 00:02.0, 02:02.0 has ACS enabled) pci_acs_enabled(05:00.0) = 05:00.0 (acs_dev = 02:02.0, 05:00.0 is not a bridge)
But it didn't really help. I still can't develop a mental picture of what this function does.
Quoted 81 lines
Quoted 78 lines
Quoted 67 lines
Signed-off-by Alex Williamson <[email protected]>---
drivers/pci/pci.c | 43 +++++++++++++++++++++++++++++++++++++++++++
include/linux/pci.h| 1 +2 files changed, 44 insertions(+), 0 deletions(-)drivers/pci/pci.c +43
-0@@ -2358,6 +2358,49 @@ void pci_enable_acs(struct pci_dev *dev) pci_write_config_word(dev, pos + PCI_ACS_CTRL, ctrl); } +#define PCI_EXT_CAP_ACS_ENABLED (PCI_ACS_SV | PCI_ACS_RR | \ + PCI_ACS_CR | PCI_ACS_UF) + +/** +* pci_acs_enabled - test ACS support in downstream chain +* @dev: starting PCI device +* +* Returns the furthest downstream device with an unbroken ACS chain. If +* ACS is enabled throughout the chain, the returned device is the same as +* the one passed in. +*/ +struct pci_dev *pci_acs_enabled (struct pci_dev *dev) +{ + struct pci_dev *acs_dev; + int pos; + u16 ctrl; + + if (!pci_is_root_bus(dev->bus)) + acs_dev = pci_acs_enabled(dev->bus->self); + else + return dev; + + /* If the chain is already broken, pass on the device */ + if (acs_dev != dev->bus->self) + return acs_dev; + + if (!pci_is_pcie(dev) || (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) + return dev; + + if (dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) + return dev; + + pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS); + if (!pos) + return acs_dev; + + pci_read_config_word(dev, pos + PCI_ACS_CTRL, &ctrl); + if ((ctrl & PCI_EXT_CAP_ACS_ENABLED) != PCI_EXT_CAP_ACS_ENABLED) + return acs_dev; + + return dev; +} + /*** pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge * @dev: the PCI device include/linux/pci.h +0
-0include/linux/pci.h static inline bool pci_is_pcie(struct pci_dev *dev) · line 1592
Quoted 7 lines
include/linux/pci.h +1
-0@@ -1586,6 +1586,7 @@ static inline bool pci_is_pcie(struct pci_dev *dev) } void pci_request_acs(void); +struct pci_dev *pci_acs_enabled (struct pci_dev *dev); #define PCI_VPD_LRDT 0x80 /* Large Resource Data Type */--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Bjorn Helgaas
maintainerRe: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
Sorry, gmail inserted line breaks that ruined this picture. Here's a URL for it:
Re: [PATCH 05/13] pci: New pci_acs_enabled()
In reply to Bjorn HelgaasQuoted 51 lines · Bjorn Helgaas
Quoted 18 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+
Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Yes, modulo this comment in libvirt source:
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))
Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)
Oops, I'm calling pci_find_upstream_pcie_bridge() first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.
Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct pci_dev parameters to make it more flexible where the iommu lives in the system.
Quoted 5 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 5 lines
pci_acs_enabled(02:02.0)= 02:02.0 (acs_dev= 00:02.0, 02:02.0 has ACS enabled)pci_acs_enabled(05:00.0)= 05:00.0 (acs_dev= 02:02.0, 05:00.0 is not a bridge)But it didn't really help. I still can't develop a mental picture of what this function does.
It helped me :) These are good examples, I'll work on fixing it for them. Thanks,
Alex
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 15 lines · Alex Williamson
Quoted 14 lines · Bjorn Helgaas
Quoted 12 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);
+1; there is a global in the PCI code, pci_acs_enable, and a function pci_enable_acs(), which the above name certainly confuses. I recommend pci_find_top_acs_bridge() would be most descriptive.
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.
Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Quoted 7 lines
Yes, modulo this comment in libvirt source:
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/
Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Quoted 31 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.
Quoted 9 lines
Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.
see comment above wrt root ports that have IOMMUs in them.
Quoted 11 lines
Quoted 5 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).
Quoted 16 lines
Quoted 5 lines
pci_acs_enabled(02:02.0)= 02:02.0 (acs_dev= 00:02.0, 02:02.0 has ACS enabled)pci_acs_enabled(05:00.0)= 05:00.0 (acs_dev= 02:02.0, 05:00.0 is not a bridge)But it didn't really help. I still can't develop a mental picture of what this function does.
It helped me :) These are good examples, I'll work on fixing it for them. Thanks,
Alex
_______________________________________________
iommu mailing list iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 18 lines · Don Dutile
Quoted 17 lines · Alex Williamson
Quoted 16 lines · Bjorn Helgaas
Quoted 14 lines · Alex Williamson
Quoted 12 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);+1; there is a global in the PCI code,
pci_acs_enable, and a functionpci_enable_acs(), which the above name certainly confuses. I recommendpci_find_top_acs_bridge()would be most descriptive.
Yep, the new API I'm working with is:
bool pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags); bool pci_acs_path_enabled(struct pci_dev *start,
struct pci_dev *end, u16 acs_flags);
Quoted 66 lines
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Yes, modulo this comment in libvirt source:
Quoted 7 lines
Quoted 5 lines
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Would you mind looking for the paragraph that says this? I'd rather code this into the iommu driver callers than core PCI code if this is just a platform standard.
Quoted 34 lines
Quoted 34 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)
Quoted 7 lines
Quoted 6 lines
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.see comment above wrt root ports that have IOMMUs in them.
Except it really seems to be platform convention where the IOMMU lives. The DMAR for VT-d describes which devices and hierarchies a DRHD is used for and from that we can make assumptions about where it physically lives. AMD-Vi exposes a PCI device as the IOMMU, but it's just a peer function on the root bus. For now I'm just allowing pci_acs_path_enabled to take NULL for and end, which means "up to the root bus".
Quoted 12 lines
Quoted 7 lines
Quoted 6 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).
Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller
00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--
00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Alex
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 47 lines · Alex Williamson
Quoted 13 lines · Don Dutile
Quoted 7 lines · Alex Williamson
Quoted 5 lines · Bjorn Helgaas
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--
Quoted 7 lines
00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Here's what I'm currently thinking. This is a much more simple interface, but I don't know if I'm correctly accounting for multifunciton devices. Callers use something like:
+ if (dma_pdev->multifunction &&
+ !pci_acs_enabled(dma_pdev, PCI_ACS_ENABLED))
+ dma_pdev = pci_get_slot(dma_pdev->bus,
+ PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
+ 0));
+
+ while (!pci_is_root_bus(dma_pdev->bus)) {
+ if (pci_acs_path_enabled(dma_pdev->bus->self,
+ NULL, PCI_ACS_ENABLED))
+ break;
+
+ dma_pdev = dma_pdev->bus->self;
+ }
Where the first test is where we have the option to make a very strict ACS check for multifunction. Does this start to make more sense? Interested in opinions on multifunction strict-ness. Thanks,
Alex
Author: Alex Williamson <[email protected]> Date: Wed May 16 13:17:24 2012 -0600
pci: Add ACS validation utility
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Intermediate switches between an endpoint and the root bus can redirect DMA back downstream before things like IOMMU have a chance to intervene. This utility function allows us to determine the closest device for which ACS is enabled back to the root bus for use in determining the boundaries of an iommu group. Logic for this extracted from libvirt.
drivers/pci/pci.c +73 -0
@@ -2359,6 +2359,79 @@ void pci_enable_acs(struct pci_dev *dev) }/** +* pci_acs_enable - test ACS against required flags for a given device +* @pdev: device to test +* @acs_flags: required PCI ACS flags +* +* Return true if the device supports the provided flags. Automatically +* filters out flags that are not implemented on multifunction devices. +*/ +bool pci_acs_enabled (struct pci_dev *pdev,u16 acs_flags) +{ +int pos; +u16 ctrl; + +if (!pci_is_pcie (pdev)) +return false; + +if (pdev->pcie_type ==PCI_EXP_TYPE_DOWNSTREAM || + pdev->pcie_type ==PCI_EXP_TYPE_ROOT_PORT ) { + pos =pci_find_ext_capability (pdev,PCI_EXT_CAP_ID_ACS ); +if (!pos) +return false; + +pci_read_config_word (pdev, pos +PCI_ACS_CTRL , &ctrl); +if ((ctrl & acs_flags) != acs_flags) +return false; + }else if (pdev->multifunction) { +/* Filter out flags not applicable to multifunction */ + acs_flags &= (PCI_ACS_RR |PCI_ACS_CR | +PCI_ACS_EC |PCI_ACS_DT ); + + pos =pci_find_ext_capability (pdev,PCI_EXT_CAP_ID_ACS ); +if (!pos) +return false; + +pci_read_config_word (pdev, pos +PCI_ACS_CTRL , &ctrl); +if ((ctrl & acs_flags) != acs_flags) +return false; + } + +return true; +} +EXPORT_SYMBOL_GPL (pci_acs_enabled); + +/** +* pci_acs_path_enable - test ACS flags from start to end in a hierarchy +* @start: starting downstream device +* @end: ending upstream device or NULL to search to the root bus +* @acs_flags: required flags +* +* Walk up a device tree from start to end testing PCI ACS support. If +* any step along the way does not support the required flags, return false. +*/ +bool pci_acs_path_enabled (struct pci_dev *start, +struct pci_dev *end,u16 acs_flags) +{ +struct pci_dev *pdev, *parent = start; + +do { + pdev = parent; + +if (!pci_acs_enabled (pdev, acs_flags)) +return false; + +if (pci_is_root_bus (pdev->bus)) +return (end ==NULL ); + + parent = pdev->bus->self; + }while (pdev != end); + +return true; +} +EXPORT_SYMBOL_GPL (pci_acs_path_enabled); + +/** * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge * @dev: the PCI device * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
include/linux/pci.h +3 -1
@@ -1586,7 +1586,9 @@ static inline bool pci_is_pcie(struct pci_dev *dev) }void pci_request_acs (void ); - +bool pci_acs_enabled (struct pci_dev *pdev,u16 acs_flags); +bool pci_acs_path_enabled (struct pci_dev *start, +struct pci_dev *end,u16 acs_flags);#define PCI_VPD_LRDT 0x80/* Large Resource Data Type */ #define PCI_VPD_LRDT_ID (x) (x | PCI_VPD_LRDT)
Re: [PATCH 02/13] iommu: IOMMU Groups
In reply to Alex WilliamsonQuoted 36 lines · Alex Williamson
Quoted 17 lines · David Gibson
Quoted 2 lines · Alex Williamson
Quoted 1 line · David Gibson
On Fri, May 11, 2012 at 04:55:41PM -0600, Alex Williamson wrote:
[snip]
Quoted 13 lines
Quoted 12 lines
drivers/iommu/iommu.c line 42
Quoted 7 lines
drivers/iommu/iommu.c +7
-0+struct iommu_group { +struct kobject kobj; +struct kobject *devices_kobj; +struct list_head devices; +struct mutex mutex; +struct blocking_notifier_head notifier; +int id;I think you should add some sort of name string to the group as well (supplied by the iommu driver creating the group). That would make it easier to connect the arbitrary assigned IDs to any platform-standard naming convention for these things.
Quoted 5 lines
Quoted 1 line
When would the name be used and how is it exposed?
I'm thinking of this basically as a debugging aid. So I'd expect it to appear in a 'name' (or 'description') sysfs property on the group, and in printk messages regarding the group.
Ok, so long as it's only descriptive/debugging I don't have a problem adding something like that.
Quoted 9 lines
[snip]
Quoted 7 lines
Quoted 3 lines
So, it's not clear that the
kobject_name()here has to be unique across all devices in the group. It might be better to use an arbitrary index here instead of a name to avoid that problem.Hmm, that loses useful convenience when they are unique, such as on PCI. I'll look and see if
sysfs_create_linkwill fail on duplicate names and see about adding some kind of instance to it.
Quoted 6 lines
Quoted 2 lines
Ok. Is the name necessarily unique even for PCI, if the group crosses multiple domains?
Yes, it includes the domain in the dddd:bb:dd.f form. I've found I can just use
sysfs_create_link_nowarnand add a .# index when we have a name collision.
Ok, that sounds good.
Quoted 30 lines
Quoted 30 lines
[snip]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/iommu/iommu.c +9
-0+mutex_lock (&group->mutex); +list_for_each_entry (device, &group->devices, list) { +if (device->dev == dev) { +list_del (&device->list); +kfree (device); +break ; + } + } +mutex_unlock (&group->mutex);Quoted 8 lines
drivers/iommu/iommu.c line 249
Quoted 5 lines
drivers/iommu/iommu.c +5
-0+ +sysfs_remove_link (group->devices_kobj,kobject_name (&dev->kobj)); +sysfs_remove_link (&dev->kobj,"iommu_group" ); + + dev->iommu_group =NULL ;I suspect the dev -> group pointer should be cleared first, under the group lock, but I'm not certain about that.
Quoted 4 lines
group->mutexis protecting the group's device list. I think my assumption is that when a device is being removed, there should be no references to it for anyone to race withiommu_group_get(dev), but I'm not sure how valid that is.What I'm concerned about here is someone grabbing the device by non-group-related means, grabbing a pointer to its group and that racing with
remove_device(). It would then end up with a group pointer it thinks is right for the device, when the group no longer thinks it owns the device.
Quoted 8 lines
Quoted 3 lines
Doing it under the lock is so that on the other side, group aware code doesn't traverse the group member list and grab a reference to a device which no longer points back to the group.
Our
for_eachfunction does grab the lock, as you noticed below, so removing it from the list under lock prevents that path. Where it gets fuzzy is if someone can calliommu_group_get(dev)to get a group reference in this gap.
Right, that's what I'm concerned about.
Quoted 5 lines
Whether we clear the
iommu_grouppointer under
lock or not doesn't matter for that path since it doesn't retrieve it under lock. The assumption there is that the caller is going to have a reference to the device and therefore the device is not being removed.
Hrm. I guess that works, assuming that removing a device from the system is the only thing that will cause a device to be removed from the group. How confident are we of that?
Quoted 3 lines
The asynchronous locking and reference counting is by far the hardest part of
iommu_groupsand vfio core, so appreciate any hard analysis of that.
Well, I've given my recommendation on this small aspect.
[snip]
Quoted 23 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
So, there's still the question of how to assign grouping for devices on a subordinate bus behind a bridge which is iommu managed. The iommu driver for the top-level bus can't know about all possible subordinate bus types, but the subordinate devices will (in most cases, anyway) be iommu translated as if originating with the bus bridge.
Not just any bridge, there has to be a different
bus_typeon the subordinate side. Things like PCI-to-PCI work as is, but a PCI-to-ISA would trigger this.Quoted 6 lines
Right, although ISA-under-PCI is a bit of a special case anyway. I think PCI to Firewire/IEEE1394 would also have this issue, as would SoC-bus-to-PCI for a SoC which had an IOMMU at the SoC bus level. And virtual struct devices where the PCI driver is structured as a wrapper around a "vanilla" device driver, a pattern used in a number of drivers for chips with both PCI and non PCI variants.
Sorry, I jumped into reliving this issue without remembering how I decided to rationalize it for IOMMU groups. Let's step through it. Given DeviceA that's a member of GroupA and potentially sources a subordinate bus (
A_bus_type) exposing DeviceA', what are the issues?Quoted 1 line
From a VFIO perspective, GroupA isn't usable so long as DeviceA is
Quoted 9 lines
claimed by a non-VFIO driver. That same non-VFIO driver is the one causing DeviceA to source
A_bus_type, so remove the driver and DeviceA' goes away and we can freely give GroupA to userspace. I believe this is always true; there are no subordinate buses to devices that meet the "viable" driver requirements of VFIO. I don't see any problems with the fact that userspace can then re-sourceA_bus_typeand find DeviceA'. That's what should happen. If we want to assign just DeviceA' to userspace, well, it has no IOMMU group of it's own, so clearly it's not assignable on it's own.
Ah, right, so the assumption is that a bridge will only expose it's subordinate bus when it has an active kernel driver. Yeah, I think that works at least for the current use cases.
Quoted 10 lines
For the more general IOMMU group case, I'm still struggling to figure out why this is an issue. If we were to do
dma_opsvia IOMMU groups, I don't think it's unreasonable thatmap_pagewould discover there's noiommu_opsondev->bus(A_bus_type) and step up todev->bus->selfto find bothiommu_groupon DeviceA andiommu_opsonDeviceA->bus. Is there a practical reason why DeviceA' would need to be listed as a member of GroupA, or is it just an optimization? I know we had a number of discussions about these type of devices for isolation groups, but I think that trying to strictly represent these types of devices was also one of the downfalls of the isolation proposal.
Yeah, walking up the tree to find a group is certainly a possibility as well. Ok, so I'm happy enough only to track primary group members for now. I think we should bear in mind the possibility we might need to track secondary members in future though (either as an optimization or as something we need for a use case we haven't pinned down yet).
Quoted 8 lines
This did make me think of one other generic quirk we might need. There's some funkiness with USB that makes me think that it's effectively a shared bus between 1.x and 2.0 controllers. So assigning the 1.x and 2.0 controllers to different groups potentially allows a fast and a slow path to the same set of devices. Is this true? If so, we probably need to quirk OHCI/UHCI and EHCI functions together when they're on the same PCI device. I think the PCI class code is sufficient to do this. Thanks,
Ah, maybe. I don't know enough about USB conventions to say for sure.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH 03/13] iommu: IOMMU groups for VT-d and AMD-Vi
In reply to Alex WilliamsonQuoted 4 lines · Alex Williamson
Add back group support for AMD & Intel.
amd_iommualready tracks devices and has init and uninit routines to manage groups. intel-iommu does this on the fly, so we make use of the notifier support built into iommu groups to create and remove groups.
Looks ok to me.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH 04/13] pci: New pci_dma_quirk()
In reply to Alex WilliamsonQuoted 32 lines · Alex Williamson
Integrating IOMMU groups more closely into the driver core allows us to more easily work around DMA quirks. The Ricoh multifunction controller is a favorite example of devices that are currently incompatible with IOMMU isolation as all the functions use the requestor ID of function 0 for DMA. Passing this device into
pci_dma_quirkreturns the PCI device to use for DMA. The IOMMU driver can then construct an IOMMU group including both devices.Signed-off-by Alex Williamson <[email protected]>---
drivers/pci/quirks.c | 22 ++++++++++++++++++++++ include/linux/pci.h | 2 ++ 2 files changed, 24 insertions(+), 0 deletions(-)drivers/pci/quirks.c +9
-0@@ -3109,3 +3109,25 @@ int pci_dev_specific_reset(struct pci_dev *dev, int probe)return -ENOTTY ; } + +struct pci_dev *pci_dma_quirk (struct pci_dev *dev) +{ +struct pci_dev *dma_dev = dev; + +/* +* https://bugzilla.redhat.com/show_bug.cgi?id=605888 +* +* Some Ricoh devices use the function 0 source ID for DMA on
drivers/pci/quirks.c int pci_dev_specific_reset(struct pci_dev *dev, int probe) · line 3130
Quoted 10 lines
drivers/pci/quirks.c +10
-0+* other functions of a multifunction device. The DMA devices +* is therefore function 0, which will have implications of the +* iommu grouping of these devices. +*/ +if (dev->vendor ==PCI_VENDOR_ID_RICOH && + (dev->device ==0xe822 || dev->device ==0xe230 || + dev->device ==0xe832 || dev->device ==0xe476 )) { + dma_dev =pci_get_slot (dev->bus, +PCI_DEVFN (PCI_SLOT (dev->devfn),0 )); + }
Hrm. This seems like a very generic name for a function performing a very specific test. We could well have devices with the same problem in future, so shouldn't this be set up so the same quirk can be easily added to new device ids without changing the function code itself.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH 04/13] pci: New pci_dma_quirk()
Quoted 40 lines · David Gibson
Quoted 39 lines · Alex Williamson
Integrating IOMMU groups more closely into the driver core allows us to more easily work around DMA quirks. The Ricoh multifunction controller is a favorite example of devices that are currently incompatible with IOMMU isolation as all the functions use the requestor ID of function 0 for DMA. Passing this device into
pci_dma_quirkreturns the PCI device to use for DMA. The IOMMU driver can then construct an IOMMU group including both devices.Signed-off-by Alex Williamson <[email protected]>---
drivers/pci/quirks.c | 22 ++++++++++++++++++++++ include/linux/pci.h | 2 ++ 2 files changed, 24 insertions(+), 0 deletions(-)drivers/pci/quirks.c +16
-0@@ -3109,3 +3109,25 @@ int pci_dev_specific_reset(struct pci_dev *dev, int probe)return -ENOTTY ; } + +struct pci_dev *pci_dma_quirk (struct pci_dev *dev) +{ +struct pci_dev *dma_dev = dev; + +/* +* https://bugzilla.redhat.com/show_bug.cgi?id=605888 +* +* Some Ricoh devices use the function 0 source ID for DMA on +* other functions of a multifunction device. The DMA devices +* is therefore function 0, which will have implications of the +* iommu grouping of these devices. +*/ +if (dev->vendor ==PCI_VENDOR_ID_RICOH && + (dev->device ==0xe822 || dev->device ==0xe230 || + dev->device ==0xe832 || dev->device ==0xe476 )) {
Quoted 8 lines
drivers/pci/quirks.c int pci_dev_specific_reset(struct pci_dev *dev, int probe) · line 3130
Quoted 3 lines
drivers/pci/quirks.c +3
-0+ dma_dev =pci_get_slot (dev->bus, +PCI_DEVFN (PCI_SLOT (dev->devfn),0 )); + }Hrm. This seems like a very generic name for a function performing a very specific test. We could well have devices with the same problem in future, so shouldn't this be set up so the same quirk can be easily added to new device ids without changing the function code itself.
I've since added a USB quirk here to group all the USB functions in a slot. I'll take a closer look at the quirk helpers to see if anything makes this easier, but I didn't see much point in spending a lot of time over-optimizing this for 1 or 2 quirks that we can just step through in a monolithic function. Thanks,
Alex
Re: [PATCH 04/13] pci: New pci_dma_quirk()
In reply to Alex WilliamsonAlex,
Quoted 8 lines · Alex Williamson
Integrating IOMMU groups more closely into the driver core allows us to more easily work around DMA quirks. The Ricoh multifunction controller is a favorite example of devices that are currently incompatible with IOMMU isolation as all the functions use the requestor ID of function 0 for DMA. Passing this device into
pci_dma_quirkreturns the PCI device to use for DMA. The IOMMU driver can then construct an IOMMU group including both devices.
Please give some thought to the Marvell SATA controller quirk as well.
Instead of multiple visible functions using the same requester ID of function 0, the Marvell device only makes function 0 visible, but uses the requestor ID of function 1 as well during DMA.
See https://bugzilla.redhat.com/show_bug.cgi?id=757166
--
A.
Re: [PATCH 04/13] pci: New pci_dma_quirk()
Quoted 13 lines · Anonymous
Alex,
Quoted 8 lines · Alex Williamson
Integrating IOMMU groups more closely into the driver core allows us to more easily work around DMA quirks. The Ricoh multifunction controller is a favorite example of devices that are currently incompatible with IOMMU isolation as all the functions use the requestor ID of function 0 for DMA. Passing this device into
pci_dma_quirkreturns the PCI device to use for DMA. The IOMMU driver can then construct an IOMMU group including both devices.
Quoted 7 lines
Please give some thought to the Marvell SATA controller quirk as well.
Instead of multiple visible functions using the same requester ID of function 0, the Marvell device only makes function 0 visible, but uses the requestor ID of function 1 as well during DMA.
Wow. That one isn't quite as easy to deal with since there's no existing device in the kernel to point to. This comment might be on the right track:
https://bugzilla.kernel.org/show_bug.cgi?id=42679#c11
Perhaps David Woodhouse can comment on support for phantom functions. If we had infrastructure for that it might be easy for the quirk to update the pci_dev struct, inserting a new phantom function. Otherwise we'd need to create a new device in the kernel for it. Thanks,
Alex
Re: RESEND3: Re: [PATCH 05/13] pci: New pci_acs_enabled()
In reply to Alex WilliamsonQuoted 19 lines · Alex Williamson
Quoted 18 lines · Don Dutile
Quoted 17 lines · Alex Williamson
Quoted 16 lines · Bjorn Helgaas
Quoted 14 lines · Alex Williamson
Quoted 12 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);+1; there is a global in the PCI code,
pci_acs_enable, and a functionpci_enable_acs(), which the above name certainly confuses. I recommendpci_find_top_acs_bridge()would be most descriptive.
Finally, with my email filters fixed, I can see this email... :)
Quoted 7 lines
Yep, the new API I'm working with is:
bool
pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags); bool pci_acs_path_enabled(structpci_dev*start,struct pci_dev *end, u16 acs_flags);
ok.
Quoted 71 lines
Quoted 71 lines
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 15 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Yes, modulo this comment in libvirt source:
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/
Quoted 7 lines
Quoted 2 lines
Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Would you mind looking for the paragraph that says this? I'd rather code this into the iommu driver callers than core PCI code if this is just a platform standard.
In section 6.12.1.1 of PCIe Base spec, rev 3.0, it states:
ACS upstream fwding: Must be implemented by Root Ports if the RC supports
Redirected Request Validation;
-- which means, if a Root port allows a peer-to-peer transaction to another
one of its ports, then it has to support ACS.
So, this means that:
(a) if a Root complex with multiple ports can't do peer-to-peer to another port, ACS isn't needed
(b) if a Root complex w/multiple ports can do peer-to-peer to another port, it must have ACS capability if it does...
and since the linux code turns on ACS for all ports with an ACS cap, it degenerates (in Linux) that all Root ports are implementing the end functionality of ACS==on, all traffic goes up to IOMMU in RC.
Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)Quoted 7 lines
Quoted 6 lines
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.see comment above wrt root ports that have IOMMUs in them.
Quoted 8 lines
Except it really seems to be platform convention where the IOMMU lives. The DMAR for VT-d describes which devices and hierarchies a DRHD is used for and from that we can make assumptions about where it physically lives. AMD-Vi exposes a PCI device as the IOMMU, but it's just a peer function on the root bus. For now I'm just allowing
pci_acs_path_enabledto take NULL for and end, which means "up to the root bus".
ATM, VT-d IOMMUs are only in the RCs, so, ACS at each downstream port
in a tree would/should return 'true' to the acs_enabled check when it gets to a Root port. For AMD-Vi, I thought the same held true, ATM, but I have to dig through yet-another spec (or ask Joerg to check-in to this thread & provide the details).
Quoted 50 lines
Quoted 12 lines
Quoted 7 lines
Quoted 6 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Quoted 5 lines
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Alex
Well, in digging into the ACS-support in Root port question above, I just found out about this ACS support status for multifunctions too. I need more time to read/digest, but a quick read says MFDs should have an ACS cap with relevant RO-status & control bits to direct/control peer-to-peer and ACS-upstream. Lack of an ACS struct implies(?) peer-to-peer can happen, and thus, the above have to be in the same iommu group. Unfortunately, I think a large lot of MFDs don't have ACS caps,
and don't/can't do peer-to-peer, so the above is heavy-handed, albeit to spec.
Maybe we need a (large?) pci-quirk for the list of existing
MFDs that don't have ACS caps that would enable the above devices to be in separate groups.
On the flip side, it solves some of the quirks for MFDs that
use the wrong BDF in their src-id dma packets! :) -- they default to the same group now...
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 22 lines · Don Dutile
Quoted 21 lines · Alex Williamson
Quoted 20 lines · Don Dutile
Quoted 19 lines · Alex Williamson
Quoted 18 lines · Bjorn Helgaas
Quoted 16 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);+1; there is a global in the PCI code,
pci_acs_enable, and a functionpci_enable_acs(), which the above name certainly confuses. I recommendpci_find_top_acs_bridge()would be most descriptive.Finally, with my email filters fixed, I can see this email... :)
Welcome back ;)
Quoted 82 lines
Quoted 6 lines
Yep, the new API I'm working with is:
bool
pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags); bool pci_acs_path_enabled(structpci_dev*start,struct pci_dev *end, u16 acs_flags);ok.
Quoted 74 lines
Quoted 73 lines
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Quoted 7 lines
Yes, modulo this comment in libvirt source:
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Quoted 7 lines
Quoted 4 lines
Would you mind looking for the paragraph that says this? I'd rather code this into the iommu driver callers than core PCI code if this is just a platform standard.
In section 6.12.1.1 of PCIe Base spec, rev 3.0, it states:
ACS upstream fwding: Must be implemented by Root Ports if the RC supportsRedirected Request Validation;
Huh? (If support ACS.RR then must support ACS.UF) != must support ACS.
Quoted 2 lines
-- which means, if a Root port allows a peer-to-peer transaction to another
one of its ports, then it has to support ACS.
I don't get that at all from 6.12.1.1, especially given the first sentence of that section:
This section applies to Root Ports and Downstream Switch Ports that implement an ACS Extended Capability structure.
Quoted 59 lines
So, this means that:
(a) if a Root complex with multiple ports can't do peer-to-peer to another port, ACS isn't needed
(b) if a Root complex w/multiple ports can do peer-to-peer to another port, it must have ACS capability if it does...
and since the linux code turns on ACS for all ports with an ACS cap, it degenerates (in Linux) that all Root ports are implementing the end functionality of ACS==on, all traffic goes up to IOMMU in RC.Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)Quoted 7 lines
Quoted 6 lines
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.see comment above wrt root ports that have IOMMUs in them.
Quoted 8 lines
Except it really seems to be platform convention where the IOMMU lives. The DMAR for VT-d describes which devices and hierarchies a DRHD is used for and from that we can make assumptions about where it physically lives. AMD-Vi exposes a PCI device as the IOMMU, but it's just a peer function on the root bus. For now I'm just allowing
pci_acs_path_enabledto take NULL for and end, which means "up to the root bus".
Quoted 4 lines
ATM, VT-d IOMMUs are only in the RCs, so, ACS at each downstream port
in a tree would/should return 'true' to theacs_enabledcheck when it gets to a Root port. For AMD-Vi, I thought the same held true, ATM, but I have to dig through yet-another spec (or ask Joerg to check-in to this thread & provide the details).
But are we programming to convention or spec? And I'm still confused about why we assume the root port isn't susceptible to redirection before IOMMU translation. One of the benefits of the pci_acs_path_enable() API is that it pushes convention out to the IOMMU driver. So it's intel-iommu.c's problem whether to test for ACS support to the RC or to a given level (and for that matter know whether IOMMU translation takes precedence over redirection in the RC).
Quoted 55 lines
Quoted 50 lines
Quoted 12 lines
Quoted 7 lines
Quoted 6 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Quoted 5 lines
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Alex
Quoted 15 lines
Well, in digging into the ACS-support in Root port question above, I just found out about this ACS support status for multifunctions too. I need more time to read/digest, but a quick read says MFDs should have an ACS cap with relevant RO-status & control bits to direct/control peer-to-peer and ACS-upstream. Lack of an ACS struct implies(?) peer-to-peer can happen, and thus, the above have to be in the same iommu group. Unfortunately, I think a large lot of MFDs don't have ACS caps,
and don't/can't do peer-to-peer, so the above is heavy-handed, albeit to spec.
Maybe we need a (large?) pci-quirk for the list of existing
MFDs that don't have ACS caps that would enable the above devices to be in separate groups.
On the flip side, it solves some of the quirks for MFDs that
use the wrong BDF in their src-id dma packets! :) -- they default to the same group now...
Yep, sounds like you might agree with my patch, it's heavy handed, but seems to adhere to the spec. That probably just means we need an option to allow a more lenient interpretation, that maybe we don't have to support. Thanks,
Alex
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 25 lines · Alex Williamson
Quoted 24 lines · Don Dutile
Quoted 23 lines · Alex Williamson
Quoted 22 lines · Don Dutile
Quoted 21 lines · Alex Williamson
Quoted 20 lines · Bjorn Helgaas
Quoted 18 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
+1; there is a global in the PCI code,
pci_acs_enable, and a functionpci_enable_acs(), which the above name certainly confuses. I recommendpci_find_top_acs_bridge()would be most descriptive.Finally, with my email filters fixed, I can see this email... :)
Welcome back ;)
Indeed... and I recvd 3 copies of this reply, so the pendulum has flipped the other direction... ;-)
Quoted 86 lines
Quoted 86 lines
Quoted 6 lines
Yep, the new API I'm working with is:
bool
pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags); bool pci_acs_path_enabled(structpci_dev*start,struct pci_dev *end, u16 acs_flags);ok.
Quoted 78 lines
Quoted 66 lines
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Yes, modulo this comment in libvirt source:
Quoted 7 lines
Quoted 5 lines
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Would you mind looking for the paragraph that says this? I'd rather code this into the iommu driver callers than core PCI code if this is just a platform standard.
Quoted 6 lines
Quoted 3 lines
In section 6.12.1.1 of PCIe Base spec, rev 3.0, it states:
ACS upstream fwding: Must be implemented by Root Ports if the RC supportsRedirected Request Validation;Huh? (If support ACS.RR then must support ACS.UF) != must support ACS.
UF?
Quoted 10 lines
Quoted 2 lines
-- which means, if a Root port allows a peer-to-peer transaction to another
one of its ports, then it has to support ACS.I don't get that at all from 6.12.1.1, especially given the first sentence of that section:
This section applies to Root Ports and Downstream Switch Ports that implement an ACS Extended Capability structure.
hmmm, well I did. The root port section is different than Downstream ports as well. downstream ports *must* support peer-xfers due to positive decoding of base/limit addresses, and ACS is optional in downstream ports. Peer-to-peer btwn root ports is optional.
so, I don't get what you don't get... ;-)
.. but, I understand how the spec can be read/interpreted differently,
given it's clarity (did lawyers write it?!?!!), so I could be interpreting incorrectly.
Quoted 9 lines
Quoted 9 lines
So, this means that:
(a) if a Root complex with multiple ports can't do peer-to-peer to another port, ACS isn't needed
(b) if a Root complex w/multiple ports can do peer-to-peer to another port, it must have ACS capability if it does...
and since the linux code turns on ACS for all ports with an ACS cap, it degenerates (in Linux) that all Root ports are implementing the end functionality of ACS==on, all traffic goes up to IOMMU in RC.
I thought I explained how I interpreted the root-port part of ACS above, so maybe you can tell me how you think my interpretation is incorrect.
Quoted 55 lines
Quoted 50 lines
Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)Quoted 7 lines
Quoted 6 lines
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.see comment above wrt root ports that have IOMMUs in them.
Quoted 8 lines
Except it really seems to be platform convention where the IOMMU lives. The DMAR for VT-d describes which devices and hierarchies a DRHD is used for and from that we can make assumptions about where it physically lives. AMD-Vi exposes a PCI device as the IOMMU, but it's just a peer function on the root bus. For now I'm just allowing
pci_acs_path_enabledto take NULL for and end, which means "up to the root bus".Quoted 4 lines
ATM, VT-d IOMMUs are only in the RCs, so, ACS at each downstream port
in a tree would/should return 'true' to theacs_enabledcheck when it gets to a Root port. For AMD-Vi, I thought the same held true, ATM, but I have to dig through yet-another spec (or ask Joerg to check-in to this thread& provide the details).
Quoted 8 lines
But are we programming to convention or spec? And I'm still confused about why we assume the root port isn't susceptible to redirection before IOMMU translation. One of the benefits of the
pci_acs_path_enable()API is that it pushes convention out to the IOMMU driver. So it's intel-iommu.c's problem whether to test for ACS support to the RC or to a given level (and for that matter know whether IOMMU translation takes precedence over redirection in the RC).
Agreed. the best design here would be for the intel-iommu.c to test for ACS wrt the root port (not to be confused with root complex) since Intel-IOMMUs are not 'attached' to a PCI bus. Now, AMD's IOMMUs are attached to a PCI bus, so maybe it can be simplified in that case.... but it may be best to mimic the iommu-set-acs-for-root-port attribute the same manner.
Quoted 71 lines
Quoted 55 lines
Quoted 50 lines
Quoted 12 lines
Quoted 7 lines
Quoted 6 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Quoted 5 lines
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Alex
Quoted 15 lines
Well, in digging into the ACS-support in Root port question above, I just found out about this ACS support status for multifunctions too. I need more time to read/digest, but a quick read says MFDs should have an ACS cap with relevant RO-status& control bits to direct/control peer-to-peer and ACS-upstream. Lack of an ACS struct implies(?) peer-to-peer can happen, and thus, the above have to be in the same iommu group. Unfortunately, I think a large lot of MFDs don't have ACS caps,
and don't/can't do peer-to-peer, so the above is heavy-handed, albeit to spec.
Maybe we need a (large?) pci-quirk for the list of existing
MFDs that don't have ACS caps that would enable the above devices to be in separate groups.
On the flip side, it solves some of the quirks for MFDs that
use the wrong BDF in their src-id dma packets! :) -- they default to the same group now...
Quoted 1 line
Yep, sounds like you might agree with my patch, it's heavy handed, but
Yes, but we should design-in a quirk check list for MFDs,
b/c we already know some will fail when they should pass this check, b/c the hw was made post ACS, or the vendors didn't see the benefit of ACS reporting (even if the funcitonality was enabling bit-settings that did nothing hw-wise), b/c they didn't understand the use case (VFIO, dev-assignment to virt guests, etc.).
Quoted 3 lines
seems to adhere to the spec. That probably just means we need an option to allow a more lenient interpretation, that maybe we don't have to support. Thanks,
Right. As I said, a hook to do quirk-level additions from the get-go would speed this expected need/addition.
Quoted 7 lines
Alex
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 05/13] pci: New pci_acs_enabled()
Quoted 123 lines · Don Dutile
Quoted 25 lines · Alex Williamson
Quoted 24 lines · Don Dutile
Quoted 23 lines · Alex Williamson
Quoted 22 lines · Don Dutile
Quoted 21 lines · Alex Williamson
Quoted 20 lines · Bjorn Helgaas
Quoted 18 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
+1; there is a global in the PCI code,
pci_acs_enable, and a functionpci_enable_acs(), which the above name certainly confuses. I recommendpci_find_top_acs_bridge()would be most descriptive.Finally, with my email filters fixed, I can see this email... :)
Welcome back ;)
Indeed... and I recvd 3 copies of this reply, so the pendulum has flipped the other direction... ;-)
Quoted 86 lines
Quoted 86 lines
Quoted 6 lines
Yep, the new API I'm working with is:
bool
pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags); bool pci_acs_path_enabled(structpci_dev*start,struct pci_dev *end, u16 acs_flags);ok.
Quoted 78 lines
Quoted 66 lines
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Yes, modulo this comment in libvirt source:
Quoted 7 lines
Quoted 5 lines
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Would you mind looking for the paragraph that says this? I'd rather code this into the iommu driver callers than core PCI code if this is just a platform standard.
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
In section 6.12.1.1 of PCIe Base spec, rev 3.0, it states:
ACS upstream fwding: Must be implemented by Root Ports if the RC supportsRedirected Request Validation;Huh? (If support ACS.RR then must support ACS.UF) != must support ACS.
UF?
Upstream Forwarding. So basically that spec quote is saying that if the RC supports one aspect of ACS then it must support this other. But that doesn't say to me that it must support ACS to begin with.
Quoted 28 lines
Quoted 10 lines
Quoted 2 lines
-- which means, if a Root port allows a peer-to-peer transaction to another
one of its ports, then it has to support ACS.I don't get that at all from 6.12.1.1, especially given the first sentence of that section:
This section applies to Root Ports and Downstream Switch Ports that implement an ACS Extended Capability structure.
hmmm, well I did. The root port section is different than Downstream ports as well. downstream ports *must* support peer-xfers due to positive decoding of base/limit addresses, and ACS is optional in downstream ports. Peer-to-peer btwn root ports is optional.
so, I don't get what you don't get... ;-)
.. but, I understand how the spec can be read/interpreted differently,
given it's clarity (did lawyers write it?!?!!), so I could be interpreting incorrectly.Quoted 9 lines
Quoted 9 lines
So, this means that:
(a) if a Root complex with multiple ports can't do peer-to-peer to another port, ACS isn't needed
(b) if a Root complex w/multiple ports can do peer-to-peer to another port, it must have ACS capability if it does...
and since the linux code turns on ACS for all ports with an ACS cap, it degenerates (in Linux) that all Root ports are implementing the end functionality of ACS==on, all traffic goes up to IOMMU in RC.
Quoted 2 lines
I thought I explained how I interpreted the root-port part of ACS above, so maybe you can tell me how you think my interpretation is incorrect.
I don't know that it's incorrect, but I don't see how you're making the leap you are. I think the version I have now of the patch leaves this nicely to the iommu drivers. It would make sense, not that hardware always makes sense, that anything with an iommu is going to hardwire translations through the iommu when enabled or they couldn't reasonable do any kind of peer-to-peer with iommu.
Quoted 148 lines
Quoted 55 lines
Quoted 50 lines
Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)Quoted 7 lines
Quoted 6 lines
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.see comment above wrt root ports that have IOMMUs in them.
Quoted 8 lines
Except it really seems to be platform convention where the IOMMU lives. The DMAR for VT-d describes which devices and hierarchies a DRHD is used for and from that we can make assumptions about where it physically lives. AMD-Vi exposes a PCI device as the IOMMU, but it's just a peer function on the root bus. For now I'm just allowing
pci_acs_path_enabledto take NULL for and end, which means "up to the root bus".Quoted 4 lines
ATM, VT-d IOMMUs are only in the RCs, so, ACS at each downstream port
in a tree would/should return 'true' to theacs_enabledcheck when it gets to a Root port. For AMD-Vi, I thought the same held true, ATM, but I have to dig through yet-another spec (or ask Joerg to check-in to this thread& provide the details).Quoted 8 lines
But are we programming to convention or spec? And I'm still confused about why we assume the root port isn't susceptible to redirection before IOMMU translation. One of the benefits of the
pci_acs_path_enable()API is that it pushes convention out to the IOMMU driver. So it's intel-iommu.c's problem whether to test for ACS support to the RC or to a given level (and for that matter know whether IOMMU translation takes precedence over redirection in the RC).Agreed. the best design here would be for the
intel-iommu.cto test for ACS wrt the root port (not to be confused with root complex) since Intel-IOMMUs are not 'attached' to a PCI bus. Now, AMD's IOMMUs are attached to a PCI bus, so maybe it can be simplified in that case.... but it may be best to mimic the iommu-set-acs-for-root-port attribute the same manner.Quoted 71 lines
Quoted 55 lines
Quoted 50 lines
Quoted 12 lines
Quoted 7 lines
Quoted 6 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Quoted 5 lines
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Alex
Quoted 15 lines
Well, in digging into the ACS-support in Root port question above, I just found out about this ACS support status for multifunctions too. I need more time to read/digest, but a quick read says MFDs should have an ACS cap with relevant RO-status& control bits to direct/control peer-to-peer and ACS-upstream. Lack of an ACS struct implies(?) peer-to-peer can happen, and thus, the above have to be in the same iommu group. Unfortunately, I think a large lot of MFDs don't have ACS caps,
and don't/can't do peer-to-peer, so the above is heavy-handed, albeit to spec.
Maybe we need a (large?) pci-quirk for the list of existing
MFDs that don't have ACS caps that would enable the above devices to be in separate groups.
On the flip side, it solves some of the quirks for MFDs that
use the wrong BDF in their src-id dma packets! :) -- they default to the same group now...Quoted 1 line
Yep, sounds like you might agree with my patch, it's heavy handed, but
Yes, but we should design-in a quirk check list for MFDs,
b/c we already know some will fail when they should pass this check, b/c the hw was made post ACS, or the vendors didn't see the benefit of ACS reporting (even if the funcitonality was enabling bit-settings that did nothing hw-wise), b/c they didn't understand the use case (VFIO, dev-assignment to virt guests, etc.).
Quoted 5 lines
Quoted 3 lines
seems to adhere to the spec. That probably just means we need an option to allow a more lenient interpretation, that maybe we don't have to support. Thanks,
Right. As I said, a hook to do quirk-level additions from the get-go would speed this expected need/addition.
Ok, a quirk should be easy there. Thanks,
Alex
Re: [PATCH 11/13] pci: Create common pcibios_err_to_errno
In reply to Alex WilliamsonQuoted 3 lines · Alex Williamson
For returning errors out to non-PCI code. Re-name xen's version.
Signed-off-by Alex Williamson <[email protected]>
Quoted 74 lines
---
drivers/xen/xen-pciback/conf_space.c | 6 +++--- include/linux/pci.h | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-)drivers/xen/xen-pciback/conf_space.c +3
-3@@ -124,7 +124,7 @@ static inline u32 merge_value(u32 val, u32 new_val, u32 new_val_mask,return val; } -static int pcibios_err_to_errno (int err) +static int xen_pcibios_err_to_errno (int err) {switch (err) {case PCIBIOS_SUCCESSFUL :@@ -202,7 +202,7 @@ out: pci_name(dev), size, offset, value); *ret_val = value; -return pcibios_err_to_errno (err); +return xen_pcibios_err_to_errno (err); }int xen_pcibk_config_write (struct pci_dev *dev,int offset,int size,u32 value)@@ -290,7 +290,7 @@ int xen_pcibk_config_write(struct pci_dev *dev, int offset, int size, u32 value) } } -return pcibios_err_to_errno (err); +return xen_pcibios_err_to_errno (err); }void xen_pcibk_config_free_dyn_fields (struct pci_dev *dev)include/linux/pci.h +26
-0@@ -467,6 +467,32 @@ static inline bool pci_dev_msi_enabled(struct pci_dev *pci_dev) { return false;#define PCIBIOS_SET_FAILED 0x88#define PCIBIOS_BUFFER_TOO_SMALL 0x89 +/* +* Translate above to generic errno for passing back through non-pci. +*/ +static inline int pcibios_err_to_errno (int err) +{ +if (err <=PCIBIOS_SUCCESSFUL ) +return err;/* Assume already errno */ + +switch (err) { +case PCIBIOS_FUNC_NOT_SUPPORTED : +return -ENOENT ; +case PCIBIOS_BAD_VENDOR_ID : +return -EINVAL ; +case PCIBIOS_DEVICE_NOT_FOUND : +return -ENODEV ; +case PCIBIOS_BAD_REGISTER_NUMBER : +return -EFAULT ; +case PCIBIOS_SET_FAILED : +return -EIO ; +case PCIBIOS_BUFFER_TOO_SMALL : +return -ENOSPC ; + } + +return -ENOTTY ; +} +/* Low-level architecture-dependent routines */ struct pci_ops {
Re: [PATCH 05/13] pci: New pci_acs_enabled()
In reply to Alex WilliamsonQuoted 128 lines · Alex Williamson
Quoted 127 lines · Don Dutile
Quoted 25 lines · Alex Williamson
Quoted 24 lines · Don Dutile
Quoted 23 lines · Alex Williamson
Quoted 22 lines · Don Dutile
Quoted 21 lines · Alex Williamson
Quoted 20 lines · Bjorn Helgaas
Quoted 18 lines · Alex Williamson
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · Alex Williamson
In a PCIe environment, transactions aren't always required to reach the root bus before being re-routed. Peer-to-peer DMA may actually not be seen by the IOMMU in these cases. For IOMMU groups, we want to provide IOMMU drivers a way to detect these restrictions. Provided with a PCI device,
pci_acs_enabledreturns the furthest downstream device with a complete PCI ACS chain. This information can then be used in grouping to create fully isolated groups. ACS chain logic extracted from libvirt.Quoted 1 line
The name "
pci_acs_enabled()" sounds like it returns a boolean, but it doesn't.Right, maybe this should be:
struct
pci_dev*pci_find_upstream_acs(structpci_dev*pdev);Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
+1; there is a global in the PCI code,
pci_acs_enable, and a functionpci_enable_acs(), which the above name certainly confuses. I recommendpci_find_top_acs_bridge()would be most descriptive.Finally, with my email filters fixed, I can see this email... :)
Welcome back ;)
Indeed... and I recvd 3 copies of this reply, so the pendulum has flipped the other direction... ;-)
Quoted 90 lines
Quoted 89 lines
Quoted 6 lines
Yep, the new API I'm working with is:
bool
pci_acs_enabled(struct pci_dev *pdev, u16 acs_flags); bool pci_acs_path_enabled(structpci_dev*start,struct pci_dev *end, u16 acs_flags);ok.
Quoted 71 lines
Quoted 71 lines
Quoted 36 lines
Quoted 31 lines
Quoted 8 lines
Quoted 5 lines
I'm not sure what "a complete PCI ACS chain" means.
The function starts from "dev" and searches *upstream*, so I'm guessing it returns the root of a subtree that must be contained in a group.
Any intermediate switch between an endpoint and the root bus can redirect a dma access without iommu translation,
Is this "redirection" just the normal PCI bridge forwarding that allows peer-to-peer transactions, i.e., the rule (from P2P bridge spec, rev 1.2, sec 4.1) that the bridge apertures define address ranges that are forwarded from primary to secondary interface, and the inverse ranges are forwarded from secondary to primary? For example, here:
^ | +--------+-------+ | | +------+-----+ +-----++-----+ | Downstream | | Downstream | | Port | | Port | | 06:05.0 | | 06:06.0 | +------+-----+ +------+-----+ | | +----v----+ +----v----+ | Endpoint| | Endpoint| | 07:00.0 | | 08:00.0 | +---------+ +---------+Quoted 4 lines
that rule is all that's needed for a transaction from 07:00.0 to be forwarded from upstream to the internal switch bus 06, then claimed by 06:06.0 and forwarded downstream to 08:00.0. This is plain old PCI, nothing specific to PCIe.
Quoted 6 lines
Right, I think the main PCI difference is the point-to-point nature of PCIe vs legacy PCI bus. On a legacy PCI bus there's no way to prevent devices talking to each other, but on PCIe the transaction makes a U-turn at some point and heads out another downstream port. ACS allows us to prevent that from happening.
detail: PCIe up/downstream routing is really done by an internal switch;
ACS forces the legacy, PCI base-limit address routing and *forces* the switch to always route the transaction from a downstream port to the upstream port.Quoted 7 lines
Quoted 4 lines
I don't understand ACS very well, but it looks like it basically provides ways to prevent that peer-to-peer forwarding, so transactions would be sent upstream toward the root (and specifically, the IOMMU) instead of being directly claimed by 06:06.0.
Yep, that's my meager understanding as well.
+1
Quoted 15 lines
Quoted 7 lines
Quoted 3 lines
so we're looking for
the furthest upstream device for which acs is enabled all the way up to the root bus.Correct me if this is wrong: To force device A's DMAs to be processed by an IOMMU, ACS must be enabled on the root port and every downstream port along the path to A.
Yes, modulo this comment in libvirt source:
/* if we have no parent, and this is the root bus, ACS doesn't come
* into play since devices on the root bus can't P2P without going
* through the root IOMMU.
*/Quoted 7 lines
Quoted 2 lines
Correct. PCIe spec says roots must support ACS. I believe all the root bridges that have an IOMMU have ACS wired in/on.
Would you mind looking for the paragraph that says this? I'd rather code this into the iommu driver callers than core PCI code if this is just a platform standard.
In section 6.12.1.1 of PCIe Base spec, rev 3.0, it states:
ACS upstream fwding: Must be implemented by Root Ports if the RC supportsRedirected Request Validation;
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
Huh? (If support ACS.RR then must support ACS.UF) != must support ACS.
UF?
Upstream Forwarding. So basically that spec quote is saying that if the RC supports one aspect of ACS then it must support this other. But that doesn't say to me that it must support ACS to begin with.
It says if RC supports peer-to-peer btwn root ports, the root ports must support ACS.... at least that's how I understood it.
but, as we've seen, there's spec, then there's reality...
Quoted 31 lines
Quoted 28 lines
Quoted 10 lines
Quoted 2 lines
-- which means, if a Root port allows a peer-to-peer transaction to another
one of its ports, then it has to support ACS.I don't get that at all from 6.12.1.1, especially given the first sentence of that section:
This section applies to Root Ports and Downstream Switch Ports that implement an ACS Extended Capability structure.
hmmm, well I did. The root port section is different than Downstream ports as well. downstream ports *must* support peer-xfers due to positive decoding of base/limit addresses, and ACS is optional in downstream ports. Peer-to-peer btwn root ports is optional.
so, I don't get what you don't get... ;-)
.. but, I understand how the spec can be read/interpreted differently,
given it's clarity (did lawyers write it?!?!!), so I could be interpreting incorrectly.Quoted 9 lines
Quoted 9 lines
So, this means that:
(a) if a Root complex with multiple ports can't do peer-to-peer to another port, ACS isn't needed
(b) if a Root complex w/multiple ports can do peer-to-peer to another port, it must have ACS capability if it does...
and since the linux code turns on ACS for all ports with an ACS cap, it degenerates (in Linux) that all Root ports are implementing the end functionality of ACS==on, all traffic goes up to IOMMU in RC.Quoted 2 lines
I thought I explained how I interpreted the root-port part of ACS above, so maybe you can tell me how you think my interpretation is incorrect.
Quoted 7 lines
I don't know that it's incorrect, but I don't see how you're making the leap you are. I think the version I have now of the patch leaves this nicely to the iommu drivers. It would make sense, not that hardware always makes sense, that anything with an iommu is going to hardwire translations through the iommu when enabled or they couldn't reasonable do any kind of peer-to-peer with iommu.
agreed. the version you have looks good,
and avoids/handles potential ACS cap idiosyncracies in RC's.
Quoted 158 lines
Quoted 148 lines
Quoted 55 lines
Quoted 50 lines
Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
So we assume that a redirect at the point of the iommu will factor in iommu translation.
Quoted 4 lines
If so, I think you're trying to find out the closest upstream device X such that everything leading to X has ACS enabled. Every device below X can DMA freely to other devices below X, so they would all have to be in the same isolated group.
Yes
Quoted 1 line
I tried to work through some examples to develop some intuition about this:
(inserting fixed url)
Quoted 4 lines
http://www.asciiflow.com/#3736558963405980039
pci_acs_enabled(00:00.0)= 00:00.0 (on root bus (but doesn't it matter if 00:00.0 is PCIe or if RP has ACS?))Hmm, the latter is the assumption above. For the former, I think libvirt was probably assuming that PCI devices must have a PCIe device upstream from them because x86 doesn't have assignment friendly IOMMUs except on PCIe. I'll need to work on making that more generic.
Quoted 3 lines
pci_acs_enabled(00:01.0)= 00:01.0 (on root bus)pci_acs_enabled(01:00.0)= 01:00.0 (acs_dev= 00:01.0, 01:00.0 is not PCIe; seems wrong)Oops, I'm calling
pci_find_upstream_pcie_bridge()first on any of my input devices, so this was passing for me. I'll need to incorporate that generically.Quoted 2 lines
pci_acs_enabled(00:02.0)= 00:02.0 (on root bus; seems wrong if RP doesn't have ACS)Quoted 7 lines
Quoted 6 lines
Yeah, let me validate the libvirt assumption. I see ACS on my root port, so maybe they're just assuming it's always enabled or that the precedence favors IOMMU translation. I'm also starting to think that we might want "from" and "to" struct
pci_devparameters to make it more flexible where the iommu lives in the system.see comment above wrt root ports that have IOMMUs in them.
Quoted 8 lines
Except it really seems to be platform convention where the IOMMU lives. The DMAR for VT-d describes which devices and hierarchies a DRHD is used for and from that we can make assumptions about where it physically lives. AMD-Vi exposes a PCI device as the IOMMU, but it's just a peer function on the root bus. For now I'm just allowing
pci_acs_path_enabledto take NULL for and end, which means "up to the root bus".Quoted 4 lines
ATM, VT-d IOMMUs are only in the RCs, so, ACS at each downstream port
in a tree would/should return 'true' to theacs_enabledcheck when it gets to a Root port. For AMD-Vi, I thought the same held true, ATM, but I have to dig through
yet-another spec (or ask Joerg to check-in to this thread& provide the details).Quoted 8 lines
But are we programming to convention or spec? And I'm still confused about why we assume the root port isn't susceptible to redirection before IOMMU translation. One of the benefits of the
pci_acs_path_enable()API is that it pushes convention out to the IOMMU driver. So it's intel-iommu.c's problem whether to test for ACS support to the RC or to a given level (and for that matter know whether IOMMU translation takes precedence over redirection in the RC).Agreed. the best design here would be for the
intel-iommu.cto test for ACS wrt the root port (not to be confused with root complex) since Intel-IOMMUs are not 'attached' to a PCI bus. Now, AMD's IOMMUs are attached to a PCI bus, so maybe it can be simplified in that case.... but it may be best to mimic the iommu-set-acs-for-root-port attribute the same manner.Quoted 71 lines
Quoted 55 lines
Quoted 50 lines
Quoted 12 lines
Quoted 7 lines
Quoted 6 lines
pci_acs_enabled(02:00.0)= 00:02.0 (acs_dev= 00:02.0, 02:00.0 has no ACS cap)pci_acs_enabled(03:00.0)= 00:02.0 (acs_dev= 00:02.0)pci_acs_enabled(02:01.0)= 02:01.0 (acs_dev= 00:02.0, 02:01.0 has ACS enabled)pci_acs_enabled(04:00.0)= 04:00.0 (acs_dev= 02:01.0, 04:00.0 is not a bridge; seems wrong if 04:00 is a multi-function device)Quoted 5 lines
AIUI, ACS is not an endpoint property, so this is what should happen. I don't think multifunction plays a role other than how much do we trust the implementation to not allow back channels between functions (the answer should probably be not at all).
Quoted 12 lines
correct. ACS is a *bridge* property.
The unknown wrt multifunction devices is that such devices *could* be implemented by a hidden (not responding to PCI cfg accesses from downstream port) PCI bridge btwn the functions within a device.
Such a bridge could allow peer-to-peer xactions and there is no way for OS's to force ACS. So, one has to ask the hw vendors if such a hidden device exists in the implementation, and whether peer-to-peer is enabled/allowed -- a hidden PCI bridge/PCIe-switch could just be hardwired to push all IO to upstream port, and allow parent bridge re-route it back down if peer-to-peer is desired. Debate exists whether multifunction devices are 'secure' b/c of this unknown. Maybe a PCIe (min., SRIOV) spec change is needed in this area to determine this status about a device (via pci cfg/cap space).Well, there is actually a section of the ACS part of the spec identifying valid flags for multifunction devices. Secretly I'd like to use this as justification for blacklisting all multifunction devices that don't explicitly support ACS, but that makes for pretty course granularity. For instance, all these devices end up in a group:
+-14.0 ATI Technologies Inc SBx00 SMBus Controller +-14.2 ATI Technologies Inc SBx00 Azalia (Intel HDA) +-14.3 ATI Technologies Inc SB7x0/SB8x0/SB9x0 LPC host controller +-14.4-[05]----0e.0 VIA Technologies, Inc. VT6306/7/8 [Fire II(M)] IEEE 1394 OHCI Controller00:14.4 PCI bridge: ATI Technologies Inc SBx00 PCI to PCI Bridge (rev 40)
And these in another:
+-15.0-[06]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller +-15.1-[07]----00.0 Etron Technology, Inc. EJ168 USB 3.0 Host Controller +-15.2-[08]-- +-15.3-[09]--00:15.0 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 0) 00:15.1 PCI bridge: ATI Technologies Inc SB700/SB800/SB900 PCI to PCI bridge (PCIE port 1) 00:15.2 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 2) 00:15.3 PCI bridge: ATI Technologies Inc SB900 PCI to PCI bridge (PCIE port 3)
Quoted 5 lines
Am I misinterpreting the spec or is this the correct, if strict, interpretation?
Alex
Quoted 15 lines
Well, in digging into the ACS-support in Root port question above, I just found out about this ACS support status for multifunctions too. I need more time to read/digest, but a quick read says MFDs should have
an ACS cap with relevant RO-status& control bits to direct/control
peer-to-peer and ACS-upstream. Lack of an ACS struct implies(?) peer-to-peer can happen, and thus, the above have to be in the same iommu group. Unfortunately, I think a large lot of MFDs don't have ACS caps,
and don't/can't do peer-to-peer, so the above is heavy-handed, albeit to spec.
Maybe we need a (large?) pci-quirk for the list of existing
MFDs that don't have ACS caps that would enable the above devices to be in separate groups.
On the flip side, it solves some of the quirks for MFDs that
use the wrong BDF in their src-id dma packets! :) -- they default to the same group now...Quoted 1 line
Yep, sounds like you might agree with my patch, it's heavy handed, but
Yes, but we should design-in a quirk check list for MFDs,
b/c we already know some will fail when they should pass this check, b/c the hw was made post ACS, or the vendors didn't see the benefit of ACS reporting (even if the funcitonality was enabling bit-settings that did nothing hw-wise), b/c they didn't understand the use case (VFIO, dev-assignment to virt guests, etc.).Quoted 5 lines
Quoted 3 lines
seems to adhere to the spec. That probably just means we need an option to allow a more lenient interpretation, that maybe we don't have to support. Thanks,
Right. As I said, a hook to do quirk-level additions from the get-go would speed this expected need/addition.
Ok, a quirk should be easy there. Thanks,
Alex