Intel IOMMU support
Subsystems (5)
cover Intel IOMMU support
Hi,
We are pleased to announce the revised version of
the Intel IOMMU driver. This driver incorporates several feedback received from Anid Kleen, David Miller and several others.
Most notable changes from previous postings (apart from general code cleanup) are
1) Replaced linear linked list with RB tree to manage IOVA's.
2) IOVA address is now being allocated from the cards MAX DMA address capability or DMA32bit limit which ever is lower. This allowed us to get rid of having to preserve certain address range when multiple cards of different DMA address capabilities share the same domain.
3)Implements generic pre-allocated pools a.k.a. resource pool to allocate memory for IOVA's and for vt-d page tables. This resource pools grows automagically in the background (work queued to keventd) based on the demand.
4) Did some tuning in terms of locking for iova allocation and freeing.
5) Changed command line options for isa and gfx workaround to CONFIG options, so that when we have all the components adhere to PCI-DMA api's we can easily yank this workarounds.
With all the above changes, the performance greatly improved and the results showed that performance with IOMMU was comparable to without IOMMU configured.
Once again, thanks for providing valuable feedback, please apply this set of patches to MM if you have no further objections.
Cheers,
-Anil Keshavmaurthy
e-mail: [email protected]
Open Source Technology Center
Intel Corp.
PATCH 1/10 DMAR detection and parsing logic
This patch adds support for early detection and parsing of DMAR's reported to OS via ACPI tables.
--- arch/x86_64/Kconfig | 11 + drivers/pci/Makefile | 3 drivers/pci/dmar.c | 318 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/acpi/actbl1.h | 27 +++- include/linux/dmar.h | 52 ++++++++ 5 files changed, 404 insertions(+), 7 deletions(-)
arch/x86_64/Kconfig +11 -0
@@ -716,6 +716,17 @@ bool "Support mmconfig PCI config space access" depends on PCI && ACPI +config DMAR + bool "Support for DMA Remapping Devices (EXPERIMENTAL)" + depends on PCI_MSI && ACPI && EXPERIMENTAL + default y + help + DMA remapping(DMAR) devices support enables independent address + translations for Direct Memory Access(DMA) from Devices. + These DMA remapping devices are reported via ACPI tables + and includes pci device scope covered by these DMA + remapping device. + source "drivers/pci/pcie/Kconfig" source "drivers/pci/Kconfig"
drivers/pci/Makefile +3 -0
@@ -20,6 +20,9 @@ # Build the Hypertransport interrupt support obj-$(CONFIG_HT_IRQ) += htirq.o +# Build Intel IOMMU support +obj-$(CONFIG_DMAR) += dmar.o + # # Some architectures use the generic PCI setup functions #
drivers/pci/dmar.c new file +318 -0
318 lines not on this page · raw message
include/acpi/actbl1.h +20 -7
@@ -257,7 +257,8 @@struct acpi_table_dmar {struct acpi_table_header header;/* Common ACPI table header */ u8 width;/* Host Address Width */ -u8 reserved[11 ]; +u8 flags; +u8 reserved[10 ]; };/* DMAR subtable header */
@@ -265,8 +266,6 @@struct acpi_dmar_header {u16 type;u16 length; -u8 flags; -u8 reserved[3 ]; };/* Values for subtable type in struct acpi_dmar_header */
@@ -274,13 +273,15 @@enum acpi_dmar_type {ACPI_DMAR_TYPE_HARDWARE_UNIT =0 ,ACPI_DMAR_TYPE_RESERVED_MEMORY =1 , -ACPI_DMAR_TYPE_RESERVED =2 /* 2 and greater are reserved */ +ACPI_DMAR_TYPE_ATSR =2 , +ACPI_DMAR_TYPE_RESERVED =3 /* 3 and greater are reserved */ };struct acpi_dmar_device_scope {u8 entry_type;u8 length; -u8 segment; +u16 reserved; +u8 enumeration_id;u8 bus; };
@@ -290,7 +291,14 @@ACPI_DMAR_SCOPE_TYPE_NOT_USED =0 ,ACPI_DMAR_SCOPE_TYPE_ENDPOINT =1 ,ACPI_DMAR_SCOPE_TYPE_BRIDGE =2 , - ACPI_DMAR_SCOPE_TYPE_RESERVED = 3 /* 3 and greater are reserved */ +ACPI_DMAR_SCOPE_TYPE_IOAPIC =3 , +ACPI_DMAR_SCOPE_TYPE_HPET =4 , + ACPI_DMAR_SCOPE_TYPE_RESERVED = 5 /* 5 and greater are reserved */ +}; + +struct acpi_dmar_pci_path { +u8 dev; +u8 fn; };/*
@@ -301,6 +309,9 @@struct acpi_dmar_hardware_unit {struct acpi_dmar_header header; +u8 flags; +u8 reserved; +u16 segment;u64 address;/* Register Base Address */ };
@@ -312,7 +323,9 @@struct acpi_dmar_reserved_memory {struct acpi_dmar_header header; -u64 address;/* 4_k aligned base address */ +u16 reserved; +u16 segment; +u64 base_address;/* 4_k aligned base address */ u64 end_address;/* 4_k aligned limit address */ };
include/linux/dmar.h new file +52 -0
52 lines not on this page · raw message
Re: [Intel-IOMMU 01/10] DMAR detection and parsing logic
drivers/pci/Makefile line 24
Quoted 8 lines · On Mon, Jun 04, 2007 at 02:02:43PM -0700, [email protected] wrote:
drivers/pci/Makefile +2
-0@@ -20,6 +20,9 @@ # Build the Hypertransport interrupt support obj-$(CONFIG_HT_IRQ) += htirq.o +# Build Intel IOMMU support +obj-$(CONFIG_DMAR) += dmar.o
It's not Intel IOMMU support though, is it?
It's x86 PCI IOMMU support (as opposed to x86 GART IOMMU).
I just want to avoid Intel branding on something that will not be specific to Intel-manufacured products in the long term.
Quoted 63 lines
Index:
linux-2.6.22-rc3/drivers/pci/dmar.c
===================================================================drivers/pci/dmar.c new file +58
-0@@ -0,0 +1,318 @@ +/* +* Copyright (c) 2006, Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope 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. +* +* Copyright (C) Ashok Raj <[email protected]> +* Copyright (C) Shaohua Li <[email protected]> +* +* This file implements early detection/parsing of DMA Remapping Devices +* reported to OS through BIOS via DMA remapping reporting (DMAR) ACPI +* tables. +*/ + +#include <linux/pci.h> +#include <linux/dmar.h> + +#undef PREFIX +#define PREFIX "DMAR:" + +/* No locks are needed as DMA remapping hardware unit +* list is constructed at boot time and hotplug of +* these units are not supported by the architecture. +*/ +LIST_HEAD (dmar_drhd_units); +LIST_HEAD (dmar_rmrr_units); + +static struct acpi_table_header *__initdata dmar_tbl; + +static void __init dmar_register_drhd_unit (struct dmar_drhd_unit *drhd) +{ +/* +* add INCLUDE_ALL at the tail, so scan the list will find it at +* the very end. +*/ +if (drhd->include_all) +list_add_tail (&drhd->list, &dmar_drhd_units); +else +list_add (&drhd->list, &dmar_drhd_units); +} + +static void __init dmar_register_rmrr_unit (struct dmar_rmrr_unit *rmrr) +{ +list_add (&rmrr->list, &dmar_rmrr_units); +} + +static int __init dmar_parse_one_dev_scope (struct acpi_dmar_device_scope *scope, +struct pci_dev **dev,u16 segment)
drivers/pci/dmar.c line 68
Quoted 10 lines
drivers/pci/dmar.c new file +10
-0+{ +struct pci_bus *bus; +struct pci_dev *pdev =NULL ; +struct acpi_dmar_pci_path *path; +int count; + + bus =pci_find_bus (segment, scope->bus); + path = (struct acpi_dmar_pci_path *)(scope +1 ); + count = (scope->length -sizeof (struct acpi_dmar_device_scope )) + /sizeof (struct acpi_dmar_pci_path );
add a space.
But overall this casting and typed-struct pointer addition is a bit fragile. No comment (I haven't read enough yet) whether it is needed or not.
Quoted 392 lines
drivers/pci/dmar.c new file +250
-0+ +while (count) { +if (pdev) +pci_dev_put (pdev); +/* +* Some BIOSes list non-exist devices in DMAR table, just +* ignore it +*/ +if (!bus) { + printk(KERN_WARNING + PREFIX "Device scope bus [%d] not found\n", + scope->bus); +break ; + } + pdev =pci_get_slot (bus,PCI_DEVFN (path->dev, path->fn)); +if (!pdev) { + printk(KERN_WARNING PREFIX +"Device scope device [%04x:%02x:%02x.%02x] not found\n" , + segment, bus->number, path->dev, path->fn); +break ; + } + path ++; + count --; + bus = pdev->subordinate; + } +if (!pdev) { + printk(KERN_WARNING PREFIX +"Device scope device [%04x:%02x:%02x.%02x] not found\n" , + segment, scope->bus, path->dev, path->fn); + *dev =NULL ; +return 0 ; + } +if ((scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_ENDPOINT && pdev->subordinate) + || (scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_BRIDGE && !pdev->subordinate)) { +pci_dev_put (pdev); + printk(KERN_WARNING PREFIX "Device scope type does not match for %s\n", pci_name(pdev)); +return -EINVAL ; + } + *dev = pdev; +return 0 ; +} + +static int __init dmar_parse_dev_scope (void *start,void *end,int *cnt, +struct pci_dev ***devices,u16 segment) +{ +struct acpi_dmar_device_scope *scope; +void * tmp = start; +int index; +int ret; + + *cnt =0 ; +while (start < end) { + scope = start; +if (scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_ENDPOINT || + scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_BRIDGE ) + (*cnt)++; +else + printk(KERN_WARNING PREFIX "Unsupported device scope\n"); + start += scope->length; + } +if (*cnt ==0 ) +return 0 ; + + *devices =kcalloc (*cnt,sizeof (struct pci_dev *),GFP_KERNEL ); +if (!*devices) +return -ENOMEM ; + + start = tmp; + index =0 ; +while (start < end) { + scope = start; +if (scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_ENDPOINT || + scope->entry_type ==ACPI_DMAR_SCOPE_TYPE_BRIDGE ) { + ret =dmar_parse_one_dev_scope (scope, + &(*devices)[index], segment); +if (ret) { +kfree (*devices); +return ret; + } + index ++; + } + start += scope->length; + } + +return 0 ; +} + +/** +* dmar_parse_one_drhd - parses exactly one DMA remapping hardware definition +* structure which uniquely represent one DMA remapping hardware unit +* present in the platform +*/ +static int __init +dmar_parse_one_drhd (struct acpi_dmar_header *header) +{ +struct acpi_dmar_hardware_unit * drhd = (struct acpi_dmar_hardware_unit *)header; +struct dmar_drhd_unit *dmaru; +int ret =0 ; +static int include_all; + + dmaru =kzalloc (sizeof (*dmaru),GFP_KERNEL ); +if (!dmaru) +return -ENOMEM ; + + dmaru->reg_base_addr = drhd->address; + dmaru->include_all = drhd->flags &0x1 ;/* BIT0: INCLUDE_ALL */ + +if (!dmaru->include_all) + ret =dmar_parse_dev_scope ((void *)(drhd +1 ), + ((void *)drhd) + header->length, + &dmaru->devices_cnt, &dmaru->devices, + drhd->segment); +else { +/* Only allow one INCLUDE_ALL */ +if (include_all) { + printk(KERN_WARNING PREFIX "Only one INCLUDE_ALL " + "device scope is allowed\n"); + ret = -EINVAL ; + } + include_all =1 ; + } + +if (ret || (dmaru->devices_cnt ==0 && !dmaru->include_all)) +kfree (dmaru); +else +dmar_register_drhd_unit (dmaru); +return ret; +} + +static int __init +dmar_parse_one_rmrr (struct acpi_dmar_header *header) +{ +struct acpi_dmar_reserved_memory *rmrr = (struct acpi_dmar_reserved_memory *)header; +struct dmar_rmrr_unit *rmrru; +int ret =0 ; + + rmrru =kzalloc (sizeof (*rmrru),GFP_KERNEL ); +if (!rmrru) +return -ENOMEM ; + + rmrru->base_address = rmrr->base_address; + rmrru->end_address = rmrr->end_address; + ret =dmar_parse_dev_scope ((void *)(rmrr +1 ), + ((void *)rmrr) + header->length, + &rmrru->devices_cnt, &rmrru->devices, rmrr->segment); + +if (ret || (rmrru->devices_cnt ==0 )) +kfree (rmrru); +else +dmar_register_rmrr_unit (rmrru); +return ret; +} + +static void __init +dmar_table_print_dmar_entry (struct acpi_dmar_header *header) +{ +struct acpi_dmar_hardware_unit *drhd; +struct acpi_dmar_reserved_memory *rmrr; + +switch (header->type) { +case ACPI_DMAR_TYPE_HARDWARE_UNIT : + drhd = (struct acpi_dmar_hardware_unit *)header; + printk (KERN_INFO PREFIX +"DRHD (flags: 0x%08x)base: 0x%016Lx\n" , + drhd->flags, drhd->address); +break ; +case ACPI_DMAR_TYPE_RESERVED_MEMORY : + rmrr = (struct acpi_dmar_reserved_memory *)header; + + printk (KERN_INFO PREFIX +"RMRR base: 0x%016Lx end: 0x%016Lx\n" , + rmrr->base_address, rmrr->end_address); +break ; + } +} + +/** +* parse_dmar_table - parses the DMA reporting table +*/ +static int __init +parse_dmar_table (void ) +{ +struct acpi_table_dmar *dmar; +struct acpi_dmar_header *entry_header; +int ret =0 ; + + dmar = (struct acpi_table_dmar *)dmar_tbl; + +if (!dmar->width) { + printk (KERN_WARNING PREFIX "Zero: Invalid DMAR haw\n"); +return -EINVAL ; + } + + printk (KERN_INFO PREFIX "Host address width %d\n", + dmar->width + 1); + + entry_header = (struct acpi_dmar_header *)(dmar +1 ); +while (((unsigned long )entry_header) < (((unsigned long )dmar) + dmar_tbl->length)) { +dmar_table_print_dmar_entry (entry_header); + +switch (entry_header->type) { +case ACPI_DMAR_TYPE_HARDWARE_UNIT : + ret =dmar_parse_one_drhd (entry_header); +break ; +case ACPI_DMAR_TYPE_RESERVED_MEMORY : + ret =dmar_parse_one_rmrr (entry_header); +break ; +default : + printk(KERN_WARNING PREFIX "Unknown DMAR structure type\n"); + ret =0 ;/* for forward compatibility */ +break ; + } +if (ret) +break ; + + entry_header = ((void *)entry_header + entry_header->length); + } +return ret; +} + + +int __init dmar_table_init (void ) +{ + +parse_dmar_table (); +if (list_empty (&dmar_drhd_units)) { + printk(KERN_ERR PREFIX "No DMAR devices found\n"); +return -ENODEV ; + } +return 0 ; +} + +/** +* early_dmar_detect - checks to see if the platform supports DMAR devices +*/ +int __init early_dmar_detect (void ) +{ +acpi_status status =AE_OK ; + +/* if we could find DMAR table, then there are DMAR devices */ + status =acpi_get_table (ACPI_SIG_DMAR ,0 , + (struct acpi_table_header **)&dmar_tbl); + +if (ACPI_SUCCESS (status) && !dmar_tbl) { + printk (KERN_WARNING PREFIX "Unable to map DMAR\n"); + status =AE_NOT_FOUND ; + } + +return (ACPI_SUCCESS (status) ?1 :0 ); +}Index:
linux-2.6.22-rc3/include/acpi/actbl1.h
===================================================================include/acpi/actbl1.h +20
-7@@ -257,7 +257,8 @@struct acpi_table_dmar {struct acpi_table_header header;/* Common ACPI table header */ u8 width;/* Host Address Width */ -u8 reserved[11 ]; +u8 flags; +u8 reserved[10 ]; };/* DMAR subtable header */ @@ -265,8 +266,6 @@struct acpi_dmar_header {u16 type;u16 length; -u8 flags; -u8 reserved[3 ]; };/* Values for subtable type in struct acpi_dmar_header */ @@ -274,13 +273,15 @@enum acpi_dmar_type {ACPI_DMAR_TYPE_HARDWARE_UNIT =0 ,ACPI_DMAR_TYPE_RESERVED_MEMORY =1 , -ACPI_DMAR_TYPE_RESERVED =2 /* 2 and greater are reserved */ +ACPI_DMAR_TYPE_ATSR =2 , +ACPI_DMAR_TYPE_RESERVED =3 /* 3 and greater are reserved */ };struct acpi_dmar_device_scope {u8 entry_type;u8 length; -u8 segment; +u16 reserved; +u8 enumeration_id;u8 bus; };@@ -290,7 +291,14 @@ACPI_DMAR_SCOPE_TYPE_NOT_USED =0 ,ACPI_DMAR_SCOPE_TYPE_ENDPOINT =1 ,ACPI_DMAR_SCOPE_TYPE_BRIDGE =2 , - ACPI_DMAR_SCOPE_TYPE_RESERVED = 3 /* 3 and greater are reserved */ +ACPI_DMAR_SCOPE_TYPE_IOAPIC =3 , +ACPI_DMAR_SCOPE_TYPE_HPET =4 , + ACPI_DMAR_SCOPE_TYPE_RESERVED = 5 /* 5 and greater are reserved */ +}; + +struct acpi_dmar_pci_path { +u8 dev; +u8 fn; };/* @@ -301,6 +309,9 @@struct acpi_dmar_hardware_unit {struct acpi_dmar_header header; +u8 flags; +u8 reserved; +u16 segment;u64 address;/* Register Base Address */ };@@ -312,7 +323,9 @@struct acpi_dmar_reserved_memory {struct acpi_dmar_header header; -u64 address;/* 4_k aligned base address */ +u16 reserved; +u16 segment; +u64 base_address;/* 4_k aligned base address */ u64 end_address;/* 4_k aligned limit address */ };Index:
linux-2.6.22-rc3/include/linux/dmar.h
===================================================================include/linux/dmar.h new file +52
-0@@ -0,0 +1,52 @@ +/* +* Copyright (c) 2006, Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* This program is distributed in the hope 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. +* +* Copyright (C) Ashok Raj <[email protected]> +* Copyright (C) Shaohua Li <[email protected]> +*/ + +#ifndef __DMAR_H__ +#define __DMAR_H__ + +#include <linux/acpi.h> +#include <linux/types.h> + + +extern int dmar_table_init (void ); +extern int early_dmar_detect (void ); + +extern struct list_head dmar_drhd_units; +extern struct list_head dmar_rmrr_units; + +struct dmar_drhd_unit { +struct list_head list;/* list of drhd units */ +u64 reg_base_addr;/* register base address*/ +struct pci_dev **devices;/* target device array */ +int devices_cnt;/* target device count */ +u8 ignored:1 ;/* ignore drhd */ +u8 include_all:1 ; +struct intel_iommu *iommu; +}; + +struct dmar_rmrr_unit { +struct list_head list;/* list of rmrr units */ +u64 base_address;/* reserved base address*/ +u64 end_address;/* reserved end address */ +struct pci_dev **devices;/* target devices */ +int devices_cnt;/* target device count */ +}; + +#endif /* __DMAR_H__ */ Signature · 6 lines
-- - 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/
Re: [Intel-IOMMU 01/10] DMAR detection and parsing logic
Quoted 11 lines · Jeff Garzik
drivers/pci/Makefile line 24
Quoted 8 lines · On Mon, Jun 04, 2007 at 02:02:43PM -0700, [email protected] wrote:
drivers/pci/Makefile +2
-0@@ -20,6 +20,9 @@ # Build the Hypertransport interrupt support obj-$(CONFIG_HT_IRQ) += htirq.o +# Build Intel IOMMU support +obj-$(CONFIG_DMAR) += dmar.oIt's not Intel IOMMU support though, is it?
Yes, it is Intel IOMMU support.
Quoted 5 lines
It's x86 PCI IOMMU support (as opposed to x86 GART IOMMU).
I just want to avoid Intel branding on something that will not be specific to Intel-manufacured products in the long term.
This DMAR (DMA Remapping unit) is Intel specific and its spec is available at www.intel.com/technology/virtualization
-Anil
Re: [Intel-IOMMU 01/10] DMAR detection and parsing logic
In reply to [email protected]Is there no way at all, other than ACPI, to find this stuff?
We would prefer to avoid hardware if the hardware enumeration is sane.
Jeff
Re: [Intel-IOMMU 01/10] DMAR detection and parsing logic
Quoted 2 lines · Jeff Garzik
Is there no way at all, other than ACPI, to find this stuff?
This is as clean as possible without letting ACPI to parse this DMA remapping unit. The only thing we might be using is the acpi data struct.
-Anil
PATCH 2/10 Library routines for handling pre-allocated pool of objects
In reply to [email protected] This patch provides a common interface for pre allocating and
managing pool of objects.
--- include/linux/respool.h | 43 +++++++++++ lib/Makefile | 1 lib/respool.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+)
include/linux/respool.h new file +43 -0
@@ -0,0 +1,43 @@ +/* +* respool.c - library routines for handling generic pre-allocated pool of objects +* +* Copyright (c) 2006, Intel Corporation. +* +* This file is released under the GPLv2. +* +* Copyright (C) 2006 Anil S Keshavamurthy <[email protected]> +* +*/ + +#ifndef _RESPOOL_H_ +#define _RESPOOL_H_ + +#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/workqueue.h> + +typedef void *(*rpool_alloc_t )(unsigned int ,gfp_t ); +typedef void (*rpool_free_t )(void *,unsigned int ); + +struct resource_pool { +struct work_struct work; +spinlock_t pool_lock;/* pool lock to walk the pool_head */ +struct list_head pool_head;/* pool objects list head */ +unsigned int min_count;/* min count to maintain */ +unsigned int grow_count;/* grow by count when time to grow */ +unsigned int curr_count;/* count of current free objects */ +unsigned int alloc_size;/* objects size */ +rpool_alloc_t alloc_mem;/* pool mem alloc function pointer */ +rpool_free_t free_mem;/* pool mem free function pointer */ +}; + +void *get_resource_pool_obj (struct resource_pool *ppool); +void put_resource_pool_obj (void * vaddr,struct resource_pool *ppool); +void destroy_resource_pool (struct resource_pool *ppool); +int init_resource_pool (struct resource_pool *res, +unsigned int min_count,unsigned int alloc_size, +unsigned int grow_count,rpool_alloc_t alloc_fn, +rpool_free_t free_fn); + +#endif
lib/Makefile +1 -0
@@ -58,6 +58,7 @@ obj-$(CONFIG_AUDIT_GENERIC) += audit.o obj-$(CONFIG_SWIOTLB) += swiotlb.o +obj-$(CONFIG_DMAR) += respool.o obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o lib-$(CONFIG_GENERIC_BUG) += bug.o
lib/respool.c new file +176 -0
176 lines not on this page · raw message
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
Quoted 120 lines · On Mon, Jun 04, 2007 at 02:02:44PM -0700, [email protected] wrote:
This patch provides a common interface for pre allocating and
managing pool of objects.Signed-off-by Anil S Keshavamurthy <[email protected]>--- include/linux/respool.h | 43 +++++++++++ lib/Makefile | 1 lib/respool.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+)Index:
linux-2.6.22-rc3/include/linux/respool.h
===================================================================include/linux/respool.h new file +43
-0@@ -0,0 +1,43 @@ +/* +* respool.c - library routines for handling generic pre-allocated pool of objects +* +* Copyright (c) 2006, Intel Corporation. +* +* This file is released under the GPLv2. +* +* Copyright (C) 2006 Anil S Keshavamurthy <[email protected]> +* +*/ + +#ifndef _RESPOOL_H_ +#define _RESPOOL_H_ + +#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/workqueue.h> + +typedef void *(*rpool_alloc_t )(unsigned int ,gfp_t ); +typedef void (*rpool_free_t )(void *,unsigned int ); + +struct resource_pool { +struct work_struct work; +spinlock_t pool_lock;/* pool lock to walk the pool_head */ +struct list_head pool_head;/* pool objects list head */ +unsigned int min_count;/* min count to maintain */ +unsigned int grow_count;/* grow by count when time to grow */ +unsigned int curr_count;/* count of current free objects */ +unsigned int alloc_size;/* objects size */ +rpool_alloc_t alloc_mem;/* pool mem alloc function pointer */ +rpool_free_t free_mem;/* pool mem free function pointer */ +}; + +void *get_resource_pool_obj (struct resource_pool *ppool); +void put_resource_pool_obj (void * vaddr,struct resource_pool *ppool); +void destroy_resource_pool (struct resource_pool *ppool); +int init_resource_pool (struct resource_pool *res, +unsigned int min_count,unsigned int alloc_size, +unsigned int grow_count,rpool_alloc_t alloc_fn, +rpool_free_t free_fn); + +#endif Index: linux-2.6.22-rc3/lib/Makefile
===================================================================lib/Makefile +1
-0@@ -58,6 +58,7 @@ obj-$(CONFIG_AUDIT_GENERIC) += audit.o obj-$(CONFIG_SWIOTLB) += swiotlb.o +obj-$(CONFIG_DMAR) += respool.o obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o lib-$(CONFIG_GENERIC_BUG) += bug.oIndex:
linux-2.6.22-rc3/lib/respool.c
===================================================================lib/respool.c new file +45
-0@@ -0,0 +1,176 @@ +/* +* respool.c - library routines for handling generic pre-allocated pool of objects +* +* Copyright (c) 2006, Intel Corporation. +* +* This file is released under the GPLv2. +* +* Copyright (C) 2006 Anil S Keshavamurthy <[email protected]> +*/ + +#include <linux/respool.h> + +/** +* get_resource_pool_obj - gets an object from the pool +* @ppool - resource pool in question +* This function gets an object from the pool and +* if the pool count drops below min_count, this +* function schedules work to grow the pool. If +* no elements are fount in the pool then this function +* tries to get memory from kernel. +*/ +void *get_resource_pool_obj (struct resource_pool *ppool) +{ +unsigned long flags; +struct list_head *plist; +bool queue_work =0 ; + +spin_lock_irqsave (&ppool->pool_lock, flags); +if (!list_empty (&ppool->pool_head)) { + plist = ppool->pool_head.next; +list_del (plist); + ppool->curr_count--; + }else { +/*Making sure that curr_count is 0 when list is empty */ + plist =NULL ; +BUG_ON (ppool->curr_count !=0 ); + } + +/* Check if pool needs to grow */ +if (ppool->curr_count <= ppool->min_count) + queue_work =1 ; +spin_unlock_irqrestore (&ppool->pool_lock, flags); + +if (queue_work) +schedule_work (&ppool->work);/* queue work to grow the pool */
lib/respool.c line 55
Quoted 10 lines
lib/respool.c new file +10
-0+ + +if (plist) { +memset (plist,0 , ppool->alloc_size);/* Zero out memory */ +return plist; + } + +/* Out of luck, try to get memory from kernel */ + plist = (struct list_head *)ppool->alloc_mem (ppool->alloc_size, +GFP_ATOMIC );
Since you are outside the lock, you should pass in gfp_flags, to enhance the chance of being able to use GFP_KERNEL in certain contexts.
Quoted 11 lines
lib/respool.c new file +11
-0+return plist; +} + +/** +* put_resource_pool_obj - puts an object back to the pool +* @vaddr - object's address +* @ppool - resource pool in question. +* This function puts an object back to the pool. +*/ +void put_resource_pool_obj (void * vaddr,struct resource_pool *ppool) +{
lib/respool.c line 77
Quoted 10 lines
lib/respool.c new file +10
-0+unsigned long flags; +struct list_head *plist = (struct list_head *)vaddr; + +BUG_ON (!vaddr); +BUG_ON (!ppool); + +spin_lock_irqsave (&ppool->pool_lock, flags); +list_add (plist, &ppool->pool_head); + ppool->curr_count++; +spin_unlock_irqrestore (&ppool->pool_lock, flags);
you should add logic to free resources here (or queue_work to free the resources), if the pool grows beyond a certain size.
Quoted 104 lines
lib/respool.c new file +97
-0+/** +* grow_resource_pool - grows the given resource pool +* @work - work struct +* This functions gets the resource pool pointer from the +* work struct and grows the resource pool by grow_count. +*/ +static void +grow_resource_pool (struct work_struct * work) +{ +struct resource_pool *ppool; +struct list_head *plist; +unsigned int min_count, grow_count =0 ; +unsigned long flags; + + ppool = container_of(work, struct resource_pool, work); + +/* compute the minimum count to grow */ +spin_lock_irqsave (&ppool->pool_lock, flags); + min_count = ppool->min_count + ppool->grow_count; +if (ppool->curr_count < min_count) + grow_count = min_count - ppool->curr_count; +spin_unlock_irqrestore (&ppool->pool_lock, flags); + +while (grow_count) { + plist = (struct list_head *)ppool->alloc_mem (ppool->alloc_size, +GFP_KERNEL ); + +if (!plist) +break ; + +/* Add the element to the list */ +spin_lock_irqsave (&ppool->pool_lock, flags); +list_add (plist, &ppool->pool_head); + ppool->curr_count++; +spin_unlock_irqrestore (&ppool->pool_lock, flags); + grow_count--; + } +} + +/** +* destroy_resource_pool - destroys the given resource pool +* @ppool - resource pool in question. +* This function walks throuhg its list and frees up the +* preallocated objects. +*/ +void +destroy_resource_pool (struct resource_pool *ppool) +{ +unsigned long flags; +struct list_head *plist; + +spin_lock_irqsave (&ppool->pool_lock, flags); +while (!list_empty (&ppool->pool_head)) { + plist = &ppool->pool_head; +list_del (plist); + + ppool->free_mem (plist, ppool->alloc_size); + + } + ppool->curr_count =0 ; +spin_unlock_irqrestore (&ppool->pool_lock, flags); +} + +/** +* init_resource_pool - initializes the resource pool +* @res: resource pool in question. +* @min_count: count of objectes to pre-allocate +* @alloc_size: size of each objects +* @grow_count: count of objects to grow when required +* @alloc_fn: function which allocates memory +* @free_fn: function which frees memory +* +* This function initializes the given resource pool and +* populates the min_count of objects to begin with. +*/ +int +init_resource_pool (struct resource_pool *res, +unsigned int min_count,unsigned int alloc_size, +unsigned int grow_count,rpool_alloc_t alloc_fn, +rpool_free_t free_fn) +{ + res->min_count = min_count; + res->alloc_size = alloc_size; + res->grow_count = grow_count; + res->curr_count =0 ; + res->alloc_mem = alloc_fn; + res->free_mem = free_fn; +spin_lock_init (&res->pool_lock); +INIT_LIST_HEAD (&res->pool_head); +INIT_WORK (&res->work, grow_resource_pool); + +/* grow the pool */ +grow_resource_pool (&res->work); + +return (res->curr_count ==0 ); +} +Signature · 6 lines
-- - 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/
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
Quoted 126 lines · Jeff Garzik
Quoted 125 lines · On Mon, Jun 04, 2007 at 02:02:44PM -0700, [email protected] wrote:
This patch provides a common interface for pre allocating and
managing pool of objects.Signed-off-by Anil S Keshavamurthy <[email protected]>--- include/linux/respool.h | 43 +++++++++++ lib/Makefile | 1 lib/respool.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+)Index:
linux-2.6.22-rc3/include/linux/respool.h
===================================================================include/linux/respool.h new file +43
-0@@ -0,0 +1,43 @@ +/* +* respool.c - library routines for handling generic pre-allocated pool of objects +* +* Copyright (c) 2006, Intel Corporation. +* +* This file is released under the GPLv2. +* +* Copyright (C) 2006 Anil S Keshavamurthy <[email protected]> +* +*/ + +#ifndef _RESPOOL_H_ +#define _RESPOOL_H_ + +#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/workqueue.h> + +typedef void *(*rpool_alloc_t )(unsigned int ,gfp_t ); +typedef void (*rpool_free_t )(void *,unsigned int ); + +struct resource_pool { +struct work_struct work; +spinlock_t pool_lock;/* pool lock to walk the pool_head */ +struct list_head pool_head;/* pool objects list head */ +unsigned int min_count;/* min count to maintain */ +unsigned int grow_count;/* grow by count when time to grow */ +unsigned int curr_count;/* count of current free objects */ +unsigned int alloc_size;/* objects size */ +rpool_alloc_t alloc_mem;/* pool mem alloc function pointer */ +rpool_free_t free_mem;/* pool mem free function pointer */ +}; + +void *get_resource_pool_obj (struct resource_pool *ppool); +void put_resource_pool_obj (void * vaddr,struct resource_pool *ppool); +void destroy_resource_pool (struct resource_pool *ppool); +int init_resource_pool (struct resource_pool *res, +unsigned int min_count,unsigned int alloc_size, +unsigned int grow_count,rpool_alloc_t alloc_fn, +rpool_free_t free_fn); + +#endif Index: linux-2.6.22-rc3/lib/Makefile
===================================================================lib/Makefile +1
-0@@ -58,6 +58,7 @@ obj-$(CONFIG_AUDIT_GENERIC) += audit.o obj-$(CONFIG_SWIOTLB) += swiotlb.o +obj-$(CONFIG_DMAR) += respool.o obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o lib-$(CONFIG_GENERIC_BUG) += bug.oIndex:
linux-2.6.22-rc3/lib/respool.c
===================================================================lib/respool.c new file +50
-0@@ -0,0 +1,176 @@ +/* +* respool.c - library routines for handling generic pre-allocated pool of objects +* +* Copyright (c) 2006, Intel Corporation. +* +* This file is released under the GPLv2. +* +* Copyright (C) 2006 Anil S Keshavamurthy <[email protected]> +*/ + +#include <linux/respool.h> + +/** +* get_resource_pool_obj - gets an object from the pool +* @ppool - resource pool in question +* This function gets an object from the pool and +* if the pool count drops below min_count, this +* function schedules work to grow the pool. If +* no elements are fount in the pool then this function +* tries to get memory from kernel. +*/ +void *get_resource_pool_obj (struct resource_pool *ppool) +{ +unsigned long flags; +struct list_head *plist; +bool queue_work =0 ; + +spin_lock_irqsave (&ppool->pool_lock, flags); +if (!list_empty (&ppool->pool_head)) { + plist = ppool->pool_head.next; +list_del (plist); + ppool->curr_count--; + }else { +/*Making sure that curr_count is 0 when list is empty */ + plist =NULL ; +BUG_ON (ppool->curr_count !=0 ); + } + +/* Check if pool needs to grow */ +if (ppool->curr_count <= ppool->min_count) + queue_work =1 ; +spin_unlock_irqrestore (&ppool->pool_lock, flags); + +if (queue_work) +schedule_work (&ppool->work);/* queue work to grow the pool */ + + +if (plist) { +memset (plist,0 , ppool->alloc_size);/* Zero out memory */ +return plist;
Quoted 8 lines
lib/respool.c line 55
Quoted 5 lines
lib/respool.c new file +5
-0+ } + +/* Out of luck, try to get memory from kernel */ + plist = (struct list_head *)ppool->alloc_mem (ppool->alloc_size, +GFP_ATOMIC );Since you are outside the lock, you should pass in
gfp_flags, to enhance the chance of being able to useGFP_KERNELin certain contexts.
No, you got it wrong. This above function get_resource_pool_obj() itself is called from interrrupt time so the logic is to get an object from the pre-allocated pool if available else get it from kernel without blocking which is to pass GFP_ATOMIC flag.
Quoted 18 lines
Quoted 18 lines
lib/respool.c new file +16
-0+return plist; +} + +/** +* put_resource_pool_obj - puts an object back to the pool +* @vaddr - object's address +* @ppool - resource pool in question. +* This function puts an object back to the pool. +*/ +void put_resource_pool_obj (void * vaddr,struct resource_pool *ppool) +{ +unsigned long flags; +struct list_head *plist = (struct list_head *)vaddr; + +BUG_ON (!vaddr); +BUG_ON (!ppool);
Quoted 8 lines
lib/respool.c line 77
Quoted 5 lines
lib/respool.c new file +5
-0+ +spin_lock_irqsave (&ppool->pool_lock, flags); +list_add (plist, &ppool->pool_head); + ppool->curr_count++; +spin_unlock_irqrestore (&ppool->pool_lock, flags);you should add logic to free resources here (or
queue_workto free the resources), if the pool grows beyond a certain size.
Can be added as an add on, testing showed that pool grows to a certain size and will not grow beyond that as we tend to reuse the elements.
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
Quoted 7 lines · Anil S
Quoted 2 lines · Jeff Garzik
you should add logic to free resources here (or
queue_workto free the resources), if the pool grows beyond a certain size.Can be added as an add on, testing showed that pool grows to a certain size and will not grow beyond that as we tend to reuse the elements.
Yes, but is it possible? If no, what part of the code guarantees the pool is limited?
We should not merge code that allows the pool to grow without bound. In-house testing certainly never covers all the cases seen in the field, so I wouldn't make too many assumptions based on that. Some vendor will inevitably build a $BigNum system where the IOMMU is very heavily used.
Jeff
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
Quoted 9 lines · Jeff Garzik
Quoted 7 lines · Anil S
Quoted 2 lines · Jeff Garzik
you should add logic to free resources here (or
queue_workto free the resources), if the pool grows beyond a certain size.Can be added as an add on, testing showed that pool grows to a certain size and will not grow beyond that as we tend to reuse the elements.
Quoted 8 lines
Yes, but is it possible? If no, what part of the code guarantees the pool is limited?
We should not merge code that allows the pool to grow without bound. In-house testing certainly never covers all the cases seen in the field, so I wouldn't make too many assumptions based on that. Some vendor will inevitably build a $BigNum system where the IOMMU is very heavily used.
No problem, I can add the code to free the pool element if the curr_count ever goes greater than (min_count + 2 * grow_count) then bring the curr_count to min_count + grow_count by freeing some pool objects.
A patch which will apply to this current patch will follow soon.
Thanks Jeff for making your case stronger :)
-Anil
Quoted 3 lines
Jeff
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
Quoted 19 lines · Anil S
Quoted 9 lines · Jeff Garzik
Quoted 7 lines · Anil S
Quoted 2 lines · Jeff Garzik
you should add logic to free resources here (or
queue_workto free the resources), if the pool grows beyond a certain size.Can be added as an add on, testing showed that pool grows to a certain size and will not grow beyond that as we tend to reuse the elements.
Quoted 8 lines
Yes, but is it possible? If no, what part of the code guarantees the pool is limited?
We should not merge code that allows the pool to grow without bound. In-house testing certainly never covers all the cases seen in the field, so I wouldn't make too many assumptions based on that. Some vendor will inevitably build a $BigNum system where the IOMMU is very heavily used.
Quoted 6 lines
No problem, I can add the code to free the pool element if the
curr_countever goes greater than (min_count+ 2 *grow_count) then bring thecurr_counttomin_count+grow_countby freeing some pool objects.A patch which will apply to this current patch will follow soon.
Here goes the patch as promised...
------------------------------------
Now adding the logic to shrink the resource pool and give back the excess pool object's memory space back to OS.
This patch add's on top of my previous posting.
--- lib/respool.c | 84 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 19 deletions(-)
lib/respool.c +65 -19
@@ -67,6 +67,7 @@ {unsigned long flags;struct list_head *plist = (struct list_head *)vaddr; +bool queue_work =0 ;BUG_ON (!vaddr);BUG_ON (!ppool);
@@ -74,21 +75,74 @@spin_lock_irqsave (&ppool->pool_lock, flags);list_add (plist, &ppool->pool_head); ppool->curr_count++; +if (ppool->curr_count > (ppool->min_count + + ppool->grow_count *2 )) + queue_work =1 ;spin_unlock_irqrestore (&ppool->pool_lock, flags); + +if (queue_work) +schedule_work (&ppool->work);/* queue work to shrink the pool */ +} + +void +__grow_resource_pool (struct resource_pool *ppool, +unsigned int grow_count) +{ +unsigned long flags; +struct list_head *plist; + +while (grow_count) { + plist = (struct list_head *)ppool->alloc_mem (ppool->alloc_size, +GFP_KERNEL ); + +if (!plist) +break ; + +/* Add the element to the list */ +spin_lock_irqsave (&ppool->pool_lock, flags); +list_add (plist, &ppool->pool_head); + ppool->curr_count++; +spin_unlock_irqrestore (&ppool->pool_lock, flags); + grow_count--; + } } +void +__shrink_resource_pool (struct resource_pool *ppool, +unsigned int shrink_count) +{ +unsigned long flags; +struct list_head *plist; + +while (shrink_count) { +/* remove an object from the pool */ +spin_lock_irqsave (&ppool->pool_lock, flags); +if (list_empty (&ppool->pool_head)) { +spin_unlock_irqrestore (&ppool->pool_lock, flags); +break ; + } + plist = ppool->pool_head.next; +list_del (plist); + ppool->curr_count--; +spin_unlock_irqrestore (&ppool->pool_lock, flags); + ppool->free_mem (plist, ppool->alloc_size); + shrink_count--; + } +} + +/** -* grow_resource_pool - grows the given resource pool +* resize_resource_pool - resize the given resource pool * @work - work struct * This functions gets the resource pool pointer from the * work struct and grows the resource pool by grow_count. */ static void -grow_resource_pool (struct work_struct * work) +resize_resource_pool (struct work_struct * work) {struct resource_pool *ppool; -struct list_head *plist;unsigned int min_count, grow_count =0 ; +unsigned int shrink_count =0 ;unsigned long flags; ppool = container_of(work, struct resource_pool, work);
@@ -98,22 +152,14 @@ min_count = ppool->min_count + ppool->grow_count;if (ppool->curr_count < min_count) grow_count = min_count - ppool->curr_count; +else if (ppool->curr_count > min_count + ppool->grow_count) + shrink_count = ppool->curr_count - min_count;spin_unlock_irqrestore (&ppool->pool_lock, flags); -while (grow_count) { - plist = (struct list_head *)ppool->alloc_mem (ppool->alloc_size, -GFP_KERNEL ); - -if (!plist) -break ; - -/* Add the element to the list */ -spin_lock_irqsave (&ppool->pool_lock, flags); -list_add (plist, &ppool->pool_head); - ppool->curr_count++; -spin_unlock_irqrestore (&ppool->pool_lock, flags); - grow_count--; - } +if (grow_count) +__grow_resource_pool (ppool, grow_count); +else if (shrink_count) +__shrink_resource_pool (ppool, shrink_count); }/**
@@ -166,10 +212,10 @@ res->free_mem = free_fn;spin_lock_init (&res->pool_lock);INIT_LIST_HEAD (&res->pool_head); -INIT_WORK (&res->work, grow_resource_pool); +INIT_WORK (&res->work, resize_resource_pool);/* grow the pool */ -grow_resource_pool (&res->work); +resize_resource_pool (&res->work);return (res->curr_count ==0 ); }
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
lib/respool.c line 70
Quoted 11 lines · Anil S
1 file changed, 65 insertions(+), 19 deletions(-)Index:
linux-2.6.22-rc3/lib/respool.c
===================================================================lib/respool.c +1
-0@@ -67,6 +67,7 @@ {unsigned long flags;struct list_head *plist = (struct list_head *)vaddr; +bool queue_work =0 ;
Seems sane to me.
I only have a very minor nit to pick: naming a variable (queue_work) the same as a public function may cause confusion.
Also, if its a bool you should initialize it to 'true' or 'false'.
Jeff
Re: [Intel-IOMMU 02/10] Library routines for handling pre-allocated pool of objects
Quoted 14 lines · Jeff Garzik
lib/respool.c line 70
Quoted 11 lines · Anil S
1 file changed, 65 insertions(+), 19 deletions(-)Index:
linux-2.6.22-rc3/lib/respool.c
===================================================================lib/respool.c +1
-0@@ -67,6 +67,7 @@ {unsigned long flags;struct list_head *plist = (struct list_head *)vaddr; +bool queue_work =0 ;Seems sane to me.
Thanks so much.
Quoted 5 lines
I only have a very minor nit to pick: naming a variable (
queue_work) the same as a public function may cause confusion.Also, if its a bool you should initialize it to 'true' or 'false'.
I will address when this patch gets into Andrew's mm tree.
Thanks once again,
Anil
PATCH 3/10 PCI generic helper function
In reply to [email protected]When devices are under a p2p bridge, upstream
transactions get replaced by the device id of the bridge as it owns the PCIE transaction. Hence its necessary to setup translations on behalf of the bridge as well. Due to this limitation all devices under a p2p share the same domain in a DMAR.
We just cache the type of device, if its a native PCIe device or not for later use.
--- drivers/pci/pci.h | 1 + drivers/pci/probe.c | 14 ++++++++++++++ drivers/pci/search.c | 30 ++++++++++++++++++++++++++++++ include/linux/pci.h | 2 ++ 4 files changed, 47 insertions(+)
drivers/pci/pci.h +1 -0
@@ -92,3 +92,4 @@return NULL ; } +struct pci_dev *pci_find_upstream_pcie_bridge (struct pci_dev *pdev);
drivers/pci/probe.c +14 -0
@@ -803,6 +803,19 @@kfree (pci_dev); } +static void set_pcie_port_type (struct pci_dev *pdev) +{ +int pos; +u16 reg16; + + pos =pci_find_capability (pdev,PCI_CAP_ID_EXP ); +if (!pos) +return ; + pdev->is_pcie =1 ; +pci_read_config_word (pdev, pos +PCI_EXP_FLAGS , ®16); + pdev->pcie_type = (reg16 &PCI_EXP_FLAGS_TYPE ) >>4 ; +} +/** * pci_cfg_space_size - get the configuration space size of the PCI device. * @dev: PCI device
@@ -917,6 +930,7 @@ dev->device = (l >>16 ) &0xffff ; dev->cfg_size =pci_cfg_space_size (dev); dev->error_state = pci_channel_io_normal; +set_pcie_port_type (dev);/* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer) set this higher, assuming the system even supports it. */
drivers/pci/search.c +30 -0
@@ -14,6 +14,36 @@#include "pci.h" DECLARE_RWSEM (pci_bus_sem); +/* +* find the upstream PCIE-to-PCI bridge of a PCI device +* if the device is PCIE, return NULL +* if the device isn't connected to a PCIE bridge (that is its parent is a +* legacy PCI bridge and the bridge is directly connected to bus 0), return its +* parent +*/ +struct pci_dev * +pci_find_upstream_pcie_bridge (struct pci_dev *pdev) +{ +struct pci_dev *tmp =NULL ; + +if (pdev->is_pcie) +return NULL ; +while (1 ) { +if (!pdev->bus->self) +break ; + pdev = pdev->bus->self; +/* a p2p bridge */ +if (!pdev->is_pcie) { + tmp = pdev; +continue ; + } +/* PCI device should connect to a PCIE bridge */ +BUG_ON (pdev->pcie_type !=PCI_EXP_TYPE_PCI_BRIDGE ); +return pdev; + } + +return tmp; +}static struct pci_bus *pci_do_find_bus (struct pci_bus *bus,unsigned char busnr) {
include/linux/pci.h +2 -0
@@ -139,6 +139,7 @@unsigned short subsystem_device;unsigned int class;/* 3 bytes: (base,sub,prog-if) */ u8 hdr_type;/* PCI header type (`multi' flag masked out) */ +u8 pcie_type;/* PCI-E device/port type */ u8 rom_base_reg;/* which config register controls the ROM */ u8 pin;/* which interrupt pin this device uses */
@@ -181,6 +182,7 @@ unsigned int msi_enabled:1; unsigned int msix_enabled:1; unsigned int is_managed:1; + unsigned int is_pcie:1;atomic_t enable_cnt;/* pci_enable_device has been called */ u32 saved_config_space[16 ];/* config space saved at suspend time */
PATCH 4/10 clflush_cache_range now takes size param
In reply to [email protected] Introduce the size param for clflush_cache_range().
--- arch/x86_64/mm/pageattr.c | 6 +++--- include/asm-x86_64/cacheflush.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-)
arch/x86_64/mm/pageattr.c +3 -3
@@ -61,10 +61,10 @@return base; } -static void cache_flush_page (void *adr) +void clflush_cache_range (void *adr,int size) {int i; -for (i =0 ; i <PAGE_SIZE ; i += boot_cpu_data.x86_clflush_size) +for (i =0 ; i < size; i += boot_cpu_data.x86_clflush_size) asmvolatile ("clflush (%0)" ::"r" (adr + i)); }
@@ -80,7 +80,7 @@list_for_each_entry (pg, l, lru) {void *adr =page_address (pg);if (cpu_has_clflush) -cache_flush_page (adr); +clflush_cache_range (adr,PAGE_SIZE ); }__flush_tlb_all (); }
include/asm-x86_64/cacheflush.h +1 -0
@@ -27,6 +27,7 @@void global_flush_tlb (void );int change_page_attr (struct page *page,int numpages,pgprot_t prot);int change_page_attr_addr (unsigned long addr,int numpages,pgprot_t prot); +void clflush_cache_range (void *addr,int size);#ifdef CONFIG_DEBUG_RODATA void mark_rodata_ro (void );
PATCH 5/10 IOVA allocation and management routines
In reply to [email protected] This code implements a generic IOVA allocation and
management. As per Dave's suggestion we are now allocating IO virtual address from Higher DMA limit address rather than lower end address and this eliminated the need to preserve the IO virtual address for multiple devices sharing the same domain virtual address.
Also this code uses red black trees to store the allocated and reserved iova nodes. This showed a good performance improvements over previous linear linked list.
--- drivers/pci/iova.c | 344 +++++++++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/iova.h | 57 ++++++++ 2 files changed, 401 insertions(+)
drivers/pci/iova.c new file +344 -0
@@ -0,0 +1,344 @@ +/* +* Copyright (c) 2006, Intel Corporation. +* +* This file is released under the GPLv2. +* +* Copyright (C) 2006 Anil S Keshavamurthy <[email protected]> +*/ + +#include "iova.h" + +void +init_iova_domain (struct iova_domain *iovad) +{ +spin_lock_init (&iovad->iova_alloc_lock); +spin_lock_init (&iovad->iova_rbtree_lock); + iovad->rbroot =RB_ROOT ; + iovad->cached32_node =NULL ; + +} + +static struct rb_node * +__get_cached_rbnode (struct iova_domain *iovad,unsigned long *limit_pfn) +{ +if ((*limit_pfn !=DMA_32BIT_PFN ) || + (iovad->cached32_node ==NULL )) +return rb_last (&iovad->rbroot); +else { +struct rb_node *prev_node =rb_prev (iovad->cached32_node); +struct iova *curr_iova = + container_of(iovad->cached32_node, struct iova, node); + *limit_pfn = curr_iova->pfn_lo -1 ; +return prev_node; + } +} + +static inline void +__cached_rbnode_insert_update (struct iova_domain *iovad, +unsigned long limit_pfn,struct iova *new) +{ +if (limit_pfn !=DMA_32BIT_PFN ) +return ; + iovad->cached32_node = &new->node; +} + +static inline void +__cached_rbnode_delete_update (struct iova_domain *iovad,struct iova *free) +{ +struct iova *cached_iova; +struct rb_node *curr; + +if (!iovad->cached32_node) +return ; + curr = iovad->cached32_node; + cached_iova = container_of(curr, struct iova, node); + +if (free->pfn_lo >= cached_iova->pfn_lo) + iovad->cached32_node =rb_next (&free->node); +} + +static inline int __alloc_iova_range (struct iova_domain *iovad, +unsigned long size,unsigned long limit_pfn,struct iova *new) +{ +struct rb_node *curr =NULL ; +unsigned long flags; +unsigned long saved_pfn; + +/* Walk the tree backwards */ +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); + saved_pfn = limit_pfn; + curr =__get_cached_rbnode (iovad, &limit_pfn); +while (curr) { + struct iova *curr_iova = container_of(curr, struct iova, node); +if (limit_pfn < curr_iova->pfn_lo) + goto move_left; +if (limit_pfn < curr_iova->pfn_hi) + goto adjust_limit_pfn; +if ((curr_iova->pfn_hi + size) <= limit_pfn) +break ;/* found a free slot */ +adjust_limit_pfn: + limit_pfn = curr_iova->pfn_lo -1 ; +move_left: + curr =rb_prev (curr); + } + +if ((!curr) && !(IOVA_START_PFN + size <= limit_pfn)) { +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); +return -ENOMEM ; + } + new->pfn_hi = limit_pfn; + new->pfn_lo = limit_pfn - size +1 ; + +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); +return 0 ; +} + +static void +iova_insert_rbtree (struct rb_root *root,struct iova *iova) +{ +struct rb_node **new = &(root->rb_node), *parent =NULL ; +/* Figure out where to put new node */ +while (*new) { + struct iova *this = container_of(*new, struct iova, node); + parent = *new; + +if (iova->pfn_lo < this->pfn_lo) + new = &((*new)->rb_left); +else if (iova->pfn_lo > this->pfn_lo) + new = &((*new)->rb_right); +else +BUG ();/* this should not happen */ + } +/* Add new node and rebalance tree. */ +rb_link_node (&iova->node, parent, new); +rb_insert_color (&iova->node, root); +} + +/** +* alloc_iova - allocates an iova +* @iovad - iova domain in question +* @size - size of page frames to allocate +* @limit_pfn - max limit address +* This function allocates an iova in the range limit_pfn to IOVA_START_PFN +* looking from limit_pfn instead from IOVA_START_PFN. +*/ + +struct iova * +alloc_iova (struct iova_domain *iovad,unsigned long size,unsigned long limit_pfn) +{ +unsigned long flags,flags1; +struct iova *new_iova; +int ret; + + new_iova =alloc_iova_mem (); +if (!new_iova) +return NULL ; + +spin_lock_irqsave (&iovad->iova_alloc_lock, flags1); + ret =__alloc_iova_range (iovad, size, limit_pfn, new_iova); + +if (ret) { +spin_unlock_irqrestore (&iovad->iova_alloc_lock, flags1); +free_iova_mem (new_iova); +return NULL ; + } + +/* Insert the new_iova into domain rbtree by holding writer lock */ +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); +iova_insert_rbtree (&iovad->rbroot, new_iova); +__cached_rbnode_insert_update (iovad, limit_pfn, new_iova); +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); + +spin_unlock_irqrestore (&iovad->iova_alloc_lock, flags1); + +return new_iova; +} + +/** +* find_iova - find's an iova for a given pfn +* @iovad - iova domain in question. +* pfn - page frame number +* This function finds and returns an iova belonging to the +* given doamin which matches the given pfn. +*/ +struct iova *find_iova (struct iova_domain *iovad,unsigned long pfn) +{ +unsigned long flags; +struct rb_node *node; + +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); + node = iovad->rbroot.rb_node; +while (node) { + struct iova *iova = container_of(node, struct iova, node); + +/* If pfn falls within iova's range, return iova */ +if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) { +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); +return iova; + } + +if (pfn < iova->pfn_lo) + node = node->rb_left; +else if (pfn > iova->pfn_lo) + node = node->rb_right; + } + +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); +return NULL ; +} + +/** +* __free_iova - frees the given iova +* @iovad: iova domain in question. +* @iova: iova in question. +* Frees the given iova belonging to the giving domain +*/ +void +__free_iova (struct iova_domain *iovad,struct iova *iova) +{ +unsigned long flags; + +if (iova) { +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); +__cached_rbnode_delete_update (iovad, iova); +rb_erase (&iova->node, &iovad->rbroot); +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); +free_iova_mem (iova); + } +} +/** +* free_iova - finds and frees the iova for a given pfn +* @iovad: - iova domain in question. +* @pfn: - pfn that is allocated previously +* This functions finds an iova for a given pfn and then +* frees the iova from that domain. +*/ + +void +free_iova (struct iova_domain *iovad,unsigned long pfn) +{ +struct iova *iova =find_iova (iovad, pfn); +__free_iova (iovad, iova); + +} + +/** +* put_iova_domain - destroys the iova doamin +* @iovad: - iova domain in question. +* All the iova's in that domain are destroyed. +*/ +void put_iova_domain (struct iova_domain *iovad) +{ +struct rb_node *node; +unsigned long flags; + +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); +while ((node =rb_first (&iovad->rbroot))) { + struct iova *iova = container_of(node, struct iova, node); +rb_erase (node, &iovad->rbroot); +free_iova_mem (iova); + } +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); +} + +static inline int +__is_range_overlap (struct rb_node *node,unsigned long pfn_lo,unsigned long pfn_hi) +{ + struct iova * iova = container_of(node, struct iova, node); + +if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo)) +return 1 ; +return 0 ; +} + +static inline struct iova * +__insert_new_range (struct iova_domain *iovad,unsigned long pfn_lo,unsigned long pfn_hi) +{ +struct iova *iova; + + iova =alloc_iova_mem (); +if (!iova) +return iova; + + iova->pfn_hi = pfn_hi; + iova->pfn_lo = pfn_lo; +iova_insert_rbtree (&iovad->rbroot,iova); +return iova; +} + +static inline void +__adjust_overlap_range (struct iova *iova,unsigned long *pfn_lo,unsigned long *pfn_hi) +{ +if (*pfn_lo < iova->pfn_lo) + iova->pfn_lo = *pfn_lo; +if (*pfn_hi > iova->pfn_hi) + *pfn_lo = iova->pfn_hi +1 ; +} + +/** +* reserve_iova - reserves an iova in the given range +* @iovad: - iova domain pointer +* @pfn_lo: - lower page frame address +* @pfn_hi:- higher pfn adderss +* This function allocates reserves the address range from pfn_lo to pfn_hi so +* that this address is not dished out as part of alloc_iova. +*/ +struct iova * +reserve_iova (struct iova_domain *iovad,unsigned long pfn_lo,unsigned long pfn_hi) +{ +struct rb_node *node; +unsigned long flags, flags1; +struct iova *iova; +unsigned int overlap =0 ; + +spin_lock_irqsave (&iovad->iova_alloc_lock, flags); +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags1); +for (node =rb_first (&iovad->rbroot); node; node =rb_next (node)) { +if (__is_range_overlap (node, pfn_lo, pfn_hi)) { + iova = container_of(node, struct iova, node); +__adjust_overlap_range (iova, &pfn_lo, &pfn_hi); +if ((pfn_lo >= iova->pfn_lo) && + (pfn_hi <= iova->pfn_hi)) + goto finish; + overlap =1 ; + + }else if (overlap) +break ; + } + +/* We are here either becasue this is the first reserver node +* or need to insert remaining non overlap addr range +*/ + iova =__insert_new_range (iovad, pfn_lo, pfn_hi); +finish: + +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags1); +spin_unlock_irqrestore (&iovad->iova_alloc_lock, flags); +return iova; +} + +/** +* copy_reserved_iova - copies the reserved between domains +* @from: - source doamin from where to copy +* @to: - destination domin where to copy +* This function copies reserved iova's from one doamin to +* other. +*/ +void +copy_reserved_iova (struct iova_domain *from,struct iova_domain *to) +{ +unsigned long flags, flags1; +struct rb_node *node; +spin_lock_irqsave (&from->iova_alloc_lock, flags); +spin_lock_irqsave (&from->iova_rbtree_lock, flags1); +for (node =rb_first (&from->rbroot); node; node =rb_next (node)) { + struct iova *iova = container_of(node, struct iova, node); +struct iova *new_iova; + new_iova =reserve_iova (to, iova->pfn_lo, iova->pfn_hi); +if (!new_iova) +printk (KERN_ERR "Reserve iova range %lx@%lx failed\n" , + iova->pfn_lo, iova->pfn_lo); + } +spin_unlock_irqrestore (&from->iova_rbtree_lock, flags1); +spin_unlock_irqrestore (&from->iova_alloc_lock, flags); +}
drivers/pci/iova.h new file +57 -0
57 lines not on this page · raw message
PATCH 6/10 Intel IOMMU driver
In reply to [email protected] Actual intel IOMMU driver. Hardware spec can be found at:
http://www.intel.com/technology/virtualization
This driver sets X86_64 'dma_ops', so hook into standard DMA APIs. In this way, PCI driver will get virtual DMA address. This change is transparent to PCI drivers.
--- Documentation/Intel-IOMMU.txt | 93 + Documentation/kernel-parameters.txt | 10 arch/x86_64/kernel/pci-dma.c | 9 drivers/pci/Makefile | 5 drivers/pci/intel-iommu.c | 1918 ++++++++++++++++++++++++++++++++++++ drivers/pci/intel-iommu.h | 296 +++++ include/linux/dmar.h | 23 7 files changed, 2353 insertions(+), 1 deletion(-)
Documentation/Intel-IOMMU.txt new file +93 -0
@@ -0,0 +1,93 @@ +Linux IOMMU Support +=================== + +The architecture spec can be obtained from the below location. + +http://www.intel.com/technology/virtualization/ + +This guide gives a quick cheat sheet for some basic understanding. + +Some Keywords + +DMAR - DMA remapping +DRHD - DMA Engine Reporting Structure +RMRR - Reserved memory Region Reporting Structure +ZLR - Zero length reads from PCI devices +IOVA - IO Virtual address. + +Basic stuff +----------- + +ACPI enumerates and lists the different DMA engines in the platform, and +device scope relationships between PCI devices and which DMA engine controls +them. + +What is RMRR? +------------- + +There are some devices the BIOS controls, for e.g USB devices to perform +PS2 emulation. The regions of memory used for these devices are marked +reserved in the e820 map. When we turn on DMA translation, DMA to those +regions will fail. Hence BIOS uses RMRR to specify these regions along with +devices that need to access these regions. OS is expected to setup +unity mappings for these regions for these devices to access these regions. + +How is IOVA generated? +--------------------- + +Well behaved drivers call pci_map_*() calls before sending command to device +that needs to perform DMA. Once DMA is completed and mapping is no longer +required, device performs a pci_unmap_*() calls to unmap the region. + +The Intel IOMMU driver allocates a virtual address per domain. Each PCIE +device has its own domain (hence protection). Devices under p2p bridges +share the virtual address with all devices under the p2p bridge due to +transaction id aliasing for p2p bridges. + +IOVA generation is pretty generic. We used the same technique as vmalloc() +but these are not global address spaces, but separate for each domain. +Different DMA engines may support different number of domains. + +We also allocate gaurd pages with each mapping, so we can attempt to catch +any overflow that might happen. + + +Graphics Problems? +------------------ +If you encounter issues with graphics devices, you can try adding +option intel_iommu=igfx_off to turn off the integrated graphics engine. + +Some exceptions to IOVA +----------------------- +Interrupt ranges are not address translated, (0xfee00000 - 0xfeefffff). +The same is true for peer to peer transactions. Hence we reserve the +address from PCI MMIO ranges so they are not allocated for IOVA addresses. + +Boot Message Sample +------------------- + +Something like this gets printed indicating presence of DMAR tables +in ACPI. + +ACPI: DMAR (v001 A M I OEMDMAR 0x00000001 MSFT 0x00000097) @ 0x000000007f5b5ef0 + +When DMAR is being processed and initialized by ACPI, prints DMAR locations +and any RMRR's processed. + +ACPI DMAR:Host address width 36 +ACPI DMAR:DRHD (flags: 0x00000000)base: 0x00000000fed90000 +ACPI DMAR:DRHD (flags: 0x00000000)base: 0x00000000fed91000 +ACPI DMAR:DRHD (flags: 0x00000001)base: 0x00000000fed93000 +ACPI DMAR:RMRR base: 0x00000000000ed000 end: 0x00000000000effff +ACPI DMAR:RMRR base: 0x000000007f600000 end: 0x000000007fffffff + +When DMAR is enabled for use, you will notice.. + +PCI-DMA: Using DMAR IOMMU + +TBD +---- + +- For compatibility testing, could use unity map domain for all devices, just + provide a 1-1 for all useful memory under a single domain for all devices. +- API for paravirt ops for abstracting functionlity for VMM folks.
Documentation/kernel-parameters.txt +10 -0
@@ -776,6 +776,16 @@ inttest= [IA64] + intel_iommu= [DMAR] Intel IOMMU driver (DMAR) option + off + Disable intel iommu driver. + igfx_off [Default Off] + By default, gfx is mapped as normal device. If a gfx + device has a dedicated DMAR unit, the DMAR unit is + bypassed by not enabling DMAR with this option. In + this case, gfx device will use physical address for + DMA. + io7= [HW] IO7 for Marvel based alpha systems See comment before marvel_specify_io7 in arch/alpha/kernel/core_marvel.c.
arch/x86_64/kernel/pci-dma.c +9 -0
@@ -7,6 +7,7 @@#include <linux/string.h> #include <linux/pci.h> #include <linux/module.h> +#include <linux/dmar.h> #include <asm/io.h> #include <asm/proto.h> #include <asm/calgary.h>
@@ -303,6 +304,10 @@detect_calgary ();#endif +#ifdef CONFIG_DMAR +detect_intel_iommu (); +#endif +#ifdef CONFIG_SWIOTLB pci_swiotlb_init ();#endif
@@ -314,6 +319,10 @@calgary_iommu_init ();#endif +#ifdef CONFIG_DMAR +intel_iommu_init (); +#endif +#ifdef CONFIG_IOMMU gart_iommu_init ();#endif
drivers/pci/Makefile +4 -1
@@ -21,7 +21,10 @@ obj-$(CONFIG_HT_IRQ) += htirq.o # Build Intel IOMMU support -obj-$(CONFIG_DMAR) += dmar.o +obj-$(CONFIG_DMAR) += dmar.o iova.o intel-iommu.o + +#Build Intel-IOMMU support +obj-$(CONFIG_DMAR) += iova.o dmar.o intel-iommu.o # # Some architectures use the generic PCI setup functions
drivers/pci/intel-iommu.c new file +1918 -0
1,918 lines not on this page · raw message
drivers/pci/intel-iommu.h new file +296 -0
296 lines not on this page · raw message
include/linux/dmar.h +23 -0
@@ -23,8 +23,15 @@#include <linux/acpi.h> #include <linux/types.h> +#include <linux/msi.h> +struct intel_iommu ; + +/* Intel IOMMU detection and initialization functions */ +extern void detect_intel_iommu (void ); +extern int intel_iommu_init (void ); +extern int dmar_table_init (void );extern int early_dmar_detect (void );
@@ -49,4 +56,20 @@int devices_cnt;/* target device count */ }; +#define for_each_drhd_unit (drhd) \ + list_for_each_entry(drhd, &dmar_drhd_units, list) +#define for_each_rmrr_units (rmrr) \ + list_for_each_entry(rmrr, &dmar_rmrr_units, list) +#define begin_for_each_rmrr_device (rmrr, pdev) \ + for_each_rmrr_units(rmrr) { \ + int _i; \ + for (_i = 0; _i < rmrr->devices_cnt; _i++) { \ + pdev = rmrr->devices[_i]; \ +/* some BIOS lists non-exist devices in DMAR table */ \ +if (!pdev) \ +continue ; +#define end_for_each_rmrr_device (rmrr, pdev) \ + } \ + } +#endif /* __DMAR_H__ */
PATCH 7/10 Intel iommu cmdline option - forcedac
In reply to [email protected] Introduce intel_iommu=forcedac commandline option.
This option is helpful to verify the pci device capability of handling physical dma'able address greater than 4G.
--- Documentation/kernel-parameters.txt | 7 +++++++ drivers/pci/intel-iommu.c | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-)
Documentation/kernel-parameters.txt +7 -0
@@ -785,6 +785,13 @@ bypassed by not enabling DMAR with this option. In this case, gfx device will use physical address for DMA. + forcedac + With this option iommu will not optimize to look + for io virtual address below 32 bit forcing dual + address cycle on pci bus for cards supporting greater + than 32 bit addressing. The default is to look + for translation below 32 bit and if not available + then look in the higher range. io7= [HW] IO7 for Marvel based alpha systems See comment before marvel_specify_io7 in
drivers/pci/intel-iommu.c +5 -1
@@ -53,6 +53,7 @@static int dmar_disabled;static int __initdata dmar_map_gfx =1 ; +static int dmar_forcedac =0 ;#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))static DEFINE_SPINLOCK (device_domain_lock);
@@ -69,6 +70,9 @@ }else if (!strncmp (str,"igfx_off" ,8 )) { dmar_map_gfx =0 ;printk (KERN_INFO "Intel-IOMMU: disable GFX device mapping\n" ); + }else if (!strncmp (str,"forcedac" ,8 )) { +printk (KERN_INFO "Intel-IOMMU: Enabling DAC for PCI supporting > 32Bit DMA\n" ); + dmar_forcedac =1 ; } str +=strcspn (str,"," );
@@ -1500,7 +1504,7 @@ start_addr =IOVA_START_ADDR ; -if (pdev->dma_mask <=DMA_32BIT_MASK ) { +if ((pdev->dma_mask <=DMA_32BIT_MASK ) || (dmar_forcedac)) { iova =iommu_alloc_iova (domain, addr, size, start_addr, pdev->dma_mask); }else {
PATCH 8/10 DMAR fault handling support
In reply to [email protected] MSI interrupt handler registrations and fault handling support
for Intel-IOMMU hadrware.
--- Documentation/Intel-IOMMU.txt | 17 +++ arch/x86_64/kernel/io_apic.c | 59 ++++++++++++ drivers/pci/intel-iommu.c | 194 ++++++++++++++++++++++++++++++++++++++++++ include/linux/dmar.h | 12 ++ 4 files changed, 281 insertions(+), 1 deletion(-)
Documentation/Intel-IOMMU.txt +17 -0
@@ -63,6 +63,15 @@ The same is true for peer to peer transactions. Hence we reserve the address from PCI MMIO ranges so they are not allocated for IOVA addresses. + +Fault reporting +--------------- +When errors are reported, the DMA engine signals via an interrupt. The fault +reason and device that caused it with fault reason is printed on console. + +See below for sample. + + Boot Message Sample -------------------
@@ -85,6 +94,14 @@ PCI-DMA: Using DMAR IOMMU +Fault reporting +--------------- + +DMAR:[DMA Write] Request device [00:02.0] fault addr 6df084000 +DMAR:[fault reason 05] PTE Write access is not set +DMAR:[DMA Write] Request device [00:02.0] fault addr 6df084000 +DMAR:[fault reason 05] PTE Write access is not set + TBD ----
arch/x86_64/kernel/io_apic.c +58 -1
72 lines not on this page · raw message
drivers/pci/intel-iommu.c +194 -0
206 lines not on this page · raw message
include/linux/dmar.h +12 -0
@@ -28,6 +28,18 @@struct intel_iommu ; +extern char *dmar_get_fault_reason (u8 fault_reason); + +/* Can't use the common MSI interrupt functions +* since DMAR is not a pci device +*/ +extern void dmar_msi_unmask (unsigned int irq); +extern void dmar_msi_mask (unsigned int irq); +extern void dmar_msi_read (int irq,struct msi_msg *msg); +extern void dmar_msi_write (int irq,struct msi_msg *msg); +extern int dmar_set_interrupt (struct intel_iommu *iommu); +extern int arch_setup_dmar_msi (unsigned int irq); +/* Intel IOMMU detection and initialization functions */ extern void detect_intel_iommu (void );extern int intel_iommu_init (void );
PATCH 9/10 Iommu Gfx workaround
In reply to [email protected]When we fix all the opensource gfx drivers to use the DMA api's, at that time we can yank this config options out.
--- Documentation/Intel-IOMMU.txt | 5 +++++ arch/x86_64/Kconfig | 11 +++++++++++ arch/x86_64/kernel/e820.c | 19 +++++++++++++++++++ drivers/pci/intel-iommu.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+)
Documentation/Intel-IOMMU.txt +5 -0
@@ -57,6 +57,11 @@ If you encounter issues with graphics devices, you can try adding option intel_iommu=igfx_off to turn off the integrated graphics engine. +If it happens to be a PCI device included in the INCLUDE_ALL Engine, +then try enabling CONFIG_DMAR_GFX_WA to setup a 1-1 map. We hear +graphics drivers may be in process of using DMA api's in the near +future and at that time this option can be yanked out. + Some exceptions to IOVA ----------------------- Interrupt ranges are not address translated, (0xfee00000 - 0xfeefffff).
arch/x86_64/Kconfig +11 -0
@@ -727,6 +727,17 @@ and includes pci device scope covered by these DMA remapping device. +config DMAR_GFX_WA + bool "Support for Graphics workaround" + depends on DMAR + default y + help + Current Graphics drivers tend to use physical address + for DMA and avoid using DMA api's. Setting this config + option permits the IOMMU driver to set a unity map for + all the OS visible memory. Hence the driver can continue + to use physical addresses for DMA. + source "drivers/pci/pcie/Kconfig" source "drivers/pci/Kconfig"
arch/x86_64/kernel/e820.c +19 -0
@@ -717,3 +717,22 @@printk (KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n" , pci_mem_start, gapstart, gapsize); } + +int __init arch_get_ram_range (int slot,u64 *addr,u64 *size) +{ +int i; + +if (slot <0 || slot >= e820.nr_map) +return -1 ; +for (i = slot; i < e820.nr_map; i++) { +if (e820.map[i].type !=E820_RAM ) +continue ; +break ; + } +if (i == e820.nr_map || e820.map[i].addr > (max_pfn <<PAGE_SHIFT )) +return -1 ; + *addr = e820.map[i].addr; + *size =min_t (u64, e820.map[i].size + e820.map[i].addr, + max_pfn <<PAGE_SHIFT ) - *addr; +return i +1 ; +}
drivers/pci/intel-iommu.c +32 -0
@@ -1556,6 +1556,34 @@ rmrr->end_address + 1); } +#ifdef CONFIG_DMAR_GFX_WA +extern int arch_get_ram_range (int slot,u64 *addr,u64 *size); +static void __init iommu_prepare_gfx_mapping (void ) +{ +struct pci_dev *pdev =NULL ; +u64 base, size; +int slot; +int ret; + +for_each_pci_dev (pdev) { +if (pdev->sysdata ==DUMMY_DEVICE_DOMAIN_INFO || + !IS_GFX_DEVICE (pdev)) +continue ; +printk (KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n" , +pci_name (pdev)); + slot =0 ; +while ((slot =arch_get_ram_range (slot, &base, &size)) >=0 ) { + ret =iommu_prepare_identity_map (pdev, base, base + size); +if (ret) + goto error; + } +continue ; +error: +printk (KERN_ERR "IOMMU: mapping reserved region failed\n" ); + } +} +#endif +int __init init_dmars (void ) {struct dmar_drhd_unit *drhd;
@@ -1611,6 +1639,10 @@printk (KERN_ERR "IOMMU: mapping reserved region failed\n" ); end_for_each_rmrr_device(rmrr, pdev) +#ifdef CONFIG_DMAR_GFX_WA +iommu_prepare_gfx_mapping (); +#endif +/* * for each drhd * enable fault log
PATCH 10/10 Iommu floppy workaround
In reply to [email protected] This config option (DMAR_FLPY_WA) sets up 1:1 mapping for the
floppy device so that the floppy device which does not use DMA api's will continue to work.
--- arch/x86_64/Kconfig | 10 ++++++++++ drivers/pci/intel-iommu.c | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+)
arch/x86_64/Kconfig +10 -0
@@ -738,6 +738,16 @@ all the OS visible memory. Hence the driver can continue to use physical addresses for DMA. +config DMAR_FLPY_WA + bool "Support for Floppy disk workaround" + depends on DMAR + default y + help + Floppy disk drivers are know to by pass dma api calls + their by failing to work when IOMMU is enabled. This + work around will setup a 1 to 1 mappings for the first + 16M to make floppy(isa device) work. + source "drivers/pci/pcie/Kconfig" source "drivers/pci/Kconfig"
drivers/pci/intel-iommu.c +23 -0
@@ -1584,6 +1584,25 @@ }#endif +#ifdef CONFIG_DMAR_FLPY_WA +static void iommu_prepare_isa (void ) +{ +struct pci_dev *pdev =NULL ; +int ret; + + pdev =pci_get_class (PCI_CLASS_BRIDGE_ISA <<8 ,NULL ); +if (!pdev) +return ; + +printk (KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n" ); + ret =iommu_prepare_identity_map (pdev,0 ,16 *1024 *1024 ); + +if (ret) +printk ("IOMMU: Failed to create 0-64M identity map, Floppy might not work\n" ); + +} +#endif +int __init init_dmars (void ) {struct dmar_drhd_unit *drhd;
@@ -1643,6 +1662,10 @@iommu_prepare_gfx_mapping ();#endif +#ifdef CONFIG_DMAR_FLPY_WA +iommu_prepare_isa (); +#endif +/* * for each drhd * enable fault log