Skip to contentSkip to conversation

iommu/fsl: Add additional iommu attributes required by the PAMU driver.

PATCH Superseded Varun Sethi 4 participants 7 messages Jun 20, 2013 – Jun 26, 2013

Subsystems (2)

v16 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

PATCH v16 2/3 iommu/fsl: Add additional iommu attributes required by the PAMU driver.

Added the following domain attributes for the FSL PAMU driver:
1. Added new iommu stash attribute, which allows setting of the LIODN specific stash id parameter through IOMMU API.
2. Added an attribute for enabling/disabling DMA to a particular memory window.
3. Added domain attribute to check for PAMUV1 specific constraints.

---
v16 changes:
- rebased to 3.10-rc6
v15 changes:
- Moved fsl_pamu_stash.h under arch/powerpc/include/asm. v14 changes:
- Add FSL prefix to PAMU attributes.
v13 changes:
- created a new file include/linux/fsl_pamu_stash.h for stash attributes.
v12 changes:
- Moved PAMU specifc stash ids and structures to PAMU header file.
- no change in v11.
- no change in v10.

 arch/powerpc/include/asm/fsl_pamu_stash.h |   39 +++++++++++++++++++++++++++++
 include/linux/iommu.h                     |   16 ++++++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/include/asm/fsl_pamu_stash.h
