Initial AMD IOMMUv2 support
Subsystems (3)
cover Initial AMD IOMMUv2 support
Hi,
here is a patch-set that adds initial support for the AMD IOMMU version 2 to the Linux kernel. The main feature of the new IOMMU version is support for DMA demand-paging and multiple DMA address spaces per device.
The patch-set consists of 2 parts. Patches 1-16 implement the hardware access functions to setup and manage the IOMMUv2 functionality of the hardware. Patches 17-22 implement a seperate module which makes use these functions to implement a page-fault handler for devices which can be used by real device drivers.
Any feedback appreciated.
Thanks,
Joerg
diff-stat:
Documentation/kernel-parameters.txt | 5 + drivers/iommu/Kconfig | 13 +- drivers/iommu/Makefile | 1 + drivers/iommu/amd_iommu.c | 791 ++++++++++++++++++++++++++++-- drivers/iommu/amd_iommu_init.c | 113 ++++- drivers/iommu/amd_iommu_proto.h | 24 + drivers/iommu/amd_iommu_types.h | 109 ++++- drivers/iommu/amd_iommu_v2.c | 951 +++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 95 ++++- 9 files changed, 2058 insertions(+), 44 deletions(-)
PATCH 1/22 iommu/amd: Convert dev_table_entry to u64
Convert the contents of 'struct dev_table_entry' to u64 to allow updating the DTE wit 64bit writes as required by the spec.
--- drivers/iommu/amd_iommu.c | 18 ++++++++++-------- drivers/iommu/amd_iommu_init.c | 12 ++++++------ drivers/iommu/amd_iommu_types.h | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-)
drivers/iommu/amd_iommu.c +10 -8
@@ -365,8 +365,8 @@ static void dump_dte_entry(u16 devid) {int i; -for (i =0 ; i <8 ; ++i) -pr_err ("AMD-Vi: DTE[%d]: %08x\n" , i, +for (i =0 ; i <4 ; ++i) +pr_err ("AMD-Vi: DTE[%d]: %016llx\n" , i, amd_iommu_dev_table[devid].data[i]); }
@@ -1583,19 +1583,22 @@ static bool dma_ops_domain(struct protection_domain *domain)static void set_dte_entry (u16 devid,struct protection_domain *domain,bool ats) {u64 pte_root =virt_to_phys (domain->pt_root); -u32 flags =0 ; +u64 flags =0 ; pte_root |= (domain->mode &DEV_ENTRY_MODE_MASK ) <<DEV_ENTRY_MODE_SHIFT ; pte_root |=IOMMU_PTE_IR |IOMMU_PTE_IW |IOMMU_PTE_P |IOMMU_PTE_TV ; + flags = amd_iommu_dev_table[devid].data[1 ]; +if (ats) flags |=DTE_FLAG_IOTLB ; - amd_iommu_dev_table[devid].data[3 ] |= flags; - amd_iommu_dev_table[devid].data[2 ] = domain->id; - amd_iommu_dev_table[devid].data[1 ] =upper_32_bits (pte_root); - amd_iommu_dev_table[devid].data[0 ] =lower_32_bits (pte_root); + flags &= ~(0xffffUL ); + flags |= domain->id; + + amd_iommu_dev_table[devid].data[1 ] = flags; + amd_iommu_dev_table[devid].data[0 ] = pte_root; }static void clear_dte_entry (u16 devid)
@@ -1603,7 +1606,6 @@ static void clear_dte_entry(u16 devid)/* remove entry from the device table seen by the hardware */ amd_iommu_dev_table[devid].data[0 ] =IOMMU_PTE_P |IOMMU_PTE_TV ; amd_iommu_dev_table[devid].data[1 ] =0 ; - amd_iommu_dev_table[devid].data[2 ] =0 ;amd_iommu_apply_erratum_63 (devid); }
drivers/iommu/amd_iommu_init.c +6 -6
@@ -584,18 +584,18 @@ static void __init free_event_buffer(struct amd_iommu *iommu)/* sets a specific bit in the device table entry. */ static void set_dev_entry_bit (u16 devid,u8 bit) { -int i = (bit >>5 ) &0x07 ; -int _bit = bit &0x1f ; +int i = (bit >>6 ) &0x03 ; +int _bit = bit &0x3f ; - amd_iommu_dev_table[devid].data[i] |= (1 << _bit); + amd_iommu_dev_table[devid].data[i] |= (1UL << _bit); }static int get_dev_entry_bit (u16 devid,u8 bit) { -int i = (bit >>5 ) &0x07 ; -int _bit = bit &0x1f ; +int i = (bit >>6 ) &0x03 ; +int _bit = bit &0x3f ; -return (amd_iommu_dev_table[devid].data[i] & (1 << _bit)) >> _bit; +return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit; }
drivers/iommu/amd_iommu_types.h +2 -2
@@ -230,7 +230,7 @@#define IOMMU_PTE_IR (1ULL << 61)#define IOMMU_PTE_IW (1ULL << 62) -#define DTE_FLAG_IOTLB 0x01 +#define DTE_FLAG_IOTLB (0x01UL << 32)#define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL)#define IOMMU_PTE_PRESENT (pte) ((pte) & IOMMU_PTE_P)
@@ -484,7 +484,7 @@ extern struct list_head amd_iommu_pd_list;* Structure defining one entry in the device table */ struct dev_table_entry { -u32 data[8 ]; +u64 data[4 ]; };/*
Signature · 1 line
1.7.5.4
PATCH 2/22 iommu/amd: Fix wrong address masks in tlb flush code
In reply to Joerg RoedelUsing PAGE_MASK is not sufficient because it masks out the high bits too. But the IOMMU supports 64 bits and not only
52. So change the masks to only mask out the low 12 bits.
--- drivers/iommu/amd_iommu.c | 4 ++-- drivers/iommu/amd_iommu_types.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/iommu/amd_iommu.c +2 -2
@@ -551,7 +551,7 @@ static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, s =1 ; } - address &=PAGE_MASK ; + address &=IOMMU_IO_PAGE_MASK ;memset (cmd,0 ,sizeof (*cmd)); cmd->data[1 ] |= domid;
@@ -582,7 +582,7 @@ static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, s =1 ; } - address &=PAGE_MASK ; + address &=IOMMU_IO_PAGE_MASK ;memset (cmd,0 ,sizeof (*cmd)); cmd->data[0 ] = devid;
drivers/iommu/amd_iommu_types.h +1 -0
@@ -233,6 +233,7 @@#define DTE_FLAG_IOTLB (0x01UL << 32)#define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL) +#define IOMMU_IO_PAGE_MASK (~(0xfffULL))#define IOMMU_PTE_PRESENT (pte) ((pte) & IOMMU_PTE_P)#define IOMMU_PTE_PAGE (pte) (phys_to_virt((pte) & IOMMU_PAGE_MASK))#define IOMMU_PTE_MODE (pte) (((pte) >> 9) & 0x07)
Signature · 1 line
1.7.5.4
PATCH 3/22 iommu/amd: Get the maximum number of PASIDs supported
In reply to Joerg RoedelRead the number of PASIDs supported by each IOMMU in the system and take the smallest number as the maximum value supported by the IOMMU driver.
--- drivers/iommu/amd_iommu_init.c | 13 +++++++++++++ drivers/iommu/amd_iommu_types.h | 6 ++++++ 2 files changed, 19 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu_init.c +13 -0
@@ -141,6 +141,8 @@ int amd_iommus_present;bool amd_iommu_np_cache__read_mostly ;bool amd_iommu_iotlb_sup__read_mostly = true; +u32 amd_iommu_max_pasids__read_mostly = ~0 ; +/* * The ACPI table parsing functions set this variable on an error */
@@ -699,6 +701,17 @@ static void __init init_iommu_from_pci(struct amd_iommu *iommu) iommu->features = ((u64 )high <<32 ) | low; +if (iommu_feature (iommu,FEATURE_GT )) { +u32 pasids; +u64 shift; + + shift = iommu->features &FEATURE_PASID_MASK ; + shift >>=FEATURE_PASID_SHIFT ; + pasids = (1 << shift); + + amd_iommu_max_pasids =min (amd_iommu_max_pasids, pasids); + } +if (!is_rd890_iommu (iommu->dev))return ;
drivers/iommu/amd_iommu_types.h +6 -0
@@ -87,6 +87,9 @@#define FEATURE_HE (1ULL<<8)#define FEATURE_PC (1ULL<<9) +#define FEATURE_PASID_SHIFT 32 +#define FEATURE_PASID_MASK (0x1fULL << FEATURE_PASID_SHIFT) +/* MMIO status bits */ #define MMIO_STATUS_COM_WAIT_INT_MASK 0x04
@@ -550,6 +553,9 @@ extern unsigned long *amd_iommu_pd_alloc_bitmap;*/ extern bool amd_iommu_unmap_flush; +/* Smallest number of PASIDs supported by any IOMMU in the system */ +extern u32 amd_iommu_max_pasids; +/* takes bus and device/function and returns the device id * FIXME: should that be in generic PCI code? */ static inline u16 calc_devid (u8 bus,u8 devfn)
Signature · 1 line
1.7.5.4
PATCH 4/22 iommu/amd: Setup PPR log when supported by IOMMU
In reply to Joerg RoedelAllocate and enable a log buffer for peripheral page faults when the IOMMU supports this feature.
--- drivers/iommu/amd_iommu_init.c | 51 +++++++++++++++++++++++++++++++++++++++ drivers/iommu/amd_iommu_types.h | 14 ++++++++++ 2 files changed, 65 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu_init.c +51 -0
@@ -583,6 +583,46 @@ static void __init free_event_buffer(struct amd_iommu *iommu)free_pages ((unsigned long )iommu->evt_buf,get_order (EVT_BUFFER_SIZE )); } +/* allocates the memory where the IOMMU will log its events to */ +static u8 *__init alloc_ppr_log (struct amd_iommu *iommu) +{ + iommu->ppr_log = (u8 *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, +get_order (PPR_LOG_SIZE )); + +if (iommu->ppr_log ==NULL ) +return NULL ; + +return iommu->ppr_log; +} + +static void iommu_enable_ppr_log (struct amd_iommu *iommu) +{ +u64 entry; + +if (iommu->ppr_log ==NULL ) +return ; + + entry = (u64 )virt_to_phys (iommu->ppr_log) |PPR_LOG_SIZE_512 ; + +memcpy_toio (iommu->mmio_base +MMIO_PPR_LOG_OFFSET , + &entry,sizeof (entry)); + +/* set head and tail to zero manually */ +writel (0x00 , iommu->mmio_base +MMIO_PPR_HEAD_OFFSET ); +writel (0x00 , iommu->mmio_base +MMIO_PPR_TAIL_OFFSET ); + +iommu_feature_enable (iommu,CONTROL_PPFLOG_EN ); +iommu_feature_enable (iommu,CONTROL_PPR_EN ); +} + +static void __init free_ppr_log (struct amd_iommu *iommu) +{ +if (iommu->ppr_log ==NULL ) +return ; + +free_pages ((unsigned long )iommu->ppr_log,get_order (PPR_LOG_SIZE )); +} +/* sets a specific bit in the device table entry. */ static void set_dev_entry_bit (u16 devid,u8 bit) {
@@ -914,6 +954,7 @@ static void __init free_iommu_one(struct amd_iommu *iommu) {free_command_buffer (iommu);free_event_buffer (iommu); +free_ppr_log (iommu);iommu_unmap_mmio_space (iommu); }
@@ -977,6 +1018,12 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)init_iommu_from_acpi (iommu, h);init_iommu_devices (iommu); +if (iommu_feature (iommu,FEATURE_PPR )) { + iommu->ppr_log =alloc_ppr_log (iommu); +if (!iommu->ppr_log) +return -ENOMEM ; + } +if (iommu->cap & (1UL <<IOMMU_CAP_NPCACHE )) amd_iommu_np_cache = true;
@@ -1063,6 +1110,9 @@ static int iommu_setup_msi(struct amd_iommu *iommu) iommu->int_enabled = true;iommu_feature_enable (iommu,CONTROL_EVT_INT_EN ); +if (iommu->ppr_log !=NULL ) +iommu_feature_enable (iommu,CONTROL_PPFINT_EN ); +return 0 ; }
@@ -1287,6 +1337,7 @@ static void enable_iommus(void)iommu_set_device_table (iommu);iommu_enable_command_buffer (iommu);iommu_enable_event_buffer (iommu); +iommu_enable_ppr_log (iommu);iommu_set_exclusion_range (iommu);iommu_init_msi (iommu);iommu_enable (iommu);
drivers/iommu/amd_iommu_types.h +14 -0
@@ -69,11 +69,14 @@#define MMIO_EXCL_BASE_OFFSET 0x0020#define MMIO_EXCL_LIMIT_OFFSET 0x0028#define MMIO_EXT_FEATURES 0x0030 +#define MMIO_PPR_LOG_OFFSET 0x0038#define MMIO_CMD_HEAD_OFFSET 0x2000#define MMIO_CMD_TAIL_OFFSET 0x2008#define MMIO_EVT_HEAD_OFFSET 0x2010#define MMIO_EVT_TAIL_OFFSET 0x2018#define MMIO_STATUS_OFFSET 0x2020 +#define MMIO_PPR_HEAD_OFFSET 0x2030 +#define MMIO_PPR_TAIL_OFFSET 0x2038/* Extended Feature Bits */
@@ -125,6 +128,7 @@#define CONTROL_CMDBUF_EN 0x0cULL#define CONTROL_PPFLOG_EN 0x0dULL#define CONTROL_PPFINT_EN 0x0eULL +#define CONTROL_PPR_EN 0x0fULL/* command specific defines */ #define CMD_COMPL_WAIT 0x01
@@ -168,6 +172,13 @@#define EVT_BUFFER_SIZE 8192/* 512 entries */ #define EVT_LEN_MASK (0x9ULL << 56) +/* Constants for PPR Log handling */ +#define PPR_LOG_ENTRIES 512 +#define PPR_LOG_SIZE_SHIFT 56 +#define PPR_LOG_SIZE_512 (0x9ULL << PPR_LOG_SIZE_SHIFT) +#define PPR_ENTRY_SIZE 16 +#define PPR_LOG_SIZE (PPR_ENTRY_SIZE * PPR_LOG_ENTRIES) +#define PAGE_MODE_NONE 0x00#define PAGE_MODE_1_LEVEL 0x01#define PAGE_MODE_2_LEVEL 0x02
@@ -435,6 +446,9 @@ struct amd_iommu {/* MSI number for event interrupt */ u16 evt_msi_num; +/* Base of the PPR log, if present */ +u8 *ppr_log; +/* true if interrupts for this IOMMU are already enabled */ bool int_enabled;
Signature · 1 line
1.7.5.4
PATCH 5/22 iommu/amd: Enable GT mode when supported by IOMMU
In reply to Joerg RoedelThis feature needs to be enabled before IOMMUv2 DTEs can be set up.
--- drivers/iommu/amd_iommu_init.c | 9 +++++++++ drivers/iommu/amd_iommu_types.h | 1 + 2 files changed, 10 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu_init.c +9 -0
@@ -623,6 +623,14 @@ static void __init free_ppr_log(struct amd_iommu *iommu)free_pages ((unsigned long )iommu->ppr_log,get_order (PPR_LOG_SIZE )); } +static void iommu_enable_gt (struct amd_iommu *iommu) +{ +if (!iommu_feature (iommu,FEATURE_GT )) +return ; + +iommu_feature_enable (iommu,CONTROL_GT_EN ); +} +/* sets a specific bit in the device table entry. */ static void set_dev_entry_bit (u16 devid,u8 bit) {
@@ -1338,6 +1346,7 @@ static void enable_iommus(void)iommu_enable_command_buffer (iommu);iommu_enable_event_buffer (iommu);iommu_enable_ppr_log (iommu); +iommu_enable_gt (iommu);iommu_set_exclusion_range (iommu);iommu_init_msi (iommu);iommu_enable (iommu);
drivers/iommu/amd_iommu_types.h +1 -0
@@ -129,6 +129,7 @@#define CONTROL_PPFLOG_EN 0x0dULL#define CONTROL_PPFINT_EN 0x0eULL#define CONTROL_PPR_EN 0x0fULL +#define CONTROL_GT_EN 0x10ULL/* command specific defines */ #define CMD_COMPL_WAIT 0x01
Signature · 1 line
1.7.5.4
PATCH 6/22 iommu/amd: Add iommuv2 flag to struct amd_iommu
In reply to Joerg RoedelIn mixed IOMMU setups this flag inidicates whether an IOMMU supports the v2 features or not. This patch also adds a global flag together with a function to query that flag from other code. The flag shows if at least one IOMMUv2 is in the system.
--- drivers/iommu/amd_iommu_init.c | 15 +++++++++++++++ drivers/iommu/amd_iommu_proto.h | 3 +++ drivers/iommu/amd_iommu_types.h | 5 +++++ 3 files changed, 23 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu_init.c +15 -0
@@ -25,6 +25,7 @@#include <linux/interrupt.h> #include <linux/msi.h> #include <linux/amd-iommu.h> +#include <linux/export.h> #include <asm/pci-direct.h> #include <asm/iommu.h> #include <asm/gart.h>
@@ -143,6 +144,8 @@ bool amd_iommu_iotlb_sup __read_mostly = true;u32 amd_iommu_max_pasids__read_mostly = ~0 ; +bool amd_iommu_v2_present__read_mostly ; +/* * The ACPI table parsing functions set this variable on an error */
@@ -760,6 +763,12 @@ static void __init init_iommu_from_pci(struct amd_iommu *iommu) amd_iommu_max_pasids =min (amd_iommu_max_pasids, pasids); } +if (iommu_feature (iommu,FEATURE_GT ) && +iommu_feature (iommu,FEATURE_PPR )) { + iommu->is_iommu_v2 = true; + amd_iommu_v2_present = true; + } +if (!is_rd890_iommu (iommu->dev))return ;
@@ -1645,3 +1654,9 @@ IOMMU_INIT_FINISH(amd_iommu_detect, gart_iommu_hole_init,0 , 0); + +bool amd_iommu_v2_supported (void ) +{ +return amd_iommu_v2_present; +} +EXPORT_SYMBOL (amd_iommu_v2_supported);
drivers/iommu/amd_iommu_proto.h +3 -0
@@ -31,6 +31,9 @@ extern int amd_iommu_init_devices(void);extern void amd_iommu_uninit_devices (void );extern void amd_iommu_init_notifier (void );extern void amd_iommu_init_api (void ); + +extern bool amd_iommu_v2_supported (void ); +#ifndef CONFIG_AMD_IOMMU_STATS static inline void amd_iommu_stats_init (void ) { }
drivers/iommu/amd_iommu_types.h +5 -0
@@ -415,6 +415,9 @@ struct amd_iommu {/* Extended features */ u64 features; +/* IOMMUv2 */ +bool is_iommu_v2; +/* * Capability pointer. There could be more than one IOMMU per PCI * device function if there are more than one AMD IOMMU capability
@@ -571,6 +574,8 @@ extern bool amd_iommu_unmap_flush;/* Smallest number of PASIDs supported by any IOMMU in the system */ extern u32 amd_iommu_max_pasids; +extern bool amd_iommu_v2_present; +/* takes bus and device/function and returns the device id * FIXME: should that be in generic PCI code? */ static inline u16 calc_devid (u8 bus,u8 devfn)
Signature · 1 line
1.7.5.4
PATCH 7/22 iommu/amd: Put IOMMUv2 capable devices in pt_domain
In reply to Joerg RoedelIf the device starts to use IOMMUv2 features the dma handles need to stay valid. The only sane way to do this is to use a identity mapping for the device and not translate it by the iommu. This is implemented with this patch. Since this lifts the device-isolation there is also a new kernel parameter which allows to disable that feature.
--- Documentation/kernel-parameters.txt | 5 ++ drivers/iommu/amd_iommu.c | 94 +++++++++++++++++++++++++++++------ drivers/iommu/amd_iommu_init.c | 4 ++ drivers/iommu/amd_iommu_types.h | 4 ++ 4 files changed, 91 insertions(+), 16 deletions(-)
Documentation/kernel-parameters.txt +5 -0
@@ -329,6 +329,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. is a lot of faster off - do not initialize any AMD IOMMU found in the system + force_isolation - Force device isolation for all + devices. The IOMMU driver is not + allowed anymore to lift isolation + requirements as needed. This option + does not override iommu=pt amijoy.map= [HW,JOY] Amiga joystick support Map of devices attached to JOY0DAT and JOY1DAT
drivers/iommu/amd_iommu.c +78 -16
188 lines not on this page · raw message
drivers/iommu/amd_iommu_init.c +4 -0
@@ -146,6 +146,8 @@ u32 amd_iommu_max_pasids __read_mostly = ~0;bool amd_iommu_v2_present__read_mostly ; +bool amd_iommu_force_isolation__read_mostly ; +/* * The ACPI table parsing functions set this variable on an error */
@@ -1642,6 +1644,8 @@ static int __init parse_amd_iommu_options(char *str) amd_iommu_unmap_flush = true;if (strncmp (str,"off" ,3 ) ==0 ) amd_iommu_disabled = true; +if (strncmp (str,"force_isolation" ,15 ) ==0 ) + amd_iommu_force_isolation = true; }return 1 ;
drivers/iommu/amd_iommu_types.h +4 -0
@@ -331,6 +331,8 @@ struct iommu_dev_data {struct protection_domain *domain;/* Domain the device is bound to */ atomic_t bind;/* Domain attach reverent count */ u16 devid;/* PCI Device ID */ +bool iommu_v2;/* Device can make use of IOMMUv2 */ +bool passthrough;/* Default for device is pt_domain */ struct {bool enabled;int qdep;
@@ -576,6 +578,8 @@ extern u32 amd_iommu_max_pasids;extern bool amd_iommu_v2_present; +extern bool amd_iommu_force_isolation; +/* takes bus and device/function and returns the device id * FIXME: should that be in generic PCI code? */ static inline u16 calc_devid (u8 bus,u8 devfn)
Signature · 1 line
1.7.5.4
PATCH 8/22 iommu/amd: Implement notifier for PPR faults
In reply to Joerg RoedelAdd a notifer at which a module can attach to get informed about incoming PPR faults.
--- drivers/iommu/amd_iommu.c | 90 ++++++++++++++++++++++++++++++++++++++- drivers/iommu/amd_iommu_proto.h | 3 + drivers/iommu/amd_iommu_types.h | 34 ++++++++++++++- 3 files changed, 125 insertions(+), 2 deletions(-)
drivers/iommu/amd_iommu.c +89 -1
@@ -17,6 +17,7 @@* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <linux/ratelimit.h> #include <linux/pci.h> #include <linux/pci-ats.h> #include <linux/bitmap.h>
@@ -28,6 +29,8 @@#include <linux/iommu.h> #include <linux/delay.h> #include <linux/amd-iommu.h> +#include <linux/notifier.h> +#include <linux/export.h> #include <asm/msidef.h> #include <asm/proto.h> #include <asm/iommu.h>
@@ -59,6 +62,8 @@ static struct protection_domain *pt_domain;static struct iommu_ops amd_iommu_ops; +static ATOMIC_NOTIFIER_HEAD (ppr_notifier); +/* * general struct to manage commands send to an IOMMU */
@@ -488,12 +493,82 @@ static void iommu_poll_events(struct amd_iommu *iommu)spin_unlock_irqrestore (&iommu->lock, flags); } +static void iommu_handle_ppr_entry (struct amd_iommu *iommu,u32 head) +{ +struct amd_iommu_fault fault; +volatile u64 *raw; +int i; + + raw = (u64 *)(iommu->ppr_log + head); + +/* +* Hardware bug: Interrupt may arrive before the entry is written to +* memory. If this happens we need to wait for the entry to arrive. +*/ +for (i =0 ; i <LOOP_TIMEOUT ; ++i) { +if (PPR_REQ_TYPE (raw[0 ]) !=0 ) +break ; +udelay (1 ); + } + +if (PPR_REQ_TYPE (raw[0 ]) !=PPR_REQ_FAULT ) { +pr_err_ratelimited ("AMD-Vi: Unknown PPR request received\n" ); +return ; + } + + fault.address = raw[1 ]; + fault.pasid =PPR_PASID (raw[0 ]); + fault.device_id =PPR_DEVID (raw[0 ]); + fault.tag =PPR_TAG (raw[0 ]); + fault.flags =PPR_FLAGS (raw[0 ]); + +/* +* To detect the hardware bug we need to clear the entry +* to back to zero. +*/ + raw[0 ] = raw[1 ] =0 ; + +atomic_notifier_call_chain (&ppr_notifier,0 , &fault); +} + +static void iommu_poll_ppr_log (struct amd_iommu *iommu) +{ +unsigned long flags; +u32 head, tail; + +if (iommu->ppr_log ==NULL ) +return ; + +spin_lock_irqsave (&iommu->lock, flags); + + head =readl (iommu->mmio_base +MMIO_PPR_HEAD_OFFSET ); + tail =readl (iommu->mmio_base +MMIO_PPR_TAIL_OFFSET ); + +while (head != tail) { + +/* Handle PPR entry */ +iommu_handle_ppr_entry (iommu, head); + +/* Update and refresh ring-buffer state*/ + head = (head +PPR_ENTRY_SIZE ) %PPR_LOG_SIZE ; +writel (head, iommu->mmio_base +MMIO_PPR_HEAD_OFFSET ); + tail =readl (iommu->mmio_base +MMIO_PPR_TAIL_OFFSET ); + } + +/* enable ppr interrupts again */ +writel (MMIO_STATUS_PPR_INT_MASK , iommu->mmio_base +MMIO_STATUS_OFFSET ); + +spin_unlock_irqrestore (&iommu->lock, flags); +} +irqreturn_t amd_iommu_int_thread (int irq,void *data) {struct amd_iommu *iommu; -for_each_iommu (iommu) +for_each_iommu (iommu) {iommu_poll_events (iommu); +iommu_poll_ppr_log (iommu); + }return IRQ_HANDLED ; }
@@ -2888,3 +2963,16 @@ int __init amd_iommu_init_passthrough(void)return 0 ; } + +/* IOMMUv2 specific functions */ +int amd_iommu_register_ppr_notifier (struct notifier_block *nb) +{ +return atomic_notifier_chain_register (&ppr_notifier, nb); +} +EXPORT_SYMBOL (amd_iommu_register_ppr_notifier); + +int amd_iommu_unregister_ppr_notifier (struct notifier_block *nb) +{ +return atomic_notifier_chain_unregister (&ppr_notifier, nb); +} +EXPORT_SYMBOL (amd_iommu_unregister_ppr_notifier);
drivers/iommu/amd_iommu_proto.h +3 -0
@@ -32,7 +32,10 @@ extern void amd_iommu_uninit_devices(void);extern void amd_iommu_init_notifier (void );extern void amd_iommu_init_api (void ); +/* IOMMUv2 specific functions */ extern bool amd_iommu_v2_supported (void ); +extern int amd_iommu_register_ppr_notifier (struct notifier_block *nb); +extern int amd_iommu_unregister_ppr_notifier (struct notifier_block *nb);#ifndef CONFIG_AMD_IOMMU_STATS
drivers/iommu/amd_iommu_types.h +33 -1
@@ -94,7 +94,8 @@#define FEATURE_PASID_MASK (0x1fULL << FEATURE_PASID_SHIFT)/* MMIO status bits */ -#define MMIO_STATUS_COM_WAIT_INT_MASK 0x04 +#define MMIO_STATUS_COM_WAIT_INT_MASK (1 << 2) +#define MMIO_STATUS_PPR_INT_MASK (1 << 6)/* event logging constants */ #define EVENT_ENTRY_SIZE 0x10
@@ -180,6 +181,16 @@#define PPR_ENTRY_SIZE 16#define PPR_LOG_SIZE (PPR_ENTRY_SIZE * PPR_LOG_ENTRIES) +#define PPR_REQ_TYPE (x) (((x) >> 60) & 0xfULL) +#define PPR_FLAGS (x) (((x) >> 48) & 0xfffULL) +#define PPR_DEVID (x) ((x) & 0xffffULL) +#define PPR_TAG (x) (((x) >> 32) & 0x3ffULL) +#define PPR_PASID1 (x) (((x) >> 16) & 0xffffULL) +#define PPR_PASID2 (x) (((x) >> 42) & 0xfULL) +#define PPR_PASID (x) ((PPR_PASID2(x) << 16) | PPR_PASID1(x)) + +#define PPR_REQ_FAULT 0x01 +#define PAGE_MODE_NONE 0x00#define PAGE_MODE_1_LEVEL 0x01#define PAGE_MODE_2_LEVEL 0x02
@@ -301,6 +312,27 @@ extern bool amd_iommu_iotlb_sup;#define APERTURE_RANGE_INDEX (a) ((a) >> APERTURE_RANGE_SHIFT)#define APERTURE_PAGE_INDEX (a) (((a) >> 21) & 0x3fULL) + +/* +* This struct is used to pass information about +* incoming PPR faults around. +*/ +struct amd_iommu_fault { +u64 address;/* IO virtual address of the fault*/ +u32 pasid;/* Address space identifier */ +u16 device_id;/* Originating PCI device id */ +u16 tag;/* PPR tag */ +u16 flags;/* Fault flags */ + +}; + +#define PPR_FAULT_EXEC (1 << 1) +#define PPR_FAULT_READ (1 << 2) +#define PPR_FAULT_WRITE (1 << 5) +#define PPR_FAULT_USER (1 << 6) +#define PPR_FAULT_RSVD (1 << 7) +#define PPR_FAULT_GN (1 << 8) +/* * This structure contains generic data for IOMMU protection domains * independent of their use.
Signature · 1 line
1.7.5.4
PATCH 9/22 iommu/amd: Add amd_iommu_domain_direct_map function
In reply to Joerg RoedelThis function can be used to switch a domain into paging-mode 0. In this mode all devices can access physical system memory directly without any remapping.
--- drivers/iommu/amd_iommu.c | 38 ++++++++++++++++++++++++++++++++++++-- drivers/iommu/amd_iommu_proto.h | 3 +++ 2 files changed, 39 insertions(+), 2 deletions(-)
drivers/iommu/amd_iommu.c +36 -2
@@ -1684,9 +1684,12 @@ static bool dma_ops_domain(struct protection_domain *domain)static void set_dte_entry (u16 devid,struct protection_domain *domain,bool ats) { -u64 pte_root =virt_to_phys (domain->pt_root); +u64 pte_root =0 ;u64 flags =0 ; +if (domain->mode !=PAGE_MODE_NONE ) + pte_root =virt_to_phys (domain->pt_root); + pte_root |= (domain->mode &DEV_ENTRY_MODE_MASK ) <<DEV_ENTRY_MODE_SHIFT ; pte_root |=IOMMU_PTE_IR |IOMMU_PTE_IW |IOMMU_PTE_P |IOMMU_PTE_TV ;
@@ -2782,7 +2785,8 @@ static void amd_iommu_domain_destroy(struct iommu_domain *dom)BUG_ON (domain->dev_cnt !=0 ); -free_pagetable (domain); +if (domain->mode !=PAGE_MODE_NONE ) +free_pagetable (domain);protection_domain_free (domain);
@@ -2846,6 +2850,9 @@ static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,int prot =0 ;int ret; +if (domain->mode ==PAGE_MODE_NONE ) +return -EINVAL ; +if (iommu_prot &IOMMU_READ ) prot |=IOMMU_PROT_IR ;if (iommu_prot &IOMMU_WRITE )
@@ -2864,6 +2871,9 @@ static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,struct protection_domain *domain = dom->priv;unsigned long page_size, unmap_size; +if (domain->mode ==PAGE_MODE_NONE ) +return -EINVAL ; + page_size =0x1000UL << gfp_order;mutex_lock (&domain->api_lock);
@@ -2883,6 +2893,9 @@ static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,phys_addr_t paddr;u64 *pte, __pte; +if (domain->mode ==PAGE_MODE_NONE ) +return iova; + pte =fetch_pte (domain, iova);if (!pte || !IOMMU_PTE_PRESENT (*pte))
@@ -2976,3 +2989,24 @@ int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)return atomic_notifier_chain_unregister (&ppr_notifier, nb); }EXPORT_SYMBOL (amd_iommu_unregister_ppr_notifier); + +void amd_iommu_domain_direct_map (struct iommu_domain *dom) +{ +struct protection_domain *domain = dom->priv; +unsigned long flags; + +spin_lock_irqsave (&domain->lock, flags); + +/* Update data structure */ + domain->mode =PAGE_MODE_NONE ; + domain->updated = true; + +/* Make changes visible to IOMMUs */ +update_domain (domain); + +/* Page-table is not visible to IOMMU anymore, so free it */ +free_pagetable (domain); + +spin_unlock_irqrestore (&domain->lock, flags); +} +EXPORT_SYMBOL (amd_iommu_domain_direct_map);
drivers/iommu/amd_iommu_proto.h +3 -0
@@ -33,9 +33,12 @@ extern void amd_iommu_init_notifier(void);extern void amd_iommu_init_api (void );/* IOMMUv2 specific functions */ +struct iommu_domain ; +extern bool amd_iommu_v2_supported (void );extern int amd_iommu_register_ppr_notifier (struct notifier_block *nb);extern int amd_iommu_unregister_ppr_notifier (struct notifier_block *nb); +extern void amd_iommu_domain_direct_map (struct iommu_domain *dom);#ifndef CONFIG_AMD_IOMMU_STATS
Signature · 1 line
1.7.5.4
PATCH 10/22 iommu/amd: Add support for IOMMUv2 domain mode
In reply to Joerg RoedelThis patch adds support for protection domains that implement two-level paging for devices.
--- drivers/iommu/Kconfig | 4 +- drivers/iommu/amd_iommu.c | 144 +++++++++++++++++++++++++++++++++++++- drivers/iommu/amd_iommu_init.c | 9 +++ drivers/iommu/amd_iommu_proto.h | 1 + drivers/iommu/amd_iommu_types.h | 27 +++++++ 5 files changed, 180 insertions(+), 5 deletions(-)
drivers/iommu/Kconfig +3 -1
@@ -34,7 +34,9 @@ config AMD_IOMMU bool "AMD IOMMU support" select SWIOTLB select PCI_MSI - select PCI_IOV + select PCI_ATS + select PCI_PRI + select PCI_PASID select IOMMU_API depends on X86_64 && PCI && ACPI ---help---
drivers/iommu/amd_iommu.c +140 -4
200 lines not on this page · raw message
drivers/iommu/amd_iommu_init.c +9 -0
@@ -755,6 +755,7 @@ static void __init init_iommu_from_pci(struct amd_iommu *iommu) iommu->features = ((u64 )high <<32 ) | low;if (iommu_feature (iommu,FEATURE_GT )) { +int glxval;u32 pasids;u64 shift;
@@ -763,6 +764,14 @@ static void __init init_iommu_from_pci(struct amd_iommu *iommu) pasids = (1 << shift); amd_iommu_max_pasids =min (amd_iommu_max_pasids, pasids); + + glxval = iommu->features &FEATURE_GLXVAL_MASK ; + glxval >>=FEATURE_GLXVAL_SHIFT ; + +if (amd_iommu_max_glx_val ==-1 ) + amd_iommu_max_glx_val = glxval; +else + amd_iommu_max_glx_val =min (amd_iommu_max_glx_val, glxval); } if (iommu_feature(iommu, FEATURE_GT) &&
drivers/iommu/amd_iommu_proto.h +1 -0
@@ -39,6 +39,7 @@ extern bool amd_iommu_v2_supported(void);extern int amd_iommu_register_ppr_notifier (struct notifier_block *nb);extern int amd_iommu_unregister_ppr_notifier (struct notifier_block *nb);extern void amd_iommu_domain_direct_map (struct iommu_domain *dom); +extern int amd_iommu_domain_enable_v2 (struct iommu_domain *dom,int pasids);#ifndef CONFIG_AMD_IOMMU_STATS
drivers/iommu/amd_iommu_types.h +27 -0
@@ -93,6 +93,11 @@#define FEATURE_PASID_SHIFT 32#define FEATURE_PASID_MASK (0x1fULL << FEATURE_PASID_SHIFT) +#define FEATURE_GLXVAL_SHIFT 14 +#define FEATURE_GLXVAL_MASK (0x03ULL << FEATURE_GLXVAL_SHIFT) + +#define PASID_MASK 0x000fffff +/* MMIO status bits */ #define MMIO_STATUS_COM_WAIT_INT_MASK (1 << 2)#define MMIO_STATUS_PPR_INT_MASK (1 << 6)
@@ -257,6 +262,22 @@#define IOMMU_PTE_IW (1ULL << 62)#define DTE_FLAG_IOTLB (0x01UL << 32) +#define DTE_FLAG_GV (0x01ULL << 55) +#define DTE_GLX_SHIFT (56) +#define DTE_GLX_MASK (3) + +#define DTE_GCR3_VAL_A (x) (((x) >> 12) & 0x00007ULL) +#define DTE_GCR3_VAL_B (x) (((x) >> 15) & 0x0ffffULL) +#define DTE_GCR3_VAL_C (x) (((x) >> 31) & 0xfffffULL) + +#define DTE_GCR3_INDEX_A 0 +#define DTE_GCR3_INDEX_B 1 +#define DTE_GCR3_INDEX_C 1 + +#define DTE_GCR3_SHIFT_A 58 +#define DTE_GCR3_SHIFT_B 16 +#define DTE_GCR3_SHIFT_C 43 +#define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL)#define IOMMU_IO_PAGE_MASK (~(0xfffULL))
@@ -284,6 +305,7 @@domain for an IOMMU */ #define PD_PASSTHROUGH_MASK (1UL << 2)/* domain has no page translation */ +#define PD_IOMMUV2_MASK (1UL << 3)/* domain has gcr3 table */ extern bool amd_iommu_dump; #define DUMP_printk(format, arg...) \
@@ -345,6 +367,8 @@ struct protection_domain {u16 id;/* the domain id written to the device table */ int mode;/* paging mode (0-6 levels) */ u64 *pt_root;/* page table root pointer */ +int glx;/* Number of levels for GCR3 table */ +u64 *gcr3_tbl;/* Guest CR3 table */ unsigned long flags;/* flags to find out type of domain */ bool updated;/* complete domain flush required */ unsigned dev_cnt;/* devices assigned to this domain */
@@ -612,6 +636,9 @@ extern bool amd_iommu_v2_present;extern bool amd_iommu_force_isolation; +/* Max levels of glxval supported */ +extern int amd_iommu_max_glx_val; +/* takes bus and device/function and returns the device id * FIXME: should that be in generic PCI code? */ static inline u16 calc_devid (u8 bus,u8 devfn)
Signature · 1 line
1.7.5.4
PATCH 11/22 iommu/amd: Implement IOMMUv2 TLB flushing routines
In reply to Joerg RoedelThe functions added with this patch allow to manage the IOMMU and the device TLBs for all devices in an IOMMUv2 domain.
--- drivers/iommu/amd_iommu.c | 136 +++++++++++++++++++++++++++++++++++++++ drivers/iommu/amd_iommu_proto.h | 3 + drivers/iommu/amd_iommu_types.h | 1 + 3 files changed, 140 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu.c +136 -0
@@ -698,6 +698,44 @@ static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_SIZE_MASK ; } +static void build_inv_iommu_pasid (struct iommu_cmd *cmd,u16 domid,int pasid, +u64 address,bool size) +{ +memset (cmd,0 ,sizeof (*cmd)); + + address &= ~(0xfffULL ); + + cmd->data[0 ] = pasid &PASID_MASK ; + cmd->data[1 ] = domid; + cmd->data[2 ] =lower_32_bits (address); + cmd->data[3 ] =upper_32_bits (address); + cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_PDE_MASK ; + cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_GN_MASK ; +if (size) + cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_SIZE_MASK ; +CMD_SET_TYPE (cmd,CMD_INV_IOMMU_PAGES ); +} + +static void build_inv_iotlb_pasid (struct iommu_cmd *cmd,u16 devid,int pasid, +int qdep,u64 address,bool size) +{ +memset (cmd,0 ,sizeof (*cmd)); + + address &= ~(0xfffULL ); + + cmd->data[0 ] = devid; + cmd->data[0 ] |= (pasid &0xff ) <<16 ; + cmd->data[0 ] |= (qdep &0xff ) <<24 ; + cmd->data[1 ] = devid; + cmd->data[1 ] |= ((pasid >>8 ) &0xfff ) <<16 ; + cmd->data[2 ] =lower_32_bits (address); + cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_GN_MASK ; + cmd->data[3 ] =upper_32_bits (address); +if (size) + cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_SIZE_MASK ; +CMD_SET_TYPE (cmd,CMD_INV_IOTLB_PAGES ); +} +static void build_inv_all (struct iommu_cmd *cmd) {memset (cmd,0 ,sizeof (*cmd));
@@ -3146,3 +3184,101 @@ out:return ret; }EXPORT_SYMBOL (amd_iommu_domain_enable_v2); + +static int __flush_pasid (struct protection_domain *domain,int pasid, +u64 address,bool size) +{ +struct iommu_dev_data *dev_data; +struct iommu_cmd cmd; +int i, ret; + +if (!(domain->flags &PD_IOMMUV2_MASK )) +return -EINVAL ; + +build_inv_iommu_pasid (&cmd, domain->id, pasid, address, size); + +/* +* IOMMU TLB needs to be flushed before Device TLB to +* prevent device TLB refill from IOMMU TLB +*/ +for (i =0 ; i < amd_iommus_present; ++i) { +if (domain->dev_iommu[i] ==0 ) +continue ; + + ret =iommu_queue_command (amd_iommus[i], &cmd); +if (ret !=0 ) + goto out; + } + +/* Wait until IOMMU TLB flushes are complete */ +domain_flush_complete (domain); + +/* Now flush device TLBs */ +list_for_each_entry (dev_data, &domain->dev_list, list) { +struct amd_iommu *iommu; +int qdep; + +BUG_ON (!dev_data->ats.enabled); + + qdep = dev_data->ats.qdep; + iommu = amd_iommu_rlookup_table[dev_data->devid]; + +build_inv_iotlb_pasid (&cmd, dev_data->devid, pasid, + qdep, address, size); + + ret =iommu_queue_command (iommu, &cmd); +if (ret !=0 ) + goto out; + } + +/* Wait until all device TLBs are flushed */ +domain_flush_complete (domain); + + ret =0 ; + +out: + +return ret; +} + +static int __amd_iommu_flush_page (struct protection_domain *domain,int pasid, +u64 address) +{ +return __flush_pasid (domain, pasid, address, false); +} + +int amd_iommu_flush_page (struct iommu_domain *dom,int pasid, +u64 address) +{ +struct protection_domain *domain = dom->priv; +unsigned long flags; +int ret; + +spin_lock_irqsave (&domain->lock, flags); + ret =__amd_iommu_flush_page (domain, pasid, address); +spin_unlock_irqrestore (&domain->lock, flags); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_flush_page); + +static int __amd_iommu_flush_tlb (struct protection_domain *domain,int pasid) +{ +return __flush_pasid (domain, pasid,CMD_INV_IOMMU_ALL_PAGES_ADDRESS , + true); +} + +int amd_iommu_flush_tlb (struct iommu_domain *dom,int pasid) +{ +struct protection_domain *domain = dom->priv; +unsigned long flags; +int ret; + +spin_lock_irqsave (&domain->lock, flags); + ret =__amd_iommu_flush_tlb (domain, pasid); +spin_unlock_irqrestore (&domain->lock, flags); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_flush_tlb); +
drivers/iommu/amd_iommu_proto.h +3 -0
@@ -40,6 +40,9 @@ extern int amd_iommu_register_ppr_notifier(struct notifier_block *nb);extern int amd_iommu_unregister_ppr_notifier (struct notifier_block *nb);extern void amd_iommu_domain_direct_map (struct iommu_domain *dom);extern int amd_iommu_domain_enable_v2 (struct iommu_domain *dom,int pasids); +extern int amd_iommu_flush_page (struct iommu_domain *dom,int pasid, +u64 address); +extern int amd_iommu_flush_tlb (struct iommu_domain *dom,int pasid);#ifndef CONFIG_AMD_IOMMU_STATS
drivers/iommu/amd_iommu_types.h +1 -0
@@ -148,6 +148,7 @@#define CMD_COMPL_WAIT_INT_MASK 0x02#define CMD_INV_IOMMU_PAGES_SIZE_MASK 0x01#define CMD_INV_IOMMU_PAGES_PDE_MASK 0x02 +#define CMD_INV_IOMMU_PAGES_GN_MASK 0x04#define CMD_INV_IOMMU_ALL_PAGES_ADDRESS 0x7fffffffffffffffULL
Signature · 1 line
1.7.5.4
PATCH 12/22 iommu/amd: Implement functions to manage GCR3 table
In reply to Joerg RoedelThis patch adds functions necessary to set and clear the GCR3 values associated with a particular PASID in an IOMMUv2 domain.
--- drivers/iommu/amd_iommu.c | 130 +++++++++++++++++++++++++++++++++++++++ drivers/iommu/amd_iommu_proto.h | 4 + drivers/iommu/amd_iommu_types.h | 1 + 3 files changed, 135 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu.c +130 -0
@@ -1637,8 +1637,45 @@ static void free_pagetable(struct protection_domain *domain) domain->pt_root =NULL ; } +static void free_gcr3_tbl_level1 (u64 *tbl) +{ +u64 *ptr; +int i; + +for (i =0 ; i <512 ; ++i) { +if (!(tbl[i] &GCR3_VALID )) +continue ; + + ptr =__va (tbl[i] &PAGE_MASK ); + +free_page ((unsigned long )ptr); + } +} + +static void free_gcr3_tbl_level2 (u64 *tbl) +{ +u64 *ptr; +int i; + +for (i =0 ; i <512 ; ++i) { +if (!(tbl[i] &GCR3_VALID )) +continue ; + + ptr =__va (tbl[i] &PAGE_MASK ); + +free_gcr3_tbl_level1 (ptr); + } +} +static void free_gcr3_table (struct protection_domain *domain) { +if (domain->glx ==2 ) +free_gcr3_tbl_level2 (domain->gcr3_tbl); +else if (domain->glx ==1 ) +free_gcr3_tbl_level1 (domain->gcr3_tbl); +else if (domain->glx !=0 ) +BUG (); +free_page ((unsigned long )domain->gcr3_tbl); }
@@ -3282,3 +3319,96 @@ int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid) }EXPORT_SYMBOL (amd_iommu_flush_tlb); +static u64 *__get_gcr3_pte (u64 *root,int level,int pasid,bool alloc) +{ +int index; +u64 *pte; + +while (true) { + + index = (pasid >> (9 * level)) &0x1ff ; + pte = &root[index]; + +if (level ==0 ) +break ; + +if (!(*pte &GCR3_VALID )) { +if (!alloc) +return NULL ; + + root = (void *)get_zeroed_page (GFP_ATOMIC ); +if (root ==NULL ) +return NULL ; + + *pte =__pa (root) |GCR3_VALID ; + } + + root =__va (*pte &PAGE_MASK ); + + level -=1 ; + } + +return pte; +} + +static int __set_gcr3 (struct protection_domain *domain,int pasid, +unsigned long cr3) +{ +u64 *pte; + +if (domain->mode !=PAGE_MODE_NONE ) +return -EINVAL ; + + pte =__get_gcr3_pte (domain->gcr3_tbl, domain->glx, pasid, true); +if (pte ==NULL ) +return -ENOMEM ; + + *pte = (cr3 &PAGE_MASK ) |GCR3_VALID ; + +return __amd_iommu_flush_tlb (domain, pasid); +} + +static int __clear_gcr3 (struct protection_domain *domain,int pasid) +{ +u64 *pte; + +if (domain->mode !=PAGE_MODE_NONE ) +return -EINVAL ; + + pte =__get_gcr3_pte (domain->gcr3_tbl, domain->glx, pasid, false); +if (pte ==NULL ) +return 0 ; + + *pte =0 ; + +return __amd_iommu_flush_tlb (domain, pasid); +} + +int amd_iommu_domain_set_gcr3 (struct iommu_domain *dom,int pasid, +unsigned long cr3) +{ +struct protection_domain *domain = dom->priv; +unsigned long flags; +int ret; + +spin_lock_irqsave (&domain->lock, flags); + ret =__set_gcr3 (domain, pasid, cr3); +spin_unlock_irqrestore (&domain->lock, flags); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_domain_set_gcr3); + +int amd_iommu_domain_clear_gcr3 (struct iommu_domain *dom,int pasid) +{ +struct protection_domain *domain = dom->priv; +unsigned long flags; +int ret; + +spin_lock_irqsave (&domain->lock, flags); + ret =__clear_gcr3 (domain, pasid); +spin_unlock_irqrestore (&domain->lock, flags); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_domain_clear_gcr3);
drivers/iommu/amd_iommu_proto.h +4 -0
@@ -43,6 +43,10 @@ extern int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids);extern int amd_iommu_flush_page (struct iommu_domain *dom,int pasid,u64 address);extern int amd_iommu_flush_tlb (struct iommu_domain *dom,int pasid); +extern int amd_iommu_domain_set_gcr3 (struct iommu_domain *dom,int pasid, +unsigned long cr3); +extern int amd_iommu_domain_clear_gcr3 (struct iommu_domain *dom,int pasid); +#ifndef CONFIG_AMD_IOMMU_STATS
drivers/iommu/amd_iommu_types.h +1 -0
@@ -279,6 +279,7 @@#define DTE_GCR3_SHIFT_B 16#define DTE_GCR3_SHIFT_C 43 +#define GCR3_VALID 0x01ULL#define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL)#define IOMMU_IO_PAGE_MASK (~(0xfffULL))
Signature · 1 line
1.7.5.4
PATCH 13/22 iommu/amd: Implement function to send PPR completions
In reply to Joerg RoedelTo send completions for PPR requests this patch adds a function which can be used by the IOMMUv2 driver.
--- drivers/iommu/amd_iommu.c | 51 +++++++++++++++++++++++++++++++++++++++ drivers/iommu/amd_iommu_proto.h | 6 ++++ drivers/iommu/amd_iommu_types.h | 6 ++++ 3 files changed, 63 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu.c +51 -0
@@ -736,6 +736,22 @@ static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,CMD_SET_TYPE (cmd,CMD_INV_IOTLB_PAGES ); } +static void build_complete_ppr (struct iommu_cmd *cmd,u16 devid,int pasid, +int status,int tag,bool gn) +{ +memset (cmd,0 ,sizeof (*cmd)); + + cmd->data[0 ] = devid; +if (gn) { + cmd->data[1 ] = pasid &PASID_MASK ; + cmd->data[2 ] =CMD_INV_IOMMU_PAGES_GN_MASK ; + } + cmd->data[3 ] = tag &0x1ff ; + cmd->data[3 ] |= (status &PPR_STATUS_MASK ) <<PPR_STATUS_SHIFT ; + +CMD_SET_TYPE (cmd,CMD_COMPLETE_PPR ); +} +static void build_inv_all (struct iommu_cmd *cmd) {memset (cmd,0 ,sizeof (*cmd));
@@ -1950,6 +1966,23 @@ out_err:return ret; } +/* FIXME: Move this to PCI code */ +#define PCI_PRI_TLP_OFF (1 << 2) + +bool pci_pri_tlp_required (struct pci_dev *pdev) +{ +u16 control; +int pos; + + pos =pci_find_ext_capability (pdev,PCI_PRI_CAP ); +if (!pos) +return false; + +pci_read_config_word (pdev, pos +PCI_PRI_CONTROL_OFF , &control); + +return (control &PCI_PRI_TLP_OFF ) ? true : false; +} +/* * If a device is not yet associated with a domain, this function does * assigns it visible for the hardware
@@ -1973,6 +2006,7 @@ static int attach_device(struct device *dev, dev_data->ats.enabled = true; dev_data->ats.qdep =pci_ats_queue_depth (pdev); + dev_data->pri_tlp =pci_pri_tlp_required (pdev); }else if (amd_iommu_iotlb_sup &&pci_enable_ats (pdev,PAGE_SHIFT ) ==0 ) { dev_data->ats.enabled = true;
@@ -3412,3 +3446,20 @@ int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)return ret; }EXPORT_SYMBOL (amd_iommu_domain_clear_gcr3); + +int amd_iommu_complete_ppr (struct pci_dev *pdev,int pasid, +int status,int tag) +{ +struct iommu_dev_data *dev_data; +struct amd_iommu *iommu; +struct iommu_cmd cmd; + + dev_data =get_dev_data (&pdev->dev); + iommu = amd_iommu_rlookup_table[dev_data->devid]; + +build_complete_ppr (&cmd, dev_data->devid, pasid, status, + tag, dev_data->pri_tlp); + +return iommu_queue_command (iommu, &cmd); +} +EXPORT_SYMBOL (amd_iommu_complete_ppr);
drivers/iommu/amd_iommu_proto.h +6 -0
@@ -47,6 +47,12 @@ extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid, unsigned long cr3);extern int amd_iommu_domain_clear_gcr3 (struct iommu_domain *dom,int pasid); +#define PPR_SUCCESS 0x0 +#define PPR_INVALID 0x1 +#define PPR_FAILURE 0xf + +extern int amd_iommu_complete_ppr (struct pci_dev *pdev,int pasid, +int status,int tag);#ifndef CONFIG_AMD_IOMMU_STATS
drivers/iommu/amd_iommu_types.h +6 -0
@@ -142,6 +142,7 @@#define CMD_INV_DEV_ENTRY 0x02#define CMD_INV_IOMMU_PAGES 0x03#define CMD_INV_IOTLB_PAGES 0x04 +#define CMD_COMPLETE_PPR 0x07#define CMD_INV_ALL 0x08#define CMD_COMPL_WAIT_STORE_MASK 0x01
@@ -150,6 +151,9 @@#define CMD_INV_IOMMU_PAGES_PDE_MASK 0x02#define CMD_INV_IOMMU_PAGES_GN_MASK 0x04 +#define PPR_STATUS_MASK 0xf +#define PPR_STATUS_SHIFT 12 +#define CMD_INV_IOMMU_ALL_PAGES_ADDRESS 0x7fffffffffffffffULL/* macros and definitions for device table entries */
@@ -395,6 +399,8 @@ struct iommu_dev_data {bool enabled;int qdep; } ats;/* ATS state */ +bool pri_tlp;/* PASID TLB required for +PPR completions */ };/*
Signature · 1 line
1.7.5.4
PATCH 14/22 iommu/amd: Add function to get IOMMUv2 domain for pdev
In reply to Joerg RoedelThe AMD IOMMUv2 driver needs to get the IOMMUv2 domain associated with a particular device. This patch adds a function to get this information.
--- drivers/iommu/amd_iommu.c | 18 ++++++++++++++++++ drivers/iommu/amd_iommu_proto.h | 1 + drivers/iommu/amd_iommu_types.h | 4 ++++ 3 files changed, 23 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu.c +18 -0
@@ -2958,6 +2958,8 @@ static int amd_iommu_domain_init(struct iommu_domain *dom)if (!domain->pt_root) goto out_free; + domain->iommu_domain = dom; + dom->priv = domain;return 0 ;
@@ -3463,3 +3465,19 @@ int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,return iommu_queue_command (iommu, &cmd); }EXPORT_SYMBOL (amd_iommu_complete_ppr); + +struct iommu_domain *amd_iommu_get_v2_domain (struct pci_dev *pdev) +{ +struct protection_domain *domain; + + domain =get_domain (&pdev->dev); +if (IS_ERR (domain)) +return NULL ; + +/* Only return IOMMUv2 domains */ +if (!(domain->flags &PD_IOMMUV2_MASK )) +return NULL ; + +return domain->iommu_domain; +} +EXPORT_SYMBOL (amd_iommu_get_v2_domain);
drivers/iommu/amd_iommu_proto.h +1 -0
@@ -46,6 +46,7 @@ extern int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid);extern int amd_iommu_domain_set_gcr3 (struct iommu_domain *dom,int pasid,unsigned long cr3);extern int amd_iommu_domain_clear_gcr3 (struct iommu_domain *dom,int pasid); +extern struct iommu_domain *amd_iommu_get_v2_domain (struct pci_dev *pdev);#define PPR_SUCCESS 0x0#define PPR_INVALID 0x1
drivers/iommu/amd_iommu_types.h +4 -0
@@ -361,6 +361,8 @@ struct amd_iommu_fault {#define PPR_FAULT_RSVD (1 << 7)#define PPR_FAULT_GN (1 << 8) +struct iommu_domain ; +/* * This structure contains generic data for IOMMU protection domains * independent of their use.
@@ -380,6 +382,8 @@ struct protection_domain {unsigned dev_cnt;/* devices assigned to this domain */ unsigned dev_iommu[MAX_IOMMUS ];/* per-IOMMU reference count */ void *priv;/* private data */ +struct iommu_domain *iommu_domain;/* Pointer to generic +domain structure */ };
Signature · 1 line
1.7.5.4
PATCH 15/22 iommu/amd: Add device errata handling
In reply to Joerg RoedelAdd infrastructure for errata-handling and handle two known erratas in the IOMMUv2 code.
--- drivers/iommu/amd_iommu.c | 57 ++++++++++++++++++++++++++++++++++++-- drivers/iommu/amd_iommu_types.h | 1 + include/linux/amd-iommu.h | 18 ++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-)
drivers/iommu/amd_iommu.c +54 -3
@@ -172,6 +172,15 @@ static bool pci_iommuv2_capable(struct pci_dev *pdev)return true; } +static bool pdev_pri_erratum (struct pci_dev *pdev,u32 erratum) +{ +struct iommu_dev_data *dev_data; + + dev_data =get_dev_data (&pdev->dev); + +return dev_data->errata & (1 << erratum) ? true : false; +} +/* * In this function the list of preallocated protection domains is traversed to * find the domain for a specific device
@@ -1934,9 +1943,33 @@ static void pdev_iommuv2_disable(struct pci_dev *pdev)pci_disable_pasid (pdev); } +/* FIXME: Change generic reset-function to do the same */ +static int pri_reset_while_enabled (struct pci_dev *pdev) +{ +u16 control; +int pos; + + pos =pci_find_ext_capability (pdev,PCI_PRI_CAP ); +if (!pos) +return -EINVAL ; + +pci_read_config_word (pdev, pos +PCI_PRI_CONTROL_OFF , &control); + control |=PCI_PRI_RESET ; +pci_write_config_word (pdev, pos +PCI_PRI_CONTROL_OFF , control); + +return 0 ; +} +static int pdev_iommuv2_enable (struct pci_dev *pdev) { -int ret; +bool reset_enable; +int reqs, ret; + +/* FIXME: Hardcode number of outstanding requests for now */ + reqs =32 ; +if (pdev_pri_erratum (pdev,AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE )) + reqs =1 ; + reset_enable =pdev_pri_erratum (pdev,AMD_PRI_DEV_ERRATUM_ENABLE_RESET );/* Only allow access to user-accessible pages */ ret =pci_enable_pasid (pdev,0 );
@@ -1948,11 +1981,17 @@ static int pdev_iommuv2_enable(struct pci_dev *pdev)if (ret) goto out_err; -/* FIXME: Hardcode number of outstanding requests for now */ - ret =pci_enable_pri (pdev,32 ); +/* Enable PRI */ + ret =pci_enable_pri (pdev, reqs);if (ret) goto out_err; +if (reset_enable) { + ret =pri_reset_while_enabled (pdev); +if (ret) + goto out_err; + } + ret =pci_enable_ats (pdev,PAGE_SHIFT );if (ret) goto out_err;
@@ -3481,3 +3520,15 @@ struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)return domain->iommu_domain; }EXPORT_SYMBOL (amd_iommu_get_v2_domain); + +void amd_iommu_enable_device_erratum (struct pci_dev *pdev,u32 erratum) +{ +struct iommu_dev_data *dev_data; + +if (!amd_iommu_v2_supported ()) +return ; + + dev_data =get_dev_data (&pdev->dev); + dev_data->errata |= (1 << erratum); +} +EXPORT_SYMBOL (amd_iommu_enable_device_erratum);
drivers/iommu/amd_iommu_types.h +1 -0
@@ -405,6 +405,7 @@ struct iommu_dev_data { } ats;/* ATS state */ bool pri_tlp;/* PASID TLB required for PPR completions */ +u32 errata;/* Bitmap for errata to apply */ };/*
include/linux/amd-iommu.h +18 -0
@@ -26,6 +26,24 @@extern int amd_iommu_detect (void ); + +/** +* amd_iommu_enable_device_erratum() - Enable erratum workaround for device +* in the IOMMUv2 driver +* @pdev: The PCI device the workaround is necessary for +* @erratum: The erratum workaround to enable +* +* Possible values for the erratum number are for now: +* - AMD_PRI_DEV_ERRATUM_ENABLE_RESET - Reset PRI capability when PRI +* is enabled +* - AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE - Limit number of outstanding PRI +* requests to one +*/ +#define AMD_PRI_DEV_ERRATUM_ENABLE_RESET 0 +#define AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE 1 + +extern void amd_iommu_enable_device_erratum (struct pci_dev *pdev,u32 erratum); +#else static inline int amd_iommu_detect (void ) {return -ENODEV ; }
Signature · 1 line
1.7.5.4
PATCH 16/22 iommu/amd: Add stat counter for IOMMUv2 events
In reply to Joerg RoedelAdd some interesting statistic counters for events when IOMMUv2 is active.
--- drivers/iommu/amd_iommu.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu.c +17 -0
@@ -359,6 +359,11 @@ DECLARE_STATS_COUNTER(domain_flush_single);DECLARE_STATS_COUNTER (domain_flush_all);DECLARE_STATS_COUNTER (alloced_io_mem);DECLARE_STATS_COUNTER (total_map_requests); +DECLARE_STATS_COUNTER (complete_ppr); +DECLARE_STATS_COUNTER (invalidate_iotlb); +DECLARE_STATS_COUNTER (invalidate_iotlb_all); +DECLARE_STATS_COUNTER (pri_requests); +static struct dentry *stats_dir;static struct dentry *de_fflush;
@@ -393,6 +398,10 @@ static void amd_iommu_stats_init(void)amd_iommu_stats_add (&domain_flush_all);amd_iommu_stats_add (&alloced_io_mem);amd_iommu_stats_add (&total_map_requests); +amd_iommu_stats_add (&complete_ppr); +amd_iommu_stats_add (&invalidate_iotlb); +amd_iommu_stats_add (&invalidate_iotlb_all); + amd_iommu_stats_add(&pri_requests); }#endif
@@ -509,6 +518,8 @@ static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u32 head)volatile u64 *raw;int i; +INC_STATS_COUNTER (pri_requests); + raw = (u64 *)(iommu->ppr_log + head);/*
@@ -3356,6 +3367,8 @@ out:static int __amd_iommu_flush_page (struct protection_domain *domain,int pasid,u64 address) { +INC_STATS_COUNTER (invalidate_iotlb); +return __flush_pasid (domain, pasid, address, false); }
@@ -3376,6 +3389,8 @@ EXPORT_SYMBOL(amd_iommu_flush_page);static int __amd_iommu_flush_tlb (struct protection_domain *domain,int pasid) { +INC_STATS_COUNTER (invalidate_iotlb_all); +return __flush_pasid (domain, pasid,CMD_INV_IOMMU_ALL_PAGES_ADDRESS , true); }
@@ -3495,6 +3510,8 @@ int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,struct amd_iommu *iommu;struct iommu_cmd cmd; +INC_STATS_COUNTER (complete_ppr); + dev_data =get_dev_data (&pdev->dev); iommu = amd_iommu_rlookup_table[dev_data->devid];
Signature · 1 line
1.7.5.4
PATCH 17/22 iommu/amd: Add driver stub for AMD IOMMUv2 support
In reply to Joerg RoedelAdd a Kconfig option for the optional driver. Since it is optional it can be compiled as a module and will only be loaded when required by another driver.
--- drivers/iommu/Kconfig | 8 ++++++++ drivers/iommu/Makefile | 1 + drivers/iommu/amd_iommu_v2.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 0 deletions(-) create mode 100644 drivers/iommu/amd_iommu_v2.c
drivers/iommu/Kconfig +8 -0
@@ -60,6 +60,14 @@ config AMD_IOMMU_STATS information to userspace via debugfs. If unsure, say N. +config AMD_IOMMU_V2 + tristate "AMD IOMMU Version 2 driver (EXPERIMENTAL)" + depends on AMD_IOMMU && EXPERIMENTAL + ---help--- + This option enables support for the AMD IOMMUv2 features of the IOMMU + hardware. Select this option if you want to use devices that support + the the PCI PRI and PASID interface. + # Intel IOMMU support config DMAR_TABLE bool
drivers/iommu/Makefile +1 -0
@@ -1,6 +1,7 @@ obj-$(CONFIG_IOMMU_API) += iommu.o obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o msm_iommu_dev.o obj-$(CONFIG_AMD_IOMMU) += amd_iommu.o amd_iommu_init.o +obj-$(CONFIG_AMD_IOMMU_V2) += amd_iommu_v2.o obj-$(CONFIG_DMAR_TABLE) += dmar.o obj-$(CONFIG_INTEL_IOMMU) += iova.o intel-iommu.o obj-$(CONFIG_IRQ_REMAP) += intr_remapping.o
drivers/iommu/amd_iommu_v2.c new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Copyright (C) 2010-2012 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/module.h> + +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR ("Joerg Roedel <[email protected]>" ); + +static int __init amd_iommu_v2_init (void ) +{ +pr_info ("AMD IOMMUv2 driver by Joerg Roedel <[email protected]>" ); + +return 0 ; +} + +static void __exit amd_iommu_v2_exit(void) +{ +} + +module_init (amd_iommu_v2_init); +module_exit (amd_iommu_v2_exit);
Signature · 1 line
1.7.5.4
PATCH 18/22 iommu/amd: Implement device aquisition code for IOMMUv2
In reply to Joerg RoedelThis patch adds the amd_iommu_init_device() and amd_iommu_free_device() functions which make a device and the IOMMU ready for IOMMUv2 usage.
--- drivers/iommu/amd_iommu_v2.c | 213 ++++++++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 23 +++++- 2 files changed, 235 insertions(+), 1 deletions(-)
drivers/iommu/amd_iommu_v2.c +213 -0
@@ -16,20 +16,233 @@* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <linux/amd-iommu.h> +#include <linux/mm_types.h> #include <linux/module.h> +#include <linux/iommu.h> +#include <linux/pci.h> +#include <linux/gfp.h> + +#include "amd_iommu_proto.h" MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Joerg Roedel <[email protected]>" ); +#define MAX_DEVICES 0x10000 +#define PRI_QUEUE_SIZE 512 + +struct pri_queue { +atomic_t inflight; +bool finish; +}; + +struct pasid_state { +struct list_head list;/* For global state-list */ +atomic_t count;/* Reference count */ +struct task_struct *task;/* Task bound to this PASID */ +struct mm_struct *mm;/* mm_struct for the faults */ +struct pri_queue pri[PRI_QUEUE_SIZE ];/* PRI tag states */ +struct device_state *device_state;/* Link to our device_state */ +int pasid;/* PASID index */ +}; + +struct device_state { +atomic_t count; +struct pci_dev *pdev; +struct pasid_state **states; +struct iommu_domain *domain; +int pasid_levels; +int max_pasids; +spinlock_t lock; +}; + +struct device_state **state_table; +static spinlock_t state_lock; + +/* List and lock for all pasid_states */ +static LIST_HEAD (pasid_state_list); +static DEFINE_SPINLOCK (ps_lock); + +static u16 device_id (struct pci_dev *pdev) +{ +u16 devid; + + devid = pdev->bus->number; + devid = (devid <<8 ) | pdev->devfn; + +return devid; +} + +static struct device_state *get_device_state (u16 devid) +{ +struct device_state *dev_state; +unsigned long flags; + +spin_lock_irqsave (&state_lock, flags); + dev_state = state_table[devid]; +if (dev_state !=NULL ) +atomic_inc (&dev_state->count); +spin_unlock_irqrestore (&state_lock, flags); + +return dev_state; +} + +static void free_device_state (struct device_state *dev_state) +{ +iommu_detach_device (dev_state->domain, &dev_state->pdev->dev); +iommu_domain_free (dev_state->domain); +kfree (dev_state); +} + +static void put_device_state (struct device_state *dev_state) +{ +if (atomic_dec_and_test (&dev_state->count)) +free_device_state (dev_state); +} + +int amd_iommu_init_device (struct pci_dev *pdev,int pasids) +{ +struct device_state *dev_state; +unsigned long flags; +int ret, tmp; +u16 devid; + +might_sleep (); + +if (!amd_iommu_v2_supported ()) +return -ENODEV ; + +if (pasids <=0 || pasids & ~PASID_MASK ) +return -EINVAL ; + + devid =device_id (pdev); + + dev_state =kzalloc (sizeof (*dev_state),GFP_KERNEL ); +if (dev_state ==NULL ) +return -ENOMEM ; + +spin_lock_init (&dev_state->lock); + dev_state->pdev = pdev; + + tmp = pasids; +while (tmp & ~0x1ff ) { + dev_state->pasid_levels +=1 ; + tmp >>=9 ; + } + +atomic_set (&dev_state->count,1 ); + dev_state->max_pasids = pasids; + + ret = -ENOMEM ; + dev_state->states = (void *)get_zeroed_page (GFP_KERNEL ); +if (dev_state->states ==NULL ) + goto out_free_dev_state; + + dev_state->domain =iommu_domain_alloc (&pci_bus_type); +if (dev_state->domain ==NULL ) + goto out_free_states; + +amd_iommu_domain_direct_map (dev_state->domain); + + ret =amd_iommu_domain_enable_v2 (dev_state->domain, pasids); +if (ret) + goto out_free_domain; + + ret =iommu_attach_device (dev_state->domain, &pdev->dev); +if (ret !=0 ) + goto out_free_domain; + +spin_lock_irqsave (&state_lock, flags); + +if (state_table[devid] !=NULL ) { +spin_unlock_irqrestore (&state_lock, flags); + ret = -EBUSY ; + goto out_free_domain; + } + + state_table[devid] = dev_state; + +spin_unlock_irqrestore (&state_lock, flags); + +return 0 ; + +out_free_domain: +iommu_domain_free (dev_state->domain); + +out_free_states: +free_page ((unsigned long )dev_state->states); + +out_free_dev_state: +kfree (dev_state); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_init_device); + +void amd_iommu_free_device (struct pci_dev *pdev) +{ +struct device_state *dev_state; +unsigned long flags; +u16 devid; + +if (!amd_iommu_v2_supported ()) +return ; + + devid =device_id (pdev); + +spin_lock_irqsave (&state_lock, flags); + + dev_state = state_table[devid]; +if (dev_state ==NULL ) { +spin_unlock_irqrestore (&state_lock, flags); +return ; + } + + state_table[devid] =NULL ; + +spin_unlock_irqrestore (&state_lock, flags); + +put_device_state (dev_state); +} +EXPORT_SYMBOL (amd_iommu_free_device); +static int __init amd_iommu_v2_init (void ) { +size_t state_table_size; +pr_info ("AMD IOMMUv2 driver by Joerg Roedel <[email protected]>" ); +spin_lock_init (&state_lock); + + state_table_size =MAX_DEVICES *sizeof (struct device_state *); + state_table = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, +get_order (state_table_size)); +if (state_table ==NULL ) +return -ENOMEM ; +return 0 ; } static void __exit amd_iommu_v2_exit(void) { +struct device_state *dev_state; +size_t state_table_size; +int i; + +for (i =0 ; i <MAX_DEVICES ; ++i) { + dev_state =get_device_state (i); + +if (dev_state ==NULL ) +continue ; + +WARN_ON_ONCE (1 ); + +amd_iommu_free_device (dev_state->pdev); +put_device_state (dev_state); + } + + state_table_size =MAX_DEVICES *sizeof (struct device_state *); +free_pages ((unsigned long )state_table,get_order (state_table_size)); }module_init (amd_iommu_v2_init);
include/linux/amd-iommu.h +22 -1
@@ -20,10 +20,12 @@#ifndef _ASM_X86_AMD_IOMMU_H#define _ASM_X86_AMD_IOMMU_H -#include <linux/irqreturn.h> +#include <linux/types.h> #ifdef CONFIG_AMD_IOMMU +struct pci_dev ; +extern int amd_iommu_detect (void );
@@ -33,6 +35,7 @@ extern int amd_iommu_detect(void);* @pdev: The PCI device the workaround is necessary for * @erratum: The erratum workaround to enable * +* The function needs to be called before amd_iommu_init_device(). * Possible values for the erratum number are for now: * - AMD_PRI_DEV_ERRATUM_ENABLE_RESET - Reset PRI capability when PRI * is enabled
@@ -44,6 +47,24 @@ extern int amd_iommu_detect(void);extern void amd_iommu_enable_device_erratum (struct pci_dev *pdev,u32 erratum); +/** +* amd_iommu_init_device() - Init device for use with IOMMUv2 driver +* @pdev: The PCI device to initialize +* @pasids: Number of PASIDs to support for this device +* +* This function does all setup for the device pdev so that it can be +* used with IOMMUv2. +* Returns 0 on success or negative value on error. +*/ +extern int amd_iommu_init_device (struct pci_dev *pdev,int pasids); + +/** +* amd_iommu_free_device() - Free all IOMMUv2 related device resources +* and disable IOMMUv2 usage for this device +* @pdev: The PCI device to disable IOMMUv2 usage for' +*/ +extern void amd_iommu_free_device (struct pci_dev *pdev); +#else static inline int amd_iommu_detect (void ) {return -ENODEV ; }
Signature · 1 line
1.7.5.4
PATCH 19/22 iommu/amd: Add routines to bind/unbind a pasid
In reply to Joerg RoedelThis patch adds routines to bind a specific process address-space to a given PASID.
--- drivers/iommu/amd_iommu_v2.c | 301 ++++++++++++++++++++++++++++++++++++++++++ include/linux/amd-iommu.h | 26 ++++ 2 files changed, 327 insertions(+), 0 deletions(-)
drivers/iommu/amd_iommu_v2.c +301 -0
@@ -19,6 +19,7 @@#include <linux/amd-iommu.h> #include <linux/mm_types.h> #include <linux/module.h> +#include <linux/sched.h> #include <linux/iommu.h> #include <linux/pci.h> #include <linux/gfp.h>
@@ -63,6 +64,9 @@ static spinlock_t state_lock;static LIST_HEAD (pasid_state_list);static DEFINE_SPINLOCK (ps_lock); +static void free_pasid_states (struct device_state *dev_state); +static void unbind_pasid (struct device_state *dev_state,int pasid); +static u16 device_id (struct pci_dev *pdev) {u16 devid;
@@ -89,8 +93,16 @@ static struct device_state *get_device_state(u16 devid)static void free_device_state (struct device_state *dev_state) { +/* +* First detach device from domain - No more PRI requests will arrive +* from that device after it is unbound from the IOMMUv2 domain. +*/ iommu_detach_device (dev_state->domain, &dev_state->pdev->dev); + +/* Everything is down now, free the IOMMUv2 domain */ iommu_domain_free (dev_state->domain); + +/* Finally get rid of the device-state */ kfree (dev_state); }
@@ -100,6 +112,292 @@ static void put_device_state(struct device_state *dev_state)free_device_state (dev_state); } +static void link_pasid_state (struct pasid_state *pasid_state) +{ +spin_lock (&ps_lock); +list_add_tail (&pasid_state->list, &pasid_state_list); +spin_unlock (&ps_lock); +} + +static void __unlink_pasid_state (struct pasid_state *pasid_state) +{ +list_del (&pasid_state->list); +} + +static void unlink_pasid_state (struct pasid_state *pasid_state) +{ +spin_lock (&ps_lock); +__unlink_pasid_state (pasid_state); +spin_unlock (&ps_lock); +} + +/* Must be called under dev_state->lock */ +static struct pasid_state **__get_pasid_state_ptr (struct device_state *dev_state, +int pasid,bool alloc) +{ +struct pasid_state **root, **ptr; +int level, index; + + level = dev_state->pasid_levels; + root = dev_state->states; + +while (true) { + + index = (pasid >> (9 * level)) &0x1ff ; + ptr = &root[index]; + +if (level ==0 ) +break ; + +if (*ptr ==NULL ) { +if (!alloc) +return NULL ; + + *ptr = (void *)get_zeroed_page (GFP_ATOMIC ); +if (*ptr ==NULL ) +return NULL ; + } + + root = (struct pasid_state **)*ptr; + level -=1 ; + } + +return ptr; +} + +static int set_pasid_state (struct device_state *dev_state, +struct pasid_state *pasid_state, +int pasid) +{ +struct pasid_state **ptr; +unsigned long flags; +int ret; + +spin_lock_irqsave (&dev_state->lock, flags); + ptr =__get_pasid_state_ptr (dev_state, pasid, true); + + ret = -ENOMEM ; +if (ptr ==NULL ) + goto out_unlock; + + ret = -ENOMEM ; +if (*ptr !=NULL ) + goto out_unlock; + + *ptr = pasid_state; + + ret =0 ; + +out_unlock: +spin_unlock_irqrestore (&dev_state->lock, flags); + +return ret; +} + +static void clear_pasid_state (struct device_state *dev_state,int pasid) +{ +struct pasid_state **ptr; +unsigned long flags; + +spin_lock_irqsave (&dev_state->lock, flags); + ptr =__get_pasid_state_ptr (dev_state, pasid, true); + +if (ptr ==NULL ) + goto out_unlock; + + *ptr =NULL ; + +out_unlock: +spin_unlock_irqrestore (&dev_state->lock, flags); +} + +static struct pasid_state *get_pasid_state (struct device_state *dev_state, +int pasid) +{ +struct pasid_state **ptr, *ret =NULL ; +unsigned long flags; + +spin_lock_irqsave (&dev_state->lock, flags); + ptr =__get_pasid_state_ptr (dev_state, pasid, false); + +if (ptr ==NULL ) + goto out_unlock; + + ret = *ptr; +if (ret) +atomic_inc (&ret->count); + +out_unlock: +spin_unlock_irqrestore (&dev_state->lock, flags); + +return ret; +} + +static void free_pasid_state (struct pasid_state *pasid_state) +{ +kfree (pasid_state); +} + +static void put_pasid_state (struct pasid_state *pasid_state) +{ +if (atomic_dec_and_test (&pasid_state->count)) { +put_device_state (pasid_state->device_state); +free_pasid_state (pasid_state); + } +} + +static void unbind_pasid (struct device_state *dev_state,int pasid) +{ +struct pasid_state *pasid_state; + + pasid_state =get_pasid_state (dev_state, pasid); +if (pasid_state ==NULL ) +return ; + +unlink_pasid_state (pasid_state); + +amd_iommu_domain_clear_gcr3 (dev_state->domain, pasid); +clear_pasid_state (dev_state, pasid); + +put_pasid_state (pasid_state);/* Reference taken in this function */ +put_pasid_state (pasid_state);/* Reference taken in bind() function */ +} + +static void free_pasid_states_level1 (struct pasid_state **tbl) +{ +int i; + +for (i =0 ; i <512 ; ++i) { +if (tbl[i] ==NULL ) +continue ; + +free_page ((unsigned long )tbl[i]); + } +} + +static void free_pasid_states_level2 (struct pasid_state **tbl) +{ +struct pasid_state **ptr; +int i; + +for (i =0 ; i <512 ; ++i) { +if (tbl[i] ==NULL ) +continue ; + + ptr = (struct pasid_state **)tbl[i]; +free_pasid_states_level1 (ptr); + } +} + +static void free_pasid_states (struct device_state *dev_state) +{ +struct pasid_state *pasid_state; +int i; + +for (i =0 ; i < dev_state->max_pasids; ++i) { + pasid_state =get_pasid_state (dev_state, i); +if (pasid_state ==NULL ) +continue ; + +unbind_pasid (dev_state, i); +put_pasid_state (pasid_state); + } + +if (dev_state->pasid_levels ==2 ) +free_pasid_states_level2 (dev_state->states); +else if (dev_state->pasid_levels ==1 ) +free_pasid_states_level1 (dev_state->states); +else if (dev_state->pasid_levels !=0 ) +BUG (); + +free_page ((unsigned long )dev_state->states); +} + +int amd_iommu_bind_pasid (struct pci_dev *pdev,int pasid, +struct task_struct *task) +{ +struct pasid_state *pasid_state; +struct device_state *dev_state; +u16 devid; +int ret; + +might_sleep (); + +if (!amd_iommu_v2_supported ()) +return -ENODEV ; + + devid =device_id (pdev); + dev_state =get_device_state (devid); + +if (dev_state ==NULL ) +return -EINVAL ; + + ret = -EINVAL ; +if (pasid <0 || pasid >= dev_state->max_pasids) + goto out; + + ret = -ENOMEM ; + pasid_state =kzalloc (sizeof (*pasid_state),GFP_KERNEL ); +if (pasid_state ==NULL ) + goto out; + +atomic_set (&pasid_state->count,1 ); + pasid_state->task = task; + pasid_state->mm = task->mm; + pasid_state->device_state = dev_state; + pasid_state->pasid = pasid; + + ret =set_pasid_state (dev_state, pasid_state, pasid); +if (ret) + goto out_free; + + ret =amd_iommu_domain_set_gcr3 (dev_state->domain, pasid, +__pa (pasid_state->mm->pgd)); +if (ret) + goto out_clear_state; + +link_pasid_state (pasid_state); + +return 0 ; + +out_clear_state: +clear_pasid_state (dev_state, pasid); + +out_free: +put_pasid_state (pasid_state); + +out: +put_device_state (dev_state); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_bind_pasid); + +void amd_iommu_unbind_pasid (struct pci_dev *pdev,int pasid) +{ +struct device_state *dev_state; +u16 devid; + +might_sleep (); + +if (!amd_iommu_v2_supported ()) +return ; + + devid =device_id (pdev); + dev_state =get_device_state (devid); +if (dev_state ==NULL ) +return ; + +if (pasid <0 || pasid >= dev_state->max_pasids) + goto out; + +unbind_pasid (dev_state, pasid); + +out: +put_device_state (dev_state); +} +EXPORT_SYMBOL (amd_iommu_unbind_pasid); +int amd_iommu_init_device (struct pci_dev *pdev,int pasids) {struct device_state *dev_state;
@@ -202,6 +500,9 @@ void amd_iommu_free_device(struct pci_dev *pdev)spin_unlock_irqrestore (&state_lock, flags); +/* Get rid of any remaining pasid states */ +free_pasid_states (dev_state); +put_device_state (dev_state); }EXPORT_SYMBOL (amd_iommu_free_device);
include/linux/amd-iommu.h +26 -0
@@ -24,9 +24,13 @@#ifdef CONFIG_AMD_IOMMU +struct task_struct ;struct pci_dev ;extern int amd_iommu_detect (void ); +extern int amd_iommu_bind_pasid (struct pci_dev *pdev,int pasid, +struct task_struct *task); +extern void amd_iommu_unbind_pasid (struct pci_dev *pdev,int pasid);/**
@@ -65,6 +69,28 @@ extern int amd_iommu_init_device(struct pci_dev *pdev, int pasids);*/ extern void amd_iommu_free_device (struct pci_dev *pdev); +/** +* amd_iommu_bind_pasid() - Bind a given task to a PASID on a device +* @pdev: The PCI device to bind the task to +* @pasid: The PASID on the device the task should be bound to +* @task: the task to bind +* +* The function returns 0 on success or a negative value on error. +*/ +extern int amd_iommu_bind_pasid (struct pci_dev *pdev,int pasid, +struct task_struct *task); + +/** +* amd_iommu_unbind_pasid() - Unbind a PASID from its task on +* a device +* @pdev: The device of the PASID +* @pasid: The PASID to unbind +* +* When this function returns the device is no longer using the PASID +* and the PASID is no longer bound to its task. +*/ +extern void amd_iommu_unbind_pasid (struct pci_dev *pdev,int pasid); +#else static inline int amd_iommu_detect (void ) {return -ENODEV ; }
Signature · 1 line
1.7.5.4
PATCH 20/22 iommu/amd: Implement IO page-fault handler
In reply to Joerg RoedelRegister the notifier for PPR faults and handle them as necessary.
--- drivers/iommu/amd_iommu_v2.c | 200 ++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 194 insertions(+), 6 deletions(-)
drivers/iommu/amd_iommu_v2.c +194 -6
@@ -21,9 +21,11 @@#include <linux/module.h> #include <linux/sched.h> #include <linux/iommu.h> +#include <linux/wait.h> #include <linux/pci.h> #include <linux/gfp.h> +#include "amd_iommu_types.h" #include "amd_iommu_proto.h" MODULE_LICENSE ("GPL v2" );
@@ -35,6 +37,7 @@ MODULE_AUTHOR("Joerg Roedel <[email protected]>");struct pri_queue {atomic_t inflight;bool finish; +int status; };struct pasid_state {
@@ -45,6 +48,8 @@ struct pasid_state {struct pri_queue pri[PRI_QUEUE_SIZE ];/* PRI tag states */ struct device_state *device_state;/* Link to our device_state */ int pasid;/* PASID index */ +spinlock_t lock;/* Protect pri_queues */ +wait_queue_head_t wq;/* To wait for count == 0 */ };struct device_state {
@@ -55,6 +60,20 @@ struct device_state {int pasid_levels;int max_pasids;spinlock_t lock; +wait_queue_head_t wq; +}; + +struct fault { +struct work_struct work; +struct device_state *dev_state; +struct pasid_state *state; +struct mm_struct *mm; +u64 address; +u16 devid; +u16 pasid; +u16 tag; +u16 finish; +u16 flags; };struct device_state **state_table;
@@ -64,6 +83,8 @@ static spinlock_t state_lock;static LIST_HEAD (pasid_state_list);static DEFINE_SPINLOCK (ps_lock); +static struct workqueue_struct *iommu_wq; +static void free_pasid_states (struct device_state *dev_state);static void unbind_pasid (struct device_state *dev_state,int pasid);
@@ -109,9 +130,20 @@ static void free_device_state(struct device_state *dev_state)static void put_device_state (struct device_state *dev_state) {if (atomic_dec_and_test (&dev_state->count)) -free_device_state (dev_state); +wake_up (&dev_state->wq); } +static void put_device_state_wait (struct device_state *dev_state) +{ +DEFINE_WAIT (wait); + +prepare_to_wait (&dev_state->wq, &wait,TASK_UNINTERRUPTIBLE ); +if (!atomic_dec_and_test (&dev_state->count)) +schedule (); +finish_wait (&dev_state->wq, &wait); + +free_device_state (dev_state); +}static void link_pasid_state (struct pasid_state *pasid_state) {spin_lock (&ps_lock);
@@ -242,10 +274,25 @@ static void put_pasid_state(struct pasid_state *pasid_state) {if (atomic_dec_and_test (&pasid_state->count)) {put_device_state (pasid_state->device_state); -free_pasid_state (pasid_state); +wake_up (&pasid_state->wq); } } +static void put_pasid_state_wait (struct pasid_state *pasid_state) +{ +DEFINE_WAIT (wait); + +prepare_to_wait (&pasid_state->wq, &wait,TASK_UNINTERRUPTIBLE ); + +if (atomic_dec_and_test (&pasid_state->count)) +put_device_state (pasid_state->device_state); +else +schedule (); + +finish_wait (&pasid_state->wq, &wait); +free_pasid_state (pasid_state); +} +static void unbind_pasid (struct device_state *dev_state,int pasid) {struct pasid_state *pasid_state;
@@ -260,7 +307,7 @@ static void unbind_pasid(struct device_state *dev_state, int pasid)clear_pasid_state (dev_state, pasid);put_pasid_state (pasid_state);/* Reference taken in this function */ -put_pasid_state (pasid_state);/* Reference taken in bind() function */ +put_pasid_state_wait (pasid_state);/* Reference from bind() function */ }static void free_pasid_states_level1 (struct pasid_state **tbl)
@@ -299,8 +346,8 @@ static void free_pasid_states(struct device_state *dev_state)if (pasid_state ==NULL )continue ; -unbind_pasid (dev_state, i);put_pasid_state (pasid_state); +unbind_pasid (dev_state, i); }if (dev_state->pasid_levels ==2 )
@@ -313,6 +360,120 @@ static void free_pasid_states(struct device_state *dev_state)free_page ((unsigned long )dev_state->states); } +static void set_pri_tag_status (struct pasid_state *pasid_state, +u16 tag,int status) +{ +unsigned long flags; + +spin_lock_irqsave (&pasid_state->lock, flags); + pasid_state->pri[tag].status = status; +spin_unlock_irqrestore (&pasid_state->lock, flags); +} + +static void finish_pri_tag (struct device_state *dev_state, +struct pasid_state *pasid_state, +u16 tag) +{ +unsigned long flags; + +spin_lock_irqsave (&pasid_state->lock, flags); +if (atomic_dec_and_test (&pasid_state->pri[tag].inflight) && + pasid_state->pri[tag].finish) { +amd_iommu_complete_ppr (dev_state->pdev, pasid_state->pasid, + pasid_state->pri[tag].status, tag); + pasid_state->pri[tag].finish = false; + pasid_state->pri[tag].status =PPR_SUCCESS ; + } +spin_unlock_irqrestore (&pasid_state->lock, flags); +} + +static void do_fault (struct work_struct *work) +{ + struct fault *fault = container_of(work, struct fault, work); +int npages, write; +struct page *page; + + write = !!(fault->flags &PPR_FAULT_WRITE ); + + npages =get_user_pages (fault->state->task, fault->state->mm, + fault->address,1 , write,0 , &page,NULL ); + +if (npages ==1 ) +put_page (page); +else +set_pri_tag_status (fault->state, fault->tag,PPR_INVALID ); + +finish_pri_tag (fault->dev_state, fault->state, fault->tag); + +put_pasid_state (fault->state); + +kfree (fault); +} + +static int ppr_notifier (struct notifier_block *nb,unsigned long e,void *data) +{ +struct amd_iommu_fault *iommu_fault; +struct pasid_state *pasid_state; +struct device_state *dev_state; +unsigned long flags; +struct fault *fault; +bool finish; +u16 tag; +int ret; + + iommu_fault = data; + tag = iommu_fault->tag &0x1ff ; + finish = (iommu_fault->tag >>9 ) &1 ; + + ret =NOTIFY_DONE ; + dev_state =get_device_state (iommu_fault->device_id); +if (dev_state ==NULL ) + goto out; + + pasid_state =get_pasid_state (dev_state, iommu_fault->pasid); +if (pasid_state ==NULL ) { +/* We know the device but not the PASID -> send INVALID */ +amd_iommu_complete_ppr (dev_state->pdev, iommu_fault->pasid, +PPR_INVALID , tag); + goto out_drop_state; + } + +spin_lock_irqsave (&pasid_state->lock, flags); +atomic_inc (&pasid_state->pri[tag].inflight); +if (finish) + pasid_state->pri[tag].finish = true; +spin_unlock_irqrestore (&pasid_state->lock, flags); + + fault =kzalloc (sizeof (*fault),GFP_ATOMIC ); +if (fault ==NULL ) { +/* We are OOM - send success and let the device re-fault */ +finish_pri_tag (dev_state, pasid_state, tag); + goto out_drop_state; + } + + fault->dev_state = dev_state; + fault->address = iommu_fault->address; + fault->state = pasid_state; + fault->tag = tag; + fault->finish = finish; + fault->flags = iommu_fault->flags; +INIT_WORK (&fault->work, do_fault); + +queue_work (iommu_wq, &fault->work); + + ret =NOTIFY_OK ; + +out_drop_state: +put_device_state (dev_state); + +out: +return ret; +} + +static struct notifier_block ppr_nb = { + .notifier_call = ppr_notifier, +}; +int amd_iommu_bind_pasid (struct pci_dev *pdev,int pasid,struct task_struct *task) {
@@ -342,6 +503,7 @@ int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid, goto out;atomic_set (&pasid_state->count,1 ); +init_waitqueue_head (&pasid_state->wq); pasid_state->task = task; pasid_state->mm = task->mm; pasid_state->device_state = dev_state;
@@ -420,6 +582,7 @@ int amd_iommu_init_device(struct pci_dev *pdev, int pasids)return -ENOMEM ;spin_lock_init (&dev_state->lock); +init_waitqueue_head (&dev_state->wq); dev_state->pdev = pdev; tmp = pasids;
@@ -503,13 +666,14 @@ void amd_iommu_free_device(struct pci_dev *pdev)/* Get rid of any remaining pasid states */ free_pasid_states (dev_state); -put_device_state (dev_state); +put_device_state_wait (dev_state); }EXPORT_SYMBOL (amd_iommu_free_device);static int __init amd_iommu_v2_init (void ) {size_t state_table_size; +int ret;pr_info ("AMD IOMMUv2 driver by Joerg Roedel <[email protected]>" );
@@ -521,7 +685,21 @@ static int __init amd_iommu_v2_init(void)if (state_table ==NULL )return -ENOMEM ; + ret = -ENOMEM ; + iommu_wq =create_workqueue ("amd_iommu_v2" ); +if (iommu_wq ==NULL ) { + ret = -ENOMEM ; + goto out_free; + } + +amd_iommu_register_ppr_notifier (&ppr_nb); +return 0 ; + +out_free: +free_pages ((unsigned long )state_table,get_order (state_table_size)); + +return ret; } static void __exit amd_iommu_v2_exit(void)
@@ -530,6 +708,14 @@ static void __exit amd_iommu_v2_exit(void)size_t state_table_size;int i; +amd_iommu_unregister_ppr_notifier (&ppr_nb); + +flush_workqueue (iommu_wq); + +/* +* The loop below might call flush_workqueue(), so call +* destroy_workqueue() after it +*/ for (i =0 ; i <MAX_DEVICES ; ++i) { dev_state =get_device_state (i);
@@ -538,10 +724,12 @@ static void __exit amd_iommu_v2_exit(void)WARN_ON_ONCE (1 ); -amd_iommu_free_device (dev_state->pdev);put_device_state (dev_state); +amd_iommu_free_device (dev_state->pdev); } +destroy_workqueue (iommu_wq); + state_table_size =MAX_DEVICES *sizeof (struct device_state *);free_pages ((unsigned long )state_table,get_order (state_table_size)); }
Signature · 1 line
1.7.5.4
Re: [PATCH 20/22] iommu/amd: Implement IO page-fault handler
Quoted 175 lines · Joerg Roedel
Register the notifier for PPR faults and handle them as necessary.
Signed-off-by Joerg Roedel <[email protected]>--- drivers/iommu/amd_iommu_v2.c | 200 ++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 194 insertions(+), 6 deletions(-)drivers/iommu/amd_iommu_v2.c +83
-4@@ -21,9 +21,11 @@#include <linux/module.h> #include <linux/sched.h> #include <linux/iommu.h> +#include <linux/wait.h> #include <linux/pci.h> #include <linux/gfp.h> +#include "amd_iommu_types.h" #include "amd_iommu_proto.h" MODULE_LICENSE ("GPL v2" );@@ -35,6 +37,7 @@ MODULE_AUTHOR("Joerg Roedel <[email protected]>");struct pri_queue {atomic_t inflight;bool finish; +int status; };struct pasid_state {@@ -45,6 +48,8 @@ struct pasid_state {struct pri_queue pri[PRI_QUEUE_SIZE ];/* PRI tag states */ struct device_state *device_state;/* Link to our device_state */ int pasid;/* PASID index */ +spinlock_t lock;/* Protect pri_queues */ +wait_queue_head_t wq;/* To wait for count == 0 */ };struct device_state {@@ -55,6 +60,20 @@ struct device_state {int pasid_levels;int max_pasids;spinlock_t lock; +wait_queue_head_t wq; +}; + +struct fault { +struct work_struct work; +struct device_state *dev_state; +struct pasid_state *state; +struct mm_struct *mm; +u64 address; +u16 devid; +u16 pasid; +u16 tag; +u16 finish; +u16 flags; };struct device_state **state_table;@@ -64,6 +83,8 @@ static spinlock_t state_lock;static LIST_HEAD (pasid_state_list);static DEFINE_SPINLOCK (ps_lock); +static struct workqueue_struct *iommu_wq; +static void free_pasid_states (struct device_state *dev_state);static void unbind_pasid (struct device_state *dev_state,int pasid);@@ -109,9 +130,20 @@ static void free_device_state(struct device_state *dev_state)static void put_device_state (struct device_state *dev_state) {if (atomic_dec_and_test (&dev_state->count)) -free_device_state (dev_state); +wake_up (&dev_state->wq); } +static void put_device_state_wait (struct device_state *dev_state) +{ +DEFINE_WAIT (wait); + +prepare_to_wait (&dev_state->wq, &wait,TASK_UNINTERRUPTIBLE ); +if (!atomic_dec_and_test (&dev_state->count)) +schedule (); +finish_wait (&dev_state->wq, &wait); + +free_device_state (dev_state); +}static void link_pasid_state (struct pasid_state *pasid_state) {spin_lock (&ps_lock);@@ -242,10 +274,25 @@ static void put_pasid_state(struct pasid_state *pasid_state) {if (atomic_dec_and_test (&pasid_state->count)) {put_device_state (pasid_state->device_state); -free_pasid_state (pasid_state); +wake_up (&pasid_state->wq); } } +static void put_pasid_state_wait (struct pasid_state *pasid_state) +{ +DEFINE_WAIT (wait); + +prepare_to_wait (&pasid_state->wq, &wait,TASK_UNINTERRUPTIBLE ); + +if (atomic_dec_and_test (&pasid_state->count)) +put_device_state (pasid_state->device_state); +else +schedule (); + +finish_wait (&pasid_state->wq, &wait); +free_pasid_state (pasid_state); +} +static void unbind_pasid (struct device_state *dev_state,int pasid) {struct pasid_state *pasid_state;@@ -260,7 +307,7 @@ static void unbind_pasid(struct device_state *dev_state, int pasid)clear_pasid_state (dev_state, pasid);put_pasid_state (pasid_state);/* Reference taken in this function */ -put_pasid_state (pasid_state);/* Reference taken in bind() function */ +put_pasid_state_wait (pasid_state);/* Reference from bind() function */ }static void free_pasid_states_level1 (struct pasid_state **tbl)@@ -299,8 +346,8 @@ static void free_pasid_states(struct device_state *dev_state)if (pasid_state ==NULL )continue ; -unbind_pasid (dev_state, i);put_pasid_state (pasid_state); +unbind_pasid (dev_state, i); }if (dev_state->pasid_levels ==2 )@@ -313,6 +360,120 @@ static void free_pasid_states(struct device_state *dev_state)free_page ((unsigned long )dev_state->states); } +static void set_pri_tag_status (struct pasid_state *pasid_state, +u16 tag,int status) +{ +unsigned long flags; + +spin_lock_irqsave (&pasid_state->lock, flags); + pasid_state->pri[tag].status = status; +spin_unlock_irqrestore (&pasid_state->lock, flags); +} + +static void finish_pri_tag (struct device_state *dev_state, +struct pasid_state *pasid_state, +u16 tag) +{ +unsigned long flags; + +spin_lock_irqsave (&pasid_state->lock, flags); +if (atomic_dec_and_test (&pasid_state->pri[tag].inflight) && + pasid_state->pri[tag].finish) { +amd_iommu_complete_ppr (dev_state->pdev, pasid_state->pasid, + pasid_state->pri[tag].status, tag); + pasid_state->pri[tag].finish = false; + pasid_state->pri[tag].status =PPR_SUCCESS ; + } +spin_unlock_irqrestore (&pasid_state->lock, flags); +} + +static void do_fault (struct work_struct *work) +{ + struct fault *fault = container_of(work, struct fault, work); +int npages, write; +struct page *page;
drivers/iommu/amd_iommu_v2.c static void free_pasid_states(struct device_state *dev_state) · line 404
Quoted 10 lines
drivers/iommu/amd_iommu_v2.c +10
-0+ + write = !!(fault->flags &PPR_FAULT_WRITE ); + + npages =get_user_pages (fault->state->task, fault->state->mm, + fault->address,1 , write,0 , &page,NULL ); + +if (npages ==1 ) +put_page (page); +else +set_pri_tag_status (fault->state, fault->tag,PPR_INVALID );
I might be missing something on how mm work in linux but can't the page in a vma change anytime (like being migrated to a node or being evicted ...) I guess my question is when a fault happen is it because the task page table have an invalid entry ?
I was under the impression that their was a page table associated with each iommu client. Thus that the task page table was never directly use. But i haven't carrefully read all the patches.
Cheers,
Jerome
Quoted 167 lines
drivers/iommu/amd_iommu_v2.c +101
-2+ +finish_pri_tag (fault->dev_state, fault->state, fault->tag); + +put_pasid_state (fault->state); + +kfree (fault); +} + +static int ppr_notifier (struct notifier_block *nb,unsigned long e,void *data) +{ +struct amd_iommu_fault *iommu_fault; +struct pasid_state *pasid_state; +struct device_state *dev_state; +unsigned long flags; +struct fault *fault; +bool finish; +u16 tag; +int ret; + + iommu_fault = data; + tag = iommu_fault->tag &0x1ff ; + finish = (iommu_fault->tag >>9 ) &1 ; + + ret =NOTIFY_DONE ; + dev_state =get_device_state (iommu_fault->device_id); +if (dev_state ==NULL ) + goto out; + + pasid_state =get_pasid_state (dev_state, iommu_fault->pasid); +if (pasid_state ==NULL ) { +/* We know the device but not the PASID -> send INVALID */ +amd_iommu_complete_ppr (dev_state->pdev, iommu_fault->pasid, +PPR_INVALID , tag); + goto out_drop_state; + } + +spin_lock_irqsave (&pasid_state->lock, flags); +atomic_inc (&pasid_state->pri[tag].inflight); +if (finish) + pasid_state->pri[tag].finish = true; +spin_unlock_irqrestore (&pasid_state->lock, flags); + + fault =kzalloc (sizeof (*fault),GFP_ATOMIC ); +if (fault ==NULL ) { +/* We are OOM - send success and let the device re-fault */ +finish_pri_tag (dev_state, pasid_state, tag); + goto out_drop_state; + } + + fault->dev_state = dev_state; + fault->address = iommu_fault->address; + fault->state = pasid_state; + fault->tag = tag; + fault->finish = finish; + fault->flags = iommu_fault->flags; +INIT_WORK (&fault->work, do_fault); + +queue_work (iommu_wq, &fault->work); + + ret =NOTIFY_OK ; + +out_drop_state: +put_device_state (dev_state); + +out: +return ret; +} + +static struct notifier_block ppr_nb = { + .notifier_call = ppr_notifier, +}; +int amd_iommu_bind_pasid (struct pci_dev *pdev,int pasid,struct task_struct *task) {@@ -342,6 +503,7 @@ int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid, goto out;atomic_set (&pasid_state->count,1 ); +init_waitqueue_head (&pasid_state->wq); pasid_state->task = task; pasid_state->mm = task->mm; pasid_state->device_state = dev_state;@@ -420,6 +582,7 @@ int amd_iommu_init_device(struct pci_dev *pdev, int pasids)return -ENOMEM ;spin_lock_init (&dev_state->lock); +init_waitqueue_head (&dev_state->wq); dev_state->pdev = pdev; tmp = pasids;@@ -503,13 +666,14 @@ void amd_iommu_free_device(struct pci_dev *pdev)/* Get rid of any remaining pasid states */ free_pasid_states (dev_state); -put_device_state (dev_state); +put_device_state_wait (dev_state); }EXPORT_SYMBOL (amd_iommu_free_device);static int __init amd_iommu_v2_init (void ) {size_t state_table_size; +int ret;pr_info ("AMD IOMMUv2 driver by Joerg Roedel <[email protected]>" );@@ -521,7 +685,21 @@ static int __init amd_iommu_v2_init(void)if (state_table ==NULL )return -ENOMEM ; + ret = -ENOMEM ; + iommu_wq =create_workqueue ("amd_iommu_v2" ); +if (iommu_wq ==NULL ) { + ret = -ENOMEM ; + goto out_free; + } + +amd_iommu_register_ppr_notifier (&ppr_nb); +return 0 ; + +out_free: +free_pages ((unsigned long )state_table,get_order (state_table_size)); + +return ret; } static void __exit amd_iommu_v2_exit(void)@@ -530,6 +708,14 @@ static void __exit amd_iommu_v2_exit(void)size_t state_table_size;int i; +amd_iommu_unregister_ppr_notifier (&ppr_nb); + +flush_workqueue (iommu_wq); + +/* +* The loop below might call flush_workqueue(), so call +* destroy_workqueue() after it +*/ for (i =0 ; i <MAX_DEVICES ; ++i) { dev_state =get_device_state (i);@@ -538,10 +724,12 @@ static void __exit amd_iommu_v2_exit(void)WARN_ON_ONCE (1 ); -amd_iommu_free_device (dev_state->pdev);put_device_state (dev_state); +amd_iommu_free_device (dev_state->pdev); } +destroy_workqueue (iommu_wq); + state_table_size =MAX_DEVICES *sizeof (struct device_state *);free_pages ((unsigned long )state_table,get_order (state_table_size)); }Signature · 8 lines
1.7.5.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Joerg Roedel
maintainerRe: [PATCH 20/22] iommu/amd: Implement IO page-fault handler
Quoted 13 lines · Jerome Glisse
drivers/iommu/amd_iommu_v2.c static void free_pasid_states(struct device_state *dev_state) · line 404
Quoted 7 lines · Joerg Roedel
drivers/iommu/amd_iommu_v2.c +7
-0+ npages =get_user_pages (fault->state->task, fault->state->mm, + fault->address,1 , write,0 , &page,NULL ); + +if (npages ==1 ) +put_page (page); +else +set_pri_tag_status (fault->state, fault->tag,PPR_INVALID );I might be missing something on how mm work in linux but can't the page in a vma change anytime (like being migrated to a node or being evicted ...) I guess my question is when a fault happen is it because the task page table have an invalid entry ?
Yes, but this is not a problem. When the page-fault handler signals completion of the page-fault back to the device it will start to re-walk the page-table. If the page-walker detects that the page is not there it will just signal the fault again. MMU-Notifiers take care of the TLB management.
Quoted 3 lines
I was under the impression that their was a page table associated with each iommu client. Thus that the task page table was never directly use. But i haven't carrefully read all the patches.
The IOMMUv2 can handle x86 long-mode page-tables (including accessed/dirty bits). So it is easy to just re-use the task page-tables in the IOMMUv2.
Regards,
Joerg
PATCH 21/22 iommu/amd: Implement notifiers for IOMMUv2
In reply to Joerg RoedelSince pages are not pinned anymore we need notifications when the VMM changes the page-tables. Use mmu_notifiers for that.
Also use the task_exit notifier from the profiling subsystem to shutdown all contexts related to this task.
--- drivers/iommu/Kconfig | 3 +- drivers/iommu/amd_iommu_v2.c | 178 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 171 insertions(+), 10 deletions(-)
drivers/iommu/Kconfig +2 -1
@@ -62,7 +62,8 @@ config AMD_IOMMU_STATS config AMD_IOMMU_V2 tristate "AMD IOMMU Version 2 driver (EXPERIMENTAL)" - depends on AMD_IOMMU && EXPERIMENTAL + depends on AMD_IOMMU && PROFILING && EXPERIMENTAL + select MMU_NOTIFIER ---help--- This option enables support for the AMD IOMMUv2 features of the IOMMU hardware. Select this option if you want to use devices that support
drivers/iommu/amd_iommu_v2.c +169 -9
260 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH 22/22 iommu/amd: Add invalid_ppr callback
In reply to Joerg RoedelThis callback can be used to change the PRI response code sent to a device when a PPR fault fails.
--- drivers/iommu/amd_iommu_v2.c | 57 ++++++++++++++++++++++++++++++++++++++++- include/linux/amd-iommu.h | 34 ++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 5 deletions(-)
drivers/iommu/amd_iommu_v2.c +55 -2
@@ -62,6 +62,7 @@ struct device_state {struct iommu_domain *domain;int pasid_levels;int max_pasids; +amd_iommu_invalid_ppr_cb inv_ppr_cb;spinlock_t lock;wait_queue_head_t wq; };
@@ -504,10 +505,31 @@ static void do_fault(struct work_struct *work) npages =get_user_pages (fault->state->task, fault->state->mm, fault->address,1 , write,0 , &page,NULL ); -if (npages ==1 ) +if (npages ==1 ) {put_page (page); -else + }else if (fault->dev_state->inv_ppr_cb) { +int status; + + status = fault->dev_state->inv_ppr_cb (fault->dev_state->pdev, + fault->pasid, + fault->address, + fault->flags); +switch (status) { +case AMD_IOMMU_INV_PRI_RSP_SUCCESS : +set_pri_tag_status (fault->state, fault->tag,PPR_SUCCESS ); +break ; +case AMD_IOMMU_INV_PRI_RSP_INVALID : +set_pri_tag_status (fault->state, fault->tag,PPR_INVALID ); +break ; +case AMD_IOMMU_INV_PRI_RSP_FAIL : +set_pri_tag_status (fault->state, fault->tag,PPR_FAILURE ); +break ; +default : +BUG (); + } + }else {set_pri_tag_status (fault->state, fault->tag,PPR_INVALID ); + }finish_pri_tag (fault->dev_state, fault->state, fault->tag);
@@ -820,6 +842,37 @@ void amd_iommu_free_device(struct pci_dev *pdev) }EXPORT_SYMBOL (amd_iommu_free_device); +int amd_iommu_set_invalid_ppr_cb (struct pci_dev *pdev, +amd_iommu_invalid_ppr_cb cb) +{ +struct device_state *dev_state; +unsigned long flags; +u16 devid; +int ret; + +if (!amd_iommu_v2_supported ()) +return -ENODEV ; + + devid =device_id (pdev); + +spin_lock_irqsave (&state_lock, flags); + + ret = -EINVAL ; + dev_state = state_table[devid]; +if (dev_state ==NULL ) + goto out_unlock; + + dev_state->inv_ppr_cb = cb; + + ret =0 ; + +out_unlock: +spin_unlock_irqrestore (&state_lock, flags); + +return ret; +} +EXPORT_SYMBOL (amd_iommu_set_invalid_ppr_cb); +static int __init amd_iommu_v2_init (void ) {size_t state_table_size;
include/linux/amd-iommu.h +31 -3
@@ -28,9 +28,6 @@ struct task_struct;struct pci_dev ;extern int amd_iommu_detect (void ); -extern int amd_iommu_bind_pasid (struct pci_dev *pdev,int pasid, -struct task_struct *task); -extern void amd_iommu_unbind_pasid (struct pci_dev *pdev,int pasid);/**
@@ -91,6 +88,37 @@ extern int amd_iommu_bind_pasid(struct pci_dev *pdev, int pasid,*/ extern void amd_iommu_unbind_pasid (struct pci_dev *pdev,int pasid); +/** +* amd_iommu_set_invalid_ppr_cb() - Register a call-back for failed +* PRI requests +* @pdev: The PCI device the call-back should be registered for +* @cb: The call-back function +* +* The IOMMUv2 driver invokes this call-back when it is unable to +* successfully handle a PRI request. The device driver can then decide +* which PRI response the device should see. Possible return values for +* the call-back are: +* +* - AMD_IOMMU_INV_PRI_RSP_SUCCESS - Send SUCCESS back to the device +* - AMD_IOMMU_INV_PRI_RSP_INVALID - Send INVALID back to the device +* - AMD_IOMMU_INV_PRI_RSP_FAIL - Send Failure back to the device, +* the device is required to disable +* PRI when it receives this response +* +* The function returns 0 on success or negative value on error. +*/ +#define AMD_IOMMU_INV_PRI_RSP_SUCCESS 0 +#define AMD_IOMMU_INV_PRI_RSP_INVALID 1 +#define AMD_IOMMU_INV_PRI_RSP_FAIL 2 + +typedef int (*amd_iommu_invalid_ppr_cb )(struct pci_dev *pdev, +int pasid, +unsigned long address, +u16 ); + +extern int amd_iommu_set_invalid_ppr_cb (struct pci_dev *pdev, +amd_iommu_invalid_ppr_cb cb); +#else static inline int amd_iommu_detect (void ) {return -ENODEV ; }
Signature · 1 line
1.7.5.4
Quoted 12 lines · Joerg Roedel
Hi,
here is a patch-set that adds initial support for the AMD IOMMU version 2 to the Linux kernel. The main feature of the new IOMMU version is support for DMA demand-paging and multiple DMA address spaces per device.
The patch-set consists of 2 parts. Patches 1-16 implement the hardware access functions to setup and manage the IOMMUv2 functionality of the hardware. Patches 17-22 implement a seperate module which makes use these functions to implement a page-fault handler for devices which can be used by real device drivers.
Quoted 6 lines
Any feedback appreciated.
Thanks,
Joerg
What hw/chipset/CPU support this functionality ?
Cheers,
Jerome
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Joerg Roedel
Hi,
here is a patch-set that adds initial support for the AMD IOMMU version 2 to the Linux kernel. The main feature of the new IOMMU version is support for DMA demand-paging and multiple DMA address spaces per device.
The patch-set consists of 2 parts. Patches 1-16 implement the hardware access functions to setup and manage the IOMMUv2 functionality of the hardware. Patches 17-22 implement a seperate module which makes use these functions to implement a page-fault handler for devices which can be used by real device drivers.
Quoted 1 line
What hw/chipset/CPU support this functionality ?
This functionality will be supported in our Trinity APUs and South Islands GPUs, which will be shipping early next year. Official product names for these APUs and GPUs have not yet been announced.
Joerg
Signature · 5 lines
AMD Operating System Research Center Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach General Managers: Alberto Bozzo, Andrew Bowd Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632
Joerg Roedel
maintainerQuoted 11 lines · Joerg Roedel
Hi,
here is a patch-set that adds initial support for the AMD IOMMU version 2 to the Linux kernel. The main feature of the new IOMMU version is support for DMA demand-paging and multiple DMA address spaces per device.
The patch-set consists of 2 parts. Patches 1-16 implement the hardware access functions to setup and manage the IOMMUv2 functionality of the hardware. Patches 17-22 implement a seperate module which makes use these functions to implement a page-fault handler for devices which can be used by real device drivers.
Removed patch 2 because it is unnecessary. Applied the rest to x86/amd.