Skip to contentSkip to conversation

Factor VT-d KVM functions into a generic API (with multiple device assignment support)

PATCH In review Joerg Roedel 5 participants 19 messages Dec 2, 2008 – Dec 4, 2008

Subsystems (6)
x86 KVM PCI Driver core +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Factor VT-d KVM functions into a generic API (with multiple device assignment support)

Hi,

this patch series makes the current KVM device passthrough code generic enough so that other IOMMU implementation can also plug into this code. It works by factoring the functions Vt-d code exports to KVM into a generic interface which allows different backends.

This is the second version of the patchset. The most important change to the previous version is that this patchset was rebased to the improved API from Han Weidong which supports multiple devices per IOMMU domain.

For completeness, this series also includes the patches from Han with some cleanups. So this patchset can be applied on current avi/master tree.

For testing this code can also be pulled against avi/master tree from

git://git.kernel.org/pub/scm/linux/kernel/git/joro/linux-2.6-iommu.git iommu-api

This a basic implementation of a generic interface. It can and should be improved later to support more types of hardware IOMMUs then VT-d and AMD IOMMU.

Since I have no VT-d hardware available these patches are only compile tested for now.

Please review, comment and test these patches.

Thanks,

Joerg

diffstat:

 arch/ia64/Kconfig               |    3 +
 arch/ia64/kvm/Makefile          |    4 +-
 arch/x86/Kconfig                |    3 +
 arch/x86/include/asm/kvm_host.h |    3 +-
 arch/x86/kvm/Makefile           |    4 +-
 drivers/base/Makefile           |    1 +
 drivers/base/iommu.c            |  100 ++++++
 drivers/pci/dmar.c              |   15 +-
 drivers/pci/intel-iommu.c       |  738 +++++++++++++++++++++++++++++++++------
 include/linux/dma_remapping.h   |   21 +-
 include/linux/dmar.h            |    2 +
 include/linux/intel-iommu.h     |   13 +-
 include/linux/iommu.h           |  109 ++++++
 include/linux/kvm.h             |    5 +
 include/linux/kvm_host.h        |   23 +-
 virt/kvm/{vtd.c => iommu.c}     |  131 +++++---
 virt/kvm/kvm_main.c             |   49 +++-
#1 lore raw

PATCH 1/12 VT-d: Support multiple device assignment for KVM

From: Weidong Han <[email protected]>

In order to support multiple device assignment for KVM, this patch does following main changes:

- extend dmar_domain to own multiple devices from different iommus, use a bitmap of iommus to replace iommu pointer in dmar_domain.
- implement independent low level functions for kvm, then won't impact native VT-d.
- "SAGAW" capability may be different across iommus, that's to say the VT-d page table levels may be different among iommus. This patch uses a defaut agaw, and skip top levels of page tables for iommus which have smaller agaw than default.
- rename the APIs for kvm VT-d, make it more readable.

[Joerg: coding style cleanups]

---
 drivers/pci/dmar.c            |   15 +-
 drivers/pci/intel-iommu.c     |  696 ++++++++++++++++++++++++++++++++++------
 include/linux/dma_remapping.h |   21 +-
 include/linux/dmar.h          |    2 +
 include/linux/intel-iommu.h   |   21 +-
 5 files changed, 636 insertions(+), 119 deletions(-)
drivers/pci/dmar.c +14 -1
index 691b3ad..d54d3db 100644
@@ -484,13 +484,14 @@ void __init detect_intel_iommu(void)
 	dmar_tbl = NULL;
 }
 
-
 int alloc_iommu(struct dmar_drhd_unit *drhd)
 {
 	struct intel_iommu *iommu;
 	int map_size;
 	u32 ver;
 	static int iommu_allocated = 0;
+	unsigned long sagaw;
+	int agaw;
 
 	iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
 	if (!iommu)
@@ -506,6 +507,18 @@ int alloc_iommu(struct dmar_drhd_unit *drhd)
 	iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
 	iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
 
+	/* set agaw, "SAGAW" may be different across iommus */
+	sagaw = cap_sagaw(iommu->cap);
+	for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH);
+	     agaw >= 0; agaw--)
+		if (test_bit(agaw, &sagaw))
+			break;
+	if (agaw < 0) {
+		printk(KERN_ERR "IOMMU: unsupported sagaw %lx\n", sagaw);
+		goto error;
+	}
+	iommu->agaw = agaw;
+
 	/* the registers might be more than one page */
 	map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
 		cap_max_fault_reg_offset(iommu->cap));
drivers/pci/intel-iommu.c +591 -105

1,044 lines not on this page · raw message

include/linux/dma_remapping.h +16 -5
index 952df39..b4200bd 100644
@@ -111,11 +111,21 @@ struct dma_pte {
 		(p).val |= ((addr) & VTD_PAGE_MASK); } while (0)
 #define dma_pte_present(p) (((p).val & 3) != 0)
 