arch/powerpc/include/asm/fsl_pamu_stash.h new file +39 -0
new file mode 100644
index 0000000..caa1b21
@@ -0,0 +1,39 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Copyright (C) 2013 Freescale Semiconductor, Inc.
+ *
+ */
+
+#ifndef __FSL_PAMU_STASH_H
+#define __FSL_PAMU_STASH_H
+
+/* cache stash targets */
+enum pamu_stash_target {
+	PAMU_ATTR_CACHE_L1 = 1,
+	PAMU_ATTR_CACHE_L2,
+	PAMU_ATTR_CACHE_L3,
+};
+
+/*
+ * This attribute allows configuring stashig specific parameters
+ * in the PAMU hardware.
+ */
+
+struct pamu_stash_attribute {
+	u32 	cpu;	/* cpu number */
+	u32 	cache;	/* cache to stash to: L1,L2,L3 */
+};
+
+#endif  /* __FSL_PAMU_STASH_H */
include/linux/iommu.h +16 -0
index 3aeb730..7ea319e 100644
@@ -58,10 +58,26 @@ struct iommu_domain {
 #define IOMMU_CAP_CACHE_COHERENCY	0x1
 #define IOMMU_CAP_INTR_REMAP		0x2	/* isolates device intrs */
 
+/*
+ * Following constraints are specifc to FSL_PAMUV1:
+ *  -aperture must be power of 2, and naturally aligned
+ *  -number of windows must be power of 2, and address space size
+ *   of each window is determined by aperture size / # of windows
+ *  -the actual size of the mapped region of a window must be power
+ *   of 2 starting with 4KB and physical address must be naturally
+ *   aligned.
+ * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
+ * The caller can invoke iommu_domain_get_attr to check if the underlying
+ * iommu implementation supports these constraints.
+ */
+
 enum iommu_attr {
 	DOMAIN_ATTR_GEOMETRY,
 	DOMAIN_ATTR_PAGING,
 	DOMAIN_ATTR_WINDOWS,
+	DOMAIN_ATTR_FSL_PAMU_STASH,
+	DOMAIN_ATTR_FSL_PAMU_ENABLE,
+	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_MAX,
 };
 
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v16 3/3 iommu/fsl: Freescale PAMU driver and iommu implementation.

Following is a brief description of the PAMU hardware:
PAMU determines what action to take and whether to authorize the action on the basis of the memory address, a Logical IO Device Number (LIODN), and PAACT table (logically) indexed by LIODN and address. Hardware devices which need to access memory must provide an LIODN in addition to the memory address.

Peripheral Access Authorization and Control Tables (PAACTs) are the primary data structures used by PAMU. A PAACT is a table of peripheral access authorization and control entries (PAACE).Each PAACE defines the range of I/O bus address space that is accessible by the LIOD and the associated access capabilities.

There are two types of PAACTs: primary PAACT (PPAACT) and secondary PAACT (SPAACT).A given physical I/O device may be able to act as one or more independent logical I/O devices (LIODs). Each such logical I/O device is assigned an identifier called logical I/O device number (LIODN). A LIODN is allocated a contiguous portion of the I/O bus address space called the DSA window for performing DSA operations. The DSA window may optionally be divided into multiple sub-windows, each of which may be used to map to a region in system storage space. The first sub-window is referred to as the primary sub-window and the remaining are called secondary sub-windows.

This patch provides the PAMU driver (fsl_pamu.c) and the corresponding IOMMU API implementation (fsl_pamu_domain.c). The PAMU hardware driver (fsl_pamu.c) has been derived from the work done by Ashish Kalra and Timur Tabi.

---
changes in v16
- rebased to kernel 3.10-rc6
changes in v15:
- Updated path for fsl_pamu_stash.h.
changes in v14:
- Use updated PAMU specific attributes.
changes in v13:
- Use the new header drivers/iommu/pci.h
- fix geometry configured check.
changes in v12:
- Use is_power_of_2 for checking alignement.
- Check for multifucntion PCI device ACS flags for determining device groups.
- Fix get_stash_id function.
- Don't crash in case of access violations, disable the LIODN.
- Don't use list_empty while traversing list using list for each entry.
- Move stash structure and ids to PAMU header files.
- Fix geometry size calculation.
changes in v11:
- changed iova to dma_addr_t in iova_to_phys API.
changes in v10:
- Support for new guts compatibe string for T4 & B4 devices.
- Modified comment about port ID and mentioned the errata number.
- Fixed the issue where data pointer was not freed in case of a an error.
- Pass data pointer while freeing irq.
- Whle initializing the SPAACE entry clear the valid bit.
changes in v9:
- Merged and createad a single function to delete
a device from domain list.
- Refactored the add_device API code.
- Renamed the paace and spaace init fucntions.
- Renamed functions for mapping windows and subwindows.
- Changed the MAX LIODN value to MAX value u-boot can
program.
- Hard coded maximum number of subwindows.
changes in v8:
- implemented the new API for window based IOMMUs.
changes in v7:
- Set max_subwidows in the geometry attribute.
- Add checking for maximum supported LIODN value.
- Use upper_32_bits and lower_32_bits macros while
intializing PAMU data structures.
changes in v6:
- Simplified complex conditional statements.
- Fixed indentation issues.
- Added comments for IOMMU API implementation.
changes in v5:
- Addressed comments from Timur.
changes in v4:
- Addressed comments from Timur and Scott.
changes in v3:
- Addressed comments by Kumar Gala
- dynamic fspi allocation
- fixed alignment check in map and unmap

 arch/powerpc/sysdev/fsl_pci.h   |    5 +
 drivers/iommu/Kconfig           |   10 +
 drivers/iommu/Makefile          |    1 +
 drivers/iommu/fsl_pamu.c        | 1309 +++++++++++++++++++++++++++++++++++++++
 drivers/iommu/fsl_pamu.h        |  410 ++++++++++++
 drivers/iommu/fsl_pamu_domain.c | 1142 ++++++++++++++++++++++++++++++++++
 drivers/iommu/fsl_pamu_domain.h |   85 +++
 7 files changed, 2962 insertions(+), 0 deletions(-)
 create mode 100644 drivers/iommu/fsl_pamu.c
 create mode 100644 drivers/iommu/fsl_pamu.h
 create mode 100644 drivers/iommu/fsl_pamu_domain.c
 create mode 100644 drivers/iommu/fsl_pamu_domain.h
arch/powerpc/sysdev/fsl_pci.h +5 -0
index 72b5625..1e01291 100644
@@ -16,6 +16,11 @@
 
 struct platform_device;
 
+
+/* FSL PCI controller BRR1 register */
+#define PCI_FSL_BRR1      0xbf8
+#define PCI_FSL_BRR1_VER 0xffff
+
 #define PCIE_LTSSM	0x0404		/* PCIE Link Training and Status */
 #define PCIE_LTSSM_L0	0x16		/* L0 state */
 #define PCIE_IP_REV_2_2		0x02080202 /* PCIE IP block version Rev2.2 */
drivers/iommu/Kconfig +10 -0
index c332fb9..f97db88 100644
@@ -17,6 +17,16 @@ config OF_IOMMU
        def_bool y
        depends on OF
 
+config FSL_PAMU
+	bool "Freescale IOMMU support"
+	depends on PPC_E500MC
+	select IOMMU_API
+	select GENERIC_ALLOCATOR
+	help
+	  Freescale PAMU support. PAMU is the IOMMU present on Freescale QorIQ platforms.
+	  PAMU can authorize memory access, remap the memory address, and remap I/O
+	  transaction types.
+
 # MSM IOMMU support
 config MSM_IOMMU
 	bool "MSM IOMMU Support"
drivers/iommu/Makefile +1 -0
index ef0e520..027d1af 100644
@@ -15,3 +15,4 @@ obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_SHMOBILE_IOMMU) += shmobile-iommu.o
 obj-$(CONFIG_SHMOBILE_IPMMU) += shmobile-ipmmu.o
+obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
drivers/iommu/fsl_pamu.c new file +1309 -0

1,309 lines not on this page · raw message

drivers/iommu/fsl_pamu.h new file +410 -0

410 lines not on this page · raw message

drivers/iommu/fsl_pamu_domain.c new file +1142 -0

1,142 lines not on this page · raw message

drivers/iommu/fsl_pamu_domain.h new file +85 -0

85 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#6 lore raw

PATCH v11 1/3 powerpc: Add iommu domain pointer to device archdata

In reply to Varun Sethi

Add an iommu domain pointer to device (powerpc) archdata. Devices are attached to iommu domains and this pointer provides a mechanism to correlate between a device and the associated iommu domain. This field is set when a device is attached to a domain.

---
- no change in v11.
- no change in v10.
- Added CONFIG_IOMMU_API in v9.

 arch/powerpc/include/asm/device.h |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/device.h +3 -0
index 77e97dd..38faede 100644
@@ -28,6 +28,9 @@ struct dev_archdata {
 		void		*iommu_table_base;
 	} dma_data;
 
+#ifdef CONFIG_IOMMU_API
+	void			*iommu_domain;
+#endif
 #ifdef CONFIG_SWIOTLB
 	dma_addr_t		max_direct_dma_addr;
 #endif
Signature · 1 line
1.7.4.1
maintainer
#2 lore raw

Re: [PATCH 3/3 v16] iommu/fsl: Freescale PAMU driver and iommu implementation.

In reply to Varun Sethi
Quoted 3 lines · Varun Sethi

This patch provides the PAMU driver (fsl_pamu.c) and the corresponding IOMMU API implementation (fsl_pamu_domain.c). The PAMU hardware driver (fsl_pamu.c) has been derived from the work done by Ashish Kalra and Timur Tabi.

AlexW,

can you please have a look at the group-code again and ack the patch if it looks right to you?

Thanks,

Joerg

#3 lore raw

Re: [PATCH 3/3 v16] iommu/fsl: Freescale PAMU driver and iommu implementation.

In reply to Varun Sethi
Quoted 13 lines · Varun Sethi
drivers/iommu/fsl_pamu_domain.c new file +13 -0
+#define REQ_ACS_FLAGS	(PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
+
+static struct iommu_group *get_device_iommu_group(struct device *dev)
+{
+	struct iommu_group *group;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		group = iommu_group_alloc();
+
+	return group;
+}
+

