2019-05-29 22:18:02 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
2014-03-06 01:09:32 +08:00
|
|
|
* Copyright © 2006-2014 Intel Corporation.
|
2007-10-22 07:41:49 +08:00
|
|
|
*
|
2014-03-06 01:09:32 +08:00
|
|
|
* Authors: David Woodhouse <dwmw2@infradead.org>,
|
|
|
|
* Ashok Raj <ashok.raj@intel.com>,
|
|
|
|
* Shaohua Li <shaohua.li@intel.com>,
|
|
|
|
* Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
|
|
|
|
* Fenghua Yu <fenghua.yu@intel.com>
|
2015-06-12 15:57:06 +08:00
|
|
|
* Joerg Roedel <jroedel@suse.de>
|
2007-10-22 07:41:49 +08:00
|
|
|
*/
|
|
|
|
|
2015-06-12 15:57:06 +08:00
|
|
|
#define pr_fmt(fmt) "DMAR: " fmt
|
2019-02-09 06:06:00 +08:00
|
|
|
#define dev_fmt(fmt) pr_fmt(fmt)
|
2015-06-12 15:57:06 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/bitmap.h>
|
2008-03-05 07:22:08 +08:00
|
|
|
#include <linux/debugfs.h>
|
2011-10-29 22:26:25 +08:00
|
|
|
#include <linux/export.h>
|
2007-10-22 07:41:49 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/dmar.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/mempool.h>
|
2014-02-19 14:07:37 +08:00
|
|
|
#include <linux/memory.h>
|
2016-04-20 16:33:02 +08:00
|
|
|
#include <linux/cpu.h>
|
2008-03-05 07:22:08 +08:00
|
|
|
#include <linux/timer.h>
|
2015-10-10 06:16:46 +08:00
|
|
|
#include <linux/io.h>
|
2008-09-09 23:37:29 +08:00
|
|
|
#include <linux/iova.h>
|
2008-12-03 21:52:32 +08:00
|
|
|
#include <linux/iommu.h>
|
2008-09-09 23:37:29 +08:00
|
|
|
#include <linux/intel-iommu.h>
|
2011-03-24 05:16:14 +08:00
|
|
|
#include <linux/syscore_ops.h>
|
2009-09-02 09:25:07 +08:00
|
|
|
#include <linux/tboot.h>
|
2009-08-31 13:24:23 +08:00
|
|
|
#include <linux/dmi.h>
|
2011-04-04 21:55:18 +08:00
|
|
|
#include <linux/pci-ats.h>
|
2011-12-09 02:22:09 +08:00
|
|
|
#include <linux/memblock.h>
|
2014-06-05 07:06:51 +08:00
|
|
|
#include <linux/dma-contiguous.h>
|
2018-03-19 18:38:15 +08:00
|
|
|
#include <linux/dma-direct.h>
|
2015-06-12 17:56:10 +08:00
|
|
|
#include <linux/crash_dump.h>
|
2019-03-06 07:42:58 +08:00
|
|
|
#include <linux/numa.h>
|
2019-09-06 14:14:52 +08:00
|
|
|
#include <linux/swiotlb.h>
|
2012-03-31 02:47:08 +08:00
|
|
|
#include <asm/irq_remapping.h>
|
2007-10-22 07:41:49 +08:00
|
|
|
#include <asm/cacheflush.h>
|
2008-07-11 09:23:42 +08:00
|
|
|
#include <asm/iommu.h>
|
2019-09-06 14:14:52 +08:00
|
|
|
#include <trace/events/intel_iommu.h>
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2012-09-26 18:44:43 +08:00
|
|
|
#include "irq_remapping.h"
|
2018-07-14 15:46:54 +08:00
|
|
|
#include "intel-pasid.h"
|
2012-09-26 18:44:43 +08:00
|
|
|
|
2008-10-17 09:02:32 +08:00
|
|
|
#define ROOT_SIZE VTD_PAGE_SIZE
|
|
|
|
#define CONTEXT_SIZE VTD_PAGE_SIZE
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
|
2015-03-25 23:05:47 +08:00
|
|
|
#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
|
2007-10-22 07:41:49 +08:00
|
|
|
#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
|
2009-10-01 00:12:17 +08:00
|
|
|
#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
#define IOAPIC_RANGE_START (0xfee00000)
|
|
|
|
#define IOAPIC_RANGE_END (0xfeefffff)
|
|
|
|
#define IOVA_START_ADDR (0x1000)
|
|
|
|
|
2017-12-21 03:59:24 +08:00
|
|
|
#define DEFAULT_DOMAIN_ADDRESS_WIDTH 57
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2009-04-25 08:30:20 +08:00
|
|
|
#define MAX_AGAW_WIDTH 64
|
2014-01-06 14:18:12 +08:00
|
|
|
#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
|
2009-04-25 08:30:20 +08:00
|
|
|
|
2009-09-19 22:34:04 +08:00
|
|
|
#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
|
|
|
|
#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
|
|
|
|
|
|
|
|
/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
|
|
|
|
to match. That way, we can use 'unsigned long' for PFNs with impunity. */
|
|
|
|
#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
|
|
|
|
__DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
|
|
|
|
#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-01-13 01:51:15 +08:00
|
|
|
/* IO virtual address start page frame number */
|
|
|
|
#define IOVA_START_PFN (1)
|
|
|
|
|
2008-11-20 23:49:43 +08:00
|
|
|
#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
|
2008-03-05 07:22:08 +08:00
|
|
|
|
2010-09-23 04:05:11 +08:00
|
|
|
/* page table handling */
|
|
|
|
#define LEVEL_STRIDE (9)
|
|
|
|
#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
|
|
|
|
|
2011-11-10 17:32:30 +08:00
|
|
|
/*
|
|
|
|
* This bitmap is used to advertise the page sizes our hardware support
|
|
|
|
* to the IOMMU core, which will then use this information to split
|
|
|
|
* physically contiguous memory regions it is mapping into page sizes
|
|
|
|
* that we support.
|
|
|
|
*
|
|
|
|
* Traditionally the IOMMU core just handed us the mappings directly,
|
|
|
|
* after making sure the size is an order of a 4KiB page and that the
|
|
|
|
* mapping has natural alignment.
|
|
|
|
*
|
|
|
|
* To retain this behavior, we currently advertise that we support
|
|
|
|
* all page sizes that are an order of 4KiB.
|
|
|
|
*
|
|
|
|
* If at some point we'd like to utilize the IOMMU core's new behavior,
|
|
|
|
* we could change this to advertise the real page sizes we support.
|
|
|
|
*/
|
|
|
|
#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
|
|
|
|
|
2010-09-23 04:05:11 +08:00
|
|
|
static inline int agaw_to_level(int agaw)
|
|
|
|
{
|
|
|
|
return agaw + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int agaw_to_width(int agaw)
|
|
|
|
{
|
2014-01-06 14:18:12 +08:00
|
|
|
return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
|
2010-09-23 04:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int width_to_agaw(int width)
|
|
|
|
{
|
2014-01-06 14:18:12 +08:00
|
|
|
return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
|
2010-09-23 04:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int level_to_offset_bits(int level)
|
|
|
|
{
|
|
|
|
return (level - 1) * LEVEL_STRIDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int pfn_level_offset(unsigned long pfn, int level)
|
|
|
|
{
|
|
|
|
return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long level_mask(int level)
|
|
|
|
{
|
|
|
|
return -1UL << level_to_offset_bits(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long level_size(int level)
|
|
|
|
{
|
|
|
|
return 1UL << level_to_offset_bits(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long align_to_level(unsigned long pfn, int level)
|
|
|
|
{
|
|
|
|
return (pfn + level_size(level) - 1) & level_mask(level);
|
|
|
|
}
|
2009-05-11 06:57:41 +08:00
|
|
|
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
|
|
|
|
{
|
2014-01-06 14:18:12 +08:00
|
|
|
return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
}
|
|
|
|
|
2009-06-27 23:21:20 +08:00
|
|
|
/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
|
|
|
|
are never going to work. */
|
|
|
|
static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
|
|
|
|
{
|
|
|
|
return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
|
|
|
|
{
|
|
|
|
return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
static inline unsigned long page_to_dma_pfn(struct page *pg)
|
|
|
|
{
|
|
|
|
return mm_to_dma_pfn(page_to_pfn(pg));
|
|
|
|
}
|
|
|
|
static inline unsigned long virt_to_dma_pfn(void *p)
|
|
|
|
{
|
|
|
|
return page_to_dma_pfn(virt_to_page(p));
|
|
|
|
}
|
|
|
|
|
2008-12-08 11:06:32 +08:00
|
|
|
/* global iommu list, set NULL for ignored DMAR units */
|
|
|
|
static struct intel_iommu **g_iommus;
|
|
|
|
|
2009-10-01 00:12:17 +08:00
|
|
|
static void __init check_tylersburg_isoch(void);
|
2009-02-14 07:18:03 +08:00
|
|
|
static int rwbf_quirk;
|
|
|
|
|
2011-05-03 15:08:37 +08:00
|
|
|
/*
|
|
|
|
* set to 1 to panic kernel if can't successfully enable VT-d
|
|
|
|
* (used when kernel is launched w/ TXT)
|
|
|
|
*/
|
|
|
|
static int force_on = 0;
|
2017-04-27 00:18:35 +08:00
|
|
|
int intel_iommu_tboot_noforce;
|
2018-10-23 15:45:01 +08:00
|
|
|
static int no_platform_optin;
|
2011-05-03 15:08:37 +08:00
|
|
|
|
2008-11-20 23:49:44 +08:00
|
|
|
#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
|
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
/*
|
|
|
|
* Take a root_entry and return the Lower Context Table Pointer (LCTP)
|
|
|
|
* if marked present.
|
|
|
|
*/
|
|
|
|
static phys_addr_t root_entry_lctp(struct root_entry *re)
|
|
|
|
{
|
|
|
|
if (!(re->lo & 1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return re->lo & VTD_PAGE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a root_entry and return the Upper Context Table Pointer (UCTP)
|
|
|
|
* if marked present.
|
|
|
|
*/
|
|
|
|
static phys_addr_t root_entry_uctp(struct root_entry *re)
|
|
|
|
{
|
|
|
|
if (!(re->hi & 1))
|
|
|
|
return 0;
|
2008-11-20 23:49:44 +08:00
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
return re->hi & VTD_PAGE_MASK;
|
|
|
|
}
|
2008-11-22 00:54:46 +08:00
|
|
|
|
2015-06-12 18:21:46 +08:00
|
|
|
static inline void context_clear_pasid_enable(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->lo &= ~(1ULL << 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool context_pasid_enabled(struct context_entry *context)
|
|
|
|
{
|
|
|
|
return !!(context->lo & (1ULL << 11));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void context_set_copied(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->hi |= (1ull << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool context_copied(struct context_entry *context)
|
|
|
|
{
|
|
|
|
return !!(context->hi & (1ULL << 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool __context_present(struct context_entry *context)
|
2008-11-22 00:54:46 +08:00
|
|
|
{
|
|
|
|
return (context->lo & 1);
|
|
|
|
}
|
2015-06-12 18:21:46 +08:00
|
|
|
|
2018-09-12 08:11:36 +08:00
|
|
|
bool context_present(struct context_entry *context)
|
2015-06-12 18:21:46 +08:00
|
|
|
{
|
|
|
|
return context_pasid_enabled(context) ?
|
|
|
|
__context_present(context) :
|
|
|
|
__context_present(context) && !context_copied(context);
|
|
|
|
}
|
|
|
|
|
2008-11-22 00:54:46 +08:00
|
|
|
static inline void context_set_present(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->lo |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void context_set_fault_enable(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->lo &= (((u64)-1) << 2) | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void context_set_translation_type(struct context_entry *context,
|
|
|
|
unsigned long value)
|
|
|
|
{
|
|
|
|
context->lo &= (((u64)-1) << 4) | 3;
|
|
|
|
context->lo |= (value & 3) << 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void context_set_address_root(struct context_entry *context,
|
|
|
|
unsigned long value)
|
|
|
|
{
|
x86/vt-d: Fix incorrect bit operations in setting values
The function context_set_address_root() and set_root_value are setting new
address in a wrong way, and this patch is trying to fix this problem.
According to Intel Vt-d specs(Feb 2011, Revision 1.3), Chapter 9.1 and 9.2,
field ctp in root entry is using bits 12:63, field asr in context entry is
using bits 12:63.
To set these fields, the following functions are used:
static inline void context_set_address_root(struct context_entry *context,
unsigned long value);
and
static inline void set_root_value(struct root_entry *root, unsigned long value)
But they are using an invalid method to set these fields, in current code, only
a '|' operator is used to set it. This will not set the asr to the expected
value if it has an old value.
For example:
Before calling this function,
context->lo = 0x3456789012111;
value = 0x123456789abcef12;
After we call context_set_address_root(context, value), expected result is
context->lo == 0x123456789abce111;
But the actual result is:
context->lo == 0x1237577f9bbde111;
So we need to clear bits 12:63 before setting the new value, this will fix
this problem.
Signed-off-by: Li, Zhen-Hua <zhen-hual@hp.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-11-05 15:30:19 +08:00
|
|
|
context->lo &= ~VTD_PAGE_MASK;
|
2008-11-22 00:54:46 +08:00
|
|
|
context->lo |= value & VTD_PAGE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void context_set_address_width(struct context_entry *context,
|
|
|
|
unsigned long value)
|
|
|
|
{
|
|
|
|
context->hi |= value & 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void context_set_domain_id(struct context_entry *context,
|
|
|
|
unsigned long value)
|
|
|
|
{
|
|
|
|
context->hi |= (value & ((1 << 16) - 1)) << 8;
|
|
|
|
}
|
|
|
|
|
2015-06-12 18:02:09 +08:00
|
|
|
static inline int context_domain_id(struct context_entry *c)
|
|
|
|
{
|
|
|
|
return((c->hi >> 8) & 0xffff);
|
|
|
|
}
|
|
|
|
|
2008-11-22 00:54:46 +08:00
|
|
|
static inline void context_clear_entry(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->lo = 0;
|
|
|
|
context->hi = 0;
|
|
|
|
}
|
2008-11-20 23:49:45 +08:00
|
|
|
|
2009-06-20 04:47:29 +08:00
|
|
|
/*
|
|
|
|
* This domain is a statically identity mapping domain.
|
|
|
|
* 1. This domain creats a static 1:1 mapping to all usable memory.
|
|
|
|
* 2. It maps to each iommu if successful.
|
|
|
|
* 3. Each iommu mapps to this domain if successful.
|
|
|
|
*/
|
2009-08-04 23:19:20 +08:00
|
|
|
static struct dmar_domain *si_domain;
|
|
|
|
static int hw_pass_through = 1;
|
2009-06-20 04:47:29 +08:00
|
|
|
|
|
|
|
/* si_domain contains mulitple devices */
|
2019-05-25 13:41:28 +08:00
|
|
|
#define DOMAIN_FLAG_STATIC_IDENTITY BIT(0)
|
2009-06-20 04:47:29 +08:00
|
|
|
|
2019-05-25 13:41:29 +08:00
|
|
|
/*
|
|
|
|
* This is a DMA domain allocated through the iommu domain allocation
|
|
|
|
* interface. But one or more devices belonging to this domain have
|
|
|
|
* been chosen to use a private domain. We should avoid to use the
|
|
|
|
* map/unmap/iova_to_phys APIs on it.
|
|
|
|
*/
|
|
|
|
#define DOMAIN_FLAG_LOSE_CHILDREN BIT(1)
|
|
|
|
|
2015-07-21 23:17:12 +08:00
|
|
|
#define for_each_domain_iommu(idx, domain) \
|
|
|
|
for (idx = 0; idx < g_num_of_iommus; idx++) \
|
|
|
|
if (domain->iommu_refcnt[idx])
|
|
|
|
|
2014-02-19 14:07:25 +08:00
|
|
|
struct dmar_rmrr_unit {
|
|
|
|
struct list_head list; /* list of rmrr units */
|
|
|
|
struct acpi_dmar_header *hdr; /* ACPI header */
|
|
|
|
u64 base_address; /* reserved base address*/
|
|
|
|
u64 end_address; /* reserved end address */
|
2014-03-07 23:08:36 +08:00
|
|
|
struct dmar_dev_scope *devices; /* target devices */
|
2014-02-19 14:07:25 +08:00
|
|
|
int devices_cnt; /* target device count */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dmar_atsr_unit {
|
|
|
|
struct list_head list; /* list of ATSR units */
|
|
|
|
struct acpi_dmar_header *hdr; /* ACPI header */
|
2014-03-07 23:08:36 +08:00
|
|
|
struct dmar_dev_scope *devices; /* target devices */
|
2014-02-19 14:07:25 +08:00
|
|
|
int devices_cnt; /* target device count */
|
|
|
|
u8 include_all:1; /* include all ports */
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(dmar_atsr_units);
|
|
|
|
static LIST_HEAD(dmar_rmrr_units);
|
|
|
|
|
|
|
|
#define for_each_rmrr_units(rmrr) \
|
|
|
|
list_for_each_entry(rmrr, &dmar_rmrr_units, list)
|
|
|
|
|
2008-03-05 07:22:08 +08:00
|
|
|
/* bitmap for indexing intel_iommus */
|
|
|
|
static int g_num_of_iommus;
|
|
|
|
|
2014-02-19 14:07:28 +08:00
|
|
|
static void domain_exit(struct dmar_domain *domain);
|
2007-10-22 07:41:49 +08:00
|
|
|
static void domain_remove_dev_info(struct dmar_domain *domain);
|
2019-02-09 06:06:15 +08:00
|
|
|
static void dmar_remove_one_dev_info(struct device *dev);
|
2015-07-23 23:44:46 +08:00
|
|
|
static void __dmar_remove_one_dev_info(struct device_domain_info *info);
|
2019-08-26 16:50:56 +08:00
|
|
|
static void domain_context_clear(struct intel_iommu *iommu,
|
|
|
|
struct device *dev);
|
2014-07-11 14:19:30 +08:00
|
|
|
static int domain_detach_iommu(struct dmar_domain *domain,
|
|
|
|
struct intel_iommu *iommu);
|
2019-05-25 13:41:27 +08:00
|
|
|
static bool device_is_rmrr_locked(struct device *dev);
|
2019-05-25 13:41:32 +08:00
|
|
|
static int intel_iommu_attach_device(struct iommu_domain *domain,
|
|
|
|
struct device *dev);
|
2019-09-06 14:14:52 +08:00
|
|
|
static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
|
|
|
|
dma_addr_t iova);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-08-24 08:05:25 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
|
2009-02-05 06:29:19 +08:00
|
|
|
int dmar_disabled = 0;
|
|
|
|
#else
|
|
|
|
int dmar_disabled = 1;
|
2011-08-24 08:05:25 +08:00
|
|
|
#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
|
2009-02-05 06:29:19 +08:00
|
|
|
|
2019-05-25 07:40:16 +08:00
|
|
|
int intel_iommu_sm;
|
2011-11-24 02:42:14 +08:00
|
|
|
int intel_iommu_enabled = 0;
|
|
|
|
EXPORT_SYMBOL_GPL(intel_iommu_enabled);
|
|
|
|
|
2010-06-15 17:57:57 +08:00
|
|
|
static int dmar_map_gfx = 1;
|
2007-10-22 07:41:53 +08:00
|
|
|
static int dmar_forcedac;
|
2008-03-05 07:22:08 +08:00
|
|
|
static int intel_iommu_strict;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
static int intel_iommu_superpage = 1;
|
2015-09-09 18:58:59 +08:00
|
|
|
static int iommu_identity_mapping;
|
2019-09-06 14:14:49 +08:00
|
|
|
static int intel_no_bounce;
|
2015-06-12 17:15:49 +08:00
|
|
|
|
2015-09-09 18:58:59 +08:00
|
|
|
#define IDENTMAP_ALL 1
|
|
|
|
#define IDENTMAP_GFX 2
|
|
|
|
#define IDENTMAP_AZALIA 4
|
2015-06-12 17:15:49 +08:00
|
|
|
|
2011-10-15 03:59:46 +08:00
|
|
|
int intel_iommu_gfx_mapped;
|
|
|
|
EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
|
2019-05-25 13:41:32 +08:00
|
|
|
#define DEFER_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-2))
|
2007-10-22 07:41:49 +08:00
|
|
|
static DEFINE_SPINLOCK(device_domain_lock);
|
|
|
|
static LIST_HEAD(device_domain_list);
|
|
|
|
|
2019-09-06 14:14:49 +08:00
|
|
|
#define device_needs_bounce(d) (!intel_no_bounce && dev_is_pci(d) && \
|
|
|
|
to_pci_dev(d)->untrusted)
|
|
|
|
|
2018-07-14 15:46:58 +08:00
|
|
|
/*
|
|
|
|
* Iterate over elements in device_domain_list and call the specified
|
2018-12-10 09:58:56 +08:00
|
|
|
* callback @fn against each element.
|
2018-07-14 15:46:58 +08:00
|
|
|
*/
|
|
|
|
int for_each_device_domain(int (*fn)(struct device_domain_info *info,
|
|
|
|
void *data), void *data)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2018-12-10 09:58:56 +08:00
|
|
|
unsigned long flags;
|
2018-07-14 15:46:58 +08:00
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
2018-07-14 15:46:58 +08:00
|
|
|
list_for_each_entry(info, &device_domain_list, global) {
|
|
|
|
ret = fn(info, data);
|
2018-12-10 09:58:56 +08:00
|
|
|
if (ret) {
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2018-07-14 15:46:58 +08:00
|
|
|
return ret;
|
2018-12-10 09:58:56 +08:00
|
|
|
}
|
2018-07-14 15:46:58 +08:00
|
|
|
}
|
2018-12-10 09:58:56 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2018-07-14 15:46:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-01 20:23:08 +08:00
|
|
|
const struct iommu_ops intel_iommu_ops;
|
2008-12-03 22:14:02 +08:00
|
|
|
|
2015-06-12 16:14:02 +08:00
|
|
|
static bool translation_pre_enabled(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
static void clear_translation_pre_enabled(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:14:02 +08:00
|
|
|
static void init_translation_status(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
u32 gsts;
|
|
|
|
|
|
|
|
gsts = readl(iommu->reg + DMAR_GSTS_REG);
|
|
|
|
if (gsts & DMA_GSTS_TES)
|
|
|
|
iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
|
|
|
|
}
|
|
|
|
|
2015-03-26 20:43:08 +08:00
|
|
|
/* Convert generic 'struct iommu_domain to private struct dmar_domain */
|
|
|
|
static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
|
|
|
|
{
|
|
|
|
return container_of(dom, struct dmar_domain, domain);
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static int __init intel_iommu_setup(char *str)
|
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return -EINVAL;
|
|
|
|
while (*str) {
|
2009-02-05 06:29:19 +08:00
|
|
|
if (!strncmp(str, "on", 2)) {
|
|
|
|
dmar_disabled = 0;
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("IOMMU enabled\n");
|
2009-02-05 06:29:19 +08:00
|
|
|
} else if (!strncmp(str, "off", 3)) {
|
2007-10-22 07:41:49 +08:00
|
|
|
dmar_disabled = 1;
|
2018-10-23 15:45:01 +08:00
|
|
|
no_platform_optin = 1;
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("IOMMU disabled\n");
|
2007-10-22 07:41:49 +08:00
|
|
|
} else if (!strncmp(str, "igfx_off", 8)) {
|
|
|
|
dmar_map_gfx = 0;
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("Disable GFX device mapping\n");
|
2007-10-22 07:41:53 +08:00
|
|
|
} else if (!strncmp(str, "forcedac", 8)) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("Forcing DAC for PCI devices\n");
|
2007-10-22 07:41:53 +08:00
|
|
|
dmar_forcedac = 1;
|
2008-03-05 07:22:08 +08:00
|
|
|
} else if (!strncmp(str, "strict", 6)) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("Disable batched IOTLB flush\n");
|
2008-03-05 07:22:08 +08:00
|
|
|
intel_iommu_strict = 1;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
} else if (!strncmp(str, "sp_off", 6)) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("Disable supported super page\n");
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
intel_iommu_superpage = 0;
|
2019-01-24 10:31:32 +08:00
|
|
|
} else if (!strncmp(str, "sm_on", 5)) {
|
|
|
|
pr_info("Intel-IOMMU: scalable mode supported\n");
|
|
|
|
intel_iommu_sm = 1;
|
2017-04-27 00:18:35 +08:00
|
|
|
} else if (!strncmp(str, "tboot_noforce", 13)) {
|
|
|
|
printk(KERN_INFO
|
|
|
|
"Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");
|
|
|
|
intel_iommu_tboot_noforce = 1;
|
2019-09-06 14:14:49 +08:00
|
|
|
} else if (!strncmp(str, "nobounce", 8)) {
|
|
|
|
pr_info("Intel-IOMMU: No bounce buffer. This could expose security risks of DMA attacks\n");
|
|
|
|
intel_no_bounce = 1;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
str += strcspn(str, ",");
|
|
|
|
while (*str == ',')
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__setup("intel_iommu=", intel_iommu_setup);
|
|
|
|
|
|
|
|
static struct kmem_cache *iommu_domain_cache;
|
|
|
|
static struct kmem_cache *iommu_devinfo_cache;
|
|
|
|
|
2015-07-21 16:00:56 +08:00
|
|
|
static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
|
|
|
|
{
|
2015-07-21 16:41:21 +08:00
|
|
|
struct dmar_domain **domains;
|
|
|
|
int idx = did >> 8;
|
|
|
|
|
|
|
|
domains = iommu->domains[idx];
|
|
|
|
if (!domains)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return domains[did & 0xff];
|
2015-07-21 16:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
|
|
|
|
struct dmar_domain *domain)
|
|
|
|
{
|
2015-07-21 16:41:21 +08:00
|
|
|
struct dmar_domain **domains;
|
|
|
|
int idx = did >> 8;
|
|
|
|
|
|
|
|
if (!iommu->domains[idx]) {
|
|
|
|
size_t size = 256 * sizeof(struct dmar_domain *);
|
|
|
|
iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = iommu->domains[idx];
|
|
|
|
if (WARN_ON(!domains))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
domains[did & 0xff] = domain;
|
2015-07-21 16:00:56 +08:00
|
|
|
}
|
|
|
|
|
2018-07-14 15:46:57 +08:00
|
|
|
void *alloc_pgtable_page(int node)
|
Intel IOMMU: Avoid memory allocation failures in dma map api calls
Intel IOMMU driver needs memory during DMA map calls to setup its internal
page tables and for other data structures. As we all know that these DMA map
calls are mostly called in the interrupt context or with the spinlock held by
the upper level drivers(network/storage drivers), so in order to avoid any
memory allocation failure due to low memory issues, this patch makes memory
allocation by temporarily setting PF_MEMALLOC flags for the current task
before making memory allocation calls.
We evaluated mempools as a backup when kmem_cache_alloc() fails
and found that mempools are really not useful here because
1) We don't know for sure how much to reserve in advance
2) And mempools are not useful for GFP_ATOMIC case (as we call
memory alloc functions with GFP_ATOMIC)
(akpm: point 2 is wrong...)
With PF_MEMALLOC flag set in the current->flags, the VM subsystem avoids any
watermark checks before allocating memory thus guarantee'ing the memory till
the last free page. Further, looking at the code in mm/page_alloc.c in
__alloc_pages() function, looks like this flag is useful only in the
non-interrupt context.
If we are in the interrupt context and memory allocation in IOMMU driver fails
for some reason, then the DMA map api's will return failure and it is up to
the higher level drivers to retry. Suppose, if upper level driver programs
the controller with the buggy DMA virtual address, the IOMMU will block that
DMA transaction when that happens thus preventing any corruption to main
memory.
So far in our test scenario, we were unable to create any memory allocation
failure inside dma map api calls.
Signed-off-by: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Muli Ben-Yehuda <muli@il.ibm.com>
Cc: "Siddha, Suresh B" <suresh.b.siddha@intel.com>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Ashok Raj <ashok.raj@intel.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-22 07:41:52 +08:00
|
|
|
{
|
2009-10-03 02:01:24 +08:00
|
|
|
struct page *page;
|
|
|
|
void *vaddr = NULL;
|
Intel IOMMU: Avoid memory allocation failures in dma map api calls
Intel IOMMU driver needs memory during DMA map calls to setup its internal
page tables and for other data structures. As we all know that these DMA map
calls are mostly called in the interrupt context or with the spinlock held by
the upper level drivers(network/storage drivers), so in order to avoid any
memory allocation failure due to low memory issues, this patch makes memory
allocation by temporarily setting PF_MEMALLOC flags for the current task
before making memory allocation calls.
We evaluated mempools as a backup when kmem_cache_alloc() fails
and found that mempools are really not useful here because
1) We don't know for sure how much to reserve in advance
2) And mempools are not useful for GFP_ATOMIC case (as we call
memory alloc functions with GFP_ATOMIC)
(akpm: point 2 is wrong...)
With PF_MEMALLOC flag set in the current->flags, the VM subsystem avoids any
watermark checks before allocating memory thus guarantee'ing the memory till
the last free page. Further, looking at the code in mm/page_alloc.c in
__alloc_pages() function, looks like this flag is useful only in the
non-interrupt context.
If we are in the interrupt context and memory allocation in IOMMU driver fails
for some reason, then the DMA map api's will return failure and it is up to
the higher level drivers to retry. Suppose, if upper level driver programs
the controller with the buggy DMA virtual address, the IOMMU will block that
DMA transaction when that happens thus preventing any corruption to main
memory.
So far in our test scenario, we were unable to create any memory allocation
failure inside dma map api calls.
Signed-off-by: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Muli Ben-Yehuda <muli@il.ibm.com>
Cc: "Siddha, Suresh B" <suresh.b.siddha@intel.com>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Ashok Raj <ashok.raj@intel.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-22 07:41:52 +08:00
|
|
|
|
2009-10-03 02:01:24 +08:00
|
|
|
page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
|
|
|
|
if (page)
|
|
|
|
vaddr = page_address(page);
|
Intel IOMMU: Avoid memory allocation failures in dma map api calls
Intel IOMMU driver needs memory during DMA map calls to setup its internal
page tables and for other data structures. As we all know that these DMA map
calls are mostly called in the interrupt context or with the spinlock held by
the upper level drivers(network/storage drivers), so in order to avoid any
memory allocation failure due to low memory issues, this patch makes memory
allocation by temporarily setting PF_MEMALLOC flags for the current task
before making memory allocation calls.
We evaluated mempools as a backup when kmem_cache_alloc() fails
and found that mempools are really not useful here because
1) We don't know for sure how much to reserve in advance
2) And mempools are not useful for GFP_ATOMIC case (as we call
memory alloc functions with GFP_ATOMIC)
(akpm: point 2 is wrong...)
With PF_MEMALLOC flag set in the current->flags, the VM subsystem avoids any
watermark checks before allocating memory thus guarantee'ing the memory till
the last free page. Further, looking at the code in mm/page_alloc.c in
__alloc_pages() function, looks like this flag is useful only in the
non-interrupt context.
If we are in the interrupt context and memory allocation in IOMMU driver fails
for some reason, then the DMA map api's will return failure and it is up to
the higher level drivers to retry. Suppose, if upper level driver programs
the controller with the buggy DMA virtual address, the IOMMU will block that
DMA transaction when that happens thus preventing any corruption to main
memory.
So far in our test scenario, we were unable to create any memory allocation
failure inside dma map api calls.
Signed-off-by: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Muli Ben-Yehuda <muli@il.ibm.com>
Cc: "Siddha, Suresh B" <suresh.b.siddha@intel.com>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Ashok Raj <ashok.raj@intel.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-22 07:41:52 +08:00
|
|
|
return vaddr;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2018-07-14 15:46:57 +08:00
|
|
|
void free_pgtable_page(void *vaddr)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
free_page((unsigned long)vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *alloc_domain_mem(void)
|
|
|
|
{
|
2009-11-17 15:21:09 +08:00
|
|
|
return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2008-09-09 23:37:29 +08:00
|
|
|
static void free_domain_mem(void *vaddr)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
kmem_cache_free(iommu_domain_cache, vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void * alloc_devinfo_mem(void)
|
|
|
|
{
|
2009-11-17 15:21:09 +08:00
|
|
|
return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void free_devinfo_mem(void *vaddr)
|
|
|
|
{
|
|
|
|
kmem_cache_free(iommu_devinfo_cache, vaddr);
|
|
|
|
}
|
|
|
|
|
2015-07-21 20:45:31 +08:00
|
|
|
static inline int domain_type_is_si(struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
|
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:35 +08:00
|
|
|
static inline int domain_pfn_supported(struct dmar_domain *domain,
|
|
|
|
unsigned long pfn)
|
|
|
|
{
|
|
|
|
int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
|
|
|
|
|
|
|
|
return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
|
|
|
|
}
|
|
|
|
|
2009-04-25 08:30:20 +08:00
|
|
|
static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
|
2008-12-08 15:34:06 +08:00
|
|
|
{
|
|
|
|
unsigned long sagaw;
|
|
|
|
int agaw = -1;
|
|
|
|
|
|
|
|
sagaw = cap_sagaw(iommu->cap);
|
2009-04-25 08:30:20 +08:00
|
|
|
for (agaw = width_to_agaw(max_gaw);
|
2008-12-08 15:34:06 +08:00
|
|
|
agaw >= 0; agaw--) {
|
|
|
|
if (test_bit(agaw, &sagaw))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return agaw;
|
|
|
|
}
|
|
|
|
|
2009-04-25 08:30:20 +08:00
|
|
|
/*
|
|
|
|
* Calculate max SAGAW for each iommu.
|
|
|
|
*/
|
|
|
|
int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate agaw for each iommu.
|
|
|
|
* "SAGAW" may be different across iommus, use a default agaw, and
|
|
|
|
* get a supported less agaw for iommus that don't support the default agaw.
|
|
|
|
*/
|
|
|
|
int iommu_calculate_agaw(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
|
|
|
|
}
|
|
|
|
|
2009-06-20 04:47:29 +08:00
|
|
|
/* This functionin only returns single iommu in a domain */
|
2018-07-14 15:46:57 +08:00
|
|
|
struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
|
2008-12-08 15:29:22 +08:00
|
|
|
{
|
|
|
|
int iommu_id;
|
|
|
|
|
2009-06-20 04:47:29 +08:00
|
|
|
/* si_domain and vm domain should not get here. */
|
2019-05-25 13:41:28 +08:00
|
|
|
if (WARN_ON(domain->domain.type != IOMMU_DOMAIN_DMA))
|
|
|
|
return NULL;
|
|
|
|
|
2015-07-21 23:17:12 +08:00
|
|
|
for_each_domain_iommu(iommu_id, domain)
|
|
|
|
break;
|
|
|
|
|
2008-12-08 15:29:22 +08:00
|
|
|
if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_iommus[iommu_id];
|
|
|
|
}
|
|
|
|
|
2008-12-08 15:49:06 +08:00
|
|
|
static void domain_update_iommu_coherency(struct dmar_domain *domain)
|
|
|
|
{
|
2014-03-12 08:10:29 +08:00
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
2015-02-06 17:59:53 +08:00
|
|
|
bool found = false;
|
|
|
|
int i;
|
2011-11-12 08:26:44 +08:00
|
|
|
|
2014-03-12 08:10:29 +08:00
|
|
|
domain->iommu_coherency = 1;
|
2008-12-08 15:49:06 +08:00
|
|
|
|
2015-07-21 23:17:12 +08:00
|
|
|
for_each_domain_iommu(i, domain) {
|
2015-02-06 17:59:53 +08:00
|
|
|
found = true;
|
2008-12-08 15:49:06 +08:00
|
|
|
if (!ecap_coherent(g_iommus[i]->ecap)) {
|
|
|
|
domain->iommu_coherency = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 08:10:29 +08:00
|
|
|
if (found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* No hardware attached; use lowest common denominator */
|
|
|
|
rcu_read_lock();
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
if (!ecap_coherent(iommu->ecap)) {
|
|
|
|
domain->iommu_coherency = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
2008-12-08 15:49:06 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:37 +08:00
|
|
|
static int domain_update_iommu_snooping(struct intel_iommu *skip)
|
2009-03-18 15:33:05 +08:00
|
|
|
{
|
2014-07-11 14:19:37 +08:00
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
int ret = 1;
|
2009-03-18 15:33:05 +08:00
|
|
|
|
2014-07-11 14:19:37 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
if (iommu != skip) {
|
|
|
|
if (!ecap_sc_support(iommu->ecap)) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-18 15:33:05 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 14:19:37 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
2009-03-18 15:33:05 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:37 +08:00
|
|
|
static int domain_update_iommu_superpage(struct intel_iommu *skip)
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
{
|
2011-10-15 03:32:17 +08:00
|
|
|
struct dmar_drhd_unit *drhd;
|
2014-07-11 14:19:37 +08:00
|
|
|
struct intel_iommu *iommu;
|
2011-10-15 03:32:17 +08:00
|
|
|
int mask = 0xf;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
|
|
|
|
if (!intel_iommu_superpage) {
|
2014-07-11 14:19:37 +08:00
|
|
|
return 0;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
}
|
|
|
|
|
2011-10-15 03:32:17 +08:00
|
|
|
/* set iommu_superpage to the smallest common denominator */
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_lock();
|
2011-10-15 03:32:17 +08:00
|
|
|
for_each_active_iommu(iommu, drhd) {
|
2014-07-11 14:19:37 +08:00
|
|
|
if (iommu != skip) {
|
|
|
|
mask &= cap_super_page_val(iommu->cap);
|
|
|
|
if (!mask)
|
|
|
|
break;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
2014-07-11 14:19:37 +08:00
|
|
|
return fls(mask);
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
}
|
|
|
|
|
2009-03-18 15:33:05 +08:00
|
|
|
/* Some capabilities may be different across iommus */
|
|
|
|
static void domain_update_iommu_cap(struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
domain_update_iommu_coherency(domain);
|
2014-07-11 14:19:37 +08:00
|
|
|
domain->iommu_snooping = domain_update_iommu_snooping(NULL);
|
|
|
|
domain->iommu_superpage = domain_update_iommu_superpage(NULL);
|
2009-03-18 15:33:05 +08:00
|
|
|
}
|
|
|
|
|
2018-09-12 08:11:36 +08:00
|
|
|
struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
|
|
|
|
u8 devfn, int alloc)
|
2015-02-13 22:35:21 +08:00
|
|
|
{
|
|
|
|
struct root_entry *root = &iommu->root_entry[bus];
|
|
|
|
struct context_entry *context;
|
|
|
|
u64 *entry;
|
|
|
|
|
2015-08-25 16:54:28 +08:00
|
|
|
entry = &root->lo;
|
2018-12-10 09:58:55 +08:00
|
|
|
if (sm_supported(iommu)) {
|
2015-02-13 22:35:21 +08:00
|
|
|
if (devfn >= 0x80) {
|
|
|
|
devfn -= 0x80;
|
|
|
|
entry = &root->hi;
|
|
|
|
}
|
|
|
|
devfn *= 2;
|
|
|
|
}
|
|
|
|
if (*entry & 1)
|
|
|
|
context = phys_to_virt(*entry & VTD_PAGE_MASK);
|
|
|
|
else {
|
|
|
|
unsigned long phy_addr;
|
|
|
|
if (!alloc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
context = alloc_pgtable_page(iommu->node);
|
|
|
|
if (!context)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
__iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
|
|
|
|
phy_addr = virt_to_phys((void *)context);
|
|
|
|
*entry = phy_addr | 1;
|
|
|
|
__iommu_flush_cache(iommu, entry, sizeof(*entry));
|
|
|
|
}
|
|
|
|
return &context[devfn];
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:59:20 +08:00
|
|
|
static int iommu_dummy(struct device *dev)
|
|
|
|
{
|
|
|
|
return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
|
|
|
|
}
|
|
|
|
|
2019-06-03 14:53:32 +08:00
|
|
|
/**
|
|
|
|
* is_downstream_to_pci_bridge - test if a device belongs to the PCI
|
|
|
|
* sub-hierarchy of a candidate PCI-PCI bridge
|
|
|
|
* @dev: candidate PCI device belonging to @bridge PCI sub-hierarchy
|
|
|
|
* @bridge: the candidate PCI-PCI bridge
|
|
|
|
*
|
|
|
|
* Return: true if @dev belongs to @bridge PCI sub-hierarchy, else false.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
is_downstream_to_pci_bridge(struct device *dev, struct device *bridge)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev, *pbridge;
|
|
|
|
|
|
|
|
if (!dev_is_pci(dev) || !dev_is_pci(bridge))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pdev = to_pci_dev(dev);
|
|
|
|
pbridge = to_pci_dev(bridge);
|
|
|
|
|
|
|
|
if (pbridge->subordinate &&
|
|
|
|
pbridge->subordinate->number <= pdev->bus->number &&
|
|
|
|
pbridge->subordinate->busn_res.end >= pdev->bus->number)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-10 05:00:57 +08:00
|
|
|
static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
|
2008-12-08 22:51:37 +08:00
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd = NULL;
|
2014-02-19 14:07:32 +08:00
|
|
|
struct intel_iommu *iommu;
|
2014-03-10 05:00:57 +08:00
|
|
|
struct device *tmp;
|
2019-06-03 14:53:32 +08:00
|
|
|
struct pci_dev *pdev = NULL;
|
2014-05-26 20:14:06 +08:00
|
|
|
u16 segment = 0;
|
2008-12-08 22:51:37 +08:00
|
|
|
int i;
|
|
|
|
|
2015-05-11 21:59:20 +08:00
|
|
|
if (iommu_dummy(dev))
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-10 05:00:57 +08:00
|
|
|
if (dev_is_pci(dev)) {
|
2016-10-22 06:32:05 +08:00
|
|
|
struct pci_dev *pf_pdev;
|
|
|
|
|
2014-03-10 05:00:57 +08:00
|
|
|
pdev = to_pci_dev(dev);
|
2017-08-31 05:05:59 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_X86
|
|
|
|
/* VMD child devices currently cannot be handled individually */
|
|
|
|
if (is_vmd(pdev->bus))
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
|
2016-10-22 06:32:05 +08:00
|
|
|
/* VFs aren't listed in scope tables; we need to look up
|
|
|
|
* the PF instead to find the IOMMU. */
|
|
|
|
pf_pdev = pci_physfn(pdev);
|
|
|
|
dev = &pf_pdev->dev;
|
2014-03-10 05:00:57 +08:00
|
|
|
segment = pci_domain_nr(pdev->bus);
|
2015-03-17 06:49:08 +08:00
|
|
|
} else if (has_acpi_companion(dev))
|
2014-03-10 05:00:57 +08:00
|
|
|
dev = &ACPI_COMPANION(dev)->dev;
|
|
|
|
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_lock();
|
2014-02-19 14:07:32 +08:00
|
|
|
for_each_active_iommu(iommu, drhd) {
|
2014-03-10 05:00:57 +08:00
|
|
|
if (pdev && segment != drhd->segment)
|
2009-04-04 08:45:37 +08:00
|
|
|
continue;
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2014-02-19 14:07:32 +08:00
|
|
|
for_each_active_dev_scope(drhd->devices,
|
2014-03-10 05:00:57 +08:00
|
|
|
drhd->devices_cnt, i, tmp) {
|
|
|
|
if (tmp == dev) {
|
2016-10-22 06:32:05 +08:00
|
|
|
/* For a VF use its original BDF# not that of the PF
|
|
|
|
* which we used for the IOMMU lookup. Strictly speaking
|
|
|
|
* we could do this for all PCI devices; we only need to
|
|
|
|
* get the BDF# from the scope table for ACPI matches. */
|
2017-03-02 04:02:50 +08:00
|
|
|
if (pdev && pdev->is_virtfn)
|
2016-10-22 06:32:05 +08:00
|
|
|
goto got_pdev;
|
|
|
|
|
2014-03-10 05:00:57 +08:00
|
|
|
*bus = drhd->devices[i].bus;
|
|
|
|
*devfn = drhd->devices[i].devfn;
|
2014-02-19 14:07:32 +08:00
|
|
|
goto out;
|
2014-03-10 05:00:57 +08:00
|
|
|
}
|
|
|
|
|
2019-06-03 14:53:32 +08:00
|
|
|
if (is_downstream_to_pci_bridge(dev, tmp))
|
2014-03-10 05:00:57 +08:00
|
|
|
goto got_pdev;
|
2009-04-04 07:39:25 +08:00
|
|
|
}
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2014-03-10 05:00:57 +08:00
|
|
|
if (pdev && drhd->include_all) {
|
|
|
|
got_pdev:
|
|
|
|
*bus = pdev->bus->number;
|
|
|
|
*devfn = pdev->devfn;
|
2014-02-19 14:07:32 +08:00
|
|
|
goto out;
|
2014-03-10 05:00:57 +08:00
|
|
|
}
|
2008-12-08 22:51:37 +08:00
|
|
|
}
|
2014-02-19 14:07:32 +08:00
|
|
|
iommu = NULL;
|
2014-03-10 05:00:57 +08:00
|
|
|
out:
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_unlock();
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2014-02-19 14:07:32 +08:00
|
|
|
return iommu;
|
2008-12-08 22:51:37 +08:00
|
|
|
}
|
|
|
|
|
2008-12-08 23:00:00 +08:00
|
|
|
static void domain_flush_cache(struct dmar_domain *domain,
|
|
|
|
void *addr, int size)
|
|
|
|
{
|
|
|
|
if (!domain->iommu_coherency)
|
|
|
|
clflush_cache_range(addr, size);
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
|
|
|
|
{
|
|
|
|
struct context_entry *context;
|
2015-02-13 22:35:21 +08:00
|
|
|
int ret = 0;
|
2007-10-22 07:41:49 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&iommu->lock, flags);
|
2015-02-13 22:35:21 +08:00
|
|
|
context = iommu_context_addr(iommu, bus, devfn, 0);
|
|
|
|
if (context)
|
|
|
|
ret = context_present(context);
|
2007-10-22 07:41:49 +08:00
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_context_table(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long flags;
|
|
|
|
struct context_entry *context;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&iommu->lock, flags);
|
|
|
|
if (!iommu->root_entry) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ROOT_ENTRY_NR; i++) {
|
2015-02-13 22:35:21 +08:00
|
|
|
context = iommu_context_addr(iommu, i, 0, 0);
|
2007-10-22 07:41:49 +08:00
|
|
|
if (context)
|
|
|
|
free_pgtable_page(context);
|
2015-02-13 22:35:21 +08:00
|
|
|
|
2018-12-10 09:58:55 +08:00
|
|
|
if (!sm_supported(iommu))
|
2015-02-13 22:35:21 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
context = iommu_context_addr(iommu, i, 0x80, 0);
|
|
|
|
if (context)
|
|
|
|
free_pgtable_page(context);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
free_pgtable_page(iommu->root_entry);
|
|
|
|
iommu->root_entry = NULL;
|
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
|
|
|
}
|
|
|
|
|
2009-06-28 17:37:25 +08:00
|
|
|
static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
|
2014-03-20 00:07:49 +08:00
|
|
|
unsigned long pfn, int *target_level)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
struct dma_pte *parent, *pte;
|
2007-10-22 07:41:49 +08:00
|
|
|
int level = agaw_to_level(domain->agaw);
|
2011-10-15 03:32:46 +08:00
|
|
|
int offset;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
BUG_ON(!domain->pgd);
|
2013-10-09 16:03:52 +08:00
|
|
|
|
2014-07-11 14:19:35 +08:00
|
|
|
if (!domain_pfn_supported(domain, pfn))
|
2013-10-09 16:03:52 +08:00
|
|
|
/* Address beyond IOMMU's addressing capabilities. */
|
|
|
|
return NULL;
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
parent = domain->pgd;
|
|
|
|
|
2014-03-20 00:07:49 +08:00
|
|
|
while (1) {
|
2007-10-22 07:41:49 +08:00
|
|
|
void *tmp_page;
|
|
|
|
|
2009-06-28 17:37:25 +08:00
|
|
|
offset = pfn_level_offset(pfn, level);
|
2007-10-22 07:41:49 +08:00
|
|
|
pte = &parent[offset];
|
2014-03-20 00:07:49 +08:00
|
|
|
if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
break;
|
2014-03-20 00:07:49 +08:00
|
|
|
if (level == *target_level)
|
2007-10-22 07:41:49 +08:00
|
|
|
break;
|
|
|
|
|
2008-11-22 00:56:53 +08:00
|
|
|
if (!dma_pte_present(pte)) {
|
2009-07-02 02:21:24 +08:00
|
|
|
uint64_t pteval;
|
|
|
|
|
2009-10-03 02:01:24 +08:00
|
|
|
tmp_page = alloc_pgtable_page(domain->nid);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2009-07-02 02:30:28 +08:00
|
|
|
if (!tmp_page)
|
2007-10-22 07:41:49 +08:00
|
|
|
return NULL;
|
2009-07-02 02:30:28 +08:00
|
|
|
|
2009-07-02 02:21:24 +08:00
|
|
|
domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
|
2009-09-17 09:05:55 +08:00
|
|
|
pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE;
|
2014-05-26 20:13:47 +08:00
|
|
|
if (cmpxchg64(&pte->val, 0ULL, pteval))
|
2009-07-02 02:21:24 +08:00
|
|
|
/* Someone else set it while we were thinking; use theirs. */
|
|
|
|
free_pgtable_page(tmp_page);
|
2014-05-26 20:13:47 +08:00
|
|
|
else
|
2009-07-02 02:21:24 +08:00
|
|
|
domain_flush_cache(domain, pte, sizeof(*pte));
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
2014-03-20 00:07:49 +08:00
|
|
|
if (level == 1)
|
|
|
|
break;
|
|
|
|
|
2008-11-22 00:56:53 +08:00
|
|
|
parent = phys_to_virt(dma_pte_addr(pte));
|
2007-10-22 07:41:49 +08:00
|
|
|
level--;
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:07:49 +08:00
|
|
|
if (!*target_level)
|
|
|
|
*target_level = level;
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
return pte;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return address's pte at specific level */
|
2009-06-28 00:14:59 +08:00
|
|
|
static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
|
|
|
|
unsigned long pfn,
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
int level, int *large_page)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
struct dma_pte *parent, *pte;
|
2007-10-22 07:41:49 +08:00
|
|
|
int total = agaw_to_level(domain->agaw);
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
parent = domain->pgd;
|
|
|
|
while (level <= total) {
|
2009-06-28 00:14:59 +08:00
|
|
|
offset = pfn_level_offset(pfn, total);
|
2007-10-22 07:41:49 +08:00
|
|
|
pte = &parent[offset];
|
|
|
|
if (level == total)
|
|
|
|
return pte;
|
|
|
|
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
if (!dma_pte_present(pte)) {
|
|
|
|
*large_page = total;
|
2007-10-22 07:41:49 +08:00
|
|
|
break;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 20:37:51 +08:00
|
|
|
if (dma_pte_superpage(pte)) {
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
*large_page = total;
|
|
|
|
return pte;
|
|
|
|
}
|
|
|
|
|
2008-11-22 00:56:53 +08:00
|
|
|
parent = phys_to_virt(dma_pte_addr(pte));
|
2007-10-22 07:41:49 +08:00
|
|
|
total--;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear last level pte, a tlb flush should be followed */
|
2014-03-20 00:07:49 +08:00
|
|
|
static void dma_pte_clear_range(struct dmar_domain *domain,
|
2009-06-28 05:09:11 +08:00
|
|
|
unsigned long start_pfn,
|
|
|
|
unsigned long last_pfn)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
unsigned int large_page;
|
2009-06-29 01:52:20 +08:00
|
|
|
struct dma_pte *first_pte, *pte;
|
2009-06-28 02:00:32 +08:00
|
|
|
|
2014-07-11 14:19:35 +08:00
|
|
|
BUG_ON(!domain_pfn_supported(domain, start_pfn));
|
|
|
|
BUG_ON(!domain_pfn_supported(domain, last_pfn));
|
2009-09-19 22:36:28 +08:00
|
|
|
BUG_ON(start_pfn > last_pfn);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2009-06-28 02:15:01 +08:00
|
|
|
/* we don't need lock here; nobody else touches the iova range */
|
2009-09-19 22:36:28 +08:00
|
|
|
do {
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
large_page = 1;
|
|
|
|
first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
|
2009-06-29 01:52:20 +08:00
|
|
|
if (!pte) {
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
start_pfn = align_to_level(start_pfn + 1, large_page + 1);
|
2009-06-29 01:52:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
do {
|
2009-06-29 01:52:20 +08:00
|
|
|
dma_clear_pte(pte);
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
start_pfn += lvl_to_nr_pages(large_page);
|
2009-06-29 01:52:20 +08:00
|
|
|
pte++;
|
2009-07-02 18:21:16 +08:00
|
|
|
} while (start_pfn <= last_pfn && !first_pte_in_page(pte));
|
|
|
|
|
2009-06-29 01:52:20 +08:00
|
|
|
domain_flush_cache(domain, first_pte,
|
|
|
|
(void *)pte - (void *)first_pte);
|
2009-09-19 22:36:28 +08:00
|
|
|
|
|
|
|
} while (start_pfn && start_pfn <= last_pfn);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-16 00:27:19 +08:00
|
|
|
static void dma_pte_free_level(struct dmar_domain *domain, int level,
|
2017-06-29 10:42:23 +08:00
|
|
|
int retain_level, struct dma_pte *pte,
|
|
|
|
unsigned long pfn, unsigned long start_pfn,
|
|
|
|
unsigned long last_pfn)
|
2013-06-16 00:27:19 +08:00
|
|
|
{
|
|
|
|
pfn = max(start_pfn, pfn);
|
|
|
|
pte = &pte[pfn_level_offset(pfn, level)];
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned long level_pfn;
|
|
|
|
struct dma_pte *level_pte;
|
|
|
|
|
|
|
|
if (!dma_pte_present(pte) || dma_pte_superpage(pte))
|
|
|
|
goto next;
|
|
|
|
|
2017-01-31 11:11:11 +08:00
|
|
|
level_pfn = pfn & level_mask(level);
|
2013-06-16 00:27:19 +08:00
|
|
|
level_pte = phys_to_virt(dma_pte_addr(pte));
|
|
|
|
|
2017-06-29 10:42:23 +08:00
|
|
|
if (level > 2) {
|
|
|
|
dma_pte_free_level(domain, level - 1, retain_level,
|
|
|
|
level_pte, level_pfn, start_pfn,
|
|
|
|
last_pfn);
|
|
|
|
}
|
2013-06-16 00:27:19 +08:00
|
|
|
|
2017-06-29 10:42:23 +08:00
|
|
|
/*
|
|
|
|
* Free the page table if we're below the level we want to
|
|
|
|
* retain and the range covers the entire table.
|
|
|
|
*/
|
|
|
|
if (level < retain_level && !(start_pfn > level_pfn ||
|
2014-01-22 07:48:18 +08:00
|
|
|
last_pfn < level_pfn + level_size(level) - 1)) {
|
2013-06-16 00:27:19 +08:00
|
|
|
dma_clear_pte(pte);
|
|
|
|
domain_flush_cache(domain, pte, sizeof(*pte));
|
|
|
|
free_pgtable_page(level_pte);
|
|
|
|
}
|
|
|
|
next:
|
|
|
|
pfn += level_size(level);
|
|
|
|
} while (!first_pte_in_page(++pte) && pfn <= last_pfn);
|
|
|
|
}
|
|
|
|
|
2017-06-29 10:42:23 +08:00
|
|
|
/*
|
|
|
|
* clear last level (leaf) ptes and free page table pages below the
|
|
|
|
* level we wish to keep intact.
|
|
|
|
*/
|
2007-10-22 07:41:49 +08:00
|
|
|
static void dma_pte_free_pagetable(struct dmar_domain *domain,
|
2009-06-28 07:27:49 +08:00
|
|
|
unsigned long start_pfn,
|
2017-06-29 10:42:23 +08:00
|
|
|
unsigned long last_pfn,
|
|
|
|
int retain_level)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2014-07-11 14:19:35 +08:00
|
|
|
BUG_ON(!domain_pfn_supported(domain, start_pfn));
|
|
|
|
BUG_ON(!domain_pfn_supported(domain, last_pfn));
|
2009-09-19 22:36:28 +08:00
|
|
|
BUG_ON(start_pfn > last_pfn);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2014-07-11 14:19:34 +08:00
|
|
|
dma_pte_clear_range(domain, start_pfn, last_pfn);
|
|
|
|
|
2009-06-30 10:40:07 +08:00
|
|
|
/* We don't need lock here; nobody else touches the iova range */
|
2017-06-29 10:42:23 +08:00
|
|
|
dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level,
|
2013-06-16 00:27:19 +08:00
|
|
|
domain->pgd, 0, start_pfn, last_pfn);
|
2009-06-28 05:41:00 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/* free pgd */
|
2009-06-28 07:27:49 +08:00
|
|
|
if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
|
2007-10-22 07:41:49 +08:00
|
|
|
free_pgtable_page(domain->pgd);
|
|
|
|
domain->pgd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:09:32 +08:00
|
|
|
/* When a page at a given level is being unlinked from its parent, we don't
|
|
|
|
need to *modify* it at all. All we need to do is make a list of all the
|
|
|
|
pages which can be freed just as soon as we've flushed the IOTLB and we
|
|
|
|
know the hardware page-walk will no longer touch them.
|
|
|
|
The 'pte' argument is the *parent* PTE, pointing to the page that is to
|
|
|
|
be freed. */
|
|
|
|
static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
|
|
|
|
int level, struct dma_pte *pte,
|
|
|
|
struct page *freelist)
|
|
|
|
{
|
|
|
|
struct page *pg;
|
|
|
|
|
|
|
|
pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
|
|
|
|
pg->freelist = freelist;
|
|
|
|
freelist = pg;
|
|
|
|
|
|
|
|
if (level == 1)
|
|
|
|
return freelist;
|
|
|
|
|
2014-04-09 10:20:39 +08:00
|
|
|
pte = page_address(pg);
|
|
|
|
do {
|
2014-03-06 01:09:32 +08:00
|
|
|
if (dma_pte_present(pte) && !dma_pte_superpage(pte))
|
|
|
|
freelist = dma_pte_list_pagetables(domain, level - 1,
|
|
|
|
pte, freelist);
|
2014-04-09 10:20:39 +08:00
|
|
|
pte++;
|
|
|
|
} while (!first_pte_in_page(pte));
|
2014-03-06 01:09:32 +08:00
|
|
|
|
|
|
|
return freelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
|
|
|
|
struct dma_pte *pte, unsigned long pfn,
|
|
|
|
unsigned long start_pfn,
|
|
|
|
unsigned long last_pfn,
|
|
|
|
struct page *freelist)
|
|
|
|
{
|
|
|
|
struct dma_pte *first_pte = NULL, *last_pte = NULL;
|
|
|
|
|
|
|
|
pfn = max(start_pfn, pfn);
|
|
|
|
pte = &pte[pfn_level_offset(pfn, level)];
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned long level_pfn;
|
|
|
|
|
|
|
|
if (!dma_pte_present(pte))
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
level_pfn = pfn & level_mask(level);
|
|
|
|
|
|
|
|
/* If range covers entire pagetable, free it */
|
|
|
|
if (start_pfn <= level_pfn &&
|
|
|
|
last_pfn >= level_pfn + level_size(level) - 1) {
|
|
|
|
/* These suborbinate page tables are going away entirely. Don't
|
|
|
|
bother to clear them; we're just going to *free* them. */
|
|
|
|
if (level > 1 && !dma_pte_superpage(pte))
|
|
|
|
freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
|
|
|
|
|
|
|
|
dma_clear_pte(pte);
|
|
|
|
if (!first_pte)
|
|
|
|
first_pte = pte;
|
|
|
|
last_pte = pte;
|
|
|
|
} else if (level > 1) {
|
|
|
|
/* Recurse down into a level that isn't *entirely* obsolete */
|
|
|
|
freelist = dma_pte_clear_level(domain, level - 1,
|
|
|
|
phys_to_virt(dma_pte_addr(pte)),
|
|
|
|
level_pfn, start_pfn, last_pfn,
|
|
|
|
freelist);
|
|
|
|
}
|
|
|
|
next:
|
|
|
|
pfn += level_size(level);
|
|
|
|
} while (!first_pte_in_page(++pte) && pfn <= last_pfn);
|
|
|
|
|
|
|
|
if (first_pte)
|
|
|
|
domain_flush_cache(domain, first_pte,
|
|
|
|
(void *)++last_pte - (void *)first_pte);
|
|
|
|
|
|
|
|
return freelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can't just free the pages because the IOMMU may still be walking
|
|
|
|
the page tables, and may have cached the intermediate levels. The
|
|
|
|
pages can only be freed after the IOTLB flush has been done. */
|
2015-08-13 17:32:18 +08:00
|
|
|
static struct page *domain_unmap(struct dmar_domain *domain,
|
|
|
|
unsigned long start_pfn,
|
|
|
|
unsigned long last_pfn)
|
2014-03-06 01:09:32 +08:00
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
struct page *freelist;
|
2014-03-06 01:09:32 +08:00
|
|
|
|
2014-07-11 14:19:35 +08:00
|
|
|
BUG_ON(!domain_pfn_supported(domain, start_pfn));
|
|
|
|
BUG_ON(!domain_pfn_supported(domain, last_pfn));
|
2014-03-06 01:09:32 +08:00
|
|
|
BUG_ON(start_pfn > last_pfn);
|
|
|
|
|
|
|
|
/* we don't need lock here; nobody else touches the iova range */
|
|
|
|
freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
|
|
|
|
domain->pgd, 0, start_pfn, last_pfn, NULL);
|
|
|
|
|
|
|
|
/* free pgd */
|
|
|
|
if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
|
|
|
|
struct page *pgd_page = virt_to_page(domain->pgd);
|
|
|
|
pgd_page->freelist = freelist;
|
|
|
|
freelist = pgd_page;
|
|
|
|
|
|
|
|
domain->pgd = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return freelist;
|
|
|
|
}
|
|
|
|
|
2015-08-13 17:32:18 +08:00
|
|
|
static void dma_free_pagelist(struct page *freelist)
|
2014-03-06 01:09:32 +08:00
|
|
|
{
|
|
|
|
struct page *pg;
|
|
|
|
|
|
|
|
while ((pg = freelist)) {
|
|
|
|
freelist = pg->freelist;
|
|
|
|
free_pgtable_page(page_address(pg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 17:40:10 +08:00
|
|
|
static void iova_entry_free(unsigned long data)
|
|
|
|
{
|
|
|
|
struct page *freelist = (struct page *)data;
|
|
|
|
|
|
|
|
dma_free_pagelist(freelist);
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/* iommu handling */
|
|
|
|
static int iommu_alloc_root_entry(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
struct root_entry *root;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2009-10-03 02:01:24 +08:00
|
|
|
root = (struct root_entry *)alloc_pgtable_page(iommu->node);
|
2014-11-09 22:48:02 +08:00
|
|
|
if (!root) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Allocating root entry for %s failed\n",
|
2014-11-09 22:48:02 +08:00
|
|
|
iommu->name);
|
2007-10-22 07:41:49 +08:00
|
|
|
return -ENOMEM;
|
2014-11-09 22:48:02 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2008-10-17 09:02:32 +08:00
|
|
|
__iommu_flush_cache(iommu, root, ROOT_SIZE);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&iommu->lock, flags);
|
|
|
|
iommu->root_entry = root;
|
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iommu_set_root_entry(struct intel_iommu *iommu)
|
|
|
|
{
|
2015-02-13 22:35:21 +08:00
|
|
|
u64 addr;
|
2009-05-11 03:30:58 +08:00
|
|
|
u32 sts;
|
2007-10-22 07:41:49 +08:00
|
|
|
unsigned long flag;
|
|
|
|
|
2015-02-13 22:35:21 +08:00
|
|
|
addr = virt_to_phys(iommu->root_entry);
|
2018-12-10 09:59:03 +08:00
|
|
|
if (sm_supported(iommu))
|
|
|
|
addr |= DMA_RTADDR_SMT;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2015-02-13 22:35:21 +08:00
|
|
|
dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2009-05-11 03:30:58 +08:00
|
|
|
writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
/* Make sure hardware complete it */
|
|
|
|
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
|
2009-05-11 03:30:58 +08:00
|
|
|
readl, (sts & DMA_GSTS_RTPS), sts);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:00 +08:00
|
|
|
void iommu_flush_write_buffer(struct intel_iommu *iommu)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
unsigned long flag;
|
|
|
|
|
2009-02-14 07:18:03 +08:00
|
|
|
if (!rwbf_quirk && !cap_rwbf(iommu->cap))
|
2007-10-22 07:41:49 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2009-05-11 03:18:18 +08:00
|
|
|
writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
/* Make sure hardware complete it */
|
|
|
|
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
|
2009-05-11 03:30:58 +08:00
|
|
|
readl, (!(val & DMA_GSTS_WBFS)), val);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return value determine if we need a write buffer flush */
|
2009-05-11 00:16:06 +08:00
|
|
|
static void __iommu_flush_context(struct intel_iommu *iommu,
|
|
|
|
u16 did, u16 source_id, u8 function_mask,
|
|
|
|
u64 type)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
u64 val = 0;
|
|
|
|
unsigned long flag;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case DMA_CCMD_GLOBAL_INVL:
|
|
|
|
val = DMA_CCMD_GLOBAL_INVL;
|
|
|
|
break;
|
|
|
|
case DMA_CCMD_DOMAIN_INVL:
|
|
|
|
val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
|
|
|
|
break;
|
|
|
|
case DMA_CCMD_DEVICE_INVL:
|
|
|
|
val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
|
|
|
|
| DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
val |= DMA_CCMD_ICC;
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
|
|
|
|
|
|
|
|
/* Make sure hardware complete it */
|
|
|
|
IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
|
|
|
|
dmar_readq, (!(val & DMA_CCMD_ICC)), val);
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return value determine if we need a write buffer flush */
|
2009-05-11 02:58:49 +08:00
|
|
|
static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
|
|
|
|
u64 addr, unsigned int size_order, u64 type)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
int tlb_offset = ecap_iotlb_offset(iommu->ecap);
|
|
|
|
u64 val = 0, val_iva = 0;
|
|
|
|
unsigned long flag;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case DMA_TLB_GLOBAL_FLUSH:
|
|
|
|
/* global flush doesn't need set IVA_REG */
|
|
|
|
val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
|
|
|
|
break;
|
|
|
|
case DMA_TLB_DSI_FLUSH:
|
|
|
|
val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
|
|
|
|
break;
|
|
|
|
case DMA_TLB_PSI_FLUSH:
|
|
|
|
val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
|
2014-03-06 01:09:32 +08:00
|
|
|
/* IH bit is passed in as part of address */
|
2007-10-22 07:41:49 +08:00
|
|
|
val_iva = size_order | addr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
/* Note: set drain read/write */
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* This is probably to be super secure.. Looks like we can
|
|
|
|
* ignore it without any impact.
|
|
|
|
*/
|
|
|
|
if (cap_read_drain(iommu->cap))
|
|
|
|
val |= DMA_TLB_READ_DRAIN;
|
|
|
|
#endif
|
|
|
|
if (cap_write_drain(iommu->cap))
|
|
|
|
val |= DMA_TLB_WRITE_DRAIN;
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
/* Note: Only uses first TLB reg currently */
|
|
|
|
if (val_iva)
|
|
|
|
dmar_writeq(iommu->reg + tlb_offset, val_iva);
|
|
|
|
dmar_writeq(iommu->reg + tlb_offset + 8, val);
|
|
|
|
|
|
|
|
/* Make sure hardware complete it */
|
|
|
|
IOMMU_WAIT_OP(iommu, tlb_offset + 8,
|
|
|
|
dmar_readq, (!(val & DMA_TLB_IVT)), val);
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
/* check IOTLB invalidation granularity */
|
|
|
|
if (DMA_TLB_IAIG(val) == 0)
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Flush IOTLB failed\n");
|
2007-10-22 07:41:49 +08:00
|
|
|
if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_debug("TLB flush request %Lx, actual %Lx\n",
|
2008-10-17 09:02:32 +08:00
|
|
|
(unsigned long long)DMA_TLB_IIRG(type),
|
|
|
|
(unsigned long long)DMA_TLB_IAIG(val));
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 03:52:30 +08:00
|
|
|
static struct device_domain_info *
|
|
|
|
iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
|
|
|
|
u8 bus, u8 devfn)
|
2009-05-18 13:51:37 +08:00
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
|
2009-05-18 13:51:37 +08:00
|
|
|
if (!iommu->qi)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_for_each_entry(info, &domain->devices, link)
|
2014-07-11 14:19:25 +08:00
|
|
|
if (info->iommu == iommu && info->bus == bus &&
|
|
|
|
info->devfn == devfn) {
|
2015-10-12 21:17:37 +08:00
|
|
|
if (info->ats_supported && info->dev)
|
|
|
|
return info;
|
2009-05-18 13:51:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-12 21:17:37 +08:00
|
|
|
return NULL;
|
2009-05-18 13:51:37 +08:00
|
|
|
}
|
|
|
|
|
2016-04-21 00:03:35 +08:00
|
|
|
static void domain_update_iotlb(struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
bool has_iotlb_device = false;
|
|
|
|
|
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(info, &domain->devices, link) {
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
|
|
|
if (!info->dev || !dev_is_pci(info->dev))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pdev = to_pci_dev(info->dev);
|
|
|
|
if (pdev->ats_enabled) {
|
|
|
|
has_iotlb_device = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
domain->has_iotlb_device = has_iotlb_device;
|
|
|
|
}
|
|
|
|
|
2009-05-18 13:51:37 +08:00
|
|
|
static void iommu_enable_dev_iotlb(struct device_domain_info *info)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2015-07-20 22:10:36 +08:00
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
2016-04-21 00:03:35 +08:00
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
|
2014-03-07 01:12:03 +08:00
|
|
|
if (!info || !dev_is_pci(info->dev))
|
2009-05-18 13:51:37 +08:00
|
|
|
return;
|
|
|
|
|
2015-07-20 22:10:36 +08:00
|
|
|
pdev = to_pci_dev(info->dev);
|
2018-06-08 00:57:00 +08:00
|
|
|
/* For IOMMU that supports device IOTLB throttling (DIT), we assign
|
|
|
|
* PFSID to the invalidation desc of a VF such that IOMMU HW can gauge
|
|
|
|
* queue depth at PF level. If DIT is not set, PFSID will be treated as
|
|
|
|
* reserved, which should be set to 0.
|
|
|
|
*/
|
|
|
|
if (!ecap_dit(info->iommu->ecap))
|
|
|
|
info->pfsid = 0;
|
|
|
|
else {
|
|
|
|
struct pci_dev *pf_pdev;
|
|
|
|
|
|
|
|
/* pdev will be returned if device is not a vf */
|
|
|
|
pf_pdev = pci_physfn(pdev);
|
2019-04-25 03:16:10 +08:00
|
|
|
info->pfsid = pci_dev_id(pf_pdev);
|
2018-06-08 00:57:00 +08:00
|
|
|
}
|
2015-07-20 22:10:36 +08:00
|
|
|
|
2015-10-12 21:17:37 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
|
|
|
/* The PCIe spec, in its wisdom, declares that the behaviour of
|
|
|
|
the device if you enable PASID support after ATS support is
|
|
|
|
undefined. So always enable PASID support on devices which
|
|
|
|
have it, even if we can't yet know if we're ever going to
|
|
|
|
use it. */
|
|
|
|
if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
|
|
|
|
info->pasid_enabled = 1;
|
|
|
|
|
2019-02-20 03:04:52 +08:00
|
|
|
if (info->pri_supported &&
|
|
|
|
(info->pasid_enabled ? pci_prg_resp_pasid_required(pdev) : 1) &&
|
|
|
|
!pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
|
2015-10-12 21:17:37 +08:00
|
|
|
info->pri_enabled = 1;
|
|
|
|
#endif
|
2018-10-29 18:47:08 +08:00
|
|
|
if (!pdev->untrusted && info->ats_supported &&
|
2019-02-20 03:06:10 +08:00
|
|
|
pci_ats_page_aligned(pdev) &&
|
2018-10-29 18:47:08 +08:00
|
|
|
!pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
|
2015-10-12 21:17:37 +08:00
|
|
|
info->ats_enabled = 1;
|
2016-04-21 00:03:35 +08:00
|
|
|
domain_update_iotlb(info->domain);
|
2015-10-12 21:17:37 +08:00
|
|
|
info->ats_qdep = pci_ats_queue_depth(pdev);
|
|
|
|
}
|
2009-05-18 13:51:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void iommu_disable_dev_iotlb(struct device_domain_info *info)
|
|
|
|
{
|
2015-10-12 21:17:37 +08:00
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
2016-04-21 00:03:35 +08:00
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
|
2016-01-15 13:33:06 +08:00
|
|
|
if (!dev_is_pci(info->dev))
|
2009-05-18 13:51:37 +08:00
|
|
|
return;
|
|
|
|
|
2015-10-12 21:17:37 +08:00
|
|
|
pdev = to_pci_dev(info->dev);
|
|
|
|
|
|
|
|
if (info->ats_enabled) {
|
|
|
|
pci_disable_ats(pdev);
|
|
|
|
info->ats_enabled = 0;
|
2016-04-21 00:03:35 +08:00
|
|
|
domain_update_iotlb(info->domain);
|
2015-10-12 21:17:37 +08:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
|
|
|
if (info->pri_enabled) {
|
|
|
|
pci_disable_pri(pdev);
|
|
|
|
info->pri_enabled = 0;
|
|
|
|
}
|
|
|
|
if (info->pasid_enabled) {
|
|
|
|
pci_disable_pasid(pdev);
|
|
|
|
info->pasid_enabled = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2009-05-18 13:51:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
|
|
|
|
u64 addr, unsigned mask)
|
|
|
|
{
|
|
|
|
u16 sid, qdep;
|
|
|
|
unsigned long flags;
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2016-04-21 00:03:35 +08:00
|
|
|
if (!domain->has_iotlb_device)
|
|
|
|
return;
|
|
|
|
|
2009-05-18 13:51:37 +08:00
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
list_for_each_entry(info, &domain->devices, link) {
|
2015-10-12 21:17:37 +08:00
|
|
|
if (!info->ats_enabled)
|
2009-05-18 13:51:37 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
sid = info->bus << 8 | info->devfn;
|
2015-10-12 21:17:37 +08:00
|
|
|
qdep = info->ats_qdep;
|
2018-06-08 00:57:00 +08:00
|
|
|
qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
|
|
|
|
qdep, addr, mask);
|
2009-05-18 13:51:37 +08:00
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:20:32 +08:00
|
|
|
static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
|
|
|
|
struct dmar_domain *domain,
|
|
|
|
unsigned long pfn, unsigned int pages,
|
|
|
|
int ih, int map)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2009-05-18 13:51:36 +08:00
|
|
|
unsigned int mask = ilog2(__roundup_pow_of_two(pages));
|
2009-06-28 22:33:46 +08:00
|
|
|
uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
|
2015-07-21 21:20:32 +08:00
|
|
|
u16 did = domain->iommu_did[iommu->seq_id];
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
BUG_ON(pages == 0);
|
|
|
|
|
2014-03-06 01:09:32 +08:00
|
|
|
if (ih)
|
|
|
|
ih = 1 << 6;
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
2009-05-18 13:51:36 +08:00
|
|
|
* Fallback to domain selective flush if no PSI support or the size is
|
|
|
|
* too big.
|
2007-10-22 07:41:49 +08:00
|
|
|
* PSI requires page size to be 2 ^ x, and the base address is naturally
|
|
|
|
* aligned to the size
|
|
|
|
*/
|
2009-05-18 13:51:36 +08:00
|
|
|
if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
|
|
|
|
iommu->flush.flush_iotlb(iommu, did, 0, 0,
|
2009-05-11 02:58:49 +08:00
|
|
|
DMA_TLB_DSI_FLUSH);
|
2009-05-18 13:51:36 +08:00
|
|
|
else
|
2014-03-06 01:09:32 +08:00
|
|
|
iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
|
2009-05-18 13:51:36 +08:00
|
|
|
DMA_TLB_PSI_FLUSH);
|
2009-06-29 11:31:45 +08:00
|
|
|
|
|
|
|
/*
|
2010-04-01 18:24:40 +08:00
|
|
|
* In caching mode, changes of pages from non-present to present require
|
|
|
|
* flush. However, device IOTLB doesn't need to be flushed in this case.
|
2009-06-29 11:31:45 +08:00
|
|
|
*/
|
2010-04-01 18:24:40 +08:00
|
|
|
if (!cap_caching_mode(iommu->cap) || !map)
|
iommu/vt-d: Use domain instead of cache fetching
after commit a1ddcbe93010 ("iommu/vt-d: Pass dmar_domain directly into
iommu_flush_iotlb_psi", 2015-08-12), we have domain pointer as parameter
to iommu_flush_iotlb_psi(), so no need to fetch it from cache again.
More importantly, a NULL reference pointer bug is reported on RHEL7 (and
it can be reproduced on some old upstream kernels too, e.g., v4.13) by
unplugging an 40g nic from a VM (hard to test unplug on real host, but
it should be the same):
https://bugzilla.redhat.com/show_bug.cgi?id=1531367
[ 24.391863] pciehp 0000:00:03.0:pcie004: Slot(0): Attention button pressed
[ 24.393442] pciehp 0000:00:03.0:pcie004: Slot(0): Powering off due to button press
[ 29.721068] i40evf 0000:01:00.0: Unable to send opcode 2 to PF, err I40E_ERR_QUEUE_EMPTY, aq_err OK
[ 29.783557] iommu: Removing device 0000:01:00.0 from group 3
[ 29.784662] BUG: unable to handle kernel NULL pointer dereference at 0000000000000304
[ 29.785817] IP: iommu_flush_iotlb_psi+0xcf/0x120
[ 29.786486] PGD 0
[ 29.786487] P4D 0
[ 29.786812]
[ 29.787390] Oops: 0000 [#1] SMP
[ 29.787876] Modules linked in: ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ip_set nfnetlink ebtable_nat ebtable_broute bridge stp llc ip6table_ng
[ 29.795371] CPU: 0 PID: 156 Comm: kworker/0:2 Not tainted 4.13.0 #14
[ 29.796366] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.11.0-1.el7 04/01/2014
[ 29.797593] Workqueue: pciehp-0 pciehp_power_thread
[ 29.798328] task: ffff94f5745b4a00 task.stack: ffffb326805ac000
[ 29.799178] RIP: 0010:iommu_flush_iotlb_psi+0xcf/0x120
[ 29.799919] RSP: 0018:ffffb326805afbd0 EFLAGS: 00010086
[ 29.800666] RAX: ffff94f5bc56e800 RBX: 0000000000000000 RCX: 0000000200000025
[ 29.801667] RDX: ffff94f5bc56e000 RSI: 0000000000000082 RDI: 0000000000000000
[ 29.802755] RBP: ffffb326805afbf8 R08: 0000000000000000 R09: ffff94f5bc86bbf0
[ 29.803772] R10: ffffb326805afba8 R11: 00000000000ffdc4 R12: ffff94f5bc86a400
[ 29.804789] R13: 0000000000000000 R14: 00000000ffdc4000 R15: 0000000000000000
[ 29.805792] FS: 0000000000000000(0000) GS:ffff94f5bfc00000(0000) knlGS:0000000000000000
[ 29.806923] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 29.807736] CR2: 0000000000000304 CR3: 000000003499d000 CR4: 00000000000006f0
[ 29.808747] Call Trace:
[ 29.809156] flush_unmaps_timeout+0x126/0x1c0
[ 29.809800] domain_exit+0xd6/0x100
[ 29.810322] device_notifier+0x6b/0x70
[ 29.810902] notifier_call_chain+0x4a/0x70
[ 29.812822] __blocking_notifier_call_chain+0x47/0x60
[ 29.814499] blocking_notifier_call_chain+0x16/0x20
[ 29.816137] device_del+0x233/0x320
[ 29.817588] pci_remove_bus_device+0x6f/0x110
[ 29.819133] pci_stop_and_remove_bus_device+0x1a/0x20
[ 29.820817] pciehp_unconfigure_device+0x7a/0x1d0
[ 29.822434] pciehp_disable_slot+0x52/0xe0
[ 29.823931] pciehp_power_thread+0x8a/0xa0
[ 29.825411] process_one_work+0x18c/0x3a0
[ 29.826875] worker_thread+0x4e/0x3b0
[ 29.828263] kthread+0x109/0x140
[ 29.829564] ? process_one_work+0x3a0/0x3a0
[ 29.831081] ? kthread_park+0x60/0x60
[ 29.832464] ret_from_fork+0x25/0x30
[ 29.833794] Code: 85 ed 74 0b 5b 41 5c 41 5d 41 5e 41 5f 5d c3 49 8b 54 24 60 44 89 f8 0f b6 c4 48 8b 04 c2 48 85 c0 74 49 45 0f b6 ff 4a 8b 3c f8 <80> bf
[ 29.838514] RIP: iommu_flush_iotlb_psi+0xcf/0x120 RSP: ffffb326805afbd0
[ 29.840362] CR2: 0000000000000304
[ 29.841716] ---[ end trace b10ec0d6900868d3 ]---
This patch fixes that problem if applied to v4.13 kernel.
The bug does not exist on latest upstream kernel since it's fixed as a
side effect of commit 13cf01744608 ("iommu/vt-d: Make use of iova
deferred flushing", 2017-08-15). But IMHO it's still good to have this
patch upstream.
CC: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Fixes: a1ddcbe93010 ("iommu/vt-d: Pass dmar_domain directly into iommu_flush_iotlb_psi")
Reviewed-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2018-01-10 13:51:37 +08:00
|
|
|
iommu_flush_dev_iotlb(domain, addr, mask);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2018-05-04 10:34:52 +08:00
|
|
|
/* Notification for newly created mappings */
|
|
|
|
static inline void __mapping_notify_one(struct intel_iommu *iommu,
|
|
|
|
struct dmar_domain *domain,
|
|
|
|
unsigned long pfn, unsigned int pages)
|
|
|
|
{
|
|
|
|
/* It's a non-present to present mapping. Only flush if caching mode */
|
|
|
|
if (cap_caching_mode(iommu->cap))
|
|
|
|
iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1);
|
|
|
|
else
|
|
|
|
iommu_flush_write_buffer(iommu);
|
|
|
|
}
|
|
|
|
|
2017-08-11 17:40:10 +08:00
|
|
|
static void iommu_flush_iova(struct iova_domain *iovad)
|
|
|
|
{
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
domain = container_of(iovad, struct dmar_domain, iovad);
|
|
|
|
|
|
|
|
for_each_domain_iommu(idx, domain) {
|
|
|
|
struct intel_iommu *iommu = g_iommus[idx];
|
|
|
|
u16 did = domain->iommu_did[iommu->seq_id];
|
|
|
|
|
|
|
|
iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
|
|
|
|
|
|
|
|
if (!cap_caching_mode(iommu->cap))
|
|
|
|
iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
|
|
|
|
0, MAX_AGAW_PFN_WIDTH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 20:18:38 +08:00
|
|
|
static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
u32 pmen;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2019-03-20 09:58:33 +08:00
|
|
|
if (!cap_plmr(iommu->cap) && !cap_phmr(iommu->cap))
|
|
|
|
return;
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flags);
|
2008-02-08 20:18:38 +08:00
|
|
|
pmen = readl(iommu->reg + DMAR_PMEN_REG);
|
|
|
|
pmen &= ~DMA_PMEN_EPM;
|
|
|
|
writel(pmen, iommu->reg + DMAR_PMEN_REG);
|
|
|
|
|
|
|
|
/* wait for the protected region status bit to clear */
|
|
|
|
IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
|
|
|
|
readl, !(pmen & DMA_PMEN_PRS), pmen);
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
|
2008-02-08 20:18:38 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:33 +08:00
|
|
|
static void iommu_enable_translation(struct intel_iommu *iommu)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
u32 sts;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flags);
|
2009-05-11 03:30:58 +08:00
|
|
|
iommu->gcmd |= DMA_GCMD_TE;
|
|
|
|
writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
/* Make sure hardware complete it */
|
|
|
|
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
|
2009-05-11 03:30:58 +08:00
|
|
|
readl, (sts & DMA_GSTS_TES), sts);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:33 +08:00
|
|
|
static void iommu_disable_translation(struct intel_iommu *iommu)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
u32 sts;
|
|
|
|
unsigned long flag;
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
iommu->gcmd &= ~DMA_GCMD_TE;
|
|
|
|
writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
|
|
|
|
|
|
|
|
/* Make sure hardware complete it */
|
|
|
|
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
|
2009-05-11 03:30:58 +08:00
|
|
|
readl, (!(sts & DMA_GSTS_TES)), sts);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int iommu_init_domains(struct intel_iommu *iommu)
|
|
|
|
{
|
2015-07-21 16:41:21 +08:00
|
|
|
u32 ndomains, nlongs;
|
|
|
|
size_t size;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
ndomains = cap_ndoms(iommu->cap);
|
2015-07-21 16:41:21 +08:00
|
|
|
pr_debug("%s: Number of Domains supported <%d>\n",
|
2015-06-12 15:57:06 +08:00
|
|
|
iommu->name, ndomains);
|
2007-10-22 07:41:49 +08:00
|
|
|
nlongs = BITS_TO_LONGS(ndomains);
|
|
|
|
|
2009-08-21 04:51:34 +08:00
|
|
|
spin_lock_init(&iommu->lock);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
|
|
|
|
if (!iommu->domain_ids) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("%s: Allocating domain id array failed\n",
|
|
|
|
iommu->name);
|
2007-10-22 07:41:49 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2015-07-21 16:41:21 +08:00
|
|
|
|
2016-05-21 10:41:51 +08:00
|
|
|
size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **);
|
2015-07-21 16:41:21 +08:00
|
|
|
iommu->domains = kzalloc(size, GFP_KERNEL);
|
|
|
|
|
|
|
|
if (iommu->domains) {
|
|
|
|
size = 256 * sizeof(struct dmar_domain *);
|
|
|
|
iommu->domains[0] = kzalloc(size, GFP_KERNEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iommu->domains || !iommu->domains[0]) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("%s: Allocating domain array failed\n",
|
|
|
|
iommu->name);
|
2014-01-06 14:18:11 +08:00
|
|
|
kfree(iommu->domain_ids);
|
2015-07-21 16:41:21 +08:00
|
|
|
kfree(iommu->domains);
|
2014-01-06 14:18:11 +08:00
|
|
|
iommu->domain_ids = NULL;
|
2015-07-21 16:41:21 +08:00
|
|
|
iommu->domains = NULL;
|
2007-10-22 07:41:49 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-07-21 15:39:46 +08:00
|
|
|
* If Caching mode is set, then invalid translations are tagged
|
|
|
|
* with domain-id 0, hence we need to pre-allocate it. We also
|
|
|
|
* use domain-id 0 as a marker for non-allocated domain-id, so
|
|
|
|
* make sure it is not used for a real domain.
|
2007-10-22 07:41:49 +08:00
|
|
|
*/
|
2015-07-21 15:39:46 +08:00
|
|
|
set_bit(0, iommu->domain_ids);
|
|
|
|
|
2018-12-10 09:58:59 +08:00
|
|
|
/*
|
|
|
|
* Vt-d spec rev3.0 (section 6.2.3.1) requires that each pasid
|
|
|
|
* entry for first-level or pass-through translation modes should
|
|
|
|
* be programmed with a domain id different from those used for
|
|
|
|
* second-level or nested translation. We reserve a domain id for
|
|
|
|
* this purpose.
|
|
|
|
*/
|
|
|
|
if (sm_supported(iommu))
|
|
|
|
set_bit(FLPT_DEFAULT_DID, iommu->domain_ids);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:48:02 +08:00
|
|
|
static void disable_dmar_iommu(struct intel_iommu *iommu)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2015-07-21 23:17:12 +08:00
|
|
|
struct device_domain_info *info, *tmp;
|
2015-07-22 22:50:40 +08:00
|
|
|
unsigned long flags;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-07-21 23:17:12 +08:00
|
|
|
if (!iommu->domains || !iommu->domain_ids)
|
|
|
|
return;
|
2014-02-19 14:07:30 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
2015-07-21 23:17:12 +08:00
|
|
|
list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
|
|
|
|
if (info->iommu != iommu)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!info->dev || !info->domain)
|
|
|
|
continue;
|
|
|
|
|
2016-11-08 22:08:26 +08:00
|
|
|
__dmar_remove_one_dev_info(info);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
2015-07-22 22:50:40 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
if (iommu->gcmd & DMA_GCMD_TE)
|
|
|
|
iommu_disable_translation(iommu);
|
2014-11-09 22:48:02 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2014-11-09 22:48:02 +08:00
|
|
|
static void free_dmar_iommu(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
if ((iommu->domains) && (iommu->domain_ids)) {
|
2016-05-21 10:41:51 +08:00
|
|
|
int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8;
|
2015-07-21 16:41:21 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < elems; i++)
|
|
|
|
kfree(iommu->domains[i]);
|
2014-11-09 22:48:02 +08:00
|
|
|
kfree(iommu->domains);
|
|
|
|
kfree(iommu->domain_ids);
|
|
|
|
iommu->domains = NULL;
|
|
|
|
iommu->domain_ids = NULL;
|
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2008-12-08 11:06:32 +08:00
|
|
|
g_iommus[iommu->seq_id] = NULL;
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/* free context mapping */
|
|
|
|
free_context_table(iommu);
|
2015-03-24 22:54:56 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
2018-12-10 09:58:55 +08:00
|
|
|
if (pasid_supported(iommu)) {
|
2015-10-08 06:35:18 +08:00
|
|
|
if (ecap_prs(iommu->ecap))
|
|
|
|
intel_svm_finish_prq(iommu);
|
|
|
|
}
|
2015-03-24 22:54:56 +08:00
|
|
|
#endif
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:27 +08:00
|
|
|
static struct dmar_domain *alloc_domain(int flags)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
|
|
|
|
domain = alloc_domain_mem();
|
|
|
|
if (!domain)
|
|
|
|
return NULL;
|
|
|
|
|
2014-07-11 14:19:27 +08:00
|
|
|
memset(domain, 0, sizeof(*domain));
|
2019-03-06 07:42:58 +08:00
|
|
|
domain->nid = NUMA_NO_NODE;
|
2014-07-11 14:19:27 +08:00
|
|
|
domain->flags = flags;
|
2016-04-21 00:03:35 +08:00
|
|
|
domain->has_iotlb_device = false;
|
2014-02-19 14:07:28 +08:00
|
|
|
INIT_LIST_HEAD(&domain->devices);
|
2009-06-20 04:47:29 +08:00
|
|
|
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
2015-07-22 17:52:53 +08:00
|
|
|
/* Must be called with iommu->lock */
|
|
|
|
static int domain_attach_iommu(struct dmar_domain *domain,
|
2014-07-11 14:19:28 +08:00
|
|
|
struct intel_iommu *iommu)
|
|
|
|
{
|
2014-07-11 14:19:29 +08:00
|
|
|
unsigned long ndomains;
|
2015-07-22 22:50:40 +08:00
|
|
|
int num;
|
2014-07-11 14:19:29 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
assert_spin_locked(&device_domain_lock);
|
2015-07-22 17:52:53 +08:00
|
|
|
assert_spin_locked(&iommu->lock);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-07-21 23:17:12 +08:00
|
|
|
domain->iommu_refcnt[iommu->seq_id] += 1;
|
|
|
|
domain->iommu_count += 1;
|
|
|
|
if (domain->iommu_refcnt[iommu->seq_id] == 1) {
|
2014-07-11 14:19:28 +08:00
|
|
|
ndomains = cap_ndoms(iommu->cap);
|
2015-07-22 17:52:53 +08:00
|
|
|
num = find_first_zero_bit(iommu->domain_ids, ndomains);
|
|
|
|
|
|
|
|
if (num >= ndomains) {
|
|
|
|
pr_err("%s: No free domain ids\n", iommu->name);
|
|
|
|
domain->iommu_refcnt[iommu->seq_id] -= 1;
|
|
|
|
domain->iommu_count -= 1;
|
2015-07-22 22:50:40 +08:00
|
|
|
return -ENOSPC;
|
2009-06-20 04:47:29 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-07-22 17:52:53 +08:00
|
|
|
set_bit(num, iommu->domain_ids);
|
|
|
|
set_iommu_domain(iommu, num, domain);
|
|
|
|
|
|
|
|
domain->iommu_did[iommu->seq_id] = num;
|
|
|
|
domain->nid = iommu->node;
|
2014-07-11 14:19:28 +08:00
|
|
|
|
|
|
|
domain_update_iommu_cap(domain);
|
|
|
|
}
|
2015-07-22 17:52:53 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
return 0;
|
2014-07-11 14:19:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int domain_detach_iommu(struct dmar_domain *domain,
|
|
|
|
struct intel_iommu *iommu)
|
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
int num, count;
|
2015-07-22 17:52:53 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
assert_spin_locked(&device_domain_lock);
|
2015-07-22 17:52:53 +08:00
|
|
|
assert_spin_locked(&iommu->lock);
|
2014-07-11 14:19:28 +08:00
|
|
|
|
2015-07-21 23:17:12 +08:00
|
|
|
domain->iommu_refcnt[iommu->seq_id] -= 1;
|
|
|
|
count = --domain->iommu_count;
|
|
|
|
if (domain->iommu_refcnt[iommu->seq_id] == 0) {
|
2015-07-22 17:52:53 +08:00
|
|
|
num = domain->iommu_did[iommu->seq_id];
|
|
|
|
clear_bit(num, iommu->domain_ids);
|
|
|
|
set_iommu_domain(iommu, num, NULL);
|
2014-07-11 14:19:28 +08:00
|
|
|
|
|
|
|
domain_update_iommu_cap(domain);
|
2015-07-21 15:39:46 +08:00
|
|
|
domain->iommu_did[iommu->seq_id] = 0;
|
2014-07-11 14:19:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static struct iova_domain reserved_iova_list;
|
2008-03-05 06:59:31 +08:00
|
|
|
static struct lock_class_key reserved_rbtree_key;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-03-22 02:04:24 +08:00
|
|
|
static int dmar_init_reserved_ranges(void)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
struct pci_dev *pdev = NULL;
|
|
|
|
struct iova *iova;
|
|
|
|
int i;
|
|
|
|
|
2017-09-21 23:52:45 +08:00
|
|
|
init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2008-03-05 06:59:31 +08:00
|
|
|
lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
|
|
|
|
&reserved_rbtree_key);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/* IOAPIC ranges shouldn't be accessed by DMA */
|
|
|
|
iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
|
|
|
|
IOVA_PFN(IOAPIC_RANGE_END));
|
2011-03-22 02:04:24 +08:00
|
|
|
if (!iova) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Reserve IOAPIC range failed\n");
|
2011-03-22 02:04:24 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
/* Reserve all PCI MMIO to avoid peer-to-peer access */
|
|
|
|
for_each_pci_dev(pdev) {
|
|
|
|
struct resource *r;
|
|
|
|
|
|
|
|
for (i = 0; i < PCI_NUM_RESOURCES; i++) {
|
|
|
|
r = &pdev->resource[i];
|
|
|
|
if (!r->flags || !(r->flags & IORESOURCE_MEM))
|
|
|
|
continue;
|
2009-06-28 23:00:42 +08:00
|
|
|
iova = reserve_iova(&reserved_iova_list,
|
|
|
|
IOVA_PFN(r->start),
|
|
|
|
IOVA_PFN(r->end));
|
2011-03-22 02:04:24 +08:00
|
|
|
if (!iova) {
|
2019-02-09 06:06:00 +08:00
|
|
|
pci_err(pdev, "Reserve iova for %pR failed\n", r);
|
2011-03-22 02:04:24 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
}
|
2011-03-22 02:04:24 +08:00
|
|
|
return 0;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void domain_reserve_special_ranges(struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
copy_reserved_iova(&reserved_iova_list, &domain->iovad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int guestwidth_to_adjustwidth(int gaw)
|
|
|
|
{
|
|
|
|
int agaw;
|
|
|
|
int r = (gaw - 12) % 9;
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
agaw = gaw;
|
|
|
|
else
|
|
|
|
agaw = gaw + 9 - r;
|
|
|
|
if (agaw > 64)
|
|
|
|
agaw = 64;
|
|
|
|
return agaw;
|
|
|
|
}
|
|
|
|
|
2019-07-22 22:21:05 +08:00
|
|
|
static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
|
|
|
|
int guest_width)
|
|
|
|
{
|
|
|
|
int adjust_width, agaw;
|
|
|
|
unsigned long sagaw;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
|
|
|
|
|
|
|
|
err = init_iova_flush_queue(&domain->iovad,
|
|
|
|
iommu_flush_iova, iova_entry_free);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
domain_reserve_special_ranges(domain);
|
|
|
|
|
|
|
|
/* calculate AGAW */
|
|
|
|
if (guest_width > cap_mgaw(iommu->cap))
|
|
|
|
guest_width = cap_mgaw(iommu->cap);
|
|
|
|
domain->gaw = guest_width;
|
|
|
|
adjust_width = guestwidth_to_adjustwidth(guest_width);
|
|
|
|
agaw = width_to_agaw(adjust_width);
|
|
|
|
sagaw = cap_sagaw(iommu->cap);
|
|
|
|
if (!test_bit(agaw, &sagaw)) {
|
|
|
|
/* hardware doesn't support it, choose a bigger one */
|
|
|
|
pr_debug("Hardware doesn't support agaw %d\n", agaw);
|
|
|
|
agaw = find_next_bit(&sagaw, 5, agaw);
|
|
|
|
if (agaw >= 5)
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
domain->agaw = agaw;
|
|
|
|
|
|
|
|
if (ecap_coherent(iommu->ecap))
|
|
|
|
domain->iommu_coherency = 1;
|
|
|
|
else
|
|
|
|
domain->iommu_coherency = 0;
|
|
|
|
|
|
|
|
if (ecap_sc_support(iommu->ecap))
|
|
|
|
domain->iommu_snooping = 1;
|
|
|
|
else
|
|
|
|
domain->iommu_snooping = 0;
|
|
|
|
|
|
|
|
if (intel_iommu_superpage)
|
|
|
|
domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
|
|
|
|
else
|
|
|
|
domain->iommu_superpage = 0;
|
|
|
|
|
|
|
|
domain->nid = iommu->node;
|
|
|
|
|
|
|
|
/* always allocate the top pgd */
|
|
|
|
domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
|
|
|
|
if (!domain->pgd)
|
|
|
|
return -ENOMEM;
|
|
|
|
__iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static void domain_exit(struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
|
2015-07-22 17:52:53 +08:00
|
|
|
/* Remove associated devices and clear attached or cached domains */
|
2007-10-22 07:41:49 +08:00
|
|
|
domain_remove_dev_info(domain);
|
2014-02-19 14:07:28 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/* destroy iovas */
|
|
|
|
put_iova_domain(&domain->iovad);
|
|
|
|
|
2019-07-17 05:38:06 +08:00
|
|
|
if (domain->pgd) {
|
|
|
|
struct page *freelist;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2019-07-17 05:38:06 +08:00
|
|
|
freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
|
|
|
|
dma_free_pagelist(freelist);
|
|
|
|
}
|
2014-03-06 01:09:32 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
free_domain_mem(domain);
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:59:03 +08:00
|
|
|
/*
|
|
|
|
* Get the PASID directory size for scalable mode context entry.
|
|
|
|
* Value of X in the PDTS field of a scalable mode context entry
|
|
|
|
* indicates PASID directory with 2^(X + 7) entries.
|
|
|
|
*/
|
|
|
|
static inline unsigned long context_get_sm_pds(struct pasid_table *table)
|
|
|
|
{
|
|
|
|
int pds, max_pde;
|
|
|
|
|
|
|
|
max_pde = table->max_pasid >> PASID_PDE_SHIFT;
|
|
|
|
pds = find_first_bit((unsigned long *)&max_pde, MAX_NR_PASID_BITS);
|
|
|
|
if (pds < 7)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return pds - 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the RID_PASID field of a scalable mode context entry. The
|
|
|
|
* IOMMU hardware will use the PASID value set in this field for
|
|
|
|
* DMA translations of DMA requests without PASID.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
context_set_sm_rid2pasid(struct context_entry *context, unsigned long pasid)
|
|
|
|
{
|
|
|
|
context->hi |= pasid & ((1 << 20) - 1);
|
|
|
|
context->hi |= (1 << 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the DTE(Device-TLB Enable) field of a scalable mode context
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
static inline void context_set_sm_dte(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->lo |= (1 << 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the PRE(Page Request Enable) field of a scalable mode context
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
static inline void context_set_sm_pre(struct context_entry *context)
|
|
|
|
{
|
|
|
|
context->lo |= (1 << 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert value to context PASID directory size field coding. */
|
|
|
|
#define context_pdts(pds) (((pds) & 0x7) << 9)
|
|
|
|
|
2014-03-10 03:52:30 +08:00
|
|
|
static int domain_context_mapping_one(struct dmar_domain *domain,
|
|
|
|
struct intel_iommu *iommu,
|
2018-12-10 09:59:02 +08:00
|
|
|
struct pasid_table *table,
|
2015-07-21 20:45:31 +08:00
|
|
|
u8 bus, u8 devfn)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2015-07-22 19:11:53 +08:00
|
|
|
u16 did = domain->iommu_did[iommu->seq_id];
|
2015-07-21 20:45:31 +08:00
|
|
|
int translation = CONTEXT_TT_MULTI_LEVEL;
|
|
|
|
struct device_domain_info *info = NULL;
|
2007-10-22 07:41:49 +08:00
|
|
|
struct context_entry *context;
|
|
|
|
unsigned long flags;
|
2018-12-10 09:59:03 +08:00
|
|
|
int ret;
|
2015-07-21 20:45:31 +08:00
|
|
|
|
2015-07-22 19:11:53 +08:00
|
|
|
WARN_ON(did == 0);
|
|
|
|
|
2015-07-21 20:45:31 +08:00
|
|
|
if (hw_pass_through && domain_type_is_si(domain))
|
|
|
|
translation = CONTEXT_TT_PASS_THROUGH;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
pr_debug("Set context mapping for %02x:%02x.%d\n",
|
|
|
|
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
|
2009-04-25 08:30:20 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
BUG_ON(!domain->pgd);
|
2008-12-08 23:00:00 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
|
|
|
|
ret = -ENOMEM;
|
2015-02-13 22:35:21 +08:00
|
|
|
context = iommu_context_addr(iommu, bus, devfn, 1);
|
2007-10-22 07:41:49 +08:00
|
|
|
if (!context)
|
2015-07-22 22:50:40 +08:00
|
|
|
goto out_unlock;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
ret = 0;
|
|
|
|
if (context_present(context))
|
|
|
|
goto out_unlock;
|
2015-06-12 18:21:46 +08:00
|
|
|
|
2016-12-05 20:09:07 +08:00
|
|
|
/*
|
|
|
|
* For kdump cases, old valid entries may be cached due to the
|
|
|
|
* in-flight DMA and copied pgtable, but there is no unmapping
|
|
|
|
* behaviour for them, thus we need an explicit cache flush for
|
|
|
|
* the newly-mapped device. For kdump, at this point, the device
|
|
|
|
* is supposed to finish reset at its driver probe stage, so no
|
|
|
|
* in-flight DMA will exist, and we don't need to worry anymore
|
|
|
|
* hereafter.
|
|
|
|
*/
|
|
|
|
if (context_copied(context)) {
|
|
|
|
u16 did_old = context_domain_id(context);
|
|
|
|
|
2017-10-09 06:33:31 +08:00
|
|
|
if (did_old < cap_ndoms(iommu->cap)) {
|
2016-12-05 20:09:07 +08:00
|
|
|
iommu->flush.flush_context(iommu, did_old,
|
|
|
|
(((u16)bus) << 8) | devfn,
|
|
|
|
DMA_CCMD_MASK_NOBIT,
|
|
|
|
DMA_CCMD_DEVICE_INVL);
|
2017-05-06 02:39:59 +08:00
|
|
|
iommu->flush.flush_iotlb(iommu, did_old, 0, 0,
|
|
|
|
DMA_TLB_DSI_FLUSH);
|
|
|
|
}
|
2016-12-05 20:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-21 20:53:04 +08:00
|
|
|
context_clear_entry(context);
|
2008-12-08 23:08:15 +08:00
|
|
|
|
2018-12-10 09:59:03 +08:00
|
|
|
if (sm_supported(iommu)) {
|
|
|
|
unsigned long pds;
|
2009-04-25 08:30:20 +08:00
|
|
|
|
2018-12-10 09:59:03 +08:00
|
|
|
WARN_ON(!table);
|
|
|
|
|
|
|
|
/* Setup the PASID DIR pointer: */
|
|
|
|
pds = context_get_sm_pds(table);
|
|
|
|
context->lo = (u64)virt_to_phys(table->table) |
|
|
|
|
context_pdts(pds);
|
|
|
|
|
|
|
|
/* Setup the RID_PASID field: */
|
|
|
|
context_set_sm_rid2pasid(context, PASID_RID2PASID);
|
2015-07-21 20:53:04 +08:00
|
|
|
|
|
|
|
/*
|
2018-12-10 09:59:03 +08:00
|
|
|
* Setup the Device-TLB enable bit and Page request
|
|
|
|
* Enable bit:
|
2015-07-21 20:53:04 +08:00
|
|
|
*/
|
2018-12-10 09:59:03 +08:00
|
|
|
info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
|
|
|
|
if (info && info->ats_supported)
|
|
|
|
context_set_sm_dte(context);
|
|
|
|
if (info && info->pri_supported)
|
|
|
|
context_set_sm_pre(context);
|
|
|
|
} else {
|
|
|
|
struct dma_pte *pgd = domain->pgd;
|
|
|
|
int agaw;
|
|
|
|
|
|
|
|
context_set_domain_id(context, did);
|
|
|
|
|
|
|
|
if (translation != CONTEXT_TT_PASS_THROUGH) {
|
|
|
|
/*
|
|
|
|
* Skip top levels of page tables for iommu which has
|
|
|
|
* less agaw than default. Unnecessary for PT mode.
|
|
|
|
*/
|
|
|
|
for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
pgd = phys_to_virt(dma_pte_addr(pgd));
|
|
|
|
if (!dma_pte_present(pgd))
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
|
|
|
|
if (info && info->ats_supported)
|
|
|
|
translation = CONTEXT_TT_DEV_IOTLB;
|
|
|
|
else
|
|
|
|
translation = CONTEXT_TT_MULTI_LEVEL;
|
|
|
|
|
|
|
|
context_set_address_root(context, virt_to_phys(pgd));
|
|
|
|
context_set_address_width(context, agaw);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* In pass through mode, AW must be programmed to
|
|
|
|
* indicate the largest AGAW value supported by
|
|
|
|
* hardware. And ASR is ignored by hardware.
|
|
|
|
*/
|
|
|
|
context_set_address_width(context, iommu->msagaw);
|
|
|
|
}
|
2019-03-01 11:23:11 +08:00
|
|
|
|
|
|
|
context_set_translation_type(context, translation);
|
2009-05-18 13:51:37 +08:00
|
|
|
}
|
2009-04-25 08:30:20 +08:00
|
|
|
|
2008-11-22 00:54:46 +08:00
|
|
|
context_set_fault_enable(context);
|
|
|
|
context_set_present(context);
|
2008-12-08 23:00:00 +08:00
|
|
|
domain_flush_cache(domain, context, sizeof(*context));
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2009-05-11 00:16:06 +08:00
|
|
|
/*
|
|
|
|
* It's a non-present to present mapping. If hardware doesn't cache
|
|
|
|
* non-present entry we only need to flush the write-buffer. If the
|
|
|
|
* _does_ cache non-present entries, then it does so in the special
|
|
|
|
* domain #0, which we have to flush:
|
|
|
|
*/
|
|
|
|
if (cap_caching_mode(iommu->cap)) {
|
|
|
|
iommu->flush.flush_context(iommu, 0,
|
|
|
|
(((u16)bus) << 8) | devfn,
|
|
|
|
DMA_CCMD_MASK_NOBIT,
|
|
|
|
DMA_CCMD_DEVICE_INVL);
|
2015-07-22 19:11:53 +08:00
|
|
|
iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
|
2009-05-11 00:16:06 +08:00
|
|
|
} else {
|
2007-10-22 07:41:49 +08:00
|
|
|
iommu_flush_write_buffer(iommu);
|
2009-05-11 00:16:06 +08:00
|
|
|
}
|
2009-05-18 13:51:37 +08:00
|
|
|
iommu_enable_dev_iotlb(info);
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2014-07-11 14:19:28 +08:00
|
|
|
|
2016-07-13 21:53:21 +08:00
|
|
|
return ret;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2019-08-26 16:50:56 +08:00
|
|
|
struct domain_context_mapping_data {
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
struct pasid_table *table;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int domain_context_mapping_cb(struct pci_dev *pdev,
|
|
|
|
u16 alias, void *opaque)
|
|
|
|
{
|
|
|
|
struct domain_context_mapping_data *data = opaque;
|
|
|
|
|
|
|
|
return domain_context_mapping_one(data->domain, data->iommu,
|
|
|
|
data->table, PCI_BUS_NUM(alias),
|
|
|
|
alias & 0xff);
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static int
|
2015-07-21 20:45:31 +08:00
|
|
|
domain_context_mapping(struct dmar_domain *domain, struct device *dev)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2019-08-26 16:50:56 +08:00
|
|
|
struct domain_context_mapping_data data;
|
2018-12-10 09:59:02 +08:00
|
|
|
struct pasid_table *table;
|
2014-03-10 03:52:30 +08:00
|
|
|
struct intel_iommu *iommu;
|
2014-03-10 05:00:57 +08:00
|
|
|
u8 bus, devfn;
|
2014-03-10 03:52:30 +08:00
|
|
|
|
2014-03-10 06:24:46 +08:00
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
2014-03-10 03:52:30 +08:00
|
|
|
if (!iommu)
|
|
|
|
return -ENODEV;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2018-12-10 09:59:02 +08:00
|
|
|
table = intel_pasid_get_table(dev);
|
2019-08-26 16:50:56 +08:00
|
|
|
|
|
|
|
if (!dev_is_pci(dev))
|
|
|
|
return domain_context_mapping_one(domain, iommu, table,
|
|
|
|
bus, devfn);
|
|
|
|
|
|
|
|
data.domain = domain;
|
|
|
|
data.iommu = iommu;
|
|
|
|
data.table = table;
|
|
|
|
|
|
|
|
return pci_for_each_dma_alias(to_pci_dev(dev),
|
|
|
|
&domain_context_mapping_cb, &data);
|
2014-07-03 23:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int domain_context_mapped_cb(struct pci_dev *pdev,
|
|
|
|
u16 alias, void *opaque)
|
|
|
|
{
|
|
|
|
struct intel_iommu *iommu = opaque;
|
|
|
|
|
|
|
|
return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 06:24:46 +08:00
|
|
|
static int domain_context_mapped(struct device *dev)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2008-12-08 23:00:00 +08:00
|
|
|
struct intel_iommu *iommu;
|
2014-03-10 05:00:57 +08:00
|
|
|
u8 bus, devfn;
|
2008-12-08 23:00:00 +08:00
|
|
|
|
2014-03-10 06:24:46 +08:00
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
2008-12-08 23:00:00 +08:00
|
|
|
if (!iommu)
|
|
|
|
return -ENODEV;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2014-07-03 23:51:43 +08:00
|
|
|
if (!dev_is_pci(dev))
|
|
|
|
return device_context_mapped(iommu, bus, devfn);
|
2014-03-10 06:24:46 +08:00
|
|
|
|
2014-07-03 23:51:43 +08:00
|
|
|
return !pci_for_each_dma_alias(to_pci_dev(dev),
|
|
|
|
domain_context_mapped_cb, iommu);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2009-08-05 06:09:37 +08:00
|
|
|
/* Returns a number of VTD pages, but aligned to MM page size */
|
|
|
|
static inline unsigned long aligned_nrpages(unsigned long host_addr,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
host_addr &= ~PAGE_MASK;
|
|
|
|
return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
|
|
|
|
}
|
|
|
|
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
/* Return largest possible superpage level for a given mapping */
|
|
|
|
static inline int hardware_largepage_caps(struct dmar_domain *domain,
|
|
|
|
unsigned long iov_pfn,
|
|
|
|
unsigned long phy_pfn,
|
|
|
|
unsigned long pages)
|
|
|
|
{
|
|
|
|
int support, level = 1;
|
|
|
|
unsigned long pfnmerge;
|
|
|
|
|
|
|
|
support = domain->iommu_superpage;
|
|
|
|
|
|
|
|
/* To use a large page, the virtual *and* physical addresses
|
|
|
|
must be aligned to 2MiB/1GiB/etc. Lower bits set in either
|
|
|
|
of them will mean we have to use smaller pages. So just
|
|
|
|
merge them and check both at once. */
|
|
|
|
pfnmerge = iov_pfn | phy_pfn;
|
|
|
|
|
|
|
|
while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
|
|
|
|
pages >>= VTD_STRIDE_SHIFT;
|
|
|
|
if (!pages)
|
|
|
|
break;
|
|
|
|
pfnmerge >>= VTD_STRIDE_SHIFT;
|
|
|
|
level++;
|
|
|
|
support--;
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
2009-06-29 19:30:54 +08:00
|
|
|
static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
|
|
|
|
struct scatterlist *sg, unsigned long phys_pfn,
|
|
|
|
unsigned long nr_pages, int prot)
|
2009-06-29 18:17:38 +08:00
|
|
|
{
|
|
|
|
struct dma_pte *first_pte = NULL, *pte = NULL;
|
2009-06-29 19:30:54 +08:00
|
|
|
phys_addr_t uninitialized_var(pteval);
|
2014-11-26 09:42:10 +08:00
|
|
|
unsigned long sg_res = 0;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
unsigned int largepage_lvl = 0;
|
|
|
|
unsigned long lvl_pages = 0;
|
2009-06-29 18:17:38 +08:00
|
|
|
|
2014-07-11 14:19:35 +08:00
|
|
|
BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
|
2009-06-29 18:17:38 +08:00
|
|
|
|
|
|
|
if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
|
|
|
|
|
2014-11-26 09:42:10 +08:00
|
|
|
if (!sg) {
|
|
|
|
sg_res = nr_pages;
|
2009-06-29 19:30:54 +08:00
|
|
|
pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
|
|
|
|
}
|
|
|
|
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
while (nr_pages > 0) {
|
2009-07-02 02:21:24 +08:00
|
|
|
uint64_t tmp;
|
|
|
|
|
2009-06-29 18:17:38 +08:00
|
|
|
if (!sg_res) {
|
2017-09-28 22:14:01 +08:00
|
|
|
unsigned int pgoff = sg->offset & ~PAGE_MASK;
|
|
|
|
|
2009-08-05 06:09:37 +08:00
|
|
|
sg_res = aligned_nrpages(sg->offset, sg->length);
|
2017-09-28 22:14:01 +08:00
|
|
|
sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff;
|
2009-06-29 18:17:38 +08:00
|
|
|
sg->dma_length = sg->length;
|
2017-09-28 22:14:01 +08:00
|
|
|
pteval = (sg_phys(sg) - pgoff) | prot;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
phys_pfn = pteval >> VTD_PAGE_SHIFT;
|
2009-06-29 18:17:38 +08:00
|
|
|
}
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
|
2009-06-29 18:17:38 +08:00
|
|
|
if (!pte) {
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
|
|
|
|
|
2014-03-20 00:07:49 +08:00
|
|
|
first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
|
2009-06-29 18:17:38 +08:00
|
|
|
if (!pte)
|
|
|
|
return -ENOMEM;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
/* It is large page*/
|
2012-12-19 21:25:35 +08:00
|
|
|
if (largepage_lvl > 1) {
|
2015-06-11 00:41:45 +08:00
|
|
|
unsigned long nr_superpages, end_pfn;
|
|
|
|
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
pteval |= DMA_PTE_LARGE_PAGE;
|
2014-07-11 14:19:34 +08:00
|
|
|
lvl_pages = lvl_to_nr_pages(largepage_lvl);
|
2015-06-11 00:41:45 +08:00
|
|
|
|
|
|
|
nr_superpages = sg_res / lvl_pages;
|
|
|
|
end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
|
|
|
|
|
2014-07-11 14:19:34 +08:00
|
|
|
/*
|
|
|
|
* Ensure that old small page tables are
|
2015-06-11 00:41:45 +08:00
|
|
|
* removed to make room for superpage(s).
|
2017-06-29 10:42:23 +08:00
|
|
|
* We're adding new large pages, so make sure
|
|
|
|
* we don't remove their parent tables.
|
2014-07-11 14:19:34 +08:00
|
|
|
*/
|
2017-06-29 10:42:23 +08:00
|
|
|
dma_pte_free_pagetable(domain, iov_pfn, end_pfn,
|
|
|
|
largepage_lvl + 1);
|
2012-12-19 21:25:35 +08:00
|
|
|
} else {
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
|
2012-12-19 21:25:35 +08:00
|
|
|
}
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
|
2009-06-29 18:17:38 +08:00
|
|
|
}
|
|
|
|
/* We don't need lock here, nobody else
|
|
|
|
* touches the iova range
|
|
|
|
*/
|
2009-07-02 03:27:03 +08:00
|
|
|
tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
|
2009-07-02 02:21:24 +08:00
|
|
|
if (tmp) {
|
2009-06-30 05:06:43 +08:00
|
|
|
static int dumps = 5;
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
|
|
|
|
iov_pfn, tmp, (unsigned long long)pteval);
|
2009-06-30 05:06:43 +08:00
|
|
|
if (dumps) {
|
|
|
|
dumps--;
|
|
|
|
debug_dma_dump_mappings(NULL);
|
|
|
|
}
|
|
|
|
WARN_ON(1);
|
|
|
|
}
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
|
|
|
|
lvl_pages = lvl_to_nr_pages(largepage_lvl);
|
|
|
|
|
|
|
|
BUG_ON(nr_pages < lvl_pages);
|
|
|
|
BUG_ON(sg_res < lvl_pages);
|
|
|
|
|
|
|
|
nr_pages -= lvl_pages;
|
|
|
|
iov_pfn += lvl_pages;
|
|
|
|
phys_pfn += lvl_pages;
|
|
|
|
pteval += lvl_pages * VTD_PAGE_SIZE;
|
|
|
|
sg_res -= lvl_pages;
|
|
|
|
|
|
|
|
/* If the next PTE would be the first in a new page, then we
|
|
|
|
need to flush the cache on the entries we've just written.
|
|
|
|
And then we'll need to recalculate 'pte', so clear it and
|
|
|
|
let it get set again in the if (!pte) block above.
|
|
|
|
|
|
|
|
If we're done (!nr_pages) we need to flush the cache too.
|
|
|
|
|
|
|
|
Also if we've been setting superpages, we may need to
|
|
|
|
recalculate 'pte' and switch back to smaller pages for the
|
|
|
|
end of the mapping, if the trailing size is not enough to
|
|
|
|
use another superpage (i.e. sg_res < lvl_pages). */
|
2009-06-29 18:17:38 +08:00
|
|
|
pte++;
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
if (!nr_pages || first_pte_in_page(pte) ||
|
|
|
|
(largepage_lvl > 1 && sg_res < lvl_pages)) {
|
2009-06-29 18:17:38 +08:00
|
|
|
domain_flush_cache(domain, first_pte,
|
|
|
|
(void *)pte - (void *)first_pte);
|
|
|
|
pte = NULL;
|
|
|
|
}
|
intel-iommu: Enable super page (2MiB, 1GiB, etc.) support
There are no externally-visible changes with this. In the loop in the
internal __domain_mapping() function, we simply detect if we are mapping:
- size >= 2MiB, and
- virtual address aligned to 2MiB, and
- physical address aligned to 2MiB, and
- on hardware that supports superpages.
(and likewise for larger superpages).
We automatically use a superpage for such mappings. We never have to
worry about *breaking* superpages, since we trust that we will always
*unmap* the same range that was mapped. So all we need to do is ensure
that dma_pte_clear_range() will also cope with superpages.
Adjust pfn_to_dma_pte() to take a superpage 'level' as an argument, so
it can return a PTE at the appropriate level rather than always
extending the page tables all the way down to level 1. Again, this is
simplified by the fact that we should never encounter existing small
pages when we're creating a mapping; any old mapping that used the same
virtual range will have been entirely removed and its obsolete page
tables freed.
Provide an 'intel_iommu=sp_off' argument on the command line as a
chicken bit. Not that it should ever be required.
==
The original commit seen in the iommu-2.6.git was Youquan's
implementation (and completion) of my own half-baked code which I'd
typed into an email. Followed by half a dozen subsequent 'fixes'.
I've taken the unusual step of rewriting history and collapsing the
original commits in order to keep the main history simpler, and make
life easier for the people who are going to have to backport this to
older kernels. And also so I can give it a more coherent commit comment
which (hopefully) gives a better explanation of what's going on.
The original sequence of commits leading to identical code was:
Youquan Song (3):
intel-iommu: super page support
intel-iommu: Fix superpage alignment calculation error
intel-iommu: Fix superpage level calculation error in dma_pfn_level_pte()
David Woodhouse (4):
intel-iommu: Precalculate superpage support for dmar_domain
intel-iommu: Fix hardware_largepage_caps()
intel-iommu: Fix inappropriate use of superpages in __domain_mapping()
intel-iommu: Fix phys_pfn in __domain_mapping for sglist pages
Signed-off-by: Youquan Song <youquan.song@intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-05-26 02:13:49 +08:00
|
|
|
|
|
|
|
if (!sg_res && nr_pages)
|
2009-06-29 18:17:38 +08:00
|
|
|
sg = sg_next(sg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-04 10:34:53 +08:00
|
|
|
static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
|
2019-04-29 09:16:02 +08:00
|
|
|
struct scatterlist *sg, unsigned long phys_pfn,
|
|
|
|
unsigned long nr_pages, int prot)
|
|
|
|
{
|
2019-05-25 13:41:28 +08:00
|
|
|
int iommu_id, ret;
|
2019-04-29 09:16:02 +08:00
|
|
|
struct intel_iommu *iommu;
|
|
|
|
|
|
|
|
/* Do the real mapping first */
|
|
|
|
ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-05-25 13:41:28 +08:00
|
|
|
for_each_domain_iommu(iommu_id, domain) {
|
|
|
|
iommu = g_iommus[iommu_id];
|
2019-04-29 09:16:02 +08:00
|
|
|
__mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-05-04 10:34:53 +08:00
|
|
|
}
|
|
|
|
|
2009-06-29 19:30:54 +08:00
|
|
|
static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
|
|
|
|
struct scatterlist *sg, unsigned long nr_pages,
|
|
|
|
int prot)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2018-05-04 10:34:53 +08:00
|
|
|
return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
|
2009-06-29 19:30:54 +08:00
|
|
|
}
|
2009-06-29 03:38:49 +08:00
|
|
|
|
2009-06-29 19:30:54 +08:00
|
|
|
static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
|
|
|
|
unsigned long phys_pfn, unsigned long nr_pages,
|
|
|
|
int prot)
|
|
|
|
{
|
2018-05-04 10:34:53 +08:00
|
|
|
return domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-23 22:20:14 +08:00
|
|
|
static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2017-08-31 16:58:11 +08:00
|
|
|
unsigned long flags;
|
|
|
|
struct context_entry *context;
|
|
|
|
u16 did_old;
|
|
|
|
|
2008-12-08 22:51:37 +08:00
|
|
|
if (!iommu)
|
|
|
|
return;
|
2008-12-08 15:29:22 +08:00
|
|
|
|
2017-08-31 16:58:11 +08:00
|
|
|
spin_lock_irqsave(&iommu->lock, flags);
|
|
|
|
context = iommu_context_addr(iommu, bus, devfn, 0);
|
|
|
|
if (!context) {
|
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
did_old = context_domain_id(context);
|
|
|
|
context_clear_entry(context);
|
|
|
|
__iommu_flush_cache(iommu, context, sizeof(*context));
|
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
|
|
|
iommu->flush.flush_context(iommu,
|
|
|
|
did_old,
|
|
|
|
(((u16)bus) << 8) | devfn,
|
|
|
|
DMA_CCMD_MASK_NOBIT,
|
|
|
|
DMA_CCMD_DEVICE_INVL);
|
|
|
|
iommu->flush.flush_iotlb(iommu,
|
|
|
|
did_old,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
DMA_TLB_DSI_FLUSH);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2012-05-26 00:43:02 +08:00
|
|
|
static inline void unlink_domain_info(struct device_domain_info *info)
|
|
|
|
{
|
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
list_del(&info->link);
|
|
|
|
list_del(&info->global);
|
|
|
|
if (info->dev)
|
2014-03-07 01:12:03 +08:00
|
|
|
info->dev->archdata.iommu = NULL;
|
2012-05-26 00:43:02 +08:00
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static void domain_remove_dev_info(struct dmar_domain *domain)
|
|
|
|
{
|
2014-05-20 20:37:47 +08:00
|
|
|
struct device_domain_info *info, *tmp;
|
2014-07-11 14:19:28 +08:00
|
|
|
unsigned long flags;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
2015-07-22 00:25:11 +08:00
|
|
|
list_for_each_entry_safe(info, tmp, &domain->devices, link)
|
2015-07-23 23:44:46 +08:00
|
|
|
__dmar_remove_one_dev_info(info);
|
2007-10-22 07:41:49 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_domain
|
2014-03-07 00:19:30 +08:00
|
|
|
* Note: we use struct device->archdata.iommu stores the info
|
2007-10-22 07:41:49 +08:00
|
|
|
*/
|
2014-03-07 00:19:30 +08:00
|
|
|
static struct dmar_domain *find_domain(struct device *dev)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2019-05-25 13:41:32 +08:00
|
|
|
if (unlikely(dev->archdata.iommu == DEFER_DEVICE_DOMAIN_INFO)) {
|
|
|
|
struct iommu_domain *domain;
|
|
|
|
|
|
|
|
dev->archdata.iommu = NULL;
|
|
|
|
domain = iommu_get_domain_for_dev(dev);
|
|
|
|
if (domain)
|
|
|
|
intel_iommu_attach_device(domain, dev);
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/* No lock here, assumes no domain exit in normal case */
|
2014-03-07 00:19:30 +08:00
|
|
|
info = dev->archdata.iommu;
|
2019-05-25 13:41:32 +08:00
|
|
|
|
2017-05-22 18:28:51 +08:00
|
|
|
if (likely(info))
|
2007-10-22 07:41:49 +08:00
|
|
|
return info->domain;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:31:18 +08:00
|
|
|
static inline struct device_domain_info *
|
2014-02-19 14:07:26 +08:00
|
|
|
dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
|
|
|
list_for_each_entry(info, &device_domain_list, global)
|
2014-03-10 04:55:54 +08:00
|
|
|
if (info->iommu->segment == segment && info->bus == bus &&
|
2014-02-19 14:07:26 +08:00
|
|
|
info->devfn == devfn)
|
2014-03-10 04:31:18 +08:00
|
|
|
return info;
|
2014-02-19 14:07:26 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:40:43 +08:00
|
|
|
static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
|
|
|
|
int bus, int devfn,
|
|
|
|
struct device *dev,
|
|
|
|
struct dmar_domain *domain)
|
2014-02-19 14:07:26 +08:00
|
|
|
{
|
2014-03-10 04:31:18 +08:00
|
|
|
struct dmar_domain *found = NULL;
|
2014-02-19 14:07:26 +08:00
|
|
|
struct device_domain_info *info;
|
|
|
|
unsigned long flags;
|
2015-07-22 17:52:53 +08:00
|
|
|
int ret;
|
2014-02-19 14:07:26 +08:00
|
|
|
|
|
|
|
info = alloc_devinfo_mem();
|
|
|
|
if (!info)
|
2014-03-10 04:11:33 +08:00
|
|
|
return NULL;
|
2014-02-19 14:07:26 +08:00
|
|
|
|
|
|
|
info->bus = bus;
|
|
|
|
info->devfn = devfn;
|
2015-10-12 21:17:37 +08:00
|
|
|
info->ats_supported = info->pasid_supported = info->pri_supported = 0;
|
|
|
|
info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
|
|
|
|
info->ats_qdep = 0;
|
2014-02-19 14:07:26 +08:00
|
|
|
info->dev = dev;
|
|
|
|
info->domain = domain;
|
2014-03-10 04:31:18 +08:00
|
|
|
info->iommu = iommu;
|
2018-07-14 15:46:59 +08:00
|
|
|
info->pasid_table = NULL;
|
2019-03-25 09:30:30 +08:00
|
|
|
info->auxd_enabled = 0;
|
2019-03-25 09:30:32 +08:00
|
|
|
INIT_LIST_HEAD(&info->auxiliary_domains);
|
2014-02-19 14:07:26 +08:00
|
|
|
|
2015-10-12 21:17:37 +08:00
|
|
|
if (dev && dev_is_pci(dev)) {
|
|
|
|
struct pci_dev *pdev = to_pci_dev(info->dev);
|
|
|
|
|
2019-03-01 11:23:10 +08:00
|
|
|
if (!pdev->untrusted &&
|
|
|
|
!pci_ats_disabled() &&
|
2018-05-11 06:56:02 +08:00
|
|
|
ecap_dev_iotlb_support(iommu->ecap) &&
|
2015-10-12 21:17:37 +08:00
|
|
|
pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
|
|
|
|
dmar_find_matched_atsr_unit(pdev))
|
|
|
|
info->ats_supported = 1;
|
|
|
|
|
2018-12-10 09:58:55 +08:00
|
|
|
if (sm_supported(iommu)) {
|
|
|
|
if (pasid_supported(iommu)) {
|
2015-10-12 21:17:37 +08:00
|
|
|
int features = pci_pasid_features(pdev);
|
|
|
|
if (features >= 0)
|
|
|
|
info->pasid_supported = features | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->ats_supported && ecap_prs(iommu->ecap) &&
|
|
|
|
pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
|
|
|
|
info->pri_supported = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 14:07:26 +08:00
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
if (dev)
|
2014-03-07 01:12:03 +08:00
|
|
|
found = find_domain(dev);
|
2015-07-24 00:37:13 +08:00
|
|
|
|
|
|
|
if (!found) {
|
2014-03-10 04:31:18 +08:00
|
|
|
struct device_domain_info *info2;
|
2014-03-10 04:55:54 +08:00
|
|
|
info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
|
2015-07-24 00:37:13 +08:00
|
|
|
if (info2) {
|
|
|
|
found = info2->domain;
|
|
|
|
info2->dev = dev;
|
|
|
|
}
|
2014-03-10 04:31:18 +08:00
|
|
|
}
|
2015-07-24 00:37:13 +08:00
|
|
|
|
2014-02-19 14:07:26 +08:00
|
|
|
if (found) {
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
free_devinfo_mem(info);
|
2014-03-10 04:11:33 +08:00
|
|
|
/* Caller must free the original domain */
|
|
|
|
return found;
|
2014-02-19 14:07:26 +08:00
|
|
|
}
|
|
|
|
|
2015-07-22 17:52:53 +08:00
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
ret = domain_attach_iommu(domain, iommu);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
if (ret) {
|
2015-07-22 19:11:53 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2015-09-18 18:57:07 +08:00
|
|
|
free_devinfo_mem(info);
|
2015-07-22 19:11:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:11:33 +08:00
|
|
|
list_add(&info->link, &domain->devices);
|
|
|
|
list_add(&info->global, &device_domain_list);
|
|
|
|
if (dev)
|
|
|
|
dev->archdata.iommu = info;
|
2018-12-10 09:58:56 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2018-07-14 15:47:00 +08:00
|
|
|
|
2018-12-10 09:58:56 +08:00
|
|
|
/* PASID table is mandatory for a PCI device in scalable mode. */
|
|
|
|
if (dev && dev_is_pci(dev) && sm_supported(iommu)) {
|
2018-07-14 15:47:00 +08:00
|
|
|
ret = intel_pasid_alloc_table(dev);
|
|
|
|
if (ret) {
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "PASID table allocation failed\n");
|
2019-02-09 06:06:15 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2018-12-10 09:58:56 +08:00
|
|
|
return NULL;
|
2018-07-14 15:47:00 +08:00
|
|
|
}
|
2018-12-10 09:59:01 +08:00
|
|
|
|
|
|
|
/* Setup the PASID entry for requests without PASID: */
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
if (hw_pass_through && domain_type_is_si(domain))
|
|
|
|
ret = intel_pasid_setup_pass_through(iommu, domain,
|
|
|
|
dev, PASID_RID2PASID);
|
|
|
|
else
|
|
|
|
ret = intel_pasid_setup_second_level(iommu, domain,
|
|
|
|
dev, PASID_RID2PASID);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
if (ret) {
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "Setup RID2PASID failed\n");
|
2019-02-09 06:06:15 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2018-12-10 09:59:01 +08:00
|
|
|
return NULL;
|
2018-07-14 15:47:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-10 04:11:33 +08:00
|
|
|
|
2015-07-22 16:04:36 +08:00
|
|
|
if (dev && domain_context_mapping(domain, dev)) {
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "Domain context map failed\n");
|
2019-02-09 06:06:15 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2015-07-22 16:04:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-10 04:11:33 +08:00
|
|
|
return domain;
|
2014-02-19 14:07:26 +08:00
|
|
|
}
|
|
|
|
|
2014-07-03 23:51:43 +08:00
|
|
|
static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
|
|
|
|
{
|
|
|
|
*(u16 *)opaque = alias;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:25:12 +08:00
|
|
|
static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
struct device_domain_info *info;
|
2016-08-25 20:25:12 +08:00
|
|
|
struct dmar_domain *domain = NULL;
|
2014-07-03 23:51:43 +08:00
|
|
|
struct intel_iommu *iommu;
|
2018-05-04 13:08:17 +08:00
|
|
|
u16 dma_alias;
|
2007-10-22 07:41:49 +08:00
|
|
|
unsigned long flags;
|
2014-05-26 20:14:06 +08:00
|
|
|
u8 bus, devfn;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2014-07-03 23:51:43 +08:00
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if (!iommu)
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-10 06:44:17 +08:00
|
|
|
if (dev_is_pci(dev)) {
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
2009-04-04 08:45:37 +08:00
|
|
|
|
2014-07-03 23:51:43 +08:00
|
|
|
pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
|
|
|
|
PCI_BUS_NUM(dma_alias),
|
|
|
|
dma_alias & 0xff);
|
|
|
|
if (info) {
|
|
|
|
iommu = info->iommu;
|
|
|
|
domain = info->domain;
|
2014-03-10 04:31:18 +08:00
|
|
|
}
|
2014-07-03 23:51:43 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2016-08-25 20:25:12 +08:00
|
|
|
/* DMA alias already has a domain, use it */
|
2014-07-03 23:51:43 +08:00
|
|
|
if (info)
|
2016-08-25 20:25:12 +08:00
|
|
|
goto out;
|
2014-07-03 23:51:43 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2014-03-10 06:44:17 +08:00
|
|
|
/* Allocate and initialize new domain for the device */
|
2014-07-11 14:19:27 +08:00
|
|
|
domain = alloc_domain(0);
|
2014-02-19 14:07:26 +08:00
|
|
|
if (!domain)
|
2014-07-03 23:51:43 +08:00
|
|
|
return NULL;
|
2019-07-22 22:21:05 +08:00
|
|
|
if (domain_init(domain, iommu, gaw)) {
|
2014-07-03 23:51:43 +08:00
|
|
|
domain_exit(domain);
|
|
|
|
return NULL;
|
2009-06-20 04:47:29 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2016-08-25 20:25:12 +08:00
|
|
|
out:
|
|
|
|
return domain;
|
|
|
|
}
|
2014-07-03 23:51:43 +08:00
|
|
|
|
2016-08-25 20:25:12 +08:00
|
|
|
static struct dmar_domain *set_domain_for_dev(struct device *dev,
|
|
|
|
struct dmar_domain *domain)
|
|
|
|
{
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
struct dmar_domain *tmp;
|
|
|
|
u16 req_id, dma_alias;
|
|
|
|
u8 bus, devfn;
|
|
|
|
|
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if (!iommu)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
req_id = ((u16)bus << 8) | devfn;
|
|
|
|
|
|
|
|
if (dev_is_pci(dev)) {
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
|
|
|
|
|
|
|
pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
|
|
|
|
|
|
|
|
/* register PCI DMA alias device */
|
|
|
|
if (req_id != dma_alias) {
|
|
|
|
tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
|
|
|
|
dma_alias & 0xff, NULL, domain);
|
|
|
|
|
|
|
|
if (!tmp || tmp != domain)
|
|
|
|
return tmp;
|
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-22 18:40:43 +08:00
|
|
|
tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
|
2016-08-25 20:25:12 +08:00
|
|
|
if (!tmp || tmp != domain)
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
return domain;
|
|
|
|
}
|
2014-07-03 23:51:43 +08:00
|
|
|
|
2009-06-27 01:50:28 +08:00
|
|
|
static int iommu_domain_identity_map(struct dmar_domain *domain,
|
|
|
|
unsigned long long start,
|
|
|
|
unsigned long long end)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2009-06-28 23:35:56 +08:00
|
|
|
unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
|
|
|
|
unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
|
|
|
|
|
|
|
|
if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
|
|
|
|
dma_to_mm_pfn(last_vpfn))) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Reserving iova failed\n");
|
2009-06-27 01:50:28 +08:00
|
|
|
return -ENOMEM;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-21 21:45:19 +08:00
|
|
|
pr_debug("Mapping reserved region %llx-%llx\n", start, end);
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
|
|
|
* RMRR range might have overlap with physical memory range,
|
|
|
|
* clear it first
|
|
|
|
*/
|
2009-06-28 23:35:56 +08:00
|
|
|
dma_pte_clear_range(domain, first_vpfn, last_vpfn);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2018-05-04 10:34:53 +08:00
|
|
|
return __domain_mapping(domain, first_vpfn, NULL,
|
|
|
|
first_vpfn, last_vpfn - first_vpfn + 1,
|
|
|
|
DMA_PTE_READ|DMA_PTE_WRITE);
|
2009-06-27 01:50:28 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 01:00:10 +08:00
|
|
|
static int domain_prepare_identity_map(struct device *dev,
|
|
|
|
struct dmar_domain *domain,
|
|
|
|
unsigned long long start,
|
|
|
|
unsigned long long end)
|
2009-06-27 01:50:28 +08:00
|
|
|
{
|
2009-08-04 23:19:20 +08:00
|
|
|
/* For _hardware_ passthrough, don't bother. But for software
|
|
|
|
passthrough, we do it anyway -- it may indicate a memory
|
|
|
|
range which is reserved in E820, so which didn't get set
|
|
|
|
up to start with in si_domain */
|
|
|
|
if (domain == si_domain && hw_pass_through) {
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_warn(dev, "Ignoring identity map for HW passthrough [0x%Lx - 0x%Lx]\n",
|
|
|
|
start, end);
|
2009-08-04 23:19:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_info(dev, "Setting identity map [0x%Lx - 0x%Lx]\n", start, end);
|
2015-06-12 15:57:06 +08:00
|
|
|
|
2009-12-02 17:21:55 +08:00
|
|
|
if (end < start) {
|
|
|
|
WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
|
|
|
|
"BIOS vendor: %s; Ver: %s; Product Version: %s\n",
|
|
|
|
dmi_get_system_info(DMI_BIOS_VENDOR),
|
|
|
|
dmi_get_system_info(DMI_BIOS_VERSION),
|
|
|
|
dmi_get_system_info(DMI_PRODUCT_VERSION));
|
2015-09-24 01:00:10 +08:00
|
|
|
return -EIO;
|
2009-12-02 17:21:55 +08:00
|
|
|
}
|
|
|
|
|
2009-08-26 21:25:41 +08:00
|
|
|
if (end >> agaw_to_width(domain->agaw)) {
|
|
|
|
WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
|
|
|
|
"BIOS vendor: %s; Ver: %s; Product Version: %s\n",
|
|
|
|
agaw_to_width(domain->agaw),
|
|
|
|
dmi_get_system_info(DMI_BIOS_VENDOR),
|
|
|
|
dmi_get_system_info(DMI_BIOS_VERSION),
|
|
|
|
dmi_get_system_info(DMI_PRODUCT_VERSION));
|
2015-09-24 01:00:10 +08:00
|
|
|
return -EIO;
|
2009-08-26 21:25:41 +08:00
|
|
|
}
|
2009-08-04 23:19:20 +08:00
|
|
|
|
2015-09-24 01:00:10 +08:00
|
|
|
return iommu_domain_identity_map(domain, start, end);
|
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2019-07-22 22:21:05 +08:00
|
|
|
static int md_domain_init(struct dmar_domain *domain, int guest_width);
|
|
|
|
|
2009-08-24 13:30:22 +08:00
|
|
|
static int __init si_domain_init(int hw)
|
2009-06-20 04:47:29 +08:00
|
|
|
{
|
2019-05-25 13:41:27 +08:00
|
|
|
struct dmar_rmrr_unit *rmrr;
|
|
|
|
struct device *dev;
|
|
|
|
int i, nid, ret;
|
2009-06-20 04:47:29 +08:00
|
|
|
|
2014-07-11 14:19:27 +08:00
|
|
|
si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
|
2009-06-20 04:47:29 +08:00
|
|
|
if (!si_domain)
|
|
|
|
return -EFAULT;
|
|
|
|
|
2019-07-22 22:21:05 +08:00
|
|
|
if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
|
2009-06-20 04:47:29 +08:00
|
|
|
domain_exit(si_domain);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2009-08-04 23:19:20 +08:00
|
|
|
if (hw)
|
|
|
|
return 0;
|
|
|
|
|
2009-06-27 02:10:36 +08:00
|
|
|
for_each_online_node(nid) {
|
2011-07-14 15:46:10 +08:00
|
|
|
unsigned long start_pfn, end_pfn;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
|
|
|
|
ret = iommu_domain_identity_map(si_domain,
|
|
|
|
PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2009-06-27 02:10:36 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:27 +08:00
|
|
|
/*
|
|
|
|
* Normally we use DMA domains for devices which have RMRRs. But we
|
|
|
|
* loose this requirement for graphic and usb devices. Identity map
|
|
|
|
* the RMRRs for graphic and USB devices so that they could use the
|
|
|
|
* si_domain.
|
|
|
|
*/
|
|
|
|
for_each_rmrr_units(rmrr) {
|
|
|
|
for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
|
|
|
|
i, dev) {
|
|
|
|
unsigned long long start = rmrr->base_address;
|
|
|
|
unsigned long long end = rmrr->end_address;
|
|
|
|
|
|
|
|
if (device_is_rmrr_locked(dev))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (WARN_ON(end < start ||
|
|
|
|
end >> agaw_to_width(si_domain->agaw)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = iommu_domain_identity_map(si_domain, start, end);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-20 04:47:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-10 05:03:28 +08:00
|
|
|
static int identity_mapping(struct device *dev)
|
2009-06-20 04:47:29 +08:00
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
|
2014-03-10 05:03:28 +08:00
|
|
|
info = dev->archdata.iommu;
|
2011-05-29 02:15:03 +08:00
|
|
|
if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
|
|
|
|
return (info->domain == si_domain);
|
2009-06-20 04:47:29 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-21 20:45:31 +08:00
|
|
|
static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
|
2009-06-20 04:47:29 +08:00
|
|
|
{
|
2014-03-10 04:19:22 +08:00
|
|
|
struct dmar_domain *ndomain;
|
2014-03-10 04:31:18 +08:00
|
|
|
struct intel_iommu *iommu;
|
2014-03-10 05:00:57 +08:00
|
|
|
u8 bus, devfn;
|
2009-06-20 04:47:29 +08:00
|
|
|
|
2014-03-10 07:27:31 +08:00
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
2014-03-10 04:31:18 +08:00
|
|
|
if (!iommu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2015-07-22 18:40:43 +08:00
|
|
|
ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
|
2014-03-10 04:19:22 +08:00
|
|
|
if (ndomain != domain)
|
|
|
|
return -EBUSY;
|
2009-06-20 04:47:29 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-10 06:48:15 +08:00
|
|
|
static bool device_has_rmrr(struct device *dev)
|
2012-11-21 03:43:17 +08:00
|
|
|
{
|
|
|
|
struct dmar_rmrr_unit *rmrr;
|
2014-03-07 23:08:36 +08:00
|
|
|
struct device *tmp;
|
2012-11-21 03:43:17 +08:00
|
|
|
int i;
|
|
|
|
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_lock();
|
2012-11-21 03:43:17 +08:00
|
|
|
for_each_rmrr_units(rmrr) {
|
2014-02-19 14:07:32 +08:00
|
|
|
/*
|
|
|
|
* Return TRUE if this RMRR contains the device that
|
|
|
|
* is passed in.
|
|
|
|
*/
|
|
|
|
for_each_active_dev_scope(rmrr->devices,
|
|
|
|
rmrr->devices_cnt, i, tmp)
|
2019-06-03 14:53:33 +08:00
|
|
|
if (tmp == dev ||
|
|
|
|
is_downstream_to_pci_bridge(dev, tmp)) {
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_unlock();
|
2012-11-21 03:43:17 +08:00
|
|
|
return true;
|
2014-02-19 14:07:32 +08:00
|
|
|
}
|
2012-11-21 03:43:17 +08:00
|
|
|
}
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_unlock();
|
2012-11-21 03:43:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-03 14:53:36 +08:00
|
|
|
/**
|
|
|
|
* device_rmrr_is_relaxable - Test whether the RMRR of this device
|
|
|
|
* is relaxable (ie. is allowed to be not enforced under some conditions)
|
|
|
|
* @dev: device handle
|
|
|
|
*
|
|
|
|
* We assume that PCI USB devices with RMRRs have them largely
|
|
|
|
* for historical reasons and that the RMRR space is not actively used post
|
|
|
|
* boot. This exclusion may change if vendors begin to abuse it.
|
|
|
|
*
|
|
|
|
* The same exception is made for graphics devices, with the requirement that
|
|
|
|
* any use of the RMRR regions will be torn down before assigning the device
|
|
|
|
* to a guest.
|
|
|
|
*
|
|
|
|
* Return: true if the RMRR is relaxable, false otherwise
|
|
|
|
*/
|
|
|
|
static bool device_rmrr_is_relaxable(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
|
|
|
if (!dev_is_pci(dev))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pdev = to_pci_dev(dev);
|
|
|
|
if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-03 23:57:02 +08:00
|
|
|
/*
|
|
|
|
* There are a couple cases where we need to restrict the functionality of
|
|
|
|
* devices associated with RMRRs. The first is when evaluating a device for
|
|
|
|
* identity mapping because problems exist when devices are moved in and out
|
|
|
|
* of domains and their respective RMRR information is lost. This means that
|
|
|
|
* a device with associated RMRRs will never be in a "passthrough" domain.
|
|
|
|
* The second is use of the device through the IOMMU API. This interface
|
|
|
|
* expects to have full control of the IOVA space for the device. We cannot
|
|
|
|
* satisfy both the requirement that RMRR access is maintained and have an
|
|
|
|
* unencumbered IOVA space. We also have no ability to quiesce the device's
|
|
|
|
* use of the RMRR space or even inform the IOMMU API user of the restriction.
|
|
|
|
* We therefore prevent devices associated with an RMRR from participating in
|
|
|
|
* the IOMMU API, which eliminates them from device assignment.
|
|
|
|
*
|
2019-06-03 14:53:36 +08:00
|
|
|
* In both cases, devices which have relaxable RMRRs are not concerned by this
|
|
|
|
* restriction. See device_rmrr_is_relaxable comment.
|
2014-07-03 23:57:02 +08:00
|
|
|
*/
|
|
|
|
static bool device_is_rmrr_locked(struct device *dev)
|
|
|
|
{
|
|
|
|
if (!device_has_rmrr(dev))
|
|
|
|
return false;
|
|
|
|
|
2019-06-03 14:53:36 +08:00
|
|
|
if (device_rmrr_is_relaxable(dev))
|
|
|
|
return false;
|
2014-07-03 23:57:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:26 +08:00
|
|
|
/*
|
|
|
|
* Return the required default domain type for a specific device.
|
|
|
|
*
|
|
|
|
* @dev: the device in query
|
|
|
|
* @startup: true if this is during early boot
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* - IOMMU_DOMAIN_DMA: device requires a dynamic mapping domain
|
|
|
|
* - IOMMU_DOMAIN_IDENTITY: device requires an identical mapping domain
|
|
|
|
* - 0: both identity and dynamic domains work for this device
|
|
|
|
*/
|
2019-05-25 13:41:34 +08:00
|
|
|
static int device_def_domain_type(struct device *dev)
|
2009-07-05 01:24:27 +08:00
|
|
|
{
|
2014-03-10 07:03:08 +08:00
|
|
|
if (dev_is_pci(dev)) {
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
2012-11-21 03:43:17 +08:00
|
|
|
|
2014-07-03 23:57:02 +08:00
|
|
|
if (device_is_rmrr_locked(dev))
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_DMA;
|
2009-10-01 00:12:17 +08:00
|
|
|
|
2018-10-23 15:45:01 +08:00
|
|
|
/*
|
|
|
|
* Prevent any device marked as untrusted from getting
|
|
|
|
* placed into the statically identity mapping domain.
|
|
|
|
*/
|
|
|
|
if (pdev->untrusted)
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_DMA;
|
2018-10-23 15:45:01 +08:00
|
|
|
|
2014-03-10 07:03:08 +08:00
|
|
|
if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_IDENTITY;
|
2009-10-01 00:12:17 +08:00
|
|
|
|
2014-03-10 07:03:08 +08:00
|
|
|
if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_IDENTITY;
|
2014-03-10 07:03:08 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to start off with all devices in the 1:1 domain, and
|
|
|
|
* take them out later if we find they can't access all of memory.
|
|
|
|
*
|
|
|
|
* However, we can't do this for PCI devices behind bridges,
|
|
|
|
* because all PCI devices behind the same bridge will end up
|
|
|
|
* with the same source-id on their transactions.
|
|
|
|
*
|
|
|
|
* Practically speaking, we can't change things around for these
|
|
|
|
* devices at run-time, because we can't be sure there'll be no
|
|
|
|
* DMA transactions in flight for any of their siblings.
|
|
|
|
*
|
|
|
|
* So PCI devices (unless they're on the root bus) as well as
|
|
|
|
* their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
|
|
|
|
* the 1:1 domain, just in _case_ one of their siblings turns out
|
|
|
|
* not to be able to map all of memory.
|
|
|
|
*/
|
|
|
|
if (!pci_is_pcie(pdev)) {
|
|
|
|
if (!pci_is_root_bus(pdev->bus))
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_DMA;
|
2014-03-10 07:03:08 +08:00
|
|
|
if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_DMA;
|
2014-03-10 07:03:08 +08:00
|
|
|
} else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_DMA;
|
2014-03-10 07:03:08 +08:00
|
|
|
} else {
|
|
|
|
if (device_has_rmrr(dev))
|
2019-05-25 13:41:26 +08:00
|
|
|
return IOMMU_DOMAIN_DMA;
|
2014-03-10 07:03:08 +08:00
|
|
|
}
|
intel-iommu: Don't use identity mapping for PCI devices behind bridges
Our current strategy for pass-through mode is to put all devices into
the 1:1 domain at startup (which is before we know what their dma_mask
will be), and only _later_ take them out of that domain, if it turns out
that they really can't address all of memory.
However, when there are a bunch of PCI devices behind a bridge, they all
end up with the same source-id on their DMA transactions, and hence in
the same IOMMU domain. This means that we _can't_ easily move them from
the 1:1 domain into their own domain at runtime, because there might be DMA
in-flight from their siblings.
So we have to adjust our pass-through strategy: For PCI devices not on
the root bus, and for the bridges which will take responsibility for
their transactions, we have to start up _out_ of the 1:1 domain, just in
case.
This fixes the BUG() we see when we have 32-bit-capable devices behind a
PCI-PCI bridge, and use the software identity mapping.
It does mean that we might end up using 'normal' mapping mode for some
devices which could actually live with the faster 1:1 mapping -- but
this is only for PCI devices behind bridges, which presumably aren't the
devices for which people are most concerned about performance.
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2009-07-05 02:11:08 +08:00
|
|
|
|
2019-05-25 13:41:26 +08:00
|
|
|
return (iommu_identity_mapping & IDENTMAP_ALL) ?
|
|
|
|
IOMMU_DOMAIN_IDENTITY : 0;
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:48:02 +08:00
|
|
|
static void intel_iommu_init_qi(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Start from the sane iommu hardware state.
|
|
|
|
* If the queued invalidation is already initialized by us
|
|
|
|
* (for example, while enabling interrupt-remapping) then
|
|
|
|
* we got the things already rolling from a sane state.
|
|
|
|
*/
|
|
|
|
if (!iommu->qi) {
|
|
|
|
/*
|
|
|
|
* Clear any previous faults.
|
|
|
|
*/
|
|
|
|
dmar_fault(-1, iommu);
|
|
|
|
/*
|
|
|
|
* Disable queued invalidation if supported and already enabled
|
|
|
|
* before OS handover.
|
|
|
|
*/
|
|
|
|
dmar_disable_qi(iommu);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dmar_enable_qi(iommu)) {
|
|
|
|
/*
|
|
|
|
* Queued Invalidate not enabled, use Register Based Invalidate
|
|
|
|
*/
|
|
|
|
iommu->flush.flush_context = __iommu_flush_context;
|
|
|
|
iommu->flush.flush_iotlb = __iommu_flush_iotlb;
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("%s: Using Register based invalidation\n",
|
2014-11-09 22:48:02 +08:00
|
|
|
iommu->name);
|
|
|
|
} else {
|
|
|
|
iommu->flush.flush_context = qi_flush_context;
|
|
|
|
iommu->flush.flush_iotlb = qi_flush_iotlb;
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("%s: Using Queued invalidation\n", iommu->name);
|
2014-11-09 22:48:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
static int copy_context_table(struct intel_iommu *iommu,
|
2015-10-10 06:16:46 +08:00
|
|
|
struct root_entry *old_re,
|
2015-06-12 17:56:10 +08:00
|
|
|
struct context_entry **tbl,
|
|
|
|
int bus, bool ext)
|
|
|
|
{
|
2015-06-12 18:02:09 +08:00
|
|
|
int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
|
2015-08-13 17:56:59 +08:00
|
|
|
struct context_entry *new_ce = NULL, ce;
|
2015-10-10 06:16:46 +08:00
|
|
|
struct context_entry *old_ce = NULL;
|
2015-08-13 17:56:59 +08:00
|
|
|
struct root_entry re;
|
2015-06-12 17:56:10 +08:00
|
|
|
phys_addr_t old_ce_phys;
|
|
|
|
|
|
|
|
tbl_idx = ext ? bus * 2 : bus;
|
2015-10-10 06:16:46 +08:00
|
|
|
memcpy(&re, old_re, sizeof(re));
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
for (devfn = 0; devfn < 256; devfn++) {
|
|
|
|
/* First calculate the correct index */
|
|
|
|
idx = (ext ? devfn * 2 : devfn) % 256;
|
|
|
|
|
|
|
|
if (idx == 0) {
|
|
|
|
/* First save what we may have and clean up */
|
|
|
|
if (new_ce) {
|
|
|
|
tbl[tbl_idx] = new_ce;
|
|
|
|
__iommu_flush_cache(iommu, new_ce,
|
|
|
|
VTD_PAGE_SIZE);
|
|
|
|
pos = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_ce)
|
2018-11-21 17:53:47 +08:00
|
|
|
memunmap(old_ce);
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
if (devfn < 0x80)
|
2015-08-13 17:56:59 +08:00
|
|
|
old_ce_phys = root_entry_lctp(&re);
|
2015-06-12 17:56:10 +08:00
|
|
|
else
|
2015-08-13 17:56:59 +08:00
|
|
|
old_ce_phys = root_entry_uctp(&re);
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
if (!old_ce_phys) {
|
|
|
|
if (ext && devfn == 0) {
|
|
|
|
/* No LCTP, try UCTP */
|
|
|
|
devfn = 0x7f;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = -ENOMEM;
|
2015-10-10 06:16:46 +08:00
|
|
|
old_ce = memremap(old_ce_phys, PAGE_SIZE,
|
|
|
|
MEMREMAP_WB);
|
2015-06-12 17:56:10 +08:00
|
|
|
if (!old_ce)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
new_ce = alloc_pgtable_page(iommu->node);
|
|
|
|
if (!new_ce)
|
|
|
|
goto out_unmap;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now copy the context entry */
|
2015-10-10 06:16:46 +08:00
|
|
|
memcpy(&ce, old_ce + idx, sizeof(ce));
|
2015-06-12 17:56:10 +08:00
|
|
|
|
2015-06-12 18:21:46 +08:00
|
|
|
if (!__context_present(&ce))
|
2015-06-12 17:56:10 +08:00
|
|
|
continue;
|
|
|
|
|
2015-06-12 18:02:09 +08:00
|
|
|
did = context_domain_id(&ce);
|
|
|
|
if (did >= 0 && did < cap_ndoms(iommu->cap))
|
|
|
|
set_bit(did, iommu->domain_ids);
|
|
|
|
|
2015-06-12 18:21:46 +08:00
|
|
|
/*
|
|
|
|
* We need a marker for copied context entries. This
|
|
|
|
* marker needs to work for the old format as well as
|
|
|
|
* for extended context entries.
|
|
|
|
*
|
|
|
|
* Bit 67 of the context entry is used. In the old
|
|
|
|
* format this bit is available to software, in the
|
|
|
|
* extended format it is the PGE bit, but PGE is ignored
|
|
|
|
* by HW if PASIDs are disabled (and thus still
|
|
|
|
* available).
|
|
|
|
*
|
|
|
|
* So disable PASIDs first and then mark the entry
|
|
|
|
* copied. This means that we don't copy PASID
|
|
|
|
* translations from the old kernel, but this is fine as
|
|
|
|
* faults there are not fatal.
|
|
|
|
*/
|
|
|
|
context_clear_pasid_enable(&ce);
|
|
|
|
context_set_copied(&ce);
|
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
new_ce[idx] = ce;
|
|
|
|
}
|
|
|
|
|
|
|
|
tbl[tbl_idx + pos] = new_ce;
|
|
|
|
|
|
|
|
__iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
|
|
|
|
|
|
|
|
out_unmap:
|
2015-10-10 06:16:46 +08:00
|
|
|
memunmap(old_ce);
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_translation_tables(struct intel_iommu *iommu)
|
|
|
|
{
|
|
|
|
struct context_entry **ctxt_tbls;
|
2015-10-10 06:16:46 +08:00
|
|
|
struct root_entry *old_rt;
|
2015-06-12 17:56:10 +08:00
|
|
|
phys_addr_t old_rt_phys;
|
|
|
|
int ctxt_table_entries;
|
|
|
|
unsigned long flags;
|
|
|
|
u64 rtaddr_reg;
|
|
|
|
int bus, ret;
|
2015-06-12 18:39:25 +08:00
|
|
|
bool new_ext, ext;
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
|
|
|
|
ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
|
2015-06-12 18:39:25 +08:00
|
|
|
new_ext = !!ecap_ecs(iommu->ecap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The RTT bit can only be changed when translation is disabled,
|
|
|
|
* but disabling translation means to open a window for data
|
|
|
|
* corruption. So bail out and don't copy anything if we would
|
|
|
|
* have to change the bit.
|
|
|
|
*/
|
|
|
|
if (new_ext != ext)
|
|
|
|
return -EINVAL;
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
|
|
|
|
if (!old_rt_phys)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-10-10 06:16:46 +08:00
|
|
|
old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB);
|
2015-06-12 17:56:10 +08:00
|
|
|
if (!old_rt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* This is too big for the stack - allocate it from slab */
|
|
|
|
ctxt_table_entries = ext ? 512 : 256;
|
|
|
|
ret = -ENOMEM;
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL);
|
2015-06-12 17:56:10 +08:00
|
|
|
if (!ctxt_tbls)
|
|
|
|
goto out_unmap;
|
|
|
|
|
|
|
|
for (bus = 0; bus < 256; bus++) {
|
|
|
|
ret = copy_context_table(iommu, &old_rt[bus],
|
|
|
|
ctxt_tbls, bus, ext);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("%s: Failed to copy context table for bus %d\n",
|
|
|
|
iommu->name, bus);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&iommu->lock, flags);
|
|
|
|
|
|
|
|
/* Context tables are copied, now write them to the root_entry table */
|
|
|
|
for (bus = 0; bus < 256; bus++) {
|
|
|
|
int idx = ext ? bus * 2 : bus;
|
|
|
|
u64 val;
|
|
|
|
|
|
|
|
if (ctxt_tbls[idx]) {
|
|
|
|
val = virt_to_phys(ctxt_tbls[idx]) | 1;
|
|
|
|
iommu->root_entry[bus].lo = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ext || !ctxt_tbls[idx + 1])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
|
|
|
|
iommu->root_entry[bus].hi = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
|
|
|
|
|
|
|
kfree(ctxt_tbls);
|
|
|
|
|
|
|
|
__iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out_unmap:
|
2015-10-10 06:16:46 +08:00
|
|
|
memunmap(old_rt);
|
2015-06-12 17:56:10 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-03 15:08:37 +08:00
|
|
|
static int __init init_dmars(void)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
2019-05-25 13:41:36 +08:00
|
|
|
int ret;
|
2009-06-20 04:47:29 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
|
|
|
* for each drhd
|
|
|
|
* allocate root
|
|
|
|
* initialize and program root entry to not present
|
|
|
|
* endfor
|
|
|
|
*/
|
|
|
|
for_each_drhd_unit(drhd) {
|
2008-03-05 07:22:08 +08:00
|
|
|
/*
|
|
|
|
* lock not needed as this is only incremented in the single
|
|
|
|
* threaded kernel __init code path all other access are read
|
|
|
|
* only
|
|
|
|
*/
|
2014-11-09 22:47:57 +08:00
|
|
|
if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
|
2012-03-06 07:05:16 +08:00
|
|
|
g_num_of_iommus++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
|
2008-03-05 07:22:08 +08:00
|
|
|
}
|
|
|
|
|
2014-11-09 22:48:02 +08:00
|
|
|
/* Preallocate enough resources for IOMMU hot-addition */
|
|
|
|
if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
|
|
|
|
g_num_of_iommus = DMAR_UNITS_SUPPORTED;
|
|
|
|
|
2008-12-08 11:06:32 +08:00
|
|
|
g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!g_iommus) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Allocating global iommu array failed\n");
|
2008-12-08 11:06:32 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:28:47 +08:00
|
|
|
for_each_iommu(iommu, drhd) {
|
|
|
|
if (drhd->ignored) {
|
|
|
|
iommu_disable_translation(iommu);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-14 15:46:54 +08:00
|
|
|
/*
|
|
|
|
* Find the max pasid size of all IOMMU's in the system.
|
|
|
|
* We need to ensure the system pasid table is no bigger
|
|
|
|
* than the smallest supported.
|
|
|
|
*/
|
2018-12-10 09:58:55 +08:00
|
|
|
if (pasid_supported(iommu)) {
|
2018-07-14 15:46:54 +08:00
|
|
|
u32 temp = 2 << ecap_pss(iommu->ecap);
|
|
|
|
|
|
|
|
intel_pasid_max_id = min_t(u32, temp,
|
|
|
|
intel_pasid_max_id);
|
|
|
|
}
|
|
|
|
|
2008-12-08 11:06:32 +08:00
|
|
|
g_iommus[iommu->seq_id] = iommu;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-06-12 15:14:34 +08:00
|
|
|
intel_iommu_init_qi(iommu);
|
|
|
|
|
2008-07-11 02:16:35 +08:00
|
|
|
ret = iommu_init_domains(iommu);
|
|
|
|
if (ret)
|
2014-02-19 14:07:21 +08:00
|
|
|
goto free_iommu;
|
2008-07-11 02:16:35 +08:00
|
|
|
|
2015-06-12 16:14:02 +08:00
|
|
|
init_translation_status(iommu);
|
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
|
|
|
|
iommu_disable_translation(iommu);
|
|
|
|
clear_translation_pre_enabled(iommu);
|
|
|
|
pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
|
|
|
|
iommu->name);
|
|
|
|
}
|
2015-06-12 16:14:02 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
|
|
|
* TBD:
|
|
|
|
* we could share the same root & context tables
|
2011-03-31 09:57:33 +08:00
|
|
|
* among all IOMMU's. Need to Split it later.
|
2007-10-22 07:41:49 +08:00
|
|
|
*/
|
|
|
|
ret = iommu_alloc_root_entry(iommu);
|
2014-11-09 22:48:02 +08:00
|
|
|
if (ret)
|
2014-02-19 14:07:21 +08:00
|
|
|
goto free_iommu;
|
2015-06-12 15:18:53 +08:00
|
|
|
|
2015-06-12 17:56:10 +08:00
|
|
|
if (translation_pre_enabled(iommu)) {
|
|
|
|
pr_info("Translation already enabled - trying to copy translation structures\n");
|
|
|
|
|
|
|
|
ret = copy_translation_tables(iommu);
|
|
|
|
if (ret) {
|
|
|
|
/*
|
|
|
|
* We found the IOMMU with translation
|
|
|
|
* enabled - but failed to copy over the
|
|
|
|
* old root-entry table. Try to proceed
|
|
|
|
* by disabling translation now and
|
|
|
|
* allocating a clean root-entry table.
|
|
|
|
* This might cause DMAR faults, but
|
|
|
|
* probably the dump will still succeed.
|
|
|
|
*/
|
|
|
|
pr_err("Failed to copy translation tables from previous kernel for %s\n",
|
|
|
|
iommu->name);
|
|
|
|
iommu_disable_translation(iommu);
|
|
|
|
clear_translation_pre_enabled(iommu);
|
|
|
|
} else {
|
|
|
|
pr_info("Copied translation tables from previous kernel for %s\n",
|
|
|
|
iommu->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 08:30:20 +08:00
|
|
|
if (!ecap_pass_through(iommu->ecap))
|
2009-08-04 23:19:20 +08:00
|
|
|
hw_pass_through = 0;
|
2015-03-24 22:54:56 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
2018-12-10 09:58:55 +08:00
|
|
|
if (pasid_supported(iommu))
|
2018-07-14 15:47:02 +08:00
|
|
|
intel_svm_init(iommu);
|
2015-03-24 22:54:56 +08:00
|
|
|
#endif
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 17:29:48 +08:00
|
|
|
/*
|
|
|
|
* Now that qi is enabled on all iommus, set the root entry and flush
|
|
|
|
* caches. This is required on some Intel X58 chipsets, otherwise the
|
|
|
|
* flush_context function will loop forever and the boot hangs.
|
|
|
|
*/
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
iommu_flush_write_buffer(iommu);
|
|
|
|
iommu_set_root_entry(iommu);
|
|
|
|
iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
|
|
|
|
iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
|
|
|
|
}
|
|
|
|
|
2019-08-19 21:22:50 +08:00
|
|
|
if (iommu_default_passthrough())
|
2009-10-01 00:12:17 +08:00
|
|
|
iommu_identity_mapping |= IDENTMAP_ALL;
|
|
|
|
|
2011-08-24 08:05:25 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
|
2019-05-02 09:34:26 +08:00
|
|
|
dmar_map_gfx = 0;
|
2009-08-04 23:19:20 +08:00
|
|
|
#endif
|
2009-10-01 00:12:17 +08:00
|
|
|
|
2019-05-02 09:34:26 +08:00
|
|
|
if (!dmar_map_gfx)
|
|
|
|
iommu_identity_mapping |= IDENTMAP_GFX;
|
|
|
|
|
2017-01-31 01:39:53 +08:00
|
|
|
check_tylersburg_isoch();
|
|
|
|
|
2019-05-25 13:41:27 +08:00
|
|
|
ret = si_domain_init(hw_pass_through);
|
|
|
|
if (ret)
|
|
|
|
goto free_iommu;
|
2015-06-12 18:27:16 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
|
|
|
* for each drhd
|
|
|
|
* enable fault log
|
|
|
|
* global invalidate context cache
|
|
|
|
* global invalidate iotlb
|
|
|
|
* enable translation
|
|
|
|
*/
|
2014-01-06 14:18:18 +08:00
|
|
|
for_each_iommu(iommu, drhd) {
|
2011-03-22 02:04:24 +08:00
|
|
|
if (drhd->ignored) {
|
|
|
|
/*
|
|
|
|
* we always have to disable PMRs or DMA may fail on
|
|
|
|
* this device
|
|
|
|
*/
|
|
|
|
if (force_on)
|
2014-01-06 14:18:18 +08:00
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
2007-10-22 07:41:49 +08:00
|
|
|
continue;
|
2011-03-22 02:04:24 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
iommu_flush_write_buffer(iommu);
|
|
|
|
|
2015-10-08 06:35:18 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
2018-12-10 09:58:55 +08:00
|
|
|
if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) {
|
2019-04-19 14:43:29 +08:00
|
|
|
/*
|
|
|
|
* Call dmar_alloc_hwirq() with dmar_global_lock held,
|
|
|
|
* could cause possible lock race condition.
|
|
|
|
*/
|
|
|
|
up_write(&dmar_global_lock);
|
2015-10-08 06:35:18 +08:00
|
|
|
ret = intel_svm_enable_prq(iommu);
|
2019-04-19 14:43:29 +08:00
|
|
|
down_write(&dmar_global_lock);
|
2015-10-08 06:35:18 +08:00
|
|
|
if (ret)
|
|
|
|
goto free_iommu;
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-22 07:41:54 +08:00
|
|
|
ret = dmar_set_interrupt(iommu);
|
|
|
|
if (ret)
|
2014-02-19 14:07:21 +08:00
|
|
|
goto free_iommu;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-02-19 14:07:21 +08:00
|
|
|
|
|
|
|
free_iommu:
|
2014-11-09 22:48:02 +08:00
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
disable_dmar_iommu(iommu);
|
2014-01-06 14:18:20 +08:00
|
|
|
free_dmar_iommu(iommu);
|
2014-11-09 22:48:02 +08:00
|
|
|
}
|
2017-08-11 17:40:10 +08:00
|
|
|
|
2008-12-08 11:06:32 +08:00
|
|
|
kfree(g_iommus);
|
2017-08-11 17:40:10 +08:00
|
|
|
|
2014-02-19 14:07:21 +08:00
|
|
|
error:
|
2007-10-22 07:41:49 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-04 16:35:44 +08:00
|
|
|
/* This takes a number of _MM_ pages, not VTD pages */
|
2016-04-20 16:33:57 +08:00
|
|
|
static unsigned long intel_alloc_iova(struct device *dev,
|
2009-06-29 04:20:51 +08:00
|
|
|
struct dmar_domain *domain,
|
|
|
|
unsigned long nrpages, uint64_t dma_mask)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
unsigned long iova_pfn;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2009-06-29 04:20:51 +08:00
|
|
|
/* Restrict dma_mask to the width that the iommu can handle */
|
|
|
|
dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
|
2015-07-17 02:40:12 +08:00
|
|
|
/* Ensure we reserve the whole size-aligned region */
|
|
|
|
nrpages = __roundup_pow_of_two(nrpages);
|
2009-06-29 04:20:51 +08:00
|
|
|
|
|
|
|
if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
|
|
|
* First try to allocate an io virtual address in
|
2009-04-07 10:01:15 +08:00
|
|
|
* DMA_BIT_MASK(32) and if that fails then try allocating
|
2007-12-18 03:40:11 +08:00
|
|
|
* from higher range
|
2007-10-22 07:41:49 +08:00
|
|
|
*/
|
2016-04-20 16:34:11 +08:00
|
|
|
iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
|
2017-09-20 16:52:02 +08:00
|
|
|
IOVA_PFN(DMA_BIT_MASK(32)), false);
|
2016-04-20 16:34:11 +08:00
|
|
|
if (iova_pfn)
|
|
|
|
return iova_pfn;
|
2009-06-29 04:20:51 +08:00
|
|
|
}
|
2017-09-20 16:52:02 +08:00
|
|
|
iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
|
|
|
|
IOVA_PFN(dma_mask), true);
|
2016-04-20 16:34:11 +08:00
|
|
|
if (unlikely(!iova_pfn)) {
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "Allocating %ld-page iova failed", nrpages);
|
2016-04-20 16:33:57 +08:00
|
|
|
return 0;
|
2007-10-22 07:41:58 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 16:34:11 +08:00
|
|
|
return iova_pfn;
|
2007-10-22 07:41:58 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:33 +08:00
|
|
|
static struct dmar_domain *get_private_domain_for_dev(struct device *dev)
|
2007-10-22 07:41:58 +08:00
|
|
|
{
|
2016-08-25 19:52:51 +08:00
|
|
|
struct dmar_domain *domain, *tmp;
|
2015-09-24 01:16:01 +08:00
|
|
|
struct dmar_rmrr_unit *rmrr;
|
|
|
|
struct device *i_dev;
|
|
|
|
int i, ret;
|
2007-10-22 07:41:58 +08:00
|
|
|
|
2019-05-25 13:41:33 +08:00
|
|
|
/* Device shouldn't be attached by any domains. */
|
2016-08-25 19:52:51 +08:00
|
|
|
domain = find_domain(dev);
|
|
|
|
if (domain)
|
2019-05-25 13:41:33 +08:00
|
|
|
return NULL;
|
2016-08-25 19:52:51 +08:00
|
|
|
|
|
|
|
domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
|
|
|
|
if (!domain)
|
|
|
|
goto out;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2015-09-24 01:16:01 +08:00
|
|
|
/* We have a new domain - setup possible RMRRs for the device */
|
|
|
|
rcu_read_lock();
|
|
|
|
for_each_rmrr_units(rmrr) {
|
|
|
|
for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
|
|
|
|
i, i_dev) {
|
|
|
|
if (i_dev != dev)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = domain_prepare_identity_map(dev, domain,
|
|
|
|
rmrr->base_address,
|
|
|
|
rmrr->end_address);
|
|
|
|
if (ret)
|
|
|
|
dev_err(dev, "Mapping reserved region failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2016-08-25 19:52:51 +08:00
|
|
|
tmp = set_domain_for_dev(dev, domain);
|
|
|
|
if (!tmp || domain != tmp) {
|
|
|
|
domain_exit(domain);
|
|
|
|
domain = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!domain)
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "Allocating domain failed\n");
|
2019-06-12 08:28:46 +08:00
|
|
|
else
|
|
|
|
domain->domain.type = IOMMU_DOMAIN_DMA;
|
2016-08-25 19:52:51 +08:00
|
|
|
|
2007-10-22 07:41:58 +08:00
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
2014-03-10 07:29:55 +08:00
|
|
|
/* Check if the dev needs to go through non-identity map and unmap process.*/
|
2019-04-11 00:14:06 +08:00
|
|
|
static bool iommu_need_mapping(struct device *dev)
|
2009-06-20 04:47:29 +08:00
|
|
|
{
|
2019-05-25 13:41:30 +08:00
|
|
|
int ret;
|
2009-06-20 04:47:29 +08:00
|
|
|
|
2014-03-06 23:59:26 +08:00
|
|
|
if (iommu_dummy(dev))
|
2019-04-11 00:14:06 +08:00
|
|
|
return false;
|
2009-07-04 17:40:38 +08:00
|
|
|
|
2019-05-25 13:41:30 +08:00
|
|
|
ret = identity_mapping(dev);
|
|
|
|
if (ret) {
|
|
|
|
u64 dma_mask = *dev->dma_mask;
|
|
|
|
|
|
|
|
if (dev->coherent_dma_mask && dev->coherent_dma_mask < dma_mask)
|
|
|
|
dma_mask = dev->coherent_dma_mask;
|
|
|
|
|
|
|
|
if (dma_mask >= dma_get_required_mask(dev))
|
2019-04-11 00:14:06 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 32 bit DMA is removed from si_domain and fall back to
|
|
|
|
* non-identity mapping.
|
|
|
|
*/
|
|
|
|
dmar_remove_one_dev_info(dev);
|
2019-05-25 13:41:30 +08:00
|
|
|
ret = iommu_request_dma_domain_for_dev(dev);
|
|
|
|
if (ret) {
|
|
|
|
struct iommu_domain *domain;
|
|
|
|
struct dmar_domain *dmar_domain;
|
|
|
|
|
|
|
|
domain = iommu_get_domain_for_dev(dev);
|
|
|
|
if (domain) {
|
|
|
|
dmar_domain = to_dmar_domain(domain);
|
|
|
|
dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
|
|
|
|
}
|
2019-08-06 08:14:08 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2019-05-25 13:41:33 +08:00
|
|
|
get_private_domain_for_dev(dev);
|
2009-06-20 04:47:29 +08:00
|
|
|
}
|
2019-05-25 13:41:30 +08:00
|
|
|
|
|
|
|
dev_info(dev, "32bit DMA uses non-identity mapping\n");
|
2009-06-20 04:47:29 +08:00
|
|
|
}
|
|
|
|
|
2019-04-11 00:14:06 +08:00
|
|
|
return true;
|
2009-06-20 04:47:29 +08:00
|
|
|
}
|
|
|
|
|
2019-01-23 05:30:45 +08:00
|
|
|
static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
|
|
|
|
size_t size, int dir, u64 dma_mask)
|
2007-10-22 07:41:58 +08:00
|
|
|
{
|
|
|
|
struct dmar_domain *domain;
|
2008-10-17 09:02:32 +08:00
|
|
|
phys_addr_t start_paddr;
|
2016-04-20 16:33:57 +08:00
|
|
|
unsigned long iova_pfn;
|
2007-10-22 07:41:58 +08:00
|
|
|
int prot = 0;
|
2008-04-22 17:09:04 +08:00
|
|
|
int ret;
|
2008-12-08 15:29:22 +08:00
|
|
|
struct intel_iommu *iommu;
|
2009-08-05 06:10:59 +08:00
|
|
|
unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
|
2007-10-22 07:41:58 +08:00
|
|
|
|
|
|
|
BUG_ON(dir == DMA_NONE);
|
2009-06-20 04:47:29 +08:00
|
|
|
|
2019-05-25 13:41:33 +08:00
|
|
|
domain = find_domain(dev);
|
2007-10-22 07:41:58 +08:00
|
|
|
if (!domain)
|
2018-11-22 02:34:10 +08:00
|
|
|
return DMA_MAPPING_ERROR;
|
2007-10-22 07:41:58 +08:00
|
|
|
|
2008-12-08 15:29:22 +08:00
|
|
|
iommu = domain_get_iommu(domain);
|
2009-06-28 22:03:06 +08:00
|
|
|
size = aligned_nrpages(paddr, size);
|
2007-10-22 07:41:58 +08:00
|
|
|
|
2016-04-20 16:33:57 +08:00
|
|
|
iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
|
|
|
|
if (!iova_pfn)
|
2007-10-22 07:41:58 +08:00
|
|
|
goto error;
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
/*
|
|
|
|
* Check if DMAR supports zero-length reads on write only
|
|
|
|
* mappings..
|
|
|
|
*/
|
|
|
|
if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
|
2008-12-08 15:29:22 +08:00
|
|
|
!cap_zlr(iommu->cap))
|
2007-10-22 07:41:49 +08:00
|
|
|
prot |= DMA_PTE_READ;
|
|
|
|
if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
|
|
|
|
prot |= DMA_PTE_WRITE;
|
|
|
|
/*
|
2008-04-22 17:09:04 +08:00
|
|
|
* paddr - (paddr + size) might be partial page, we should map the whole
|
2007-10-22 07:41:49 +08:00
|
|
|
* page. Note: if two part of one page are separately mapped, we
|
2008-04-22 17:09:04 +08:00
|
|
|
* might have two guest_addr mapping to the same host paddr, but this
|
2007-10-22 07:41:49 +08:00
|
|
|
* is not a big problem
|
|
|
|
*/
|
2016-04-20 16:33:57 +08:00
|
|
|
ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
|
2009-08-05 06:10:59 +08:00
|
|
|
mm_to_dma_pfn(paddr_pfn), size, prot);
|
2007-10-22 07:41:49 +08:00
|
|
|
if (ret)
|
|
|
|
goto error;
|
|
|
|
|
2016-04-20 16:33:57 +08:00
|
|
|
start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
|
2009-06-28 22:33:46 +08:00
|
|
|
start_paddr += paddr & ~PAGE_MASK;
|
2019-09-06 14:14:51 +08:00
|
|
|
|
|
|
|
trace_map_single(dev, start_paddr, paddr, size << VTD_PAGE_SHIFT);
|
|
|
|
|
2009-06-28 22:33:46 +08:00
|
|
|
return start_paddr;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
error:
|
2016-04-20 16:33:57 +08:00
|
|
|
if (iova_pfn)
|
2016-04-20 16:34:11 +08:00
|
|
|
free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "Device request: %zx@%llx dir %d --- failed\n",
|
|
|
|
size, (unsigned long long)paddr, dir);
|
2018-11-22 02:34:10 +08:00
|
|
|
return DMA_MAPPING_ERROR;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2009-01-05 22:47:26 +08:00
|
|
|
static dma_addr_t intel_map_page(struct device *dev, struct page *page,
|
|
|
|
unsigned long offset, size_t size,
|
|
|
|
enum dma_data_direction dir,
|
2016-08-04 04:46:00 +08:00
|
|
|
unsigned long attrs)
|
2008-10-15 15:08:28 +08:00
|
|
|
{
|
2019-04-11 00:14:07 +08:00
|
|
|
if (iommu_need_mapping(dev))
|
|
|
|
return __intel_map_single(dev, page_to_phys(page) + offset,
|
|
|
|
size, dir, *dev->dma_mask);
|
|
|
|
return dma_direct_map_page(dev, page, offset, size, dir, attrs);
|
2019-01-23 05:30:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static dma_addr_t intel_map_resource(struct device *dev, phys_addr_t phys_addr,
|
|
|
|
size_t size, enum dma_data_direction dir,
|
|
|
|
unsigned long attrs)
|
|
|
|
{
|
2019-04-11 00:14:07 +08:00
|
|
|
if (iommu_need_mapping(dev))
|
|
|
|
return __intel_map_single(dev, phys_addr, size, dir,
|
|
|
|
*dev->dma_mask);
|
|
|
|
return dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
|
2008-10-15 15:08:28 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 16:33:25 +08:00
|
|
|
static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2007-10-22 07:41:58 +08:00
|
|
|
struct dmar_domain *domain;
|
2009-06-28 07:27:49 +08:00
|
|
|
unsigned long start_pfn, last_pfn;
|
2016-04-20 16:33:25 +08:00
|
|
|
unsigned long nrpages;
|
2016-04-20 16:33:57 +08:00
|
|
|
unsigned long iova_pfn;
|
2008-12-08 15:29:22 +08:00
|
|
|
struct intel_iommu *iommu;
|
2014-03-06 01:09:32 +08:00
|
|
|
struct page *freelist;
|
2019-04-12 12:26:13 +08:00
|
|
|
struct pci_dev *pdev = NULL;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2014-03-07 00:19:30 +08:00
|
|
|
domain = find_domain(dev);
|
2007-10-22 07:41:49 +08:00
|
|
|
BUG_ON(!domain);
|
|
|
|
|
2008-12-08 15:29:22 +08:00
|
|
|
iommu = domain_get_iommu(domain);
|
|
|
|
|
2016-04-20 16:33:57 +08:00
|
|
|
iova_pfn = IOVA_PFN(dev_addr);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2016-04-20 16:33:25 +08:00
|
|
|
nrpages = aligned_nrpages(dev_addr, size);
|
2016-04-20 16:33:57 +08:00
|
|
|
start_pfn = mm_to_dma_pfn(iova_pfn);
|
2016-04-20 16:33:25 +08:00
|
|
|
last_pfn = start_pfn + nrpages - 1;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2019-04-12 12:26:13 +08:00
|
|
|
if (dev_is_pci(dev))
|
|
|
|
pdev = to_pci_dev(dev);
|
|
|
|
|
2014-03-06 01:09:32 +08:00
|
|
|
freelist = domain_unmap(domain, start_pfn, last_pfn);
|
iommu/vt-d: Don't queue_iova() if there is no flush queue
Intel VT-d driver was reworked to use common deferred flushing
implementation. Previously there was one global per-cpu flush queue,
afterwards - one per domain.
Before deferring a flush, the queue should be allocated and initialized.
Currently only domains with IOMMU_DOMAIN_DMA type initialize their flush
queue. It's probably worth to init it for static or unmanaged domains
too, but it may be arguable - I'm leaving it to iommu folks.
Prevent queuing an iova flush if the domain doesn't have a queue.
The defensive check seems to be worth to keep even if queue would be
initialized for all kinds of domains. And is easy backportable.
On 4.19.43 stable kernel it has a user-visible effect: previously for
devices in si domain there were crashes, on sata devices:
BUG: spinlock bad magic on CPU#6, swapper/0/1
lock: 0xffff88844f582008, .magic: 00000000, .owner: <none>/-1, .owner_cpu: 0
CPU: 6 PID: 1 Comm: swapper/0 Not tainted 4.19.43 #1
Call Trace:
<IRQ>
dump_stack+0x61/0x7e
spin_bug+0x9d/0xa3
do_raw_spin_lock+0x22/0x8e
_raw_spin_lock_irqsave+0x32/0x3a
queue_iova+0x45/0x115
intel_unmap+0x107/0x113
intel_unmap_sg+0x6b/0x76
__ata_qc_complete+0x7f/0x103
ata_qc_complete+0x9b/0x26a
ata_qc_complete_multiple+0xd0/0xe3
ahci_handle_port_interrupt+0x3ee/0x48a
ahci_handle_port_intr+0x73/0xa9
ahci_single_level_irq_intr+0x40/0x60
__handle_irq_event_percpu+0x7f/0x19a
handle_irq_event_percpu+0x32/0x72
handle_irq_event+0x38/0x56
handle_edge_irq+0x102/0x121
handle_irq+0x147/0x15c
do_IRQ+0x66/0xf2
common_interrupt+0xf/0xf
RIP: 0010:__do_softirq+0x8c/0x2df
The same for usb devices that use ehci-pci:
BUG: spinlock bad magic on CPU#0, swapper/0/1
lock: 0xffff88844f402008, .magic: 00000000, .owner: <none>/-1, .owner_cpu: 0
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.19.43 #4
Call Trace:
<IRQ>
dump_stack+0x61/0x7e
spin_bug+0x9d/0xa3
do_raw_spin_lock+0x22/0x8e
_raw_spin_lock_irqsave+0x32/0x3a
queue_iova+0x77/0x145
intel_unmap+0x107/0x113
intel_unmap_page+0xe/0x10
usb_hcd_unmap_urb_setup_for_dma+0x53/0x9d
usb_hcd_unmap_urb_for_dma+0x17/0x100
unmap_urb_for_dma+0x22/0x24
__usb_hcd_giveback_urb+0x51/0xc3
usb_giveback_urb_bh+0x97/0xde
tasklet_action_common.isra.4+0x5f/0xa1
tasklet_action+0x2d/0x30
__do_softirq+0x138/0x2df
irq_exit+0x7d/0x8b
smp_apic_timer_interrupt+0x10f/0x151
apic_timer_interrupt+0xf/0x20
</IRQ>
RIP: 0010:_raw_spin_unlock_irqrestore+0x17/0x39
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Lu Baolu <baolu.lu@linux.intel.com>
Cc: iommu@lists.linux-foundation.org
Cc: <stable@vger.kernel.org> # 4.14+
Fixes: 13cf01744608 ("iommu/vt-d: Make use of iova deferred flushing")
Signed-off-by: Dmitry Safonov <dima@arista.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2019-07-17 05:38:05 +08:00
|
|
|
if (intel_iommu_strict || (pdev && pdev->untrusted) ||
|
|
|
|
!has_iova_flush_queue(&domain->iovad)) {
|
2015-07-21 21:20:32 +08:00
|
|
|
iommu_flush_iotlb_psi(iommu, domain, start_pfn,
|
2016-04-20 16:33:25 +08:00
|
|
|
nrpages, !freelist, 0);
|
2008-03-05 07:22:08 +08:00
|
|
|
/* free iova */
|
2016-04-20 16:34:11 +08:00
|
|
|
free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
|
2014-03-06 01:09:32 +08:00
|
|
|
dma_free_pagelist(freelist);
|
2008-03-05 07:22:08 +08:00
|
|
|
} else {
|
2017-08-11 17:40:10 +08:00
|
|
|
queue_iova(&domain->iovad, iova_pfn, nrpages,
|
|
|
|
(unsigned long)freelist);
|
2008-03-05 07:22:08 +08:00
|
|
|
/*
|
|
|
|
* queue up the release of the unmap to save the 1/6th of the
|
|
|
|
* cpu used up by the iotlb flush operation...
|
|
|
|
*/
|
|
|
|
}
|
2019-09-06 14:14:51 +08:00
|
|
|
|
|
|
|
trace_unmap_single(dev, dev_addr, size);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-07-11 14:19:34 +08:00
|
|
|
static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
|
|
|
|
size_t size, enum dma_data_direction dir,
|
2016-08-04 04:46:00 +08:00
|
|
|
unsigned long attrs)
|
2014-07-11 14:19:34 +08:00
|
|
|
{
|
2019-04-11 00:14:07 +08:00
|
|
|
if (iommu_need_mapping(dev))
|
|
|
|
intel_unmap(dev, dev_addr, size);
|
|
|
|
else
|
|
|
|
dma_direct_unmap_page(dev, dev_addr, size, dir, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void intel_unmap_resource(struct device *dev, dma_addr_t dev_addr,
|
|
|
|
size_t size, enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
if (iommu_need_mapping(dev))
|
|
|
|
intel_unmap(dev, dev_addr, size);
|
2014-07-11 14:19:34 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 07:14:00 +08:00
|
|
|
static void *intel_alloc_coherent(struct device *dev, size_t size,
|
2012-03-27 20:28:18 +08:00
|
|
|
dma_addr_t *dma_handle, gfp_t flags,
|
2016-08-04 04:46:00 +08:00
|
|
|
unsigned long attrs)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2018-07-06 03:29:55 +08:00
|
|
|
struct page *page = NULL;
|
|
|
|
int order;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2019-04-11 00:14:07 +08:00
|
|
|
if (!iommu_need_mapping(dev))
|
|
|
|
return dma_direct_alloc(dev, size, dma_handle, flags, attrs);
|
|
|
|
|
2018-07-06 03:29:55 +08:00
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
order = get_order(size);
|
|
|
|
|
|
|
|
if (gfpflags_allow_blocking(flags)) {
|
|
|
|
unsigned int count = size >> PAGE_SHIFT;
|
|
|
|
|
2018-08-18 06:49:00 +08:00
|
|
|
page = dma_alloc_from_contiguous(dev, count, order,
|
|
|
|
flags & __GFP_NOWARN);
|
2018-07-06 03:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!page)
|
|
|
|
page = alloc_pages(flags, order);
|
|
|
|
if (!page)
|
|
|
|
return NULL;
|
|
|
|
memset(page_address(page), 0, size);
|
|
|
|
|
2019-01-23 05:30:45 +08:00
|
|
|
*dma_handle = __intel_map_single(dev, page_to_phys(page), size,
|
|
|
|
DMA_BIDIRECTIONAL,
|
|
|
|
dev->coherent_dma_mask);
|
2018-11-22 02:34:10 +08:00
|
|
|
if (*dma_handle != DMA_MAPPING_ERROR)
|
2018-07-06 03:29:55 +08:00
|
|
|
return page_address(page);
|
|
|
|
if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
|
|
|
|
__free_pages(page, order);
|
2014-06-05 07:06:51 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-10 07:14:00 +08:00
|
|
|
static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
|
2016-08-04 04:46:00 +08:00
|
|
|
dma_addr_t dma_handle, unsigned long attrs)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2018-07-06 03:29:55 +08:00
|
|
|
int order;
|
|
|
|
struct page *page = virt_to_page(vaddr);
|
|
|
|
|
2019-04-11 00:14:07 +08:00
|
|
|
if (!iommu_need_mapping(dev))
|
|
|
|
return dma_direct_free(dev, size, vaddr, dma_handle, attrs);
|
|
|
|
|
2018-07-06 03:29:55 +08:00
|
|
|
size = PAGE_ALIGN(size);
|
|
|
|
order = get_order(size);
|
|
|
|
|
|
|
|
intel_unmap(dev, dma_handle, size);
|
|
|
|
if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
|
|
|
|
__free_pages(page, order);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 07:14:00 +08:00
|
|
|
static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
|
2009-01-28 20:53:18 +08:00
|
|
|
int nelems, enum dma_data_direction dir,
|
2016-08-04 04:46:00 +08:00
|
|
|
unsigned long attrs)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
2016-04-20 16:33:25 +08:00
|
|
|
dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK;
|
|
|
|
unsigned long nrpages = 0;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
|
|
|
|
2019-04-11 00:14:07 +08:00
|
|
|
if (!iommu_need_mapping(dev))
|
|
|
|
return dma_direct_unmap_sg(dev, sglist, nelems, dir, attrs);
|
|
|
|
|
2016-04-20 16:33:25 +08:00
|
|
|
for_each_sg(sglist, sg, nelems, i) {
|
|
|
|
nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg));
|
|
|
|
}
|
|
|
|
|
|
|
|
intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
|
2019-09-06 14:14:51 +08:00
|
|
|
|
|
|
|
trace_unmap_sg(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 07:14:00 +08:00
|
|
|
static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
|
2016-08-04 04:46:00 +08:00
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
2007-10-22 07:41:49 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dmar_domain *domain;
|
2007-10-22 07:41:58 +08:00
|
|
|
size_t size = 0;
|
|
|
|
int prot = 0;
|
2016-04-20 16:33:57 +08:00
|
|
|
unsigned long iova_pfn;
|
2007-10-22 07:41:58 +08:00
|
|
|
int ret;
|
2007-10-22 07:42:00 +08:00
|
|
|
struct scatterlist *sg;
|
2009-06-28 21:49:31 +08:00
|
|
|
unsigned long start_vpfn;
|
2008-12-08 15:29:22 +08:00
|
|
|
struct intel_iommu *iommu;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
BUG_ON(dir == DMA_NONE);
|
2019-04-11 00:14:06 +08:00
|
|
|
if (!iommu_need_mapping(dev))
|
2019-04-11 00:14:07 +08:00
|
|
|
return dma_direct_map_sg(dev, sglist, nelems, dir, attrs);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2019-05-25 13:41:33 +08:00
|
|
|
domain = find_domain(dev);
|
2007-10-22 07:41:58 +08:00
|
|
|
if (!domain)
|
|
|
|
return 0;
|
|
|
|
|
2008-12-08 15:29:22 +08:00
|
|
|
iommu = domain_get_iommu(domain);
|
|
|
|
|
2009-06-28 21:49:31 +08:00
|
|
|
for_each_sg(sglist, sg, nelems, i)
|
2009-06-28 22:03:06 +08:00
|
|
|
size += aligned_nrpages(sg->offset, sg->length);
|
2007-10-22 07:41:58 +08:00
|
|
|
|
2016-04-20 16:33:57 +08:00
|
|
|
iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
|
2014-03-10 07:14:00 +08:00
|
|
|
*dev->dma_mask);
|
2016-04-20 16:33:57 +08:00
|
|
|
if (!iova_pfn) {
|
2007-10-22 07:42:00 +08:00
|
|
|
sglist->dma_length = 0;
|
2007-10-22 07:41:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if DMAR supports zero-length reads on write only
|
|
|
|
* mappings..
|
|
|
|
*/
|
|
|
|
if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
|
2008-12-08 15:29:22 +08:00
|
|
|
!cap_zlr(iommu->cap))
|
2007-10-22 07:41:58 +08:00
|
|
|
prot |= DMA_PTE_READ;
|
|
|
|
if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
|
|
|
|
prot |= DMA_PTE_WRITE;
|
|
|
|
|
2016-04-20 16:33:57 +08:00
|
|
|
start_vpfn = mm_to_dma_pfn(iova_pfn);
|
2009-06-29 18:17:38 +08:00
|
|
|
|
2009-08-05 06:09:37 +08:00
|
|
|
ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
|
2009-06-29 18:17:38 +08:00
|
|
|
if (unlikely(ret)) {
|
|
|
|
dma_pte_free_pagetable(domain, start_vpfn,
|
2017-06-29 10:42:23 +08:00
|
|
|
start_vpfn + size - 1,
|
|
|
|
agaw_to_level(domain->agaw) + 1);
|
2016-04-20 16:34:11 +08:00
|
|
|
free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
|
2009-06-29 18:17:38 +08:00
|
|
|
return 0;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 14:14:51 +08:00
|
|
|
trace_map_sg(dev, iova_pfn << PAGE_SHIFT,
|
|
|
|
sg_phys(sglist), size << VTD_PAGE_SHIFT);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
return nelems;
|
|
|
|
}
|
|
|
|
|
2018-09-18 01:10:31 +08:00
|
|
|
static const struct dma_map_ops intel_dma_ops = {
|
2012-03-27 20:28:18 +08:00
|
|
|
.alloc = intel_alloc_coherent,
|
|
|
|
.free = intel_free_coherent,
|
2007-10-22 07:41:49 +08:00
|
|
|
.map_sg = intel_map_sg,
|
|
|
|
.unmap_sg = intel_unmap_sg,
|
2009-01-05 22:47:26 +08:00
|
|
|
.map_page = intel_map_page,
|
|
|
|
.unmap_page = intel_unmap_page,
|
2019-01-23 05:30:45 +08:00
|
|
|
.map_resource = intel_map_resource,
|
2019-04-11 00:14:07 +08:00
|
|
|
.unmap_resource = intel_unmap_resource,
|
2018-03-19 18:38:15 +08:00
|
|
|
.dma_supported = dma_direct_supported,
|
2019-08-06 20:01:50 +08:00
|
|
|
.mmap = dma_common_mmap,
|
|
|
|
.get_sgtable = dma_common_get_sgtable,
|
2007-10-22 07:41:49 +08:00
|
|
|
};
|
|
|
|
|
2019-09-06 14:14:52 +08:00
|
|
|
static void
|
|
|
|
bounce_sync_single(struct device *dev, dma_addr_t addr, size_t size,
|
|
|
|
enum dma_data_direction dir, enum dma_sync_target target)
|
|
|
|
{
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
phys_addr_t tlb_addr;
|
|
|
|
|
|
|
|
domain = find_domain(dev);
|
|
|
|
if (WARN_ON(!domain))
|
|
|
|
return;
|
|
|
|
|
|
|
|
tlb_addr = intel_iommu_iova_to_phys(&domain->domain, addr);
|
|
|
|
if (is_swiotlb_buffer(tlb_addr))
|
|
|
|
swiotlb_tbl_sync_single(dev, tlb_addr, size, dir, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
static dma_addr_t
|
|
|
|
bounce_map_single(struct device *dev, phys_addr_t paddr, size_t size,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs,
|
|
|
|
u64 dma_mask)
|
|
|
|
{
|
|
|
|
size_t aligned_size = ALIGN(size, VTD_PAGE_SIZE);
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
unsigned long iova_pfn;
|
|
|
|
unsigned long nrpages;
|
|
|
|
phys_addr_t tlb_addr;
|
|
|
|
int prot = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
domain = find_domain(dev);
|
|
|
|
if (WARN_ON(dir == DMA_NONE || !domain))
|
|
|
|
return DMA_MAPPING_ERROR;
|
|
|
|
|
|
|
|
iommu = domain_get_iommu(domain);
|
|
|
|
if (WARN_ON(!iommu))
|
|
|
|
return DMA_MAPPING_ERROR;
|
|
|
|
|
|
|
|
nrpages = aligned_nrpages(0, size);
|
|
|
|
iova_pfn = intel_alloc_iova(dev, domain,
|
|
|
|
dma_to_mm_pfn(nrpages), dma_mask);
|
|
|
|
if (!iova_pfn)
|
|
|
|
return DMA_MAPPING_ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if DMAR supports zero-length reads on write only
|
|
|
|
* mappings..
|
|
|
|
*/
|
|
|
|
if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL ||
|
|
|
|
!cap_zlr(iommu->cap))
|
|
|
|
prot |= DMA_PTE_READ;
|
|
|
|
if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
|
|
|
|
prot |= DMA_PTE_WRITE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If both the physical buffer start address and size are
|
|
|
|
* page aligned, we don't need to use a bounce page.
|
|
|
|
*/
|
|
|
|
if (!IS_ALIGNED(paddr | size, VTD_PAGE_SIZE)) {
|
|
|
|
tlb_addr = swiotlb_tbl_map_single(dev,
|
|
|
|
__phys_to_dma(dev, io_tlb_start),
|
|
|
|
paddr, size, aligned_size, dir, attrs);
|
|
|
|
if (tlb_addr == DMA_MAPPING_ERROR) {
|
|
|
|
goto swiotlb_error;
|
|
|
|
} else {
|
|
|
|
/* Cleanup the padding area. */
|
|
|
|
void *padding_start = phys_to_virt(tlb_addr);
|
|
|
|
size_t padding_size = aligned_size;
|
|
|
|
|
|
|
|
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
|
|
|
|
(dir == DMA_TO_DEVICE ||
|
|
|
|
dir == DMA_BIDIRECTIONAL)) {
|
|
|
|
padding_start += size;
|
|
|
|
padding_size -= size;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(padding_start, 0, padding_size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tlb_addr = paddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
|
|
|
|
tlb_addr >> VTD_PAGE_SHIFT, nrpages, prot);
|
|
|
|
if (ret)
|
|
|
|
goto mapping_error;
|
|
|
|
|
|
|
|
trace_bounce_map_single(dev, iova_pfn << PAGE_SHIFT, paddr, size);
|
|
|
|
|
|
|
|
return (phys_addr_t)iova_pfn << PAGE_SHIFT;
|
|
|
|
|
|
|
|
mapping_error:
|
|
|
|
if (is_swiotlb_buffer(tlb_addr))
|
|
|
|
swiotlb_tbl_unmap_single(dev, tlb_addr, size,
|
|
|
|
aligned_size, dir, attrs);
|
|
|
|
swiotlb_error:
|
|
|
|
free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
|
|
|
|
dev_err(dev, "Device bounce map: %zx@%llx dir %d --- failed\n",
|
|
|
|
size, (unsigned long long)paddr, dir);
|
|
|
|
|
|
|
|
return DMA_MAPPING_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
size_t aligned_size = ALIGN(size, VTD_PAGE_SIZE);
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
phys_addr_t tlb_addr;
|
|
|
|
|
|
|
|
domain = find_domain(dev);
|
|
|
|
if (WARN_ON(!domain))
|
|
|
|
return;
|
|
|
|
|
|
|
|
tlb_addr = intel_iommu_iova_to_phys(&domain->domain, dev_addr);
|
|
|
|
if (WARN_ON(!tlb_addr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
intel_unmap(dev, dev_addr, size);
|
|
|
|
if (is_swiotlb_buffer(tlb_addr))
|
|
|
|
swiotlb_tbl_unmap_single(dev, tlb_addr, size,
|
|
|
|
aligned_size, dir, attrs);
|
|
|
|
|
|
|
|
trace_bounce_unmap_single(dev, dev_addr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static dma_addr_t
|
|
|
|
bounce_map_page(struct device *dev, struct page *page, unsigned long offset,
|
|
|
|
size_t size, enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
return bounce_map_single(dev, page_to_phys(page) + offset,
|
|
|
|
size, dir, attrs, *dev->dma_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static dma_addr_t
|
|
|
|
bounce_map_resource(struct device *dev, phys_addr_t phys_addr, size_t size,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
return bounce_map_single(dev, phys_addr, size,
|
|
|
|
dir, attrs, *dev->dma_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_unmap_page(struct device *dev, dma_addr_t dev_addr, size_t size,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
bounce_unmap_single(dev, dev_addr, size, dir, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_unmap_resource(struct device *dev, dma_addr_t dev_addr, size_t size,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
bounce_unmap_single(dev, dev_addr, size, dir, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_unmap_sg(struct device *dev, struct scatterlist *sglist, int nelems,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_sg(sglist, sg, nelems, i)
|
|
|
|
bounce_unmap_page(dev, sg->dma_address,
|
|
|
|
sg_dma_len(sg), dir, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bounce_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
|
|
|
|
enum dma_data_direction dir, unsigned long attrs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
|
|
|
|
for_each_sg(sglist, sg, nelems, i) {
|
|
|
|
sg->dma_address = bounce_map_page(dev, sg_page(sg),
|
|
|
|
sg->offset, sg->length,
|
|
|
|
dir, attrs);
|
|
|
|
if (sg->dma_address == DMA_MAPPING_ERROR)
|
|
|
|
goto out_unmap;
|
|
|
|
sg_dma_len(sg) = sg->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nelems;
|
|
|
|
|
|
|
|
out_unmap:
|
|
|
|
bounce_unmap_sg(dev, sglist, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
|
|
|
|
size_t size, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
bounce_sync_single(dev, addr, size, dir, SYNC_FOR_CPU);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_sync_single_for_device(struct device *dev, dma_addr_t addr,
|
|
|
|
size_t size, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
bounce_sync_single(dev, addr, size, dir, SYNC_FOR_DEVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_sync_sg_for_cpu(struct device *dev, struct scatterlist *sglist,
|
|
|
|
int nelems, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_sg(sglist, sg, nelems, i)
|
|
|
|
bounce_sync_single(dev, sg_dma_address(sg),
|
|
|
|
sg_dma_len(sg), dir, SYNC_FOR_CPU);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bounce_sync_sg_for_device(struct device *dev, struct scatterlist *sglist,
|
|
|
|
int nelems, enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_sg(sglist, sg, nelems, i)
|
|
|
|
bounce_sync_single(dev, sg_dma_address(sg),
|
|
|
|
sg_dma_len(sg), dir, SYNC_FOR_DEVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dma_map_ops bounce_dma_ops = {
|
|
|
|
.alloc = intel_alloc_coherent,
|
|
|
|
.free = intel_free_coherent,
|
|
|
|
.map_sg = bounce_map_sg,
|
|
|
|
.unmap_sg = bounce_unmap_sg,
|
|
|
|
.map_page = bounce_map_page,
|
|
|
|
.unmap_page = bounce_unmap_page,
|
|
|
|
.sync_single_for_cpu = bounce_sync_single_for_cpu,
|
|
|
|
.sync_single_for_device = bounce_sync_single_for_device,
|
|
|
|
.sync_sg_for_cpu = bounce_sync_sg_for_cpu,
|
|
|
|
.sync_sg_for_device = bounce_sync_sg_for_device,
|
|
|
|
.map_resource = bounce_map_resource,
|
|
|
|
.unmap_resource = bounce_unmap_resource,
|
|
|
|
.dma_supported = dma_direct_supported,
|
|
|
|
};
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static inline int iommu_domain_cache_init(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
iommu_domain_cache = kmem_cache_create("iommu_domain",
|
|
|
|
sizeof(struct dmar_domain),
|
|
|
|
0,
|
|
|
|
SLAB_HWCACHE_ALIGN,
|
|
|
|
|
|
|
|
NULL);
|
|
|
|
if (!iommu_domain_cache) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Couldn't create iommu_domain cache\n");
|
2007-10-22 07:41:49 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int iommu_devinfo_cache_init(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
|
|
|
|
sizeof(struct device_domain_info),
|
|
|
|
0,
|
|
|
|
SLAB_HWCACHE_ALIGN,
|
|
|
|
NULL);
|
|
|
|
if (!iommu_devinfo_cache) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Couldn't create devinfo cache\n");
|
2007-10-22 07:41:49 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init iommu_init_mempool(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2015-07-13 19:31:28 +08:00
|
|
|
ret = iova_cache_get();
|
2007-10-22 07:41:49 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = iommu_domain_cache_init();
|
|
|
|
if (ret)
|
|
|
|
goto domain_error;
|
|
|
|
|
|
|
|
ret = iommu_devinfo_cache_init();
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
kmem_cache_destroy(iommu_domain_cache);
|
|
|
|
domain_error:
|
2015-07-13 19:31:28 +08:00
|
|
|
iova_cache_put();
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init iommu_exit_mempool(void)
|
|
|
|
{
|
|
|
|
kmem_cache_destroy(iommu_devinfo_cache);
|
|
|
|
kmem_cache_destroy(iommu_domain_cache);
|
2015-07-13 19:31:28 +08:00
|
|
|
iova_cache_put();
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
|
2010-07-24 06:47:56 +08:00
|
|
|
static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
u32 vtbar;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* We know that this device on this chipset has its own IOMMU.
|
|
|
|
* If we find it under a different IOMMU, then the BIOS is lying
|
|
|
|
* to us. Hope that the IOMMU for this device is actually
|
|
|
|
* disabled, and it needs no translation...
|
|
|
|
*/
|
|
|
|
rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
|
|
|
|
if (rc) {
|
|
|
|
/* "can't" happen */
|
|
|
|
dev_info(&pdev->dev, "failed to run vt-d quirk\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vtbar &= 0xffff0000;
|
|
|
|
|
|
|
|
/* we know that the this iommu should be at offset 0xa000 from vtbar */
|
|
|
|
drhd = dmar_find_matched_drhd_unit(pdev);
|
|
|
|
if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
|
|
|
|
TAINT_FIRMWARE_WORKAROUND,
|
|
|
|
"BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
|
|
|
|
pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
|
|
|
|
}
|
|
|
|
DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
static void __init init_no_remapping_devices(void)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
2014-03-07 23:08:36 +08:00
|
|
|
struct device *dev;
|
2014-02-19 14:07:32 +08:00
|
|
|
int i;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
|
|
|
for_each_drhd_unit(drhd) {
|
|
|
|
if (!drhd->include_all) {
|
2014-02-19 14:07:32 +08:00
|
|
|
for_each_active_dev_scope(drhd->devices,
|
|
|
|
drhd->devices_cnt, i, dev)
|
|
|
|
break;
|
2014-03-07 23:08:36 +08:00
|
|
|
/* ignore DMAR unit if no devices exist */
|
2007-10-22 07:41:49 +08:00
|
|
|
if (i == drhd->devices_cnt)
|
|
|
|
drhd->ignored = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:18:18 +08:00
|
|
|
for_each_active_drhd_unit(drhd) {
|
|
|
|
if (drhd->include_all)
|
2007-10-22 07:41:49 +08:00
|
|
|
continue;
|
|
|
|
|
2014-02-19 14:07:32 +08:00
|
|
|
for_each_active_dev_scope(drhd->devices,
|
|
|
|
drhd->devices_cnt, i, dev)
|
2014-03-07 23:08:36 +08:00
|
|
|
if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
|
2007-10-22 07:41:49 +08:00
|
|
|
break;
|
|
|
|
if (i < drhd->devices_cnt)
|
|
|
|
continue;
|
|
|
|
|
2011-10-15 03:59:46 +08:00
|
|
|
/* This IOMMU has *only* gfx devices. Either bypass it or
|
|
|
|
set the gfx_mapped flag, as appropriate */
|
2019-05-02 09:34:25 +08:00
|
|
|
if (!dmar_map_gfx) {
|
2011-10-15 03:59:46 +08:00
|
|
|
drhd->ignored = 1;
|
2014-02-19 14:07:32 +08:00
|
|
|
for_each_active_dev_scope(drhd->devices,
|
|
|
|
drhd->devices_cnt, i, dev)
|
2014-03-07 23:08:36 +08:00
|
|
|
dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 05:22:42 +08:00
|
|
|
#ifdef CONFIG_SUSPEND
|
|
|
|
static int init_iommu_hw(void)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu = NULL;
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd)
|
|
|
|
if (iommu->qi)
|
|
|
|
dmar_reenable_qi(iommu);
|
|
|
|
|
2011-05-03 15:08:37 +08:00
|
|
|
for_each_iommu(iommu, drhd) {
|
|
|
|
if (drhd->ignored) {
|
|
|
|
/*
|
|
|
|
* we always have to disable PMRs or DMA may fail on
|
|
|
|
* this device
|
|
|
|
*/
|
|
|
|
if (force_on)
|
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-29 09:16:02 +08:00
|
|
|
|
2009-03-28 05:22:42 +08:00
|
|
|
iommu_flush_write_buffer(iommu);
|
|
|
|
|
|
|
|
iommu_set_root_entry(iommu);
|
|
|
|
|
|
|
|
iommu->flush.flush_context(iommu, 0, 0, 0,
|
2009-05-11 02:58:49 +08:00
|
|
|
DMA_CCMD_GLOBAL_INVL);
|
2014-07-11 14:19:33 +08:00
|
|
|
iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
|
|
|
|
iommu_enable_translation(iommu);
|
2009-09-20 06:28:12 +08:00
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
2009-03-28 05:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iommu_flush_all(void)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
iommu->flush.flush_context(iommu, 0, 0, 0,
|
2009-05-11 02:58:49 +08:00
|
|
|
DMA_CCMD_GLOBAL_INVL);
|
2009-03-28 05:22:42 +08:00
|
|
|
iommu->flush.flush_iotlb(iommu, 0, 0, 0,
|
2009-05-11 02:58:49 +08:00
|
|
|
DMA_TLB_GLOBAL_FLUSH);
|
2009-03-28 05:22:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-24 05:16:14 +08:00
|
|
|
static int iommu_suspend(void)
|
2009-03-28 05:22:42 +08:00
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu = NULL;
|
|
|
|
unsigned long flag;
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32),
|
2009-03-28 05:22:42 +08:00
|
|
|
GFP_ATOMIC);
|
|
|
|
if (!iommu->iommu_state)
|
|
|
|
goto nomem;
|
|
|
|
}
|
|
|
|
|
|
|
|
iommu_flush_all();
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
iommu_disable_translation(iommu);
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2009-03-28 05:22:42 +08:00
|
|
|
|
|
|
|
iommu->iommu_state[SR_DMAR_FECTL_REG] =
|
|
|
|
readl(iommu->reg + DMAR_FECTL_REG);
|
|
|
|
iommu->iommu_state[SR_DMAR_FEDATA_REG] =
|
|
|
|
readl(iommu->reg + DMAR_FEDATA_REG);
|
|
|
|
iommu->iommu_state[SR_DMAR_FEADDR_REG] =
|
|
|
|
readl(iommu->reg + DMAR_FEADDR_REG);
|
|
|
|
iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
|
|
|
|
readl(iommu->reg + DMAR_FEUADDR_REG);
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2009-03-28 05:22:42 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
for_each_active_iommu(iommu, drhd)
|
|
|
|
kfree(iommu->iommu_state);
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2011-03-24 05:16:14 +08:00
|
|
|
static void iommu_resume(void)
|
2009-03-28 05:22:42 +08:00
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu = NULL;
|
|
|
|
unsigned long flag;
|
|
|
|
|
|
|
|
if (init_iommu_hw()) {
|
2011-05-03 15:08:37 +08:00
|
|
|
if (force_on)
|
|
|
|
panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
|
|
|
|
else
|
|
|
|
WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
|
2011-03-24 05:16:14 +08:00
|
|
|
return;
|
2009-03-28 05:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_lock_irqsave(&iommu->register_lock, flag);
|
2009-03-28 05:22:42 +08:00
|
|
|
|
|
|
|
writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
|
|
|
|
iommu->reg + DMAR_FECTL_REG);
|
|
|
|
writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
|
|
|
|
iommu->reg + DMAR_FEDATA_REG);
|
|
|
|
writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
|
|
|
|
iommu->reg + DMAR_FEADDR_REG);
|
|
|
|
writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
|
|
|
|
iommu->reg + DMAR_FEUADDR_REG);
|
|
|
|
|
2011-07-19 22:19:51 +08:00
|
|
|
raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
|
2009-03-28 05:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd)
|
|
|
|
kfree(iommu->iommu_state);
|
|
|
|
}
|
|
|
|
|
2011-03-24 05:16:14 +08:00
|
|
|
static struct syscore_ops iommu_syscore_ops = {
|
2009-03-28 05:22:42 +08:00
|
|
|
.resume = iommu_resume,
|
|
|
|
.suspend = iommu_suspend,
|
|
|
|
};
|
|
|
|
|
2011-03-24 05:16:14 +08:00
|
|
|
static void __init init_iommu_pm_ops(void)
|
2009-03-28 05:22:42 +08:00
|
|
|
{
|
2011-03-24 05:16:14 +08:00
|
|
|
register_syscore_ops(&iommu_syscore_ops);
|
2009-03-28 05:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2011-06-08 03:32:31 +08:00
|
|
|
static inline void init_iommu_pm_ops(void) {}
|
2009-03-28 05:22:42 +08:00
|
|
|
#endif /* CONFIG_PM */
|
|
|
|
|
2014-11-09 22:47:56 +08:00
|
|
|
int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
|
2011-08-24 08:05:20 +08:00
|
|
|
{
|
|
|
|
struct acpi_dmar_reserved_memory *rmrr;
|
|
|
|
struct dmar_rmrr_unit *rmrru;
|
|
|
|
|
|
|
|
rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
|
|
|
|
if (!rmrru)
|
2017-01-20 04:57:53 +08:00
|
|
|
goto out;
|
2011-08-24 08:05:20 +08:00
|
|
|
|
|
|
|
rmrru->hdr = header;
|
|
|
|
rmrr = (struct acpi_dmar_reserved_memory *)header;
|
|
|
|
rmrru->base_address = rmrr->base_address;
|
|
|
|
rmrru->end_address = rmrr->end_address;
|
2017-01-20 04:57:53 +08:00
|
|
|
|
2014-02-19 14:07:36 +08:00
|
|
|
rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
|
|
|
|
((void *)rmrr) + rmrr->header.length,
|
|
|
|
&rmrru->devices_cnt);
|
2017-01-20 04:57:53 +08:00
|
|
|
if (rmrru->devices_cnt && rmrru->devices == NULL)
|
2019-06-03 14:53:31 +08:00
|
|
|
goto free_rmrru;
|
2011-08-24 08:05:20 +08:00
|
|
|
|
2014-02-19 14:07:36 +08:00
|
|
|
list_add(&rmrru->list, &dmar_rmrr_units);
|
2011-08-24 08:05:20 +08:00
|
|
|
|
2014-02-19 14:07:36 +08:00
|
|
|
return 0;
|
2017-01-20 04:57:53 +08:00
|
|
|
free_rmrru:
|
|
|
|
kfree(rmrru);
|
|
|
|
out:
|
|
|
|
return -ENOMEM;
|
2011-08-24 08:05:20 +08:00
|
|
|
}
|
|
|
|
|
2014-11-09 22:47:58 +08:00
|
|
|
static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
|
|
|
|
{
|
|
|
|
struct dmar_atsr_unit *atsru;
|
|
|
|
struct acpi_dmar_atsr *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
|
|
|
|
tmp = (struct acpi_dmar_atsr *)atsru->hdr;
|
|
|
|
if (atsr->segment != tmp->segment)
|
|
|
|
continue;
|
|
|
|
if (atsr->header.length != tmp->header.length)
|
|
|
|
continue;
|
|
|
|
if (memcmp(atsr, tmp, atsr->header.length) == 0)
|
|
|
|
return atsru;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
|
2011-08-24 08:05:20 +08:00
|
|
|
{
|
|
|
|
struct acpi_dmar_atsr *atsr;
|
|
|
|
struct dmar_atsr_unit *atsru;
|
|
|
|
|
2017-05-17 02:42:41 +08:00
|
|
|
if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
|
2014-11-09 22:47:58 +08:00
|
|
|
return 0;
|
|
|
|
|
2011-08-24 08:05:20 +08:00
|
|
|
atsr = container_of(hdr, struct acpi_dmar_atsr, header);
|
2014-11-09 22:47:58 +08:00
|
|
|
atsru = dmar_find_atsr(atsr);
|
|
|
|
if (atsru)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
|
2011-08-24 08:05:20 +08:00
|
|
|
if (!atsru)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-11-09 22:47:58 +08:00
|
|
|
/*
|
|
|
|
* If memory is allocated from slab by ACPI _DSM method, we need to
|
|
|
|
* copy the memory content because the memory buffer will be freed
|
|
|
|
* on return.
|
|
|
|
*/
|
|
|
|
atsru->hdr = (void *)(atsru + 1);
|
|
|
|
memcpy(atsru->hdr, hdr, hdr->length);
|
2011-08-24 08:05:20 +08:00
|
|
|
atsru->include_all = atsr->flags & 0x1;
|
2014-02-19 14:07:36 +08:00
|
|
|
if (!atsru->include_all) {
|
|
|
|
atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
|
|
|
|
(void *)atsr + atsr->header.length,
|
|
|
|
&atsru->devices_cnt);
|
|
|
|
if (atsru->devices_cnt && atsru->devices == NULL) {
|
|
|
|
kfree(atsru);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
2011-08-24 08:05:20 +08:00
|
|
|
|
2014-02-19 14:07:34 +08:00
|
|
|
list_add_rcu(&atsru->list, &dmar_atsr_units);
|
2011-08-24 08:05:20 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:18:27 +08:00
|
|
|
static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
|
|
|
|
{
|
|
|
|
dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
|
|
|
|
kfree(atsru);
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:47:58 +08:00
|
|
|
int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
|
|
|
|
{
|
|
|
|
struct acpi_dmar_atsr *atsr;
|
|
|
|
struct dmar_atsr_unit *atsru;
|
|
|
|
|
|
|
|
atsr = container_of(hdr, struct acpi_dmar_atsr, header);
|
|
|
|
atsru = dmar_find_atsr(atsr);
|
|
|
|
if (atsru) {
|
|
|
|
list_del_rcu(&atsru->list);
|
|
|
|
synchronize_rcu();
|
|
|
|
intel_iommu_free_atsr(atsru);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct device *dev;
|
|
|
|
struct acpi_dmar_atsr *atsr;
|
|
|
|
struct dmar_atsr_unit *atsru;
|
|
|
|
|
|
|
|
atsr = container_of(hdr, struct acpi_dmar_atsr, header);
|
|
|
|
atsru = dmar_find_atsr(atsr);
|
|
|
|
if (!atsru)
|
|
|
|
return 0;
|
|
|
|
|
2016-07-28 11:03:31 +08:00
|
|
|
if (!atsru->include_all && atsru->devices && atsru->devices_cnt) {
|
2014-11-09 22:47:58 +08:00
|
|
|
for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
|
|
|
|
i, dev)
|
|
|
|
return -EBUSY;
|
2016-07-28 11:03:31 +08:00
|
|
|
}
|
2014-11-09 22:47:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:48:02 +08:00
|
|
|
static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
|
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
int sp, ret;
|
2014-11-09 22:48:02 +08:00
|
|
|
struct intel_iommu *iommu = dmaru->iommu;
|
|
|
|
|
|
|
|
if (g_iommus[iommu->seq_id])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_warn("%s: Doesn't support hardware pass through.\n",
|
2014-11-09 22:48:02 +08:00
|
|
|
iommu->name);
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
if (!ecap_sc_support(iommu->ecap) &&
|
|
|
|
domain_update_iommu_snooping(iommu)) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_warn("%s: Doesn't support snooping.\n",
|
2014-11-09 22:48:02 +08:00
|
|
|
iommu->name);
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
sp = domain_update_iommu_superpage(iommu) - 1;
|
|
|
|
if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_warn("%s: Doesn't support large page.\n",
|
2014-11-09 22:48:02 +08:00
|
|
|
iommu->name);
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable translation if already enabled prior to OS handover.
|
|
|
|
*/
|
|
|
|
if (iommu->gcmd & DMA_GCMD_TE)
|
|
|
|
iommu_disable_translation(iommu);
|
|
|
|
|
|
|
|
g_iommus[iommu->seq_id] = iommu;
|
|
|
|
ret = iommu_init_domains(iommu);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = iommu_alloc_root_entry(iommu);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2015-03-24 22:54:56 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
2018-12-10 09:58:55 +08:00
|
|
|
if (pasid_supported(iommu))
|
2018-07-14 15:47:02 +08:00
|
|
|
intel_svm_init(iommu);
|
2015-03-24 22:54:56 +08:00
|
|
|
#endif
|
|
|
|
|
2014-11-09 22:48:02 +08:00
|
|
|
if (dmaru->ignored) {
|
|
|
|
/*
|
|
|
|
* we always have to disable PMRs or DMA may fail on this device
|
|
|
|
*/
|
|
|
|
if (force_on)
|
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
intel_iommu_init_qi(iommu);
|
|
|
|
iommu_flush_write_buffer(iommu);
|
2015-10-08 06:35:18 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
2018-12-10 09:58:55 +08:00
|
|
|
if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) {
|
2015-10-08 06:35:18 +08:00
|
|
|
ret = intel_svm_enable_prq(iommu);
|
|
|
|
if (ret)
|
|
|
|
goto disable_iommu;
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-09 22:48:02 +08:00
|
|
|
ret = dmar_set_interrupt(iommu);
|
|
|
|
if (ret)
|
|
|
|
goto disable_iommu;
|
|
|
|
|
|
|
|
iommu_set_root_entry(iommu);
|
|
|
|
iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
|
|
|
|
iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
|
|
|
|
iommu_enable_translation(iommu);
|
|
|
|
|
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
disable_iommu:
|
|
|
|
disable_dmar_iommu(iommu);
|
|
|
|
out:
|
|
|
|
free_dmar_iommu(iommu);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:47:58 +08:00
|
|
|
int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
|
|
|
|
{
|
2014-11-09 22:48:02 +08:00
|
|
|
int ret = 0;
|
|
|
|
struct intel_iommu *iommu = dmaru->iommu;
|
|
|
|
|
|
|
|
if (!intel_iommu_enabled)
|
|
|
|
return 0;
|
|
|
|
if (iommu == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (insert) {
|
|
|
|
ret = intel_iommu_add(dmaru);
|
|
|
|
} else {
|
|
|
|
disable_dmar_iommu(iommu);
|
|
|
|
free_dmar_iommu(iommu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2014-11-09 22:47:58 +08:00
|
|
|
}
|
|
|
|
|
2014-01-06 14:18:27 +08:00
|
|
|
static void intel_iommu_free_dmars(void)
|
|
|
|
{
|
|
|
|
struct dmar_rmrr_unit *rmrru, *rmrr_n;
|
|
|
|
struct dmar_atsr_unit *atsru, *atsr_n;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
|
|
|
|
list_del(&rmrru->list);
|
|
|
|
dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
|
|
|
|
kfree(rmrru);
|
2011-08-24 08:05:20 +08:00
|
|
|
}
|
|
|
|
|
2014-01-06 14:18:27 +08:00
|
|
|
list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
|
|
|
|
list_del(&atsru->list);
|
|
|
|
intel_iommu_free_atsr(atsru);
|
|
|
|
}
|
2011-08-24 08:05:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dmar_find_matched_atsr_unit(struct pci_dev *dev)
|
|
|
|
{
|
2014-02-19 14:07:32 +08:00
|
|
|
int i, ret = 1;
|
2011-08-24 08:05:20 +08:00
|
|
|
struct pci_bus *bus;
|
2014-03-07 23:08:36 +08:00
|
|
|
struct pci_dev *bridge = NULL;
|
|
|
|
struct device *tmp;
|
2011-08-24 08:05:20 +08:00
|
|
|
struct acpi_dmar_atsr *atsr;
|
|
|
|
struct dmar_atsr_unit *atsru;
|
|
|
|
|
|
|
|
dev = pci_physfn(dev);
|
|
|
|
for (bus = dev->bus; bus; bus = bus->parent) {
|
iommu/vt-d: Fix error in detect ATS capability
Current Intel IOMMU driver only matches a PCIe root port with the first
DRHD unit with the samge segment number. It will report false result
if there are multiple DRHD units with the same segment number, thus fail
to detect ATS capability for some PCIe devices.
This patch refines function dmar_find_matched_atsr_unit() to search all
DRHD units with the same segment number.
An example DMAR table entries as below:
[1D0h 0464 2] Subtable Type : 0002 <Root Port ATS Capability>
[1D2h 0466 2] Length : 0028
[1D4h 0468 1] Flags : 00
[1D5h 0469 1] Reserved : 00
[1D6h 0470 2] PCI Segment Number : 0000
[1D8h 0472 1] Device Scope Entry Type : 02
[1D9h 0473 1] Entry Length : 08
[1DAh 0474 2] Reserved : 0000
[1DCh 0476 1] Enumeration ID : 00
[1DDh 0477 1] PCI Bus Number : 00
[1DEh 0478 2] PCI Path : [02, 00]
[1E0h 0480 1] Device Scope Entry Type : 02
[1E1h 0481 1] Entry Length : 08
[1E2h 0482 2] Reserved : 0000
[1E4h 0484 1] Enumeration ID : 00
[1E5h 0485 1] PCI Bus Number : 00
[1E6h 0486 2] PCI Path : [03, 00]
[1E8h 0488 1] Device Scope Entry Type : 02
[1E9h 0489 1] Entry Length : 08
[1EAh 0490 2] Reserved : 0000
[1ECh 0492 1] Enumeration ID : 00
[1EDh 0493 1] PCI Bus Number : 00
[1EEh 0494 2] PCI Path : [03, 02]
[1F0h 0496 1] Device Scope Entry Type : 02
[1F1h 0497 1] Entry Length : 08
[1F2h 0498 2] Reserved : 0000
[1F4h 0500 1] Enumeration ID : 00
[1F5h 0501 1] PCI Bus Number : 00
[1F6h 0502 2] PCI Path : [03, 03]
[1F8h 0504 2] Subtable Type : 0002 <Root Port ATS Capability>
[1FAh 0506 2] Length : 0020
[1FCh 0508 1] Flags : 00
[1FDh 0509 1] Reserved : 00
[1FEh 0510 2] PCI Segment Number : 0000
[200h 0512 1] Device Scope Entry Type : 02
[201h 0513 1] Entry Length : 08
[202h 0514 2] Reserved : 0000
[204h 0516 1] Enumeration ID : 00
[205h 0517 1] PCI Bus Number : 40
[206h 0518 2] PCI Path : [02, 00]
[208h 0520 1] Device Scope Entry Type : 02
[209h 0521 1] Entry Length : 08
[20Ah 0522 2] Reserved : 0000
[20Ch 0524 1] Enumeration ID : 00
[20Dh 0525 1] PCI Bus Number : 40
[20Eh 0526 2] PCI Path : [02, 02]
[210h 0528 1] Device Scope Entry Type : 02
[211h 0529 1] Entry Length : 08
[212h 0530 2] Reserved : 0000
[214h 0532 1] Enumeration ID : 00
[215h 0533 1] PCI Bus Number : 40
[216h 0534 2] PCI Path : [03, 00]
[218h 0536 2] Subtable Type : 0002 <Root Port ATS Capability>
[21Ah 0538 2] Length : 0020
[21Ch 0540 1] Flags : 00
[21Dh 0541 1] Reserved : 00
[21Eh 0542 2] PCI Segment Number : 0000
[220h 0544 1] Device Scope Entry Type : 02
[221h 0545 1] Entry Length : 08
[222h 0546 2] Reserved : 0000
[224h 0548 1] Enumeration ID : 00
[225h 0549 1] PCI Bus Number : 80
[226h 0550 2] PCI Path : [02, 00]
[228h 0552 1] Device Scope Entry Type : 02
[229h 0553 1] Entry Length : 08
[22Ah 0554 2] Reserved : 0000
[22Ch 0556 1] Enumeration ID : 00
[22Dh 0557 1] PCI Bus Number : 80
[22Eh 0558 2] PCI Path : [02, 02]
[230h 0560 1] Device Scope Entry Type : 02
[231h 0561 1] Entry Length : 08
[232h 0562 2] Reserved : 0000
[234h 0564 1] Enumeration ID : 00
[235h 0565 1] PCI Bus Number : 80
[236h 0566 2] PCI Path : [03, 00]
[238h 0568 2] Subtable Type : 0002 <Root Port ATS Capability>
[23Ah 0570 2] Length : 0020
[23Ch 0572 1] Flags : 00
[23Dh 0573 1] Reserved : 00
[23Eh 0574 2] PCI Segment Number : 0000
[240h 0576 1] Device Scope Entry Type : 02
[241h 0577 1] Entry Length : 08
[242h 0578 2] Reserved : 0000
[244h 0580 1] Enumeration ID : 00
[245h 0581 1] PCI Bus Number : C0
[246h 0582 2] PCI Path : [02, 00]
[248h 0584 1] Device Scope Entry Type : 02
[249h 0585 1] Entry Length : 08
[24Ah 0586 2] Reserved : 0000
[24Ch 0588 1] Enumeration ID : 00
[24Dh 0589 1] PCI Bus Number : C0
[24Eh 0590 2] PCI Path : [02, 02]
[250h 0592 1] Device Scope Entry Type : 02
[251h 0593 1] Entry Length : 08
[252h 0594 2] Reserved : 0000
[254h 0596 1] Enumeration ID : 00
[255h 0597 1] PCI Bus Number : C0
[256h 0598 2] PCI Path : [03, 00]
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:31 +08:00
|
|
|
bridge = bus->self;
|
2015-10-15 16:28:06 +08:00
|
|
|
/* If it's an integrated device, allow ATS */
|
|
|
|
if (!bridge)
|
|
|
|
return 1;
|
|
|
|
/* Connected via non-PCIe: no ATS */
|
|
|
|
if (!pci_is_pcie(bridge) ||
|
2012-07-24 17:20:03 +08:00
|
|
|
pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
|
2011-08-24 08:05:20 +08:00
|
|
|
return 0;
|
2015-10-15 16:28:06 +08:00
|
|
|
/* If we found the root port, look it up in the ATSR */
|
iommu/vt-d: Fix error in detect ATS capability
Current Intel IOMMU driver only matches a PCIe root port with the first
DRHD unit with the samge segment number. It will report false result
if there are multiple DRHD units with the same segment number, thus fail
to detect ATS capability for some PCIe devices.
This patch refines function dmar_find_matched_atsr_unit() to search all
DRHD units with the same segment number.
An example DMAR table entries as below:
[1D0h 0464 2] Subtable Type : 0002 <Root Port ATS Capability>
[1D2h 0466 2] Length : 0028
[1D4h 0468 1] Flags : 00
[1D5h 0469 1] Reserved : 00
[1D6h 0470 2] PCI Segment Number : 0000
[1D8h 0472 1] Device Scope Entry Type : 02
[1D9h 0473 1] Entry Length : 08
[1DAh 0474 2] Reserved : 0000
[1DCh 0476 1] Enumeration ID : 00
[1DDh 0477 1] PCI Bus Number : 00
[1DEh 0478 2] PCI Path : [02, 00]
[1E0h 0480 1] Device Scope Entry Type : 02
[1E1h 0481 1] Entry Length : 08
[1E2h 0482 2] Reserved : 0000
[1E4h 0484 1] Enumeration ID : 00
[1E5h 0485 1] PCI Bus Number : 00
[1E6h 0486 2] PCI Path : [03, 00]
[1E8h 0488 1] Device Scope Entry Type : 02
[1E9h 0489 1] Entry Length : 08
[1EAh 0490 2] Reserved : 0000
[1ECh 0492 1] Enumeration ID : 00
[1EDh 0493 1] PCI Bus Number : 00
[1EEh 0494 2] PCI Path : [03, 02]
[1F0h 0496 1] Device Scope Entry Type : 02
[1F1h 0497 1] Entry Length : 08
[1F2h 0498 2] Reserved : 0000
[1F4h 0500 1] Enumeration ID : 00
[1F5h 0501 1] PCI Bus Number : 00
[1F6h 0502 2] PCI Path : [03, 03]
[1F8h 0504 2] Subtable Type : 0002 <Root Port ATS Capability>
[1FAh 0506 2] Length : 0020
[1FCh 0508 1] Flags : 00
[1FDh 0509 1] Reserved : 00
[1FEh 0510 2] PCI Segment Number : 0000
[200h 0512 1] Device Scope Entry Type : 02
[201h 0513 1] Entry Length : 08
[202h 0514 2] Reserved : 0000
[204h 0516 1] Enumeration ID : 00
[205h 0517 1] PCI Bus Number : 40
[206h 0518 2] PCI Path : [02, 00]
[208h 0520 1] Device Scope Entry Type : 02
[209h 0521 1] Entry Length : 08
[20Ah 0522 2] Reserved : 0000
[20Ch 0524 1] Enumeration ID : 00
[20Dh 0525 1] PCI Bus Number : 40
[20Eh 0526 2] PCI Path : [02, 02]
[210h 0528 1] Device Scope Entry Type : 02
[211h 0529 1] Entry Length : 08
[212h 0530 2] Reserved : 0000
[214h 0532 1] Enumeration ID : 00
[215h 0533 1] PCI Bus Number : 40
[216h 0534 2] PCI Path : [03, 00]
[218h 0536 2] Subtable Type : 0002 <Root Port ATS Capability>
[21Ah 0538 2] Length : 0020
[21Ch 0540 1] Flags : 00
[21Dh 0541 1] Reserved : 00
[21Eh 0542 2] PCI Segment Number : 0000
[220h 0544 1] Device Scope Entry Type : 02
[221h 0545 1] Entry Length : 08
[222h 0546 2] Reserved : 0000
[224h 0548 1] Enumeration ID : 00
[225h 0549 1] PCI Bus Number : 80
[226h 0550 2] PCI Path : [02, 00]
[228h 0552 1] Device Scope Entry Type : 02
[229h 0553 1] Entry Length : 08
[22Ah 0554 2] Reserved : 0000
[22Ch 0556 1] Enumeration ID : 00
[22Dh 0557 1] PCI Bus Number : 80
[22Eh 0558 2] PCI Path : [02, 02]
[230h 0560 1] Device Scope Entry Type : 02
[231h 0561 1] Entry Length : 08
[232h 0562 2] Reserved : 0000
[234h 0564 1] Enumeration ID : 00
[235h 0565 1] PCI Bus Number : 80
[236h 0566 2] PCI Path : [03, 00]
[238h 0568 2] Subtable Type : 0002 <Root Port ATS Capability>
[23Ah 0570 2] Length : 0020
[23Ch 0572 1] Flags : 00
[23Dh 0573 1] Reserved : 00
[23Eh 0574 2] PCI Segment Number : 0000
[240h 0576 1] Device Scope Entry Type : 02
[241h 0577 1] Entry Length : 08
[242h 0578 2] Reserved : 0000
[244h 0580 1] Enumeration ID : 00
[245h 0581 1] PCI Bus Number : C0
[246h 0582 2] PCI Path : [02, 00]
[248h 0584 1] Device Scope Entry Type : 02
[249h 0585 1] Entry Length : 08
[24Ah 0586 2] Reserved : 0000
[24Ch 0588 1] Enumeration ID : 00
[24Dh 0589 1] PCI Bus Number : C0
[24Eh 0590 2] PCI Path : [02, 02]
[250h 0592 1] Device Scope Entry Type : 02
[251h 0593 1] Entry Length : 08
[252h 0594 2] Reserved : 0000
[254h 0596 1] Enumeration ID : 00
[255h 0597 1] PCI Bus Number : C0
[256h 0598 2] PCI Path : [03, 00]
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:31 +08:00
|
|
|
if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
|
2011-08-24 08:05:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_lock();
|
iommu/vt-d: Fix error in detect ATS capability
Current Intel IOMMU driver only matches a PCIe root port with the first
DRHD unit with the samge segment number. It will report false result
if there are multiple DRHD units with the same segment number, thus fail
to detect ATS capability for some PCIe devices.
This patch refines function dmar_find_matched_atsr_unit() to search all
DRHD units with the same segment number.
An example DMAR table entries as below:
[1D0h 0464 2] Subtable Type : 0002 <Root Port ATS Capability>
[1D2h 0466 2] Length : 0028
[1D4h 0468 1] Flags : 00
[1D5h 0469 1] Reserved : 00
[1D6h 0470 2] PCI Segment Number : 0000
[1D8h 0472 1] Device Scope Entry Type : 02
[1D9h 0473 1] Entry Length : 08
[1DAh 0474 2] Reserved : 0000
[1DCh 0476 1] Enumeration ID : 00
[1DDh 0477 1] PCI Bus Number : 00
[1DEh 0478 2] PCI Path : [02, 00]
[1E0h 0480 1] Device Scope Entry Type : 02
[1E1h 0481 1] Entry Length : 08
[1E2h 0482 2] Reserved : 0000
[1E4h 0484 1] Enumeration ID : 00
[1E5h 0485 1] PCI Bus Number : 00
[1E6h 0486 2] PCI Path : [03, 00]
[1E8h 0488 1] Device Scope Entry Type : 02
[1E9h 0489 1] Entry Length : 08
[1EAh 0490 2] Reserved : 0000
[1ECh 0492 1] Enumeration ID : 00
[1EDh 0493 1] PCI Bus Number : 00
[1EEh 0494 2] PCI Path : [03, 02]
[1F0h 0496 1] Device Scope Entry Type : 02
[1F1h 0497 1] Entry Length : 08
[1F2h 0498 2] Reserved : 0000
[1F4h 0500 1] Enumeration ID : 00
[1F5h 0501 1] PCI Bus Number : 00
[1F6h 0502 2] PCI Path : [03, 03]
[1F8h 0504 2] Subtable Type : 0002 <Root Port ATS Capability>
[1FAh 0506 2] Length : 0020
[1FCh 0508 1] Flags : 00
[1FDh 0509 1] Reserved : 00
[1FEh 0510 2] PCI Segment Number : 0000
[200h 0512 1] Device Scope Entry Type : 02
[201h 0513 1] Entry Length : 08
[202h 0514 2] Reserved : 0000
[204h 0516 1] Enumeration ID : 00
[205h 0517 1] PCI Bus Number : 40
[206h 0518 2] PCI Path : [02, 00]
[208h 0520 1] Device Scope Entry Type : 02
[209h 0521 1] Entry Length : 08
[20Ah 0522 2] Reserved : 0000
[20Ch 0524 1] Enumeration ID : 00
[20Dh 0525 1] PCI Bus Number : 40
[20Eh 0526 2] PCI Path : [02, 02]
[210h 0528 1] Device Scope Entry Type : 02
[211h 0529 1] Entry Length : 08
[212h 0530 2] Reserved : 0000
[214h 0532 1] Enumeration ID : 00
[215h 0533 1] PCI Bus Number : 40
[216h 0534 2] PCI Path : [03, 00]
[218h 0536 2] Subtable Type : 0002 <Root Port ATS Capability>
[21Ah 0538 2] Length : 0020
[21Ch 0540 1] Flags : 00
[21Dh 0541 1] Reserved : 00
[21Eh 0542 2] PCI Segment Number : 0000
[220h 0544 1] Device Scope Entry Type : 02
[221h 0545 1] Entry Length : 08
[222h 0546 2] Reserved : 0000
[224h 0548 1] Enumeration ID : 00
[225h 0549 1] PCI Bus Number : 80
[226h 0550 2] PCI Path : [02, 00]
[228h 0552 1] Device Scope Entry Type : 02
[229h 0553 1] Entry Length : 08
[22Ah 0554 2] Reserved : 0000
[22Ch 0556 1] Enumeration ID : 00
[22Dh 0557 1] PCI Bus Number : 80
[22Eh 0558 2] PCI Path : [02, 02]
[230h 0560 1] Device Scope Entry Type : 02
[231h 0561 1] Entry Length : 08
[232h 0562 2] Reserved : 0000
[234h 0564 1] Enumeration ID : 00
[235h 0565 1] PCI Bus Number : 80
[236h 0566 2] PCI Path : [03, 00]
[238h 0568 2] Subtable Type : 0002 <Root Port ATS Capability>
[23Ah 0570 2] Length : 0020
[23Ch 0572 1] Flags : 00
[23Dh 0573 1] Reserved : 00
[23Eh 0574 2] PCI Segment Number : 0000
[240h 0576 1] Device Scope Entry Type : 02
[241h 0577 1] Entry Length : 08
[242h 0578 2] Reserved : 0000
[244h 0580 1] Enumeration ID : 00
[245h 0581 1] PCI Bus Number : C0
[246h 0582 2] PCI Path : [02, 00]
[248h 0584 1] Device Scope Entry Type : 02
[249h 0585 1] Entry Length : 08
[24Ah 0586 2] Reserved : 0000
[24Ch 0588 1] Enumeration ID : 00
[24Dh 0589 1] PCI Bus Number : C0
[24Eh 0590 2] PCI Path : [02, 02]
[250h 0592 1] Device Scope Entry Type : 02
[251h 0593 1] Entry Length : 08
[252h 0594 2] Reserved : 0000
[254h 0596 1] Enumeration ID : 00
[255h 0597 1] PCI Bus Number : C0
[256h 0598 2] PCI Path : [03, 00]
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:31 +08:00
|
|
|
list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
|
|
|
|
atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
|
|
|
|
if (atsr->segment != pci_domain_nr(dev->bus))
|
|
|
|
continue;
|
|
|
|
|
2014-02-19 14:07:32 +08:00
|
|
|
for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
|
2014-03-07 23:08:36 +08:00
|
|
|
if (tmp == &bridge->dev)
|
2014-02-19 14:07:32 +08:00
|
|
|
goto out;
|
iommu/vt-d: Fix error in detect ATS capability
Current Intel IOMMU driver only matches a PCIe root port with the first
DRHD unit with the samge segment number. It will report false result
if there are multiple DRHD units with the same segment number, thus fail
to detect ATS capability for some PCIe devices.
This patch refines function dmar_find_matched_atsr_unit() to search all
DRHD units with the same segment number.
An example DMAR table entries as below:
[1D0h 0464 2] Subtable Type : 0002 <Root Port ATS Capability>
[1D2h 0466 2] Length : 0028
[1D4h 0468 1] Flags : 00
[1D5h 0469 1] Reserved : 00
[1D6h 0470 2] PCI Segment Number : 0000
[1D8h 0472 1] Device Scope Entry Type : 02
[1D9h 0473 1] Entry Length : 08
[1DAh 0474 2] Reserved : 0000
[1DCh 0476 1] Enumeration ID : 00
[1DDh 0477 1] PCI Bus Number : 00
[1DEh 0478 2] PCI Path : [02, 00]
[1E0h 0480 1] Device Scope Entry Type : 02
[1E1h 0481 1] Entry Length : 08
[1E2h 0482 2] Reserved : 0000
[1E4h 0484 1] Enumeration ID : 00
[1E5h 0485 1] PCI Bus Number : 00
[1E6h 0486 2] PCI Path : [03, 00]
[1E8h 0488 1] Device Scope Entry Type : 02
[1E9h 0489 1] Entry Length : 08
[1EAh 0490 2] Reserved : 0000
[1ECh 0492 1] Enumeration ID : 00
[1EDh 0493 1] PCI Bus Number : 00
[1EEh 0494 2] PCI Path : [03, 02]
[1F0h 0496 1] Device Scope Entry Type : 02
[1F1h 0497 1] Entry Length : 08
[1F2h 0498 2] Reserved : 0000
[1F4h 0500 1] Enumeration ID : 00
[1F5h 0501 1] PCI Bus Number : 00
[1F6h 0502 2] PCI Path : [03, 03]
[1F8h 0504 2] Subtable Type : 0002 <Root Port ATS Capability>
[1FAh 0506 2] Length : 0020
[1FCh 0508 1] Flags : 00
[1FDh 0509 1] Reserved : 00
[1FEh 0510 2] PCI Segment Number : 0000
[200h 0512 1] Device Scope Entry Type : 02
[201h 0513 1] Entry Length : 08
[202h 0514 2] Reserved : 0000
[204h 0516 1] Enumeration ID : 00
[205h 0517 1] PCI Bus Number : 40
[206h 0518 2] PCI Path : [02, 00]
[208h 0520 1] Device Scope Entry Type : 02
[209h 0521 1] Entry Length : 08
[20Ah 0522 2] Reserved : 0000
[20Ch 0524 1] Enumeration ID : 00
[20Dh 0525 1] PCI Bus Number : 40
[20Eh 0526 2] PCI Path : [02, 02]
[210h 0528 1] Device Scope Entry Type : 02
[211h 0529 1] Entry Length : 08
[212h 0530 2] Reserved : 0000
[214h 0532 1] Enumeration ID : 00
[215h 0533 1] PCI Bus Number : 40
[216h 0534 2] PCI Path : [03, 00]
[218h 0536 2] Subtable Type : 0002 <Root Port ATS Capability>
[21Ah 0538 2] Length : 0020
[21Ch 0540 1] Flags : 00
[21Dh 0541 1] Reserved : 00
[21Eh 0542 2] PCI Segment Number : 0000
[220h 0544 1] Device Scope Entry Type : 02
[221h 0545 1] Entry Length : 08
[222h 0546 2] Reserved : 0000
[224h 0548 1] Enumeration ID : 00
[225h 0549 1] PCI Bus Number : 80
[226h 0550 2] PCI Path : [02, 00]
[228h 0552 1] Device Scope Entry Type : 02
[229h 0553 1] Entry Length : 08
[22Ah 0554 2] Reserved : 0000
[22Ch 0556 1] Enumeration ID : 00
[22Dh 0557 1] PCI Bus Number : 80
[22Eh 0558 2] PCI Path : [02, 02]
[230h 0560 1] Device Scope Entry Type : 02
[231h 0561 1] Entry Length : 08
[232h 0562 2] Reserved : 0000
[234h 0564 1] Enumeration ID : 00
[235h 0565 1] PCI Bus Number : 80
[236h 0566 2] PCI Path : [03, 00]
[238h 0568 2] Subtable Type : 0002 <Root Port ATS Capability>
[23Ah 0570 2] Length : 0020
[23Ch 0572 1] Flags : 00
[23Dh 0573 1] Reserved : 00
[23Eh 0574 2] PCI Segment Number : 0000
[240h 0576 1] Device Scope Entry Type : 02
[241h 0577 1] Entry Length : 08
[242h 0578 2] Reserved : 0000
[244h 0580 1] Enumeration ID : 00
[245h 0581 1] PCI Bus Number : C0
[246h 0582 2] PCI Path : [02, 00]
[248h 0584 1] Device Scope Entry Type : 02
[249h 0585 1] Entry Length : 08
[24Ah 0586 2] Reserved : 0000
[24Ch 0588 1] Enumeration ID : 00
[24Dh 0589 1] PCI Bus Number : C0
[24Eh 0590 2] PCI Path : [02, 02]
[250h 0592 1] Device Scope Entry Type : 02
[251h 0593 1] Entry Length : 08
[252h 0594 2] Reserved : 0000
[254h 0596 1] Enumeration ID : 00
[255h 0597 1] PCI Bus Number : C0
[256h 0598 2] PCI Path : [03, 00]
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:31 +08:00
|
|
|
|
|
|
|
if (atsru->include_all)
|
2014-02-19 14:07:32 +08:00
|
|
|
goto out;
|
iommu/vt-d: Fix error in detect ATS capability
Current Intel IOMMU driver only matches a PCIe root port with the first
DRHD unit with the samge segment number. It will report false result
if there are multiple DRHD units with the same segment number, thus fail
to detect ATS capability for some PCIe devices.
This patch refines function dmar_find_matched_atsr_unit() to search all
DRHD units with the same segment number.
An example DMAR table entries as below:
[1D0h 0464 2] Subtable Type : 0002 <Root Port ATS Capability>
[1D2h 0466 2] Length : 0028
[1D4h 0468 1] Flags : 00
[1D5h 0469 1] Reserved : 00
[1D6h 0470 2] PCI Segment Number : 0000
[1D8h 0472 1] Device Scope Entry Type : 02
[1D9h 0473 1] Entry Length : 08
[1DAh 0474 2] Reserved : 0000
[1DCh 0476 1] Enumeration ID : 00
[1DDh 0477 1] PCI Bus Number : 00
[1DEh 0478 2] PCI Path : [02, 00]
[1E0h 0480 1] Device Scope Entry Type : 02
[1E1h 0481 1] Entry Length : 08
[1E2h 0482 2] Reserved : 0000
[1E4h 0484 1] Enumeration ID : 00
[1E5h 0485 1] PCI Bus Number : 00
[1E6h 0486 2] PCI Path : [03, 00]
[1E8h 0488 1] Device Scope Entry Type : 02
[1E9h 0489 1] Entry Length : 08
[1EAh 0490 2] Reserved : 0000
[1ECh 0492 1] Enumeration ID : 00
[1EDh 0493 1] PCI Bus Number : 00
[1EEh 0494 2] PCI Path : [03, 02]
[1F0h 0496 1] Device Scope Entry Type : 02
[1F1h 0497 1] Entry Length : 08
[1F2h 0498 2] Reserved : 0000
[1F4h 0500 1] Enumeration ID : 00
[1F5h 0501 1] PCI Bus Number : 00
[1F6h 0502 2] PCI Path : [03, 03]
[1F8h 0504 2] Subtable Type : 0002 <Root Port ATS Capability>
[1FAh 0506 2] Length : 0020
[1FCh 0508 1] Flags : 00
[1FDh 0509 1] Reserved : 00
[1FEh 0510 2] PCI Segment Number : 0000
[200h 0512 1] Device Scope Entry Type : 02
[201h 0513 1] Entry Length : 08
[202h 0514 2] Reserved : 0000
[204h 0516 1] Enumeration ID : 00
[205h 0517 1] PCI Bus Number : 40
[206h 0518 2] PCI Path : [02, 00]
[208h 0520 1] Device Scope Entry Type : 02
[209h 0521 1] Entry Length : 08
[20Ah 0522 2] Reserved : 0000
[20Ch 0524 1] Enumeration ID : 00
[20Dh 0525 1] PCI Bus Number : 40
[20Eh 0526 2] PCI Path : [02, 02]
[210h 0528 1] Device Scope Entry Type : 02
[211h 0529 1] Entry Length : 08
[212h 0530 2] Reserved : 0000
[214h 0532 1] Enumeration ID : 00
[215h 0533 1] PCI Bus Number : 40
[216h 0534 2] PCI Path : [03, 00]
[218h 0536 2] Subtable Type : 0002 <Root Port ATS Capability>
[21Ah 0538 2] Length : 0020
[21Ch 0540 1] Flags : 00
[21Dh 0541 1] Reserved : 00
[21Eh 0542 2] PCI Segment Number : 0000
[220h 0544 1] Device Scope Entry Type : 02
[221h 0545 1] Entry Length : 08
[222h 0546 2] Reserved : 0000
[224h 0548 1] Enumeration ID : 00
[225h 0549 1] PCI Bus Number : 80
[226h 0550 2] PCI Path : [02, 00]
[228h 0552 1] Device Scope Entry Type : 02
[229h 0553 1] Entry Length : 08
[22Ah 0554 2] Reserved : 0000
[22Ch 0556 1] Enumeration ID : 00
[22Dh 0557 1] PCI Bus Number : 80
[22Eh 0558 2] PCI Path : [02, 02]
[230h 0560 1] Device Scope Entry Type : 02
[231h 0561 1] Entry Length : 08
[232h 0562 2] Reserved : 0000
[234h 0564 1] Enumeration ID : 00
[235h 0565 1] PCI Bus Number : 80
[236h 0566 2] PCI Path : [03, 00]
[238h 0568 2] Subtable Type : 0002 <Root Port ATS Capability>
[23Ah 0570 2] Length : 0020
[23Ch 0572 1] Flags : 00
[23Dh 0573 1] Reserved : 00
[23Eh 0574 2] PCI Segment Number : 0000
[240h 0576 1] Device Scope Entry Type : 02
[241h 0577 1] Entry Length : 08
[242h 0578 2] Reserved : 0000
[244h 0580 1] Enumeration ID : 00
[245h 0581 1] PCI Bus Number : C0
[246h 0582 2] PCI Path : [02, 00]
[248h 0584 1] Device Scope Entry Type : 02
[249h 0585 1] Entry Length : 08
[24Ah 0586 2] Reserved : 0000
[24Ch 0588 1] Enumeration ID : 00
[24Dh 0589 1] PCI Bus Number : C0
[24Eh 0590 2] PCI Path : [02, 02]
[250h 0592 1] Device Scope Entry Type : 02
[251h 0593 1] Entry Length : 08
[252h 0594 2] Reserved : 0000
[254h 0596 1] Enumeration ID : 00
[255h 0597 1] PCI Bus Number : C0
[256h 0598 2] PCI Path : [03, 00]
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:31 +08:00
|
|
|
}
|
2014-02-19 14:07:32 +08:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2014-02-19 14:07:34 +08:00
|
|
|
rcu_read_unlock();
|
2011-08-24 08:05:20 +08:00
|
|
|
|
2014-02-19 14:07:32 +08:00
|
|
|
return ret;
|
2011-08-24 08:05:20 +08:00
|
|
|
}
|
|
|
|
|
2014-02-19 14:07:35 +08:00
|
|
|
int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
|
|
|
|
{
|
2019-02-09 06:06:08 +08:00
|
|
|
int ret;
|
2014-02-19 14:07:35 +08:00
|
|
|
struct dmar_rmrr_unit *rmrru;
|
|
|
|
struct dmar_atsr_unit *atsru;
|
|
|
|
struct acpi_dmar_atsr *atsr;
|
|
|
|
struct acpi_dmar_reserved_memory *rmrr;
|
|
|
|
|
2017-05-17 02:42:41 +08:00
|
|
|
if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
|
2014-02-19 14:07:35 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
|
|
|
|
rmrr = container_of(rmrru->hdr,
|
|
|
|
struct acpi_dmar_reserved_memory, header);
|
|
|
|
if (info->event == BUS_NOTIFY_ADD_DEVICE) {
|
|
|
|
ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
|
|
|
|
((void *)rmrr) + rmrr->header.length,
|
|
|
|
rmrr->segment, rmrru->devices,
|
|
|
|
rmrru->devices_cnt);
|
2019-02-09 06:06:08 +08:00
|
|
|
if (ret < 0)
|
2014-02-19 14:07:35 +08:00
|
|
|
return ret;
|
2016-03-01 06:49:47 +08:00
|
|
|
} else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
|
2014-06-20 15:08:06 +08:00
|
|
|
dmar_remove_dev_scope(info, rmrr->segment,
|
|
|
|
rmrru->devices, rmrru->devices_cnt);
|
2014-02-19 14:07:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(atsru, &dmar_atsr_units, list) {
|
|
|
|
if (atsru->include_all)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
|
|
|
|
if (info->event == BUS_NOTIFY_ADD_DEVICE) {
|
|
|
|
ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
|
|
|
|
(void *)atsr + atsr->header.length,
|
|
|
|
atsr->segment, atsru->devices,
|
|
|
|
atsru->devices_cnt);
|
|
|
|
if (ret > 0)
|
|
|
|
break;
|
2019-02-09 06:06:08 +08:00
|
|
|
else if (ret < 0)
|
2014-02-19 14:07:35 +08:00
|
|
|
return ret;
|
2016-03-01 06:49:47 +08:00
|
|
|
} else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
|
2014-02-19 14:07:35 +08:00
|
|
|
if (dmar_remove_dev_scope(info, atsr->segment,
|
|
|
|
atsru->devices, atsru->devices_cnt))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-19 14:07:37 +08:00
|
|
|
static int intel_iommu_memory_notifier(struct notifier_block *nb,
|
|
|
|
unsigned long val, void *v)
|
|
|
|
{
|
|
|
|
struct memory_notify *mhp = v;
|
|
|
|
unsigned long long start, end;
|
|
|
|
unsigned long start_vpfn, last_vpfn;
|
|
|
|
|
|
|
|
switch (val) {
|
|
|
|
case MEM_GOING_ONLINE:
|
|
|
|
start = mhp->start_pfn << PAGE_SHIFT;
|
|
|
|
end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
|
|
|
|
if (iommu_domain_identity_map(si_domain, start, end)) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_warn("Failed to build identity map for [%llx-%llx]\n",
|
2014-02-19 14:07:37 +08:00
|
|
|
start, end);
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEM_OFFLINE:
|
|
|
|
case MEM_CANCEL_ONLINE:
|
|
|
|
start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
|
|
|
|
last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
|
|
|
|
while (start_vpfn <= last_vpfn) {
|
|
|
|
struct iova *iova;
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
2014-03-06 01:09:32 +08:00
|
|
|
struct page *freelist;
|
2014-02-19 14:07:37 +08:00
|
|
|
|
|
|
|
iova = find_iova(&si_domain->iovad, start_vpfn);
|
|
|
|
if (iova == NULL) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_debug("Failed get IOVA for PFN %lx\n",
|
2014-02-19 14:07:37 +08:00
|
|
|
start_vpfn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iova = split_and_remove_iova(&si_domain->iovad, iova,
|
|
|
|
start_vpfn, last_vpfn);
|
|
|
|
if (iova == NULL) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
|
2014-02-19 14:07:37 +08:00
|
|
|
start_vpfn, last_vpfn);
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:09:32 +08:00
|
|
|
freelist = domain_unmap(si_domain, iova->pfn_lo,
|
|
|
|
iova->pfn_hi);
|
|
|
|
|
2014-02-19 14:07:37 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
for_each_active_iommu(iommu, drhd)
|
2015-07-21 21:20:32 +08:00
|
|
|
iommu_flush_iotlb_psi(iommu, si_domain,
|
2014-07-11 14:19:36 +08:00
|
|
|
iova->pfn_lo, iova_size(iova),
|
2014-03-06 01:09:32 +08:00
|
|
|
!freelist, 0);
|
2014-02-19 14:07:37 +08:00
|
|
|
rcu_read_unlock();
|
2014-03-06 01:09:32 +08:00
|
|
|
dma_free_pagelist(freelist);
|
2014-02-19 14:07:37 +08:00
|
|
|
|
|
|
|
start_vpfn = iova->pfn_hi + 1;
|
|
|
|
free_iova_mem(iova);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block intel_iommu_memory_nb = {
|
|
|
|
.notifier_call = intel_iommu_memory_notifier,
|
|
|
|
.priority = 0
|
|
|
|
};
|
|
|
|
|
2016-04-20 16:34:11 +08:00
|
|
|
static void free_all_cpu_cached_iovas(unsigned int cpu)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < g_num_of_iommus; i++) {
|
|
|
|
struct intel_iommu *iommu = g_iommus[i];
|
|
|
|
struct dmar_domain *domain;
|
2016-07-03 08:23:24 +08:00
|
|
|
int did;
|
2016-04-20 16:34:11 +08:00
|
|
|
|
|
|
|
if (!iommu)
|
|
|
|
continue;
|
|
|
|
|
2016-06-06 20:20:11 +08:00
|
|
|
for (did = 0; did < cap_ndoms(iommu->cap); did++) {
|
2016-07-03 08:23:24 +08:00
|
|
|
domain = get_iommu_domain(iommu, (u16)did);
|
2016-04-20 16:34:11 +08:00
|
|
|
|
|
|
|
if (!domain)
|
|
|
|
continue;
|
|
|
|
free_cpu_cached_iovas(cpu, &domain->iovad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 07:13:41 +08:00
|
|
|
static int intel_iommu_cpu_dead(unsigned int cpu)
|
2016-04-20 16:33:02 +08:00
|
|
|
{
|
2016-11-27 07:13:41 +08:00
|
|
|
free_all_cpu_cached_iovas(cpu);
|
|
|
|
return 0;
|
2016-04-20 16:33:02 +08:00
|
|
|
}
|
|
|
|
|
2017-03-28 23:04:52 +08:00
|
|
|
static void intel_disable_iommus(void)
|
|
|
|
{
|
|
|
|
struct intel_iommu *iommu = NULL;
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
|
|
|
|
for_each_iommu(iommu, drhd)
|
|
|
|
iommu_disable_translation(iommu);
|
|
|
|
}
|
|
|
|
|
2017-02-28 20:57:18 +08:00
|
|
|
static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev)
|
|
|
|
{
|
2017-08-14 23:19:26 +08:00
|
|
|
struct iommu_device *iommu_dev = dev_to_iommu_device(dev);
|
|
|
|
|
|
|
|
return container_of(iommu_dev, struct intel_iommu, iommu);
|
2017-02-28 20:57:18 +08:00
|
|
|
}
|
|
|
|
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
static ssize_t intel_iommu_show_version(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2017-02-28 20:57:18 +08:00
|
|
|
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
u32 ver = readl(iommu->reg + DMAR_VER_REG);
|
|
|
|
return sprintf(buf, "%d:%d\n",
|
|
|
|
DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
|
|
|
|
|
|
|
|
static ssize_t intel_iommu_show_address(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2017-02-28 20:57:18 +08:00
|
|
|
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
return sprintf(buf, "%llx\n", iommu->reg_phys);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
|
|
|
|
|
|
|
|
static ssize_t intel_iommu_show_cap(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2017-02-28 20:57:18 +08:00
|
|
|
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
return sprintf(buf, "%llx\n", iommu->cap);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
|
|
|
|
|
|
|
|
static ssize_t intel_iommu_show_ecap(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2017-02-28 20:57:18 +08:00
|
|
|
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
return sprintf(buf, "%llx\n", iommu->ecap);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
|
|
|
|
|
2015-07-15 05:24:53 +08:00
|
|
|
static ssize_t intel_iommu_show_ndoms(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2017-02-28 20:57:18 +08:00
|
|
|
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
|
2015-07-15 05:24:53 +08:00
|
|
|
return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
|
|
|
|
|
|
|
|
static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2017-02-28 20:57:18 +08:00
|
|
|
struct intel_iommu *iommu = dev_to_intel_iommu(dev);
|
2015-07-15 05:24:53 +08:00
|
|
|
return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
|
|
|
|
cap_ndoms(iommu->cap)));
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
|
|
|
|
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
static struct attribute *intel_iommu_attrs[] = {
|
|
|
|
&dev_attr_version.attr,
|
|
|
|
&dev_attr_address.attr,
|
|
|
|
&dev_attr_cap.attr,
|
|
|
|
&dev_attr_ecap.attr,
|
2015-07-15 05:24:53 +08:00
|
|
|
&dev_attr_domains_supported.attr,
|
|
|
|
&dev_attr_domains_used.attr,
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct attribute_group intel_iommu_group = {
|
|
|
|
.name = "intel-iommu",
|
|
|
|
.attrs = intel_iommu_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct attribute_group *intel_iommu_groups[] = {
|
|
|
|
&intel_iommu_group,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2019-09-06 14:14:50 +08:00
|
|
|
static inline bool has_untrusted_dev(void)
|
2018-10-23 15:45:01 +08:00
|
|
|
{
|
|
|
|
struct pci_dev *pdev = NULL;
|
|
|
|
|
2019-09-06 14:14:50 +08:00
|
|
|
for_each_pci_dev(pdev)
|
|
|
|
if (pdev->untrusted)
|
|
|
|
return true;
|
2018-10-23 15:45:01 +08:00
|
|
|
|
2019-09-06 14:14:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-23 15:45:01 +08:00
|
|
|
|
2019-09-06 14:14:50 +08:00
|
|
|
static int __init platform_optin_force_iommu(void)
|
|
|
|
{
|
|
|
|
if (!dmar_platform_optin() || no_platform_optin || !has_untrusted_dev())
|
2018-10-23 15:45:01 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (no_iommu || dmar_disabled)
|
|
|
|
pr_info("Intel-IOMMU force enabled due to platform opt in\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If Intel-IOMMU is disabled by default, we will apply identity
|
|
|
|
* map for all devices except those marked as being untrusted.
|
|
|
|
*/
|
|
|
|
if (dmar_disabled)
|
|
|
|
iommu_identity_mapping |= IDENTMAP_ALL;
|
|
|
|
|
|
|
|
dmar_disabled = 0;
|
|
|
|
no_iommu = 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:31 +08:00
|
|
|
static int __init probe_acpi_namespace_devices(void)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
2019-06-03 22:05:19 +08:00
|
|
|
/* To avoid a -Wunused-but-set-variable warning. */
|
|
|
|
struct intel_iommu *iommu __maybe_unused;
|
2019-05-25 13:41:31 +08:00
|
|
|
struct device *dev;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
for_each_active_dev_scope(drhd->devices,
|
|
|
|
drhd->devices_cnt, i, dev) {
|
|
|
|
struct acpi_device_physical_node *pn;
|
|
|
|
struct iommu_group *group;
|
|
|
|
struct acpi_device *adev;
|
|
|
|
|
|
|
|
if (dev->bus != &acpi_bus_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
adev = to_acpi_device(dev);
|
|
|
|
mutex_lock(&adev->physical_node_lock);
|
|
|
|
list_for_each_entry(pn,
|
|
|
|
&adev->physical_node_list, node) {
|
|
|
|
group = iommu_group_get(pn->dev);
|
|
|
|
if (group) {
|
|
|
|
iommu_group_put(group);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pn->dev->bus->iommu_ops = &intel_iommu_ops;
|
|
|
|
ret = iommu_probe_device(pn->dev);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&adev->physical_node_lock);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
int __init intel_iommu_init(void)
|
|
|
|
{
|
2014-01-06 14:18:27 +08:00
|
|
|
int ret = -ENODEV;
|
2013-04-23 16:35:03 +08:00
|
|
|
struct dmar_drhd_unit *drhd;
|
2014-01-06 14:18:18 +08:00
|
|
|
struct intel_iommu *iommu;
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2018-10-23 15:45:01 +08:00
|
|
|
/*
|
|
|
|
* Intel IOMMU is required for a TXT/tboot launch or platform
|
|
|
|
* opt in, so enforce that.
|
|
|
|
*/
|
|
|
|
force_on = tboot_force_iommu() || platform_optin_force_iommu();
|
2009-07-01 10:31:10 +08:00
|
|
|
|
iommu/vt-d: Introduce a rwsem to protect global data structures
Introduce a global rwsem dmar_global_lock, which will be used to
protect DMAR related global data structures from DMAR/PCI/memory
device hotplug operations in process context.
DMA and interrupt remapping related data structures are read most,
and only change when memory/PCI/DMAR hotplug event happens.
So a global rwsem solution is adopted for balance between simplicity
and performance.
For interrupt remapping driver, function intel_irq_remapping_supported(),
dmar_table_init(), intel_enable_irq_remapping(), disable_irq_remapping(),
reenable_irq_remapping() and enable_drhd_fault_handling() etc
are called during booting, suspending and resuming with interrupt
disabled, so no need to take the global lock.
For interrupt remapping entry allocation, the locking model is:
down_read(&dmar_global_lock);
/* Find corresponding iommu */
iommu = map_hpet_to_ir(id);
if (iommu)
/*
* Allocate remapping entry and mark entry busy,
* the IOMMU won't be hot-removed until the
* allocated entry has been released.
*/
index = alloc_irte(iommu, irq, 1);
up_read(&dmar_global_lock);
For DMA remmaping driver, we only uses the dmar_global_lock rwsem to
protect functions which are only called in process context. For any
function which may be called in interrupt context, we will use RCU
to protect them in following patches.
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:33 +08:00
|
|
|
if (iommu_init_mempool()) {
|
|
|
|
if (force_on)
|
|
|
|
panic("tboot: Failed to initialize iommu memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
down_write(&dmar_global_lock);
|
2009-07-01 10:31:10 +08:00
|
|
|
if (dmar_table_init()) {
|
|
|
|
if (force_on)
|
|
|
|
panic("tboot: Failed to initialize DMAR table\n");
|
2014-01-06 14:18:27 +08:00
|
|
|
goto out_free_dmar;
|
2009-07-01 10:31:10 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2011-08-24 08:05:19 +08:00
|
|
|
if (dmar_dev_scope_init() < 0) {
|
2009-07-01 10:31:10 +08:00
|
|
|
if (force_on)
|
|
|
|
panic("tboot: Failed to initialize DMAR device scope\n");
|
2014-01-06 14:18:27 +08:00
|
|
|
goto out_free_dmar;
|
2009-07-01 10:31:10 +08:00
|
|
|
}
|
2008-07-11 02:16:37 +08:00
|
|
|
|
2017-10-06 21:00:53 +08:00
|
|
|
up_write(&dmar_global_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The bus notifier takes the dmar_global_lock, so lockdep will
|
|
|
|
* complain later when we register it under the lock.
|
|
|
|
*/
|
|
|
|
dmar_register_bus_notifier();
|
|
|
|
|
|
|
|
down_write(&dmar_global_lock);
|
|
|
|
|
2017-03-28 23:04:52 +08:00
|
|
|
if (no_iommu || dmar_disabled) {
|
2017-04-27 00:18:35 +08:00
|
|
|
/*
|
|
|
|
* We exit the function here to ensure IOMMU's remapping and
|
|
|
|
* mempool aren't setup, which means that the IOMMU's PMRs
|
|
|
|
* won't be disabled via the call to init_dmars(). So disable
|
|
|
|
* it explicitly here. The PMRs were setup by tboot prior to
|
|
|
|
* calling SENTER, but the kernel is expected to reset/tear
|
|
|
|
* down the PMRs.
|
|
|
|
*/
|
|
|
|
if (intel_iommu_tboot_noforce) {
|
|
|
|
for_each_iommu(iommu, drhd)
|
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:04:52 +08:00
|
|
|
/*
|
|
|
|
* Make sure the IOMMUs are switched off, even when we
|
|
|
|
* boot into a kexec kernel and the previous kernel left
|
|
|
|
* them enabled
|
|
|
|
*/
|
|
|
|
intel_disable_iommus();
|
2014-01-06 14:18:27 +08:00
|
|
|
goto out_free_dmar;
|
2017-03-28 23:04:52 +08:00
|
|
|
}
|
2008-07-11 02:16:43 +08:00
|
|
|
|
2011-08-24 08:05:20 +08:00
|
|
|
if (list_empty(&dmar_rmrr_units))
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("No RMRR found\n");
|
2011-08-24 08:05:20 +08:00
|
|
|
|
|
|
|
if (list_empty(&dmar_atsr_units))
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_info("No ATSR found\n");
|
2011-08-24 08:05:20 +08:00
|
|
|
|
2011-03-22 02:04:24 +08:00
|
|
|
if (dmar_init_reserved_ranges()) {
|
|
|
|
if (force_on)
|
|
|
|
panic("tboot: Failed to reserve iommu ranges\n");
|
iommu/vt-d: Introduce a rwsem to protect global data structures
Introduce a global rwsem dmar_global_lock, which will be used to
protect DMAR related global data structures from DMAR/PCI/memory
device hotplug operations in process context.
DMA and interrupt remapping related data structures are read most,
and only change when memory/PCI/DMAR hotplug event happens.
So a global rwsem solution is adopted for balance between simplicity
and performance.
For interrupt remapping driver, function intel_irq_remapping_supported(),
dmar_table_init(), intel_enable_irq_remapping(), disable_irq_remapping(),
reenable_irq_remapping() and enable_drhd_fault_handling() etc
are called during booting, suspending and resuming with interrupt
disabled, so no need to take the global lock.
For interrupt remapping entry allocation, the locking model is:
down_read(&dmar_global_lock);
/* Find corresponding iommu */
iommu = map_hpet_to_ir(id);
if (iommu)
/*
* Allocate remapping entry and mark entry busy,
* the IOMMU won't be hot-removed until the
* allocated entry has been released.
*/
index = alloc_irte(iommu, irq, 1);
up_read(&dmar_global_lock);
For DMA remmaping driver, we only uses the dmar_global_lock rwsem to
protect functions which are only called in process context. For any
function which may be called in interrupt context, we will use RCU
to protect them in following patches.
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:33 +08:00
|
|
|
goto out_free_reserved_range;
|
2011-03-22 02:04:24 +08:00
|
|
|
}
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2019-05-02 09:34:25 +08:00
|
|
|
if (dmar_map_gfx)
|
|
|
|
intel_iommu_gfx_mapped = 1;
|
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
init_no_remapping_devices();
|
|
|
|
|
2011-05-03 15:08:37 +08:00
|
|
|
ret = init_dmars();
|
2007-10-22 07:41:49 +08:00
|
|
|
if (ret) {
|
2009-07-01 10:31:10 +08:00
|
|
|
if (force_on)
|
|
|
|
panic("tboot: Failed to initialize DMARs\n");
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("Initialization failed\n");
|
2014-01-06 14:18:27 +08:00
|
|
|
goto out_free_reserved_range;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
iommu/vt-d: Introduce a rwsem to protect global data structures
Introduce a global rwsem dmar_global_lock, which will be used to
protect DMAR related global data structures from DMAR/PCI/memory
device hotplug operations in process context.
DMA and interrupt remapping related data structures are read most,
and only change when memory/PCI/DMAR hotplug event happens.
So a global rwsem solution is adopted for balance between simplicity
and performance.
For interrupt remapping driver, function intel_irq_remapping_supported(),
dmar_table_init(), intel_enable_irq_remapping(), disable_irq_remapping(),
reenable_irq_remapping() and enable_drhd_fault_handling() etc
are called during booting, suspending and resuming with interrupt
disabled, so no need to take the global lock.
For interrupt remapping entry allocation, the locking model is:
down_read(&dmar_global_lock);
/* Find corresponding iommu */
iommu = map_hpet_to_ir(id);
if (iommu)
/*
* Allocate remapping entry and mark entry busy,
* the IOMMU won't be hot-removed until the
* allocated entry has been released.
*/
index = alloc_irte(iommu, irq, 1);
up_read(&dmar_global_lock);
For DMA remmaping driver, we only uses the dmar_global_lock rwsem to
protect functions which are only called in process context. For any
function which may be called in interrupt context, we will use RCU
to protect them in following patches.
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:33 +08:00
|
|
|
up_write(&dmar_global_lock);
|
2007-10-22 07:41:49 +08:00
|
|
|
|
2017-12-24 20:57:08 +08:00
|
|
|
#if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB)
|
2019-09-06 14:14:50 +08:00
|
|
|
/*
|
|
|
|
* If the system has no untrusted device or the user has decided
|
|
|
|
* to disable the bounce page mechanisms, we don't need swiotlb.
|
|
|
|
* Mark this and the pre-allocated bounce pages will be released
|
|
|
|
* later.
|
|
|
|
*/
|
|
|
|
if (!has_untrusted_dev() || intel_no_bounce)
|
|
|
|
swiotlb = 0;
|
2009-11-10 18:46:20 +08:00
|
|
|
#endif
|
2009-08-04 23:19:20 +08:00
|
|
|
dma_ops = &intel_dma_ops;
|
2009-04-25 08:30:20 +08:00
|
|
|
|
2011-03-24 05:16:14 +08:00
|
|
|
init_iommu_pm_ops();
|
2008-12-03 22:14:02 +08:00
|
|
|
|
2017-02-01 23:56:46 +08:00
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
iommu_device_sysfs_add(&iommu->iommu, NULL,
|
|
|
|
intel_iommu_groups,
|
|
|
|
"%s", iommu->name);
|
|
|
|
iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
|
|
|
|
iommu_device_register(&iommu->iommu);
|
|
|
|
}
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
|
2011-09-06 23:56:07 +08:00
|
|
|
bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
|
2014-02-19 14:07:37 +08:00
|
|
|
if (si_domain && !hw_pass_through)
|
|
|
|
register_memory_notifier(&intel_iommu_memory_nb);
|
2016-11-27 07:13:41 +08:00
|
|
|
cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL,
|
|
|
|
intel_iommu_cpu_dead);
|
2019-05-25 13:41:25 +08:00
|
|
|
|
2019-06-12 08:28:49 +08:00
|
|
|
down_read(&dmar_global_lock);
|
2019-05-25 13:41:31 +08:00
|
|
|
if (probe_acpi_namespace_devices())
|
|
|
|
pr_warn("ACPI name space devices didn't probe correctly\n");
|
2019-06-12 08:28:49 +08:00
|
|
|
up_read(&dmar_global_lock);
|
2019-05-25 13:41:31 +08:00
|
|
|
|
2019-05-25 13:41:25 +08:00
|
|
|
/* Finally, we enable the DMA remapping hardware. */
|
|
|
|
for_each_iommu(iommu, drhd) {
|
2019-06-12 08:28:47 +08:00
|
|
|
if (!drhd->ignored && !translation_pre_enabled(iommu))
|
2019-05-25 13:41:25 +08:00
|
|
|
iommu_enable_translation(iommu);
|
|
|
|
|
|
|
|
iommu_disable_protect_mem_regions(iommu);
|
|
|
|
}
|
|
|
|
pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
|
|
|
|
|
2011-11-24 02:42:14 +08:00
|
|
|
intel_iommu_enabled = 1;
|
2018-09-12 08:11:38 +08:00
|
|
|
intel_iommu_debugfs_init();
|
2011-11-24 02:42:14 +08:00
|
|
|
|
2007-10-22 07:41:49 +08:00
|
|
|
return 0;
|
2014-01-06 14:18:27 +08:00
|
|
|
|
|
|
|
out_free_reserved_range:
|
|
|
|
put_iova_domain(&reserved_iova_list);
|
|
|
|
out_free_dmar:
|
|
|
|
intel_iommu_free_dmars();
|
iommu/vt-d: Introduce a rwsem to protect global data structures
Introduce a global rwsem dmar_global_lock, which will be used to
protect DMAR related global data structures from DMAR/PCI/memory
device hotplug operations in process context.
DMA and interrupt remapping related data structures are read most,
and only change when memory/PCI/DMAR hotplug event happens.
So a global rwsem solution is adopted for balance between simplicity
and performance.
For interrupt remapping driver, function intel_irq_remapping_supported(),
dmar_table_init(), intel_enable_irq_remapping(), disable_irq_remapping(),
reenable_irq_remapping() and enable_drhd_fault_handling() etc
are called during booting, suspending and resuming with interrupt
disabled, so no need to take the global lock.
For interrupt remapping entry allocation, the locking model is:
down_read(&dmar_global_lock);
/* Find corresponding iommu */
iommu = map_hpet_to_ir(id);
if (iommu)
/*
* Allocate remapping entry and mark entry busy,
* the IOMMU won't be hot-removed until the
* allocated entry has been released.
*/
index = alloc_irte(iommu, irq, 1);
up_read(&dmar_global_lock);
For DMA remmaping driver, we only uses the dmar_global_lock rwsem to
protect functions which are only called in process context. For any
function which may be called in interrupt context, we will use RCU
to protect them in following patches.
Signed-off-by: Jiang Liu <jiang.liu@linux.intel.com>
Signed-off-by: Joerg Roedel <joro@8bytes.org>
2014-02-19 14:07:33 +08:00
|
|
|
up_write(&dmar_global_lock);
|
|
|
|
iommu_exit_mempool();
|
2014-01-06 14:18:27 +08:00
|
|
|
return ret;
|
2007-10-22 07:41:49 +08:00
|
|
|
}
|
2007-10-22 07:41:55 +08:00
|
|
|
|
2019-08-26 16:50:56 +08:00
|
|
|
static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
|
|
|
|
{
|
|
|
|
struct intel_iommu *iommu = opaque;
|
|
|
|
|
|
|
|
domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NB - intel-iommu lacks any sort of reference counting for the users of
|
|
|
|
* dependent devices. If multiple endpoints have intersecting dependent
|
|
|
|
* devices, unbinding the driver from any one of them will possibly leave
|
|
|
|
* the others unable to operate.
|
|
|
|
*/
|
|
|
|
static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
|
|
|
|
{
|
|
|
|
if (!iommu || !dev || !dev_is_pci(dev))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
|
|
|
|
}
|
|
|
|
|
2015-07-23 23:44:46 +08:00
|
|
|
static void __dmar_remove_one_dev_info(struct device_domain_info *info)
|
2008-12-08 22:51:37 +08:00
|
|
|
{
|
2019-05-25 13:41:29 +08:00
|
|
|
struct dmar_domain *domain;
|
2008-12-08 22:51:37 +08:00
|
|
|
struct intel_iommu *iommu;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
|
2015-07-23 23:44:46 +08:00
|
|
|
if (WARN_ON(!info))
|
2008-12-08 22:51:37 +08:00
|
|
|
return;
|
|
|
|
|
2015-07-23 23:44:46 +08:00
|
|
|
iommu = info->iommu;
|
2019-05-25 13:41:29 +08:00
|
|
|
domain = info->domain;
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2015-07-23 23:44:46 +08:00
|
|
|
if (info->dev) {
|
2018-12-10 09:59:01 +08:00
|
|
|
if (dev_is_pci(info->dev) && sm_supported(iommu))
|
|
|
|
intel_pasid_tear_down_entry(iommu, info->dev,
|
|
|
|
PASID_RID2PASID);
|
|
|
|
|
2015-07-23 23:44:46 +08:00
|
|
|
iommu_disable_dev_iotlb(info);
|
2019-08-26 16:50:56 +08:00
|
|
|
domain_context_clear(iommu, info->dev);
|
2018-07-14 15:47:00 +08:00
|
|
|
intel_pasid_free_table(info->dev);
|
2015-07-23 23:44:46 +08:00
|
|
|
}
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2015-07-22 00:19:08 +08:00
|
|
|
unlink_domain_info(info);
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2015-07-22 17:52:53 +08:00
|
|
|
spin_lock_irqsave(&iommu->lock, flags);
|
2019-05-25 13:41:29 +08:00
|
|
|
domain_detach_iommu(domain, iommu);
|
2015-07-22 17:52:53 +08:00
|
|
|
spin_unlock_irqrestore(&iommu->lock, flags);
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2019-05-25 13:41:29 +08:00
|
|
|
/* free the private domain */
|
|
|
|
if (domain->flags & DOMAIN_FLAG_LOSE_CHILDREN &&
|
2019-08-06 08:14:09 +08:00
|
|
|
!(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) &&
|
|
|
|
list_empty(&domain->devices))
|
2019-05-25 13:41:29 +08:00
|
|
|
domain_exit(info->domain);
|
|
|
|
|
2015-07-23 23:44:46 +08:00
|
|
|
free_devinfo_mem(info);
|
2008-12-08 22:51:37 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 06:06:15 +08:00
|
|
|
static void dmar_remove_one_dev_info(struct device *dev)
|
2015-07-22 22:50:40 +08:00
|
|
|
{
|
2015-07-23 23:44:46 +08:00
|
|
|
struct device_domain_info *info;
|
2015-07-22 22:50:40 +08:00
|
|
|
unsigned long flags;
|
intel-iommu: Fix AB-BA lockdep report
When unbinding a device so that I could pass it through to a KVM VM, I
got the lockdep report below. It looks like a legitimate lock
ordering problem:
- domain_context_mapping_one() takes iommu->lock and calls
iommu_support_dev_iotlb(), which takes device_domain_lock (inside
iommu->lock).
- domain_remove_one_dev_info() starts by taking device_domain_lock
then takes iommu->lock inside it (near the end of the function).
So this is the classic AB-BA deadlock. It looks like a safe fix is to
simply release device_domain_lock a bit earlier, since as far as I can
tell, it doesn't protect any of the stuff accessed at the end of
domain_remove_one_dev_info() anyway.
BTW, the use of device_domain_lock looks a bit unsafe to me... it's
at least not obvious to me why we aren't vulnerable to the race below:
iommu_support_dev_iotlb()
domain_remove_dev_info()
lock device_domain_lock
find info
unlock device_domain_lock
lock device_domain_lock
find same info
unlock device_domain_lock
free_devinfo_mem(info)
do stuff with info after it's free
However I don't understand the locking here well enough to know if
this is a real problem, let alone what the best fix is.
Anyway here's the full lockdep output that prompted all of this:
=======================================================
[ INFO: possible circular locking dependency detected ]
2.6.39.1+ #1
-------------------------------------------------------
bash/13954 is trying to acquire lock:
(&(&iommu->lock)->rlock){......}, at: [<ffffffff812f6421>] domain_remove_one_dev_info+0x121/0x230
but task is already holding lock:
(device_domain_lock){-.-...}, at: [<ffffffff812f6508>] domain_remove_one_dev_info+0x208/0x230
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (device_domain_lock){-.-...}:
[<ffffffff8109ca9d>] lock_acquire+0x9d/0x130
[<ffffffff81571475>] _raw_spin_lock_irqsave+0x55/0xa0
[<ffffffff812f8350>] domain_context_mapping_one+0x600/0x750
[<ffffffff812f84df>] domain_context_mapping+0x3f/0x120
[<ffffffff812f9175>] iommu_prepare_identity_map+0x1c5/0x1e0
[<ffffffff81ccf1ca>] intel_iommu_init+0x88e/0xb5e
[<ffffffff81cab204>] pci_iommu_init+0x16/0x41
[<ffffffff81002165>] do_one_initcall+0x45/0x190
[<ffffffff81ca3d3f>] kernel_init+0xe3/0x168
[<ffffffff8157ac24>] kernel_thread_helper+0x4/0x10
-> #0 (&(&iommu->lock)->rlock){......}:
[<ffffffff8109bf3e>] __lock_acquire+0x195e/0x1e10
[<ffffffff8109ca9d>] lock_acquire+0x9d/0x130
[<ffffffff81571475>] _raw_spin_lock_irqsave+0x55/0xa0
[<ffffffff812f6421>] domain_remove_one_dev_info+0x121/0x230
[<ffffffff812f8b42>] device_notifier+0x72/0x90
[<ffffffff8157555c>] notifier_call_chain+0x8c/0xc0
[<ffffffff81089768>] __blocking_notifier_call_chain+0x78/0xb0
[<ffffffff810897b6>] blocking_notifier_call_chain+0x16/0x20
[<ffffffff81373a5c>] __device_release_driver+0xbc/0xe0
[<ffffffff81373ccf>] device_release_driver+0x2f/0x50
[<ffffffff81372ee3>] driver_unbind+0xa3/0xc0
[<ffffffff813724ac>] drv_attr_store+0x2c/0x30
[<ffffffff811e4506>] sysfs_write_file+0xe6/0x170
[<ffffffff8117569e>] vfs_write+0xce/0x190
[<ffffffff811759e4>] sys_write+0x54/0xa0
[<ffffffff81579a82>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
6 locks held by bash/13954:
#0: (&buffer->mutex){+.+.+.}, at: [<ffffffff811e4464>] sysfs_write_file+0x44/0x170
#1: (s_active#3){++++.+}, at: [<ffffffff811e44ed>] sysfs_write_file+0xcd/0x170
#2: (&__lockdep_no_validate__){+.+.+.}, at: [<ffffffff81372edb>] driver_unbind+0x9b/0xc0
#3: (&__lockdep_no_validate__){+.+.+.}, at: [<ffffffff81373cc7>] device_release_driver+0x27/0x50
#4: (&(&priv->bus_notifier)->rwsem){.+.+.+}, at: [<ffffffff8108974f>] __blocking_notifier_call_chain+0x5f/0xb0
#5: (device_domain_lock){-.-...}, at: [<ffffffff812f6508>] domain_remove_one_dev_info+0x208/0x230
stack backtrace:
Pid: 13954, comm: bash Not tainted 2.6.39.1+ #1
Call Trace:
[<ffffffff810993a7>] print_circular_bug+0xf7/0x100
[<ffffffff8109bf3e>] __lock_acquire+0x195e/0x1e10
[<ffffffff810972bd>] ? trace_hardirqs_off+0xd/0x10
[<ffffffff8109d57d>] ? trace_hardirqs_on_caller+0x13d/0x180
[<ffffffff8109ca9d>] lock_acquire+0x9d/0x130
[<ffffffff812f6421>] ? domain_remove_one_dev_info+0x121/0x230
[<ffffffff81571475>] _raw_spin_lock_irqsave+0x55/0xa0
[<ffffffff812f6421>] ? domain_remove_one_dev_info+0x121/0x230
[<ffffffff810972bd>] ? trace_hardirqs_off+0xd/0x10
[<ffffffff812f6421>] domain_remove_one_dev_info+0x121/0x230
[<ffffffff812f8b42>] device_notifier+0x72/0x90
[<ffffffff8157555c>] notifier_call_chain+0x8c/0xc0
[<ffffffff81089768>] __blocking_notifier_call_chain+0x78/0xb0
[<ffffffff810897b6>] blocking_notifier_call_chain+0x16/0x20
[<ffffffff81373a5c>] __device_release_driver+0xbc/0xe0
[<ffffffff81373ccf>] device_release_driver+0x2f/0x50
[<ffffffff81372ee3>] driver_unbind+0xa3/0xc0
[<ffffffff813724ac>] drv_attr_store+0x2c/0x30
[<ffffffff811e4506>] sysfs_write_file+0xe6/0x170
[<ffffffff8117569e>] vfs_write+0xce/0x190
[<ffffffff811759e4>] sys_write+0x54/0xa0
[<ffffffff81579a82>] system_call_fastpath+0x16/0x1b
Signed-off-by: Roland Dreier <roland@purestorage.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-07-20 21:22:21 +08:00
|
|
|
|
2015-07-22 22:50:40 +08:00
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
2015-07-23 23:44:46 +08:00
|
|
|
info = dev->archdata.iommu;
|
2019-08-06 08:14:08 +08:00
|
|
|
if (info)
|
|
|
|
__dmar_remove_one_dev_info(info);
|
2015-07-22 22:50:40 +08:00
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
2008-12-08 22:51:37 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 22:21:05 +08:00
|
|
|
static int md_domain_init(struct dmar_domain *domain, int guest_width)
|
|
|
|
{
|
|
|
|
int adjust_width;
|
|
|
|
|
|
|
|
init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
|
|
|
|
domain_reserve_special_ranges(domain);
|
|
|
|
|
|
|
|
/* calculate AGAW */
|
|
|
|
domain->gaw = guest_width;
|
|
|
|
adjust_width = guestwidth_to_adjustwidth(guest_width);
|
|
|
|
domain->agaw = width_to_agaw(adjust_width);
|
|
|
|
|
|
|
|
domain->iommu_coherency = 0;
|
|
|
|
domain->iommu_snooping = 0;
|
|
|
|
domain->iommu_superpage = 0;
|
|
|
|
domain->max_addr = 0;
|
|
|
|
|
|
|
|
/* always allocate the top pgd */
|
|
|
|
domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
|
|
|
|
if (!domain->pgd)
|
|
|
|
return -ENOMEM;
|
|
|
|
domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 20:43:08 +08:00
|
|
|
static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
|
2008-09-09 23:37:29 +08:00
|
|
|
{
|
2008-12-03 21:52:32 +08:00
|
|
|
struct dmar_domain *dmar_domain;
|
2015-03-26 20:43:08 +08:00
|
|
|
struct iommu_domain *domain;
|
|
|
|
|
2019-05-25 13:41:27 +08:00
|
|
|
switch (type) {
|
2019-05-25 13:41:28 +08:00
|
|
|
case IOMMU_DOMAIN_DMA:
|
|
|
|
/* fallthrough */
|
2019-05-25 13:41:27 +08:00
|
|
|
case IOMMU_DOMAIN_UNMANAGED:
|
2019-05-25 13:41:28 +08:00
|
|
|
dmar_domain = alloc_domain(0);
|
2019-05-25 13:41:27 +08:00
|
|
|
if (!dmar_domain) {
|
|
|
|
pr_err("Can't allocate dmar_domain\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-22 22:21:05 +08:00
|
|
|
if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
|
2019-05-25 13:41:27 +08:00
|
|
|
pr_err("Domain initialization failed\n");
|
|
|
|
domain_exit(dmar_domain);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-25 13:41:28 +08:00
|
|
|
|
|
|
|
if (type == IOMMU_DOMAIN_DMA &&
|
|
|
|
init_iova_flush_queue(&dmar_domain->iovad,
|
|
|
|
iommu_flush_iova, iova_entry_free)) {
|
|
|
|
pr_warn("iova flush queue initialization failed\n");
|
|
|
|
intel_iommu_strict = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:27 +08:00
|
|
|
domain_update_iommu_cap(dmar_domain);
|
2008-09-09 23:37:29 +08:00
|
|
|
|
2019-05-25 13:41:27 +08:00
|
|
|
domain = &dmar_domain->domain;
|
|
|
|
domain->geometry.aperture_start = 0;
|
|
|
|
domain->geometry.aperture_end =
|
|
|
|
__DOMAIN_MAX_ADDR(dmar_domain->gaw);
|
|
|
|
domain->geometry.force_aperture = true;
|
|
|
|
|
|
|
|
return domain;
|
|
|
|
case IOMMU_DOMAIN_IDENTITY:
|
|
|
|
return &si_domain->domain;
|
|
|
|
default:
|
2015-03-26 20:43:08 +08:00
|
|
|
return NULL;
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
2012-01-27 02:40:54 +08:00
|
|
|
|
2019-05-25 13:41:27 +08:00
|
|
|
return NULL;
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 20:43:08 +08:00
|
|
|
static void intel_iommu_domain_free(struct iommu_domain *domain)
|
2008-09-09 23:37:29 +08:00
|
|
|
{
|
2019-05-25 13:41:27 +08:00
|
|
|
if (domain != &si_domain->domain)
|
|
|
|
domain_exit(to_dmar_domain(domain));
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:32 +08:00
|
|
|
/*
|
|
|
|
* Check whether a @domain could be attached to the @dev through the
|
|
|
|
* aux-domain attach/detach APIs.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
is_aux_domain(struct device *dev, struct iommu_domain *domain)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev->archdata.iommu;
|
|
|
|
|
|
|
|
return info && info->auxd_enabled &&
|
|
|
|
domain->type == IOMMU_DOMAIN_UNMANAGED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void auxiliary_link_device(struct dmar_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev->archdata.iommu;
|
|
|
|
|
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
if (WARN_ON(!info))
|
|
|
|
return;
|
|
|
|
|
|
|
|
domain->auxd_refcnt++;
|
|
|
|
list_add(&domain->auxd, &info->auxiliary_domains);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void auxiliary_unlink_device(struct dmar_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev->archdata.iommu;
|
|
|
|
|
|
|
|
assert_spin_locked(&device_domain_lock);
|
|
|
|
if (WARN_ON(!info))
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_del(&domain->auxd);
|
|
|
|
domain->auxd_refcnt--;
|
|
|
|
|
|
|
|
if (!domain->auxd_refcnt && domain->default_pasid > 0)
|
|
|
|
intel_pasid_free_id(domain->default_pasid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aux_domain_add_dev(struct dmar_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 bus, devfn;
|
|
|
|
unsigned long flags;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
|
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if (!iommu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (domain->default_pasid <= 0) {
|
|
|
|
int pasid;
|
|
|
|
|
|
|
|
pasid = intel_pasid_alloc_id(domain, PASID_MIN,
|
|
|
|
pci_max_pasids(to_pci_dev(dev)),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (pasid <= 0) {
|
|
|
|
pr_err("Can't allocate default pasid\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
domain->default_pasid = pasid;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
/*
|
|
|
|
* iommu->lock must be held to attach domain to iommu and setup the
|
|
|
|
* pasid entry for second level translation.
|
|
|
|
*/
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
ret = domain_attach_iommu(domain, iommu);
|
|
|
|
if (ret)
|
|
|
|
goto attach_failed;
|
|
|
|
|
|
|
|
/* Setup the PASID entry for mediated devices: */
|
|
|
|
ret = intel_pasid_setup_second_level(iommu, domain, dev,
|
|
|
|
domain->default_pasid);
|
|
|
|
if (ret)
|
|
|
|
goto table_failed;
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
auxiliary_link_device(domain, dev);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
table_failed:
|
|
|
|
domain_detach_iommu(domain, iommu);
|
|
|
|
attach_failed:
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
if (!domain->auxd_refcnt && domain->default_pasid > 0)
|
|
|
|
intel_pasid_free_id(domain->default_pasid);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aux_domain_remove_dev(struct dmar_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!is_aux_domain(dev, &domain->domain))
|
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
info = dev->archdata.iommu;
|
|
|
|
iommu = info->iommu;
|
|
|
|
|
|
|
|
auxiliary_unlink_device(domain, dev);
|
|
|
|
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
intel_pasid_tear_down_entry(iommu, dev, domain->default_pasid);
|
|
|
|
domain_detach_iommu(domain, iommu);
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:31 +08:00
|
|
|
static int prepare_domain_attach_device(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
2008-09-09 23:37:29 +08:00
|
|
|
{
|
2015-03-26 20:43:08 +08:00
|
|
|
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
|
2008-12-08 23:10:23 +08:00
|
|
|
struct intel_iommu *iommu;
|
|
|
|
int addr_width;
|
2014-03-10 05:00:57 +08:00
|
|
|
u8 bus, devfn;
|
2008-12-08 23:09:29 +08:00
|
|
|
|
2014-03-10 05:00:57 +08:00
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
2008-12-08 23:10:23 +08:00
|
|
|
if (!iommu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
/* check if this iommu agaw is sufficient for max mapped address */
|
|
|
|
addr_width = agaw_to_width(iommu->agaw);
|
2010-05-17 15:20:45 +08:00
|
|
|
if (addr_width > cap_mgaw(iommu->cap))
|
|
|
|
addr_width = cap_mgaw(iommu->cap);
|
|
|
|
|
|
|
|
if (dmar_domain->max_addr > (1LL << addr_width)) {
|
2019-02-09 06:06:00 +08:00
|
|
|
dev_err(dev, "%s: iommu width (%d) is not "
|
|
|
|
"sufficient for the mapped address (%llx)\n",
|
|
|
|
__func__, addr_width, dmar_domain->max_addr);
|
2008-12-08 23:10:23 +08:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
2010-05-17 15:20:45 +08:00
|
|
|
dmar_domain->gaw = addr_width;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Knock out extra levels of page tables if necessary
|
|
|
|
*/
|
|
|
|
while (iommu->agaw < dmar_domain->agaw) {
|
|
|
|
struct dma_pte *pte;
|
|
|
|
|
|
|
|
pte = dmar_domain->pgd;
|
|
|
|
if (dma_pte_present(pte)) {
|
2010-06-12 19:21:42 +08:00
|
|
|
dmar_domain->pgd = (struct dma_pte *)
|
|
|
|
phys_to_virt(dma_pte_addr(pte));
|
2010-11-02 15:05:51 +08:00
|
|
|
free_pgtable_page(pte);
|
2010-05-17 15:20:45 +08:00
|
|
|
}
|
|
|
|
dmar_domain->agaw--;
|
|
|
|
}
|
2008-12-08 23:10:23 +08:00
|
|
|
|
2019-03-25 09:30:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int intel_iommu_attach_device(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-06-12 08:28:48 +08:00
|
|
|
if (domain->type == IOMMU_DOMAIN_UNMANAGED &&
|
|
|
|
device_is_rmrr_locked(dev)) {
|
2019-03-25 09:30:31 +08:00
|
|
|
dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:32 +08:00
|
|
|
if (is_aux_domain(dev, domain))
|
|
|
|
return -EPERM;
|
|
|
|
|
2019-03-25 09:30:31 +08:00
|
|
|
/* normally dev is not mapped */
|
|
|
|
if (unlikely(domain_context_mapped(dev))) {
|
|
|
|
struct dmar_domain *old_domain;
|
|
|
|
|
|
|
|
old_domain = find_domain(dev);
|
2019-05-25 13:41:28 +08:00
|
|
|
if (old_domain)
|
2019-03-25 09:30:31 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = prepare_domain_attach_device(domain, dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return domain_add_dev_info(to_dmar_domain(domain), dev);
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:32 +08:00
|
|
|
static int intel_iommu_aux_attach_device(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!is_aux_domain(dev, domain))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
ret = prepare_domain_attach_device(domain, dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return aux_domain_add_dev(to_dmar_domain(domain), dev);
|
|
|
|
}
|
|
|
|
|
2008-12-03 21:58:24 +08:00
|
|
|
static void intel_iommu_detach_device(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
2008-09-09 23:37:29 +08:00
|
|
|
{
|
2019-02-09 06:06:15 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2008-12-08 23:09:29 +08:00
|
|
|
}
|
2008-12-08 22:51:37 +08:00
|
|
|
|
2019-03-25 09:30:32 +08:00
|
|
|
static void intel_iommu_aux_detach_device(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
aux_domain_remove_dev(to_dmar_domain(domain), dev);
|
|
|
|
}
|
|
|
|
|
2010-01-21 00:17:37 +08:00
|
|
|
static int intel_iommu_map(struct iommu_domain *domain,
|
|
|
|
unsigned long iova, phys_addr_t hpa,
|
2011-11-10 17:32:25 +08:00
|
|
|
size_t size, int iommu_prot)
|
2008-12-08 23:09:29 +08:00
|
|
|
{
|
2015-03-26 20:43:08 +08:00
|
|
|
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
|
2008-12-08 23:10:23 +08:00
|
|
|
u64 max_addr;
|
2008-12-03 22:04:09 +08:00
|
|
|
int prot = 0;
|
2008-12-08 23:09:29 +08:00
|
|
|
int ret;
|
2008-12-08 23:10:23 +08:00
|
|
|
|
2019-05-25 13:41:29 +08:00
|
|
|
if (dmar_domain->flags & DOMAIN_FLAG_LOSE_CHILDREN)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2008-12-03 22:04:09 +08:00
|
|
|
if (iommu_prot & IOMMU_READ)
|
|
|
|
prot |= DMA_PTE_READ;
|
|
|
|
if (iommu_prot & IOMMU_WRITE)
|
|
|
|
prot |= DMA_PTE_WRITE;
|
2009-03-18 15:33:07 +08:00
|
|
|
if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
|
|
|
|
prot |= DMA_PTE_SNP;
|
2008-12-03 22:04:09 +08:00
|
|
|
|
2009-06-28 07:51:17 +08:00
|
|
|
max_addr = iova + size;
|
2008-12-03 22:04:09 +08:00
|
|
|
if (dmar_domain->max_addr < max_addr) {
|
2008-12-08 23:10:23 +08:00
|
|
|
u64 end;
|
|
|
|
|
|
|
|
/* check if minimum agaw is sufficient for mapped address */
|
2010-05-17 15:19:52 +08:00
|
|
|
end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
|
2008-12-08 23:10:23 +08:00
|
|
|
if (end < max_addr) {
|
2015-06-12 15:57:06 +08:00
|
|
|
pr_err("%s: iommu width (%d) is not "
|
2008-12-08 23:10:23 +08:00
|
|
|
"sufficient for the mapped address (%llx)\n",
|
2010-05-17 15:19:52 +08:00
|
|
|
__func__, dmar_domain->gaw, max_addr);
|
2008-12-08 23:10:23 +08:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
2008-12-03 22:04:09 +08:00
|
|
|
dmar_domain->max_addr = max_addr;
|
2008-12-08 23:10:23 +08:00
|
|
|
}
|
2009-06-28 21:22:28 +08:00
|
|
|
/* Round up size to next multiple of PAGE_SIZE, if it and
|
|
|
|
the low bits of hpa would take us onto the next page */
|
2009-06-28 22:03:06 +08:00
|
|
|
size = aligned_nrpages(hpa, size);
|
2009-06-28 21:22:28 +08:00
|
|
|
ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
|
|
|
|
hpa >> VTD_PAGE_SHIFT, size, prot);
|
2008-12-08 23:09:29 +08:00
|
|
|
return ret;
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
|
|
|
|
2011-11-10 17:32:25 +08:00
|
|
|
static size_t intel_iommu_unmap(struct iommu_domain *domain,
|
2019-07-02 23:44:06 +08:00
|
|
|
unsigned long iova, size_t size,
|
|
|
|
struct iommu_iotlb_gather *gather)
|
2008-09-09 23:37:29 +08:00
|
|
|
{
|
2015-03-26 20:43:08 +08:00
|
|
|
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
|
2014-03-06 01:09:32 +08:00
|
|
|
struct page *freelist = NULL;
|
|
|
|
unsigned long start_pfn, last_pfn;
|
|
|
|
unsigned int npages;
|
2015-07-21 21:50:02 +08:00
|
|
|
int iommu_id, level = 0;
|
2014-03-20 00:07:49 +08:00
|
|
|
|
|
|
|
/* Cope with horrid API which requires us to unmap more than the
|
|
|
|
size argument if it happens to be a large-page mapping. */
|
2015-08-13 17:15:13 +08:00
|
|
|
BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
|
2019-05-25 13:41:29 +08:00
|
|
|
if (dmar_domain->flags & DOMAIN_FLAG_LOSE_CHILDREN)
|
|
|
|
return 0;
|
2014-03-20 00:07:49 +08:00
|
|
|
|
|
|
|
if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
|
|
|
|
size = VTD_PAGE_SIZE << level_to_offset_bits(level);
|
2009-07-08 18:52:52 +08:00
|
|
|
|
2014-03-06 01:09:32 +08:00
|
|
|
start_pfn = iova >> VTD_PAGE_SHIFT;
|
|
|
|
last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
|
|
|
|
|
|
|
|
freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
|
|
|
|
|
|
|
|
npages = last_pfn - start_pfn + 1;
|
|
|
|
|
2018-03-22 18:18:06 +08:00
|
|
|
for_each_domain_iommu(iommu_id, dmar_domain)
|
2015-07-21 21:50:02 +08:00
|
|
|
iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
|
|
|
|
start_pfn, npages, !freelist, 0);
|
2014-03-06 01:09:32 +08:00
|
|
|
|
|
|
|
dma_free_pagelist(freelist);
|
2008-12-08 23:10:23 +08:00
|
|
|
|
2009-06-28 07:51:17 +08:00
|
|
|
if (dmar_domain->max_addr == iova + size)
|
|
|
|
dmar_domain->max_addr = iova;
|
2010-01-21 00:17:37 +08:00
|
|
|
|
2014-03-20 00:07:49 +08:00
|
|
|
return size;
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
|
|
|
|
2008-12-03 22:06:57 +08:00
|
|
|
static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
|
2013-03-29 03:53:58 +08:00
|
|
|
dma_addr_t iova)
|
2008-09-09 23:37:29 +08:00
|
|
|
{
|
2015-03-26 20:43:08 +08:00
|
|
|
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
|
2008-09-09 23:37:29 +08:00
|
|
|
struct dma_pte *pte;
|
2014-03-20 00:07:49 +08:00
|
|
|
int level = 0;
|
2008-12-08 23:09:29 +08:00
|
|
|
u64 phys = 0;
|
2008-09-09 23:37:29 +08:00
|
|
|
|
2019-05-25 13:41:29 +08:00
|
|
|
if (dmar_domain->flags & DOMAIN_FLAG_LOSE_CHILDREN)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-20 00:07:49 +08:00
|
|
|
pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
|
2008-09-09 23:37:29 +08:00
|
|
|
if (pte)
|
2008-12-08 23:09:29 +08:00
|
|
|
phys = dma_pte_addr(pte);
|
2008-09-09 23:37:29 +08:00
|
|
|
|
2008-12-08 23:09:29 +08:00
|
|
|
return phys;
|
2008-09-09 23:37:29 +08:00
|
|
|
}
|
2008-12-03 22:14:02 +08:00
|
|
|
|
2019-03-25 09:30:30 +08:00
|
|
|
static inline bool scalable_mode_support(void)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
if (!sm_supported(iommu)) {
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool iommu_pasid_support(void)
|
|
|
|
{
|
|
|
|
struct dmar_drhd_unit *drhd;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
for_each_active_iommu(iommu, drhd) {
|
|
|
|
if (!pasid_supported(iommu)) {
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-05 16:50:45 +08:00
|
|
|
static bool intel_iommu_capable(enum iommu_cap cap)
|
2009-03-18 15:33:06 +08:00
|
|
|
{
|
|
|
|
if (cap == IOMMU_CAP_CACHE_COHERENCY)
|
2014-09-05 16:50:45 +08:00
|
|
|
return domain_update_iommu_snooping(NULL) == 1;
|
2010-07-03 04:56:14 +08:00
|
|
|
if (cap == IOMMU_CAP_INTR_REMAP)
|
2014-09-05 16:50:45 +08:00
|
|
|
return irq_remapping_enabled == 1;
|
2009-03-18 15:33:06 +08:00
|
|
|
|
2014-09-05 16:50:45 +08:00
|
|
|
return false;
|
2009-03-18 15:33:06 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 04:19:19 +08:00
|
|
|
static int intel_iommu_add_device(struct device *dev)
|
|
|
|
{
|
2019-05-25 13:41:29 +08:00
|
|
|
struct dmar_domain *dmar_domain;
|
|
|
|
struct iommu_domain *domain;
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
struct intel_iommu *iommu;
|
2012-05-31 04:19:19 +08:00
|
|
|
struct iommu_group *group;
|
2014-03-10 05:00:57 +08:00
|
|
|
u8 bus, devfn;
|
2019-05-25 13:41:29 +08:00
|
|
|
int ret;
|
2011-10-22 03:56:11 +08:00
|
|
|
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if (!iommu)
|
2011-10-22 03:56:11 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2017-02-02 00:23:22 +08:00
|
|
|
iommu_device_link(&iommu->iommu, dev);
|
2012-08-05 02:08:55 +08:00
|
|
|
|
2019-05-25 13:41:32 +08:00
|
|
|
if (translation_pre_enabled(iommu))
|
|
|
|
dev->archdata.iommu = DEFER_DEVICE_DOMAIN_INFO;
|
|
|
|
|
2014-07-03 23:51:37 +08:00
|
|
|
group = iommu_group_get_for_dev(dev);
|
2012-05-31 04:19:43 +08:00
|
|
|
|
2014-07-03 23:51:37 +08:00
|
|
|
if (IS_ERR(group))
|
|
|
|
return PTR_ERR(group);
|
2011-10-22 03:56:24 +08:00
|
|
|
|
2012-05-31 04:19:19 +08:00
|
|
|
iommu_group_put(group);
|
2019-05-25 13:41:29 +08:00
|
|
|
|
|
|
|
domain = iommu_get_domain_for_dev(dev);
|
|
|
|
dmar_domain = to_dmar_domain(domain);
|
|
|
|
if (domain->type == IOMMU_DOMAIN_DMA) {
|
2019-05-25 13:41:34 +08:00
|
|
|
if (device_def_domain_type(dev) == IOMMU_DOMAIN_IDENTITY) {
|
2019-05-25 13:41:29 +08:00
|
|
|
ret = iommu_request_dm_for_dev(dev);
|
|
|
|
if (ret) {
|
2019-08-06 08:14:08 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2019-05-25 13:41:29 +08:00
|
|
|
dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
|
|
|
|
domain_add_dev_info(si_domain, dev);
|
|
|
|
dev_info(dev,
|
|
|
|
"Device uses a private identity domain.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-25 13:41:34 +08:00
|
|
|
if (device_def_domain_type(dev) == IOMMU_DOMAIN_DMA) {
|
2019-05-25 13:41:29 +08:00
|
|
|
ret = iommu_request_dma_domain_for_dev(dev);
|
|
|
|
if (ret) {
|
2019-08-06 08:14:08 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
2019-05-25 13:41:29 +08:00
|
|
|
dmar_domain->flags |= DOMAIN_FLAG_LOSE_CHILDREN;
|
2019-05-25 13:41:33 +08:00
|
|
|
if (!get_private_domain_for_dev(dev)) {
|
2019-05-25 13:41:29 +08:00
|
|
|
dev_warn(dev,
|
|
|
|
"Failed to get a private domain.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_info(dev,
|
|
|
|
"Device uses a private dma domain.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:14:52 +08:00
|
|
|
if (device_needs_bounce(dev)) {
|
|
|
|
dev_info(dev, "Use Intel IOMMU bounce page dma_ops\n");
|
|
|
|
set_dma_ops(dev, &bounce_dma_ops);
|
|
|
|
}
|
|
|
|
|
2014-07-03 23:51:37 +08:00
|
|
|
return 0;
|
2012-05-31 04:19:19 +08:00
|
|
|
}
|
2011-10-22 03:56:11 +08:00
|
|
|
|
2012-05-31 04:19:19 +08:00
|
|
|
static void intel_iommu_remove_device(struct device *dev)
|
|
|
|
{
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
struct intel_iommu *iommu;
|
|
|
|
u8 bus, devfn;
|
|
|
|
|
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if (!iommu)
|
|
|
|
return;
|
|
|
|
|
2019-08-01 11:14:58 +08:00
|
|
|
dmar_remove_one_dev_info(dev);
|
|
|
|
|
2012-05-31 04:19:19 +08:00
|
|
|
iommu_group_remove_device(dev);
|
iommu/vt-d: Make use of IOMMU sysfs support
Register our DRHD IOMMUs, cross link devices, and provide a base set
of attributes for the IOMMU. Note that IRQ remapping support parses
the DMAR table very early in boot, well before the iommu_class can
reasonably be setup, so our registration is split between
intel_iommu_init(), which occurs later, and alloc_iommu(), which
typically occurs much earlier, but may happen at any time later
with IOMMU hot-add support.
On a typical desktop system, this provides the following (pruned):
$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
/sys/devices/virtual/iommu/dmar0/devices
/sys/devices/virtual/iommu/dmar0/devices/0000:00:02.0
/sys/devices/virtual/iommu/dmar0/intel-iommu
/sys/devices/virtual/iommu/dmar0/intel-iommu/cap
/sys/devices/virtual/iommu/dmar0/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar0/intel-iommu/address
/sys/devices/virtual/iommu/dmar0/intel-iommu/version
/sys/devices/virtual/iommu/dmar1
/sys/devices/virtual/iommu/dmar1/devices
/sys/devices/virtual/iommu/dmar1/devices/0000:00:00.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:01.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:16.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1a.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1b.0
/sys/devices/virtual/iommu/dmar1/devices/0000:00:1c.0
...
/sys/devices/virtual/iommu/dmar1/intel-iommu
/sys/devices/virtual/iommu/dmar1/intel-iommu/cap
/sys/devices/virtual/iommu/dmar1/intel-iommu/ecap
/sys/devices/virtual/iommu/dmar1/intel-iommu/address
/sys/devices/virtual/iommu/dmar1/intel-iommu/version
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1
(devices also link back to the dmar units)
This makes address, version, capabilities, and extended capabilities
available, just like printed on boot. I've tried not to duplicate
data that can be found in the DMAR table, with the exception of the
address, which provides an easy way to associate the sysfs device with
a DRHD entry in the DMAR. It's tempting to add scopes and RMRR data
here, but the full DMAR table is already exposed under /sys/firmware/
and therefore already provides a way for userspace to learn such
details.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2014-06-13 06:12:31 +08:00
|
|
|
|
2017-02-02 00:23:22 +08:00
|
|
|
iommu_device_unlink(&iommu->iommu, dev);
|
2019-09-06 14:14:52 +08:00
|
|
|
|
|
|
|
if (device_needs_bounce(dev))
|
|
|
|
set_dma_ops(dev, NULL);
|
2011-10-22 03:56:11 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 04:57:53 +08:00
|
|
|
static void intel_iommu_get_resv_regions(struct device *device,
|
|
|
|
struct list_head *head)
|
|
|
|
{
|
2019-06-03 14:53:31 +08:00
|
|
|
int prot = DMA_PTE_READ | DMA_PTE_WRITE;
|
2017-01-20 04:57:53 +08:00
|
|
|
struct iommu_resv_region *reg;
|
|
|
|
struct dmar_rmrr_unit *rmrr;
|
|
|
|
struct device *i_dev;
|
|
|
|
int i;
|
|
|
|
|
2019-06-03 14:53:31 +08:00
|
|
|
down_read(&dmar_global_lock);
|
2017-01-20 04:57:53 +08:00
|
|
|
for_each_rmrr_units(rmrr) {
|
|
|
|
for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
|
|
|
|
i, i_dev) {
|
2019-06-03 14:53:31 +08:00
|
|
|
struct iommu_resv_region *resv;
|
2019-06-03 14:53:36 +08:00
|
|
|
enum iommu_resv_type type;
|
2019-06-03 14:53:31 +08:00
|
|
|
size_t length;
|
|
|
|
|
2019-06-03 14:53:34 +08:00
|
|
|
if (i_dev != device &&
|
|
|
|
!is_downstream_to_pci_bridge(device, i_dev))
|
2017-01-20 04:57:53 +08:00
|
|
|
continue;
|
|
|
|
|
2019-06-03 14:53:31 +08:00
|
|
|
length = rmrr->end_address - rmrr->base_address + 1;
|
2019-06-03 14:53:36 +08:00
|
|
|
|
|
|
|
type = device_rmrr_is_relaxable(device) ?
|
|
|
|
IOMMU_RESV_DIRECT_RELAXABLE : IOMMU_RESV_DIRECT;
|
|
|
|
|
2019-06-03 14:53:31 +08:00
|
|
|
resv = iommu_alloc_resv_region(rmrr->base_address,
|
2019-06-03 14:53:36 +08:00
|
|
|
length, prot, type);
|
2019-06-03 14:53:31 +08:00
|
|
|
if (!resv)
|
|
|
|
break;
|
|
|
|
|
|
|
|
list_add_tail(&resv->list, head);
|
2017-01-20 04:57:53 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-03 14:53:31 +08:00
|
|
|
up_read(&dmar_global_lock);
|
2017-01-20 04:57:53 +08:00
|
|
|
|
2019-05-25 13:41:24 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
|
|
|
|
if (dev_is_pci(device)) {
|
|
|
|
struct pci_dev *pdev = to_pci_dev(device);
|
|
|
|
|
|
|
|
if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA) {
|
|
|
|
reg = iommu_alloc_resv_region(0, 1UL << 24, 0,
|
|
|
|
IOMMU_RESV_DIRECT);
|
|
|
|
if (reg)
|
|
|
|
list_add_tail(®->list, head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_INTEL_IOMMU_FLOPPY_WA */
|
|
|
|
|
2017-01-20 04:57:53 +08:00
|
|
|
reg = iommu_alloc_resv_region(IOAPIC_RANGE_START,
|
|
|
|
IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1,
|
iommu: Disambiguate MSI region types
The introduction of reserved regions has left a couple of rough edges
which we could do with sorting out sooner rather than later. Since we
are not yet addressing the potential dynamic aspect of software-managed
reservations and presenting them at arbitrary fixed addresses, it is
incongruous that we end up displaying hardware vs. software-managed MSI
regions to userspace differently, especially since ARM-based systems may
actually require one or the other, or even potentially both at once,
(which iommu-dma currently has no hope of dealing with at all). Let's
resolve the former user-visible inconsistency ASAP before the ABI has
been baked into a kernel release, in a way that also lays the groundwork
for the latter shortcoming to be addressed by follow-up patches.
For clarity, rename the software-managed type to IOMMU_RESV_SW_MSI, use
IOMMU_RESV_MSI to describe the hardware type, and document everything a
little bit. Since the x86 MSI remapping hardware falls squarely under
this meaning of IOMMU_RESV_MSI, apply that type to their regions as well,
so that we tell the same story to userspace across all platforms.
Secondly, as the various region types require quite different handling,
and it really makes little sense to ever try combining them, convert the
bitfield-esque #defines to a plain enum in the process before anyone
gets the wrong impression.
Fixes: d30ddcaa7b02 ("iommu: Add a new type field in iommu_resv_region")
Reviewed-by: Eric Auger <eric.auger@redhat.com>
CC: Alex Williamson <alex.williamson@redhat.com>
CC: David Woodhouse <dwmw2@infradead.org>
CC: kvm@vger.kernel.org
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2017-03-17 01:00:16 +08:00
|
|
|
0, IOMMU_RESV_MSI);
|
2017-01-20 04:57:53 +08:00
|
|
|
if (!reg)
|
|
|
|
return;
|
|
|
|
list_add_tail(®->list, head);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void intel_iommu_put_resv_regions(struct device *dev,
|
|
|
|
struct list_head *head)
|
|
|
|
{
|
|
|
|
struct iommu_resv_region *entry, *next;
|
|
|
|
|
2019-06-03 14:53:31 +08:00
|
|
|
list_for_each_entry_safe(entry, next, head, list)
|
|
|
|
kfree(entry);
|
2011-10-22 03:56:11 +08:00
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:29 +08:00
|
|
|
int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev)
|
2015-09-09 18:40:47 +08:00
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
struct context_entry *context;
|
|
|
|
struct dmar_domain *domain;
|
|
|
|
unsigned long flags;
|
|
|
|
u64 ctx_lo;
|
|
|
|
int ret;
|
|
|
|
|
2019-05-25 13:41:33 +08:00
|
|
|
domain = find_domain(dev);
|
2015-09-09 18:40:47 +08:00
|
|
|
if (!domain)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
spin_lock(&iommu->lock);
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
2019-03-25 09:30:29 +08:00
|
|
|
info = dev->archdata.iommu;
|
2015-09-09 18:40:47 +08:00
|
|
|
if (!info || !info->pasid_supported)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
|
|
|
|
if (WARN_ON(!context))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ctx_lo = context[0].lo;
|
|
|
|
|
|
|
|
if (!(ctx_lo & CONTEXT_PASIDE)) {
|
|
|
|
ctx_lo |= CONTEXT_PASIDE;
|
|
|
|
context[0].lo = ctx_lo;
|
|
|
|
wmb();
|
2019-03-25 09:30:29 +08:00
|
|
|
iommu->flush.flush_context(iommu,
|
|
|
|
domain->iommu_did[iommu->seq_id],
|
|
|
|
PCI_DEVID(info->bus, info->devfn),
|
2015-09-09 18:40:47 +08:00
|
|
|
DMA_CCMD_MASK_NOBIT,
|
|
|
|
DMA_CCMD_DEVICE_INVL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable PASID support in the device, if it wasn't already */
|
|
|
|
if (!info->pasid_enabled)
|
|
|
|
iommu_enable_dev_iotlb(info);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock(&iommu->lock);
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:23 +08:00
|
|
|
static void intel_iommu_apply_resv_region(struct device *dev,
|
|
|
|
struct iommu_domain *domain,
|
|
|
|
struct iommu_resv_region *region)
|
|
|
|
{
|
|
|
|
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
|
|
|
|
unsigned long start, end;
|
|
|
|
|
|
|
|
start = IOVA_PFN(region->start);
|
|
|
|
end = IOVA_PFN(region->start + region->length - 1);
|
|
|
|
|
|
|
|
WARN_ON_ONCE(!reserve_iova(&dmar_domain->iovad, start, end));
|
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:29 +08:00
|
|
|
#ifdef CONFIG_INTEL_IOMMU_SVM
|
2015-09-09 18:40:47 +08:00
|
|
|
struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
|
|
|
|
{
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
u8 bus, devfn;
|
|
|
|
|
|
|
|
if (iommu_dummy(dev)) {
|
|
|
|
dev_warn(dev,
|
|
|
|
"No IOMMU translation for device; cannot enable SVM\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if ((!iommu)) {
|
2015-10-19 11:54:37 +08:00
|
|
|
dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
|
2015-09-09 18:40:47 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iommu;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_INTEL_IOMMU_SVM */
|
|
|
|
|
2019-03-25 09:30:30 +08:00
|
|
|
static int intel_iommu_enable_auxd(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
struct intel_iommu *iommu;
|
|
|
|
unsigned long flags;
|
|
|
|
u8 bus, devfn;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
iommu = device_to_iommu(dev, &bus, &devfn);
|
|
|
|
if (!iommu || dmar_disabled)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!sm_supported(iommu) || !pasid_supported(iommu))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = intel_iommu_enable_pasid(iommu, dev);
|
|
|
|
if (ret)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
info = dev->archdata.iommu;
|
|
|
|
info->auxd_enabled = 1;
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int intel_iommu_disable_auxd(struct device *dev)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&device_domain_lock, flags);
|
|
|
|
info = dev->archdata.iommu;
|
|
|
|
if (!WARN_ON(!info))
|
|
|
|
info->auxd_enabled = 0;
|
|
|
|
spin_unlock_irqrestore(&device_domain_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A PCI express designated vendor specific extended capability is defined
|
|
|
|
* in the section 3.7 of Intel scalable I/O virtualization technical spec
|
|
|
|
* for system software and tools to detect endpoint devices supporting the
|
|
|
|
* Intel scalable IO virtualization without host driver dependency.
|
|
|
|
*
|
|
|
|
* Returns the address of the matching extended capability structure within
|
|
|
|
* the device's PCI configuration space or 0 if the device does not support
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
static int siov_find_pci_dvsec(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
u16 vendor, id;
|
|
|
|
|
|
|
|
pos = pci_find_next_ext_capability(pdev, 0, 0x23);
|
|
|
|
while (pos) {
|
|
|
|
pci_read_config_word(pdev, pos + 4, &vendor);
|
|
|
|
pci_read_config_word(pdev, pos + 8, &id);
|
|
|
|
if (vendor == PCI_VENDOR_ID_INTEL && id == 5)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
pos = pci_find_next_ext_capability(pdev, pos, 0x23);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
intel_iommu_dev_has_feat(struct device *dev, enum iommu_dev_features feat)
|
|
|
|
{
|
|
|
|
if (feat == IOMMU_DEV_FEAT_AUX) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dev_is_pci(dev) || dmar_disabled ||
|
|
|
|
!scalable_mode_support() || !iommu_pasid_support())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ret = pci_pasid_features(to_pci_dev(dev));
|
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !!siov_find_pci_dvsec(to_pci_dev(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
intel_iommu_dev_enable_feat(struct device *dev, enum iommu_dev_features feat)
|
|
|
|
{
|
|
|
|
if (feat == IOMMU_DEV_FEAT_AUX)
|
|
|
|
return intel_iommu_enable_auxd(dev);
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
intel_iommu_dev_disable_feat(struct device *dev, enum iommu_dev_features feat)
|
|
|
|
{
|
|
|
|
if (feat == IOMMU_DEV_FEAT_AUX)
|
|
|
|
return intel_iommu_disable_auxd(dev);
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
intel_iommu_dev_feat_enabled(struct device *dev, enum iommu_dev_features feat)
|
|
|
|
{
|
|
|
|
struct device_domain_info *info = dev->archdata.iommu;
|
|
|
|
|
|
|
|
if (feat == IOMMU_DEV_FEAT_AUX)
|
|
|
|
return scalable_mode_support() && info && info->auxd_enabled;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-25 09:30:33 +08:00
|
|
|
static int
|
|
|
|
intel_iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
|
|
|
|
{
|
|
|
|
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
|
|
|
|
|
|
|
|
return dmar_domain->default_pasid > 0 ?
|
|
|
|
dmar_domain->default_pasid : -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:41:32 +08:00
|
|
|
static bool intel_iommu_is_attach_deferred(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
return dev->archdata.iommu == DEFER_DEVICE_DOMAIN_INFO;
|
|
|
|
}
|
|
|
|
|
2017-02-01 20:23:08 +08:00
|
|
|
const struct iommu_ops intel_iommu_ops = {
|
2017-01-20 04:57:53 +08:00
|
|
|
.capable = intel_iommu_capable,
|
|
|
|
.domain_alloc = intel_iommu_domain_alloc,
|
|
|
|
.domain_free = intel_iommu_domain_free,
|
|
|
|
.attach_dev = intel_iommu_attach_device,
|
|
|
|
.detach_dev = intel_iommu_detach_device,
|
2019-03-25 09:30:32 +08:00
|
|
|
.aux_attach_dev = intel_iommu_aux_attach_device,
|
|
|
|
.aux_detach_dev = intel_iommu_aux_detach_device,
|
2019-03-25 09:30:33 +08:00
|
|
|
.aux_get_pasid = intel_iommu_aux_get_pasid,
|
2017-01-20 04:57:53 +08:00
|
|
|
.map = intel_iommu_map,
|
|
|
|
.unmap = intel_iommu_unmap,
|
|
|
|
.iova_to_phys = intel_iommu_iova_to_phys,
|
|
|
|
.add_device = intel_iommu_add_device,
|
|
|
|
.remove_device = intel_iommu_remove_device,
|
|
|
|
.get_resv_regions = intel_iommu_get_resv_regions,
|
|
|
|
.put_resv_regions = intel_iommu_put_resv_regions,
|
2019-05-25 13:41:23 +08:00
|
|
|
.apply_resv_region = intel_iommu_apply_resv_region,
|
2017-01-20 04:57:53 +08:00
|
|
|
.device_group = pci_device_group,
|
2019-03-25 09:30:30 +08:00
|
|
|
.dev_has_feat = intel_iommu_dev_has_feat,
|
|
|
|
.dev_feat_enabled = intel_iommu_dev_feat_enabled,
|
|
|
|
.dev_enable_feat = intel_iommu_dev_enable_feat,
|
|
|
|
.dev_disable_feat = intel_iommu_dev_disable_feat,
|
2019-05-25 13:41:32 +08:00
|
|
|
.is_attach_deferred = intel_iommu_is_attach_deferred,
|
2017-01-20 04:57:53 +08:00
|
|
|
.pgsize_bitmap = INTEL_IOMMU_PGSIZES,
|
2008-12-03 22:14:02 +08:00
|
|
|
};
|
2009-02-14 07:18:03 +08:00
|
|
|
|
2019-09-09 19:00:10 +08:00
|
|
|
static void quirk_iommu_igfx(struct pci_dev *dev)
|
2013-01-21 06:50:13 +08:00
|
|
|
{
|
2019-02-09 06:06:00 +08:00
|
|
|
pci_info(dev, "Disabling IOMMU for graphics on this chipset\n");
|
2013-01-21 06:50:13 +08:00
|
|
|
dmar_map_gfx = 0;
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:00:10 +08:00
|
|
|
/* G4x/GM45 integrated gfx dmar support is totally busted. */
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_igfx);
|
|
|
|
|
|
|
|
/* Broadwell igfx malfunctions with dmar */
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1606, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x160B, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x160E, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1602, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x160A, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x160D, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1616, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x161B, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x161E, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1612, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x161A, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x161D, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1626, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x162B, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x162E, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1622, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x162A, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x162D, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1636, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163B, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163E, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1632, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163A, quirk_iommu_igfx);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);
|
2013-01-21 06:50:13 +08:00
|
|
|
|
2012-12-22 07:05:21 +08:00
|
|
|
static void quirk_iommu_rwbf(struct pci_dev *dev)
|
2009-02-14 07:18:03 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Mobile 4 Series Chipset neglects to set RWBF capability,
|
2013-01-22 02:48:59 +08:00
|
|
|
* but needs it. Same seems to hold for the desktop versions.
|
2009-02-14 07:18:03 +08:00
|
|
|
*/
|
2019-02-09 06:06:00 +08:00
|
|
|
pci_info(dev, "Forcing write-buffer flush capability\n");
|
2009-02-14 07:18:03 +08:00
|
|
|
rwbf_quirk = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
|
2013-01-22 02:48:59 +08:00
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
|
2009-10-01 00:12:17 +08:00
|
|
|
|
2010-08-26 04:17:34 +08:00
|
|
|
#define GGC 0x52
|
|
|
|
#define GGC_MEMORY_SIZE_MASK (0xf << 8)
|
|
|
|
#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
|
|
|
|
#define GGC_MEMORY_SIZE_1M (0x1 << 8)
|
|
|
|
#define GGC_MEMORY_SIZE_2M (0x3 << 8)
|
|
|
|
#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
|
|
|
|
#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
|
|
|
|
#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
|
|
|
|
#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
|
|
|
|
|
2012-12-22 07:05:21 +08:00
|
|
|
static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
|
2010-09-22 05:28:23 +08:00
|
|
|
{
|
|
|
|
unsigned short ggc;
|
|
|
|
|
2010-08-26 04:17:34 +08:00
|
|
|
if (pci_read_config_word(dev, GGC, &ggc))
|
2010-09-22 05:28:23 +08:00
|
|
|
return;
|
|
|
|
|
2010-08-26 04:17:34 +08:00
|
|
|
if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
|
2019-02-09 06:06:00 +08:00
|
|
|
pci_info(dev, "BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
|
2010-09-22 05:28:23 +08:00
|
|
|
dmar_map_gfx = 0;
|
2011-09-26 10:11:14 +08:00
|
|
|
} else if (dmar_map_gfx) {
|
|
|
|
/* we have to ensure the gfx device is idle before we flush */
|
2019-02-09 06:06:00 +08:00
|
|
|
pci_info(dev, "Disabling batched IOTLB flush on Ironlake\n");
|
2011-09-26 10:11:14 +08:00
|
|
|
intel_iommu_strict = 1;
|
|
|
|
}
|
2010-09-22 05:28:23 +08:00
|
|
|
}
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
|
|
|
|
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
|
|
|
|
|
2009-10-01 00:12:17 +08:00
|
|
|
/* On Tylersburg chipsets, some BIOSes have been known to enable the
|
|
|
|
ISOCH DMAR unit for the Azalia sound device, but not give it any
|
|
|
|
TLB entries, which causes it to deadlock. Check for that. We do
|
|
|
|
this in a function called from init_dmars(), instead of in a PCI
|
|
|
|
quirk, because we don't want to print the obnoxious "BIOS broken"
|
|
|
|
message if VT-d is actually disabled.
|
|
|
|
*/
|
|
|
|
static void __init check_tylersburg_isoch(void)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
uint32_t vtisochctrl;
|
|
|
|
|
|
|
|
/* If there's no Azalia in the system anyway, forget it. */
|
|
|
|
pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
|
|
|
|
if (!pdev)
|
|
|
|
return;
|
|
|
|
pci_dev_put(pdev);
|
|
|
|
|
|
|
|
/* System Management Registers. Might be hidden, in which case
|
|
|
|
we can't do the sanity check. But that's OK, because the
|
|
|
|
known-broken BIOSes _don't_ actually hide it, so far. */
|
|
|
|
pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
|
|
|
|
if (!pdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
|
|
|
|
pci_dev_put(pdev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pci_dev_put(pdev);
|
|
|
|
|
|
|
|
/* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
|
|
|
|
if (vtisochctrl & 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Drop all bits other than the number of TLB entries */
|
|
|
|
vtisochctrl &= 0x1c;
|
|
|
|
|
|
|
|
/* If we have the recommended number of TLB entries (16), fine. */
|
|
|
|
if (vtisochctrl == 0x10)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Zero TLB entries? You get to ride the short bus to school. */
|
|
|
|
if (!vtisochctrl) {
|
|
|
|
WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
|
|
|
|
"BIOS vendor: %s; Ver: %s; Product Version: %s\n",
|
|
|
|
dmi_get_system_info(DMI_BIOS_VENDOR),
|
|
|
|
dmi_get_system_info(DMI_BIOS_VERSION),
|
|
|
|
dmi_get_system_info(DMI_PRODUCT_VERSION));
|
|
|
|
iommu_identity_mapping |= IDENTMAP_AZALIA;
|
|
|
|
return;
|
|
|
|
}
|
2015-06-12 15:57:06 +08:00
|
|
|
|
|
|
|
pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
|
2009-10-01 00:12:17 +08:00
|
|
|
vtisochctrl);
|
|
|
|
}
|