+/* domain flags, one domain owns one device by default */
+
+/* devices under the same p2p bridge are owned in one domain */
+#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES	(1 << 0)
+
+/* domain represents a virtual machine, more than one devices
+ * across iommus may be owned in one domain, e.g. kvm guest.
+ */
+#define DOMAIN_FLAG_VIRTUAL_MACHINE		(1 << 1)
+
 struct intel_iommu;
 
 struct dmar_domain {
 	int	id;			/* domain id */
-	struct intel_iommu *iommu;	/* back pointer to owning iommu */
+	unsigned long iommu_bmp;	/* bitmap of iommus this domain uses*/
 
 	struct list_head devices; 	/* all devices' list */
 	struct iova_domain iovad;	/* iova's that belong to this domain */
@@ -123,12 +133,13 @@ struct dmar_domain {
 	struct dma_pte	*pgd;		/* virtual address */
 	spinlock_t	mapping_lock;	/* page table lock */
 	int		gaw;		/* max guest address width */
+	int		agaw;		/* adjusted guest address width */
 
-	/* adjusted guest address width, 0 is level 2 30-bit */
-	int		agaw;
+	int		flags;		/* domain flag */
 
-#define DOMAIN_FLAG_MULTIPLE_DEVICES 1
-	int		flags;
+	/* following fields are used in virtual machine case */
+	int		iommu_count;	/* reference count of iommu */
+	u64		max_addr;	/* maximum mapped address */
 };
 
 /* PCI domain-device relationship */
include/linux/dmar.h +2 -0
index f1984fc..157dcb7 100644
@@ -53,6 +53,8 @@ extern void detect_intel_iommu(void);
 
 extern int parse_ioapics_under_ir(void);
 extern int alloc_iommu(struct dmar_drhd_unit *);
+extern int width_to_agaw(int width);
+
 #else
 static inline void detect_intel_iommu(void)
 {
include/linux/intel-iommu.h +13 -8
index 3d017cf..c2f37b8 100644
@@ -219,6 +219,8 @@ do {									\
 	}								\
 } while (0)
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 #define QI_LENGTH	256	/* queue length */
 
 enum {
@@ -299,6 +301,7 @@ struct intel_iommu {
 	struct dmar_domain **domains; /* ptr to domains */
 	spinlock_t	lock; /* protect context, domain ids */
 	struct root_entry *root_entry; /* virtual address */
+	int		agaw;
 
 	unsigned int irq;
 	unsigned char name[7];    /* Device Name */
@@ -334,14 +337,16 @@ extern int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
 
 extern void qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu);
 
-void intel_iommu_domain_exit(struct dmar_domain *domain);
-struct dmar_domain *intel_iommu_domain_alloc(struct pci_dev *pdev);
-int intel_iommu_context_mapping(struct dmar_domain *domain,
-				struct pci_dev *pdev);
-int intel_iommu_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
-			     u64 hpa, size_t size, int prot);
-void intel_iommu_detach_dev(struct dmar_domain *domain, u8 bus, u8 devfn);
-struct dmar_domain *intel_iommu_find_domain(struct pci_dev *pdev);
+struct dmar_domain *intel_iommu_alloc_domain(void);
+void intel_iommu_free_domain(struct dmar_domain *domain);
+int intel_iommu_assign_device(struct dmar_domain *domain,
+			      struct pci_dev *pdev);
+void intel_iommu_deassign_device(struct dmar_domain *domain,
+				 struct pci_dev *pdev);
+int intel_iommu_map_pages(struct dmar_domain *domain, dma_addr_t iova,
+			  u64 hpa, size_t size, int prot);
+void intel_iommu_unmap_pages(struct dmar_domain *domain,
+			     dma_addr_t iova, size_t size);
 u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova);
 
 #ifdef CONFIG_DMAR
Signature · 1 line
1.5.6.4
#2 lore raw

PATCH 2/12 KVM: change to use new APIs for kvm vtd

In reply to Joerg Roedel

From: Weidong Han <[email protected]>

This patch changes to use new APIs for KVM VT-d, and add device deassignment for hotplug.

[Joerg: coding style cleanups]

---
 include/linux/kvm.h      |    5 ++
 include/linux/kvm_host.h |   23 ++++++++--
 virt/kvm/kvm_main.c      |   49 ++++++++++++++++++++-
 virt/kvm/vtd.c           |  107 +++++++++++++++++++++++++++++++---------------
 4 files changed, 143 insertions(+), 41 deletions(-)