[snip]

Quoted 1 line

+

This really gets parent or peer, right?

drivers/iommu/fsl_pamu_domain.c line 927

Quoted 6 lines
drivers/iommu/fsl_pamu_domain.c new file +6 -0
+static struct iommu_group *get_peer_pci_device_group(struct pci_dev *pdev)
+{
+	struct iommu_group *group = NULL;
+
+	/* check if this is the first device on the bus*/
+	if (pdev->bus_list.next == pdev->bus_list.prev) {

It's a list_head, use list functions. The list implementation should be treated as opaque.

if (list_is_singular(&pdev->bus_list))

drivers/iommu/fsl_pamu_domain.c line 935

Quoted 8 lines
drivers/iommu/fsl_pamu_domain.c new file +8 -0
+		struct pci_bus *bus = pdev->bus->parent;
+		/* Traverese the parent bus list to get
+		 * pdev & dev for the sibling device.
+		 */
+		while (bus) {
+			if (!list_empty(&bus->devices)) {
+				pdev = container_of(bus->devices.next,
+					            struct pci_dev, bus_list);

pdev = list_first_entry(&bus->devices, struct pci_dev, bus_list);

drivers/iommu/fsl_pamu_domain.c line 939

Quoted 4 lines
drivers/iommu/fsl_pamu_domain.c new file +4 -0
+				group = iommu_group_get(&pdev->dev);
+				break;
+			} else
+				bus = bus->parent;

Is this ever reached? Don't you always have bus->self?

drivers/iommu/fsl_pamu_domain.c line 946

Quoted 7 lines
drivers/iommu/fsl_pamu_domain.c new file +7 -0
+		}
+	} else {
+		/*
+		 * Get the pdev & dev for the sibling device
+		 */
+		pdev = container_of(pdev->bus_list.prev,
+				    struct pci_dev, bus_list);

How do you know if you're at the head or tail of the list?

struct pci_dev *tmp;

list_for_each_entry(tmp, &pdev->bus_list, bus_list) {
	if (tmp == pdev)
		continue;

	group = iommu_group_get(&tmp->dev);
	break;
}
Quoted 25 lines
drivers/iommu/fsl_pamu_domain.c new file +25 -0
+		group = iommu_group_get(&pdev->dev);
+	}
+
+	return group;
+}
+
+static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
+{
+	struct iommu_group *group = NULL;
+	struct pci_dev *bridge, *dma_pdev = NULL;
+	struct pci_controller *pci_ctl;
+	bool pci_endpt_partioning;
+
+	pci_ctl = pci_bus_to_host(pdev->bus);
+	pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
+	/* We can partition PCIe devices so assign device group to the device */
+	if (pci_endpt_partioning) {
+		bridge = pci_find_upstream_pcie_bridge(pdev);
+		if (bridge) {
+			if (pci_is_pcie(bridge))
+				dma_pdev = pci_get_domain_bus_and_slot(
+						pci_domain_nr(pdev->bus),
+						bridge->subordinate->number, 0);
+			if (!dma_pdev)
+				dma_pdev = pci_dev_get(bridge);

drivers/iommu/fsl_pamu_domain.c line 981

Quoted 10 lines
drivers/iommu/fsl_pamu_domain.c new file +10 -0
+		} else
+			dma_pdev = pci_dev_get(pdev);
+
+		/* Account for quirked devices */
+		swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
+
+		/*
+		 * If it's a multifunction device that does not support our
+		 * required ACS flags, add to the same group as function 0.
+		 */

See c14d2690 in Joerg's next tree, using function 0 was a poor assumption.

drivers/iommu/fsl_pamu_domain.c line 990

Quoted 9 lines
drivers/iommu/fsl_pamu_domain.c new file +9 -0
+		if (dma_pdev->multifunction &&
+		    !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
+			swap_pci_ref(&dma_pdev,
+				     pci_get_slot(dma_pdev->bus,
+						  PCI_DEVFN(PCI_SLOT(dma_pdev->devfn),
+						  0)));
+
+		group = get_device_iommu_group(&pdev->dev);
+		pci_dev_put(pdev);

What was the point of all the above if we use pdev here instead of dma_pdev? Wrong device and broken reference counting. This also isn't testing ACS all the way up to the root complex or controller.

Quoted 65 lines
drivers/iommu/fsl_pamu_domain.c new file +65 -0
+		/*
+		 * PCIe controller is not a paritionable entity
+		 * free the controller device iommu_group.
+		 */
+		if (pci_ctl->parent->iommu_group)
+			iommu_group_remove_device(pci_ctl->parent);
+	} else {
+		/*
+		 * All devices connected to the controller will share the
+		 * PCI controllers device group. If this is the first
+		 * device to be probed for the pci controller, copy the
+		 * device group information from the PCI controller device
+		 * node and remove the PCI controller iommu group.
+		 * For subsequent devices, the iommu group information can
+		 * be obtained from sibling devices (i.e. from the bus_devices
+		 * link list).
+		 */
+		if (pci_ctl->parent->iommu_group) {
+			group = get_device_iommu_group(pci_ctl->parent);
+			iommu_group_remove_device(pci_ctl->parent);
+		} else
+			group = get_peer_pci_device_group(pdev);
+	}
+
+	return group;
+}
+
+static int fsl_pamu_add_device(struct device *dev)
+{
+	struct iommu_group *group = NULL;
+	struct pci_dev *pdev;
+	const u32 *prop;
+	int ret, len;
+
+	/*
+	 * For platform devices we allocate a separate group for
+	 * each of the devices.
+	 */
+	if (dev->bus == &pci_bus_type) {
+		pdev = to_pci_dev(dev);
+		/* Don't create device groups for virtual PCI bridges */
+		if (pdev->subordinate)
+			return 0;
+
+		group = get_pci_device_group(pdev);
+
+	} else {
+		prop = of_get_property(dev->of_node, "fsl,liodn", &len);
+		if (prop)
+			group = get_device_iommu_group(dev);
+	}
+
+	if (!group || IS_ERR(group))
+		return PTR_ERR(group);
+
+	ret = iommu_group_add_device(group, dev);
+
+	iommu_group_put(group);
+	return ret;
+}
+
+static void fsl_pamu_remove_device(struct device *dev)
+{
+	iommu_group_remove_device(dev);
+}
#4 lore raw

RE: [PATCH 3/3 v16] iommu/fsl: Freescale PAMU driver and iommu implementation.

Quoted 57 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, June 25, 2013 10:27 AM
To: Sethi Varun-B16395
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; linuxppc- dev-uLR06cmDAlY/[email protected]; [email protected]; [email protected]; [email protected]; Yoder Stuart-B08248; Wood Scott-B07421; Timur Tabi
Subject: Re: [PATCH 3/3 v16] iommu/fsl: Freescale PAMU driver and iommu
implementation.

Quoted 1 line · Varun Sethi

+#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR |

PCI_ACS_UF)

