Skip to contentSkip to conversation

iommu/s390: add iommu api for s390 pci devices

PATCH Applied Gerald Schaefer 2 participants 5 messages Aug 27, 2015 – Oct 6, 2015

Subsystems (6)
KVM PCI s390 IOMMU +2 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/s390: add iommu api for s390 pci devices

This adds an IOMMU API implementation for s390 PCI devices.

---
 MAINTAINERS                     |   7 +
 arch/s390/Kconfig               |   1 +
 arch/s390/include/asm/pci.h     |   4 +
 arch/s390/include/asm/pci_dma.h |   5 +-
 arch/s390/pci/pci_dma.c         |  37 +++--
 drivers/iommu/Kconfig           |   7 +
 drivers/iommu/Makefile          |   1 +
 drivers/iommu/s390-iommu.c      | 337 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 386 insertions(+), 13 deletions(-)
 create mode 100644 drivers/iommu/s390-iommu.c
MAINTAINERS +7 -0
index 23da5da..8845410 100644
@@ -8937,6 +8937,13 @@ F:	drivers/s390/net/*iucv*
 F:	include/net/iucv/
 F:	net/iucv/
 
+S390 IOMMU (PCI)
+M:	Gerald Schaefer <[email protected]>
+L:	[email protected]
+W:	http://www.ibm.com/developerworks/linux/linux390/
+S:	Supported
+F:	drivers/iommu/s390-iommu.c
+
 S3C24XX SD/MMC Driver
 M:	Ben Dooks <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
arch/s390/Kconfig +1 -0
index 1d57000..74db332 100644
@@ -582,6 +582,7 @@ menuconfig PCI
 	bool "PCI support"
 	select HAVE_DMA_ATTRS
 	select PCI_MSI
+	select IOMMU_SUPPORT
 	help
 	  Enable PCI support.
 
arch/s390/include/asm/pci.h +4 -0
index 34d9603..c873e68 100644
@@ -62,6 +62,8 @@ struct zpci_bar_struct {
 	u8		size;		/* order 2 exponent */
 };
 
+struct s390_domain;
+
 /* Private data per function */
 struct zpci_dev {
 	struct pci_dev	*pdev;
@@ -118,6 +120,8 @@ struct zpci_dev {
 
 	struct dentry	*debugfs_dev;
 	struct dentry	*debugfs_perf;
+
+	struct s390_domain *s390_domain; /* s390 IOMMU domain data */
 };
 
 static inline bool zdev_enabled(struct zpci_dev *zdev)
arch/s390/include/asm/pci_dma.h +4 -1
index 30b4c17..7a7abf1 100644
@@ -192,5 +192,8 @@ static inline unsigned long *get_st_pto(unsigned long entry)
 /* Prototypes */
 int zpci_dma_init_device(struct zpci_dev *);
 void zpci_dma_exit_device(struct zpci_dev *);
-
+void dma_free_seg_table(unsigned long);
+unsigned long *dma_alloc_cpu_table(void);
+void dma_cleanup_tables(unsigned long *);
+void dma_update_cpu_trans(unsigned long *, void *, dma_addr_t, int);
 #endif
arch/s390/pci/pci_dma.c +25 -12
index 37505b8..37d10f7 100644
@@ -24,7 +24,7 @@ static int zpci_refresh_global(struct zpci_dev *zdev)
 				  zdev->iommu_pages * PAGE_SIZE);
 }
 
-static unsigned long *dma_alloc_cpu_table(void)
+unsigned long *dma_alloc_cpu_table(void)
 {
 	unsigned long *table, *entry;
 
@@ -114,12 +114,12 @@ static unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr
 	return &pto[px];
 }
 