include/linux/kvm.h +5 -0
index 0997e6f..49432e9 100644
@@ -395,6 +395,9 @@ struct kvm_trace_rec {
 #if defined(CONFIG_X86)
 #define KVM_CAP_DEVICE_MSI 20
 #endif
+#if defined(CONFIG_X86) || defined(CONFIG_IA64)
+#define KVM_CAP_DEVICE_DEASSIGNMENT 21
+#endif
 
 /*
  * ioctls for VM fds
@@ -428,6 +431,8 @@ struct kvm_trace_rec {
 				   struct kvm_assigned_pci_dev)
 #define KVM_ASSIGN_IRQ _IOR(KVMIO, 0x70, \
 			    struct kvm_assigned_irq)
+#define KVM_DEASSIGN_PCI_DEVICE _IOR(KVMIO, 0x71, \
+				     struct kvm_assigned_pci_dev)
 
 /*
  * ioctls for vcpu fds
include/linux/kvm_host.h +18 -5
index 8091a4d..cb1d404 100644
@@ -329,9 +329,12 @@ void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
 #ifdef CONFIG_DMAR
 int kvm_iommu_map_pages(struct kvm *kvm, gfn_t base_gfn,
 			unsigned long npages);
-int kvm_iommu_map_guest(struct kvm *kvm,
-			struct kvm_assigned_dev_kernel *assigned_dev);
+int kvm_iommu_map_guest(struct kvm *kvm);
 int kvm_iommu_unmap_guest(struct kvm *kvm);
+int kvm_assign_device(struct kvm *kvm,
+		      struct kvm_assigned_dev_kernel *assigned_dev);
+int kvm_deassign_device(struct kvm *kvm,
+			struct kvm_assigned_dev_kernel *assigned_dev);
 #else /* CONFIG_DMAR */
 static inline int kvm_iommu_map_pages(struct kvm *kvm,
 				      gfn_t base_gfn,
@@ -340,9 +343,7 @@ static inline int kvm_iommu_map_pages(struct kvm *kvm,
 	return 0;
 }
 
-static inline int kvm_iommu_map_guest(struct kvm *kvm,
-				      struct kvm_assigned_dev_kernel
-				      *assigned_dev)
+static inline int kvm_iommu_map_guest(struct kvm *kvm)
 {
 	return -ENODEV;
 }
@@ -351,6 +352,18 @@ static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
 {
 	return 0;
 }
+
+static inline int kvm_assign_device(struct kvm *kvm,
+		struct kvm_assigned_dev_kernel *assigned_dev)
+{
+	return 0;
+}
+
+static inline int kvm_deassign_device(struct kvm *kvm,
+		struct kvm_assigned_dev_kernel *assigned_dev)
+{
+	return 0;
+}
 #endif /* CONFIG_DMAR */
 
 static inline void kvm_guest_enter(void)
virt/kvm/kvm_main.c +48 -1

67 lines not on this page · raw message

virt/kvm/vtd.c +72 -35

172 lines not on this page · raw message

Signature · 1 line
1.5.6.4
#3 lore raw

PATCH 3/12 KVM: rename vtd.c to iommu.c

In reply to Joerg Roedel

Impace: file renamed

The code in the vtd.c file can be reused for other IOMMUs as well. So rename it to make it clear that it handle more than VT-d.

---
 arch/ia64/kvm/Makefile      |    2 +-
 arch/x86/kvm/Makefile       |    2 +-
 virt/kvm/{vtd.c => iommu.c} |    0 
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename virt/kvm/{vtd.c => iommu.c} (100%)
arch/ia64/kvm/Makefile +1 -1
index 76464dc..cb69dfc 100644
@@ -52,7 +52,7 @@ common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o ioapic.o \
 		coalesced_mmio.o irq_comm.o)
 
 ifeq ($(CONFIG_DMAR),y)
-common-objs += $(addprefix ../../../virt/kvm/, vtd.o)
+common-objs += $(addprefix ../../../virt/kvm/, iommu.o)
 endif
 
 kvm-objs := $(common-objs) kvm-ia64.o kvm_fw.o
arch/x86/kvm/Makefile +1 -1
index c023435..00f46c2 100644
@@ -8,7 +8,7 @@ ifeq ($(CONFIG_KVM_TRACE),y)
 common-objs += $(addprefix ../../../virt/kvm/, kvm_trace.o)
 endif
 ifeq ($(CONFIG_DMAR),y)
-common-objs += $(addprefix ../../../virt/kvm/, vtd.o)
+common-objs += $(addprefix ../../../virt/kvm/, iommu.o)
 endif
 
 EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm
virt/kvm/iommu.c renamed from virt/kvm/vtd.c +0 -0
similarity index 100%
rename from virt/kvm/vtd.c
rename to virt/kvm/iommu.c
Signature · 1 line
1.5.6.4
#4 lore raw

PATCH 4/12 introcude linux/iommu.h for an iommu api

In reply to Joerg Roedel

This patch introduces the API to abstract the exported VT-d functions for KVM into a generic API. This way the AMD IOMMU implementation can plug into this API later.

---
 include/linux/iommu.h |  109 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/iommu.h
include/linux/iommu.h new file +109 -0
new file mode 100644
index 0000000..47e9ec8
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
+ * Author: Joerg Roedel <[email protected]>
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+#ifndef __LINUX_IOMMU_H
+#define __LINUX_IOMMU_H
+
+struct device;
+
+struct iommu_domain {
+	void *priv;
+};
+
+struct iommu_ops {
+	int (*domain_init)(struct iommu_domain *domain);
+	void (*domain_destroy)(struct iommu_domain *domain);
+	int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
+	void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
+	int (*map)(struct iommu_domain *domain, unsigned long iova,
+		   phys_addr_t paddr, size_t size, int prot);
+	void (*unmap)(struct iommu_domain *domain, unsigned long iova,
+		      size_t size);
+	phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,
+				    unsigned long iova);
+};
+
+#ifdef CONFIG_IOMMU_API
+
+extern void register_iommu(struct iommu_ops *ops);
+extern bool iommu_found(void);
+extern struct iommu_domain *iommu_domain_alloc(void);
+extern void iommu_domain_free(struct iommu_domain *domain);
+extern int iommu_attach_device(struct iommu_domain *domain,
+			       struct device *dev);
+extern void iommu_detach_device(struct iommu_domain *domain,
+				struct device *dev);
+extern int iommu_map_range(struct iommu_domain *domain, unsigned long iova,
+			   phys_addr_t paddr, size_t size, int prot);
+extern void iommu_unmap_range(struct iommu_domain *domain, unsigned long iova,
+			      size_t size);
+extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
+				      unsigned long iova);
+
+#else /* CONFIG_IOMMU_API */
+
+static inline void register_iommu(struct iommu_ops *ops)
+{
+}
+
+static inline bool iommu_found(void)
+{
+	return false;
+}
+
+static inline struct iommu_domain *iommu_domain_alloc(void)
+{
+	return NULL;
+}
+
+static inline void iommu_domain_free(struct iommu_domain *domain)
+{
+}
+
+static inline int iommu_attach_device(struct iommu_domain *domain,
+				      struct device *dev)
+{
+	return -ENODEV;
+}
+
+static inline void iommu_detach_device(struct iommu_domain *domain,
+				       struct device *dev)
+{
+}
+
+static inline int iommu_map_range(struct iommu_domain *domain,
+				  unsigned long iova, phys_addr_t paddr,
+				  size_t size, int prot)
+{
+	return -ENODEV;
+}
+
+static inline void iommu_unmap_range(struct iommu_domain *domain,
+				     unsigned long iova, size_t size)
+{
+}
+
+static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
+					     unsigned long iova)
+{
+	return 0;
+}
+
+#endif /* CONFIG_IOMMU_API */
+
+#endif /* __LINUX_IOMMU_H */
Signature · 1 line
1.5.6.4
#5 lore raw