Quoted 12 lines
drivers/iommu/fsl_pamu_domain.c new file +12 -0
+
+static struct iommu_group *get_device_iommu_group(struct device *dev)
+{
+	struct iommu_group *group;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		group = iommu_group_alloc();
+
+	return group;
+}
+

[snip]

Quoted 1 line

+

This really gets parent or peer, right?

drivers/iommu/fsl_pamu_domain.c line 927

Quoted 6 lines
drivers/iommu/fsl_pamu_domain.c new file +6 -0
+static struct iommu_group *get_peer_pci_device_group(struct pci_dev
+*pdev) {
+	struct iommu_group *group = NULL;
+
+	/* check if this is the first device on the bus*/
+	if (pdev->bus_list.next == pdev->bus_list.prev) {

It's a list_head, use list functions. The list implementation should be treated as opaque.

if (list_is_singular(&pdev->bus_list))

drivers/iommu/fsl_pamu_domain.c line 935

Quoted 8 lines
drivers/iommu/fsl_pamu_domain.c new file +8 -0
+		struct pci_bus *bus = pdev->bus->parent;
+		/* Traverese the parent bus list to get
+		 * pdev & dev for the sibling device.
+		 */
+		while (bus) {
+			if (!list_empty(&bus->devices)) {
+				pdev = container_of(bus->devices.next,
+					            struct pci_dev, bus_list);

pdev = list_first_entry(&bus->devices, struct pci_dev, bus_list);

Quoted 7 lines

drivers/iommu/fsl_pamu_domain.c line 939

Quoted 4 lines
drivers/iommu/fsl_pamu_domain.c new file +4 -0
+				group = iommu_group_get(&pdev->dev);
+				break;
+			} else
+				bus = bus->parent;

Is this ever reached? Don't you always have bus->self?

[Sethi Varun-B16395] Not sure I understand. Trying to get the group information from the parent bus, if there are no sibling devices on the current bus.

Quoted 50 lines

drivers/iommu/fsl_pamu_domain.c line 946

Quoted 7 lines
drivers/iommu/fsl_pamu_domain.c new file +7 -0
+		}
+	} else {
+		/*
+		 * Get the pdev & dev for the sibling device
+		 */
+		pdev = container_of(pdev->bus_list.prev,
+				    struct pci_dev, bus_list);

How do you know if you're at the head or tail of the list?

struct pci_dev *tmp;

list_for_each_entry(tmp, &pdev->bus_list, bus_list) {
	if (tmp == pdev)
		continue;

	group = iommu_group_get(&tmp->dev);
	break;
}
Quoted 6 lines
drivers/iommu/fsl_pamu_domain.c new file +6 -0
+		group = iommu_group_get(&pdev->dev);
+	}
+
+	return group;
+}
+

