blob: bf3e450b5b976dbe7dadb575f25bd0b697493bea [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 Hongyang284901a2009-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 McLoughlina647dacb2008-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 Woodhouse276dbf992009-04-04 01:45:37 +0100353 u8 bus; /* PCI bus number */
Mark McLoughlina647dacb2008-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 McLoughlina647dacb2008-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 Woodhouse276dbf992009-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 Woodhouse206a73c2009-07-01 19:30:28 +0100864 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700865 return NULL;
David Woodhouse206a73c2009-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 Woodhouse595badf52009-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 Dutile94a91b502009-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 Dutile94a91b502009-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 Dutile94a91b502009-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 Mingarelliea2447f72012-11-20 19:43:17 +00002531{
2532 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002533 struct device *tmp;
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002534 int i;
2535
Jiang Liu0e242612014-02-19 14:07:34 +08002536 rcu_read_lock();
Tom Mingarelliea2447f72012-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 Mingarelliea2447f72012-11-20 19:43:17 +00002546 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002547 }
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002548 }
Jiang Liu0e242612014-02-19 14:07:34 +08002549 rcu_read_unlock();
Tom Mingarelliea2447f72012-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 Mingarelliea2447f72012-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 Mingarelliea2447f72012-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
Joerg Roedelb63d80d2015-06-12 09:14:34 +02002806 intel_iommu_init_qi(iommu);
2807
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002808 ret = iommu_init_domains(iommu);
2809 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002810 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002811
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002812 /*
2813 * TBD:
2814 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002815 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002816 */
2817 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08002818 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002819 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02002820
2821 iommu_flush_write_buffer(iommu);
2822 iommu_set_root_entry(iommu);
2823 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
2824 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
2825
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002826 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01002827 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002828 }
2829
David Woodhouse19943b02009-08-04 16:19:20 +01002830 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07002831 iommu_identity_mapping |= IDENTMAP_ALL;
2832
Suresh Siddhad3f13812011-08-23 17:05:25 -07002833#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07002834 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01002835#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07002836
2837 check_tylersburg_isoch();
2838
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002839 /*
2840 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002841 * identity mappings for rmrr, gfx, and isa and may fall back to static
2842 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002843 */
David Woodhouse19943b02009-08-04 16:19:20 +01002844 if (iommu_identity_mapping) {
2845 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
2846 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002847 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08002848 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002849 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002850 }
David Woodhouse19943b02009-08-04 16:19:20 +01002851 /*
2852 * For each rmrr
2853 * for each dev attached to rmrr
2854 * do
2855 * locate drhd for dev, alloc domain for dev
2856 * allocate free domain
2857 * allocate page table entries for rmrr
2858 * if context not allocated for bus
2859 * allocate and init context
2860 * set present in root table for this bus
2861 * init context with domain, translation etc
2862 * endfor
2863 * endfor
2864 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002865 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01002866 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002867 /* some BIOS lists non-exist devices in DMAR table. */
2868 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00002869 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07002870 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01002871 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002872 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01002873 }
2874 }
2875
2876 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002877
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002878 /*
2879 * for each drhd
2880 * enable fault log
2881 * global invalidate context cache
2882 * global invalidate iotlb
2883 * enable translation
2884 */
Jiang Liu7c919772014-01-06 14:18:18 +08002885 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07002886 if (drhd->ignored) {
2887 /*
2888 * we always have to disable PMRs or DMA may fail on
2889 * this device
2890 */
2891 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08002892 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002893 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07002894 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002895
2896 iommu_flush_write_buffer(iommu);
2897
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002898 ret = dmar_set_interrupt(iommu);
2899 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002900 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002901
Jiang Liu2a41cce2014-07-11 14:19:33 +08002902 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07002903 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002904 }
2905
2906 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08002907
2908free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08002909 for_each_active_iommu(iommu, drhd) {
2910 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08002911 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08002912 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08002913 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08002914free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08002915 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08002916error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002917 return ret;
2918}
2919
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002920/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01002921static struct iova *intel_alloc_iova(struct device *dev,
2922 struct dmar_domain *domain,
2923 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002924{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002925 struct iova *iova = NULL;
2926
David Woodhouse875764d2009-06-28 21:20:51 +01002927 /* Restrict dma_mask to the width that the iommu can handle */
2928 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
2929
2930 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002931 /*
2932 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002933 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002934 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002935 */
David Woodhouse875764d2009-06-28 21:20:51 +01002936 iova = alloc_iova(&domain->iovad, nrpages,
2937 IOVA_PFN(DMA_BIT_MASK(32)), 1);
2938 if (iova)
2939 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002940 }
David Woodhouse875764d2009-06-28 21:20:51 +01002941 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
2942 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002943 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07002944 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002945 return NULL;
2946 }
2947
2948 return iova;
2949}
2950
David Woodhoused4b709f2014-03-09 16:07:40 -07002951static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002952{
2953 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002954 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002955
David Woodhoused4b709f2014-03-09 16:07:40 -07002956 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002957 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002958 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07002959 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002960 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002961 }
2962
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002963 /* make sure context mapping is ok */
David Woodhoused4b709f2014-03-09 16:07:40 -07002964 if (unlikely(!domain_context_mapped(dev))) {
2965 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002966 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002967 pr_err("Domain context map for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07002968 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002969 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002970 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002971 }
2972
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002973 return domain;
2974}
2975
David Woodhoused4b709f2014-03-09 16:07:40 -07002976static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01002977{
2978 struct device_domain_info *info;
2979
2980 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07002981 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01002982 if (likely(info))
2983 return info->domain;
2984
2985 return __get_valid_domain_for_dev(dev);
2986}
2987
David Woodhouseecb509e2014-03-09 16:29:55 -07002988/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01002989static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002990{
2991 int found;
2992
David Woodhouse3d891942014-03-06 15:59:26 +00002993 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002994 return 1;
2995
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002996 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002997 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002998
David Woodhouse9b226622014-03-09 14:03:28 -07002999 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003000 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003001 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003002 return 1;
3003 else {
3004 /*
3005 * 32 bit DMA is removed from si_domain and fall back
3006 * to non-identity mapping.
3007 */
David Woodhousebf9c9ed2014-03-09 16:19:13 -07003008 domain_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003009 pr_info("32bit %s uses non-identity mapping\n",
3010 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003011 return 0;
3012 }
3013 } else {
3014 /*
3015 * In case of a detached 64 bit DMA device from vm, the device
3016 * is put into si_domain for identity mapping.
3017 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003018 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003019 int ret;
David Woodhouse5913c9b2014-03-09 16:27:31 -07003020 ret = domain_add_dev_info(si_domain, dev,
David Woodhouse5fe60f42009-08-09 10:53:41 +01003021 hw_pass_through ?
3022 CONTEXT_TT_PASS_THROUGH :
3023 CONTEXT_TT_MULTI_LEVEL);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003024 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003025 pr_info("64bit %s uses identity mapping\n",
3026 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003027 return 1;
3028 }
3029 }
3030 }
3031
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003032 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003033}
3034
David Woodhouse5040a912014-03-09 16:14:00 -07003035static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003036 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003037{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003038 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003039 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003040 struct iova *iova;
3041 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003042 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003043 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003044 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003045
3046 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003047
David Woodhouse5040a912014-03-09 16:14:00 -07003048 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003049 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003050
David Woodhouse5040a912014-03-09 16:14:00 -07003051 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003052 if (!domain)
3053 return 0;
3054
Weidong Han8c11e792008-12-08 15:29:22 +08003055 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003056 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003057
David Woodhouse5040a912014-03-09 16:14:00 -07003058 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003059 if (!iova)
3060 goto error;
3061
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003062 /*
3063 * Check if DMAR supports zero-length reads on write only
3064 * mappings..
3065 */
3066 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003067 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003068 prot |= DMA_PTE_READ;
3069 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3070 prot |= DMA_PTE_WRITE;
3071 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003072 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003073 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003074 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003075 * is not a big problem
3076 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003077 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003078 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003079 if (ret)
3080 goto error;
3081
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003082 /* it's a non-present to present mapping. Only flush if caching mode */
3083 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003084 iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003085 else
Weidong Han8c11e792008-12-08 15:29:22 +08003086 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003087
David Woodhouse03d6a242009-06-28 15:33:46 +01003088 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3089 start_paddr += paddr & ~PAGE_MASK;
3090 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003091
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003092error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003093 if (iova)
3094 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003095 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003096 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003097 return 0;
3098}
3099
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003100static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3101 unsigned long offset, size_t size,
3102 enum dma_data_direction dir,
3103 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003104{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003105 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003106 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003107}
3108
mark gross5e0d2a62008-03-04 15:22:08 -08003109static void flush_unmaps(void)
3110{
mark gross80b20dd2008-04-18 13:53:58 -07003111 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003112
mark gross5e0d2a62008-03-04 15:22:08 -08003113 timer_on = 0;
3114
3115 /* just flush them all */
3116 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003117 struct intel_iommu *iommu = g_iommus[i];
3118 if (!iommu)
3119 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003120
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003121 if (!deferred_flush[i].next)
3122 continue;
3123
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003124 /* In caching mode, global flushes turn emulation expensive */
3125 if (!cap_caching_mode(iommu->cap))
3126 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003127 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003128 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003129 unsigned long mask;
3130 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003131 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003132
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003133 /* On real hardware multiple invalidations are expensive */
3134 if (cap_caching_mode(iommu->cap))
3135 iommu_flush_iotlb_psi(iommu, domain->id,
Jiang Liua156ef92014-07-11 14:19:36 +08003136 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003137 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003138 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003139 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003140 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3141 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3142 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003143 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003144 if (deferred_flush[i].freelist[j])
3145 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003146 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003147 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003148 }
3149
mark gross5e0d2a62008-03-04 15:22:08 -08003150 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003151}
3152
3153static void flush_unmaps_timeout(unsigned long data)
3154{
mark gross80b20dd2008-04-18 13:53:58 -07003155 unsigned long flags;
3156
3157 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003158 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003159 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003160}
3161
David Woodhouseea8ea462014-03-05 17:09:32 +00003162static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003163{
3164 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003165 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003166 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003167
3168 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003169 if (list_size == HIGH_WATER_MARK)
3170 flush_unmaps();
3171
Weidong Han8c11e792008-12-08 15:29:22 +08003172 iommu = domain_get_iommu(dom);
3173 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003174
mark gross80b20dd2008-04-18 13:53:58 -07003175 next = deferred_flush[iommu_id].next;
3176 deferred_flush[iommu_id].domain[next] = dom;
3177 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003178 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003179 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003180
3181 if (!timer_on) {
3182 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3183 timer_on = 1;
3184 }
3185 list_size++;
3186 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3187}
3188
Jiang Liud41a4ad2014-07-11 14:19:34 +08003189static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003190{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003191 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003192 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003193 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003194 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003195 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003196
David Woodhouse73676832009-07-04 14:08:36 +01003197 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003198 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003199
David Woodhouse1525a292014-03-06 16:19:30 +00003200 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003201 BUG_ON(!domain);
3202
Weidong Han8c11e792008-12-08 15:29:22 +08003203 iommu = domain_get_iommu(domain);
3204
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003205 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003206 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3207 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003208 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003209
David Woodhoused794dc92009-06-28 00:27:49 +01003210 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3211 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003212
David Woodhoused794dc92009-06-28 00:27:49 +01003213 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003214 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003215
David Woodhouseea8ea462014-03-05 17:09:32 +00003216 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003217
mark gross5e0d2a62008-03-04 15:22:08 -08003218 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01003219 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003220 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003221 /* free iova */
3222 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003223 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003224 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003225 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003226 /*
3227 * queue up the release of the unmap to save the 1/6th of the
3228 * cpu used up by the iotlb flush operation...
3229 */
mark gross5e0d2a62008-03-04 15:22:08 -08003230 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003231}
3232
Jiang Liud41a4ad2014-07-11 14:19:34 +08003233static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3234 size_t size, enum dma_data_direction dir,
3235 struct dma_attrs *attrs)
3236{
3237 intel_unmap(dev, dev_addr);
3238}
3239
David Woodhouse5040a912014-03-09 16:14:00 -07003240static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003241 dma_addr_t *dma_handle, gfp_t flags,
3242 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003243{
Akinobu Mita36746432014-06-04 16:06:51 -07003244 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003245 int order;
3246
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003247 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003248 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003249
David Woodhouse5040a912014-03-09 16:14:00 -07003250 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003251 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003252 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3253 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003254 flags |= GFP_DMA;
3255 else
3256 flags |= GFP_DMA32;
3257 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003258
Akinobu Mita36746432014-06-04 16:06:51 -07003259 if (flags & __GFP_WAIT) {
3260 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003261
Akinobu Mita36746432014-06-04 16:06:51 -07003262 page = dma_alloc_from_contiguous(dev, count, order);
3263 if (page && iommu_no_mapping(dev) &&
3264 page_to_phys(page) + size > dev->coherent_dma_mask) {
3265 dma_release_from_contiguous(dev, page, count);
3266 page = NULL;
3267 }
3268 }
3269
3270 if (!page)
3271 page = alloc_pages(flags, order);
3272 if (!page)
3273 return NULL;
3274 memset(page_address(page), 0, size);
3275
3276 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003277 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003278 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003279 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003280 return page_address(page);
3281 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3282 __free_pages(page, order);
3283
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003284 return NULL;
3285}
3286
David Woodhouse5040a912014-03-09 16:14:00 -07003287static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003288 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003289{
3290 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003291 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003292
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003293 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003294 order = get_order(size);
3295
Jiang Liud41a4ad2014-07-11 14:19:34 +08003296 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003297 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3298 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003299}
3300
David Woodhouse5040a912014-03-09 16:14:00 -07003301static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003302 int nelems, enum dma_data_direction dir,
3303 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003304{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003305 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003306}
3307
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003308static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003309 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003310{
3311 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003312 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003313
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003314 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003315 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003316 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003317 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003318 }
3319 return nelems;
3320}
3321
David Woodhouse5040a912014-03-09 16:14:00 -07003322static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003323 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003324{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003325 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003326 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003327 size_t size = 0;
3328 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003329 struct iova *iova = NULL;
3330 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003331 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003332 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003333 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003334
3335 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003336 if (iommu_no_mapping(dev))
3337 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003338
David Woodhouse5040a912014-03-09 16:14:00 -07003339 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003340 if (!domain)
3341 return 0;
3342
Weidong Han8c11e792008-12-08 15:29:22 +08003343 iommu = domain_get_iommu(domain);
3344
David Woodhouseb536d242009-06-28 14:49:31 +01003345 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003346 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003347
David Woodhouse5040a912014-03-09 16:14:00 -07003348 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3349 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003350 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003351 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003352 return 0;
3353 }
3354
3355 /*
3356 * Check if DMAR supports zero-length reads on write only
3357 * mappings..
3358 */
3359 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003360 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003361 prot |= DMA_PTE_READ;
3362 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3363 prot |= DMA_PTE_WRITE;
3364
David Woodhouseb536d242009-06-28 14:49:31 +01003365 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003366
Fenghua Yuf5329592009-08-04 15:09:37 -07003367 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003368 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003369 dma_pte_free_pagetable(domain, start_vpfn,
3370 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003371 __free_iova(&domain->iovad, iova);
3372 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003373 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003374
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003375 /* it's a non-present to present mapping. Only flush if caching mode */
3376 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003377 iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003378 else
Weidong Han8c11e792008-12-08 15:29:22 +08003379 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003380
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003381 return nelems;
3382}
3383
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003384static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3385{
3386 return !dma_addr;
3387}
3388
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003389struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003390 .alloc = intel_alloc_coherent,
3391 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003392 .map_sg = intel_map_sg,
3393 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003394 .map_page = intel_map_page,
3395 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003396 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003397};
3398
3399static inline int iommu_domain_cache_init(void)
3400{
3401 int ret = 0;
3402
3403 iommu_domain_cache = kmem_cache_create("iommu_domain",
3404 sizeof(struct dmar_domain),
3405 0,
3406 SLAB_HWCACHE_ALIGN,
3407
3408 NULL);
3409 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003410 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003411 ret = -ENOMEM;
3412 }
3413
3414 return ret;
3415}
3416
3417static inline int iommu_devinfo_cache_init(void)
3418{
3419 int ret = 0;
3420
3421 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3422 sizeof(struct device_domain_info),
3423 0,
3424 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003425 NULL);
3426 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003427 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003428 ret = -ENOMEM;
3429 }
3430
3431 return ret;
3432}
3433
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003434static int __init iommu_init_mempool(void)
3435{
3436 int ret;
3437 ret = iommu_iova_cache_init();
3438 if (ret)
3439 return ret;
3440
3441 ret = iommu_domain_cache_init();
3442 if (ret)
3443 goto domain_error;
3444
3445 ret = iommu_devinfo_cache_init();
3446 if (!ret)
3447 return ret;
3448
3449 kmem_cache_destroy(iommu_domain_cache);
3450domain_error:
Robin Murphy85b45452015-01-12 17:51:14 +00003451 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003452
3453 return -ENOMEM;
3454}
3455
3456static void __init iommu_exit_mempool(void)
3457{
3458 kmem_cache_destroy(iommu_devinfo_cache);
3459 kmem_cache_destroy(iommu_domain_cache);
Robin Murphy85b45452015-01-12 17:51:14 +00003460 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003461}
3462
Dan Williams556ab452010-07-23 15:47:56 -07003463static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3464{
3465 struct dmar_drhd_unit *drhd;
3466 u32 vtbar;
3467 int rc;
3468
3469 /* We know that this device on this chipset has its own IOMMU.
3470 * If we find it under a different IOMMU, then the BIOS is lying
3471 * to us. Hope that the IOMMU for this device is actually
3472 * disabled, and it needs no translation...
3473 */
3474 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3475 if (rc) {
3476 /* "can't" happen */
3477 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3478 return;
3479 }
3480 vtbar &= 0xffff0000;
3481
3482 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3483 drhd = dmar_find_matched_drhd_unit(pdev);
3484 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3485 TAINT_FIRMWARE_WORKAROUND,
3486 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3487 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3488}
3489DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3490
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003491static void __init init_no_remapping_devices(void)
3492{
3493 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003494 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003495 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003496
3497 for_each_drhd_unit(drhd) {
3498 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003499 for_each_active_dev_scope(drhd->devices,
3500 drhd->devices_cnt, i, dev)
3501 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003502 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003503 if (i == drhd->devices_cnt)
3504 drhd->ignored = 1;
3505 }
3506 }
3507
Jiang Liu7c919772014-01-06 14:18:18 +08003508 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003509 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003510 continue;
3511
Jiang Liub683b232014-02-19 14:07:32 +08003512 for_each_active_dev_scope(drhd->devices,
3513 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003514 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003515 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003516 if (i < drhd->devices_cnt)
3517 continue;
3518
David Woodhousec0771df2011-10-14 20:59:46 +01003519 /* This IOMMU has *only* gfx devices. Either bypass it or
3520 set the gfx_mapped flag, as appropriate */
3521 if (dmar_map_gfx) {
3522 intel_iommu_gfx_mapped = 1;
3523 } else {
3524 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003525 for_each_active_dev_scope(drhd->devices,
3526 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003527 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003528 }
3529 }
3530}
3531
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003532#ifdef CONFIG_SUSPEND
3533static int init_iommu_hw(void)
3534{
3535 struct dmar_drhd_unit *drhd;
3536 struct intel_iommu *iommu = NULL;
3537
3538 for_each_active_iommu(iommu, drhd)
3539 if (iommu->qi)
3540 dmar_reenable_qi(iommu);
3541
Joseph Cihulab7792602011-05-03 00:08:37 -07003542 for_each_iommu(iommu, drhd) {
3543 if (drhd->ignored) {
3544 /*
3545 * we always have to disable PMRs or DMA may fail on
3546 * this device
3547 */
3548 if (force_on)
3549 iommu_disable_protect_mem_regions(iommu);
3550 continue;
3551 }
3552
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003553 iommu_flush_write_buffer(iommu);
3554
3555 iommu_set_root_entry(iommu);
3556
3557 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003558 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003559 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3560 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003561 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003562 }
3563
3564 return 0;
3565}
3566
3567static void iommu_flush_all(void)
3568{
3569 struct dmar_drhd_unit *drhd;
3570 struct intel_iommu *iommu;
3571
3572 for_each_active_iommu(iommu, drhd) {
3573 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003574 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003575 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003576 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003577 }
3578}
3579
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003580static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003581{
3582 struct dmar_drhd_unit *drhd;
3583 struct intel_iommu *iommu = NULL;
3584 unsigned long flag;
3585
3586 for_each_active_iommu(iommu, drhd) {
3587 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3588 GFP_ATOMIC);
3589 if (!iommu->iommu_state)
3590 goto nomem;
3591 }
3592
3593 iommu_flush_all();
3594
3595 for_each_active_iommu(iommu, drhd) {
3596 iommu_disable_translation(iommu);
3597
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003598 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003599
3600 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3601 readl(iommu->reg + DMAR_FECTL_REG);
3602 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3603 readl(iommu->reg + DMAR_FEDATA_REG);
3604 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3605 readl(iommu->reg + DMAR_FEADDR_REG);
3606 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3607 readl(iommu->reg + DMAR_FEUADDR_REG);
3608
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003609 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003610 }
3611 return 0;
3612
3613nomem:
3614 for_each_active_iommu(iommu, drhd)
3615 kfree(iommu->iommu_state);
3616
3617 return -ENOMEM;
3618}
3619
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003620static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003621{
3622 struct dmar_drhd_unit *drhd;
3623 struct intel_iommu *iommu = NULL;
3624 unsigned long flag;
3625
3626 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003627 if (force_on)
3628 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3629 else
3630 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003631 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003632 }
3633
3634 for_each_active_iommu(iommu, drhd) {
3635
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003636 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003637
3638 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3639 iommu->reg + DMAR_FECTL_REG);
3640 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3641 iommu->reg + DMAR_FEDATA_REG);
3642 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3643 iommu->reg + DMAR_FEADDR_REG);
3644 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3645 iommu->reg + DMAR_FEUADDR_REG);
3646
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003647 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003648 }
3649
3650 for_each_active_iommu(iommu, drhd)
3651 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003652}
3653
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003654static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003655 .resume = iommu_resume,
3656 .suspend = iommu_suspend,
3657};
3658
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003659static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003660{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003661 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003662}
3663
3664#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003665static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003666#endif /* CONFIG_PM */
3667
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003668
Jiang Liuc2a0b532014-11-09 22:47:56 +08003669int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003670{
3671 struct acpi_dmar_reserved_memory *rmrr;
3672 struct dmar_rmrr_unit *rmrru;
3673
3674 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3675 if (!rmrru)
3676 return -ENOMEM;
3677
3678 rmrru->hdr = header;
3679 rmrr = (struct acpi_dmar_reserved_memory *)header;
3680 rmrru->base_address = rmrr->base_address;
3681 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08003682 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
3683 ((void *)rmrr) + rmrr->header.length,
3684 &rmrru->devices_cnt);
3685 if (rmrru->devices_cnt && rmrru->devices == NULL) {
3686 kfree(rmrru);
3687 return -ENOMEM;
3688 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003689
Jiang Liu2e455282014-02-19 14:07:36 +08003690 list_add(&rmrru->list, &dmar_rmrr_units);
3691
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003692 return 0;
3693}
3694
Jiang Liu6b197242014-11-09 22:47:58 +08003695static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
3696{
3697 struct dmar_atsr_unit *atsru;
3698 struct acpi_dmar_atsr *tmp;
3699
3700 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3701 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
3702 if (atsr->segment != tmp->segment)
3703 continue;
3704 if (atsr->header.length != tmp->header.length)
3705 continue;
3706 if (memcmp(atsr, tmp, atsr->header.length) == 0)
3707 return atsru;
3708 }
3709
3710 return NULL;
3711}
3712
3713int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003714{
3715 struct acpi_dmar_atsr *atsr;
3716 struct dmar_atsr_unit *atsru;
3717
Jiang Liu6b197242014-11-09 22:47:58 +08003718 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
3719 return 0;
3720
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003721 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08003722 atsru = dmar_find_atsr(atsr);
3723 if (atsru)
3724 return 0;
3725
3726 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003727 if (!atsru)
3728 return -ENOMEM;
3729
Jiang Liu6b197242014-11-09 22:47:58 +08003730 /*
3731 * If memory is allocated from slab by ACPI _DSM method, we need to
3732 * copy the memory content because the memory buffer will be freed
3733 * on return.
3734 */
3735 atsru->hdr = (void *)(atsru + 1);
3736 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003737 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08003738 if (!atsru->include_all) {
3739 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
3740 (void *)atsr + atsr->header.length,
3741 &atsru->devices_cnt);
3742 if (atsru->devices_cnt && atsru->devices == NULL) {
3743 kfree(atsru);
3744 return -ENOMEM;
3745 }
3746 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003747
Jiang Liu0e242612014-02-19 14:07:34 +08003748 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003749
3750 return 0;
3751}
3752
Jiang Liu9bdc5312014-01-06 14:18:27 +08003753static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
3754{
3755 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
3756 kfree(atsru);
3757}
3758
Jiang Liu6b197242014-11-09 22:47:58 +08003759int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
3760{
3761 struct acpi_dmar_atsr *atsr;
3762 struct dmar_atsr_unit *atsru;
3763
3764 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
3765 atsru = dmar_find_atsr(atsr);
3766 if (atsru) {
3767 list_del_rcu(&atsru->list);
3768 synchronize_rcu();
3769 intel_iommu_free_atsr(atsru);
3770 }
3771
3772 return 0;
3773}
3774
3775int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
3776{
3777 int i;
3778 struct device *dev;
3779 struct acpi_dmar_atsr *atsr;
3780 struct dmar_atsr_unit *atsru;
3781
3782 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
3783 atsru = dmar_find_atsr(atsr);
3784 if (!atsru)
3785 return 0;
3786
3787 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
3788 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
3789 i, dev)
3790 return -EBUSY;
3791
3792 return 0;
3793}
3794
Jiang Liuffebeb42014-11-09 22:48:02 +08003795static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
3796{
3797 int sp, ret = 0;
3798 struct intel_iommu *iommu = dmaru->iommu;
3799
3800 if (g_iommus[iommu->seq_id])
3801 return 0;
3802
3803 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003804 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003805 iommu->name);
3806 return -ENXIO;
3807 }
3808 if (!ecap_sc_support(iommu->ecap) &&
3809 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003810 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003811 iommu->name);
3812 return -ENXIO;
3813 }
3814 sp = domain_update_iommu_superpage(iommu) - 1;
3815 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003816 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003817 iommu->name);
3818 return -ENXIO;
3819 }
3820
3821 /*
3822 * Disable translation if already enabled prior to OS handover.
3823 */
3824 if (iommu->gcmd & DMA_GCMD_TE)
3825 iommu_disable_translation(iommu);
3826
3827 g_iommus[iommu->seq_id] = iommu;
3828 ret = iommu_init_domains(iommu);
3829 if (ret == 0)
3830 ret = iommu_alloc_root_entry(iommu);
3831 if (ret)
3832 goto out;
3833
3834 if (dmaru->ignored) {
3835 /*
3836 * we always have to disable PMRs or DMA may fail on this device
3837 */
3838 if (force_on)
3839 iommu_disable_protect_mem_regions(iommu);
3840 return 0;
3841 }
3842
3843 intel_iommu_init_qi(iommu);
3844 iommu_flush_write_buffer(iommu);
3845 ret = dmar_set_interrupt(iommu);
3846 if (ret)
3847 goto disable_iommu;
3848
3849 iommu_set_root_entry(iommu);
3850 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3851 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3852 iommu_enable_translation(iommu);
3853
3854 if (si_domain) {
3855 ret = iommu_attach_domain(si_domain, iommu);
3856 if (ret < 0 || si_domain->id != ret)
3857 goto disable_iommu;
3858 domain_attach_iommu(si_domain, iommu);
3859 }
3860
3861 iommu_disable_protect_mem_regions(iommu);
3862 return 0;
3863
3864disable_iommu:
3865 disable_dmar_iommu(iommu);
3866out:
3867 free_dmar_iommu(iommu);
3868 return ret;
3869}
3870
Jiang Liu6b197242014-11-09 22:47:58 +08003871int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
3872{
Jiang Liuffebeb42014-11-09 22:48:02 +08003873 int ret = 0;
3874 struct intel_iommu *iommu = dmaru->iommu;
3875
3876 if (!intel_iommu_enabled)
3877 return 0;
3878 if (iommu == NULL)
3879 return -EINVAL;
3880
3881 if (insert) {
3882 ret = intel_iommu_add(dmaru);
3883 } else {
3884 disable_dmar_iommu(iommu);
3885 free_dmar_iommu(iommu);
3886 }
3887
3888 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08003889}
3890
Jiang Liu9bdc5312014-01-06 14:18:27 +08003891static void intel_iommu_free_dmars(void)
3892{
3893 struct dmar_rmrr_unit *rmrru, *rmrr_n;
3894 struct dmar_atsr_unit *atsru, *atsr_n;
3895
3896 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
3897 list_del(&rmrru->list);
3898 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
3899 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003900 }
3901
Jiang Liu9bdc5312014-01-06 14:18:27 +08003902 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
3903 list_del(&atsru->list);
3904 intel_iommu_free_atsr(atsru);
3905 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003906}
3907
3908int dmar_find_matched_atsr_unit(struct pci_dev *dev)
3909{
Jiang Liub683b232014-02-19 14:07:32 +08003910 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003911 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00003912 struct pci_dev *bridge = NULL;
3913 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003914 struct acpi_dmar_atsr *atsr;
3915 struct dmar_atsr_unit *atsru;
3916
3917 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003918 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08003919 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003920 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08003921 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003922 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003923 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003924 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003925 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08003926 if (!bridge)
3927 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003928
Jiang Liu0e242612014-02-19 14:07:34 +08003929 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08003930 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3931 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
3932 if (atsr->segment != pci_domain_nr(dev->bus))
3933 continue;
3934
Jiang Liub683b232014-02-19 14:07:32 +08003935 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00003936 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08003937 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003938
3939 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08003940 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003941 }
Jiang Liub683b232014-02-19 14:07:32 +08003942 ret = 0;
3943out:
Jiang Liu0e242612014-02-19 14:07:34 +08003944 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003945
Jiang Liub683b232014-02-19 14:07:32 +08003946 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003947}
3948
Jiang Liu59ce0512014-02-19 14:07:35 +08003949int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
3950{
3951 int ret = 0;
3952 struct dmar_rmrr_unit *rmrru;
3953 struct dmar_atsr_unit *atsru;
3954 struct acpi_dmar_atsr *atsr;
3955 struct acpi_dmar_reserved_memory *rmrr;
3956
3957 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
3958 return 0;
3959
3960 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
3961 rmrr = container_of(rmrru->hdr,
3962 struct acpi_dmar_reserved_memory, header);
3963 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
3964 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
3965 ((void *)rmrr) + rmrr->header.length,
3966 rmrr->segment, rmrru->devices,
3967 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08003968 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08003969 return ret;
3970 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08003971 dmar_remove_dev_scope(info, rmrr->segment,
3972 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08003973 }
3974 }
3975
3976 list_for_each_entry(atsru, &dmar_atsr_units, list) {
3977 if (atsru->include_all)
3978 continue;
3979
3980 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
3981 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
3982 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
3983 (void *)atsr + atsr->header.length,
3984 atsr->segment, atsru->devices,
3985 atsru->devices_cnt);
3986 if (ret > 0)
3987 break;
3988 else if(ret < 0)
3989 return ret;
3990 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
3991 if (dmar_remove_dev_scope(info, atsr->segment,
3992 atsru->devices, atsru->devices_cnt))
3993 break;
3994 }
3995 }
3996
3997 return 0;
3998}
3999
Fenghua Yu99dcade2009-11-11 07:23:06 -08004000/*
4001 * Here we only respond to action of unbound device from driver.
4002 *
4003 * Added device is not attached to its DMAR domain here yet. That will happen
4004 * when mapping the device to iova.
4005 */
4006static int device_notifier(struct notifier_block *nb,
4007 unsigned long action, void *data)
4008{
4009 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004010 struct dmar_domain *domain;
4011
David Woodhouse3d891942014-03-06 15:59:26 +00004012 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004013 return 0;
4014
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004015 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004016 return 0;
4017
David Woodhouse1525a292014-03-06 16:19:30 +00004018 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004019 if (!domain)
4020 return 0;
4021
Jiang Liu3a5670e2014-02-19 14:07:33 +08004022 down_read(&dmar_global_lock);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004023 domain_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004024 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004025 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08004026 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07004027
Fenghua Yu99dcade2009-11-11 07:23:06 -08004028 return 0;
4029}
4030
4031static struct notifier_block device_nb = {
4032 .notifier_call = device_notifier,
4033};
4034
Jiang Liu75f05562014-02-19 14:07:37 +08004035static int intel_iommu_memory_notifier(struct notifier_block *nb,
4036 unsigned long val, void *v)
4037{
4038 struct memory_notify *mhp = v;
4039 unsigned long long start, end;
4040 unsigned long start_vpfn, last_vpfn;
4041
4042 switch (val) {
4043 case MEM_GOING_ONLINE:
4044 start = mhp->start_pfn << PAGE_SHIFT;
4045 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4046 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004047 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004048 start, end);
4049 return NOTIFY_BAD;
4050 }
4051 break;
4052
4053 case MEM_OFFLINE:
4054 case MEM_CANCEL_ONLINE:
4055 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4056 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4057 while (start_vpfn <= last_vpfn) {
4058 struct iova *iova;
4059 struct dmar_drhd_unit *drhd;
4060 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004061 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004062
4063 iova = find_iova(&si_domain->iovad, start_vpfn);
4064 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004065 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004066 start_vpfn);
4067 break;
4068 }
4069
4070 iova = split_and_remove_iova(&si_domain->iovad, iova,
4071 start_vpfn, last_vpfn);
4072 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004073 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004074 start_vpfn, last_vpfn);
4075 return NOTIFY_BAD;
4076 }
4077
David Woodhouseea8ea462014-03-05 17:09:32 +00004078 freelist = domain_unmap(si_domain, iova->pfn_lo,
4079 iova->pfn_hi);
4080
Jiang Liu75f05562014-02-19 14:07:37 +08004081 rcu_read_lock();
4082 for_each_active_iommu(iommu, drhd)
4083 iommu_flush_iotlb_psi(iommu, si_domain->id,
Jiang Liua156ef92014-07-11 14:19:36 +08004084 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004085 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004086 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004087 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004088
4089 start_vpfn = iova->pfn_hi + 1;
4090 free_iova_mem(iova);
4091 }
4092 break;
4093 }
4094
4095 return NOTIFY_OK;
4096}
4097
4098static struct notifier_block intel_iommu_memory_nb = {
4099 .notifier_call = intel_iommu_memory_notifier,
4100 .priority = 0
4101};
4102
Alex Williamsona5459cf2014-06-12 16:12:31 -06004103
4104static ssize_t intel_iommu_show_version(struct device *dev,
4105 struct device_attribute *attr,
4106 char *buf)
4107{
4108 struct intel_iommu *iommu = dev_get_drvdata(dev);
4109 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4110 return sprintf(buf, "%d:%d\n",
4111 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4112}
4113static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4114
4115static ssize_t intel_iommu_show_address(struct device *dev,
4116 struct device_attribute *attr,
4117 char *buf)
4118{
4119 struct intel_iommu *iommu = dev_get_drvdata(dev);
4120 return sprintf(buf, "%llx\n", iommu->reg_phys);
4121}
4122static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4123
4124static ssize_t intel_iommu_show_cap(struct device *dev,
4125 struct device_attribute *attr,
4126 char *buf)
4127{
4128 struct intel_iommu *iommu = dev_get_drvdata(dev);
4129 return sprintf(buf, "%llx\n", iommu->cap);
4130}
4131static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4132
4133static ssize_t intel_iommu_show_ecap(struct device *dev,
4134 struct device_attribute *attr,
4135 char *buf)
4136{
4137 struct intel_iommu *iommu = dev_get_drvdata(dev);
4138 return sprintf(buf, "%llx\n", iommu->ecap);
4139}
4140static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4141
4142static struct attribute *intel_iommu_attrs[] = {
4143 &dev_attr_version.attr,
4144 &dev_attr_address.attr,
4145 &dev_attr_cap.attr,
4146 &dev_attr_ecap.attr,
4147 NULL,
4148};
4149
4150static struct attribute_group intel_iommu_group = {
4151 .name = "intel-iommu",
4152 .attrs = intel_iommu_attrs,
4153};
4154
4155const struct attribute_group *intel_iommu_groups[] = {
4156 &intel_iommu_group,
4157 NULL,
4158};
4159
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004160int __init intel_iommu_init(void)
4161{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004162 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004163 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004164 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004165
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004166 /* VT-d is required for a TXT/tboot launch, so enforce that */
4167 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004168
Jiang Liu3a5670e2014-02-19 14:07:33 +08004169 if (iommu_init_mempool()) {
4170 if (force_on)
4171 panic("tboot: Failed to initialize iommu memory\n");
4172 return -ENOMEM;
4173 }
4174
4175 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004176 if (dmar_table_init()) {
4177 if (force_on)
4178 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004179 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004180 }
4181
Takao Indoh3a93c842013-04-23 17:35:03 +09004182 /*
4183 * Disable translation if already enabled prior to OS handover.
4184 */
Jiang Liu7c919772014-01-06 14:18:18 +08004185 for_each_active_iommu(iommu, drhd)
Takao Indoh3a93c842013-04-23 17:35:03 +09004186 if (iommu->gcmd & DMA_GCMD_TE)
4187 iommu_disable_translation(iommu);
Takao Indoh3a93c842013-04-23 17:35:03 +09004188
Suresh Siddhac2c72862011-08-23 17:05:19 -07004189 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004190 if (force_on)
4191 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004192 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004193 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004194
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004195 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004196 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004197
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004198 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004199 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004200
4201 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004202 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004203
Joseph Cihula51a63e62011-03-21 11:04:24 -07004204 if (dmar_init_reserved_ranges()) {
4205 if (force_on)
4206 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004207 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004208 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004209
4210 init_no_remapping_devices();
4211
Joseph Cihulab7792602011-05-03 00:08:37 -07004212 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004213 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004214 if (force_on)
4215 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004216 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004217 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004218 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004219 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004220 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004221
mark gross5e0d2a62008-03-04 15:22:08 -08004222 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004223#ifdef CONFIG_SWIOTLB
4224 swiotlb = 0;
4225#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004226 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004227
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004228 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004229
Alex Williamsona5459cf2014-06-12 16:12:31 -06004230 for_each_active_iommu(iommu, drhd)
4231 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4232 intel_iommu_groups,
4233 iommu->name);
4234
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004235 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004236 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004237 if (si_domain && !hw_pass_through)
4238 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004239
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004240 intel_iommu_enabled = 1;
4241
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004242 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004243
4244out_free_reserved_range:
4245 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004246out_free_dmar:
4247 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004248 up_write(&dmar_global_lock);
4249 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004250 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004251}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004252
Alex Williamson579305f2014-07-03 09:51:43 -06004253static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque)
4254{
4255 struct intel_iommu *iommu = opaque;
4256
4257 iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff);
4258 return 0;
4259}
4260
4261/*
4262 * NB - intel-iommu lacks any sort of reference counting for the users of
4263 * dependent devices. If multiple endpoints have intersecting dependent
4264 * devices, unbinding the driver from any one of them will possibly leave
4265 * the others unable to operate.
4266 */
Han, Weidong3199aa62009-02-26 17:31:12 +08004267static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004268 struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004269{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004270 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004271 return;
4272
Alex Williamson579305f2014-07-03 09:51:43 -06004273 pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004274}
4275
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004276static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004277 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004278{
Yijing Wangbca2b912013-10-31 17:26:04 +08004279 struct device_domain_info *info, *tmp;
Weidong Hanc7151a82008-12-08 22:51:37 +08004280 struct intel_iommu *iommu;
4281 unsigned long flags;
Quentin Lambert2f119c72015-02-06 10:59:53 +01004282 bool found = false;
David Woodhouse156baca2014-03-09 14:00:57 -07004283 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004284
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004285 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004286 if (!iommu)
4287 return;
4288
4289 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wangbca2b912013-10-31 17:26:04 +08004290 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004291 if (info->iommu == iommu && info->bus == bus &&
4292 info->devfn == devfn) {
David Woodhouse109b9b02012-05-25 17:43:02 +01004293 unlink_domain_info(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004294 spin_unlock_irqrestore(&device_domain_lock, flags);
4295
Yu Zhao93a23a72009-05-18 13:51:37 +08004296 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004297 iommu_detach_dev(iommu, info->bus, info->devfn);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004298 iommu_detach_dependent_devices(iommu, dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08004299 free_devinfo_mem(info);
4300
4301 spin_lock_irqsave(&device_domain_lock, flags);
4302
4303 if (found)
4304 break;
4305 else
4306 continue;
4307 }
4308
4309 /* if there is no other devices under the same iommu
4310 * owned by this domain, clear this iommu in iommu_bmp
4311 * update iommu count and coherency
4312 */
David Woodhouse8bbc4412014-03-09 13:52:37 -07004313 if (info->iommu == iommu)
Quentin Lambert2f119c72015-02-06 10:59:53 +01004314 found = true;
Weidong Hanc7151a82008-12-08 22:51:37 +08004315 }
4316
Roland Dreier3e7abe22011-07-20 06:22:21 -07004317 spin_unlock_irqrestore(&device_domain_lock, flags);
4318
Weidong Hanc7151a82008-12-08 22:51:37 +08004319 if (found == 0) {
Jiang Liufb170fb2014-07-11 14:19:28 +08004320 domain_detach_iommu(domain, iommu);
4321 if (!domain_type_is_vm_or_si(domain))
4322 iommu_detach_domain(domain, iommu);
Weidong Hanc7151a82008-12-08 22:51:37 +08004323 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004324}
4325
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004326static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004327{
4328 int adjust_width;
4329
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004330 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4331 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004332 domain_reserve_special_ranges(domain);
4333
4334 /* calculate AGAW */
4335 domain->gaw = guest_width;
4336 adjust_width = guestwidth_to_adjustwidth(guest_width);
4337 domain->agaw = width_to_agaw(adjust_width);
4338
Weidong Han5e98c4b2008-12-08 23:03:27 +08004339 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004340 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004341 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004342 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004343
4344 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004345 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004346 if (!domain->pgd)
4347 return -ENOMEM;
4348 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4349 return 0;
4350}
4351
Joerg Roedel00a77de2015-03-26 13:43:08 +01004352static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004353{
Joerg Roedel5d450802008-12-03 14:52:32 +01004354 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004355 struct iommu_domain *domain;
4356
4357 if (type != IOMMU_DOMAIN_UNMANAGED)
4358 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004359
Jiang Liuab8dfe22014-07-11 14:19:27 +08004360 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004361 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004362 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004363 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004364 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004365 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004366 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004367 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004368 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004369 }
Allen Kay8140a952011-10-14 12:32:17 -07004370 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004371
Joerg Roedel00a77de2015-03-26 13:43:08 +01004372 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004373 domain->geometry.aperture_start = 0;
4374 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4375 domain->geometry.force_aperture = true;
4376
Joerg Roedel00a77de2015-03-26 13:43:08 +01004377 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004378}
Kay, Allen M38717942008-09-09 18:37:29 +03004379
Joerg Roedel00a77de2015-03-26 13:43:08 +01004380static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004381{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004382 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004383}
Kay, Allen M38717942008-09-09 18:37:29 +03004384
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004385static int intel_iommu_attach_device(struct iommu_domain *domain,
4386 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004387{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004388 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004389 struct intel_iommu *iommu;
4390 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004391 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004392
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004393 if (device_is_rmrr_locked(dev)) {
4394 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4395 return -EPERM;
4396 }
4397
David Woodhouse7207d8f2014-03-09 16:31:06 -07004398 /* normally dev is not mapped */
4399 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004400 struct dmar_domain *old_domain;
4401
David Woodhouse1525a292014-03-06 16:19:30 +00004402 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004403 if (old_domain) {
Jiang Liuab8dfe22014-07-11 14:19:27 +08004404 if (domain_type_is_vm_or_si(dmar_domain))
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004405 domain_remove_one_dev_info(old_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004406 else
4407 domain_remove_dev_info(old_domain);
Joerg Roedel62c22162014-12-09 12:56:45 +01004408
4409 if (!domain_type_is_vm_or_si(old_domain) &&
4410 list_empty(&old_domain->devices))
4411 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004412 }
4413 }
4414
David Woodhouse156baca2014-03-09 14:00:57 -07004415 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004416 if (!iommu)
4417 return -ENODEV;
4418
4419 /* check if this iommu agaw is sufficient for max mapped address */
4420 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004421 if (addr_width > cap_mgaw(iommu->cap))
4422 addr_width = cap_mgaw(iommu->cap);
4423
4424 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004425 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004426 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004427 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004428 return -EFAULT;
4429 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004430 dmar_domain->gaw = addr_width;
4431
4432 /*
4433 * Knock out extra levels of page tables if necessary
4434 */
4435 while (iommu->agaw < dmar_domain->agaw) {
4436 struct dma_pte *pte;
4437
4438 pte = dmar_domain->pgd;
4439 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004440 dmar_domain->pgd = (struct dma_pte *)
4441 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004442 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004443 }
4444 dmar_domain->agaw--;
4445 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004446
David Woodhouse5913c9b2014-03-09 16:27:31 -07004447 return domain_add_dev_info(dmar_domain, dev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004448}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004449
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004450static void intel_iommu_detach_device(struct iommu_domain *domain,
4451 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004452{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004453 domain_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004454}
Kay, Allen M38717942008-09-09 18:37:29 +03004455
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004456static int intel_iommu_map(struct iommu_domain *domain,
4457 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004458 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004459{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004460 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004461 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004462 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004463 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004464
Joerg Roedeldde57a22008-12-03 15:04:09 +01004465 if (iommu_prot & IOMMU_READ)
4466 prot |= DMA_PTE_READ;
4467 if (iommu_prot & IOMMU_WRITE)
4468 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08004469 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4470 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004471
David Woodhouse163cc522009-06-28 00:51:17 +01004472 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004473 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004474 u64 end;
4475
4476 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004477 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004478 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004479 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004480 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004481 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004482 return -EFAULT;
4483 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004484 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004485 }
David Woodhousead051222009-06-28 14:22:28 +01004486 /* Round up size to next multiple of PAGE_SIZE, if it and
4487 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004488 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004489 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4490 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004491 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004492}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004493
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004494static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004495 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004496{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004497 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004498 struct page *freelist = NULL;
4499 struct intel_iommu *iommu;
4500 unsigned long start_pfn, last_pfn;
4501 unsigned int npages;
4502 int iommu_id, num, ndomains, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004503
David Woodhouse5cf0a762014-03-19 16:07:49 +00004504 /* Cope with horrid API which requires us to unmap more than the
4505 size argument if it happens to be a large-page mapping. */
4506 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4507 BUG();
4508
4509 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4510 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4511
David Woodhouseea8ea462014-03-05 17:09:32 +00004512 start_pfn = iova >> VTD_PAGE_SHIFT;
4513 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4514
4515 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4516
4517 npages = last_pfn - start_pfn + 1;
4518
4519 for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) {
4520 iommu = g_iommus[iommu_id];
4521
4522 /*
4523 * find bit position of dmar_domain
4524 */
4525 ndomains = cap_ndoms(iommu->cap);
4526 for_each_set_bit(num, iommu->domain_ids, ndomains) {
4527 if (iommu->domains[num] == dmar_domain)
4528 iommu_flush_iotlb_psi(iommu, num, start_pfn,
4529 npages, !freelist, 0);
4530 }
4531
4532 }
4533
4534 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004535
David Woodhouse163cc522009-06-28 00:51:17 +01004536 if (dmar_domain->max_addr == iova + size)
4537 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004538
David Woodhouse5cf0a762014-03-19 16:07:49 +00004539 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004540}
Kay, Allen M38717942008-09-09 18:37:29 +03004541
Joerg Roedeld14d6572008-12-03 15:06:57 +01004542static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05304543 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004544{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004545 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004546 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004547 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004548 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004549
David Woodhouse5cf0a762014-03-19 16:07:49 +00004550 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004551 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004552 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004553
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004554 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004555}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004556
Joerg Roedel5d587b82014-09-05 10:50:45 +02004557static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004558{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004559 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004560 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004561 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004562 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004563
Joerg Roedel5d587b82014-09-05 10:50:45 +02004564 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004565}
4566
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004567static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004568{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004569 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004570 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004571 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004572
Alex Williamsona5459cf2014-06-12 16:12:31 -06004573 iommu = device_to_iommu(dev, &bus, &devfn);
4574 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004575 return -ENODEV;
4576
Alex Williamsona5459cf2014-06-12 16:12:31 -06004577 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004578
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004579 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004580
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004581 if (IS_ERR(group))
4582 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004583
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004584 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004585 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004586}
4587
4588static void intel_iommu_remove_device(struct device *dev)
4589{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004590 struct intel_iommu *iommu;
4591 u8 bus, devfn;
4592
4593 iommu = device_to_iommu(dev, &bus, &devfn);
4594 if (!iommu)
4595 return;
4596
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004597 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004598
4599 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004600}
4601
Thierry Redingb22f6432014-06-27 09:03:12 +02004602static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004603 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004604 .domain_alloc = intel_iommu_domain_alloc,
4605 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004606 .attach_dev = intel_iommu_attach_device,
4607 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004608 .map = intel_iommu_map,
4609 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004610 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004611 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004612 .add_device = intel_iommu_add_device,
4613 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004614 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004615};
David Woodhouse9af88142009-02-13 23:18:03 +00004616
Daniel Vetter94526182013-01-20 23:50:13 +01004617static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4618{
4619 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004620 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004621 dmar_map_gfx = 0;
4622}
4623
4624DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4625DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4626DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4627DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4628DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4629DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4630DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4631
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004632static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004633{
4634 /*
4635 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004636 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004637 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004638 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00004639 rwbf_quirk = 1;
4640}
4641
4642DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004643DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4644DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4645DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4646DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4647DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4648DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004649
Adam Jacksoneecfd572010-08-25 21:17:34 +01004650#define GGC 0x52
4651#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4652#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4653#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4654#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4655#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4656#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4657#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4658#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4659
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004660static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004661{
4662 unsigned short ggc;
4663
Adam Jacksoneecfd572010-08-25 21:17:34 +01004664 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004665 return;
4666
Adam Jacksoneecfd572010-08-25 21:17:34 +01004667 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004668 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01004669 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004670 } else if (dmar_map_gfx) {
4671 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004672 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004673 intel_iommu_strict = 1;
4674 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01004675}
4676DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
4677DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
4678DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
4679DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
4680
David Woodhousee0fc7e02009-09-30 09:12:17 -07004681/* On Tylersburg chipsets, some BIOSes have been known to enable the
4682 ISOCH DMAR unit for the Azalia sound device, but not give it any
4683 TLB entries, which causes it to deadlock. Check for that. We do
4684 this in a function called from init_dmars(), instead of in a PCI
4685 quirk, because we don't want to print the obnoxious "BIOS broken"
4686 message if VT-d is actually disabled.
4687*/
4688static void __init check_tylersburg_isoch(void)
4689{
4690 struct pci_dev *pdev;
4691 uint32_t vtisochctrl;
4692
4693 /* If there's no Azalia in the system anyway, forget it. */
4694 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
4695 if (!pdev)
4696 return;
4697 pci_dev_put(pdev);
4698
4699 /* System Management Registers. Might be hidden, in which case
4700 we can't do the sanity check. But that's OK, because the
4701 known-broken BIOSes _don't_ actually hide it, so far. */
4702 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
4703 if (!pdev)
4704 return;
4705
4706 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
4707 pci_dev_put(pdev);
4708 return;
4709 }
4710
4711 pci_dev_put(pdev);
4712
4713 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
4714 if (vtisochctrl & 1)
4715 return;
4716
4717 /* Drop all bits other than the number of TLB entries */
4718 vtisochctrl &= 0x1c;
4719
4720 /* If we have the recommended number of TLB entries (16), fine. */
4721 if (vtisochctrl == 0x10)
4722 return;
4723
4724 /* Zero TLB entries? You get to ride the short bus to school. */
4725 if (!vtisochctrl) {
4726 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
4727 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
4728 dmi_get_system_info(DMI_BIOS_VENDOR),
4729 dmi_get_system_info(DMI_BIOS_VERSION),
4730 dmi_get_system_info(DMI_PRODUCT_VERSION));
4731 iommu_identity_mapping |= IDENTMAP_AZALIA;
4732 return;
4733 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004734
4735 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07004736 vtisochctrl);
4737}