PATCH 5/12 add frontend implementation for the IOMMU API

In reply to Joerg Roedel
---
 drivers/base/iommu.c |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 100 insertions(+), 0 deletions(-)
 create mode 100644 drivers/base/iommu.c
drivers/base/iommu.c new file +100 -0
new file mode 100644
index 0000000..5e039d4
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
+ * Author: Joerg Roedel <[email protected]>
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+#include <linux/bug.h>
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/iommu.h>
+
+static struct iommu_ops *iommu_ops;
+
+void register_iommu(struct iommu_ops *ops)
+{
+	if (iommu_ops)
+		BUG();
+
+	iommu_ops = ops;
+}
+
+bool iommu_found()
+{
+	return iommu_ops != NULL;
+}
+EXPORT_SYMBOL_GPL(iommu_found);
+
+struct iommu_domain *iommu_domain_alloc(void)
+{
+	struct iommu_domain *domain;
+	int ret;
+
+	domain = kmalloc(sizeof(*domain), GFP_KERNEL);
+	if (!domain)
+		return NULL;
+
+	ret = iommu_ops->domain_init(domain);
+	if (ret)
+		goto out_free;
+
+	return domain;
+
+out_free:
+	kfree(domain);
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(iommu_domain_alloc);
+
+void iommu_domain_free(struct iommu_domain *domain)
+{
+	iommu_ops->domain_destroy(domain);
+	kfree(domain);
+}
+EXPORT_SYMBOL_GPL(iommu_domain_free);
+
+int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
+{
+	return iommu_ops->attach_dev(domain, dev);
+}
+EXPORT_SYMBOL_GPL(iommu_attach_device);
+
+void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
+{
+	iommu_ops->detach_dev(domain, dev);
+}
+EXPORT_SYMBOL_GPL(iommu_detach_device);
+
+int iommu_map_range(struct iommu_domain *domain, unsigned long iova,
+		    phys_addr_t paddr, size_t size, int prot)
+{
+	return iommu_ops->map(domain, iova, paddr, size, prot);
+}
+EXPORT_SYMBOL_GPL(iommu_map_range);
+
+void iommu_unmap_range(struct iommu_domain *domain, unsigned long iova,
+		      size_t size)
+{
+	iommu_ops->unmap(domain, iova, size);
+}
+EXPORT_SYMBOL_GPL(iommu_unmap_range);
+
+phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
+			       unsigned long iova)
+{
+	return iommu_ops->iova_to_phys(domain, iova);
+}
+EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
Signature · 1 line
1.5.6.4
#6 lore raw

PATCH 6/12 select IOMMU_API when DMAR and/or AMD_IOMMU is selected

In reply to Joerg Roedel

These two IOMMUs can implement the current version of this API. So select the API if one or both of these IOMMU drivers is selected.

---
 arch/ia64/Kconfig     |    3 +++
 arch/x86/Kconfig      |    3 +++
 drivers/base/Makefile |    1 +
 3 files changed, 7 insertions(+), 0 deletions(-)
arch/ia64/Kconfig +3 -0
index 6bd91ed..6a7b0c9 100644
@@ -687,3 +687,6 @@ config IRQ_PER_CPU
 
 config IOMMU_HELPER
 	def_bool (IA64_HP_ZX1 || IA64_HP_ZX1_SWIOTLB || IA64_GENERIC || SWIOTLB)
+
+config IOMMU_API
+	def_bool (DMAR)
arch/x86/Kconfig +3 -0
index ac22bb7..b9f7187 100644
@@ -580,6 +580,9 @@ config SWIOTLB
 config IOMMU_HELPER
 	def_bool (CALGARY_IOMMU || GART_IOMMU || SWIOTLB || AMD_IOMMU)
 
+config IOMMU_API
+	def_bool (AMD_IOMMU || DMAR)
+
 config MAXSMP
 	bool "Configure Maximum number of SMP Processors and NUMA Nodes"
 	depends on X86_64 && SMP && BROKEN
drivers/base/Makefile +1 -0
index c666373..b5b8ba5 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_FW_LOADER)	+= firmware_class.o
 obj-$(CONFIG_NUMA)	+= node.o
 obj-$(CONFIG_MEMORY_HOTPLUG_SPARSE) += memory.o
 obj-$(CONFIG_SMP)	+= topology.o
+obj-$(CONFIG_IOMMU_API) += iommu.o
 ifeq ($(CONFIG_SYSFS),y)
 obj-$(CONFIG_MODULES)	+= module.o
 endif
Signature · 1 line
1.5.6.4
#7 lore raw

PATCH 7/12 KVM: change KVM iommu.c to use IOMMU API