drivers/iommu/fsl_pamu_domain.c

Quoted 10 lines
drivers/iommu/fsl_pamu_domain.c new file +10 -0
+static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
+{
+	struct iommu_group *group = NULL;
+	struct pci_dev *bridge, *dma_pdev = NULL;
+	struct pci_controller *pci_ctl;
+	bool pci_endpt_partioning;
+
+	pci_ctl = pci_bus_to_host(pdev->bus);
+	pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
+	/* We can partition PCIe devices so assign device group to the

device */

Quoted 14 lines
drivers/iommu/fsl_pamu_domain.c new file +14 -0
+	if (pci_endpt_partioning) {
+		bridge = pci_find_upstream_pcie_bridge(pdev);
+		if (bridge) {
+			if (pci_is_pcie(bridge))
+				dma_pdev = pci_get_domain_bus_and_slot(
+						pci_domain_nr(pdev->bus),
+						bridge->subordinate->number, 0);
+			if (!dma_pdev)
+				dma_pdev = pci_dev_get(bridge);
+		} else
+			dma_pdev = pci_dev_get(pdev);
+
+		/* Account for quirked devices */
+		swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
Quoted 8 lines

drivers/iommu/fsl_pamu_domain.c line 981

Quoted 5 lines
drivers/iommu/fsl_pamu_domain.c new file +5 -0
+
+		/*
+		 * If it's a multifunction device that does not support our
+		 * required ACS flags, add to the same group as function 0.
+		 */

See c14d2690 in Joerg's next tree, using function 0 was a poor assumption.

[Sethi Varun-B16395] ok.

Quoted 14 lines

drivers/iommu/fsl_pamu_domain.c line 990

Quoted 11 lines
drivers/iommu/fsl_pamu_domain.c new file +5 -0
+		if (dma_pdev->multifunction &&
+		    !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
+			swap_pci_ref(&dma_pdev,
+				     pci_get_slot(dma_pdev->bus,
+						  PCI_DEVFN(PCI_SLOT(dma_pdev-

devfn),

drivers/iommu/fsl_pamu_domain.c new file +4 -0
+						  0)));
+
+		group = get_device_iommu_group(&pdev->dev);
+		pci_dev_put(pdev);

What was the point of all the above if we use pdev here instead of dma_pdev? Wrong device and broken reference counting.

[Sethi Varun-B16395] Will fix this

This also isn't

Quoted 1 line

testing ACS all the way up to the root complex or controller.

[Sethi Varun-B16395] In our case the IOMMU can differentiate transactions based on the LIODN. The PCIe controller can generate a unique LIODN based on the bus,device,function number. I believe this would even be true for devices connected to a PCIe bridge (and not on the root bus). So, do we still need to check for ACS up to the root node?

-Varun

#5 lore raw

Re: [PATCH 3/3 v16] iommu/fsl: Freescale PAMU driver and iommu implementation.

Quoted 63 lines · On Wed, 2013-06-26 at 06:24 +0000, Sethi Varun-B16395 wrote:
Quoted 56 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, June 25, 2013 10:27 AM
To: Sethi Varun-B16395
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; linuxppc- dev-uLR06cmDAlY/[email protected]; [email protected]; [email protected]; [email protected]; Yoder Stuart-B08248; Wood Scott-B07421; Timur Tabi
Subject: Re: [PATCH 3/3 v16] iommu/fsl: Freescale PAMU driver and iommu
implementation.

Quoted 1 line · Varun Sethi

+#define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR |

PCI_ACS_UF)

Quoted 12 lines
drivers/iommu/fsl_pamu_domain.c new file +12 -0
+
+static struct iommu_group *get_device_iommu_group(struct device *dev)
+{
+	struct iommu_group *group;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		group = iommu_group_alloc();
+
+	return group;
+}
+

[snip]

Quoted 1 line

+

This really gets parent or peer, right?

drivers/iommu/fsl_pamu_domain.c line 927

Quoted 6 lines
drivers/iommu/fsl_pamu_domain.c new file +6 -0
+static struct iommu_group *get_peer_pci_device_group(struct pci_dev
+*pdev) {
+	struct iommu_group *group = NULL;
+
+	/* check if this is the first device on the bus*/
+	if (pdev->bus_list.next == pdev->bus_list.prev) {

It's a list_head, use list functions. The list implementation should be treated as opaque.

if (list_is_singular(&pdev->bus_list))

drivers/iommu/fsl_pamu_domain.c line 935

Quoted 8 lines
drivers/iommu/fsl_pamu_domain.c new file +8 -0
+		struct pci_bus *bus = pdev->bus->parent;
+		/* Traverese the parent bus list to get
+		 * pdev & dev for the sibling device.
+		 */
+		while (bus) {
+			if (!list_empty(&bus->devices)) {
+				pdev = container_of(bus->devices.next,
+					            struct pci_dev, bus_list);

pdev = list_first_entry(&bus->devices, struct pci_dev, bus_list);

Quoted 7 lines

drivers/iommu/fsl_pamu_domain.c line 939

Quoted 4 lines
drivers/iommu/fsl_pamu_domain.c new file +4 -0
+				group = iommu_group_get(&pdev->dev);
+				break;
+			} else
+				bus = bus->parent;

Is this ever reached? Don't you always have bus->self?

Quoted 3 lines

[Sethi Varun-B16395] Not sure I understand. Trying to get the group information from the parent bus, if there are no sibling devices on the current bus.

I assume there's always a bridge on a bus, but maybe that bridge (parent->self) is not in the list of parent->devices? Is that the case? If not, then there's always a device on the bus, the bridge that created it.

Quoted 76 lines
Quoted 50 lines

drivers/iommu/fsl_pamu_domain.c line 946

Quoted 7 lines
drivers/iommu/fsl_pamu_domain.c new file +7 -0
+		}
+	} else {
+		/*
+		 * Get the pdev & dev for the sibling device
+		 */
+		pdev = container_of(pdev->bus_list.prev,
+				    struct pci_dev, bus_list);

How do you know if you're at the head or tail of the list?

struct pci_dev *tmp;

list_for_each_entry(tmp, &pdev->bus_list, bus_list) {
	if (tmp == pdev)
		continue;

	group = iommu_group_get(&tmp->dev);
	break;
}
Quoted 6 lines
drivers/iommu/fsl_pamu_domain.c new file +6 -0
+		group = iommu_group_get(&pdev->dev);
+	}
+
+	return group;
+}
+

drivers/iommu/fsl_pamu_domain.c

Quoted 10 lines
drivers/iommu/fsl_pamu_domain.c new file +10 -0
+static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
+{
+	struct iommu_group *group = NULL;
+	struct pci_dev *bridge, *dma_pdev = NULL;
+	struct pci_controller *pci_ctl;
+	bool pci_endpt_partioning;
+
+	pci_ctl = pci_bus_to_host(pdev->bus);
+	pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
+	/* We can partition PCIe devices so assign device group to the

device */

Quoted 14 lines
drivers/iommu/fsl_pamu_domain.c new file +14 -0
+	if (pci_endpt_partioning) {
+		bridge = pci_find_upstream_pcie_bridge(pdev);
+		if (bridge) {
+			if (pci_is_pcie(bridge))
+				dma_pdev = pci_get_domain_bus_and_slot(
+						pci_domain_nr(pdev->bus),
+						bridge->subordinate->number, 0);
+			if (!dma_pdev)
+				dma_pdev = pci_dev_get(bridge);
+		} else
+			dma_pdev = pci_dev_get(pdev);
+
+		/* Account for quirked devices */
+		swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
Quoted 8 lines

drivers/iommu/fsl_pamu_domain.c line 981

Quoted 5 lines
drivers/iommu/fsl_pamu_domain.c new file +5 -0
+
+		/*
+		 * If it's a multifunction device that does not support our
+		 * required ACS flags, add to the same group as function 0.
+		 */

See c14d2690 in Joerg's next tree, using function 0 was a poor assumption.

[Sethi Varun-B16395] ok.

Quoted 14 lines

drivers/iommu/fsl_pamu_domain.c line 990

Quoted 11 lines
drivers/iommu/fsl_pamu_domain.c new file +5 -0
+		if (dma_pdev->multifunction &&
+		    !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS))
+			swap_pci_ref(&dma_pdev,
+				     pci_get_slot(dma_pdev->bus,
+						  PCI_DEVFN(PCI_SLOT(dma_pdev-

devfn),

drivers/iommu/fsl_pamu_domain.c new file +4 -0
+						  0)));
+
+		group = get_device_iommu_group(&pdev->dev);
+		pci_dev_put(pdev);

What was the point of all the above if we use pdev here instead of dma_pdev? Wrong device and broken reference counting.

[Sethi Varun-B16395] Will fix this

Quoted 8 lines

This also isn't

Quoted 1 line

testing ACS all the way up to the root complex or controller.

[Sethi Varun-B16395] In our case the IOMMU can differentiate transactions based on the LIODN. The PCIe controller can generate a unique LIODN based on the bus,device,function number. I believe this would even be true for devices connected to a PCIe bridge (and not on the root bus). So, do we still need to check for ACS up to the root node?

ACS is the PCI capability that tells us whether a device allows peer-to-peer or requires all DMA to be forwarded upstream. At any step along the way from an endpoint to the IOMMU the transaction can be re-routed. We therefore need to test the ACS property not only of the endpoint, but every device between the endpoint and the IOMMU to form the IOMMU groups. An IOMMU cannot isolate a device if a DMA access is re-routed by a link in the topology that prevents the IOMMU from even being part of the transaction. Thanks,

Alex