blob: 4faec337c0cf419a880090e7872d7dd359c24471 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
mark gross5e0d2a62008-03-04 15:22:08 -080036#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030037#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010038#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010040#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070041#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100042#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020043#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080044#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070045#include <linux/dma-contiguous.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070046#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070047#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090048#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070049
Joerg Roedel078e1ee2012-09-26 12:44:43 +020050#include "irq_remapping.h"
51
Fenghua Yu5b6985c2008-10-16 18:02:32 -070052#define ROOT_SIZE VTD_PAGE_SIZE
53#define CONTEXT_SIZE VTD_PAGE_SIZE
54
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070055#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000056#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070057#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070058#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070059
60#define IOAPIC_RANGE_START (0xfee00000)
61#define IOAPIC_RANGE_END (0xfeefffff)
62#define IOVA_START_ADDR (0x1000)
63
64#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
65
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070066#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080067#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070068
David Woodhouse2ebe3152009-09-19 07:34:04 -070069#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
70#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
71
72/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
73 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
74#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
75 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
76#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070077
Robin Murphy1b722502015-01-12 17:51:15 +000078/* IO virtual address start page frame number */
79#define IOVA_START_PFN (1)
80
Mark McLoughlinf27be032008-11-20 15:49:43 +000081#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a92009-04-06 19:01:15 -070082#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070083#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080084
Andrew Mortondf08cdc2010-09-22 13:05:11 -070085/* page table handling */
86#define LEVEL_STRIDE (9)
87#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
88
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020089/*
90 * This bitmap is used to advertise the page sizes our hardware support
91 * to the IOMMU core, which will then use this information to split
92 * physically contiguous memory regions it is mapping into page sizes
93 * that we support.
94 *
95 * Traditionally the IOMMU core just handed us the mappings directly,
96 * after making sure the size is an order of a 4KiB page and that the
97 * mapping has natural alignment.
98 *
99 * To retain this behavior, we currently advertise that we support
100 * all page sizes that are an order of 4KiB.
101 *
102 * If at some point we'd like to utilize the IOMMU core's new behavior,
103 * we could change this to advertise the real page sizes we support.
104 */
105#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
106
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700107static inline int agaw_to_level(int agaw)
108{
109 return agaw + 2;
110}
111
112static inline int agaw_to_width(int agaw)
113{
Jiang Liu5c645b32014-01-06 14:18:12 +0800114 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700115}
116
117static inline int width_to_agaw(int width)
118{
Jiang Liu5c645b32014-01-06 14:18:12 +0800119 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700120}
121
122static inline unsigned int level_to_offset_bits(int level)
123{
124 return (level - 1) * LEVEL_STRIDE;
125}
126
127static inline int pfn_level_offset(unsigned long pfn, int level)
128{
129 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
130}
131
132static inline unsigned long level_mask(int level)
133{
134 return -1UL << level_to_offset_bits(level);
135}
136
137static inline unsigned long level_size(int level)
138{
139 return 1UL << level_to_offset_bits(level);
140}
141
142static inline unsigned long align_to_level(unsigned long pfn, int level)
143{
144 return (pfn + level_size(level) - 1) & level_mask(level);
145}
David Woodhousefd18de52009-05-10 23:57:41 +0100146
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100147static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
148{
Jiang Liu5c645b32014-01-06 14:18:12 +0800149 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100150}
151
David Woodhousedd4e8312009-06-27 16:21:20 +0100152/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
153 are never going to work. */
154static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
155{
156 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
157}
158
159static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
160{
161 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
162}
163static inline unsigned long page_to_dma_pfn(struct page *pg)
164{
165 return mm_to_dma_pfn(page_to_pfn(pg));
166}
167static inline unsigned long virt_to_dma_pfn(void *p)
168{
169 return page_to_dma_pfn(virt_to_page(p));
170}
171
Weidong Hand9630fe2008-12-08 11:06:32 +0800172/* global iommu list, set NULL for ignored DMAR units */
173static struct intel_iommu **g_iommus;
174
David Woodhousee0fc7e02009-09-30 09:12:17 -0700175static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000176static int rwbf_quirk;
177
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000178/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700179 * set to 1 to panic kernel if can't successfully enable VT-d
180 * (used when kernel is launched w/ TXT)
181 */
182static int force_on = 0;
183
184/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000185 * 0: Present
186 * 1-11: Reserved
187 * 12-63: Context Ptr (12 - (haw-1))
188 * 64-127: Reserved
189 */
190struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000191 u64 lo;
192 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000193};
194#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000195
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000196
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000197/*
198 * low 64 bits:
199 * 0: present
200 * 1: fault processing disable
201 * 2-3: translation type
202 * 12-63: address space root
203 * high 64 bits:
204 * 0-2: address width
205 * 3-6: aval
206 * 8-23: domain id
207 */
208struct context_entry {
209 u64 lo;
210 u64 hi;
211};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000212
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000213static inline bool context_present(struct context_entry *context)
214{
215 return (context->lo & 1);
216}
217static inline void context_set_present(struct context_entry *context)
218{
219 context->lo |= 1;
220}
221
222static inline void context_set_fault_enable(struct context_entry *context)
223{
224 context->lo &= (((u64)-1) << 2) | 1;
225}
226
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000227static inline void context_set_translation_type(struct context_entry *context,
228 unsigned long value)
229{
230 context->lo &= (((u64)-1) << 4) | 3;
231 context->lo |= (value & 3) << 2;
232}
233
234static inline void context_set_address_root(struct context_entry *context,
235 unsigned long value)
236{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800237 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000238 context->lo |= value & VTD_PAGE_MASK;
239}
240
241static inline void context_set_address_width(struct context_entry *context,
242 unsigned long value)
243{
244 context->hi |= value & 7;
245}
246
247static inline void context_set_domain_id(struct context_entry *context,
248 unsigned long value)
249{
250 context->hi |= (value & ((1 << 16) - 1)) << 8;
251}
252
253static inline void context_clear_entry(struct context_entry *context)
254{
255 context->lo = 0;
256 context->hi = 0;
257}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000258
Mark McLoughlin622ba122008-11-20 15:49:46 +0000259/*
260 * 0: readable
261 * 1: writable
262 * 2-6: reserved
263 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800264 * 8-10: available
265 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000266 * 12-63: Host physcial address
267 */
268struct dma_pte {
269 u64 val;
270};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000271
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000272static inline void dma_clear_pte(struct dma_pte *pte)
273{
274 pte->val = 0;
275}
276
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000277static inline u64 dma_pte_addr(struct dma_pte *pte)
278{
David Woodhousec85994e2009-07-01 19:21:24 +0100279#ifdef CONFIG_64BIT
280 return pte->val & VTD_PAGE_MASK;
281#else
282 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100283 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100284#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000285}
286
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000287static inline bool dma_pte_present(struct dma_pte *pte)
288{
289 return (pte->val & 3) != 0;
290}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000291
Allen Kay4399c8b2011-10-14 12:32:46 -0700292static inline bool dma_pte_superpage(struct dma_pte *pte)
293{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200294 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700295}
296
David Woodhouse75e6bf92009-07-02 11:21:16 +0100297static inline int first_pte_in_page(struct dma_pte *pte)
298{
299 return !((unsigned long)pte & ~VTD_PAGE_MASK);
300}
301
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700302/*
303 * This domain is a statically identity mapping domain.
304 * 1. This domain creats a static 1:1 mapping to all usable memory.
305 * 2. It maps to each iommu if successful.
306 * 3. Each iommu mapps to this domain if successful.
307 */
David Woodhouse19943b02009-08-04 16:19:20 +0100308static struct dmar_domain *si_domain;
309static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700310
Weidong Han1ce28fe2008-12-08 16:35:39 +0800311/* domain represents a virtual machine, more than one devices
312 * across iommus may be owned in one domain, e.g. kvm guest.
313 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800314#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800315
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700316/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800317#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700318
Mark McLoughlin99126f72008-11-20 15:49:47 +0000319struct dmar_domain {
320 int id; /* domain id */
Suresh Siddha4c923d42009-10-02 11:01:24 -0700321 int nid; /* node id */
Jiang Liu78d8e702014-11-09 22:47:57 +0800322 DECLARE_BITMAP(iommu_bmp, DMAR_UNITS_SUPPORTED);
Mike Travis1b198bb2012-03-05 15:05:16 -0800323 /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000324
Joerg Roedel00a77de2015-03-26 13:43:08 +0100325 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000326 struct iova_domain iovad; /* iova's that belong to this domain */
327
328 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000329 int gaw; /* max guest address width */
330
331 /* adjusted guest address width, 0 is level 2 30-bit */
332 int agaw;
333
Weidong Han3b5410e2008-12-08 09:17:15 +0800334 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800335
336 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800337 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800338 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100339 int iommu_superpage;/* Level of superpages supported:
340 0 == 4KiB (no superpages), 1 == 2MiB,
341 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanc7151a82008-12-08 22:51:37 +0800342 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800343 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100344
345 struct iommu_domain domain; /* generic domain data structure for
346 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000347};
348
Mark McLoughlina647dac2008-11-20 15:49:48 +0000349/* PCI domain-device relationship */
350struct device_domain_info {
351 struct list_head link; /* link to domain siblings */
352 struct list_head global; /* link to global list */
David Woodhouse276dbf92009-04-04 01:45:37 +0100353 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000354 u8 devfn; /* PCI devfn number */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000355 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800356 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000357 struct dmar_domain *domain; /* pointer to domain */
358};
359
Jiang Liub94e4112014-02-19 14:07:25 +0800360struct dmar_rmrr_unit {
361 struct list_head list; /* list of rmrr units */
362 struct acpi_dmar_header *hdr; /* ACPI header */
363 u64 base_address; /* reserved base address*/
364 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000365 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800366 int devices_cnt; /* target device count */
367};
368
369struct dmar_atsr_unit {
370 struct list_head list; /* list of ATSR units */
371 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000372 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800373 int devices_cnt; /* target device count */
374 u8 include_all:1; /* include all ports */
375};
376
377static LIST_HEAD(dmar_atsr_units);
378static LIST_HEAD(dmar_rmrr_units);
379
380#define for_each_rmrr_units(rmrr) \
381 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
382
mark gross5e0d2a62008-03-04 15:22:08 -0800383static void flush_unmaps_timeout(unsigned long data);
384
Jiang Liub707cb02014-01-06 14:18:26 +0800385static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800386
mark gross80b20dd2008-04-18 13:53:58 -0700387#define HIGH_WATER_MARK 250
388struct deferred_flush_tables {
389 int next;
390 struct iova *iova[HIGH_WATER_MARK];
391 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000392 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700393};
394
395static struct deferred_flush_tables *deferred_flush;
396
mark gross5e0d2a62008-03-04 15:22:08 -0800397/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800398static int g_num_of_iommus;
399
400static DEFINE_SPINLOCK(async_umap_flush_lock);
401static LIST_HEAD(unmaps_to_do);
402
403static int timer_on;
404static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800405
Jiang Liu92d03cc2014-02-19 14:07:28 +0800406static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700407static void domain_remove_dev_info(struct dmar_domain *domain);
Jiang Liub94e4112014-02-19 14:07:25 +0800408static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -0700409 struct device *dev);
Jiang Liu92d03cc2014-02-19 14:07:28 +0800410static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000411 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800412static int domain_detach_iommu(struct dmar_domain *domain,
413 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700414
Suresh Siddhad3f13812011-08-23 17:05:25 -0700415#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800416int dmar_disabled = 0;
417#else
418int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700419#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800420
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200421int intel_iommu_enabled = 0;
422EXPORT_SYMBOL_GPL(intel_iommu_enabled);
423
David Woodhouse2d9e6672010-06-15 10:57:57 +0100424static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700425static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800426static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100427static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100428static int intel_iommu_ecs = 1;
429
430/* We only actually use ECS when PASID support (on the new bit 40)
431 * is also advertised. Some early implementations — the ones with
432 * PASID support on bit 28 — have issues even when we *only* use
433 * extended root/context tables. */
434#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
435 ecap_pasid(iommu->ecap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700436
David Woodhousec0771df2011-10-14 20:59:46 +0100437int intel_iommu_gfx_mapped;
438EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
439
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700440#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
441static DEFINE_SPINLOCK(device_domain_lock);
442static LIST_HEAD(device_domain_list);
443
Thierry Redingb22f6432014-06-27 09:03:12 +0200444static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100445
Joerg Roedel00a77de2015-03-26 13:43:08 +0100446/* Convert generic 'struct iommu_domain to private struct dmar_domain */
447static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
448{
449 return container_of(dom, struct dmar_domain, domain);
450}
451
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700452static int __init intel_iommu_setup(char *str)
453{
454 if (!str)
455 return -EINVAL;
456 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800457 if (!strncmp(str, "on", 2)) {
458 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200459 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800460 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700461 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200462 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700463 } else if (!strncmp(str, "igfx_off", 8)) {
464 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200465 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700466 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200467 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700468 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800469 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200470 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800471 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100472 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200473 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100474 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100475 } else if (!strncmp(str, "ecs_off", 7)) {
476 printk(KERN_INFO
477 "Intel-IOMMU: disable extended context table support\n");
478 intel_iommu_ecs = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700479 }
480
481 str += strcspn(str, ",");
482 while (*str == ',')
483 str++;
484 }
485 return 0;
486}
487__setup("intel_iommu=", intel_iommu_setup);
488
489static struct kmem_cache *iommu_domain_cache;
490static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700491
Suresh Siddha4c923d42009-10-02 11:01:24 -0700492static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700493{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700494 struct page *page;
495 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700496
Suresh Siddha4c923d42009-10-02 11:01:24 -0700497 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
498 if (page)
499 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700500 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700501}
502
503static inline void free_pgtable_page(void *vaddr)
504{
505 free_page((unsigned long)vaddr);
506}
507
508static inline void *alloc_domain_mem(void)
509{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900510 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700511}
512
Kay, Allen M38717942008-09-09 18:37:29 +0300513static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700514{
515 kmem_cache_free(iommu_domain_cache, vaddr);
516}
517
518static inline void * alloc_devinfo_mem(void)
519{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900520 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700521}
522
523static inline void free_devinfo_mem(void *vaddr)
524{
525 kmem_cache_free(iommu_devinfo_cache, vaddr);
526}
527
Jiang Liuab8dfe22014-07-11 14:19:27 +0800528static inline int domain_type_is_vm(struct dmar_domain *domain)
529{
530 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
531}
532
533static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
534{
535 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
536 DOMAIN_FLAG_STATIC_IDENTITY);
537}
Weidong Han1b573682008-12-08 15:34:06 +0800538
Jiang Liu162d1b12014-07-11 14:19:35 +0800539static inline int domain_pfn_supported(struct dmar_domain *domain,
540 unsigned long pfn)
541{
542 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
543
544 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
545}
546
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700547static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800548{
549 unsigned long sagaw;
550 int agaw = -1;
551
552 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700553 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800554 agaw >= 0; agaw--) {
555 if (test_bit(agaw, &sagaw))
556 break;
557 }
558
559 return agaw;
560}
561
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700562/*
563 * Calculate max SAGAW for each iommu.
564 */
565int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
566{
567 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
568}
569
570/*
571 * calculate agaw for each iommu.
572 * "SAGAW" may be different across iommus, use a default agaw, and
573 * get a supported less agaw for iommus that don't support the default agaw.
574 */
575int iommu_calculate_agaw(struct intel_iommu *iommu)
576{
577 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
578}
579
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700580/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800581static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
582{
583 int iommu_id;
584
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700585 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800586 BUG_ON(domain_type_is_vm_or_si(domain));
Mike Travis1b198bb2012-03-05 15:05:16 -0800587 iommu_id = find_first_bit(domain->iommu_bmp, g_num_of_iommus);
Weidong Han8c11e792008-12-08 15:29:22 +0800588 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
589 return NULL;
590
591 return g_iommus[iommu_id];
592}
593
Weidong Han8e6040972008-12-08 15:49:06 +0800594static void domain_update_iommu_coherency(struct dmar_domain *domain)
595{
David Woodhoused0501962014-03-11 17:10:29 -0700596 struct dmar_drhd_unit *drhd;
597 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100598 bool found = false;
599 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800600
David Woodhoused0501962014-03-11 17:10:29 -0700601 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800602
Mike Travis1b198bb2012-03-05 15:05:16 -0800603 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100604 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800605 if (!ecap_coherent(g_iommus[i]->ecap)) {
606 domain->iommu_coherency = 0;
607 break;
608 }
Weidong Han8e6040972008-12-08 15:49:06 +0800609 }
David Woodhoused0501962014-03-11 17:10:29 -0700610 if (found)
611 return;
612
613 /* No hardware attached; use lowest common denominator */
614 rcu_read_lock();
615 for_each_active_iommu(iommu, drhd) {
616 if (!ecap_coherent(iommu->ecap)) {
617 domain->iommu_coherency = 0;
618 break;
619 }
620 }
621 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800622}
623
Jiang Liu161f6932014-07-11 14:19:37 +0800624static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100625{
Allen Kay8140a952011-10-14 12:32:17 -0700626 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800627 struct intel_iommu *iommu;
628 int ret = 1;
629
630 rcu_read_lock();
631 for_each_active_iommu(iommu, drhd) {
632 if (iommu != skip) {
633 if (!ecap_sc_support(iommu->ecap)) {
634 ret = 0;
635 break;
636 }
637 }
638 }
639 rcu_read_unlock();
640
641 return ret;
642}
643
644static int domain_update_iommu_superpage(struct intel_iommu *skip)
645{
646 struct dmar_drhd_unit *drhd;
647 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700648 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100649
650 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800651 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100652 }
653
Allen Kay8140a952011-10-14 12:32:17 -0700654 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800655 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700656 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800657 if (iommu != skip) {
658 mask &= cap_super_page_val(iommu->cap);
659 if (!mask)
660 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100661 }
662 }
Jiang Liu0e242612014-02-19 14:07:34 +0800663 rcu_read_unlock();
664
Jiang Liu161f6932014-07-11 14:19:37 +0800665 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100666}
667
Sheng Yang58c610b2009-03-18 15:33:05 +0800668/* Some capabilities may be different across iommus */
669static void domain_update_iommu_cap(struct dmar_domain *domain)
670{
671 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800672 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
673 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800674}
675
David Woodhouse03ecc322015-02-13 14:35:21 +0000676static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
677 u8 bus, u8 devfn, int alloc)
678{
679 struct root_entry *root = &iommu->root_entry[bus];
680 struct context_entry *context;
681 u64 *entry;
682
David Woodhousec83b2f22015-06-12 10:15:49 +0100683 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000684 if (devfn >= 0x80) {
685 devfn -= 0x80;
686 entry = &root->hi;
687 }
688 devfn *= 2;
689 }
690 entry = &root->lo;
691 if (*entry & 1)
692 context = phys_to_virt(*entry & VTD_PAGE_MASK);
693 else {
694 unsigned long phy_addr;
695 if (!alloc)
696 return NULL;
697
698 context = alloc_pgtable_page(iommu->node);
699 if (!context)
700 return NULL;
701
702 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
703 phy_addr = virt_to_phys((void *)context);
704 *entry = phy_addr | 1;
705 __iommu_flush_cache(iommu, entry, sizeof(*entry));
706 }
707 return &context[devfn];
708}
709
David Woodhouse4ed6a542015-05-11 14:59:20 +0100710static int iommu_dummy(struct device *dev)
711{
712 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
713}
714
David Woodhouse156baca2014-03-09 14:00:57 -0700715static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800716{
717 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800718 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700719 struct device *tmp;
720 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800721 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800722 int i;
723
David Woodhouse4ed6a542015-05-11 14:59:20 +0100724 if (iommu_dummy(dev))
725 return NULL;
726
David Woodhouse156baca2014-03-09 14:00:57 -0700727 if (dev_is_pci(dev)) {
728 pdev = to_pci_dev(dev);
729 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100730 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700731 dev = &ACPI_COMPANION(dev)->dev;
732
Jiang Liu0e242612014-02-19 14:07:34 +0800733 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800734 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700735 if (pdev && segment != drhd->segment)
David Woodhouse276dbf92009-04-04 01:45:37 +0100736 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800737
Jiang Liub683b232014-02-19 14:07:32 +0800738 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700739 drhd->devices_cnt, i, tmp) {
740 if (tmp == dev) {
741 *bus = drhd->devices[i].bus;
742 *devfn = drhd->devices[i].devfn;
743 goto out;
744 }
745
746 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000747 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700748
749 ptmp = to_pci_dev(tmp);
750 if (ptmp->subordinate &&
751 ptmp->subordinate->number <= pdev->bus->number &&
752 ptmp->subordinate->busn_res.end >= pdev->bus->number)
753 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100754 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800755
David Woodhouse156baca2014-03-09 14:00:57 -0700756 if (pdev && drhd->include_all) {
757 got_pdev:
758 *bus = pdev->bus->number;
759 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800760 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700761 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800762 }
Jiang Liub683b232014-02-19 14:07:32 +0800763 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700764 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800765 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800766
Jiang Liub683b232014-02-19 14:07:32 +0800767 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800768}
769
Weidong Han5331fe62008-12-08 23:00:00 +0800770static void domain_flush_cache(struct dmar_domain *domain,
771 void *addr, int size)
772{
773 if (!domain->iommu_coherency)
774 clflush_cache_range(addr, size);
775}
776
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700777static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
778{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700779 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000780 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700781 unsigned long flags;
782
783 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000784 context = iommu_context_addr(iommu, bus, devfn, 0);
785 if (context)
786 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700787 spin_unlock_irqrestore(&iommu->lock, flags);
788 return ret;
789}
790
791static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
792{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700793 struct context_entry *context;
794 unsigned long flags;
795
796 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000797 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700798 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000799 context_clear_entry(context);
800 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700801 }
802 spin_unlock_irqrestore(&iommu->lock, flags);
803}
804
805static void free_context_table(struct intel_iommu *iommu)
806{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700807 int i;
808 unsigned long flags;
809 struct context_entry *context;
810
811 spin_lock_irqsave(&iommu->lock, flags);
812 if (!iommu->root_entry) {
813 goto out;
814 }
815 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000816 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700817 if (context)
818 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000819
David Woodhousec83b2f22015-06-12 10:15:49 +0100820 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000821 continue;
822
823 context = iommu_context_addr(iommu, i, 0x80, 0);
824 if (context)
825 free_pgtable_page(context);
826
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700827 }
828 free_pgtable_page(iommu->root_entry);
829 iommu->root_entry = NULL;
830out:
831 spin_unlock_irqrestore(&iommu->lock, flags);
832}
833
David Woodhouseb026fd22009-06-28 10:37:25 +0100834static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000835 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700836{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700837 struct dma_pte *parent, *pte = NULL;
838 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700839 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700840
841 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200842
Jiang Liu162d1b12014-07-11 14:19:35 +0800843 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200844 /* Address beyond IOMMU's addressing capabilities. */
845 return NULL;
846
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700847 parent = domain->pgd;
848
David Woodhouse5cf0a762014-03-19 16:07:49 +0000849 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700850 void *tmp_page;
851
David Woodhouseb026fd22009-06-28 10:37:25 +0100852 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700853 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000854 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100855 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000856 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700857 break;
858
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000859 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100860 uint64_t pteval;
861
Suresh Siddha4c923d42009-10-02 11:01:24 -0700862 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700863
David Woodhouse206a73c12009-07-01 19:30:28 +0100864 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700865 return NULL;
David Woodhouse206a73c12009-07-01 19:30:28 +0100866
David Woodhousec85994e2009-07-01 19:21:24 +0100867 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400868 pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE;
Yijing Wangeffad4b2014-05-26 20:13:47 +0800869 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100870 /* Someone else set it while we were thinking; use theirs. */
871 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800872 else
David Woodhousec85994e2009-07-01 19:21:24 +0100873 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700874 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000875 if (level == 1)
876 break;
877
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000878 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700879 level--;
880 }
881
David Woodhouse5cf0a762014-03-19 16:07:49 +0000882 if (!*target_level)
883 *target_level = level;
884
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700885 return pte;
886}
887
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100888
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700889/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100890static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
891 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100892 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700893{
894 struct dma_pte *parent, *pte = NULL;
895 int total = agaw_to_level(domain->agaw);
896 int offset;
897
898 parent = domain->pgd;
899 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100900 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700901 pte = &parent[offset];
902 if (level == total)
903 return pte;
904
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100905 if (!dma_pte_present(pte)) {
906 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700907 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100908 }
909
Yijing Wange16922a2014-05-20 20:37:51 +0800910 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100911 *large_page = total;
912 return pte;
913 }
914
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000915 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700916 total--;
917 }
918 return NULL;
919}
920
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700921/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +0000922static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +0100923 unsigned long start_pfn,
924 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700925{
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100926 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +0100927 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700928
Jiang Liu162d1b12014-07-11 14:19:35 +0800929 BUG_ON(!domain_pfn_supported(domain, start_pfn));
930 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -0700931 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +0100932
David Woodhouse04b18e62009-06-27 19:15:01 +0100933 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -0700934 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100935 large_page = 1;
936 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100937 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100938 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100939 continue;
940 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100941 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +0100942 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100943 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100944 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +0100945 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
946
David Woodhouse310a5ab2009-06-28 18:52:20 +0100947 domain_flush_cache(domain, first_pte,
948 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -0700949
950 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700951}
952
Alex Williamson3269ee02013-06-15 10:27:19 -0600953static void dma_pte_free_level(struct dmar_domain *domain, int level,
954 struct dma_pte *pte, unsigned long pfn,
955 unsigned long start_pfn, unsigned long last_pfn)
956{
957 pfn = max(start_pfn, pfn);
958 pte = &pte[pfn_level_offset(pfn, level)];
959
960 do {
961 unsigned long level_pfn;
962 struct dma_pte *level_pte;
963
964 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
965 goto next;
966
967 level_pfn = pfn & level_mask(level - 1);
968 level_pte = phys_to_virt(dma_pte_addr(pte));
969
970 if (level > 2)
971 dma_pte_free_level(domain, level - 1, level_pte,
972 level_pfn, start_pfn, last_pfn);
973
974 /* If range covers entire pagetable, free it */
975 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -0800976 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -0600977 dma_clear_pte(pte);
978 domain_flush_cache(domain, pte, sizeof(*pte));
979 free_pgtable_page(level_pte);
980 }
981next:
982 pfn += level_size(level);
983 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
984}
985
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700986/* free page table pages. last level pte should already be cleared */
987static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +0100988 unsigned long start_pfn,
989 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700990{
Jiang Liu162d1b12014-07-11 14:19:35 +0800991 BUG_ON(!domain_pfn_supported(domain, start_pfn));
992 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -0700993 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700994
Jiang Liud41a4ad2014-07-11 14:19:34 +0800995 dma_pte_clear_range(domain, start_pfn, last_pfn);
996
David Woodhousef3a0a522009-06-30 03:40:07 +0100997 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -0600998 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
999 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001000
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001001 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001002 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001003 free_pgtable_page(domain->pgd);
1004 domain->pgd = NULL;
1005 }
1006}
1007
David Woodhouseea8ea462014-03-05 17:09:32 +00001008/* When a page at a given level is being unlinked from its parent, we don't
1009 need to *modify* it at all. All we need to do is make a list of all the
1010 pages which can be freed just as soon as we've flushed the IOTLB and we
1011 know the hardware page-walk will no longer touch them.
1012 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1013 be freed. */
1014static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1015 int level, struct dma_pte *pte,
1016 struct page *freelist)
1017{
1018 struct page *pg;
1019
1020 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1021 pg->freelist = freelist;
1022 freelist = pg;
1023
1024 if (level == 1)
1025 return freelist;
1026
Jiang Liuadeb2592014-04-09 10:20:39 +08001027 pte = page_address(pg);
1028 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001029 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1030 freelist = dma_pte_list_pagetables(domain, level - 1,
1031 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001032 pte++;
1033 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001034
1035 return freelist;
1036}
1037
1038static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1039 struct dma_pte *pte, unsigned long pfn,
1040 unsigned long start_pfn,
1041 unsigned long last_pfn,
1042 struct page *freelist)
1043{
1044 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1045
1046 pfn = max(start_pfn, pfn);
1047 pte = &pte[pfn_level_offset(pfn, level)];
1048
1049 do {
1050 unsigned long level_pfn;
1051
1052 if (!dma_pte_present(pte))
1053 goto next;
1054
1055 level_pfn = pfn & level_mask(level);
1056
1057 /* If range covers entire pagetable, free it */
1058 if (start_pfn <= level_pfn &&
1059 last_pfn >= level_pfn + level_size(level) - 1) {
1060 /* These suborbinate page tables are going away entirely. Don't
1061 bother to clear them; we're just going to *free* them. */
1062 if (level > 1 && !dma_pte_superpage(pte))
1063 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1064
1065 dma_clear_pte(pte);
1066 if (!first_pte)
1067 first_pte = pte;
1068 last_pte = pte;
1069 } else if (level > 1) {
1070 /* Recurse down into a level that isn't *entirely* obsolete */
1071 freelist = dma_pte_clear_level(domain, level - 1,
1072 phys_to_virt(dma_pte_addr(pte)),
1073 level_pfn, start_pfn, last_pfn,
1074 freelist);
1075 }
1076next:
1077 pfn += level_size(level);
1078 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1079
1080 if (first_pte)
1081 domain_flush_cache(domain, first_pte,
1082 (void *)++last_pte - (void *)first_pte);
1083
1084 return freelist;
1085}
1086
1087/* We can't just free the pages because the IOMMU may still be walking
1088 the page tables, and may have cached the intermediate levels. The
1089 pages can only be freed after the IOTLB flush has been done. */
1090struct page *domain_unmap(struct dmar_domain *domain,
1091 unsigned long start_pfn,
1092 unsigned long last_pfn)
1093{
David Woodhouseea8ea462014-03-05 17:09:32 +00001094 struct page *freelist = NULL;
1095
Jiang Liu162d1b12014-07-11 14:19:35 +08001096 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1097 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001098 BUG_ON(start_pfn > last_pfn);
1099
1100 /* we don't need lock here; nobody else touches the iova range */
1101 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1102 domain->pgd, 0, start_pfn, last_pfn, NULL);
1103
1104 /* free pgd */
1105 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1106 struct page *pgd_page = virt_to_page(domain->pgd);
1107 pgd_page->freelist = freelist;
1108 freelist = pgd_page;
1109
1110 domain->pgd = NULL;
1111 }
1112
1113 return freelist;
1114}
1115
1116void dma_free_pagelist(struct page *freelist)
1117{
1118 struct page *pg;
1119
1120 while ((pg = freelist)) {
1121 freelist = pg->freelist;
1122 free_pgtable_page(page_address(pg));
1123 }
1124}
1125
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001126/* iommu handling */
1127static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1128{
1129 struct root_entry *root;
1130 unsigned long flags;
1131
Suresh Siddha4c923d42009-10-02 11:01:24 -07001132 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001133 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001134 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001135 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001136 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001137 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001138
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001139 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001140
1141 spin_lock_irqsave(&iommu->lock, flags);
1142 iommu->root_entry = root;
1143 spin_unlock_irqrestore(&iommu->lock, flags);
1144
1145 return 0;
1146}
1147
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001148static void iommu_set_root_entry(struct intel_iommu *iommu)
1149{
David Woodhouse03ecc322015-02-13 14:35:21 +00001150 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001151 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001152 unsigned long flag;
1153
David Woodhouse03ecc322015-02-13 14:35:21 +00001154 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001155 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001156 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001157
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001158 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001159 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001160
David Woodhousec416daa2009-05-10 20:30:58 +01001161 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001162
1163 /* Make sure hardware complete it */
1164 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001165 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001166
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001167 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001168}
1169
1170static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1171{
1172 u32 val;
1173 unsigned long flag;
1174
David Woodhouse9af88142009-02-13 23:18:03 +00001175 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001176 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001177
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001178 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001179 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001180
1181 /* Make sure hardware complete it */
1182 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001183 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001184
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001185 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001186}
1187
1188/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001189static void __iommu_flush_context(struct intel_iommu *iommu,
1190 u16 did, u16 source_id, u8 function_mask,
1191 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001192{
1193 u64 val = 0;
1194 unsigned long flag;
1195
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001196 switch (type) {
1197 case DMA_CCMD_GLOBAL_INVL:
1198 val = DMA_CCMD_GLOBAL_INVL;
1199 break;
1200 case DMA_CCMD_DOMAIN_INVL:
1201 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1202 break;
1203 case DMA_CCMD_DEVICE_INVL:
1204 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1205 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1206 break;
1207 default:
1208 BUG();
1209 }
1210 val |= DMA_CCMD_ICC;
1211
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001212 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001213 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1214
1215 /* Make sure hardware complete it */
1216 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1217 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1218
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001219 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001220}
1221
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001222/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001223static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1224 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001225{
1226 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1227 u64 val = 0, val_iva = 0;
1228 unsigned long flag;
1229
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001230 switch (type) {
1231 case DMA_TLB_GLOBAL_FLUSH:
1232 /* global flush doesn't need set IVA_REG */
1233 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1234 break;
1235 case DMA_TLB_DSI_FLUSH:
1236 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1237 break;
1238 case DMA_TLB_PSI_FLUSH:
1239 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001240 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241 val_iva = size_order | addr;
1242 break;
1243 default:
1244 BUG();
1245 }
1246 /* Note: set drain read/write */
1247#if 0
1248 /*
1249 * This is probably to be super secure.. Looks like we can
1250 * ignore it without any impact.
1251 */
1252 if (cap_read_drain(iommu->cap))
1253 val |= DMA_TLB_READ_DRAIN;
1254#endif
1255 if (cap_write_drain(iommu->cap))
1256 val |= DMA_TLB_WRITE_DRAIN;
1257
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001258 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001259 /* Note: Only uses first TLB reg currently */
1260 if (val_iva)
1261 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1262 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1263
1264 /* Make sure hardware complete it */
1265 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1266 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1267
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001268 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001269
1270 /* check IOTLB invalidation granularity */
1271 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001272 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001273 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001274 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001275 (unsigned long long)DMA_TLB_IIRG(type),
1276 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001277}
1278
David Woodhouse64ae8922014-03-09 12:52:30 -07001279static struct device_domain_info *
1280iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1281 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001282{
Quentin Lambert2f119c72015-02-06 10:59:53 +01001283 bool found = false;
Yu Zhao93a23a72009-05-18 13:51:37 +08001284 unsigned long flags;
1285 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001286 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001287
1288 if (!ecap_dev_iotlb_support(iommu->ecap))
1289 return NULL;
1290
1291 if (!iommu->qi)
1292 return NULL;
1293
1294 spin_lock_irqsave(&device_domain_lock, flags);
1295 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001296 if (info->iommu == iommu && info->bus == bus &&
1297 info->devfn == devfn) {
Quentin Lambert2f119c72015-02-06 10:59:53 +01001298 found = true;
Yu Zhao93a23a72009-05-18 13:51:37 +08001299 break;
1300 }
1301 spin_unlock_irqrestore(&device_domain_lock, flags);
1302
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001303 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001304 return NULL;
1305
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001306 pdev = to_pci_dev(info->dev);
1307
1308 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001309 return NULL;
1310
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001311 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001312 return NULL;
1313
Yu Zhao93a23a72009-05-18 13:51:37 +08001314 return info;
1315}
1316
1317static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1318{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001319 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001320 return;
1321
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001322 pci_enable_ats(to_pci_dev(info->dev), VTD_PAGE_SHIFT);
Yu Zhao93a23a72009-05-18 13:51:37 +08001323}
1324
1325static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1326{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001327 if (!info->dev || !dev_is_pci(info->dev) ||
1328 !pci_ats_enabled(to_pci_dev(info->dev)))
Yu Zhao93a23a72009-05-18 13:51:37 +08001329 return;
1330
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001331 pci_disable_ats(to_pci_dev(info->dev));
Yu Zhao93a23a72009-05-18 13:51:37 +08001332}
1333
1334static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1335 u64 addr, unsigned mask)
1336{
1337 u16 sid, qdep;
1338 unsigned long flags;
1339 struct device_domain_info *info;
1340
1341 spin_lock_irqsave(&device_domain_lock, flags);
1342 list_for_each_entry(info, &domain->devices, link) {
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001343 struct pci_dev *pdev;
1344 if (!info->dev || !dev_is_pci(info->dev))
1345 continue;
1346
1347 pdev = to_pci_dev(info->dev);
1348 if (!pci_ats_enabled(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001349 continue;
1350
1351 sid = info->bus << 8 | info->devfn;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001352 qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001353 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1354 }
1355 spin_unlock_irqrestore(&device_domain_lock, flags);
1356}
1357
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001358static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
David Woodhouseea8ea462014-03-05 17:09:32 +00001359 unsigned long pfn, unsigned int pages, int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001360{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001361 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001362 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001363
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001364 BUG_ON(pages == 0);
1365
David Woodhouseea8ea462014-03-05 17:09:32 +00001366 if (ih)
1367 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001368 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001369 * Fallback to domain selective flush if no PSI support or the size is
1370 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001371 * PSI requires page size to be 2 ^ x, and the base address is naturally
1372 * aligned to the size
1373 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001374 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1375 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001376 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001377 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001378 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001379 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001380
1381 /*
Nadav Amit82653632010-04-01 13:24:40 +03001382 * In caching mode, changes of pages from non-present to present require
1383 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001384 */
Nadav Amit82653632010-04-01 13:24:40 +03001385 if (!cap_caching_mode(iommu->cap) || !map)
Yu Zhao93a23a72009-05-18 13:51:37 +08001386 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001387}
1388
mark grossf8bab732008-02-08 04:18:38 -08001389static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1390{
1391 u32 pmen;
1392 unsigned long flags;
1393
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001394 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001395 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1396 pmen &= ~DMA_PMEN_EPM;
1397 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1398
1399 /* wait for the protected region status bit to clear */
1400 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1401 readl, !(pmen & DMA_PMEN_PRS), pmen);
1402
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001403 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001404}
1405
Jiang Liu2a41cce2014-07-11 14:19:33 +08001406static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001407{
1408 u32 sts;
1409 unsigned long flags;
1410
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001411 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001412 iommu->gcmd |= DMA_GCMD_TE;
1413 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001414
1415 /* Make sure hardware complete it */
1416 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001417 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001418
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001419 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001420}
1421
Jiang Liu2a41cce2014-07-11 14:19:33 +08001422static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001423{
1424 u32 sts;
1425 unsigned long flag;
1426
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001427 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001428 iommu->gcmd &= ~DMA_GCMD_TE;
1429 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1430
1431 /* Make sure hardware complete it */
1432 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001433 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001434
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001435 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436}
1437
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001438
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001439static int iommu_init_domains(struct intel_iommu *iommu)
1440{
1441 unsigned long ndomains;
1442 unsigned long nlongs;
1443
1444 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001445 pr_debug("%s: Number of Domains supported <%ld>\n",
1446 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001447 nlongs = BITS_TO_LONGS(ndomains);
1448
Donald Dutile94a91b52009-08-20 16:51:34 -04001449 spin_lock_init(&iommu->lock);
1450
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001451 /* TBD: there might be 64K domains,
1452 * consider other allocation for future chip
1453 */
1454 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1455 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001456 pr_err("%s: Allocating domain id array failed\n",
1457 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001458 return -ENOMEM;
1459 }
1460 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1461 GFP_KERNEL);
1462 if (!iommu->domains) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001463 pr_err("%s: Allocating domain array failed\n",
1464 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001465 kfree(iommu->domain_ids);
1466 iommu->domain_ids = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001467 return -ENOMEM;
1468 }
1469
1470 /*
1471 * if Caching mode is set, then invalid translations are tagged
1472 * with domainid 0. Hence we need to pre-allocate it.
1473 */
1474 if (cap_caching_mode(iommu->cap))
1475 set_bit(0, iommu->domain_ids);
1476 return 0;
1477}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001478
Jiang Liuffebeb42014-11-09 22:48:02 +08001479static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001480{
1481 struct dmar_domain *domain;
Jiang Liu2a46ddf2014-07-11 14:19:30 +08001482 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001483
Donald Dutile94a91b52009-08-20 16:51:34 -04001484 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001485 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Jiang Liua4eaa862014-02-19 14:07:30 +08001486 /*
1487 * Domain id 0 is reserved for invalid translation
1488 * if hardware supports caching mode.
1489 */
1490 if (cap_caching_mode(iommu->cap) && i == 0)
1491 continue;
1492
Donald Dutile94a91b52009-08-20 16:51:34 -04001493 domain = iommu->domains[i];
1494 clear_bit(i, iommu->domain_ids);
Jiang Liu129ad282014-07-11 14:19:31 +08001495 if (domain_detach_iommu(domain, iommu) == 0 &&
1496 !domain_type_is_vm(domain))
Jiang Liu92d03cc2014-02-19 14:07:28 +08001497 domain_exit(domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001498 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001499 }
1500
1501 if (iommu->gcmd & DMA_GCMD_TE)
1502 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001503}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001504
Jiang Liuffebeb42014-11-09 22:48:02 +08001505static void free_dmar_iommu(struct intel_iommu *iommu)
1506{
1507 if ((iommu->domains) && (iommu->domain_ids)) {
1508 kfree(iommu->domains);
1509 kfree(iommu->domain_ids);
1510 iommu->domains = NULL;
1511 iommu->domain_ids = NULL;
1512 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001513
Weidong Hand9630fe2008-12-08 11:06:32 +08001514 g_iommus[iommu->seq_id] = NULL;
1515
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001516 /* free context mapping */
1517 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001518}
1519
Jiang Liuab8dfe22014-07-11 14:19:27 +08001520static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001521{
Jiang Liu92d03cc2014-02-19 14:07:28 +08001522 /* domain id for virtual machine, it won't be set in context */
1523 static atomic_t vm_domid = ATOMIC_INIT(0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001524 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001525
1526 domain = alloc_domain_mem();
1527 if (!domain)
1528 return NULL;
1529
Jiang Liuab8dfe22014-07-11 14:19:27 +08001530 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001531 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001532 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001533 spin_lock_init(&domain->iommu_lock);
1534 INIT_LIST_HEAD(&domain->devices);
Jiang Liuab8dfe22014-07-11 14:19:27 +08001535 if (flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Jiang Liu92d03cc2014-02-19 14:07:28 +08001536 domain->id = atomic_inc_return(&vm_domid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001537
1538 return domain;
1539}
1540
Jiang Liufb170fb2014-07-11 14:19:28 +08001541static int __iommu_attach_domain(struct dmar_domain *domain,
1542 struct intel_iommu *iommu)
1543{
1544 int num;
1545 unsigned long ndomains;
1546
1547 ndomains = cap_ndoms(iommu->cap);
1548 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1549 if (num < ndomains) {
1550 set_bit(num, iommu->domain_ids);
1551 iommu->domains[num] = domain;
1552 } else {
1553 num = -ENOSPC;
1554 }
1555
1556 return num;
1557}
1558
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001559static int iommu_attach_domain(struct dmar_domain *domain,
1560 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001561{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001562 int num;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001563 unsigned long flags;
1564
Weidong Han8c11e792008-12-08 15:29:22 +08001565 spin_lock_irqsave(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001566 num = __iommu_attach_domain(domain, iommu);
Jiang Liu44bde612014-07-11 14:19:29 +08001567 spin_unlock_irqrestore(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001568 if (num < 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001569 pr_err("%s: No free domain ids\n", iommu->name);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001570
Jiang Liufb170fb2014-07-11 14:19:28 +08001571 return num;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001572}
1573
Jiang Liu44bde612014-07-11 14:19:29 +08001574static int iommu_attach_vm_domain(struct dmar_domain *domain,
1575 struct intel_iommu *iommu)
1576{
1577 int num;
1578 unsigned long ndomains;
1579
1580 ndomains = cap_ndoms(iommu->cap);
1581 for_each_set_bit(num, iommu->domain_ids, ndomains)
1582 if (iommu->domains[num] == domain)
1583 return num;
1584
1585 return __iommu_attach_domain(domain, iommu);
1586}
1587
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001588static void iommu_detach_domain(struct dmar_domain *domain,
1589 struct intel_iommu *iommu)
1590{
1591 unsigned long flags;
1592 int num, ndomains;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001593
1594 spin_lock_irqsave(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001595 if (domain_type_is_vm_or_si(domain)) {
1596 ndomains = cap_ndoms(iommu->cap);
1597 for_each_set_bit(num, iommu->domain_ids, ndomains) {
1598 if (iommu->domains[num] == domain) {
1599 clear_bit(num, iommu->domain_ids);
1600 iommu->domains[num] = NULL;
1601 break;
1602 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001603 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001604 } else {
1605 clear_bit(domain->id, iommu->domain_ids);
1606 iommu->domains[domain->id] = NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001607 }
Weidong Han8c11e792008-12-08 15:29:22 +08001608 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001609}
1610
Jiang Liufb170fb2014-07-11 14:19:28 +08001611static void domain_attach_iommu(struct dmar_domain *domain,
1612 struct intel_iommu *iommu)
1613{
1614 unsigned long flags;
1615
1616 spin_lock_irqsave(&domain->iommu_lock, flags);
1617 if (!test_and_set_bit(iommu->seq_id, domain->iommu_bmp)) {
1618 domain->iommu_count++;
1619 if (domain->iommu_count == 1)
1620 domain->nid = iommu->node;
1621 domain_update_iommu_cap(domain);
1622 }
1623 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1624}
1625
1626static int domain_detach_iommu(struct dmar_domain *domain,
1627 struct intel_iommu *iommu)
1628{
1629 unsigned long flags;
1630 int count = INT_MAX;
1631
1632 spin_lock_irqsave(&domain->iommu_lock, flags);
1633 if (test_and_clear_bit(iommu->seq_id, domain->iommu_bmp)) {
1634 count = --domain->iommu_count;
1635 domain_update_iommu_cap(domain);
1636 }
1637 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1638
1639 return count;
1640}
1641
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001642static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001643static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001644
Joseph Cihula51a63e62011-03-21 11:04:24 -07001645static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001646{
1647 struct pci_dev *pdev = NULL;
1648 struct iova *iova;
1649 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001650
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001651 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1652 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001653
Mark Gross8a443df2008-03-04 14:59:31 -08001654 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1655 &reserved_rbtree_key);
1656
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001657 /* IOAPIC ranges shouldn't be accessed by DMA */
1658 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1659 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001660 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001661 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001662 return -ENODEV;
1663 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001664
1665 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1666 for_each_pci_dev(pdev) {
1667 struct resource *r;
1668
1669 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1670 r = &pdev->resource[i];
1671 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1672 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001673 iova = reserve_iova(&reserved_iova_list,
1674 IOVA_PFN(r->start),
1675 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001676 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001677 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001678 return -ENODEV;
1679 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001680 }
1681 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001682 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001683}
1684
1685static void domain_reserve_special_ranges(struct dmar_domain *domain)
1686{
1687 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1688}
1689
1690static inline int guestwidth_to_adjustwidth(int gaw)
1691{
1692 int agaw;
1693 int r = (gaw - 12) % 9;
1694
1695 if (r == 0)
1696 agaw = gaw;
1697 else
1698 agaw = gaw + 9 - r;
1699 if (agaw > 64)
1700 agaw = 64;
1701 return agaw;
1702}
1703
1704static int domain_init(struct dmar_domain *domain, int guest_width)
1705{
1706 struct intel_iommu *iommu;
1707 int adjust_width, agaw;
1708 unsigned long sagaw;
1709
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001710 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1711 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001712 domain_reserve_special_ranges(domain);
1713
1714 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001715 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001716 if (guest_width > cap_mgaw(iommu->cap))
1717 guest_width = cap_mgaw(iommu->cap);
1718 domain->gaw = guest_width;
1719 adjust_width = guestwidth_to_adjustwidth(guest_width);
1720 agaw = width_to_agaw(adjust_width);
1721 sagaw = cap_sagaw(iommu->cap);
1722 if (!test_bit(agaw, &sagaw)) {
1723 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001724 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001725 agaw = find_next_bit(&sagaw, 5, agaw);
1726 if (agaw >= 5)
1727 return -ENODEV;
1728 }
1729 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001730
Weidong Han8e6040972008-12-08 15:49:06 +08001731 if (ecap_coherent(iommu->ecap))
1732 domain->iommu_coherency = 1;
1733 else
1734 domain->iommu_coherency = 0;
1735
Sheng Yang58c610b2009-03-18 15:33:05 +08001736 if (ecap_sc_support(iommu->ecap))
1737 domain->iommu_snooping = 1;
1738 else
1739 domain->iommu_snooping = 0;
1740
David Woodhouse214e39a2014-03-19 10:38:49 +00001741 if (intel_iommu_superpage)
1742 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1743 else
1744 domain->iommu_superpage = 0;
1745
Suresh Siddha4c923d42009-10-02 11:01:24 -07001746 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001747
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001748 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001749 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001750 if (!domain->pgd)
1751 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001752 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001753 return 0;
1754}
1755
1756static void domain_exit(struct dmar_domain *domain)
1757{
David Woodhouseea8ea462014-03-05 17:09:32 +00001758 struct page *freelist = NULL;
Alex Williamson71684402015-03-04 11:30:10 -07001759 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001760
1761 /* Domain 0 is reserved, so dont process it */
1762 if (!domain)
1763 return;
1764
Alex Williamson7b668352011-05-24 12:02:41 +01001765 /* Flush any lazy unmaps that may reference this domain */
1766 if (!intel_iommu_strict)
1767 flush_unmaps_timeout(0);
1768
Jiang Liu92d03cc2014-02-19 14:07:28 +08001769 /* remove associated devices */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001770 domain_remove_dev_info(domain);
Jiang Liu92d03cc2014-02-19 14:07:28 +08001771
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001772 /* destroy iovas */
1773 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001774
David Woodhouseea8ea462014-03-05 17:09:32 +00001775 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001776
Jiang Liu92d03cc2014-02-19 14:07:28 +08001777 /* clear attached or cached domains */
Jiang Liu0e242612014-02-19 14:07:34 +08001778 rcu_read_lock();
Alex Williamson71684402015-03-04 11:30:10 -07001779 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus)
1780 iommu_detach_domain(domain, g_iommus[i]);
Jiang Liu0e242612014-02-19 14:07:34 +08001781 rcu_read_unlock();
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001782
David Woodhouseea8ea462014-03-05 17:09:32 +00001783 dma_free_pagelist(freelist);
1784
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001785 free_domain_mem(domain);
1786}
1787
David Woodhouse64ae8922014-03-09 12:52:30 -07001788static int domain_context_mapping_one(struct dmar_domain *domain,
1789 struct intel_iommu *iommu,
1790 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001791{
1792 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001793 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001794 struct dma_pte *pgd;
Weidong Hanea6606b2008-12-08 23:08:15 +08001795 int id;
1796 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001797 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001798
1799 pr_debug("Set context mapping for %02x:%02x.%d\n",
1800 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001801
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001802 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001803 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1804 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001805
David Woodhouse03ecc322015-02-13 14:35:21 +00001806 spin_lock_irqsave(&iommu->lock, flags);
1807 context = iommu_context_addr(iommu, bus, devfn, 1);
1808 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001809 if (!context)
1810 return -ENOMEM;
1811 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001812 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001813 spin_unlock_irqrestore(&iommu->lock, flags);
1814 return 0;
1815 }
1816
Weidong Hanea6606b2008-12-08 23:08:15 +08001817 id = domain->id;
1818 pgd = domain->pgd;
1819
Jiang Liuab8dfe22014-07-11 14:19:27 +08001820 if (domain_type_is_vm_or_si(domain)) {
Jiang Liu44bde612014-07-11 14:19:29 +08001821 if (domain_type_is_vm(domain)) {
1822 id = iommu_attach_vm_domain(domain, iommu);
Jiang Liufb170fb2014-07-11 14:19:28 +08001823 if (id < 0) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001824 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001825 pr_err("%s: No free domain ids\n", iommu->name);
Weidong Hanea6606b2008-12-08 23:08:15 +08001826 return -EFAULT;
1827 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001828 }
1829
1830 /* Skip top levels of page tables for
1831 * iommu which has less agaw than default.
Chris Wright1672af12009-12-02 12:06:34 -08001832 * Unnecessary for PT mode.
Weidong Hanea6606b2008-12-08 23:08:15 +08001833 */
Chris Wright1672af12009-12-02 12:06:34 -08001834 if (translation != CONTEXT_TT_PASS_THROUGH) {
1835 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1836 pgd = phys_to_virt(dma_pte_addr(pgd));
1837 if (!dma_pte_present(pgd)) {
1838 spin_unlock_irqrestore(&iommu->lock, flags);
1839 return -ENOMEM;
1840 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001841 }
1842 }
1843 }
1844
1845 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001846
Yu Zhao93a23a72009-05-18 13:51:37 +08001847 if (translation != CONTEXT_TT_PASS_THROUGH) {
David Woodhouse64ae8922014-03-09 12:52:30 -07001848 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001849 translation = info ? CONTEXT_TT_DEV_IOTLB :
1850 CONTEXT_TT_MULTI_LEVEL;
1851 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001852 /*
1853 * In pass through mode, AW must be programmed to indicate the largest
1854 * AGAW value supported by hardware. And ASR is ignored by hardware.
1855 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001856 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001857 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001858 else {
1859 context_set_address_root(context, virt_to_phys(pgd));
1860 context_set_address_width(context, iommu->agaw);
1861 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001862
1863 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001864 context_set_fault_enable(context);
1865 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001866 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001867
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001868 /*
1869 * It's a non-present to present mapping. If hardware doesn't cache
1870 * non-present entry we only need to flush the write-buffer. If the
1871 * _does_ cache non-present entries, then it does so in the special
1872 * domain #0, which we have to flush:
1873 */
1874 if (cap_caching_mode(iommu->cap)) {
1875 iommu->flush.flush_context(iommu, 0,
1876 (((u16)bus) << 8) | devfn,
1877 DMA_CCMD_MASK_NOBIT,
1878 DMA_CCMD_DEVICE_INVL);
Jiang Liu18fd7792014-07-11 14:19:26 +08001879 iommu->flush.flush_iotlb(iommu, id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001880 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001881 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001882 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001883 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001884 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001885
Jiang Liufb170fb2014-07-11 14:19:28 +08001886 domain_attach_iommu(domain, iommu);
1887
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001888 return 0;
1889}
1890
Alex Williamson579305f2014-07-03 09:51:43 -06001891struct domain_context_mapping_data {
1892 struct dmar_domain *domain;
1893 struct intel_iommu *iommu;
1894 int translation;
1895};
1896
1897static int domain_context_mapping_cb(struct pci_dev *pdev,
1898 u16 alias, void *opaque)
1899{
1900 struct domain_context_mapping_data *data = opaque;
1901
1902 return domain_context_mapping_one(data->domain, data->iommu,
1903 PCI_BUS_NUM(alias), alias & 0xff,
1904 data->translation);
1905}
1906
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001907static int
David Woodhousee1f167f2014-03-09 15:24:46 -07001908domain_context_mapping(struct dmar_domain *domain, struct device *dev,
1909 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001910{
David Woodhouse64ae8922014-03-09 12:52:30 -07001911 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07001912 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06001913 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001914
David Woodhousee1f167f2014-03-09 15:24:46 -07001915 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07001916 if (!iommu)
1917 return -ENODEV;
1918
Alex Williamson579305f2014-07-03 09:51:43 -06001919 if (!dev_is_pci(dev))
1920 return domain_context_mapping_one(domain, iommu, bus, devfn,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001921 translation);
Alex Williamson579305f2014-07-03 09:51:43 -06001922
1923 data.domain = domain;
1924 data.iommu = iommu;
1925 data.translation = translation;
1926
1927 return pci_for_each_dma_alias(to_pci_dev(dev),
1928 &domain_context_mapping_cb, &data);
1929}
1930
1931static int domain_context_mapped_cb(struct pci_dev *pdev,
1932 u16 alias, void *opaque)
1933{
1934 struct intel_iommu *iommu = opaque;
1935
1936 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001937}
1938
David Woodhousee1f167f2014-03-09 15:24:46 -07001939static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001940{
Weidong Han5331fe62008-12-08 23:00:00 +08001941 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07001942 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08001943
David Woodhousee1f167f2014-03-09 15:24:46 -07001944 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001945 if (!iommu)
1946 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001947
Alex Williamson579305f2014-07-03 09:51:43 -06001948 if (!dev_is_pci(dev))
1949 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07001950
Alex Williamson579305f2014-07-03 09:51:43 -06001951 return !pci_for_each_dma_alias(to_pci_dev(dev),
1952 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001953}
1954
Fenghua Yuf5329592009-08-04 15:09:37 -07001955/* Returns a number of VTD pages, but aligned to MM page size */
1956static inline unsigned long aligned_nrpages(unsigned long host_addr,
1957 size_t size)
1958{
1959 host_addr &= ~PAGE_MASK;
1960 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
1961}
1962
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001963/* Return largest possible superpage level for a given mapping */
1964static inline int hardware_largepage_caps(struct dmar_domain *domain,
1965 unsigned long iov_pfn,
1966 unsigned long phy_pfn,
1967 unsigned long pages)
1968{
1969 int support, level = 1;
1970 unsigned long pfnmerge;
1971
1972 support = domain->iommu_superpage;
1973
1974 /* To use a large page, the virtual *and* physical addresses
1975 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
1976 of them will mean we have to use smaller pages. So just
1977 merge them and check both at once. */
1978 pfnmerge = iov_pfn | phy_pfn;
1979
1980 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
1981 pages >>= VTD_STRIDE_SHIFT;
1982 if (!pages)
1983 break;
1984 pfnmerge >>= VTD_STRIDE_SHIFT;
1985 level++;
1986 support--;
1987 }
1988 return level;
1989}
1990
David Woodhouse9051aa02009-06-29 12:30:54 +01001991static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1992 struct scatterlist *sg, unsigned long phys_pfn,
1993 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01001994{
1995 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01001996 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08001997 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001998 unsigned int largepage_lvl = 0;
1999 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002000
Jiang Liu162d1b12014-07-11 14:19:35 +08002001 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002002
2003 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2004 return -EINVAL;
2005
2006 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2007
Jiang Liucc4f14a2014-11-26 09:42:10 +08002008 if (!sg) {
2009 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002010 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2011 }
2012
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002013 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002014 uint64_t tmp;
2015
David Woodhousee1605492009-06-29 11:17:38 +01002016 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002017 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002018 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2019 sg->dma_length = sg->length;
2020 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002021 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002022 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002023
David Woodhousee1605492009-06-29 11:17:38 +01002024 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002025 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2026
David Woodhouse5cf0a762014-03-19 16:07:49 +00002027 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002028 if (!pte)
2029 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002030 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002031 if (largepage_lvl > 1) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002032 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002033 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2034 /*
2035 * Ensure that old small page tables are
2036 * removed to make room for superpage,
2037 * if they exist.
2038 */
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002039 dma_pte_free_pagetable(domain, iov_pfn,
Jiang Liud41a4ad2014-07-11 14:19:34 +08002040 iov_pfn + lvl_pages - 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002041 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002042 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002043 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002044
David Woodhousee1605492009-06-29 11:17:38 +01002045 }
2046 /* We don't need lock here, nobody else
2047 * touches the iova range
2048 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002049 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002050 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002051 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002052 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2053 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002054 if (dumps) {
2055 dumps--;
2056 debug_dma_dump_mappings(NULL);
2057 }
2058 WARN_ON(1);
2059 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002060
2061 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2062
2063 BUG_ON(nr_pages < lvl_pages);
2064 BUG_ON(sg_res < lvl_pages);
2065
2066 nr_pages -= lvl_pages;
2067 iov_pfn += lvl_pages;
2068 phys_pfn += lvl_pages;
2069 pteval += lvl_pages * VTD_PAGE_SIZE;
2070 sg_res -= lvl_pages;
2071
2072 /* If the next PTE would be the first in a new page, then we
2073 need to flush the cache on the entries we've just written.
2074 And then we'll need to recalculate 'pte', so clear it and
2075 let it get set again in the if (!pte) block above.
2076
2077 If we're done (!nr_pages) we need to flush the cache too.
2078
2079 Also if we've been setting superpages, we may need to
2080 recalculate 'pte' and switch back to smaller pages for the
2081 end of the mapping, if the trailing size is not enough to
2082 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002083 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002084 if (!nr_pages || first_pte_in_page(pte) ||
2085 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002086 domain_flush_cache(domain, first_pte,
2087 (void *)pte - (void *)first_pte);
2088 pte = NULL;
2089 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002090
2091 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002092 sg = sg_next(sg);
2093 }
2094 return 0;
2095}
2096
David Woodhouse9051aa02009-06-29 12:30:54 +01002097static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2098 struct scatterlist *sg, unsigned long nr_pages,
2099 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002100{
David Woodhouse9051aa02009-06-29 12:30:54 +01002101 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2102}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002103
David Woodhouse9051aa02009-06-29 12:30:54 +01002104static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2105 unsigned long phys_pfn, unsigned long nr_pages,
2106 int prot)
2107{
2108 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002109}
2110
Weidong Hanc7151a82008-12-08 22:51:37 +08002111static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002112{
Weidong Hanc7151a82008-12-08 22:51:37 +08002113 if (!iommu)
2114 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002115
2116 clear_context_table(iommu, bus, devfn);
2117 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002118 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002119 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002120}
2121
David Woodhouse109b9b02012-05-25 17:43:02 +01002122static inline void unlink_domain_info(struct device_domain_info *info)
2123{
2124 assert_spin_locked(&device_domain_lock);
2125 list_del(&info->link);
2126 list_del(&info->global);
2127 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002128 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002129}
2130
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002131static void domain_remove_dev_info(struct dmar_domain *domain)
2132{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002133 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002134 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002135
2136 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wang3a74ca02014-05-20 20:37:47 +08002137 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhouse109b9b02012-05-25 17:43:02 +01002138 unlink_domain_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002139 spin_unlock_irqrestore(&device_domain_lock, flags);
2140
Yu Zhao93a23a72009-05-18 13:51:37 +08002141 iommu_disable_dev_iotlb(info);
David Woodhouse7c7faa12014-03-09 13:33:06 -07002142 iommu_detach_dev(info->iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002143
Jiang Liuab8dfe22014-07-11 14:19:27 +08002144 if (domain_type_is_vm(domain)) {
David Woodhouse7c7faa12014-03-09 13:33:06 -07002145 iommu_detach_dependent_devices(info->iommu, info->dev);
Jiang Liufb170fb2014-07-11 14:19:28 +08002146 domain_detach_iommu(domain, info->iommu);
Jiang Liu92d03cc2014-02-19 14:07:28 +08002147 }
2148
2149 free_devinfo_mem(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002150 spin_lock_irqsave(&device_domain_lock, flags);
2151 }
2152 spin_unlock_irqrestore(&device_domain_lock, flags);
2153}
2154
2155/*
2156 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002157 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002158 */
David Woodhouse1525a292014-03-06 16:19:30 +00002159static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002160{
2161 struct device_domain_info *info;
2162
2163 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002164 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002165 if (info)
2166 return info->domain;
2167 return NULL;
2168}
2169
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002170static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002171dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2172{
2173 struct device_domain_info *info;
2174
2175 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002176 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002177 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002178 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002179
2180 return NULL;
2181}
2182
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002183static struct dmar_domain *dmar_insert_dev_info(struct intel_iommu *iommu,
David Woodhouse41e80dca2014-03-09 13:55:54 -07002184 int bus, int devfn,
David Woodhouseb718cd32014-03-09 13:11:33 -07002185 struct device *dev,
2186 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002187{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002188 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002189 struct device_domain_info *info;
2190 unsigned long flags;
2191
2192 info = alloc_devinfo_mem();
2193 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002194 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002195
Jiang Liu745f2582014-02-19 14:07:26 +08002196 info->bus = bus;
2197 info->devfn = devfn;
2198 info->dev = dev;
2199 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002200 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002201
2202 spin_lock_irqsave(&device_domain_lock, flags);
2203 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002204 found = find_domain(dev);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002205 else {
2206 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002207 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002208 if (info2)
2209 found = info2->domain;
2210 }
Jiang Liu745f2582014-02-19 14:07:26 +08002211 if (found) {
2212 spin_unlock_irqrestore(&device_domain_lock, flags);
2213 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002214 /* Caller must free the original domain */
2215 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002216 }
2217
David Woodhouseb718cd32014-03-09 13:11:33 -07002218 list_add(&info->link, &domain->devices);
2219 list_add(&info->global, &device_domain_list);
2220 if (dev)
2221 dev->archdata.iommu = info;
2222 spin_unlock_irqrestore(&device_domain_lock, flags);
2223
2224 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002225}
2226
Alex Williamson579305f2014-07-03 09:51:43 -06002227static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2228{
2229 *(u16 *)opaque = alias;
2230 return 0;
2231}
2232
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002233/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002234static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002235{
Alex Williamson579305f2014-07-03 09:51:43 -06002236 struct dmar_domain *domain, *tmp;
2237 struct intel_iommu *iommu;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002238 struct device_domain_info *info;
Alex Williamson579305f2014-07-03 09:51:43 -06002239 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002240 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002241 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002242
David Woodhouse146922e2014-03-09 15:44:17 -07002243 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002244 if (domain)
2245 return domain;
2246
David Woodhouse146922e2014-03-09 15:44:17 -07002247 iommu = device_to_iommu(dev, &bus, &devfn);
2248 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002249 return NULL;
2250
2251 if (dev_is_pci(dev)) {
2252 struct pci_dev *pdev = to_pci_dev(dev);
2253
2254 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2255
2256 spin_lock_irqsave(&device_domain_lock, flags);
2257 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2258 PCI_BUS_NUM(dma_alias),
2259 dma_alias & 0xff);
2260 if (info) {
2261 iommu = info->iommu;
2262 domain = info->domain;
2263 }
2264 spin_unlock_irqrestore(&device_domain_lock, flags);
2265
2266 /* DMA alias already has a domain, uses it */
2267 if (info)
2268 goto found_domain;
2269 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002270
David Woodhouse146922e2014-03-09 15:44:17 -07002271 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002272 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002273 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002274 return NULL;
Jiang Liu44bde612014-07-11 14:19:29 +08002275 domain->id = iommu_attach_domain(domain, iommu);
2276 if (domain->id < 0) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07002277 free_domain_mem(domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002278 return NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002279 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002280 domain_attach_iommu(domain, iommu);
Alex Williamson579305f2014-07-03 09:51:43 -06002281 if (domain_init(domain, gaw)) {
2282 domain_exit(domain);
2283 return NULL;
2284 }
2285
2286 /* register PCI DMA alias device */
2287 if (dev_is_pci(dev)) {
2288 tmp = dmar_insert_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2289 dma_alias & 0xff, NULL, domain);
2290
2291 if (!tmp || tmp != domain) {
2292 domain_exit(domain);
2293 domain = tmp;
2294 }
2295
David Woodhouseb718cd32014-03-09 13:11:33 -07002296 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002297 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002298 }
2299
2300found_domain:
Alex Williamson579305f2014-07-03 09:51:43 -06002301 tmp = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
2302
2303 if (!tmp || tmp != domain) {
2304 domain_exit(domain);
2305 domain = tmp;
2306 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002307
2308 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002309}
2310
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002311static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002312#define IDENTMAP_ALL 1
2313#define IDENTMAP_GFX 2
2314#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002315
David Woodhouseb2132032009-06-26 18:50:28 +01002316static int iommu_domain_identity_map(struct dmar_domain *domain,
2317 unsigned long long start,
2318 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002319{
David Woodhousec5395d52009-06-28 16:35:56 +01002320 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2321 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002322
David Woodhousec5395d52009-06-28 16:35:56 +01002323 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2324 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002325 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002326 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002327 }
2328
David Woodhousec5395d52009-06-28 16:35:56 +01002329 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
2330 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002331 /*
2332 * RMRR range might have overlap with physical memory range,
2333 * clear it first
2334 */
David Woodhousec5395d52009-06-28 16:35:56 +01002335 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002336
David Woodhousec5395d52009-06-28 16:35:56 +01002337 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2338 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002339 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002340}
2341
David Woodhouse0b9d9752014-03-09 15:48:15 -07002342static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002343 unsigned long long start,
2344 unsigned long long end)
2345{
2346 struct dmar_domain *domain;
2347 int ret;
2348
David Woodhouse0b9d9752014-03-09 15:48:15 -07002349 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002350 if (!domain)
2351 return -ENOMEM;
2352
David Woodhouse19943b02009-08-04 16:19:20 +01002353 /* For _hardware_ passthrough, don't bother. But for software
2354 passthrough, we do it anyway -- it may indicate a memory
2355 range which is reserved in E820, so which didn't get set
2356 up to start with in si_domain */
2357 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002358 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2359 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002360 return 0;
2361 }
2362
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002363 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2364 dev_name(dev), start, end);
2365
David Woodhouse5595b522009-12-02 09:21:55 +00002366 if (end < start) {
2367 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2368 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2369 dmi_get_system_info(DMI_BIOS_VENDOR),
2370 dmi_get_system_info(DMI_BIOS_VERSION),
2371 dmi_get_system_info(DMI_PRODUCT_VERSION));
2372 ret = -EIO;
2373 goto error;
2374 }
2375
David Woodhouse2ff729f2009-08-26 14:25:41 +01002376 if (end >> agaw_to_width(domain->agaw)) {
2377 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2378 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2379 agaw_to_width(domain->agaw),
2380 dmi_get_system_info(DMI_BIOS_VENDOR),
2381 dmi_get_system_info(DMI_BIOS_VERSION),
2382 dmi_get_system_info(DMI_PRODUCT_VERSION));
2383 ret = -EIO;
2384 goto error;
2385 }
David Woodhouse19943b02009-08-04 16:19:20 +01002386
David Woodhouseb2132032009-06-26 18:50:28 +01002387 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002388 if (ret)
2389 goto error;
2390
2391 /* context entry init */
David Woodhouse0b9d9752014-03-09 15:48:15 -07002392 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01002393 if (ret)
2394 goto error;
2395
2396 return 0;
2397
2398 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002399 domain_exit(domain);
2400 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002401}
2402
2403static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002404 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002405{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002406 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002407 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002408 return iommu_prepare_identity_map(dev, rmrr->base_address,
2409 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002410}
2411
Suresh Siddhad3f13812011-08-23 17:05:25 -07002412#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002413static inline void iommu_prepare_isa(void)
2414{
2415 struct pci_dev *pdev;
2416 int ret;
2417
2418 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2419 if (!pdev)
2420 return;
2421
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002422 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002423 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002424
2425 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002426 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002427
Yijing Wang9b27e822014-05-20 20:37:52 +08002428 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002429}
2430#else
2431static inline void iommu_prepare_isa(void)
2432{
2433 return;
2434}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002435#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002436
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002437static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002438
Matt Kraai071e1372009-08-23 22:30:22 -07002439static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002440{
2441 struct dmar_drhd_unit *drhd;
2442 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002443 int nid, ret = 0;
Jiang Liu44bde612014-07-11 14:19:29 +08002444 bool first = true;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002445
Jiang Liuab8dfe22014-07-11 14:19:27 +08002446 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002447 if (!si_domain)
2448 return -EFAULT;
2449
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002450 for_each_active_iommu(iommu, drhd) {
2451 ret = iommu_attach_domain(si_domain, iommu);
Jiang Liufb170fb2014-07-11 14:19:28 +08002452 if (ret < 0) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002453 domain_exit(si_domain);
2454 return -EFAULT;
Jiang Liu44bde612014-07-11 14:19:29 +08002455 } else if (first) {
2456 si_domain->id = ret;
2457 first = false;
2458 } else if (si_domain->id != ret) {
2459 domain_exit(si_domain);
2460 return -EFAULT;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002461 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002462 domain_attach_iommu(si_domain, iommu);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002463 }
2464
2465 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2466 domain_exit(si_domain);
2467 return -EFAULT;
2468 }
2469
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002470 pr_debug("Identity mapping domain is domain %d\n",
Jiang Liu9544c002014-01-06 14:18:13 +08002471 si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002472
David Woodhouse19943b02009-08-04 16:19:20 +01002473 if (hw)
2474 return 0;
2475
David Woodhousec7ab48d2009-06-26 19:10:36 +01002476 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002477 unsigned long start_pfn, end_pfn;
2478 int i;
2479
2480 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2481 ret = iommu_domain_identity_map(si_domain,
2482 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2483 if (ret)
2484 return ret;
2485 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002486 }
2487
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002488 return 0;
2489}
2490
David Woodhouse9b226622014-03-09 14:03:28 -07002491static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002492{
2493 struct device_domain_info *info;
2494
2495 if (likely(!iommu_identity_mapping))
2496 return 0;
2497
David Woodhouse9b226622014-03-09 14:03:28 -07002498 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002499 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2500 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002501
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002502 return 0;
2503}
2504
2505static int domain_add_dev_info(struct dmar_domain *domain,
David Woodhouse5913c9b2014-03-09 16:27:31 -07002506 struct device *dev, int translation)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002507{
David Woodhouse0ac72662014-03-09 13:19:22 -07002508 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002509 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002510 u8 bus, devfn;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002511 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002512
David Woodhouse5913c9b2014-03-09 16:27:31 -07002513 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002514 if (!iommu)
2515 return -ENODEV;
2516
David Woodhouse5913c9b2014-03-09 16:27:31 -07002517 ndomain = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002518 if (ndomain != domain)
2519 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002520
David Woodhouse5913c9b2014-03-09 16:27:31 -07002521 ret = domain_context_mapping(domain, dev, translation);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002522 if (ret) {
David Woodhouse5913c9b2014-03-09 16:27:31 -07002523 domain_remove_one_dev_info(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002524 return ret;
2525 }
2526
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002527 return 0;
2528}
2529
David Woodhouse0b9d9752014-03-09 15:48:15 -07002530static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002531{
2532 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002533 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002534 int i;
2535
Jiang Liu0e242612014-02-19 14:07:34 +08002536 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002537 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002538 /*
2539 * Return TRUE if this RMRR contains the device that
2540 * is passed in.
2541 */
2542 for_each_active_dev_scope(rmrr->devices,
2543 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002544 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002545 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002546 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002547 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002548 }
Jiang Liu0e242612014-02-19 14:07:34 +08002549 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002550 return false;
2551}
2552
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002553/*
2554 * There are a couple cases where we need to restrict the functionality of
2555 * devices associated with RMRRs. The first is when evaluating a device for
2556 * identity mapping because problems exist when devices are moved in and out
2557 * of domains and their respective RMRR information is lost. This means that
2558 * a device with associated RMRRs will never be in a "passthrough" domain.
2559 * The second is use of the device through the IOMMU API. This interface
2560 * expects to have full control of the IOVA space for the device. We cannot
2561 * satisfy both the requirement that RMRR access is maintained and have an
2562 * unencumbered IOVA space. We also have no ability to quiesce the device's
2563 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2564 * We therefore prevent devices associated with an RMRR from participating in
2565 * the IOMMU API, which eliminates them from device assignment.
2566 *
2567 * In both cases we assume that PCI USB devices with RMRRs have them largely
2568 * for historical reasons and that the RMRR space is not actively used post
2569 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002570 *
2571 * The same exception is made for graphics devices, with the requirement that
2572 * any use of the RMRR regions will be torn down before assigning the device
2573 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002574 */
2575static bool device_is_rmrr_locked(struct device *dev)
2576{
2577 if (!device_has_rmrr(dev))
2578 return false;
2579
2580 if (dev_is_pci(dev)) {
2581 struct pci_dev *pdev = to_pci_dev(dev);
2582
David Woodhouse18436af2015-03-25 15:05:47 +00002583 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002584 return false;
2585 }
2586
2587 return true;
2588}
2589
David Woodhouse3bdb2592014-03-09 16:03:08 -07002590static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002591{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002592
David Woodhouse3bdb2592014-03-09 16:03:08 -07002593 if (dev_is_pci(dev)) {
2594 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002595
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002596 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002597 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002598
David Woodhouse3bdb2592014-03-09 16:03:08 -07002599 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2600 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002601
David Woodhouse3bdb2592014-03-09 16:03:08 -07002602 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2603 return 1;
2604
2605 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2606 return 0;
2607
2608 /*
2609 * We want to start off with all devices in the 1:1 domain, and
2610 * take them out later if we find they can't access all of memory.
2611 *
2612 * However, we can't do this for PCI devices behind bridges,
2613 * because all PCI devices behind the same bridge will end up
2614 * with the same source-id on their transactions.
2615 *
2616 * Practically speaking, we can't change things around for these
2617 * devices at run-time, because we can't be sure there'll be no
2618 * DMA transactions in flight for any of their siblings.
2619 *
2620 * So PCI devices (unless they're on the root bus) as well as
2621 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2622 * the 1:1 domain, just in _case_ one of their siblings turns out
2623 * not to be able to map all of memory.
2624 */
2625 if (!pci_is_pcie(pdev)) {
2626 if (!pci_is_root_bus(pdev->bus))
2627 return 0;
2628 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2629 return 0;
2630 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2631 return 0;
2632 } else {
2633 if (device_has_rmrr(dev))
2634 return 0;
2635 }
David Woodhouse6941af22009-07-04 18:24:27 +01002636
David Woodhouse3dfc8132009-07-04 19:11:08 +01002637 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002638 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002639 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002640 * take them out of the 1:1 domain later.
2641 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002642 if (!startup) {
2643 /*
2644 * If the device's dma_mask is less than the system's memory
2645 * size then this is not a candidate for identity mapping.
2646 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002647 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002648
David Woodhouse3bdb2592014-03-09 16:03:08 -07002649 if (dev->coherent_dma_mask &&
2650 dev->coherent_dma_mask < dma_mask)
2651 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002652
David Woodhouse3bdb2592014-03-09 16:03:08 -07002653 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002654 }
David Woodhouse6941af22009-07-04 18:24:27 +01002655
2656 return 1;
2657}
2658
David Woodhousecf04eee2014-03-21 16:49:04 +00002659static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2660{
2661 int ret;
2662
2663 if (!iommu_should_identity_map(dev, 1))
2664 return 0;
2665
2666 ret = domain_add_dev_info(si_domain, dev,
2667 hw ? CONTEXT_TT_PASS_THROUGH :
2668 CONTEXT_TT_MULTI_LEVEL);
2669 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002670 pr_info("%s identity mapping for device %s\n",
2671 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002672 else if (ret == -ENODEV)
2673 /* device not associated with an iommu */
2674 ret = 0;
2675
2676 return ret;
2677}
2678
2679
Matt Kraai071e1372009-08-23 22:30:22 -07002680static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002681{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002682 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002683 struct dmar_drhd_unit *drhd;
2684 struct intel_iommu *iommu;
2685 struct device *dev;
2686 int i;
2687 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002688
David Woodhouse19943b02009-08-04 16:19:20 +01002689 ret = si_domain_init(hw);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002690 if (ret)
2691 return -EFAULT;
2692
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002693 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002694 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2695 if (ret)
2696 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002697 }
2698
David Woodhousecf04eee2014-03-21 16:49:04 +00002699 for_each_active_iommu(iommu, drhd)
2700 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2701 struct acpi_device_physical_node *pn;
2702 struct acpi_device *adev;
2703
2704 if (dev->bus != &acpi_bus_type)
2705 continue;
2706
2707 adev= to_acpi_device(dev);
2708 mutex_lock(&adev->physical_node_lock);
2709 list_for_each_entry(pn, &adev->physical_node_list, node) {
2710 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2711 if (ret)
2712 break;
2713 }
2714 mutex_unlock(&adev->physical_node_lock);
2715 if (ret)
2716 return ret;
2717 }
2718
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002719 return 0;
2720}
2721
Jiang Liuffebeb42014-11-09 22:48:02 +08002722static void intel_iommu_init_qi(struct intel_iommu *iommu)
2723{
2724 /*
2725 * Start from the sane iommu hardware state.
2726 * If the queued invalidation is already initialized by us
2727 * (for example, while enabling interrupt-remapping) then
2728 * we got the things already rolling from a sane state.
2729 */
2730 if (!iommu->qi) {
2731 /*
2732 * Clear any previous faults.
2733 */
2734 dmar_fault(-1, iommu);
2735 /*
2736 * Disable queued invalidation if supported and already enabled
2737 * before OS handover.
2738 */
2739 dmar_disable_qi(iommu);
2740 }
2741
2742 if (dmar_enable_qi(iommu)) {
2743 /*
2744 * Queued Invalidate not enabled, use Register Based Invalidate
2745 */
2746 iommu->flush.flush_context = __iommu_flush_context;
2747 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002748 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002749 iommu->name);
2750 } else {
2751 iommu->flush.flush_context = qi_flush_context;
2752 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002753 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002754 }
2755}
2756
Joseph Cihulab7792602011-05-03 00:08:37 -07002757static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002758{
2759 struct dmar_drhd_unit *drhd;
2760 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002761 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002762 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002763 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002764
2765 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002766 * for each drhd
2767 * allocate root
2768 * initialize and program root entry to not present
2769 * endfor
2770 */
2771 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002772 /*
2773 * lock not needed as this is only incremented in the single
2774 * threaded kernel __init code path all other access are read
2775 * only
2776 */
Jiang Liu78d8e702014-11-09 22:47:57 +08002777 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08002778 g_num_of_iommus++;
2779 continue;
2780 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002781 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08002782 }
2783
Jiang Liuffebeb42014-11-09 22:48:02 +08002784 /* Preallocate enough resources for IOMMU hot-addition */
2785 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
2786 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
2787
Weidong Hand9630fe2008-12-08 11:06:32 +08002788 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2789 GFP_KERNEL);
2790 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002791 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08002792 ret = -ENOMEM;
2793 goto error;
2794 }
2795
mark gross80b20dd2008-04-18 13:53:58 -07002796 deferred_flush = kzalloc(g_num_of_iommus *
2797 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2798 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08002799 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08002800 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08002801 }
2802
Jiang Liu7c919772014-01-06 14:18:18 +08002803 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08002804 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002805
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002806 ret = iommu_init_domains(iommu);
2807 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002808 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002809
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002810 /*
2811 * TBD:
2812 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002813 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002814 */
2815 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08002816 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002817 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002818 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01002819 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002820 }
2821
Jiang Liuffebeb42014-11-09 22:48:02 +08002822 for_each_active_iommu(iommu, drhd)
2823 intel_iommu_init_qi(iommu);
Youquan Songa77b67d2008-10-16 16:31:56 -07002824
David Woodhouse19943b02009-08-04 16:19:20 +01002825 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07002826 iommu_identity_mapping |= IDENTMAP_ALL;
2827
Suresh Siddhad3f13812011-08-23 17:05:25 -07002828#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07002829 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01002830#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07002831
2832 check_tylersburg_isoch();
2833
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002834 /*
2835 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002836 * identity mappings for rmrr, gfx, and isa and may fall back to static
2837 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002838 */
David Woodhouse19943b02009-08-04 16:19:20 +01002839 if (iommu_identity_mapping) {
2840 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
2841 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002842 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08002843 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002844 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002845 }
David Woodhouse19943b02009-08-04 16:19:20 +01002846 /*
2847 * For each rmrr
2848 * for each dev attached to rmrr
2849 * do
2850 * locate drhd for dev, alloc domain for dev
2851 * allocate free domain
2852 * allocate page table entries for rmrr
2853 * if context not allocated for bus
2854 * allocate and init context
2855 * set present in root table for this bus
2856 * init context with domain, translation etc
2857 * endfor
2858 * endfor
2859 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002860 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01002861 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002862 /* some BIOS lists non-exist devices in DMAR table. */
2863 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00002864 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07002865 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01002866 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002867 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01002868 }
2869 }
2870
2871 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002872
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002873 /*
2874 * for each drhd
2875 * enable fault log
2876 * global invalidate context cache
2877 * global invalidate iotlb
2878 * enable translation
2879 */
Jiang Liu7c919772014-01-06 14:18:18 +08002880 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07002881 if (drhd->ignored) {
2882 /*
2883 * we always have to disable PMRs or DMA may fail on
2884 * this device
2885 */
2886 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08002887 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002888 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07002889 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002890
2891 iommu_flush_write_buffer(iommu);
2892
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002893 ret = dmar_set_interrupt(iommu);
2894 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002895 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002896
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002897 iommu_set_root_entry(iommu);
2898
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002899 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002900 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Jiang Liu2a41cce2014-07-11 14:19:33 +08002901 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07002902 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002903 }
2904
2905 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08002906
2907free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08002908 for_each_active_iommu(iommu, drhd) {
2909 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08002910 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08002911 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08002912 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08002913free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08002914 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08002915error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002916 return ret;
2917}
2918
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002919/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01002920static struct iova *intel_alloc_iova(struct device *dev,
2921 struct dmar_domain *domain,
2922 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002923{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002924 struct iova *iova = NULL;
2925
David Woodhouse875764d2009-06-28 21:20:51 +01002926 /* Restrict dma_mask to the width that the iommu can handle */
2927 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
2928
2929 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002930 /*
2931 * First try to allocate an io virtual address in
Yang Hongyang284901a92009-04-06 19:01:15 -07002932 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002933 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002934 */
David Woodhouse875764d2009-06-28 21:20:51 +01002935 iova = alloc_iova(&domain->iovad, nrpages,
2936 IOVA_PFN(DMA_BIT_MASK(32)), 1);
2937 if (iova)
2938 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002939 }
David Woodhouse875764d2009-06-28 21:20:51 +01002940 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
2941 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002942 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07002943 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002944 return NULL;
2945 }
2946
2947 return iova;
2948}
2949
David Woodhoused4b709f2014-03-09 16:07:40 -07002950static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002951{
2952 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002953 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002954
David Woodhoused4b709f2014-03-09 16:07:40 -07002955 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002956 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002957 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07002958 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002959 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002960 }
2961
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002962 /* make sure context mapping is ok */
David Woodhoused4b709f2014-03-09 16:07:40 -07002963 if (unlikely(!domain_context_mapped(dev))) {
2964 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002965 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002966 pr_err("Domain context map for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07002967 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002968 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002969 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002970 }
2971
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002972 return domain;
2973}
2974
David Woodhoused4b709f2014-03-09 16:07:40 -07002975static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01002976{
2977 struct device_domain_info *info;
2978
2979 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07002980 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01002981 if (likely(info))
2982 return info->domain;
2983
2984 return __get_valid_domain_for_dev(dev);
2985}
2986
David Woodhouseecb509e2014-03-09 16:29:55 -07002987/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01002988static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002989{
2990 int found;
2991
David Woodhouse3d891942014-03-06 15:59:26 +00002992 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002993 return 1;
2994
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002995 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002996 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002997
David Woodhouse9b226622014-03-09 14:03:28 -07002998 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002999 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003000 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003001 return 1;
3002 else {
3003 /*
3004 * 32 bit DMA is removed from si_domain and fall back
3005 * to non-identity mapping.
3006 */
David Woodhousebf9c9ed2014-03-09 16:19:13 -07003007 domain_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003008 pr_info("32bit %s uses non-identity mapping\n",
3009 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003010 return 0;
3011 }
3012 } else {
3013 /*
3014 * In case of a detached 64 bit DMA device from vm, the device
3015 * is put into si_domain for identity mapping.
3016 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003017 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003018 int ret;
David Woodhouse5913c9b2014-03-09 16:27:31 -07003019 ret = domain_add_dev_info(si_domain, dev,
David Woodhouse5fe60f42009-08-09 10:53:41 +01003020 hw_pass_through ?
3021 CONTEXT_TT_PASS_THROUGH :
3022 CONTEXT_TT_MULTI_LEVEL);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003023 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003024 pr_info("64bit %s uses identity mapping\n",
3025 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003026 return 1;
3027 }
3028 }
3029 }
3030
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003031 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003032}
3033
David Woodhouse5040a912014-03-09 16:14:00 -07003034static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003035 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003036{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003037 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003038 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003039 struct iova *iova;
3040 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003041 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003042 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003043 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003044
3045 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003046
David Woodhouse5040a912014-03-09 16:14:00 -07003047 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003048 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003049
David Woodhouse5040a912014-03-09 16:14:00 -07003050 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003051 if (!domain)
3052 return 0;
3053
Weidong Han8c11e792008-12-08 15:29:22 +08003054 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003055 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003056
David Woodhouse5040a912014-03-09 16:14:00 -07003057 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003058 if (!iova)
3059 goto error;
3060
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003061 /*
3062 * Check if DMAR supports zero-length reads on write only
3063 * mappings..
3064 */
3065 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003066 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003067 prot |= DMA_PTE_READ;
3068 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3069 prot |= DMA_PTE_WRITE;
3070 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003071 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003072 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003073 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003074 * is not a big problem
3075 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003076 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003077 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003078 if (ret)
3079 goto error;
3080
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003081 /* it's a non-present to present mapping. Only flush if caching mode */
3082 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003083 iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003084 else
Weidong Han8c11e792008-12-08 15:29:22 +08003085 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003086
David Woodhouse03d6a242009-06-28 15:33:46 +01003087 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3088 start_paddr += paddr & ~PAGE_MASK;
3089 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003090
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003091error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003092 if (iova)
3093 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003094 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003095 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003096 return 0;
3097}
3098
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003099static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3100 unsigned long offset, size_t size,
3101 enum dma_data_direction dir,
3102 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003103{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003104 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003105 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003106}
3107
mark gross5e0d2a62008-03-04 15:22:08 -08003108static void flush_unmaps(void)
3109{
mark gross80b20dd2008-04-18 13:53:58 -07003110 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003111
mark gross5e0d2a62008-03-04 15:22:08 -08003112 timer_on = 0;
3113
3114 /* just flush them all */
3115 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003116 struct intel_iommu *iommu = g_iommus[i];
3117 if (!iommu)
3118 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003119
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003120 if (!deferred_flush[i].next)
3121 continue;
3122
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003123 /* In caching mode, global flushes turn emulation expensive */
3124 if (!cap_caching_mode(iommu->cap))
3125 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003126 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003127 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003128 unsigned long mask;
3129 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003130 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003131
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003132 /* On real hardware multiple invalidations are expensive */
3133 if (cap_caching_mode(iommu->cap))
3134 iommu_flush_iotlb_psi(iommu, domain->id,
Jiang Liua156ef92014-07-11 14:19:36 +08003135 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003136 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003137 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003138 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003139 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3140 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3141 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003142 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003143 if (deferred_flush[i].freelist[j])
3144 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003145 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003146 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003147 }
3148
mark gross5e0d2a62008-03-04 15:22:08 -08003149 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003150}
3151
3152static void flush_unmaps_timeout(unsigned long data)
3153{
mark gross80b20dd2008-04-18 13:53:58 -07003154 unsigned long flags;
3155
3156 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003157 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003158 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003159}
3160
David Woodhouseea8ea462014-03-05 17:09:32 +00003161static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003162{
3163 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003164 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003165 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003166
3167 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003168 if (list_size == HIGH_WATER_MARK)
3169 flush_unmaps();
3170
Weidong Han8c11e792008-12-08 15:29:22 +08003171 iommu = domain_get_iommu(dom);
3172 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003173
mark gross80b20dd2008-04-18 13:53:58 -07003174 next = deferred_flush[iommu_id].next;
3175 deferred_flush[iommu_id].domain[next] = dom;
3176 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003177 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003178 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003179
3180 if (!timer_on) {
3181 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3182 timer_on = 1;
3183 }
3184 list_size++;
3185 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3186}
3187
Jiang Liud41a4ad2014-07-11 14:19:34 +08003188static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003189{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003190 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003191 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003192 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003193 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003194 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003195
David Woodhouse73676832009-07-04 14:08:36 +01003196 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003197 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003198
David Woodhouse1525a292014-03-06 16:19:30 +00003199 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003200 BUG_ON(!domain);
3201
Weidong Han8c11e792008-12-08 15:29:22 +08003202 iommu = domain_get_iommu(domain);
3203
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003204 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003205 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3206 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003207 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003208
David Woodhoused794dc92009-06-28 00:27:49 +01003209 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3210 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003211
David Woodhoused794dc92009-06-28 00:27:49 +01003212 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003213 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003214
David Woodhouseea8ea462014-03-05 17:09:32 +00003215 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003216
mark gross5e0d2a62008-03-04 15:22:08 -08003217 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01003218 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003219 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003220 /* free iova */
3221 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003222 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003223 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003224 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003225 /*
3226 * queue up the release of the unmap to save the 1/6th of the
3227 * cpu used up by the iotlb flush operation...
3228 */
mark gross5e0d2a62008-03-04 15:22:08 -08003229 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003230}
3231
Jiang Liud41a4ad2014-07-11 14:19:34 +08003232static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3233 size_t size, enum dma_data_direction dir,
3234 struct dma_attrs *attrs)
3235{
3236 intel_unmap(dev, dev_addr);
3237}
3238
David Woodhouse5040a912014-03-09 16:14:00 -07003239static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003240 dma_addr_t *dma_handle, gfp_t flags,
3241 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003242{
Akinobu Mita36746432014-06-04 16:06:51 -07003243 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003244 int order;
3245
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003246 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003247 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003248
David Woodhouse5040a912014-03-09 16:14:00 -07003249 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003250 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003251 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3252 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003253 flags |= GFP_DMA;
3254 else
3255 flags |= GFP_DMA32;
3256 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003257
Akinobu Mita36746432014-06-04 16:06:51 -07003258 if (flags & __GFP_WAIT) {
3259 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003260
Akinobu Mita36746432014-06-04 16:06:51 -07003261 page = dma_alloc_from_contiguous(dev, count, order);
3262 if (page && iommu_no_mapping(dev) &&
3263 page_to_phys(page) + size > dev->coherent_dma_mask) {
3264 dma_release_from_contiguous(dev, page, count);
3265 page = NULL;
3266 }
3267 }
3268
3269 if (!page)
3270 page = alloc_pages(flags, order);
3271 if (!page)
3272 return NULL;
3273 memset(page_address(page), 0, size);
3274
3275 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003276 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003277 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003278 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003279 return page_address(page);
3280 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3281 __free_pages(page, order);
3282
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003283 return NULL;
3284}
3285
David Woodhouse5040a912014-03-09 16:14:00 -07003286static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003287 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003288{
3289 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003290 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003291
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003292 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003293 order = get_order(size);
3294
Jiang Liud41a4ad2014-07-11 14:19:34 +08003295 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003296 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3297 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003298}
3299
David Woodhouse5040a912014-03-09 16:14:00 -07003300static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003301 int nelems, enum dma_data_direction dir,
3302 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003303{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003304 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003305}
3306
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003307static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003308 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003309{
3310 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003311 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003312
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003313 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003314 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003315 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003316 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003317 }
3318 return nelems;
3319}
3320
David Woodhouse5040a912014-03-09 16:14:00 -07003321static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003322 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003323{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003324 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003325 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003326 size_t size = 0;
3327 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003328 struct iova *iova = NULL;
3329 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003330 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003331 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003332 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003333
3334 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003335 if (iommu_no_mapping(dev))
3336 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003337
David Woodhouse5040a912014-03-09 16:14:00 -07003338 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003339 if (!domain)
3340 return 0;
3341
Weidong Han8c11e792008-12-08 15:29:22 +08003342 iommu = domain_get_iommu(domain);
3343
David Woodhouseb536d242009-06-28 14:49:31 +01003344 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003345 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003346
David Woodhouse5040a912014-03-09 16:14:00 -07003347 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3348 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003349 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003350 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003351 return 0;
3352 }
3353
3354 /*
3355 * Check if DMAR supports zero-length reads on write only
3356 * mappings..
3357 */
3358 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003359 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003360 prot |= DMA_PTE_READ;
3361 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3362 prot |= DMA_PTE_WRITE;
3363
David Woodhouseb536d242009-06-28 14:49:31 +01003364 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003365
Fenghua Yuf5329592009-08-04 15:09:37 -07003366 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003367 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003368 dma_pte_free_pagetable(domain, start_vpfn,
3369 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003370 __free_iova(&domain->iovad, iova);
3371 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003372 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003373
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003374 /* it's a non-present to present mapping. Only flush if caching mode */
3375 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003376 iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003377 else
Weidong Han8c11e792008-12-08 15:29:22 +08003378 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003379
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003380 return nelems;
3381}
3382
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003383static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3384{
3385 return !dma_addr;
3386}
3387
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003388struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003389 .alloc = intel_alloc_coherent,
3390 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003391 .map_sg = intel_map_sg,
3392 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003393 .map_page = intel_map_page,
3394 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003395 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003396};
3397
3398static inline int iommu_domain_cache_init(void)
3399{
3400 int ret = 0;
3401
3402 iommu_domain_cache = kmem_cache_create("iommu_domain",
3403 sizeof(struct dmar_domain),
3404 0,
3405 SLAB_HWCACHE_ALIGN,
3406
3407 NULL);
3408 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003409 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003410 ret = -ENOMEM;
3411 }
3412
3413 return ret;
3414}
3415
3416static inline int iommu_devinfo_cache_init(void)
3417{
3418 int ret = 0;
3419
3420 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3421 sizeof(struct device_domain_info),
3422 0,
3423 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003424 NULL);
3425 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003426 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003427 ret = -ENOMEM;
3428 }
3429
3430 return ret;
3431}
3432
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003433static int __init iommu_init_mempool(void)
3434{
3435 int ret;
3436 ret = iommu_iova_cache_init();
3437 if (ret)
3438 return ret;
3439
3440 ret = iommu_domain_cache_init();
3441 if (ret)
3442 goto domain_error;
3443
3444 ret = iommu_devinfo_cache_init();
3445 if (!ret)
3446 return ret;
3447
3448 kmem_cache_destroy(iommu_domain_cache);
3449domain_error:
Robin Murphy85b45452015-01-12 17:51:14 +00003450 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003451
3452 return -ENOMEM;
3453}
3454
3455static void __init iommu_exit_mempool(void)
3456{
3457 kmem_cache_destroy(iommu_devinfo_cache);
3458 kmem_cache_destroy(iommu_domain_cache);
Robin Murphy85b45452015-01-12 17:51:14 +00003459 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003460}
3461
Dan Williams556ab452010-07-23 15:47:56 -07003462static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3463{
3464 struct dmar_drhd_unit *drhd;
3465 u32 vtbar;
3466 int rc;
3467
3468 /* We know that this device on this chipset has its own IOMMU.
3469 * If we find it under a different IOMMU, then the BIOS is lying
3470 * to us. Hope that the IOMMU for this device is actually
3471 * disabled, and it needs no translation...
3472 */
3473 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3474 if (rc) {
3475 /* "can't" happen */
3476 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3477 return;
3478 }
3479 vtbar &= 0xffff0000;
3480
3481 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3482 drhd = dmar_find_matched_drhd_unit(pdev);
3483 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3484 TAINT_FIRMWARE_WORKAROUND,
3485 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3486 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3487}
3488DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3489
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003490static void __init init_no_remapping_devices(void)
3491{
3492 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003493 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003494 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003495
3496 for_each_drhd_unit(drhd) {
3497 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003498 for_each_active_dev_scope(drhd->devices,
3499 drhd->devices_cnt, i, dev)
3500 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003501 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003502 if (i == drhd->devices_cnt)
3503 drhd->ignored = 1;
3504 }
3505 }
3506
Jiang Liu7c919772014-01-06 14:18:18 +08003507 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003508 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003509 continue;
3510
Jiang Liub683b232014-02-19 14:07:32 +08003511 for_each_active_dev_scope(drhd->devices,
3512 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003513 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003514 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003515 if (i < drhd->devices_cnt)
3516 continue;
3517
David Woodhousec0771df2011-10-14 20:59:46 +01003518 /* This IOMMU has *only* gfx devices. Either bypass it or
3519 set the gfx_mapped flag, as appropriate */
3520 if (dmar_map_gfx) {
3521 intel_iommu_gfx_mapped = 1;
3522 } else {
3523 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003524 for_each_active_dev_scope(drhd->devices,
3525 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003526 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003527 }
3528 }
3529}
3530
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003531#ifdef CONFIG_SUSPEND
3532static int init_iommu_hw(void)
3533{
3534 struct dmar_drhd_unit *drhd;
3535 struct intel_iommu *iommu = NULL;
3536
3537 for_each_active_iommu(iommu, drhd)
3538 if (iommu->qi)
3539 dmar_reenable_qi(iommu);
3540
Joseph Cihulab7792602011-05-03 00:08:37 -07003541 for_each_iommu(iommu, drhd) {
3542 if (drhd->ignored) {
3543 /*
3544 * we always have to disable PMRs or DMA may fail on
3545 * this device
3546 */
3547 if (force_on)
3548 iommu_disable_protect_mem_regions(iommu);
3549 continue;
3550 }
3551
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003552 iommu_flush_write_buffer(iommu);
3553
3554 iommu_set_root_entry(iommu);
3555
3556 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003557 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003558 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3559 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003560 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003561 }
3562
3563 return 0;
3564}
3565
3566static void iommu_flush_all(void)
3567{
3568 struct dmar_drhd_unit *drhd;
3569 struct intel_iommu *iommu;
3570
3571 for_each_active_iommu(iommu, drhd) {
3572 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003573 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003574 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003575 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003576 }
3577}
3578
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003579static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003580{
3581 struct dmar_drhd_unit *drhd;
3582 struct intel_iommu *iommu = NULL;
3583 unsigned long flag;
3584
3585 for_each_active_iommu(iommu, drhd) {
3586 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3587 GFP_ATOMIC);
3588 if (!iommu->iommu_state)
3589 goto nomem;
3590 }
3591
3592 iommu_flush_all();
3593
3594 for_each_active_iommu(iommu, drhd) {
3595 iommu_disable_translation(iommu);
3596
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003597 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003598
3599 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3600 readl(iommu->reg + DMAR_FECTL_REG);
3601 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3602 readl(iommu->reg + DMAR_FEDATA_REG);
3603 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3604 readl(iommu->reg + DMAR_FEADDR_REG);
3605 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3606 readl(iommu->reg + DMAR_FEUADDR_REG);
3607
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003608 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003609 }
3610 return 0;
3611
3612nomem:
3613 for_each_active_iommu(iommu, drhd)
3614 kfree(iommu->iommu_state);
3615
3616 return -ENOMEM;
3617}
3618
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003619static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003620{
3621 struct dmar_drhd_unit *drhd;
3622 struct intel_iommu *iommu = NULL;
3623 unsigned long flag;
3624
3625 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003626 if (force_on)
3627 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3628 else
3629 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003630 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003631 }
3632
3633 for_each_active_iommu(iommu, drhd) {
3634
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003635 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003636
3637 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3638 iommu->reg + DMAR_FECTL_REG);
3639 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3640 iommu->reg + DMAR_FEDATA_REG);
3641 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3642 iommu->reg + DMAR_FEADDR_REG);
3643 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3644 iommu->reg + DMAR_FEUADDR_REG);
3645
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003646 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003647 }
3648
3649 for_each_active_iommu(iommu, drhd)
3650 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003651}
3652
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003653static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003654 .resume = iommu_resume,
3655 .suspend = iommu_suspend,
3656};
3657
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003658static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003659{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003660 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003661}
3662
3663#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003664static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003665#endif /* CONFIG_PM */
3666
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003667
Jiang Liuc2a0b532014-11-09 22:47:56 +08003668int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003669{
3670 struct acpi_dmar_reserved_memory *rmrr;
3671 struct dmar_rmrr_unit *rmrru;
3672
3673 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3674 if (!rmrru)
3675 return -ENOMEM;
3676
3677 rmrru->hdr = header;
3678 rmrr = (struct acpi_dmar_reserved_memory *)header;
3679 rmrru->base_address = rmrr->base_address;
3680 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08003681 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
3682 ((void *)rmrr) + rmrr->header.length,
3683 &rmrru->devices_cnt);
3684 if (rmrru->devices_cnt && rmrru->devices == NULL) {
3685 kfree(rmrru);
3686 return -ENOMEM;
3687 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003688
Jiang Liu2e455282014-02-19 14:07:36 +08003689 list_add(&rmrru->list, &dmar_rmrr_units);
3690
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003691 return 0;
3692}
3693
Jiang Liu6b197242014-11-09 22:47:58 +08003694static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
3695{
3696 struct dmar_atsr_unit *atsru;
3697 struct acpi_dmar_atsr *tmp;
3698
3699 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3700 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
3701 if (atsr->segment != tmp->segment)
3702 continue;
3703 if (atsr->header.length != tmp->header.length)
3704 continue;
3705 if (memcmp(atsr, tmp, atsr->header.length) == 0)
3706 return atsru;
3707 }
3708
3709 return NULL;
3710}
3711
3712int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003713{
3714 struct acpi_dmar_atsr *atsr;
3715 struct dmar_atsr_unit *atsru;
3716
Jiang Liu6b197242014-11-09 22:47:58 +08003717 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
3718 return 0;
3719
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003720 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08003721 atsru = dmar_find_atsr(atsr);
3722 if (atsru)
3723 return 0;
3724
3725 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003726 if (!atsru)
3727 return -ENOMEM;
3728
Jiang Liu6b197242014-11-09 22:47:58 +08003729 /*
3730 * If memory is allocated from slab by ACPI _DSM method, we need to
3731 * copy the memory content because the memory buffer will be freed
3732 * on return.
3733 */
3734 atsru->hdr = (void *)(atsru + 1);
3735 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003736 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08003737 if (!atsru->include_all) {
3738 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
3739 (void *)atsr + atsr->header.length,
3740 &atsru->devices_cnt);
3741 if (atsru->devices_cnt && atsru->devices == NULL) {
3742 kfree(atsru);
3743 return -ENOMEM;
3744 }
3745 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003746
Jiang Liu0e242612014-02-19 14:07:34 +08003747 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003748
3749 return 0;
3750}
3751
Jiang Liu9bdc5312014-01-06 14:18:27 +08003752static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
3753{
3754 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
3755 kfree(atsru);
3756}
3757
Jiang Liu6b197242014-11-09 22:47:58 +08003758int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
3759{
3760 struct acpi_dmar_atsr *atsr;
3761 struct dmar_atsr_unit *atsru;
3762
3763 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
3764 atsru = dmar_find_atsr(atsr);
3765 if (atsru) {
3766 list_del_rcu(&atsru->list);
3767 synchronize_rcu();
3768 intel_iommu_free_atsr(atsru);
3769 }
3770
3771 return 0;
3772}
3773
3774int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
3775{
3776 int i;
3777 struct device *dev;
3778 struct acpi_dmar_atsr *atsr;
3779 struct dmar_atsr_unit *atsru;
3780
3781 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
3782 atsru = dmar_find_atsr(atsr);
3783 if (!atsru)
3784 return 0;
3785
3786 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
3787 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
3788 i, dev)
3789 return -EBUSY;
3790
3791 return 0;
3792}
3793
Jiang Liuffebeb42014-11-09 22:48:02 +08003794static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
3795{
3796 int sp, ret = 0;
3797 struct intel_iommu *iommu = dmaru->iommu;
3798
3799 if (g_iommus[iommu->seq_id])
3800 return 0;
3801
3802 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003803 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003804 iommu->name);
3805 return -ENXIO;
3806 }
3807 if (!ecap_sc_support(iommu->ecap) &&
3808 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003809 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003810 iommu->name);
3811 return -ENXIO;
3812 }
3813 sp = domain_update_iommu_superpage(iommu) - 1;
3814 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003815 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003816 iommu->name);
3817 return -ENXIO;
3818 }
3819
3820 /*
3821 * Disable translation if already enabled prior to OS handover.
3822 */
3823 if (iommu->gcmd & DMA_GCMD_TE)
3824 iommu_disable_translation(iommu);
3825
3826 g_iommus[iommu->seq_id] = iommu;
3827 ret = iommu_init_domains(iommu);
3828 if (ret == 0)
3829 ret = iommu_alloc_root_entry(iommu);
3830 if (ret)
3831 goto out;
3832
3833 if (dmaru->ignored) {
3834 /*
3835 * we always have to disable PMRs or DMA may fail on this device
3836 */
3837 if (force_on)
3838 iommu_disable_protect_mem_regions(iommu);
3839 return 0;
3840 }
3841
3842 intel_iommu_init_qi(iommu);
3843 iommu_flush_write_buffer(iommu);
3844 ret = dmar_set_interrupt(iommu);
3845 if (ret)
3846 goto disable_iommu;
3847
3848 iommu_set_root_entry(iommu);
3849 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3850 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3851 iommu_enable_translation(iommu);
3852
3853 if (si_domain) {
3854 ret = iommu_attach_domain(si_domain, iommu);
3855 if (ret < 0 || si_domain->id != ret)
3856 goto disable_iommu;
3857 domain_attach_iommu(si_domain, iommu);
3858 }
3859
3860 iommu_disable_protect_mem_regions(iommu);
3861 return 0;
3862
3863disable_iommu:
3864 disable_dmar_iommu(iommu);
3865out:
3866 free_dmar_iommu(iommu);
3867 return ret;
3868}
3869
Jiang Liu6b197242014-11-09 22:47:58 +08003870int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
3871{
Jiang Liuffebeb42014-11-09 22:48:02 +08003872 int ret = 0;
3873 struct intel_iommu *iommu = dmaru->iommu;
3874
3875 if (!intel_iommu_enabled)
3876 return 0;
3877 if (iommu == NULL)
3878 return -EINVAL;
3879
3880 if (insert) {
3881 ret = intel_iommu_add(dmaru);
3882 } else {
3883 disable_dmar_iommu(iommu);
3884 free_dmar_iommu(iommu);
3885 }
3886
3887 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08003888}
3889
Jiang Liu9bdc5312014-01-06 14:18:27 +08003890static void intel_iommu_free_dmars(void)
3891{
3892 struct dmar_rmrr_unit *rmrru, *rmrr_n;
3893 struct dmar_atsr_unit *atsru, *atsr_n;
3894
3895 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
3896 list_del(&rmrru->list);
3897 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
3898 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003899 }
3900
Jiang Liu9bdc5312014-01-06 14:18:27 +08003901 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
3902 list_del(&atsru->list);
3903 intel_iommu_free_atsr(atsru);
3904 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003905}
3906
3907int dmar_find_matched_atsr_unit(struct pci_dev *dev)
3908{
Jiang Liub683b232014-02-19 14:07:32 +08003909 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003910 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00003911 struct pci_dev *bridge = NULL;
3912 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003913 struct acpi_dmar_atsr *atsr;
3914 struct dmar_atsr_unit *atsru;
3915
3916 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003917 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08003918 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003919 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08003920 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003921 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003922 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003923 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003924 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08003925 if (!bridge)
3926 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003927
Jiang Liu0e242612014-02-19 14:07:34 +08003928 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08003929 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3930 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
3931 if (atsr->segment != pci_domain_nr(dev->bus))
3932 continue;
3933
Jiang Liub683b232014-02-19 14:07:32 +08003934 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00003935 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08003936 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003937
3938 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08003939 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003940 }
Jiang Liub683b232014-02-19 14:07:32 +08003941 ret = 0;
3942out:
Jiang Liu0e242612014-02-19 14:07:34 +08003943 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003944
Jiang Liub683b232014-02-19 14:07:32 +08003945 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003946}
3947
Jiang Liu59ce0512014-02-19 14:07:35 +08003948int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
3949{
3950 int ret = 0;
3951 struct dmar_rmrr_unit *rmrru;
3952 struct dmar_atsr_unit *atsru;
3953 struct acpi_dmar_atsr *atsr;
3954 struct acpi_dmar_reserved_memory *rmrr;
3955
3956 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
3957 return 0;
3958
3959 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
3960 rmrr = container_of(rmrru->hdr,
3961 struct acpi_dmar_reserved_memory, header);
3962 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
3963 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
3964 ((void *)rmrr) + rmrr->header.length,
3965 rmrr->segment, rmrru->devices,
3966 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08003967 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08003968 return ret;
3969 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08003970 dmar_remove_dev_scope(info, rmrr->segment,
3971 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08003972 }
3973 }
3974
3975 list_for_each_entry(atsru, &dmar_atsr_units, list) {
3976 if (atsru->include_all)
3977 continue;
3978
3979 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
3980 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
3981 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
3982 (void *)atsr + atsr->header.length,
3983 atsr->segment, atsru->devices,
3984 atsru->devices_cnt);
3985 if (ret > 0)
3986 break;
3987 else if(ret < 0)
3988 return ret;
3989 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
3990 if (dmar_remove_dev_scope(info, atsr->segment,
3991 atsru->devices, atsru->devices_cnt))
3992 break;
3993 }
3994 }
3995
3996 return 0;
3997}
3998
Fenghua Yu99dcade2009-11-11 07:23:06 -08003999/*
4000 * Here we only respond to action of unbound device from driver.
4001 *
4002 * Added device is not attached to its DMAR domain here yet. That will happen
4003 * when mapping the device to iova.
4004 */
4005static int device_notifier(struct notifier_block *nb,
4006 unsigned long action, void *data)
4007{
4008 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004009 struct dmar_domain *domain;
4010
David Woodhouse3d891942014-03-06 15:59:26 +00004011 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004012 return 0;
4013
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004014 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004015 return 0;
4016
David Woodhouse1525a292014-03-06 16:19:30 +00004017 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004018 if (!domain)
4019 return 0;
4020
Jiang Liu3a5670e2014-02-19 14:07:33 +08004021 down_read(&dmar_global_lock);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004022 domain_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004023 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004024 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08004025 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07004026
Fenghua Yu99dcade2009-11-11 07:23:06 -08004027 return 0;
4028}
4029
4030static struct notifier_block device_nb = {
4031 .notifier_call = device_notifier,
4032};
4033
Jiang Liu75f05562014-02-19 14:07:37 +08004034static int intel_iommu_memory_notifier(struct notifier_block *nb,
4035 unsigned long val, void *v)
4036{
4037 struct memory_notify *mhp = v;
4038 unsigned long long start, end;
4039 unsigned long start_vpfn, last_vpfn;
4040
4041 switch (val) {
4042 case MEM_GOING_ONLINE:
4043 start = mhp->start_pfn << PAGE_SHIFT;
4044 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4045 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004046 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004047 start, end);
4048 return NOTIFY_BAD;
4049 }
4050 break;
4051
4052 case MEM_OFFLINE:
4053 case MEM_CANCEL_ONLINE:
4054 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4055 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4056 while (start_vpfn <= last_vpfn) {
4057 struct iova *iova;
4058 struct dmar_drhd_unit *drhd;
4059 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004060 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004061
4062 iova = find_iova(&si_domain->iovad, start_vpfn);
4063 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004064 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004065 start_vpfn);
4066 break;
4067 }
4068
4069 iova = split_and_remove_iova(&si_domain->iovad, iova,
4070 start_vpfn, last_vpfn);
4071 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004072 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004073 start_vpfn, last_vpfn);
4074 return NOTIFY_BAD;
4075 }
4076
David Woodhouseea8ea462014-03-05 17:09:32 +00004077 freelist = domain_unmap(si_domain, iova->pfn_lo,
4078 iova->pfn_hi);
4079
Jiang Liu75f05562014-02-19 14:07:37 +08004080 rcu_read_lock();
4081 for_each_active_iommu(iommu, drhd)
4082 iommu_flush_iotlb_psi(iommu, si_domain->id,
Jiang Liua156ef92014-07-11 14:19:36 +08004083 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004084 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004085 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004086 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004087
4088 start_vpfn = iova->pfn_hi + 1;
4089 free_iova_mem(iova);
4090 }
4091 break;
4092 }
4093
4094 return NOTIFY_OK;
4095}
4096
4097static struct notifier_block intel_iommu_memory_nb = {
4098 .notifier_call = intel_iommu_memory_notifier,
4099 .priority = 0
4100};
4101
Alex Williamsona5459cf2014-06-12 16:12:31 -06004102
4103static ssize_t intel_iommu_show_version(struct device *dev,
4104 struct device_attribute *attr,
4105 char *buf)
4106{
4107 struct intel_iommu *iommu = dev_get_drvdata(dev);
4108 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4109 return sprintf(buf, "%d:%d\n",
4110 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4111}
4112static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4113
4114static ssize_t intel_iommu_show_address(struct device *dev,
4115 struct device_attribute *attr,
4116 char *buf)
4117{
4118 struct intel_iommu *iommu = dev_get_drvdata(dev);
4119 return sprintf(buf, "%llx\n", iommu->reg_phys);
4120}
4121static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4122
4123static ssize_t intel_iommu_show_cap(struct device *dev,
4124 struct device_attribute *attr,
4125 char *buf)
4126{
4127 struct intel_iommu *iommu = dev_get_drvdata(dev);
4128 return sprintf(buf, "%llx\n", iommu->cap);
4129}
4130static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4131
4132static ssize_t intel_iommu_show_ecap(struct device *dev,
4133 struct device_attribute *attr,
4134 char *buf)
4135{
4136 struct intel_iommu *iommu = dev_get_drvdata(dev);
4137 return sprintf(buf, "%llx\n", iommu->ecap);
4138}
4139static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4140
4141static struct attribute *intel_iommu_attrs[] = {
4142 &dev_attr_version.attr,
4143 &dev_attr_address.attr,
4144 &dev_attr_cap.attr,
4145 &dev_attr_ecap.attr,
4146 NULL,
4147};
4148
4149static struct attribute_group intel_iommu_group = {
4150 .name = "intel-iommu",
4151 .attrs = intel_iommu_attrs,
4152};
4153
4154const struct attribute_group *intel_iommu_groups[] = {
4155 &intel_iommu_group,
4156 NULL,
4157};
4158
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004159int __init intel_iommu_init(void)
4160{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004161 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004162 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004163 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004164
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004165 /* VT-d is required for a TXT/tboot launch, so enforce that */
4166 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004167
Jiang Liu3a5670e2014-02-19 14:07:33 +08004168 if (iommu_init_mempool()) {
4169 if (force_on)
4170 panic("tboot: Failed to initialize iommu memory\n");
4171 return -ENOMEM;
4172 }
4173
4174 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004175 if (dmar_table_init()) {
4176 if (force_on)
4177 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004178 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004179 }
4180
Takao Indoh3a93c842013-04-23 17:35:03 +09004181 /*
4182 * Disable translation if already enabled prior to OS handover.
4183 */
Jiang Liu7c919772014-01-06 14:18:18 +08004184 for_each_active_iommu(iommu, drhd)
Takao Indoh3a93c842013-04-23 17:35:03 +09004185 if (iommu->gcmd & DMA_GCMD_TE)
4186 iommu_disable_translation(iommu);
Takao Indoh3a93c842013-04-23 17:35:03 +09004187
Suresh Siddhac2c72862011-08-23 17:05:19 -07004188 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004189 if (force_on)
4190 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004191 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004192 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004193
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004194 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004195 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004196
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004197 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004198 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004199
4200 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004201 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004202
Joseph Cihula51a63e62011-03-21 11:04:24 -07004203 if (dmar_init_reserved_ranges()) {
4204 if (force_on)
4205 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004206 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004207 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004208
4209 init_no_remapping_devices();
4210
Joseph Cihulab7792602011-05-03 00:08:37 -07004211 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004212 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004213 if (force_on)
4214 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004215 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004216 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004217 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004218 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004219 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004220
mark gross5e0d2a62008-03-04 15:22:08 -08004221 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004222#ifdef CONFIG_SWIOTLB
4223 swiotlb = 0;
4224#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004225 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004226
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004227 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004228
Alex Williamsona5459cf2014-06-12 16:12:31 -06004229 for_each_active_iommu(iommu, drhd)
4230 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4231 intel_iommu_groups,
4232 iommu->name);
4233
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004234 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004235 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004236 if (si_domain && !hw_pass_through)
4237 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004238
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004239 intel_iommu_enabled = 1;
4240
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004241 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004242
4243out_free_reserved_range:
4244 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004245out_free_dmar:
4246 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004247 up_write(&dmar_global_lock);
4248 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004249 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004250}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004251
Alex Williamson579305f2014-07-03 09:51:43 -06004252static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque)
4253{
4254 struct intel_iommu *iommu = opaque;
4255
4256 iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff);
4257 return 0;
4258}
4259
4260/*
4261 * NB - intel-iommu lacks any sort of reference counting for the users of
4262 * dependent devices. If multiple endpoints have intersecting dependent
4263 * devices, unbinding the driver from any one of them will possibly leave
4264 * the others unable to operate.
4265 */
Han, Weidong3199aa62009-02-26 17:31:12 +08004266static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004267 struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004268{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004269 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004270 return;
4271
Alex Williamson579305f2014-07-03 09:51:43 -06004272 pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004273}
4274
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004275static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004276 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004277{
Yijing Wangbca2b912013-10-31 17:26:04 +08004278 struct device_domain_info *info, *tmp;
Weidong Hanc7151a82008-12-08 22:51:37 +08004279 struct intel_iommu *iommu;
4280 unsigned long flags;
Quentin Lambert2f119c72015-02-06 10:59:53 +01004281 bool found = false;
David Woodhouse156baca2014-03-09 14:00:57 -07004282 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004283
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004284 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004285 if (!iommu)
4286 return;
4287
4288 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wangbca2b912013-10-31 17:26:04 +08004289 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004290 if (info->iommu == iommu && info->bus == bus &&
4291 info->devfn == devfn) {
David Woodhouse109b9b02012-05-25 17:43:02 +01004292 unlink_domain_info(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004293 spin_unlock_irqrestore(&device_domain_lock, flags);
4294
Yu Zhao93a23a72009-05-18 13:51:37 +08004295 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004296 iommu_detach_dev(iommu, info->bus, info->devfn);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004297 iommu_detach_dependent_devices(iommu, dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08004298 free_devinfo_mem(info);
4299
4300 spin_lock_irqsave(&device_domain_lock, flags);
4301
4302 if (found)
4303 break;
4304 else
4305 continue;
4306 }
4307
4308 /* if there is no other devices under the same iommu
4309 * owned by this domain, clear this iommu in iommu_bmp
4310 * update iommu count and coherency
4311 */
David Woodhouse8bbc4412014-03-09 13:52:37 -07004312 if (info->iommu == iommu)
Quentin Lambert2f119c72015-02-06 10:59:53 +01004313 found = true;
Weidong Hanc7151a82008-12-08 22:51:37 +08004314 }
4315
Roland Dreier3e7abe22011-07-20 06:22:21 -07004316 spin_unlock_irqrestore(&device_domain_lock, flags);
4317
Weidong Hanc7151a82008-12-08 22:51:37 +08004318 if (found == 0) {
Jiang Liufb170fb2014-07-11 14:19:28 +08004319 domain_detach_iommu(domain, iommu);
4320 if (!domain_type_is_vm_or_si(domain))
4321 iommu_detach_domain(domain, iommu);
Weidong Hanc7151a82008-12-08 22:51:37 +08004322 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004323}
4324
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004325static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004326{
4327 int adjust_width;
4328
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004329 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4330 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004331 domain_reserve_special_ranges(domain);
4332
4333 /* calculate AGAW */
4334 domain->gaw = guest_width;
4335 adjust_width = guestwidth_to_adjustwidth(guest_width);
4336 domain->agaw = width_to_agaw(adjust_width);
4337
Weidong Han5e98c4b2008-12-08 23:03:27 +08004338 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004339 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004340 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004341 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004342
4343 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004344 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004345 if (!domain->pgd)
4346 return -ENOMEM;
4347 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4348 return 0;
4349}
4350
Joerg Roedel00a77de2015-03-26 13:43:08 +01004351static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004352{
Joerg Roedel5d450802008-12-03 14:52:32 +01004353 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004354 struct iommu_domain *domain;
4355
4356 if (type != IOMMU_DOMAIN_UNMANAGED)
4357 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004358
Jiang Liuab8dfe22014-07-11 14:19:27 +08004359 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004360 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004361 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004362 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004363 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004364 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004365 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004366 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004367 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004368 }
Allen Kay8140a952011-10-14 12:32:17 -07004369 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004370
Joerg Roedel00a77de2015-03-26 13:43:08 +01004371 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004372 domain->geometry.aperture_start = 0;
4373 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4374 domain->geometry.force_aperture = true;
4375
Joerg Roedel00a77de2015-03-26 13:43:08 +01004376 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004377}
Kay, Allen M38717942008-09-09 18:37:29 +03004378
Joerg Roedel00a77de2015-03-26 13:43:08 +01004379static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004380{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004381 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004382}
Kay, Allen M38717942008-09-09 18:37:29 +03004383
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004384static int intel_iommu_attach_device(struct iommu_domain *domain,
4385 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004386{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004387 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004388 struct intel_iommu *iommu;
4389 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004390 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004391
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004392 if (device_is_rmrr_locked(dev)) {
4393 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4394 return -EPERM;
4395 }
4396
David Woodhouse7207d8f2014-03-09 16:31:06 -07004397 /* normally dev is not mapped */
4398 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004399 struct dmar_domain *old_domain;
4400
David Woodhouse1525a292014-03-06 16:19:30 +00004401 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004402 if (old_domain) {
Jiang Liuab8dfe22014-07-11 14:19:27 +08004403 if (domain_type_is_vm_or_si(dmar_domain))
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004404 domain_remove_one_dev_info(old_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004405 else
4406 domain_remove_dev_info(old_domain);
Joerg Roedel62c22162014-12-09 12:56:45 +01004407
4408 if (!domain_type_is_vm_or_si(old_domain) &&
4409 list_empty(&old_domain->devices))
4410 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004411 }
4412 }
4413
David Woodhouse156baca2014-03-09 14:00:57 -07004414 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004415 if (!iommu)
4416 return -ENODEV;
4417
4418 /* check if this iommu agaw is sufficient for max mapped address */
4419 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004420 if (addr_width > cap_mgaw(iommu->cap))
4421 addr_width = cap_mgaw(iommu->cap);
4422
4423 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004424 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004425 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004426 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004427 return -EFAULT;
4428 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004429 dmar_domain->gaw = addr_width;
4430
4431 /*
4432 * Knock out extra levels of page tables if necessary
4433 */
4434 while (iommu->agaw < dmar_domain->agaw) {
4435 struct dma_pte *pte;
4436
4437 pte = dmar_domain->pgd;
4438 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004439 dmar_domain->pgd = (struct dma_pte *)
4440 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004441 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004442 }
4443 dmar_domain->agaw--;
4444 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004445
David Woodhouse5913c9b2014-03-09 16:27:31 -07004446 return domain_add_dev_info(dmar_domain, dev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004447}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004448
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004449static void intel_iommu_detach_device(struct iommu_domain *domain,
4450 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004451{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004452 domain_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004453}
Kay, Allen M38717942008-09-09 18:37:29 +03004454
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004455static int intel_iommu_map(struct iommu_domain *domain,
4456 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004457 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004458{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004459 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004460 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004461 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004462 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004463
Joerg Roedeldde57a22008-12-03 15:04:09 +01004464 if (iommu_prot & IOMMU_READ)
4465 prot |= DMA_PTE_READ;
4466 if (iommu_prot & IOMMU_WRITE)
4467 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08004468 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4469 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004470
David Woodhouse163cc522009-06-28 00:51:17 +01004471 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004472 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004473 u64 end;
4474
4475 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004476 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004477 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004478 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004479 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004480 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004481 return -EFAULT;
4482 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004483 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004484 }
David Woodhousead051222009-06-28 14:22:28 +01004485 /* Round up size to next multiple of PAGE_SIZE, if it and
4486 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004487 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004488 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4489 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004490 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004491}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004492
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004493static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004494 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004495{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004496 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004497 struct page *freelist = NULL;
4498 struct intel_iommu *iommu;
4499 unsigned long start_pfn, last_pfn;
4500 unsigned int npages;
4501 int iommu_id, num, ndomains, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004502
David Woodhouse5cf0a762014-03-19 16:07:49 +00004503 /* Cope with horrid API which requires us to unmap more than the
4504 size argument if it happens to be a large-page mapping. */
4505 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4506 BUG();
4507
4508 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4509 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4510
David Woodhouseea8ea462014-03-05 17:09:32 +00004511 start_pfn = iova >> VTD_PAGE_SHIFT;
4512 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4513
4514 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4515
4516 npages = last_pfn - start_pfn + 1;
4517
4518 for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) {
4519 iommu = g_iommus[iommu_id];
4520
4521 /*
4522 * find bit position of dmar_domain
4523 */
4524 ndomains = cap_ndoms(iommu->cap);
4525 for_each_set_bit(num, iommu->domain_ids, ndomains) {
4526 if (iommu->domains[num] == dmar_domain)
4527 iommu_flush_iotlb_psi(iommu, num, start_pfn,
4528 npages, !freelist, 0);
4529 }
4530
4531 }
4532
4533 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004534
David Woodhouse163cc522009-06-28 00:51:17 +01004535 if (dmar_domain->max_addr == iova + size)
4536 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004537
David Woodhouse5cf0a762014-03-19 16:07:49 +00004538 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004539}
Kay, Allen M38717942008-09-09 18:37:29 +03004540
Joerg Roedeld14d6572008-12-03 15:06:57 +01004541static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05304542 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004543{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004544 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004545 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004546 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004547 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004548
David Woodhouse5cf0a762014-03-19 16:07:49 +00004549 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004550 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004551 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004552
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004553 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004554}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004555
Joerg Roedel5d587b82014-09-05 10:50:45 +02004556static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004557{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004558 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004559 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004560 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004561 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004562
Joerg Roedel5d587b82014-09-05 10:50:45 +02004563 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004564}
4565
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004566static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004567{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004568 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004569 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004570 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004571
Alex Williamsona5459cf2014-06-12 16:12:31 -06004572 iommu = device_to_iommu(dev, &bus, &devfn);
4573 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004574 return -ENODEV;
4575
Alex Williamsona5459cf2014-06-12 16:12:31 -06004576 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004577
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004578 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004579
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004580 if (IS_ERR(group))
4581 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004582
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004583 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004584 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004585}
4586
4587static void intel_iommu_remove_device(struct device *dev)
4588{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004589 struct intel_iommu *iommu;
4590 u8 bus, devfn;
4591
4592 iommu = device_to_iommu(dev, &bus, &devfn);
4593 if (!iommu)
4594 return;
4595
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004596 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004597
4598 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004599}
4600
Thierry Redingb22f6432014-06-27 09:03:12 +02004601static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004602 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004603 .domain_alloc = intel_iommu_domain_alloc,
4604 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004605 .attach_dev = intel_iommu_attach_device,
4606 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004607 .map = intel_iommu_map,
4608 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004609 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004610 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004611 .add_device = intel_iommu_add_device,
4612 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004613 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004614};
David Woodhouse9af88142009-02-13 23:18:03 +00004615
Daniel Vetter94526182013-01-20 23:50:13 +01004616static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4617{
4618 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004619 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004620 dmar_map_gfx = 0;
4621}
4622
4623DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4624DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4625DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4626DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4627DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4628DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4629DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4630
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004631static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004632{
4633 /*
4634 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004635 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004636 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004637 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00004638 rwbf_quirk = 1;
4639}
4640
4641DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004642DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4643DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4644DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4645DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4646DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4647DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004648
Adam Jacksoneecfd572010-08-25 21:17:34 +01004649#define GGC 0x52
4650#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4651#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4652#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4653#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4654#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4655#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4656#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4657#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4658
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004659static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004660{
4661 unsigned short ggc;
4662
Adam Jacksoneecfd572010-08-25 21:17:34 +01004663 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004664 return;
4665
Adam Jacksoneecfd572010-08-25 21:17:34 +01004666 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004667 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01004668 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004669 } else if (dmar_map_gfx) {
4670 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004671 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004672 intel_iommu_strict = 1;
4673 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01004674}
4675DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
4676DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
4677DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
4678DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
4679
David Woodhousee0fc7e02009-09-30 09:12:17 -07004680/* On Tylersburg chipsets, some BIOSes have been known to enable the
4681 ISOCH DMAR unit for the Azalia sound device, but not give it any
4682 TLB entries, which causes it to deadlock. Check for that. We do
4683 this in a function called from init_dmars(), instead of in a PCI
4684 quirk, because we don't want to print the obnoxious "BIOS broken"
4685 message if VT-d is actually disabled.
4686*/
4687static void __init check_tylersburg_isoch(void)
4688{
4689 struct pci_dev *pdev;
4690 uint32_t vtisochctrl;
4691
4692 /* If there's no Azalia in the system anyway, forget it. */
4693 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
4694 if (!pdev)
4695 return;
4696 pci_dev_put(pdev);
4697
4698 /* System Management Registers. Might be hidden, in which case
4699 we can't do the sanity check. But that's OK, because the
4700 known-broken BIOSes _don't_ actually hide it, so far. */
4701 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
4702 if (!pdev)
4703 return;
4704
4705 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
4706 pci_dev_put(pdev);
4707 return;
4708 }
4709
4710 pci_dev_put(pdev);
4711
4712 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
4713 if (vtisochctrl & 1)
4714 return;
4715
4716 /* Drop all bits other than the number of TLB entries */
4717 vtisochctrl &= 0x1c;
4718
4719 /* If we have the recommended number of TLB entries (16), fine. */
4720 if (vtisochctrl == 0x10)
4721 return;
4722
4723 /* Zero TLB entries? You get to ride the short bus to school. */
4724 if (!vtisochctrl) {
4725 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
4726 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
4727 dmi_get_system_info(DMI_BIOS_VENDOR),
4728 dmi_get_system_info(DMI_BIOS_VERSION),
4729 dmi_get_system_info(DMI_PRODUCT_VERSION));
4730 iommu_identity_mapping |= IDENTMAP_AZALIA;
4731 return;
4732 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004733
4734 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07004735 vtisochctrl);
4736}