In reply to Joerg Roedel
---
 arch/ia64/kvm/Makefile          |    2 +-
 arch/x86/include/asm/kvm_host.h |    3 +-
 arch/x86/kvm/Makefile           |    2 +-
 virt/kvm/iommu.c                |   68 ++++++++++++++++++++-------------------
 virt/kvm/kvm_main.c             |    2 +-
 5 files changed, 40 insertions(+), 37 deletions(-)
arch/ia64/kvm/Makefile +1 -1
index cb69dfc..0bb99b7 100644
@@ -51,7 +51,7 @@ EXTRA_AFLAGS += -Ivirt/kvm -Iarch/ia64/kvm/
 common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o ioapic.o \
 		coalesced_mmio.o irq_comm.o)
 
-ifeq ($(CONFIG_DMAR),y)
+ifeq ($(CONFIG_IOMMU_API),y)
 common-objs += $(addprefix ../../../virt/kvm/, iommu.o)
 endif
 
arch/x86/include/asm/kvm_host.h +2 -1
index f58f7eb..77f4afa 100644
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/mmu_notifier.h>
+#include <linux/iommu.h>
 
 #include <linux/kvm.h>
 #include <linux/kvm_para.h>
@@ -356,7 +357,7 @@ struct kvm_arch{
 	 */
 	struct list_head active_mmu_pages;
 	struct list_head assigned_dev_head;
-	struct dmar_domain *intel_iommu_domain;
+	struct iommu_domain *iommu_domain;
 	struct kvm_pic *vpic;
 	struct kvm_ioapic *vioapic;
 	struct kvm_pit *vpit;
arch/x86/kvm/Makefile +1 -1
index 00f46c2..d3ec292 100644
@@ -7,7 +7,7 @@ common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o ioapic.o \
 ifeq ($(CONFIG_KVM_TRACE),y)
 common-objs += $(addprefix ../../../virt/kvm/, kvm_trace.o)
 endif
-ifeq ($(CONFIG_DMAR),y)
+ifeq ($(CONFIG_IOMMU_API),y)
 common-objs += $(addprefix ../../../virt/kvm/, iommu.o)
 endif
 
virt/kvm/iommu.c +35 -33

170 lines not on this page · raw message

virt/kvm/kvm_main.c +1 -1
index fe6aba0..397ad0f 100644
@@ -472,7 +472,7 @@ static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
 	list_add(&match->list, &kvm->arch.assigned_dev_head);
 
 	if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
-		if (!kvm->arch.intel_iommu_domain) {
+		if (!kvm->arch.iommu_domain) {
 			r = kvm_iommu_map_guest(kvm);
 			if (r)
 				goto out_list_del;
Signature · 1 line
1.5.6.4
#8 lore raw

PATCH 8/12 VT-d: adapt domain init and destroy functions for IOMMU API

In reply to Joerg Roedel
---
 drivers/pci/intel-iommu.c   |   30 +++++++++++++++++-------------
 include/linux/intel-iommu.h |    2 --
 2 files changed, 17 insertions(+), 15 deletions(-)
drivers/pci/intel-iommu.c +17 -13
index 7f12852..59b9cdb 100644
@@ -35,6 +35,7 @@
 #include <linux/mempool.h>
 #include <linux/timer.h>
 #include <linux/iova.h>
+#include <linux/iommu.h>
 #include <linux/intel-iommu.h>
 #include <asm/cacheflush.h>
 #include <asm/iommu.h>
@@ -2779,32 +2780,34 @@ static struct dmar_domain *iommu_alloc_vm_domain(void)
 	return domain;
 }
 
-struct dmar_domain *intel_iommu_alloc_domain(void)
+static int intel_iommu_domain_init(struct iommu_domain *domain)
 {
-	struct dmar_domain *domain;
+	struct dmar_domain *dmar_domain;
 
-	domain = iommu_alloc_vm_domain();
-	if (!domain) {
+	dmar_domain = iommu_alloc_vm_domain();
+	if (!dmar_domain) {
 		printk(KERN_ERR
 			"intel_iommu_domain_alloc: domain == NULL\n");
-		return NULL;
+		return -ENOMEM;
 	}
-	if (vm_domain_init(domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
+	if (vm_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
 		printk(KERN_ERR
 			"intel_iommu_domain_alloc: domain_init() failed\n");
-		vm_domain_exit(domain);
-		return NULL;
+		vm_domain_exit(dmar_domain);
+		return -ENOMEM;
 	}
+	domain->priv = dmar_domain;
 
-	return domain;
+	return 0;
 }
-EXPORT_SYMBOL_GPL(intel_iommu_alloc_domain);
 
-void intel_iommu_free_domain(struct dmar_domain *domain)
+static void intel_iommu_domain_destroy(struct iommu_domain *domain)
 {
-	vm_domain_exit(domain);
+	struct dmar_domain *dmar_domain = domain->priv;
+
+	domain->priv = NULL;
+	vm_domain_exit(dmar_domain);
 }
-EXPORT_SYMBOL_GPL(intel_iommu_free_domain);
 
 int intel_iommu_assign_device(struct dmar_domain *domain,
 			      struct pci_dev *pdev)
@@ -2922,3 +2925,4 @@ u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova)
 	return pfn >> VTD_PAGE_SHIFT;
 }
 EXPORT_SYMBOL_GPL(intel_iommu_iova_to_pfn);
+
include/linux/intel-iommu.h +0 -2
index c2f37b8..5a4ce23 100644
@@ -337,8 +337,6 @@ extern int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
 
 extern void qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu);
 
-struct dmar_domain *intel_iommu_alloc_domain(void);
-void intel_iommu_free_domain(struct dmar_domain *domain);
 int intel_iommu_assign_device(struct dmar_domain *domain,
 			      struct pci_dev *pdev);
 void intel_iommu_deassign_device(struct dmar_domain *domain,
Signature · 1 line
1.5.6.4
#9 lore raw

PATCH 9/12 VT-d: adapt device attach and detach functions for IOMMU API

In reply to Joerg Roedel
---
 drivers/pci/intel-iommu.c   |   28 +++++++++++++++-------------
 include/linux/intel-iommu.h |    4 ----
 2 files changed, 15 insertions(+), 17 deletions(-)
drivers/pci/intel-iommu.c +15 -13
index 59b9cdb..62ae6b1 100644
@@ -2809,9 +2809,11 @@ static void intel_iommu_domain_destroy(struct iommu_domain *domain)
 	vm_domain_exit(dmar_domain);
 }
 
-int intel_iommu_assign_device(struct dmar_domain *domain,
-			      struct pci_dev *pdev)
+static int intel_iommu_attach_device(struct iommu_domain *domain,
+				     struct device *dev)
 {
+	struct dmar_domain *dmar_domain = domain->priv;
+	struct pci_dev *pdev = to_pci_dev(dev);
 	struct intel_iommu *iommu;
 	int addr_width;
 	u64 end;
@@ -2823,7 +2825,7 @@ int intel_iommu_assign_device(struct dmar_domain *domain,
 
 		old_domain = find_domain(pdev);
 		if (old_domain) {
-			if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
+			if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
 				vm_domain_remove_one_dev_info(old_domain, pdev);
 			else
 				domain_remove_dev_info(old_domain);
@@ -2838,29 +2840,29 @@ int intel_iommu_assign_device(struct dmar_domain *domain,
 	addr_width = agaw_to_width(iommu->agaw);
 	end = DOMAIN_MAX_ADDR(addr_width);
 	end = end & VTD_PAGE_MASK;
-	if (end < domain->max_addr) {
+	if (end < dmar_domain->max_addr) {
 		printk(KERN_ERR "%s: iommu agaw (%d) is not "
 		       "sufficient for the mapped address (%llx)\n",
-		       __func__, iommu->agaw, domain->max_addr);
+		       __func__, iommu->agaw, dmar_domain->max_addr);
 		return -EFAULT;
 	}
 
-	ret = vm_domain_context_mapping(domain, pdev);
+	ret = vm_domain_context_mapping(dmar_domain, pdev);
 	if (ret)
 		return ret;
 
-	ret = vm_domain_add_dev_info(domain, pdev);
+	ret = vm_domain_add_dev_info(dmar_domain, pdev);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(intel_iommu_assign_device);
-
 
-void intel_iommu_deassign_device(struct dmar_domain *domain,
-				 struct pci_dev *pdev)
+static void intel_iommu_detach_device(struct iommu_domain *domain,
+			       struct device *dev)
 {
-	vm_domain_remove_one_dev_info(domain, pdev);
+	struct dmar_domain *dmar_domain = domain->priv;
+	struct pci_dev *pdev = to_pci_dev(dev);
+
+	vm_domain_remove_one_dev_info(dmar_domain, pdev);
 }
-EXPORT_SYMBOL_GPL(intel_iommu_deassign_device);
 
 int intel_iommu_map_pages(struct dmar_domain *domain, dma_addr_t iova,
 			  u64 hpa, size_t size, int prot)
include/linux/intel-iommu.h +0 -4
index 5a4ce23..41d2a3b 100644
@@ -337,10 +337,6 @@ extern int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
 
 extern void qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu);
 
-int intel_iommu_assign_device(struct dmar_domain *domain,
-			      struct pci_dev *pdev);
-void intel_iommu_deassign_device(struct dmar_domain *domain,
-				 struct pci_dev *pdev);
 int intel_iommu_map_pages(struct dmar_domain *domain, dma_addr_t iova,
 			  u64 hpa, size_t size, int prot);
 void intel_iommu_unmap_pages(struct dmar_domain *domain,
Signature · 1 line
1.5.6.4
#10 lore raw

PATCH 10/12 VT-d: adapt domain map and unmap functions for IOMMU API

In reply to Joerg Roedel
---
 drivers/pci/intel-iommu.c   |   22 +++++++++++-----------
 include/linux/intel-iommu.h |    4 ----
 2 files changed, 11 insertions(+), 15 deletions(-)
drivers/pci/intel-iommu.c +11 -11
index 62ae6b1..ac22973 100644
@@ -2864,20 +2864,21 @@ static void intel_iommu_detach_device(struct iommu_domain *domain,
 	vm_domain_remove_one_dev_info(dmar_domain, pdev);
 }
 
-int intel_iommu_map_pages(struct dmar_domain *domain, dma_addr_t iova,
-			  u64 hpa, size_t size, int prot)
+static int intel_iommu_map(struct iommu_domain *domain, unsigned long iova,
+			   phys_addr_t hpa, size_t size, int prot)
 {
+	struct dmar_domain *dmar_domain = domain->priv;
 	u64 max_addr;
 	int addr_width;
 	int ret;
 
 	max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
-	if (domain->max_addr < max_addr) {
+	if (dmar_domain->max_addr < max_addr) {
 		int min_agaw;
 		u64 end;
 
 		/* check if minimum agaw is sufficient for mapped address */
-		min_agaw = vm_domain_min_agaw(domain);
+		min_agaw = vm_domain_min_agaw(dmar_domain);
 		addr_width = agaw_to_width(min_agaw);
 		end = DOMAIN_MAX_ADDR(addr_width);
 		end = end & VTD_PAGE_MASK;
@@ -2887,25 +2888,24 @@ int intel_iommu_map_pages(struct dmar_domain *domain, dma_addr_t iova,
 			       __func__, min_agaw, max_addr);
 			return -EFAULT;
 		}
-		domain->max_addr = max_addr;
+		dmar_domain->max_addr = max_addr;
 	}
 
-	ret = domain_page_mapping(domain, iova, hpa, size, prot);
+	ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(intel_iommu_map_pages);
 
-void intel_iommu_unmap_pages(struct dmar_domain *domain,
-			     dma_addr_t iova, size_t size)
+static void intel_iommu_unmap(struct iommu_domain *domain,
+			      unsigned long iova, size_t size)
 {
+	struct dmar_domain *dmar_domain = domain->priv;
 	dma_addr_t base;
 
 	/* The address might not be aligned */
 	base = iova & PAGE_MASK;
 	size = PAGE_ALIGN(size);
-	dma_pte_clear_range(domain, base, base + size);
+	dma_pte_clear_range(dmar_domain, base, base + size);
 }
-EXPORT_SYMBOL_GPL(intel_iommu_unmap_pages);
 
 int intel_iommu_found(void)
 {
include/linux/intel-iommu.h +0 -4
index 41d2a3b..ac79a1c 100644
@@ -337,10 +337,6 @@ extern int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
 
 extern void qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu);
 
-int intel_iommu_map_pages(struct dmar_domain *domain, dma_addr_t iova,
-			  u64 hpa, size_t size, int prot);
-void intel_iommu_unmap_pages(struct dmar_domain *domain,
-			     dma_addr_t iova, size_t size);
 u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova);
 
 #ifdef CONFIG_DMAR
Signature · 1 line
1.5.6.4
#11 lore raw

PATCH 11/12 VT-d: adapt domain iova_to_phys function for IOMMU API

In reply to Joerg Roedel
---
 drivers/pci/intel-iommu.c   |   15 ++++++++-------
 include/linux/intel-iommu.h |    2 --
 2 files changed, 8 insertions(+), 9 deletions(-)
drivers/pci/intel-iommu.c +8 -7
index ac22973..2e8b102 100644
@@ -2913,18 +2913,19 @@ int intel_iommu_found(void)
 }
 EXPORT_SYMBOL_GPL(intel_iommu_found);
 
-u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova)
+static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
+					    unsigned long iova)
 {
+	struct dmar_domain *dmar_domain = domain->priv;
 	struct dma_pte *pte;
-	u64 pfn;
+	phys_addr_t paddr;
 
-	pfn = 0;
-	pte = addr_to_dma_pte(domain, iova);
+	paddr = 0;
+	pte = addr_to_dma_pte(dmar_domain, iova);
 
 	if (pte)
-		pfn = dma_pte_addr(*pte);
+		paddr = dma_pte_addr(*pte);
 
-	return pfn >> VTD_PAGE_SHIFT;
+	return paddr;
 }
-EXPORT_SYMBOL_GPL(intel_iommu_iova_to_pfn);
 
include/linux/intel-iommu.h +0 -2
index ac79a1c..469508f 100644
@@ -337,8 +337,6 @@ extern int qi_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
 
 extern void qi_submit_sync(struct qi_desc *desc, struct intel_iommu *iommu);
 
-u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova);
-
 #ifdef CONFIG_DMAR
 int intel_iommu_found(void);
 #else /* CONFIG_DMAR */
Signature · 1 line
1.5.6.4
#12 lore raw

PATCH 12/12 VT-d: register functions for the IOMMU API

In reply to Joerg Roedel
---
 drivers/pci/intel-iommu.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/pci/intel-iommu.c +13 -0
index 2e8b102..bb6f771 100644
@@ -55,6 +55,7 @@
 
 
 static void flush_unmaps_timeout(unsigned long data);
+static struct iommu_ops intel_iommu_ops;
 
 DEFINE_TIMER(unmap_timer,  flush_unmaps_timeout, 0, 0);
 
@@ -2429,6 +2430,9 @@ int __init intel_iommu_init(void)
 	init_timer(&unmap_timer);
 	force_iommu = 1;
 	dma_ops = &intel_dma_ops;
+
+	register_iommu(&intel_iommu_ops);
+
 	return 0;
 }
 
@@ -2929,3 +2933,12 @@ static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
 	return paddr;
 }
 