-static void dma_update_cpu_trans(struct zpci_dev *zdev, void *page_addr,
-				 dma_addr_t dma_addr, int flags)
+void dma_update_cpu_trans(unsigned long *dma_table, void *page_addr,
+			  dma_addr_t dma_addr, int flags)
 {
 	unsigned long *entry;
 
-	entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr);
+	entry = dma_walk_cpu_trans(dma_table, dma_addr);
 	if (!entry) {
 		WARN_ON_ONCE(1);
 		return;
@@ -156,7 +156,8 @@ static int dma_update_trans(struct zpci_dev *zdev, unsigned long pa,
 		goto no_refresh;
 
 	for (i = 0; i < nr_pages; i++) {
-		dma_update_cpu_trans(zdev, page_addr, dma_addr, flags);
+		dma_update_cpu_trans(zdev->dma_table, page_addr, dma_addr,
+				     flags);
 		page_addr += PAGE_SIZE;
 		dma_addr += PAGE_SIZE;
 	}
@@ -181,7 +182,7 @@ no_refresh:
 	return rc;
 }
 
-static void dma_free_seg_table(unsigned long entry)
+void dma_free_seg_table(unsigned long entry)
 {
 	unsigned long *sto = get_rt_sto(entry);
 	int sx;
@@ -193,21 +194,18 @@ static void dma_free_seg_table(unsigned long entry)
 	dma_free_cpu_table(sto);
 }
 
-static void dma_cleanup_tables(struct zpci_dev *zdev)
+void dma_cleanup_tables(unsigned long *table)
 {
-	unsigned long *table;
 	int rtx;
 
-	if (!zdev || !zdev->dma_table)
+	if (!table)
 		return;
 
-	table = zdev->dma_table;
 	for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
 		if (reg_entry_isvalid(table[rtx]))
 			dma_free_seg_table(table[rtx]);
 
 	dma_free_cpu_table(table);
-	zdev->dma_table = NULL;
 }
 
 static unsigned long __dma_alloc_iommu(struct zpci_dev *zdev,
@@ -416,6 +414,13 @@ int zpci_dma_init_device(struct zpci_dev *zdev)
 {
 	int rc;
 
+	/*
+	 * At this point, if the device is part of an IOMMU domain, this would
+	 * be a strong hint towards a bug in the IOMMU API (common) code and/or
+	 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
+	 */
+	WARN_ON(zdev->s390_domain);
+
 	spin_lock_init(&zdev->iommu_bitmap_lock);
 	spin_lock_init(&zdev->dma_table_lock);
 
@@ -450,8 +455,16 @@ out_clean:
 
 void zpci_dma_exit_device(struct zpci_dev *zdev)
 {
+	/*
+	 * At this point, if the device is part of an IOMMU domain, this would
+	 * be a strong hint towards a bug in the IOMMU API (common) code and/or
+	 * simultaneous access via IOMMU and DMA API. So let's issue a warning.
+	 */
+	WARN_ON(zdev->s390_domain);
+
 	zpci_unregister_ioat(zdev, 0);
-	dma_cleanup_tables(zdev);
+	dma_cleanup_tables(zdev->dma_table);
+	zdev->dma_table = NULL;
 	vfree(zdev->iommu_bitmap);
 	zdev->iommu_bitmap = NULL;
 	zdev->next_bit = 0;
drivers/iommu/Kconfig +7 -0
index e3b2c2e..0330fb4 100644
@@ -377,4 +377,11 @@ config ARM_SMMU_V3
 	  Say Y here if your system includes an IOMMU device implementing
 	  the ARM SMMUv3 architecture.
 
+config S390_IOMMU
+	def_bool y if S390 && PCI
+	depends on S390 && PCI
+	select IOMMU_API
+	help
+	  Support for the IOMMU API for s390 PCI devices.
+
 endif # IOMMU_SUPPORT
drivers/iommu/Makefile +1 -0
index dc6f511..c0a0649 100644
@@ -24,3 +24,4 @@ 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
+obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
drivers/iommu/s390-iommu.c new file +337 -0

337 lines not on this page · raw message

Signature · 1 line
2.3.8
maintainer
#1 lore raw

Hi Gerald,

thanks for your patch. It looks pretty good and addresses my previous review comments. I have a few questions, first one is how this operates with DMA-API on s390. Is there a seperate DMA-API implementation besides the IOMMU-API one for PCI devices?

My other question is inline:

drivers/iommu/s390-iommu.c line 33

Quoted 4 lines · Gerald Schaefer
drivers/iommu/s390-iommu.c new file +4 -0
+struct s390_domain_device {
+	struct list_head	list;
+	struct zpci_dev		*zdev;
+};

Instead of using your own struct here, have you considered using the struct iommu_group instead? The struct devices contains a pointer to an iommu_group and the struct itself contains pointers to the domain it is currently bound to.

Regards,

Joerg

#2 lore raw
Quoted 6 lines · On Tue, 29 Sep 2015 14:40:30 +0200 Joerg Roedel…

Hi Gerald,

thanks for your patch. It looks pretty good and addresses my previous review comments. I have a few questions, first one is how this operates with DMA-API on s390. Is there a seperate DMA-API implementation besides the IOMMU-API one for PCI devices?

Yes, the DMA API is already implemented in arch/s390/pci/pci_dma.c. I thought about moving it over to the new location in drivers/iommu/, but I don't see any benefit from it.

Also, the two APIs are quite different on s390 and must not be mixed-up. For example, we have optimizations in the DMA API to reduce TLB flushes based on iommu bitmap wrap-around, which is not possible for the map/unmap logic in the IOMMU API. There is also the requirement that each device has its own DMA page table (not shared), which is important for DMA API device recovery and map/unmap on s390.

Quoted 4 lines

My other question is inline:

On Thu, Aug 27, 2015 at 03:33:03PM +0200, Gerald Schaefer wrote:

Quoted 9 lines

drivers/iommu/s390-iommu.c line 33

Quoted 4 lines
drivers/iommu/s390-iommu.c new file +4 -0
+struct s390_domain_device {
+	struct list_head	list;
+	struct zpci_dev		*zdev;
+};

Instead of using your own struct here, have you considered using the struct iommu_group instead? The struct devices contains a pointer to an iommu_group and the struct itself contains pointers to the domain it is currently bound to.

Hmm, not sure how this can replace my own struct. I need the struct to maintain a list of all devices that share a dma page table. And the devices need to be added and removed to/from that list in attach/detach_dev.

I also need that list during map/unmap, in order to do a TLB flush for all affected devices, and this happens under a spin lock.

So I guess I cannot use the iommu_group->devices list, which is managed in add/remove_device and under a mutex, if that was on your mind.

Regards,
Gerald

maintainer
#3 lore raw
Quoted 3 lines · Gerald Schaefer

Yes, the DMA API is already implemented in arch/s390/pci/pci_dma.c. I thought about moving it over to the new location in drivers/iommu/, but I don't see any benefit from it.

Okay, this is true for now. At some point we hopefully have a common DMA-API implementation for all IOMMU driver, at which point s390 can make use of it too and abandon its own implementation.

Quoted 6 lines

Also, the two APIs are quite different on s390 and must not be mixed-up. For example, we have optimizations in the DMA API to reduce TLB flushes based on iommu bitmap wrap-around, which is not possible for the map/unmap logic in the IOMMU API. There is also the requirement that each device has its own DMA page table (not shared), which is important for DMA API device recovery and map/unmap on s390.

This sounds quite similar to what other IOMMU drivers also implement, especially the AMD IOMMU driver. It also uses non-shared page-tables for devices and implements the bitmap-allocator optimization.

Quoted 9 lines

Hmm, not sure how this can replace my own struct. I need the struct to maintain a list of all devices that share a dma page table. And the devices need to be added and removed to/from that list in attach/detach_dev.

I also need that list during map/unmap, in order to do a TLB flush for all affected devices, and this happens under a spin lock.

So I guess I cannot use the iommu_group->devices list, which is managed in add/remove_device and under a mutex, if that was on your mind.

Yeah, right. Thanks for the explanation.

Joerg

maintainer Applied
#4 lore raw
In reply to Gerald Schaefer
Quoted 4 lines · Gerald Schaefer

This adds an IOMMU API implementation for s390 PCI devices.

Quoted 11 lines
---
 MAINTAINERS                     |   7 +
 arch/s390/Kconfig               |   1 +
 arch/s390/include/asm/pci.h     |   4 +
 arch/s390/include/asm/pci_dma.h |   5 +-
 arch/s390/pci/pci_dma.c         |  37 +++--
 drivers/iommu/Kconfig           |   7 +
 drivers/iommu/Makefile          |   1 +
 drivers/iommu/s390-iommu.c      | 337 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 386 insertions(+), 13 deletions(-)
 create mode 100644 drivers/iommu/s390-iommu.c

Applied, thanks.