+static struct iommu_ops intel_iommu_ops = {
+	.domain_init	= intel_iommu_domain_init,
+	.domain_destroy	= intel_iommu_domain_destroy,
+	.attach_dev	= intel_iommu_attach_device,
+	.detach_dev	= intel_iommu_detach_device,
+	.map		= intel_iommu_map,
+	.unmap		= intel_iommu_unmap,
+	.iova_to_phys	= intel_iommu_iova_to_phys,
+};
Signature · 1 line
1.5.6.4
#13 lore raw
In reply to Joerg Roedel
Quoted 7 lines · Joerg Roedel

Hi,

this patch series makes the current KVM device passthrough code generic enough so that other IOMMU implementation can also plug into this code. It works by factoring the functions Vt-d code exports to KVM into a generic interface which allows different backends.

Quoted 7 lines

This is the second version of the patchset. The most important change to the previous version is that this patchset was rebased to the improved API from Han Weidong which supports multiple devices per IOMMU domain.

For completeness, this series also includes the patches from Han with some cleanups. So this patchset can be applied on current avi/master tree.

Have you tried porting any of the current iommu controllers to this new framework to see if it works properly for them?

thanks,

greg k-h

maintainer
#14 lore raw
Quoted 12 lines · Greg KH
Quoted 11 lines · Joerg Roedel

Hi,

this patch series makes the current KVM device passthrough code generic enough so that other IOMMU implementation can also plug into this code. It works by factoring the functions Vt-d code exports to KVM into a generic interface which allows different backends.

This is the second version of the patchset. The most important change to the previous version is that this patchset was rebased to the improved API from Han Weidong which supports multiple devices per IOMMU domain.

Quoted 6 lines
Quoted 3 lines

For completeness, this series also includes the patches from Han with some cleanups. So this patchset can be applied on current avi/master tree.

Have you tried porting any of the current iommu controllers to this new framework to see if it works properly for them?

It works currently for VT-d. I also port it to AMD IOMMU currently. With some extensions (offset for start address, flags and size limitation) it is also suitable for IOMMUs like GART or similar ones.

Joerg

#15 lore raw
Quoted 17 lines · Joerg Roedel
Quoted 16 lines · Greg KH
Quoted 14 lines · Joerg Roedel

Hi,

this patch series makes the current KVM device passthrough code generic enough so that other IOMMU implementation can also plug into this code. It works by factoring the functions Vt-d code exports to KVM into a generic interface which allows different backends.

This is the second version of the patchset. The most important change to the previous version is that this patchset was rebased to the improved API from Han Weidong which supports multiple devices per IOMMU domain.

For completeness, this series also includes the patches from Han with some cleanups. So this patchset can be applied on current avi/master tree.

Quoted 6 lines
Quoted 2 lines

Have you tried porting any of the current iommu controllers to this new framework to see if it works properly for them?

It works currently for VT-d. I also port it to AMD IOMMU currently. With some extensions (offset for start address, flags and size limitation) it is also suitable for IOMMUs like GART or similar ones.

What about the Calgary chipset?

thanks,

gerg k-h

maintainer
#16 lore raw
Quoted 18 lines · Greg KH
Quoted 17 lines · Joerg Roedel
Quoted 16 lines · Greg KH
Quoted 14 lines · Joerg Roedel

Hi,

this patch series makes the current KVM device passthrough code generic enough so that other IOMMU implementation can also plug into this code. It works by factoring the functions Vt-d code exports to KVM into a generic interface which allows different backends.

This is the second version of the patchset. The most important change to the previous version is that this patchset was rebased to the improved API from Han Weidong which supports multiple devices per IOMMU domain.

For completeness, this series also includes the patches from Han with some cleanups. So this patchset can be applied on current avi/master tree.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Have you tried porting any of the current iommu controllers to this new framework to see if it works properly for them?

It works currently for VT-d. I also port it to AMD IOMMU currently. With some extensions (offset for start address, flags and size limitation) it is also suitable for IOMMUs like GART or similar ones.

What about the Calgary chipset?

Calgary is quite similar to GART (there is something like the aperture and a linear single-level pagetable). So with the before mentioned extensions it is also suitable for Calgary.

Joerg

#17 lore raw
Quoted 8 lines · Joerg Roedel
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Have you tried porting any of the current iommu controllers to this new framework to see if it works properly for them?

It works currently for VT-d. I also port it to AMD IOMMU currently. With some extensions (offset for start address, flags and size limitation) it is also suitable for IOMMUs like GART or similar ones.

Quoted 4 lines
Quoted 1 line

What about the Calgary chipset?

Calgary is quite similar to GART (there is something like the aperture and a linear single-level pagetable).

Actually, Calgary has multiple per-bus address spaces (each of which is a single-level linear pagetable limited to 4GB of addressable memory), so I think it should work with your current approach pretty much as is, once we take into account these two (per-bus and 32-bit addressability) limitations.

Cheers,
Muli

Signature · 5 lines
The First Workshop on I/O Virtualization (WIOV '08)
Dec 2008, San Diego, CA, http://www.usenix.org/wiov08/
                       <->
SYSTOR 2009---The Israeli Experimental Systems Conference
http://www.haifa.il.ibm.com/conferences/systor2009/
#18 lore raw
In reply to Joerg Roedel
Quoted 28 lines · Joerg Roedel

Hi,

this patch series makes the current KVM device passthrough code generic
enough so that other IOMMU implementation can also plug into this code.
It works by factoring the functions Vt-d code exports to KVM into a generic interface which allows different backends.

This is the second version of the patchset. The most important change to
the previous version is that this patchset was rebased to the improved API from Han Weidong which supports multiple devices per IOMMU domain.

For completeness, this series also includes the patches from Han with some cleanups. So this patchset can be applied on current avi/master tree.

For testing this code can also be pulled against avi/master tree from

git://git.kernel.org/pub/scm/linux/kernel/git/joro/linux-2.6-iommu.git
iommu-api

This a basic implementation of a generic interface. It can and should be improved later to support more types of hardware IOMMUs then VT-d and
AMD IOMMU.

Quoted 4 lines

Since I have no VT-d hardware available these patches are only compile tested for now.

Please review, comment and test these patches.

Tried this patchset on avi/master tree, VT-d works. Your generic API approach is ok for me. But the multiple devices assignment patches (here patch 01 and 02) needs some improvement, and they are too big to review. I have split them into a serial small patches, and sent out for review in another mail thread. After my multiple device assignment patchset is accepted, I think you can easily rebase your generic patchset on it.

Regards,
Weidong