blob: 52250b3e58d171b90200818432ade6ba4404a415 [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>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070018 */
19
20#include <linux/init.h>
21#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080022#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040023#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070024#include <linux/slab.h>
25#include <linux/irq.h>
26#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/spinlock.h>
28#include <linux/pci.h>
29#include <linux/dmar.h>
30#include <linux/dma-mapping.h>
31#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080032#include <linux/memory.h>
mark gross5e0d2a62008-03-04 15:22:08 -080033#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030034#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010035#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030036#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010037#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070038#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100039#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020040#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080041#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070042#include <linux/dma-contiguous.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070043#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070044#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090045#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070046
Joerg Roedel078e1ee2012-09-26 12:44:43 +020047#include "irq_remapping.h"
48
Fenghua Yu5b6985c2008-10-16 18:02:32 -070049#define ROOT_SIZE VTD_PAGE_SIZE
50#define CONTEXT_SIZE VTD_PAGE_SIZE
51
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070052#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
53#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070054#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070055
56#define IOAPIC_RANGE_START (0xfee00000)
57#define IOAPIC_RANGE_END (0xfeefffff)
58#define IOVA_START_ADDR (0x1000)
59
60#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
61
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070062#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080063#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070064
David Woodhouse2ebe3152009-09-19 07:34:04 -070065#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
66#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
67
68/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
69 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
70#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
71 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
72#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070073
Mark McLoughlinf27be032008-11-20 15:49:43 +000074#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070075#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070076#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080077
Andrew Mortondf08cdc2010-09-22 13:05:11 -070078/* page table handling */
79#define LEVEL_STRIDE (9)
80#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
81
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020082/*
83 * This bitmap is used to advertise the page sizes our hardware support
84 * to the IOMMU core, which will then use this information to split
85 * physically contiguous memory regions it is mapping into page sizes
86 * that we support.
87 *
88 * Traditionally the IOMMU core just handed us the mappings directly,
89 * after making sure the size is an order of a 4KiB page and that the
90 * mapping has natural alignment.
91 *
92 * To retain this behavior, we currently advertise that we support
93 * all page sizes that are an order of 4KiB.
94 *
95 * If at some point we'd like to utilize the IOMMU core's new behavior,
96 * we could change this to advertise the real page sizes we support.
97 */
98#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
99
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700100static inline int agaw_to_level(int agaw)
101{
102 return agaw + 2;
103}
104
105static inline int agaw_to_width(int agaw)
106{
Jiang Liu5c645b32014-01-06 14:18:12 +0800107 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700108}
109
110static inline int width_to_agaw(int width)
111{
Jiang Liu5c645b32014-01-06 14:18:12 +0800112 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700113}
114
115static inline unsigned int level_to_offset_bits(int level)
116{
117 return (level - 1) * LEVEL_STRIDE;
118}
119
120static inline int pfn_level_offset(unsigned long pfn, int level)
121{
122 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
123}
124
125static inline unsigned long level_mask(int level)
126{
127 return -1UL << level_to_offset_bits(level);
128}
129
130static inline unsigned long level_size(int level)
131{
132 return 1UL << level_to_offset_bits(level);
133}
134
135static inline unsigned long align_to_level(unsigned long pfn, int level)
136{
137 return (pfn + level_size(level) - 1) & level_mask(level);
138}
David Woodhousefd18de52009-05-10 23:57:41 +0100139
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100140static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
141{
Jiang Liu5c645b32014-01-06 14:18:12 +0800142 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100143}
144
David Woodhousedd4e8312009-06-27 16:21:20 +0100145/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
146 are never going to work. */
147static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
148{
149 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
150}
151
152static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
153{
154 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
155}
156static inline unsigned long page_to_dma_pfn(struct page *pg)
157{
158 return mm_to_dma_pfn(page_to_pfn(pg));
159}
160static inline unsigned long virt_to_dma_pfn(void *p)
161{
162 return page_to_dma_pfn(virt_to_page(p));
163}
164
Weidong Hand9630fe2008-12-08 11:06:32 +0800165/* global iommu list, set NULL for ignored DMAR units */
166static struct intel_iommu **g_iommus;
167
David Woodhousee0fc7e02009-09-30 09:12:17 -0700168static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000169static int rwbf_quirk;
170
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000171/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700172 * set to 1 to panic kernel if can't successfully enable VT-d
173 * (used when kernel is launched w/ TXT)
174 */
175static int force_on = 0;
176
177/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000178 * 0: Present
179 * 1-11: Reserved
180 * 12-63: Context Ptr (12 - (haw-1))
181 * 64-127: Reserved
182 */
183struct root_entry {
184 u64 val;
185 u64 rsvd1;
186};
187#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
188static inline bool root_present(struct root_entry *root)
189{
190 return (root->val & 1);
191}
192static inline void set_root_present(struct root_entry *root)
193{
194 root->val |= 1;
195}
196static inline void set_root_value(struct root_entry *root, unsigned long value)
197{
198 root->val |= value & VTD_PAGE_MASK;
199}
200
201static inline struct context_entry *
202get_context_addr_from_root(struct root_entry *root)
203{
204 return (struct context_entry *)
205 (root_present(root)?phys_to_virt(
206 root->val & VTD_PAGE_MASK) :
207 NULL);
208}
209
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000210/*
211 * low 64 bits:
212 * 0: present
213 * 1: fault processing disable
214 * 2-3: translation type
215 * 12-63: address space root
216 * high 64 bits:
217 * 0-2: address width
218 * 3-6: aval
219 * 8-23: domain id
220 */
221struct context_entry {
222 u64 lo;
223 u64 hi;
224};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000225
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000226static inline bool context_present(struct context_entry *context)
227{
228 return (context->lo & 1);
229}
230static inline void context_set_present(struct context_entry *context)
231{
232 context->lo |= 1;
233}
234
235static inline void context_set_fault_enable(struct context_entry *context)
236{
237 context->lo &= (((u64)-1) << 2) | 1;
238}
239
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000240static inline void context_set_translation_type(struct context_entry *context,
241 unsigned long value)
242{
243 context->lo &= (((u64)-1) << 4) | 3;
244 context->lo |= (value & 3) << 2;
245}
246
247static inline void context_set_address_root(struct context_entry *context,
248 unsigned long value)
249{
250 context->lo |= value & VTD_PAGE_MASK;
251}
252
253static inline void context_set_address_width(struct context_entry *context,
254 unsigned long value)
255{
256 context->hi |= value & 7;
257}
258
259static inline void context_set_domain_id(struct context_entry *context,
260 unsigned long value)
261{
262 context->hi |= (value & ((1 << 16) - 1)) << 8;
263}
264
265static inline void context_clear_entry(struct context_entry *context)
266{
267 context->lo = 0;
268 context->hi = 0;
269}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000270
Mark McLoughlin622ba122008-11-20 15:49:46 +0000271/*
272 * 0: readable
273 * 1: writable
274 * 2-6: reserved
275 * 7: super page
Sheng Yang9cf066972009-03-18 15:33:07 +0800276 * 8-10: available
277 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000278 * 12-63: Host physcial address
279 */
280struct dma_pte {
281 u64 val;
282};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000283
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000284static inline void dma_clear_pte(struct dma_pte *pte)
285{
286 pte->val = 0;
287}
288
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000289static inline u64 dma_pte_addr(struct dma_pte *pte)
290{
David Woodhousec85994e2009-07-01 19:21:24 +0100291#ifdef CONFIG_64BIT
292 return pte->val & VTD_PAGE_MASK;
293#else
294 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100295 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100296#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000297}
298
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000299static inline bool dma_pte_present(struct dma_pte *pte)
300{
301 return (pte->val & 3) != 0;
302}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000303
Allen Kay4399c8b2011-10-14 12:32:46 -0700304static inline bool dma_pte_superpage(struct dma_pte *pte)
305{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200306 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700307}
308
David Woodhouse75e6bf92009-07-02 11:21:16 +0100309static inline int first_pte_in_page(struct dma_pte *pte)
310{
311 return !((unsigned long)pte & ~VTD_PAGE_MASK);
312}
313
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700314/*
315 * This domain is a statically identity mapping domain.
316 * 1. This domain creats a static 1:1 mapping to all usable memory.
317 * 2. It maps to each iommu if successful.
318 * 3. Each iommu mapps to this domain if successful.
319 */
David Woodhouse19943b02009-08-04 16:19:20 +0100320static struct dmar_domain *si_domain;
321static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700322
Weidong Han1ce28fe2008-12-08 16:35:39 +0800323/* domain represents a virtual machine, more than one devices
324 * across iommus may be owned in one domain, e.g. kvm guest.
325 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800326#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800327
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700328/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800329#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700330
Mike Travis1b198bb2012-03-05 15:05:16 -0800331/* define the limit of IOMMUs supported in each domain */
332#ifdef CONFIG_X86
333# define IOMMU_UNITS_SUPPORTED MAX_IO_APICS
334#else
335# define IOMMU_UNITS_SUPPORTED 64
336#endif
337
Mark McLoughlin99126f72008-11-20 15:49:47 +0000338struct dmar_domain {
339 int id; /* domain id */
Suresh Siddha4c923d42009-10-02 11:01:24 -0700340 int nid; /* node id */
Mike Travis1b198bb2012-03-05 15:05:16 -0800341 DECLARE_BITMAP(iommu_bmp, IOMMU_UNITS_SUPPORTED);
342 /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000343
344 struct list_head devices; /* all devices' list */
345 struct iova_domain iovad; /* iova's that belong to this domain */
346
347 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000348 int gaw; /* max guest address width */
349
350 /* adjusted guest address width, 0 is level 2 30-bit */
351 int agaw;
352
Weidong Han3b5410e2008-12-08 09:17:15 +0800353 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800354
355 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800356 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800357 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100358 int iommu_superpage;/* Level of superpages supported:
359 0 == 4KiB (no superpages), 1 == 2MiB,
360 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanc7151a82008-12-08 22:51:37 +0800361 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800362 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000363};
364
Mark McLoughlina647dac2008-11-20 15:49:48 +0000365/* PCI domain-device relationship */
366struct device_domain_info {
367 struct list_head link; /* link to domain siblings */
368 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100369 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000370 u8 devfn; /* PCI devfn number */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000371 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800372 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000373 struct dmar_domain *domain; /* pointer to domain */
374};
375
Jiang Liub94e4112014-02-19 14:07:25 +0800376struct dmar_rmrr_unit {
377 struct list_head list; /* list of rmrr units */
378 struct acpi_dmar_header *hdr; /* ACPI header */
379 u64 base_address; /* reserved base address*/
380 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000381 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800382 int devices_cnt; /* target device count */
383};
384
385struct dmar_atsr_unit {
386 struct list_head list; /* list of ATSR units */
387 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000388 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800389 int devices_cnt; /* target device count */
390 u8 include_all:1; /* include all ports */
391};
392
393static LIST_HEAD(dmar_atsr_units);
394static LIST_HEAD(dmar_rmrr_units);
395
396#define for_each_rmrr_units(rmrr) \
397 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
398
mark gross5e0d2a62008-03-04 15:22:08 -0800399static void flush_unmaps_timeout(unsigned long data);
400
Jiang Liub707cb02014-01-06 14:18:26 +0800401static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800402
mark gross80b20dd2008-04-18 13:53:58 -0700403#define HIGH_WATER_MARK 250
404struct deferred_flush_tables {
405 int next;
406 struct iova *iova[HIGH_WATER_MARK];
407 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000408 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700409};
410
411static struct deferred_flush_tables *deferred_flush;
412
mark gross5e0d2a62008-03-04 15:22:08 -0800413/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800414static int g_num_of_iommus;
415
416static DEFINE_SPINLOCK(async_umap_flush_lock);
417static LIST_HEAD(unmaps_to_do);
418
419static int timer_on;
420static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800421
Jiang Liu92d03cc2014-02-19 14:07:28 +0800422static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700423static void domain_remove_dev_info(struct dmar_domain *domain);
Jiang Liub94e4112014-02-19 14:07:25 +0800424static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -0700425 struct device *dev);
Jiang Liu92d03cc2014-02-19 14:07:28 +0800426static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000427 struct device *dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700428
Suresh Siddhad3f13812011-08-23 17:05:25 -0700429#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800430int dmar_disabled = 0;
431#else
432int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700433#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800434
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200435int intel_iommu_enabled = 0;
436EXPORT_SYMBOL_GPL(intel_iommu_enabled);
437
David Woodhouse2d9e6672010-06-15 10:57:57 +0100438static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700439static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800440static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100441static int intel_iommu_superpage = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700442
David Woodhousec0771df2011-10-14 20:59:46 +0100443int intel_iommu_gfx_mapped;
444EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
445
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700446#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
447static DEFINE_SPINLOCK(device_domain_lock);
448static LIST_HEAD(device_domain_list);
449
Thierry Redingb22f6432014-06-27 09:03:12 +0200450static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100451
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;
459 printk(KERN_INFO "Intel-IOMMU: enabled\n");
460 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700461 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800462 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700463 } else if (!strncmp(str, "igfx_off", 8)) {
464 dmar_map_gfx = 0;
465 printk(KERN_INFO
466 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700467 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800468 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700469 "Intel-IOMMU: Forcing DAC for PCI devices\n");
470 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800471 } else if (!strncmp(str, "strict", 6)) {
472 printk(KERN_INFO
473 "Intel-IOMMU: disable batched IOTLB flush\n");
474 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100475 } else if (!strncmp(str, "sp_off", 6)) {
476 printk(KERN_INFO
477 "Intel-IOMMU: disable supported super page\n");
478 intel_iommu_superpage = 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;
491static struct kmem_cache *iommu_iova_cache;
492
Suresh Siddha4c923d42009-10-02 11:01:24 -0700493static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700494{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700495 struct page *page;
496 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700497
Suresh Siddha4c923d42009-10-02 11:01:24 -0700498 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
499 if (page)
500 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700501 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700502}
503
504static inline void free_pgtable_page(void *vaddr)
505{
506 free_page((unsigned long)vaddr);
507}
508
509static inline void *alloc_domain_mem(void)
510{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900511 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700512}
513
Kay, Allen M38717942008-09-09 18:37:29 +0300514static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700515{
516 kmem_cache_free(iommu_domain_cache, vaddr);
517}
518
519static inline void * alloc_devinfo_mem(void)
520{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900521 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700522}
523
524static inline void free_devinfo_mem(void *vaddr)
525{
526 kmem_cache_free(iommu_devinfo_cache, vaddr);
527}
528
529struct iova *alloc_iova_mem(void)
530{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900531 return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700532}
533
534void free_iova_mem(struct iova *iova)
535{
536 kmem_cache_free(iommu_iova_cache, iova);
537}
538
Jiang Liuab8dfe22014-07-11 14:19:27 +0800539static inline int domain_type_is_vm(struct dmar_domain *domain)
540{
541 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
542}
543
544static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
545{
546 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
547 DOMAIN_FLAG_STATIC_IDENTITY);
548}
Weidong Han1b573682008-12-08 15:34:06 +0800549
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700550static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800551{
552 unsigned long sagaw;
553 int agaw = -1;
554
555 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700556 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800557 agaw >= 0; agaw--) {
558 if (test_bit(agaw, &sagaw))
559 break;
560 }
561
562 return agaw;
563}
564
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700565/*
566 * Calculate max SAGAW for each iommu.
567 */
568int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
569{
570 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
571}
572
573/*
574 * calculate agaw for each iommu.
575 * "SAGAW" may be different across iommus, use a default agaw, and
576 * get a supported less agaw for iommus that don't support the default agaw.
577 */
578int iommu_calculate_agaw(struct intel_iommu *iommu)
579{
580 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
581}
582
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700583/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800584static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
585{
586 int iommu_id;
587
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700588 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800589 BUG_ON(domain_type_is_vm_or_si(domain));
Mike Travis1b198bb2012-03-05 15:05:16 -0800590 iommu_id = find_first_bit(domain->iommu_bmp, g_num_of_iommus);
Weidong Han8c11e792008-12-08 15:29:22 +0800591 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
592 return NULL;
593
594 return g_iommus[iommu_id];
595}
596
Weidong Han8e6040972008-12-08 15:49:06 +0800597static void domain_update_iommu_coherency(struct dmar_domain *domain)
598{
David Woodhoused0501962014-03-11 17:10:29 -0700599 struct dmar_drhd_unit *drhd;
600 struct intel_iommu *iommu;
601 int i, found = 0;
Weidong Han8e6040972008-12-08 15:49:06 +0800602
David Woodhoused0501962014-03-11 17:10:29 -0700603 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800604
Mike Travis1b198bb2012-03-05 15:05:16 -0800605 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) {
David Woodhoused0501962014-03-11 17:10:29 -0700606 found = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800607 if (!ecap_coherent(g_iommus[i]->ecap)) {
608 domain->iommu_coherency = 0;
609 break;
610 }
Weidong Han8e6040972008-12-08 15:49:06 +0800611 }
David Woodhoused0501962014-03-11 17:10:29 -0700612 if (found)
613 return;
614
615 /* No hardware attached; use lowest common denominator */
616 rcu_read_lock();
617 for_each_active_iommu(iommu, drhd) {
618 if (!ecap_coherent(iommu->ecap)) {
619 domain->iommu_coherency = 0;
620 break;
621 }
622 }
623 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800624}
625
Sheng Yang58c610b2009-03-18 15:33:05 +0800626static void domain_update_iommu_snooping(struct dmar_domain *domain)
627{
628 int i;
629
630 domain->iommu_snooping = 1;
631
Mike Travis1b198bb2012-03-05 15:05:16 -0800632 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) {
Sheng Yang58c610b2009-03-18 15:33:05 +0800633 if (!ecap_sc_support(g_iommus[i]->ecap)) {
634 domain->iommu_snooping = 0;
635 break;
636 }
Sheng Yang58c610b2009-03-18 15:33:05 +0800637 }
638}
639
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100640static void domain_update_iommu_superpage(struct dmar_domain *domain)
641{
Allen Kay8140a952011-10-14 12:32:17 -0700642 struct dmar_drhd_unit *drhd;
643 struct intel_iommu *iommu = NULL;
644 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100645
646 if (!intel_iommu_superpage) {
647 domain->iommu_superpage = 0;
648 return;
649 }
650
Allen Kay8140a952011-10-14 12:32:17 -0700651 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800652 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700653 for_each_active_iommu(iommu, drhd) {
654 mask &= cap_super_page_val(iommu->cap);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100655 if (!mask) {
656 break;
657 }
658 }
Jiang Liu0e242612014-02-19 14:07:34 +0800659 rcu_read_unlock();
660
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100661 domain->iommu_superpage = fls(mask);
662}
663
Sheng Yang58c610b2009-03-18 15:33:05 +0800664/* Some capabilities may be different across iommus */
665static void domain_update_iommu_cap(struct dmar_domain *domain)
666{
667 domain_update_iommu_coherency(domain);
668 domain_update_iommu_snooping(domain);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100669 domain_update_iommu_superpage(domain);
Sheng Yang58c610b2009-03-18 15:33:05 +0800670}
671
David Woodhouse156baca2014-03-09 14:00:57 -0700672static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800673{
674 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800675 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700676 struct device *tmp;
677 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800678 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800679 int i;
680
David Woodhouse156baca2014-03-09 14:00:57 -0700681 if (dev_is_pci(dev)) {
682 pdev = to_pci_dev(dev);
683 segment = pci_domain_nr(pdev->bus);
684 } else if (ACPI_COMPANION(dev))
685 dev = &ACPI_COMPANION(dev)->dev;
686
Jiang Liu0e242612014-02-19 14:07:34 +0800687 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800688 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700689 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100690 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800691
Jiang Liub683b232014-02-19 14:07:32 +0800692 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700693 drhd->devices_cnt, i, tmp) {
694 if (tmp == dev) {
695 *bus = drhd->devices[i].bus;
696 *devfn = drhd->devices[i].devfn;
697 goto out;
698 }
699
700 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000701 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700702
703 ptmp = to_pci_dev(tmp);
704 if (ptmp->subordinate &&
705 ptmp->subordinate->number <= pdev->bus->number &&
706 ptmp->subordinate->busn_res.end >= pdev->bus->number)
707 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100708 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800709
David Woodhouse156baca2014-03-09 14:00:57 -0700710 if (pdev && drhd->include_all) {
711 got_pdev:
712 *bus = pdev->bus->number;
713 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800714 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700715 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800716 }
Jiang Liub683b232014-02-19 14:07:32 +0800717 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700718 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800719 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800720
Jiang Liub683b232014-02-19 14:07:32 +0800721 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800722}
723
Weidong Han5331fe62008-12-08 23:00:00 +0800724static void domain_flush_cache(struct dmar_domain *domain,
725 void *addr, int size)
726{
727 if (!domain->iommu_coherency)
728 clflush_cache_range(addr, size);
729}
730
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700731/* Gets context entry for a given bus and devfn */
732static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
733 u8 bus, u8 devfn)
734{
735 struct root_entry *root;
736 struct context_entry *context;
737 unsigned long phy_addr;
738 unsigned long flags;
739
740 spin_lock_irqsave(&iommu->lock, flags);
741 root = &iommu->root_entry[bus];
742 context = get_context_addr_from_root(root);
743 if (!context) {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700744 context = (struct context_entry *)
745 alloc_pgtable_page(iommu->node);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700746 if (!context) {
747 spin_unlock_irqrestore(&iommu->lock, flags);
748 return NULL;
749 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700750 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700751 phy_addr = virt_to_phys((void *)context);
752 set_root_value(root, phy_addr);
753 set_root_present(root);
754 __iommu_flush_cache(iommu, root, sizeof(*root));
755 }
756 spin_unlock_irqrestore(&iommu->lock, flags);
757 return &context[devfn];
758}
759
760static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
761{
762 struct root_entry *root;
763 struct context_entry *context;
764 int ret;
765 unsigned long flags;
766
767 spin_lock_irqsave(&iommu->lock, flags);
768 root = &iommu->root_entry[bus];
769 context = get_context_addr_from_root(root);
770 if (!context) {
771 ret = 0;
772 goto out;
773 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000774 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700775out:
776 spin_unlock_irqrestore(&iommu->lock, flags);
777 return ret;
778}
779
780static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
781{
782 struct root_entry *root;
783 struct context_entry *context;
784 unsigned long flags;
785
786 spin_lock_irqsave(&iommu->lock, flags);
787 root = &iommu->root_entry[bus];
788 context = get_context_addr_from_root(root);
789 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000790 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700791 __iommu_flush_cache(iommu, &context[devfn], \
792 sizeof(*context));
793 }
794 spin_unlock_irqrestore(&iommu->lock, flags);
795}
796
797static void free_context_table(struct intel_iommu *iommu)
798{
799 struct root_entry *root;
800 int i;
801 unsigned long flags;
802 struct context_entry *context;
803
804 spin_lock_irqsave(&iommu->lock, flags);
805 if (!iommu->root_entry) {
806 goto out;
807 }
808 for (i = 0; i < ROOT_ENTRY_NR; i++) {
809 root = &iommu->root_entry[i];
810 context = get_context_addr_from_root(root);
811 if (context)
812 free_pgtable_page(context);
813 }
814 free_pgtable_page(iommu->root_entry);
815 iommu->root_entry = NULL;
816out:
817 spin_unlock_irqrestore(&iommu->lock, flags);
818}
819
David Woodhouseb026fd22009-06-28 10:37:25 +0100820static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000821 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700822{
David Woodhouseb026fd22009-06-28 10:37:25 +0100823 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700824 struct dma_pte *parent, *pte = NULL;
825 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700826 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700827
828 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200829
830 if (addr_width < BITS_PER_LONG && pfn >> addr_width)
831 /* Address beyond IOMMU's addressing capabilities. */
832 return NULL;
833
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700834 parent = domain->pgd;
835
David Woodhouse5cf0a762014-03-19 16:07:49 +0000836 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700837 void *tmp_page;
838
David Woodhouseb026fd22009-06-28 10:37:25 +0100839 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700840 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000841 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100842 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000843 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700844 break;
845
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000846 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100847 uint64_t pteval;
848
Suresh Siddha4c923d42009-10-02 11:01:24 -0700849 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700850
David Woodhouse206a73c12009-07-01 19:30:28 +0100851 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700852 return NULL;
David Woodhouse206a73c12009-07-01 19:30:28 +0100853
David Woodhousec85994e2009-07-01 19:21:24 +0100854 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400855 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 +0800856 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100857 /* Someone else set it while we were thinking; use theirs. */
858 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800859 else
David Woodhousec85994e2009-07-01 19:21:24 +0100860 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700861 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000862 if (level == 1)
863 break;
864
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000865 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700866 level--;
867 }
868
David Woodhouse5cf0a762014-03-19 16:07:49 +0000869 if (!*target_level)
870 *target_level = level;
871
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700872 return pte;
873}
874
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100875
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700876/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100877static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
878 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100879 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700880{
881 struct dma_pte *parent, *pte = NULL;
882 int total = agaw_to_level(domain->agaw);
883 int offset;
884
885 parent = domain->pgd;
886 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100887 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700888 pte = &parent[offset];
889 if (level == total)
890 return pte;
891
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100892 if (!dma_pte_present(pte)) {
893 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700894 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100895 }
896
Yijing Wange16922a2014-05-20 20:37:51 +0800897 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100898 *large_page = total;
899 return pte;
900 }
901
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000902 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700903 total--;
904 }
905 return NULL;
906}
907
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700908/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +0000909static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +0100910 unsigned long start_pfn,
911 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700912{
David Woodhouse04b18e62009-06-27 19:15:01 +0100913 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100914 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +0100915 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700916
David Woodhouse04b18e62009-06-27 19:15:01 +0100917 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
David Woodhouse595badf2009-06-27 22:09:11 +0100918 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse59c36282009-09-19 07:36:28 -0700919 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +0100920
David Woodhouse04b18e62009-06-27 19:15:01 +0100921 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -0700922 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100923 large_page = 1;
924 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100925 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100926 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100927 continue;
928 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100929 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +0100930 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100931 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100932 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +0100933 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
934
David Woodhouse310a5ab2009-06-28 18:52:20 +0100935 domain_flush_cache(domain, first_pte,
936 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -0700937
938 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700939}
940
Alex Williamson3269ee02013-06-15 10:27:19 -0600941static void dma_pte_free_level(struct dmar_domain *domain, int level,
942 struct dma_pte *pte, unsigned long pfn,
943 unsigned long start_pfn, unsigned long last_pfn)
944{
945 pfn = max(start_pfn, pfn);
946 pte = &pte[pfn_level_offset(pfn, level)];
947
948 do {
949 unsigned long level_pfn;
950 struct dma_pte *level_pte;
951
952 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
953 goto next;
954
955 level_pfn = pfn & level_mask(level - 1);
956 level_pte = phys_to_virt(dma_pte_addr(pte));
957
958 if (level > 2)
959 dma_pte_free_level(domain, level - 1, level_pte,
960 level_pfn, start_pfn, last_pfn);
961
962 /* If range covers entire pagetable, free it */
963 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -0800964 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -0600965 dma_clear_pte(pte);
966 domain_flush_cache(domain, pte, sizeof(*pte));
967 free_pgtable_page(level_pte);
968 }
969next:
970 pfn += level_size(level);
971 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
972}
973
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700974/* free page table pages. last level pte should already be cleared */
975static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +0100976 unsigned long start_pfn,
977 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700978{
David Woodhouse6660c632009-06-27 22:41:00 +0100979 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700980
David Woodhouse6660c632009-06-27 22:41:00 +0100981 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
982 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse59c36282009-09-19 07:36:28 -0700983 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700984
David Woodhousef3a0a522009-06-30 03:40:07 +0100985 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -0600986 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
987 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +0100988
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700989 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +0100990 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700991 free_pgtable_page(domain->pgd);
992 domain->pgd = NULL;
993 }
994}
995
David Woodhouseea8ea462014-03-05 17:09:32 +0000996/* When a page at a given level is being unlinked from its parent, we don't
997 need to *modify* it at all. All we need to do is make a list of all the
998 pages which can be freed just as soon as we've flushed the IOTLB and we
999 know the hardware page-walk will no longer touch them.
1000 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1001 be freed. */
1002static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1003 int level, struct dma_pte *pte,
1004 struct page *freelist)
1005{
1006 struct page *pg;
1007
1008 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1009 pg->freelist = freelist;
1010 freelist = pg;
1011
1012 if (level == 1)
1013 return freelist;
1014
Jiang Liuadeb2592014-04-09 10:20:39 +08001015 pte = page_address(pg);
1016 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001017 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1018 freelist = dma_pte_list_pagetables(domain, level - 1,
1019 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001020 pte++;
1021 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001022
1023 return freelist;
1024}
1025
1026static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1027 struct dma_pte *pte, unsigned long pfn,
1028 unsigned long start_pfn,
1029 unsigned long last_pfn,
1030 struct page *freelist)
1031{
1032 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1033
1034 pfn = max(start_pfn, pfn);
1035 pte = &pte[pfn_level_offset(pfn, level)];
1036
1037 do {
1038 unsigned long level_pfn;
1039
1040 if (!dma_pte_present(pte))
1041 goto next;
1042
1043 level_pfn = pfn & level_mask(level);
1044
1045 /* If range covers entire pagetable, free it */
1046 if (start_pfn <= level_pfn &&
1047 last_pfn >= level_pfn + level_size(level) - 1) {
1048 /* These suborbinate page tables are going away entirely. Don't
1049 bother to clear them; we're just going to *free* them. */
1050 if (level > 1 && !dma_pte_superpage(pte))
1051 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1052
1053 dma_clear_pte(pte);
1054 if (!first_pte)
1055 first_pte = pte;
1056 last_pte = pte;
1057 } else if (level > 1) {
1058 /* Recurse down into a level that isn't *entirely* obsolete */
1059 freelist = dma_pte_clear_level(domain, level - 1,
1060 phys_to_virt(dma_pte_addr(pte)),
1061 level_pfn, start_pfn, last_pfn,
1062 freelist);
1063 }
1064next:
1065 pfn += level_size(level);
1066 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1067
1068 if (first_pte)
1069 domain_flush_cache(domain, first_pte,
1070 (void *)++last_pte - (void *)first_pte);
1071
1072 return freelist;
1073}
1074
1075/* We can't just free the pages because the IOMMU may still be walking
1076 the page tables, and may have cached the intermediate levels. The
1077 pages can only be freed after the IOTLB flush has been done. */
1078struct page *domain_unmap(struct dmar_domain *domain,
1079 unsigned long start_pfn,
1080 unsigned long last_pfn)
1081{
1082 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
1083 struct page *freelist = NULL;
1084
1085 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
1086 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
1087 BUG_ON(start_pfn > last_pfn);
1088
1089 /* we don't need lock here; nobody else touches the iova range */
1090 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1091 domain->pgd, 0, start_pfn, last_pfn, NULL);
1092
1093 /* free pgd */
1094 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1095 struct page *pgd_page = virt_to_page(domain->pgd);
1096 pgd_page->freelist = freelist;
1097 freelist = pgd_page;
1098
1099 domain->pgd = NULL;
1100 }
1101
1102 return freelist;
1103}
1104
1105void dma_free_pagelist(struct page *freelist)
1106{
1107 struct page *pg;
1108
1109 while ((pg = freelist)) {
1110 freelist = pg->freelist;
1111 free_pgtable_page(page_address(pg));
1112 }
1113}
1114
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001115/* iommu handling */
1116static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1117{
1118 struct root_entry *root;
1119 unsigned long flags;
1120
Suresh Siddha4c923d42009-10-02 11:01:24 -07001121 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001122 if (!root)
1123 return -ENOMEM;
1124
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001125 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001126
1127 spin_lock_irqsave(&iommu->lock, flags);
1128 iommu->root_entry = root;
1129 spin_unlock_irqrestore(&iommu->lock, flags);
1130
1131 return 0;
1132}
1133
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001134static void iommu_set_root_entry(struct intel_iommu *iommu)
1135{
1136 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001137 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001138 unsigned long flag;
1139
1140 addr = iommu->root_entry;
1141
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001142 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001143 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
1144
David Woodhousec416daa2009-05-10 20:30:58 +01001145 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001146
1147 /* Make sure hardware complete it */
1148 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001149 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001150
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001151 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001152}
1153
1154static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1155{
1156 u32 val;
1157 unsigned long flag;
1158
David Woodhouse9af88142009-02-13 23:18:03 +00001159 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001160 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001161
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001162 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001163 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001164
1165 /* Make sure hardware complete it */
1166 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001167 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001168
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001169 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001170}
1171
1172/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001173static void __iommu_flush_context(struct intel_iommu *iommu,
1174 u16 did, u16 source_id, u8 function_mask,
1175 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001176{
1177 u64 val = 0;
1178 unsigned long flag;
1179
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001180 switch (type) {
1181 case DMA_CCMD_GLOBAL_INVL:
1182 val = DMA_CCMD_GLOBAL_INVL;
1183 break;
1184 case DMA_CCMD_DOMAIN_INVL:
1185 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1186 break;
1187 case DMA_CCMD_DEVICE_INVL:
1188 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1189 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1190 break;
1191 default:
1192 BUG();
1193 }
1194 val |= DMA_CCMD_ICC;
1195
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001196 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001197 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1198
1199 /* Make sure hardware complete it */
1200 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1201 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1202
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001203 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001204}
1205
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001206/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001207static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1208 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001209{
1210 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1211 u64 val = 0, val_iva = 0;
1212 unsigned long flag;
1213
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001214 switch (type) {
1215 case DMA_TLB_GLOBAL_FLUSH:
1216 /* global flush doesn't need set IVA_REG */
1217 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1218 break;
1219 case DMA_TLB_DSI_FLUSH:
1220 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1221 break;
1222 case DMA_TLB_PSI_FLUSH:
1223 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001224 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001225 val_iva = size_order | addr;
1226 break;
1227 default:
1228 BUG();
1229 }
1230 /* Note: set drain read/write */
1231#if 0
1232 /*
1233 * This is probably to be super secure.. Looks like we can
1234 * ignore it without any impact.
1235 */
1236 if (cap_read_drain(iommu->cap))
1237 val |= DMA_TLB_READ_DRAIN;
1238#endif
1239 if (cap_write_drain(iommu->cap))
1240 val |= DMA_TLB_WRITE_DRAIN;
1241
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001242 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001243 /* Note: Only uses first TLB reg currently */
1244 if (val_iva)
1245 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1246 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1247
1248 /* Make sure hardware complete it */
1249 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1250 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1251
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001252 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001253
1254 /* check IOTLB invalidation granularity */
1255 if (DMA_TLB_IAIG(val) == 0)
1256 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
1257 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
1258 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001259 (unsigned long long)DMA_TLB_IIRG(type),
1260 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001261}
1262
David Woodhouse64ae8922014-03-09 12:52:30 -07001263static struct device_domain_info *
1264iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1265 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001266{
Yu Zhao93a23a72009-05-18 13:51:37 +08001267 int found = 0;
1268 unsigned long flags;
1269 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001270 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001271
1272 if (!ecap_dev_iotlb_support(iommu->ecap))
1273 return NULL;
1274
1275 if (!iommu->qi)
1276 return NULL;
1277
1278 spin_lock_irqsave(&device_domain_lock, flags);
1279 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001280 if (info->iommu == iommu && info->bus == bus &&
1281 info->devfn == devfn) {
Yu Zhao93a23a72009-05-18 13:51:37 +08001282 found = 1;
1283 break;
1284 }
1285 spin_unlock_irqrestore(&device_domain_lock, flags);
1286
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001287 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001288 return NULL;
1289
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001290 pdev = to_pci_dev(info->dev);
1291
1292 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001293 return NULL;
1294
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001295 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001296 return NULL;
1297
Yu Zhao93a23a72009-05-18 13:51:37 +08001298 return info;
1299}
1300
1301static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1302{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001303 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001304 return;
1305
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001306 pci_enable_ats(to_pci_dev(info->dev), VTD_PAGE_SHIFT);
Yu Zhao93a23a72009-05-18 13:51:37 +08001307}
1308
1309static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1310{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001311 if (!info->dev || !dev_is_pci(info->dev) ||
1312 !pci_ats_enabled(to_pci_dev(info->dev)))
Yu Zhao93a23a72009-05-18 13:51:37 +08001313 return;
1314
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001315 pci_disable_ats(to_pci_dev(info->dev));
Yu Zhao93a23a72009-05-18 13:51:37 +08001316}
1317
1318static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1319 u64 addr, unsigned mask)
1320{
1321 u16 sid, qdep;
1322 unsigned long flags;
1323 struct device_domain_info *info;
1324
1325 spin_lock_irqsave(&device_domain_lock, flags);
1326 list_for_each_entry(info, &domain->devices, link) {
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001327 struct pci_dev *pdev;
1328 if (!info->dev || !dev_is_pci(info->dev))
1329 continue;
1330
1331 pdev = to_pci_dev(info->dev);
1332 if (!pci_ats_enabled(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001333 continue;
1334
1335 sid = info->bus << 8 | info->devfn;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001336 qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001337 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1338 }
1339 spin_unlock_irqrestore(&device_domain_lock, flags);
1340}
1341
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001342static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
David Woodhouseea8ea462014-03-05 17:09:32 +00001343 unsigned long pfn, unsigned int pages, int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001344{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001345 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001346 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001347
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001348 BUG_ON(pages == 0);
1349
David Woodhouseea8ea462014-03-05 17:09:32 +00001350 if (ih)
1351 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001352 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001353 * Fallback to domain selective flush if no PSI support or the size is
1354 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001355 * PSI requires page size to be 2 ^ x, and the base address is naturally
1356 * aligned to the size
1357 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001358 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1359 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001360 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001361 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001362 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001363 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001364
1365 /*
Nadav Amit82653632010-04-01 13:24:40 +03001366 * In caching mode, changes of pages from non-present to present require
1367 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001368 */
Nadav Amit82653632010-04-01 13:24:40 +03001369 if (!cap_caching_mode(iommu->cap) || !map)
Yu Zhao93a23a72009-05-18 13:51:37 +08001370 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001371}
1372
mark grossf8bab732008-02-08 04:18:38 -08001373static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1374{
1375 u32 pmen;
1376 unsigned long flags;
1377
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001378 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001379 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1380 pmen &= ~DMA_PMEN_EPM;
1381 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1382
1383 /* wait for the protected region status bit to clear */
1384 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1385 readl, !(pmen & DMA_PMEN_PRS), pmen);
1386
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001387 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001388}
1389
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001390static int iommu_enable_translation(struct intel_iommu *iommu)
1391{
1392 u32 sts;
1393 unsigned long flags;
1394
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001395 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001396 iommu->gcmd |= DMA_GCMD_TE;
1397 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001398
1399 /* Make sure hardware complete it */
1400 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001401 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001402
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001403 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001404 return 0;
1405}
1406
1407static int iommu_disable_translation(struct intel_iommu *iommu)
1408{
1409 u32 sts;
1410 unsigned long flag;
1411
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001412 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001413 iommu->gcmd &= ~DMA_GCMD_TE;
1414 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1415
1416 /* Make sure hardware complete it */
1417 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001418 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001419
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001420 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001421 return 0;
1422}
1423
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001424
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001425static int iommu_init_domains(struct intel_iommu *iommu)
1426{
1427 unsigned long ndomains;
1428 unsigned long nlongs;
1429
1430 ndomains = cap_ndoms(iommu->cap);
Jiang Liu852bdb02014-01-06 14:18:11 +08001431 pr_debug("IOMMU%d: Number of Domains supported <%ld>\n",
1432 iommu->seq_id, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001433 nlongs = BITS_TO_LONGS(ndomains);
1434
Donald Dutile94a91b52009-08-20 16:51:34 -04001435 spin_lock_init(&iommu->lock);
1436
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001437 /* TBD: there might be 64K domains,
1438 * consider other allocation for future chip
1439 */
1440 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1441 if (!iommu->domain_ids) {
Jiang Liu852bdb02014-01-06 14:18:11 +08001442 pr_err("IOMMU%d: allocating domain id array failed\n",
1443 iommu->seq_id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001444 return -ENOMEM;
1445 }
1446 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1447 GFP_KERNEL);
1448 if (!iommu->domains) {
Jiang Liu852bdb02014-01-06 14:18:11 +08001449 pr_err("IOMMU%d: allocating domain array failed\n",
1450 iommu->seq_id);
1451 kfree(iommu->domain_ids);
1452 iommu->domain_ids = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001453 return -ENOMEM;
1454 }
1455
1456 /*
1457 * if Caching mode is set, then invalid translations are tagged
1458 * with domainid 0. Hence we need to pre-allocate it.
1459 */
1460 if (cap_caching_mode(iommu->cap))
1461 set_bit(0, iommu->domain_ids);
1462 return 0;
1463}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001464
Jiang Liua868e6b2014-01-06 14:18:20 +08001465static void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001466{
1467 struct dmar_domain *domain;
Jiang Liu5ced12a2014-01-06 14:18:22 +08001468 int i, count;
Weidong Hanc7151a82008-12-08 22:51:37 +08001469 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001470
Donald Dutile94a91b52009-08-20 16:51:34 -04001471 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001472 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Jiang Liua4eaa862014-02-19 14:07:30 +08001473 /*
1474 * Domain id 0 is reserved for invalid translation
1475 * if hardware supports caching mode.
1476 */
1477 if (cap_caching_mode(iommu->cap) && i == 0)
1478 continue;
1479
Donald Dutile94a91b52009-08-20 16:51:34 -04001480 domain = iommu->domains[i];
1481 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001482
Donald Dutile94a91b52009-08-20 16:51:34 -04001483 spin_lock_irqsave(&domain->iommu_lock, flags);
Jiang Liu5ced12a2014-01-06 14:18:22 +08001484 count = --domain->iommu_count;
1485 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Jiang Liu92d03cc2014-02-19 14:07:28 +08001486 if (count == 0)
1487 domain_exit(domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001488 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001489 }
1490
1491 if (iommu->gcmd & DMA_GCMD_TE)
1492 iommu_disable_translation(iommu);
1493
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001494 kfree(iommu->domains);
1495 kfree(iommu->domain_ids);
Jiang Liua868e6b2014-01-06 14:18:20 +08001496 iommu->domains = NULL;
1497 iommu->domain_ids = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001498
Weidong Hand9630fe2008-12-08 11:06:32 +08001499 g_iommus[iommu->seq_id] = NULL;
1500
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001501 /* free context mapping */
1502 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001503}
1504
Jiang Liuab8dfe22014-07-11 14:19:27 +08001505static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001506{
Jiang Liu92d03cc2014-02-19 14:07:28 +08001507 /* domain id for virtual machine, it won't be set in context */
1508 static atomic_t vm_domid = ATOMIC_INIT(0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001509 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001510
1511 domain = alloc_domain_mem();
1512 if (!domain)
1513 return NULL;
1514
Jiang Liuab8dfe22014-07-11 14:19:27 +08001515 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001516 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001517 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001518 spin_lock_init(&domain->iommu_lock);
1519 INIT_LIST_HEAD(&domain->devices);
Jiang Liuab8dfe22014-07-11 14:19:27 +08001520 if (flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Jiang Liu92d03cc2014-02-19 14:07:28 +08001521 domain->id = atomic_inc_return(&vm_domid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001522
1523 return domain;
1524}
1525
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001526static int iommu_attach_domain(struct dmar_domain *domain,
1527 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001528{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001529 int num;
1530 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001531 unsigned long flags;
1532
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001533 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001534
1535 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001536
1537 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1538 if (num >= ndomains) {
1539 spin_unlock_irqrestore(&iommu->lock, flags);
1540 printk(KERN_ERR "IOMMU: no free domain ids\n");
1541 return -ENOMEM;
1542 }
1543
1544 domain->id = num;
Jiang Liu9ebd6822014-02-19 14:07:29 +08001545 domain->iommu_count++;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001546 set_bit(num, iommu->domain_ids);
Mike Travis1b198bb2012-03-05 15:05:16 -08001547 set_bit(iommu->seq_id, domain->iommu_bmp);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001548 iommu->domains[num] = domain;
1549 spin_unlock_irqrestore(&iommu->lock, flags);
1550
1551 return 0;
1552}
1553
1554static void iommu_detach_domain(struct dmar_domain *domain,
1555 struct intel_iommu *iommu)
1556{
1557 unsigned long flags;
1558 int num, ndomains;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001559
1560 spin_lock_irqsave(&iommu->lock, flags);
1561 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001562 for_each_set_bit(num, iommu->domain_ids, ndomains) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001563 if (iommu->domains[num] == domain) {
Jiang Liu92d03cc2014-02-19 14:07:28 +08001564 clear_bit(num, iommu->domain_ids);
1565 iommu->domains[num] = NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001566 break;
1567 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001568 }
Weidong Han8c11e792008-12-08 15:29:22 +08001569 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001570}
1571
1572static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001573static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001574
Joseph Cihula51a63e62011-03-21 11:04:24 -07001575static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001576{
1577 struct pci_dev *pdev = NULL;
1578 struct iova *iova;
1579 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001580
David Millerf6611972008-02-06 01:36:23 -08001581 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001582
Mark Gross8a443df2008-03-04 14:59:31 -08001583 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1584 &reserved_rbtree_key);
1585
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001586 /* IOAPIC ranges shouldn't be accessed by DMA */
1587 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1588 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001589 if (!iova) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001590 printk(KERN_ERR "Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001591 return -ENODEV;
1592 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001593
1594 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1595 for_each_pci_dev(pdev) {
1596 struct resource *r;
1597
1598 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1599 r = &pdev->resource[i];
1600 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1601 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001602 iova = reserve_iova(&reserved_iova_list,
1603 IOVA_PFN(r->start),
1604 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001605 if (!iova) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001606 printk(KERN_ERR "Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001607 return -ENODEV;
1608 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001609 }
1610 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001611 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001612}
1613
1614static void domain_reserve_special_ranges(struct dmar_domain *domain)
1615{
1616 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1617}
1618
1619static inline int guestwidth_to_adjustwidth(int gaw)
1620{
1621 int agaw;
1622 int r = (gaw - 12) % 9;
1623
1624 if (r == 0)
1625 agaw = gaw;
1626 else
1627 agaw = gaw + 9 - r;
1628 if (agaw > 64)
1629 agaw = 64;
1630 return agaw;
1631}
1632
1633static int domain_init(struct dmar_domain *domain, int guest_width)
1634{
1635 struct intel_iommu *iommu;
1636 int adjust_width, agaw;
1637 unsigned long sagaw;
1638
David Millerf6611972008-02-06 01:36:23 -08001639 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001640 domain_reserve_special_ranges(domain);
1641
1642 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001643 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001644 if (guest_width > cap_mgaw(iommu->cap))
1645 guest_width = cap_mgaw(iommu->cap);
1646 domain->gaw = guest_width;
1647 adjust_width = guestwidth_to_adjustwidth(guest_width);
1648 agaw = width_to_agaw(adjust_width);
1649 sagaw = cap_sagaw(iommu->cap);
1650 if (!test_bit(agaw, &sagaw)) {
1651 /* hardware doesn't support it, choose a bigger one */
1652 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1653 agaw = find_next_bit(&sagaw, 5, agaw);
1654 if (agaw >= 5)
1655 return -ENODEV;
1656 }
1657 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001658
Weidong Han8e6040972008-12-08 15:49:06 +08001659 if (ecap_coherent(iommu->ecap))
1660 domain->iommu_coherency = 1;
1661 else
1662 domain->iommu_coherency = 0;
1663
Sheng Yang58c610b2009-03-18 15:33:05 +08001664 if (ecap_sc_support(iommu->ecap))
1665 domain->iommu_snooping = 1;
1666 else
1667 domain->iommu_snooping = 0;
1668
David Woodhouse214e39a2014-03-19 10:38:49 +00001669 if (intel_iommu_superpage)
1670 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1671 else
1672 domain->iommu_superpage = 0;
1673
Suresh Siddha4c923d42009-10-02 11:01:24 -07001674 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001675
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001676 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001677 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001678 if (!domain->pgd)
1679 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001680 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001681 return 0;
1682}
1683
1684static void domain_exit(struct dmar_domain *domain)
1685{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001686 struct dmar_drhd_unit *drhd;
1687 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00001688 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001689
1690 /* Domain 0 is reserved, so dont process it */
1691 if (!domain)
1692 return;
1693
Alex Williamson7b668352011-05-24 12:02:41 +01001694 /* Flush any lazy unmaps that may reference this domain */
1695 if (!intel_iommu_strict)
1696 flush_unmaps_timeout(0);
1697
Jiang Liu92d03cc2014-02-19 14:07:28 +08001698 /* remove associated devices */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001699 domain_remove_dev_info(domain);
Jiang Liu92d03cc2014-02-19 14:07:28 +08001700
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001701 /* destroy iovas */
1702 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001703
David Woodhouseea8ea462014-03-05 17:09:32 +00001704 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001705
Jiang Liu92d03cc2014-02-19 14:07:28 +08001706 /* clear attached or cached domains */
Jiang Liu0e242612014-02-19 14:07:34 +08001707 rcu_read_lock();
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001708 for_each_active_iommu(iommu, drhd)
Jiang Liuab8dfe22014-07-11 14:19:27 +08001709 if (domain_type_is_vm(domain) ||
Jiang Liu92d03cc2014-02-19 14:07:28 +08001710 test_bit(iommu->seq_id, domain->iommu_bmp))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001711 iommu_detach_domain(domain, iommu);
Jiang Liu0e242612014-02-19 14:07:34 +08001712 rcu_read_unlock();
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001713
David Woodhouseea8ea462014-03-05 17:09:32 +00001714 dma_free_pagelist(freelist);
1715
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001716 free_domain_mem(domain);
1717}
1718
David Woodhouse64ae8922014-03-09 12:52:30 -07001719static int domain_context_mapping_one(struct dmar_domain *domain,
1720 struct intel_iommu *iommu,
1721 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001722{
1723 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001724 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001725 struct dma_pte *pgd;
1726 unsigned long num;
1727 unsigned long ndomains;
1728 int id;
1729 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001730 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001731
1732 pr_debug("Set context mapping for %02x:%02x.%d\n",
1733 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001734
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001735 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001736 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1737 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001738
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001739 context = device_to_context_entry(iommu, bus, devfn);
1740 if (!context)
1741 return -ENOMEM;
1742 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001743 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001744 spin_unlock_irqrestore(&iommu->lock, flags);
1745 return 0;
1746 }
1747
Weidong Hanea6606b2008-12-08 23:08:15 +08001748 id = domain->id;
1749 pgd = domain->pgd;
1750
Jiang Liuab8dfe22014-07-11 14:19:27 +08001751 if (domain_type_is_vm_or_si(domain)) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001752 int found = 0;
1753
1754 /* find an available domain id for this device in iommu */
1755 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001756 for_each_set_bit(num, iommu->domain_ids, ndomains) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001757 if (iommu->domains[num] == domain) {
1758 id = num;
1759 found = 1;
1760 break;
1761 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001762 }
1763
1764 if (found == 0) {
1765 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1766 if (num >= ndomains) {
1767 spin_unlock_irqrestore(&iommu->lock, flags);
1768 printk(KERN_ERR "IOMMU: no free domain ids\n");
1769 return -EFAULT;
1770 }
1771
1772 set_bit(num, iommu->domain_ids);
1773 iommu->domains[num] = domain;
1774 id = num;
1775 }
1776
1777 /* Skip top levels of page tables for
1778 * iommu which has less agaw than default.
Chris Wright1672af12009-12-02 12:06:34 -08001779 * Unnecessary for PT mode.
Weidong Hanea6606b2008-12-08 23:08:15 +08001780 */
Chris Wright1672af12009-12-02 12:06:34 -08001781 if (translation != CONTEXT_TT_PASS_THROUGH) {
1782 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1783 pgd = phys_to_virt(dma_pte_addr(pgd));
1784 if (!dma_pte_present(pgd)) {
1785 spin_unlock_irqrestore(&iommu->lock, flags);
1786 return -ENOMEM;
1787 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001788 }
1789 }
1790 }
1791
1792 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001793
Yu Zhao93a23a72009-05-18 13:51:37 +08001794 if (translation != CONTEXT_TT_PASS_THROUGH) {
David Woodhouse64ae8922014-03-09 12:52:30 -07001795 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001796 translation = info ? CONTEXT_TT_DEV_IOTLB :
1797 CONTEXT_TT_MULTI_LEVEL;
1798 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001799 /*
1800 * In pass through mode, AW must be programmed to indicate the largest
1801 * AGAW value supported by hardware. And ASR is ignored by hardware.
1802 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001803 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001804 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001805 else {
1806 context_set_address_root(context, virt_to_phys(pgd));
1807 context_set_address_width(context, iommu->agaw);
1808 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001809
1810 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001811 context_set_fault_enable(context);
1812 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001813 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001814
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001815 /*
1816 * It's a non-present to present mapping. If hardware doesn't cache
1817 * non-present entry we only need to flush the write-buffer. If the
1818 * _does_ cache non-present entries, then it does so in the special
1819 * domain #0, which we have to flush:
1820 */
1821 if (cap_caching_mode(iommu->cap)) {
1822 iommu->flush.flush_context(iommu, 0,
1823 (((u16)bus) << 8) | devfn,
1824 DMA_CCMD_MASK_NOBIT,
1825 DMA_CCMD_DEVICE_INVL);
Jiang Liu18fd7792014-07-11 14:19:26 +08001826 iommu->flush.flush_iotlb(iommu, id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001827 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001828 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001829 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001830 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001831 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001832
1833 spin_lock_irqsave(&domain->iommu_lock, flags);
Mike Travis1b198bb2012-03-05 15:05:16 -08001834 if (!test_and_set_bit(iommu->seq_id, domain->iommu_bmp)) {
Weidong Hanc7151a82008-12-08 22:51:37 +08001835 domain->iommu_count++;
Suresh Siddha4c923d42009-10-02 11:01:24 -07001836 if (domain->iommu_count == 1)
1837 domain->nid = iommu->node;
Sheng Yang58c610b2009-03-18 15:33:05 +08001838 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001839 }
1840 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001841 return 0;
1842}
1843
Alex Williamson579305f2014-07-03 09:51:43 -06001844struct domain_context_mapping_data {
1845 struct dmar_domain *domain;
1846 struct intel_iommu *iommu;
1847 int translation;
1848};
1849
1850static int domain_context_mapping_cb(struct pci_dev *pdev,
1851 u16 alias, void *opaque)
1852{
1853 struct domain_context_mapping_data *data = opaque;
1854
1855 return domain_context_mapping_one(data->domain, data->iommu,
1856 PCI_BUS_NUM(alias), alias & 0xff,
1857 data->translation);
1858}
1859
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001860static int
David Woodhousee1f167f2014-03-09 15:24:46 -07001861domain_context_mapping(struct dmar_domain *domain, struct device *dev,
1862 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863{
David Woodhouse64ae8922014-03-09 12:52:30 -07001864 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07001865 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06001866 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001867
David Woodhousee1f167f2014-03-09 15:24:46 -07001868 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07001869 if (!iommu)
1870 return -ENODEV;
1871
Alex Williamson579305f2014-07-03 09:51:43 -06001872 if (!dev_is_pci(dev))
1873 return domain_context_mapping_one(domain, iommu, bus, devfn,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001874 translation);
Alex Williamson579305f2014-07-03 09:51:43 -06001875
1876 data.domain = domain;
1877 data.iommu = iommu;
1878 data.translation = translation;
1879
1880 return pci_for_each_dma_alias(to_pci_dev(dev),
1881 &domain_context_mapping_cb, &data);
1882}
1883
1884static int domain_context_mapped_cb(struct pci_dev *pdev,
1885 u16 alias, void *opaque)
1886{
1887 struct intel_iommu *iommu = opaque;
1888
1889 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001890}
1891
David Woodhousee1f167f2014-03-09 15:24:46 -07001892static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001893{
Weidong Han5331fe62008-12-08 23:00:00 +08001894 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07001895 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08001896
David Woodhousee1f167f2014-03-09 15:24:46 -07001897 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001898 if (!iommu)
1899 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001900
Alex Williamson579305f2014-07-03 09:51:43 -06001901 if (!dev_is_pci(dev))
1902 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07001903
Alex Williamson579305f2014-07-03 09:51:43 -06001904 return !pci_for_each_dma_alias(to_pci_dev(dev),
1905 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001906}
1907
Fenghua Yuf5329592009-08-04 15:09:37 -07001908/* Returns a number of VTD pages, but aligned to MM page size */
1909static inline unsigned long aligned_nrpages(unsigned long host_addr,
1910 size_t size)
1911{
1912 host_addr &= ~PAGE_MASK;
1913 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
1914}
1915
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001916/* Return largest possible superpage level for a given mapping */
1917static inline int hardware_largepage_caps(struct dmar_domain *domain,
1918 unsigned long iov_pfn,
1919 unsigned long phy_pfn,
1920 unsigned long pages)
1921{
1922 int support, level = 1;
1923 unsigned long pfnmerge;
1924
1925 support = domain->iommu_superpage;
1926
1927 /* To use a large page, the virtual *and* physical addresses
1928 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
1929 of them will mean we have to use smaller pages. So just
1930 merge them and check both at once. */
1931 pfnmerge = iov_pfn | phy_pfn;
1932
1933 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
1934 pages >>= VTD_STRIDE_SHIFT;
1935 if (!pages)
1936 break;
1937 pfnmerge >>= VTD_STRIDE_SHIFT;
1938 level++;
1939 support--;
1940 }
1941 return level;
1942}
1943
David Woodhouse9051aa02009-06-29 12:30:54 +01001944static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1945 struct scatterlist *sg, unsigned long phys_pfn,
1946 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01001947{
1948 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01001949 phys_addr_t uninitialized_var(pteval);
David Woodhousee1605492009-06-29 11:17:38 +01001950 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhouse9051aa02009-06-29 12:30:54 +01001951 unsigned long sg_res;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001952 unsigned int largepage_lvl = 0;
1953 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01001954
1955 BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width);
1956
1957 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1958 return -EINVAL;
1959
1960 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
1961
David Woodhouse9051aa02009-06-29 12:30:54 +01001962 if (sg)
1963 sg_res = 0;
1964 else {
1965 sg_res = nr_pages + 1;
1966 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
1967 }
1968
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001969 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01001970 uint64_t tmp;
1971
David Woodhousee1605492009-06-29 11:17:38 +01001972 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07001973 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01001974 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
1975 sg->dma_length = sg->length;
1976 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001977 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01001978 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001979
David Woodhousee1605492009-06-29 11:17:38 +01001980 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001981 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
1982
David Woodhouse5cf0a762014-03-19 16:07:49 +00001983 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01001984 if (!pte)
1985 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001986 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00001987 if (largepage_lvl > 1) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001988 pteval |= DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00001989 /* Ensure that old small page tables are removed to make room
1990 for superpage, if they exist. */
1991 dma_pte_clear_range(domain, iov_pfn,
1992 iov_pfn + lvl_to_nr_pages(largepage_lvl) - 1);
1993 dma_pte_free_pagetable(domain, iov_pfn,
1994 iov_pfn + lvl_to_nr_pages(largepage_lvl) - 1);
1995 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001996 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00001997 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001998
David Woodhousee1605492009-06-29 11:17:38 +01001999 }
2000 /* We don't need lock here, nobody else
2001 * touches the iova range
2002 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002003 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002004 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002005 static int dumps = 5;
David Woodhousec85994e2009-07-01 19:21:24 +01002006 printk(KERN_CRIT "ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2007 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002008 if (dumps) {
2009 dumps--;
2010 debug_dma_dump_mappings(NULL);
2011 }
2012 WARN_ON(1);
2013 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002014
2015 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2016
2017 BUG_ON(nr_pages < lvl_pages);
2018 BUG_ON(sg_res < lvl_pages);
2019
2020 nr_pages -= lvl_pages;
2021 iov_pfn += lvl_pages;
2022 phys_pfn += lvl_pages;
2023 pteval += lvl_pages * VTD_PAGE_SIZE;
2024 sg_res -= lvl_pages;
2025
2026 /* If the next PTE would be the first in a new page, then we
2027 need to flush the cache on the entries we've just written.
2028 And then we'll need to recalculate 'pte', so clear it and
2029 let it get set again in the if (!pte) block above.
2030
2031 If we're done (!nr_pages) we need to flush the cache too.
2032
2033 Also if we've been setting superpages, we may need to
2034 recalculate 'pte' and switch back to smaller pages for the
2035 end of the mapping, if the trailing size is not enough to
2036 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002037 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002038 if (!nr_pages || first_pte_in_page(pte) ||
2039 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002040 domain_flush_cache(domain, first_pte,
2041 (void *)pte - (void *)first_pte);
2042 pte = NULL;
2043 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002044
2045 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002046 sg = sg_next(sg);
2047 }
2048 return 0;
2049}
2050
David Woodhouse9051aa02009-06-29 12:30:54 +01002051static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2052 struct scatterlist *sg, unsigned long nr_pages,
2053 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002054{
David Woodhouse9051aa02009-06-29 12:30:54 +01002055 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2056}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002057
David Woodhouse9051aa02009-06-29 12:30:54 +01002058static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2059 unsigned long phys_pfn, unsigned long nr_pages,
2060 int prot)
2061{
2062 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002063}
2064
Weidong Hanc7151a82008-12-08 22:51:37 +08002065static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002066{
Weidong Hanc7151a82008-12-08 22:51:37 +08002067 if (!iommu)
2068 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002069
2070 clear_context_table(iommu, bus, devfn);
2071 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002072 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002073 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002074}
2075
David Woodhouse109b9b02012-05-25 17:43:02 +01002076static inline void unlink_domain_info(struct device_domain_info *info)
2077{
2078 assert_spin_locked(&device_domain_lock);
2079 list_del(&info->link);
2080 list_del(&info->global);
2081 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002082 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002083}
2084
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002085static void domain_remove_dev_info(struct dmar_domain *domain)
2086{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002087 struct device_domain_info *info, *tmp;
Jiang Liu92d03cc2014-02-19 14:07:28 +08002088 unsigned long flags, flags2;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002089
2090 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wang3a74ca02014-05-20 20:37:47 +08002091 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhouse109b9b02012-05-25 17:43:02 +01002092 unlink_domain_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002093 spin_unlock_irqrestore(&device_domain_lock, flags);
2094
Yu Zhao93a23a72009-05-18 13:51:37 +08002095 iommu_disable_dev_iotlb(info);
David Woodhouse7c7faa12014-03-09 13:33:06 -07002096 iommu_detach_dev(info->iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002097
Jiang Liuab8dfe22014-07-11 14:19:27 +08002098 if (domain_type_is_vm(domain)) {
David Woodhouse7c7faa12014-03-09 13:33:06 -07002099 iommu_detach_dependent_devices(info->iommu, info->dev);
Jiang Liu92d03cc2014-02-19 14:07:28 +08002100 /* clear this iommu in iommu_bmp, update iommu count
2101 * and capabilities
2102 */
2103 spin_lock_irqsave(&domain->iommu_lock, flags2);
David Woodhouse7c7faa12014-03-09 13:33:06 -07002104 if (test_and_clear_bit(info->iommu->seq_id,
Jiang Liu92d03cc2014-02-19 14:07:28 +08002105 domain->iommu_bmp)) {
2106 domain->iommu_count--;
2107 domain_update_iommu_cap(domain);
2108 }
2109 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
2110 }
2111
2112 free_devinfo_mem(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002113 spin_lock_irqsave(&device_domain_lock, flags);
2114 }
2115 spin_unlock_irqrestore(&device_domain_lock, flags);
2116}
2117
2118/*
2119 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002120 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002121 */
David Woodhouse1525a292014-03-06 16:19:30 +00002122static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002123{
2124 struct device_domain_info *info;
2125
2126 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002127 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002128 if (info)
2129 return info->domain;
2130 return NULL;
2131}
2132
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002133static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002134dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2135{
2136 struct device_domain_info *info;
2137
2138 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002139 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002140 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002141 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002142
2143 return NULL;
2144}
2145
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002146static struct dmar_domain *dmar_insert_dev_info(struct intel_iommu *iommu,
David Woodhouse41e80dca2014-03-09 13:55:54 -07002147 int bus, int devfn,
David Woodhouseb718cd32014-03-09 13:11:33 -07002148 struct device *dev,
2149 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002150{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002151 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002152 struct device_domain_info *info;
2153 unsigned long flags;
2154
2155 info = alloc_devinfo_mem();
2156 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002157 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002158
Jiang Liu745f2582014-02-19 14:07:26 +08002159 info->bus = bus;
2160 info->devfn = devfn;
2161 info->dev = dev;
2162 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002163 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002164
2165 spin_lock_irqsave(&device_domain_lock, flags);
2166 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002167 found = find_domain(dev);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002168 else {
2169 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002170 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002171 if (info2)
2172 found = info2->domain;
2173 }
Jiang Liu745f2582014-02-19 14:07:26 +08002174 if (found) {
2175 spin_unlock_irqrestore(&device_domain_lock, flags);
2176 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002177 /* Caller must free the original domain */
2178 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002179 }
2180
David Woodhouseb718cd32014-03-09 13:11:33 -07002181 list_add(&info->link, &domain->devices);
2182 list_add(&info->global, &device_domain_list);
2183 if (dev)
2184 dev->archdata.iommu = info;
2185 spin_unlock_irqrestore(&device_domain_lock, flags);
2186
2187 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002188}
2189
Alex Williamson579305f2014-07-03 09:51:43 -06002190static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2191{
2192 *(u16 *)opaque = alias;
2193 return 0;
2194}
2195
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002196/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002197static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002198{
Alex Williamson579305f2014-07-03 09:51:43 -06002199 struct dmar_domain *domain, *tmp;
2200 struct intel_iommu *iommu;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002201 struct device_domain_info *info;
Alex Williamson579305f2014-07-03 09:51:43 -06002202 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002203 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002204 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002205
David Woodhouse146922e2014-03-09 15:44:17 -07002206 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002207 if (domain)
2208 return domain;
2209
David Woodhouse146922e2014-03-09 15:44:17 -07002210 iommu = device_to_iommu(dev, &bus, &devfn);
2211 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002212 return NULL;
2213
2214 if (dev_is_pci(dev)) {
2215 struct pci_dev *pdev = to_pci_dev(dev);
2216
2217 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2218
2219 spin_lock_irqsave(&device_domain_lock, flags);
2220 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2221 PCI_BUS_NUM(dma_alias),
2222 dma_alias & 0xff);
2223 if (info) {
2224 iommu = info->iommu;
2225 domain = info->domain;
2226 }
2227 spin_unlock_irqrestore(&device_domain_lock, flags);
2228
2229 /* DMA alias already has a domain, uses it */
2230 if (info)
2231 goto found_domain;
2232 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002233
David Woodhouse146922e2014-03-09 15:44:17 -07002234 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002235 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002236 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002237 return NULL;
2238
Jiang Liu745f2582014-02-19 14:07:26 +08002239 if (iommu_attach_domain(domain, iommu)) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07002240 free_domain_mem(domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002241 return NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002242 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002243
Alex Williamson579305f2014-07-03 09:51:43 -06002244 if (domain_init(domain, gaw)) {
2245 domain_exit(domain);
2246 return NULL;
2247 }
2248
2249 /* register PCI DMA alias device */
2250 if (dev_is_pci(dev)) {
2251 tmp = dmar_insert_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2252 dma_alias & 0xff, NULL, domain);
2253
2254 if (!tmp || tmp != domain) {
2255 domain_exit(domain);
2256 domain = tmp;
2257 }
2258
David Woodhouseb718cd32014-03-09 13:11:33 -07002259 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002260 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002261 }
2262
2263found_domain:
Alex Williamson579305f2014-07-03 09:51:43 -06002264 tmp = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
2265
2266 if (!tmp || tmp != domain) {
2267 domain_exit(domain);
2268 domain = tmp;
2269 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002270
2271 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002272}
2273
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002274static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002275#define IDENTMAP_ALL 1
2276#define IDENTMAP_GFX 2
2277#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002278
David Woodhouseb2132032009-06-26 18:50:28 +01002279static int iommu_domain_identity_map(struct dmar_domain *domain,
2280 unsigned long long start,
2281 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002282{
David Woodhousec5395d52009-06-28 16:35:56 +01002283 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2284 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002285
David Woodhousec5395d52009-06-28 16:35:56 +01002286 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2287 dma_to_mm_pfn(last_vpfn))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002288 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002289 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002290 }
2291
David Woodhousec5395d52009-06-28 16:35:56 +01002292 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
2293 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002294 /*
2295 * RMRR range might have overlap with physical memory range,
2296 * clear it first
2297 */
David Woodhousec5395d52009-06-28 16:35:56 +01002298 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002299
David Woodhousec5395d52009-06-28 16:35:56 +01002300 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2301 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002302 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002303}
2304
David Woodhouse0b9d9752014-03-09 15:48:15 -07002305static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002306 unsigned long long start,
2307 unsigned long long end)
2308{
2309 struct dmar_domain *domain;
2310 int ret;
2311
David Woodhouse0b9d9752014-03-09 15:48:15 -07002312 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002313 if (!domain)
2314 return -ENOMEM;
2315
David Woodhouse19943b02009-08-04 16:19:20 +01002316 /* For _hardware_ passthrough, don't bother. But for software
2317 passthrough, we do it anyway -- it may indicate a memory
2318 range which is reserved in E820, so which didn't get set
2319 up to start with in si_domain */
2320 if (domain == si_domain && hw_pass_through) {
2321 printk("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
David Woodhouse0b9d9752014-03-09 15:48:15 -07002322 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002323 return 0;
2324 }
2325
2326 printk(KERN_INFO
2327 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
David Woodhouse0b9d9752014-03-09 15:48:15 -07002328 dev_name(dev), start, end);
David Woodhouse2ff729f2009-08-26 14:25:41 +01002329
David Woodhouse5595b522009-12-02 09:21:55 +00002330 if (end < start) {
2331 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2332 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2333 dmi_get_system_info(DMI_BIOS_VENDOR),
2334 dmi_get_system_info(DMI_BIOS_VERSION),
2335 dmi_get_system_info(DMI_PRODUCT_VERSION));
2336 ret = -EIO;
2337 goto error;
2338 }
2339
David Woodhouse2ff729f2009-08-26 14:25:41 +01002340 if (end >> agaw_to_width(domain->agaw)) {
2341 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2342 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2343 agaw_to_width(domain->agaw),
2344 dmi_get_system_info(DMI_BIOS_VENDOR),
2345 dmi_get_system_info(DMI_BIOS_VERSION),
2346 dmi_get_system_info(DMI_PRODUCT_VERSION));
2347 ret = -EIO;
2348 goto error;
2349 }
David Woodhouse19943b02009-08-04 16:19:20 +01002350
David Woodhouseb2132032009-06-26 18:50:28 +01002351 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002352 if (ret)
2353 goto error;
2354
2355 /* context entry init */
David Woodhouse0b9d9752014-03-09 15:48:15 -07002356 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01002357 if (ret)
2358 goto error;
2359
2360 return 0;
2361
2362 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002363 domain_exit(domain);
2364 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002365}
2366
2367static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002368 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002369{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002370 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002371 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002372 return iommu_prepare_identity_map(dev, rmrr->base_address,
2373 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002374}
2375
Suresh Siddhad3f13812011-08-23 17:05:25 -07002376#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002377static inline void iommu_prepare_isa(void)
2378{
2379 struct pci_dev *pdev;
2380 int ret;
2381
2382 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2383 if (!pdev)
2384 return;
2385
David Woodhousec7ab48d2009-06-26 19:10:36 +01002386 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002387 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002388
2389 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01002390 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
2391 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002392
Yijing Wang9b27e822014-05-20 20:37:52 +08002393 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002394}
2395#else
2396static inline void iommu_prepare_isa(void)
2397{
2398 return;
2399}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002400#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002401
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002402static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002403
Matt Kraai071e1372009-08-23 22:30:22 -07002404static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002405{
2406 struct dmar_drhd_unit *drhd;
2407 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002408 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002409
Jiang Liuab8dfe22014-07-11 14:19:27 +08002410 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002411 if (!si_domain)
2412 return -EFAULT;
2413
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002414 for_each_active_iommu(iommu, drhd) {
2415 ret = iommu_attach_domain(si_domain, iommu);
2416 if (ret) {
2417 domain_exit(si_domain);
2418 return -EFAULT;
2419 }
2420 }
2421
2422 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2423 domain_exit(si_domain);
2424 return -EFAULT;
2425 }
2426
Jiang Liu9544c002014-01-06 14:18:13 +08002427 pr_debug("IOMMU: identity mapping domain is domain %d\n",
2428 si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002429
David Woodhouse19943b02009-08-04 16:19:20 +01002430 if (hw)
2431 return 0;
2432
David Woodhousec7ab48d2009-06-26 19:10:36 +01002433 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002434 unsigned long start_pfn, end_pfn;
2435 int i;
2436
2437 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2438 ret = iommu_domain_identity_map(si_domain,
2439 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2440 if (ret)
2441 return ret;
2442 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002443 }
2444
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002445 return 0;
2446}
2447
David Woodhouse9b226622014-03-09 14:03:28 -07002448static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002449{
2450 struct device_domain_info *info;
2451
2452 if (likely(!iommu_identity_mapping))
2453 return 0;
2454
David Woodhouse9b226622014-03-09 14:03:28 -07002455 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002456 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2457 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002458
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002459 return 0;
2460}
2461
2462static int domain_add_dev_info(struct dmar_domain *domain,
David Woodhouse5913c9b2014-03-09 16:27:31 -07002463 struct device *dev, int translation)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002464{
David Woodhouse0ac72662014-03-09 13:19:22 -07002465 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002466 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002467 u8 bus, devfn;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002468 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002469
David Woodhouse5913c9b2014-03-09 16:27:31 -07002470 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002471 if (!iommu)
2472 return -ENODEV;
2473
David Woodhouse5913c9b2014-03-09 16:27:31 -07002474 ndomain = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002475 if (ndomain != domain)
2476 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002477
David Woodhouse5913c9b2014-03-09 16:27:31 -07002478 ret = domain_context_mapping(domain, dev, translation);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002479 if (ret) {
David Woodhouse5913c9b2014-03-09 16:27:31 -07002480 domain_remove_one_dev_info(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002481 return ret;
2482 }
2483
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002484 return 0;
2485}
2486
David Woodhouse0b9d9752014-03-09 15:48:15 -07002487static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002488{
2489 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002490 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002491 int i;
2492
Jiang Liu0e242612014-02-19 14:07:34 +08002493 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002494 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002495 /*
2496 * Return TRUE if this RMRR contains the device that
2497 * is passed in.
2498 */
2499 for_each_active_dev_scope(rmrr->devices,
2500 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002501 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002502 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002503 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002504 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002505 }
Jiang Liu0e242612014-02-19 14:07:34 +08002506 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002507 return false;
2508}
2509
David Woodhouse3bdb2592014-03-09 16:03:08 -07002510static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002511{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002512
David Woodhouse3bdb2592014-03-09 16:03:08 -07002513 if (dev_is_pci(dev)) {
2514 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002515
David Woodhouse3bdb2592014-03-09 16:03:08 -07002516 /*
2517 * We want to prevent any device associated with an RMRR from
2518 * getting placed into the SI Domain. This is done because
2519 * problems exist when devices are moved in and out of domains
2520 * and their respective RMRR info is lost. We exempt USB devices
2521 * from this process due to their usage of RMRRs that are known
2522 * to not be needed after BIOS hand-off to OS.
2523 */
2524 if (device_has_rmrr(dev) &&
2525 (pdev->class >> 8) != PCI_CLASS_SERIAL_USB)
2526 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002527
David Woodhouse3bdb2592014-03-09 16:03:08 -07002528 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2529 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002530
David Woodhouse3bdb2592014-03-09 16:03:08 -07002531 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2532 return 1;
2533
2534 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2535 return 0;
2536
2537 /*
2538 * We want to start off with all devices in the 1:1 domain, and
2539 * take them out later if we find they can't access all of memory.
2540 *
2541 * However, we can't do this for PCI devices behind bridges,
2542 * because all PCI devices behind the same bridge will end up
2543 * with the same source-id on their transactions.
2544 *
2545 * Practically speaking, we can't change things around for these
2546 * devices at run-time, because we can't be sure there'll be no
2547 * DMA transactions in flight for any of their siblings.
2548 *
2549 * So PCI devices (unless they're on the root bus) as well as
2550 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2551 * the 1:1 domain, just in _case_ one of their siblings turns out
2552 * not to be able to map all of memory.
2553 */
2554 if (!pci_is_pcie(pdev)) {
2555 if (!pci_is_root_bus(pdev->bus))
2556 return 0;
2557 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2558 return 0;
2559 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2560 return 0;
2561 } else {
2562 if (device_has_rmrr(dev))
2563 return 0;
2564 }
David Woodhouse6941af22009-07-04 18:24:27 +01002565
David Woodhouse3dfc8132009-07-04 19:11:08 +01002566 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002567 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002568 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002569 * take them out of the 1:1 domain later.
2570 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002571 if (!startup) {
2572 /*
2573 * If the device's dma_mask is less than the system's memory
2574 * size then this is not a candidate for identity mapping.
2575 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002576 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002577
David Woodhouse3bdb2592014-03-09 16:03:08 -07002578 if (dev->coherent_dma_mask &&
2579 dev->coherent_dma_mask < dma_mask)
2580 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002581
David Woodhouse3bdb2592014-03-09 16:03:08 -07002582 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002583 }
David Woodhouse6941af22009-07-04 18:24:27 +01002584
2585 return 1;
2586}
2587
David Woodhousecf04eee2014-03-21 16:49:04 +00002588static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2589{
2590 int ret;
2591
2592 if (!iommu_should_identity_map(dev, 1))
2593 return 0;
2594
2595 ret = domain_add_dev_info(si_domain, dev,
2596 hw ? CONTEXT_TT_PASS_THROUGH :
2597 CONTEXT_TT_MULTI_LEVEL);
2598 if (!ret)
2599 pr_info("IOMMU: %s identity mapping for device %s\n",
2600 hw ? "hardware" : "software", dev_name(dev));
2601 else if (ret == -ENODEV)
2602 /* device not associated with an iommu */
2603 ret = 0;
2604
2605 return ret;
2606}
2607
2608
Matt Kraai071e1372009-08-23 22:30:22 -07002609static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002610{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002611 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002612 struct dmar_drhd_unit *drhd;
2613 struct intel_iommu *iommu;
2614 struct device *dev;
2615 int i;
2616 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002617
David Woodhouse19943b02009-08-04 16:19:20 +01002618 ret = si_domain_init(hw);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002619 if (ret)
2620 return -EFAULT;
2621
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002622 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002623 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2624 if (ret)
2625 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002626 }
2627
David Woodhousecf04eee2014-03-21 16:49:04 +00002628 for_each_active_iommu(iommu, drhd)
2629 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2630 struct acpi_device_physical_node *pn;
2631 struct acpi_device *adev;
2632
2633 if (dev->bus != &acpi_bus_type)
2634 continue;
2635
2636 adev= to_acpi_device(dev);
2637 mutex_lock(&adev->physical_node_lock);
2638 list_for_each_entry(pn, &adev->physical_node_list, node) {
2639 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2640 if (ret)
2641 break;
2642 }
2643 mutex_unlock(&adev->physical_node_lock);
2644 if (ret)
2645 return ret;
2646 }
2647
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002648 return 0;
2649}
2650
Joseph Cihulab7792602011-05-03 00:08:37 -07002651static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002652{
2653 struct dmar_drhd_unit *drhd;
2654 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002655 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002656 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002657 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002658
2659 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002660 * for each drhd
2661 * allocate root
2662 * initialize and program root entry to not present
2663 * endfor
2664 */
2665 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002666 /*
2667 * lock not needed as this is only incremented in the single
2668 * threaded kernel __init code path all other access are read
2669 * only
2670 */
Mike Travis1b198bb2012-03-05 15:05:16 -08002671 if (g_num_of_iommus < IOMMU_UNITS_SUPPORTED) {
2672 g_num_of_iommus++;
2673 continue;
2674 }
2675 printk_once(KERN_ERR "intel-iommu: exceeded %d IOMMUs\n",
2676 IOMMU_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08002677 }
2678
Weidong Hand9630fe2008-12-08 11:06:32 +08002679 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2680 GFP_KERNEL);
2681 if (!g_iommus) {
2682 printk(KERN_ERR "Allocating global iommu array failed\n");
2683 ret = -ENOMEM;
2684 goto error;
2685 }
2686
mark gross80b20dd2008-04-18 13:53:58 -07002687 deferred_flush = kzalloc(g_num_of_iommus *
2688 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2689 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08002690 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08002691 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08002692 }
2693
Jiang Liu7c919772014-01-06 14:18:18 +08002694 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08002695 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002696
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002697 ret = iommu_init_domains(iommu);
2698 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002699 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002700
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002701 /*
2702 * TBD:
2703 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002704 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002705 */
2706 ret = iommu_alloc_root_entry(iommu);
2707 if (ret) {
2708 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08002709 goto free_iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002710 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002711 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01002712 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002713 }
2714
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002715 /*
2716 * Start from the sane iommu hardware state.
2717 */
Jiang Liu7c919772014-01-06 14:18:18 +08002718 for_each_active_iommu(iommu, drhd) {
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002719 /*
2720 * If the queued invalidation is already initialized by us
2721 * (for example, while enabling interrupt-remapping) then
2722 * we got the things already rolling from a sane state.
2723 */
2724 if (iommu->qi)
2725 continue;
2726
2727 /*
2728 * Clear any previous faults.
2729 */
2730 dmar_fault(-1, iommu);
2731 /*
2732 * Disable queued invalidation if supported and already enabled
2733 * before OS handover.
2734 */
2735 dmar_disable_qi(iommu);
2736 }
2737
Jiang Liu7c919772014-01-06 14:18:18 +08002738 for_each_active_iommu(iommu, drhd) {
Youquan Songa77b67d2008-10-16 16:31:56 -07002739 if (dmar_enable_qi(iommu)) {
2740 /*
2741 * Queued Invalidate not enabled, use Register Based
2742 * Invalidate
2743 */
2744 iommu->flush.flush_context = __iommu_flush_context;
2745 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Yinghai Lu680a7522010-04-08 19:58:23 +01002746 printk(KERN_INFO "IOMMU %d 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002747 "invalidation\n",
Yinghai Lu680a7522010-04-08 19:58:23 +01002748 iommu->seq_id,
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002749 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002750 } else {
2751 iommu->flush.flush_context = qi_flush_context;
2752 iommu->flush.flush_iotlb = qi_flush_iotlb;
Yinghai Lu680a7522010-04-08 19:58:23 +01002753 printk(KERN_INFO "IOMMU %d 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002754 "invalidation\n",
Yinghai Lu680a7522010-04-08 19:58:23 +01002755 iommu->seq_id,
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002756 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002757 }
2758 }
2759
David Woodhouse19943b02009-08-04 16:19:20 +01002760 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07002761 iommu_identity_mapping |= IDENTMAP_ALL;
2762
Suresh Siddhad3f13812011-08-23 17:05:25 -07002763#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07002764 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01002765#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07002766
2767 check_tylersburg_isoch();
2768
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002769 /*
2770 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002771 * identity mappings for rmrr, gfx, and isa and may fall back to static
2772 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002773 */
David Woodhouse19943b02009-08-04 16:19:20 +01002774 if (iommu_identity_mapping) {
2775 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
2776 if (ret) {
2777 printk(KERN_CRIT "Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08002778 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002779 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002780 }
David Woodhouse19943b02009-08-04 16:19:20 +01002781 /*
2782 * For each rmrr
2783 * for each dev attached to rmrr
2784 * do
2785 * locate drhd for dev, alloc domain for dev
2786 * allocate free domain
2787 * allocate page table entries for rmrr
2788 * if context not allocated for bus
2789 * allocate and init context
2790 * set present in root table for this bus
2791 * init context with domain, translation etc
2792 * endfor
2793 * endfor
2794 */
2795 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
2796 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002797 /* some BIOS lists non-exist devices in DMAR table. */
2798 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00002799 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07002800 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01002801 if (ret)
2802 printk(KERN_ERR
2803 "IOMMU: mapping reserved region failed\n");
2804 }
2805 }
2806
2807 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002808
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002809 /*
2810 * for each drhd
2811 * enable fault log
2812 * global invalidate context cache
2813 * global invalidate iotlb
2814 * enable translation
2815 */
Jiang Liu7c919772014-01-06 14:18:18 +08002816 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07002817 if (drhd->ignored) {
2818 /*
2819 * we always have to disable PMRs or DMA may fail on
2820 * this device
2821 */
2822 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08002823 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002824 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07002825 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002826
2827 iommu_flush_write_buffer(iommu);
2828
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002829 ret = dmar_set_interrupt(iommu);
2830 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002831 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002832
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002833 iommu_set_root_entry(iommu);
2834
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002835 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002836 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002837
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002838 ret = iommu_enable_translation(iommu);
2839 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08002840 goto free_iommu;
David Woodhouseb94996c2009-09-19 15:28:12 -07002841
2842 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002843 }
2844
2845 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08002846
2847free_iommu:
Jiang Liu7c919772014-01-06 14:18:18 +08002848 for_each_active_iommu(iommu, drhd)
Jiang Liua868e6b2014-01-06 14:18:20 +08002849 free_dmar_iommu(iommu);
Jiang Liu9bdc5312014-01-06 14:18:27 +08002850 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08002851free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08002852 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08002853error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002854 return ret;
2855}
2856
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002857/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01002858static struct iova *intel_alloc_iova(struct device *dev,
2859 struct dmar_domain *domain,
2860 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002861{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002862 struct iova *iova = NULL;
2863
David Woodhouse875764d2009-06-28 21:20:51 +01002864 /* Restrict dma_mask to the width that the iommu can handle */
2865 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
2866
2867 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002868 /*
2869 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002870 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002871 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002872 */
David Woodhouse875764d2009-06-28 21:20:51 +01002873 iova = alloc_iova(&domain->iovad, nrpages,
2874 IOVA_PFN(DMA_BIT_MASK(32)), 1);
2875 if (iova)
2876 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002877 }
David Woodhouse875764d2009-06-28 21:20:51 +01002878 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
2879 if (unlikely(!iova)) {
2880 printk(KERN_ERR "Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07002881 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002882 return NULL;
2883 }
2884
2885 return iova;
2886}
2887
David Woodhoused4b709f2014-03-09 16:07:40 -07002888static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002889{
2890 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002891 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002892
David Woodhoused4b709f2014-03-09 16:07:40 -07002893 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002894 if (!domain) {
David Woodhoused4b709f2014-03-09 16:07:40 -07002895 printk(KERN_ERR "Allocating domain for %s failed",
2896 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002897 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002898 }
2899
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002900 /* make sure context mapping is ok */
David Woodhoused4b709f2014-03-09 16:07:40 -07002901 if (unlikely(!domain_context_mapped(dev))) {
2902 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002903 if (ret) {
David Woodhoused4b709f2014-03-09 16:07:40 -07002904 printk(KERN_ERR "Domain context map for %s failed",
2905 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002906 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002907 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002908 }
2909
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002910 return domain;
2911}
2912
David Woodhoused4b709f2014-03-09 16:07:40 -07002913static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01002914{
2915 struct device_domain_info *info;
2916
2917 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07002918 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01002919 if (likely(info))
2920 return info->domain;
2921
2922 return __get_valid_domain_for_dev(dev);
2923}
2924
David Woodhouse3d891942014-03-06 15:59:26 +00002925static int iommu_dummy(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002926{
David Woodhouse3d891942014-03-06 15:59:26 +00002927 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002928}
2929
David Woodhouseecb509e2014-03-09 16:29:55 -07002930/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01002931static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002932{
2933 int found;
2934
David Woodhouse3d891942014-03-06 15:59:26 +00002935 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002936 return 1;
2937
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002938 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002939 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002940
David Woodhouse9b226622014-03-09 14:03:28 -07002941 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002942 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07002943 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002944 return 1;
2945 else {
2946 /*
2947 * 32 bit DMA is removed from si_domain and fall back
2948 * to non-identity mapping.
2949 */
David Woodhousebf9c9ed2014-03-09 16:19:13 -07002950 domain_remove_one_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002951 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
David Woodhouseecb509e2014-03-09 16:29:55 -07002952 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002953 return 0;
2954 }
2955 } else {
2956 /*
2957 * In case of a detached 64 bit DMA device from vm, the device
2958 * is put into si_domain for identity mapping.
2959 */
David Woodhouseecb509e2014-03-09 16:29:55 -07002960 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002961 int ret;
David Woodhouse5913c9b2014-03-09 16:27:31 -07002962 ret = domain_add_dev_info(si_domain, dev,
David Woodhouse5fe60f42009-08-09 10:53:41 +01002963 hw_pass_through ?
2964 CONTEXT_TT_PASS_THROUGH :
2965 CONTEXT_TT_MULTI_LEVEL);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002966 if (!ret) {
2967 printk(KERN_INFO "64bit %s uses identity mapping\n",
David Woodhouseecb509e2014-03-09 16:29:55 -07002968 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002969 return 1;
2970 }
2971 }
2972 }
2973
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002974 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002975}
2976
David Woodhouse5040a912014-03-09 16:14:00 -07002977static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002978 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002979{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002980 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002981 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002982 struct iova *iova;
2983 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002984 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002985 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07002986 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002987
2988 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002989
David Woodhouse5040a912014-03-09 16:14:00 -07002990 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002991 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002992
David Woodhouse5040a912014-03-09 16:14:00 -07002993 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002994 if (!domain)
2995 return 0;
2996
Weidong Han8c11e792008-12-08 15:29:22 +08002997 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01002998 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002999
David Woodhouse5040a912014-03-09 16:14:00 -07003000 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003001 if (!iova)
3002 goto error;
3003
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003004 /*
3005 * Check if DMAR supports zero-length reads on write only
3006 * mappings..
3007 */
3008 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003009 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003010 prot |= DMA_PTE_READ;
3011 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3012 prot |= DMA_PTE_WRITE;
3013 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003014 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003015 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003016 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003017 * is not a big problem
3018 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003019 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003020 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003021 if (ret)
3022 goto error;
3023
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003024 /* it's a non-present to present mapping. Only flush if caching mode */
3025 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003026 iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003027 else
Weidong Han8c11e792008-12-08 15:29:22 +08003028 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003029
David Woodhouse03d6a242009-06-28 15:33:46 +01003030 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3031 start_paddr += paddr & ~PAGE_MASK;
3032 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003033
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003034error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003035 if (iova)
3036 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00003037 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003038 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003039 return 0;
3040}
3041
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003042static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3043 unsigned long offset, size_t size,
3044 enum dma_data_direction dir,
3045 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003046{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003047 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003048 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003049}
3050
mark gross5e0d2a62008-03-04 15:22:08 -08003051static void flush_unmaps(void)
3052{
mark gross80b20dd2008-04-18 13:53:58 -07003053 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003054
mark gross5e0d2a62008-03-04 15:22:08 -08003055 timer_on = 0;
3056
3057 /* just flush them all */
3058 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003059 struct intel_iommu *iommu = g_iommus[i];
3060 if (!iommu)
3061 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003062
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003063 if (!deferred_flush[i].next)
3064 continue;
3065
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003066 /* In caching mode, global flushes turn emulation expensive */
3067 if (!cap_caching_mode(iommu->cap))
3068 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003069 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003070 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003071 unsigned long mask;
3072 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003073 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003074
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003075 /* On real hardware multiple invalidations are expensive */
3076 if (cap_caching_mode(iommu->cap))
3077 iommu_flush_iotlb_psi(iommu, domain->id,
David Woodhouseea8ea462014-03-05 17:09:32 +00003078 iova->pfn_lo, iova->pfn_hi - iova->pfn_lo + 1,
3079 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003080 else {
3081 mask = ilog2(mm_to_dma_pfn(iova->pfn_hi - iova->pfn_lo + 1));
3082 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3083 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3084 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003085 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003086 if (deferred_flush[i].freelist[j])
3087 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003088 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003089 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003090 }
3091
mark gross5e0d2a62008-03-04 15:22:08 -08003092 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003093}
3094
3095static void flush_unmaps_timeout(unsigned long data)
3096{
mark gross80b20dd2008-04-18 13:53:58 -07003097 unsigned long flags;
3098
3099 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003100 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003101 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003102}
3103
David Woodhouseea8ea462014-03-05 17:09:32 +00003104static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003105{
3106 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003107 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003108 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003109
3110 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003111 if (list_size == HIGH_WATER_MARK)
3112 flush_unmaps();
3113
Weidong Han8c11e792008-12-08 15:29:22 +08003114 iommu = domain_get_iommu(dom);
3115 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003116
mark gross80b20dd2008-04-18 13:53:58 -07003117 next = deferred_flush[iommu_id].next;
3118 deferred_flush[iommu_id].domain[next] = dom;
3119 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003120 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003121 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003122
3123 if (!timer_on) {
3124 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3125 timer_on = 1;
3126 }
3127 list_size++;
3128 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3129}
3130
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003131static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3132 size_t size, enum dma_data_direction dir,
3133 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003134{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003135 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003136 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003137 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003138 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003139 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003140
David Woodhouse73676832009-07-04 14:08:36 +01003141 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003142 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003143
David Woodhouse1525a292014-03-06 16:19:30 +00003144 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003145 BUG_ON(!domain);
3146
Weidong Han8c11e792008-12-08 15:29:22 +08003147 iommu = domain_get_iommu(domain);
3148
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003149 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003150 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3151 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003152 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003153
David Woodhoused794dc92009-06-28 00:27:49 +01003154 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3155 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003156
David Woodhoused794dc92009-06-28 00:27:49 +01003157 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003158 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003159
David Woodhouseea8ea462014-03-05 17:09:32 +00003160 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003161
mark gross5e0d2a62008-03-04 15:22:08 -08003162 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01003163 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003164 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003165 /* free iova */
3166 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003167 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003168 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003169 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003170 /*
3171 * queue up the release of the unmap to save the 1/6th of the
3172 * cpu used up by the iotlb flush operation...
3173 */
mark gross5e0d2a62008-03-04 15:22:08 -08003174 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003175}
3176
David Woodhouse5040a912014-03-09 16:14:00 -07003177static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003178 dma_addr_t *dma_handle, gfp_t flags,
3179 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003180{
Akinobu Mita36746432014-06-04 16:06:51 -07003181 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003182 int order;
3183
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003184 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003185 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003186
David Woodhouse5040a912014-03-09 16:14:00 -07003187 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003188 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003189 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3190 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003191 flags |= GFP_DMA;
3192 else
3193 flags |= GFP_DMA32;
3194 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003195
Akinobu Mita36746432014-06-04 16:06:51 -07003196 if (flags & __GFP_WAIT) {
3197 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003198
Akinobu Mita36746432014-06-04 16:06:51 -07003199 page = dma_alloc_from_contiguous(dev, count, order);
3200 if (page && iommu_no_mapping(dev) &&
3201 page_to_phys(page) + size > dev->coherent_dma_mask) {
3202 dma_release_from_contiguous(dev, page, count);
3203 page = NULL;
3204 }
3205 }
3206
3207 if (!page)
3208 page = alloc_pages(flags, order);
3209 if (!page)
3210 return NULL;
3211 memset(page_address(page), 0, size);
3212
3213 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003214 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003215 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003216 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003217 return page_address(page);
3218 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3219 __free_pages(page, order);
3220
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003221 return NULL;
3222}
3223
David Woodhouse5040a912014-03-09 16:14:00 -07003224static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003225 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003226{
3227 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003228 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003229
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003230 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003231 order = get_order(size);
3232
David Woodhouse5040a912014-03-09 16:14:00 -07003233 intel_unmap_page(dev, dma_handle, size, DMA_BIDIRECTIONAL, NULL);
Akinobu Mita36746432014-06-04 16:06:51 -07003234 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3235 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003236}
3237
David Woodhouse5040a912014-03-09 16:14:00 -07003238static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003239 int nelems, enum dma_data_direction dir,
3240 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003241{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003242 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003243 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003244 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003245 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003246 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003247
David Woodhouse5040a912014-03-09 16:14:00 -07003248 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003249 return;
3250
David Woodhouse5040a912014-03-09 16:14:00 -07003251 domain = find_domain(dev);
Weidong Han8c11e792008-12-08 15:29:22 +08003252 BUG_ON(!domain);
3253
3254 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003255
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003256 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
David Woodhouse85b98272009-07-01 19:27:53 +01003257 if (WARN_ONCE(!iova, "Driver unmaps unmatched sglist at PFN %llx\n",
3258 (unsigned long long)sglist[0].dma_address))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003259 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003260
David Woodhoused794dc92009-06-28 00:27:49 +01003261 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3262 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003263
David Woodhouseea8ea462014-03-05 17:09:32 +00003264 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003265
David Woodhouseacea0012009-07-14 01:55:11 +01003266 if (intel_iommu_strict) {
3267 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003268 last_pfn - start_pfn + 1, !freelist, 0);
David Woodhouseacea0012009-07-14 01:55:11 +01003269 /* free iova */
3270 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003271 dma_free_pagelist(freelist);
David Woodhouseacea0012009-07-14 01:55:11 +01003272 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003273 add_unmap(domain, iova, freelist);
David Woodhouseacea0012009-07-14 01:55:11 +01003274 /*
3275 * queue up the release of the unmap to save the 1/6th of the
3276 * cpu used up by the iotlb flush operation...
3277 */
3278 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003279}
3280
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003281static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003282 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003283{
3284 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003285 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003286
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003287 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003288 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003289 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003290 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003291 }
3292 return nelems;
3293}
3294
David Woodhouse5040a912014-03-09 16:14:00 -07003295static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003296 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003297{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003298 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003299 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003300 size_t size = 0;
3301 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003302 struct iova *iova = NULL;
3303 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003304 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003305 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003306 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003307
3308 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003309 if (iommu_no_mapping(dev))
3310 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003311
David Woodhouse5040a912014-03-09 16:14:00 -07003312 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003313 if (!domain)
3314 return 0;
3315
Weidong Han8c11e792008-12-08 15:29:22 +08003316 iommu = domain_get_iommu(domain);
3317
David Woodhouseb536d242009-06-28 14:49:31 +01003318 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003319 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003320
David Woodhouse5040a912014-03-09 16:14:00 -07003321 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3322 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003323 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003324 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003325 return 0;
3326 }
3327
3328 /*
3329 * Check if DMAR supports zero-length reads on write only
3330 * mappings..
3331 */
3332 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003333 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003334 prot |= DMA_PTE_READ;
3335 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3336 prot |= DMA_PTE_WRITE;
3337
David Woodhouseb536d242009-06-28 14:49:31 +01003338 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003339
Fenghua Yuf5329592009-08-04 15:09:37 -07003340 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003341 if (unlikely(ret)) {
3342 /* clear the page */
3343 dma_pte_clear_range(domain, start_vpfn,
3344 start_vpfn + size - 1);
3345 /* free page tables */
3346 dma_pte_free_pagetable(domain, start_vpfn,
3347 start_vpfn + size - 1);
3348 /* free iova */
3349 __free_iova(&domain->iovad, iova);
3350 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003351 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003352
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003353 /* it's a non-present to present mapping. Only flush if caching mode */
3354 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003355 iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003356 else
Weidong Han8c11e792008-12-08 15:29:22 +08003357 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003358
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003359 return nelems;
3360}
3361
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003362static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3363{
3364 return !dma_addr;
3365}
3366
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003367struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003368 .alloc = intel_alloc_coherent,
3369 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003370 .map_sg = intel_map_sg,
3371 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003372 .map_page = intel_map_page,
3373 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003374 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003375};
3376
3377static inline int iommu_domain_cache_init(void)
3378{
3379 int ret = 0;
3380
3381 iommu_domain_cache = kmem_cache_create("iommu_domain",
3382 sizeof(struct dmar_domain),
3383 0,
3384 SLAB_HWCACHE_ALIGN,
3385
3386 NULL);
3387 if (!iommu_domain_cache) {
3388 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
3389 ret = -ENOMEM;
3390 }
3391
3392 return ret;
3393}
3394
3395static inline int iommu_devinfo_cache_init(void)
3396{
3397 int ret = 0;
3398
3399 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3400 sizeof(struct device_domain_info),
3401 0,
3402 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003403 NULL);
3404 if (!iommu_devinfo_cache) {
3405 printk(KERN_ERR "Couldn't create devinfo cache\n");
3406 ret = -ENOMEM;
3407 }
3408
3409 return ret;
3410}
3411
3412static inline int iommu_iova_cache_init(void)
3413{
3414 int ret = 0;
3415
3416 iommu_iova_cache = kmem_cache_create("iommu_iova",
3417 sizeof(struct iova),
3418 0,
3419 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003420 NULL);
3421 if (!iommu_iova_cache) {
3422 printk(KERN_ERR "Couldn't create iova cache\n");
3423 ret = -ENOMEM;
3424 }
3425
3426 return ret;
3427}
3428
3429static int __init iommu_init_mempool(void)
3430{
3431 int ret;
3432 ret = iommu_iova_cache_init();
3433 if (ret)
3434 return ret;
3435
3436 ret = iommu_domain_cache_init();
3437 if (ret)
3438 goto domain_error;
3439
3440 ret = iommu_devinfo_cache_init();
3441 if (!ret)
3442 return ret;
3443
3444 kmem_cache_destroy(iommu_domain_cache);
3445domain_error:
3446 kmem_cache_destroy(iommu_iova_cache);
3447
3448 return -ENOMEM;
3449}
3450
3451static void __init iommu_exit_mempool(void)
3452{
3453 kmem_cache_destroy(iommu_devinfo_cache);
3454 kmem_cache_destroy(iommu_domain_cache);
3455 kmem_cache_destroy(iommu_iova_cache);
3456
3457}
3458
Dan Williams556ab452010-07-23 15:47:56 -07003459static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3460{
3461 struct dmar_drhd_unit *drhd;
3462 u32 vtbar;
3463 int rc;
3464
3465 /* We know that this device on this chipset has its own IOMMU.
3466 * If we find it under a different IOMMU, then the BIOS is lying
3467 * to us. Hope that the IOMMU for this device is actually
3468 * disabled, and it needs no translation...
3469 */
3470 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3471 if (rc) {
3472 /* "can't" happen */
3473 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3474 return;
3475 }
3476 vtbar &= 0xffff0000;
3477
3478 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3479 drhd = dmar_find_matched_drhd_unit(pdev);
3480 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3481 TAINT_FIRMWARE_WORKAROUND,
3482 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3483 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3484}
3485DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3486
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003487static void __init init_no_remapping_devices(void)
3488{
3489 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003490 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003491 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003492
3493 for_each_drhd_unit(drhd) {
3494 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003495 for_each_active_dev_scope(drhd->devices,
3496 drhd->devices_cnt, i, dev)
3497 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003498 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003499 if (i == drhd->devices_cnt)
3500 drhd->ignored = 1;
3501 }
3502 }
3503
Jiang Liu7c919772014-01-06 14:18:18 +08003504 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003505 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003506 continue;
3507
Jiang Liub683b232014-02-19 14:07:32 +08003508 for_each_active_dev_scope(drhd->devices,
3509 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003510 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003511 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003512 if (i < drhd->devices_cnt)
3513 continue;
3514
David Woodhousec0771df2011-10-14 20:59:46 +01003515 /* This IOMMU has *only* gfx devices. Either bypass it or
3516 set the gfx_mapped flag, as appropriate */
3517 if (dmar_map_gfx) {
3518 intel_iommu_gfx_mapped = 1;
3519 } else {
3520 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003521 for_each_active_dev_scope(drhd->devices,
3522 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003523 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003524 }
3525 }
3526}
3527
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003528#ifdef CONFIG_SUSPEND
3529static int init_iommu_hw(void)
3530{
3531 struct dmar_drhd_unit *drhd;
3532 struct intel_iommu *iommu = NULL;
3533
3534 for_each_active_iommu(iommu, drhd)
3535 if (iommu->qi)
3536 dmar_reenable_qi(iommu);
3537
Joseph Cihulab7792602011-05-03 00:08:37 -07003538 for_each_iommu(iommu, drhd) {
3539 if (drhd->ignored) {
3540 /*
3541 * we always have to disable PMRs or DMA may fail on
3542 * this device
3543 */
3544 if (force_on)
3545 iommu_disable_protect_mem_regions(iommu);
3546 continue;
3547 }
3548
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003549 iommu_flush_write_buffer(iommu);
3550
3551 iommu_set_root_entry(iommu);
3552
3553 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003554 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003555 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003556 DMA_TLB_GLOBAL_FLUSH);
Joseph Cihulab7792602011-05-03 00:08:37 -07003557 if (iommu_enable_translation(iommu))
3558 return 1;
David Woodhouseb94996c2009-09-19 15:28:12 -07003559 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003560 }
3561
3562 return 0;
3563}
3564
3565static void iommu_flush_all(void)
3566{
3567 struct dmar_drhd_unit *drhd;
3568 struct intel_iommu *iommu;
3569
3570 for_each_active_iommu(iommu, drhd) {
3571 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003572 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003573 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003574 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003575 }
3576}
3577
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003578static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003579{
3580 struct dmar_drhd_unit *drhd;
3581 struct intel_iommu *iommu = NULL;
3582 unsigned long flag;
3583
3584 for_each_active_iommu(iommu, drhd) {
3585 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3586 GFP_ATOMIC);
3587 if (!iommu->iommu_state)
3588 goto nomem;
3589 }
3590
3591 iommu_flush_all();
3592
3593 for_each_active_iommu(iommu, drhd) {
3594 iommu_disable_translation(iommu);
3595
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003596 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003597
3598 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3599 readl(iommu->reg + DMAR_FECTL_REG);
3600 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3601 readl(iommu->reg + DMAR_FEDATA_REG);
3602 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3603 readl(iommu->reg + DMAR_FEADDR_REG);
3604 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3605 readl(iommu->reg + DMAR_FEUADDR_REG);
3606
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003607 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003608 }
3609 return 0;
3610
3611nomem:
3612 for_each_active_iommu(iommu, drhd)
3613 kfree(iommu->iommu_state);
3614
3615 return -ENOMEM;
3616}
3617
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003618static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003619{
3620 struct dmar_drhd_unit *drhd;
3621 struct intel_iommu *iommu = NULL;
3622 unsigned long flag;
3623
3624 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003625 if (force_on)
3626 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3627 else
3628 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003629 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003630 }
3631
3632 for_each_active_iommu(iommu, drhd) {
3633
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003634 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003635
3636 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3637 iommu->reg + DMAR_FECTL_REG);
3638 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3639 iommu->reg + DMAR_FEDATA_REG);
3640 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3641 iommu->reg + DMAR_FEADDR_REG);
3642 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3643 iommu->reg + DMAR_FEUADDR_REG);
3644
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003645 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003646 }
3647
3648 for_each_active_iommu(iommu, drhd)
3649 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003650}
3651
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003652static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003653 .resume = iommu_resume,
3654 .suspend = iommu_suspend,
3655};
3656
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003657static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003658{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003659 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003660}
3661
3662#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003663static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003664#endif /* CONFIG_PM */
3665
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003666
3667int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header)
3668{
3669 struct acpi_dmar_reserved_memory *rmrr;
3670 struct dmar_rmrr_unit *rmrru;
3671
3672 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3673 if (!rmrru)
3674 return -ENOMEM;
3675
3676 rmrru->hdr = header;
3677 rmrr = (struct acpi_dmar_reserved_memory *)header;
3678 rmrru->base_address = rmrr->base_address;
3679 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08003680 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
3681 ((void *)rmrr) + rmrr->header.length,
3682 &rmrru->devices_cnt);
3683 if (rmrru->devices_cnt && rmrru->devices == NULL) {
3684 kfree(rmrru);
3685 return -ENOMEM;
3686 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003687
Jiang Liu2e455282014-02-19 14:07:36 +08003688 list_add(&rmrru->list, &dmar_rmrr_units);
3689
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003690 return 0;
3691}
3692
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003693int __init dmar_parse_one_atsr(struct acpi_dmar_header *hdr)
3694{
3695 struct acpi_dmar_atsr *atsr;
3696 struct dmar_atsr_unit *atsru;
3697
3698 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
3699 atsru = kzalloc(sizeof(*atsru), GFP_KERNEL);
3700 if (!atsru)
3701 return -ENOMEM;
3702
3703 atsru->hdr = hdr;
3704 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08003705 if (!atsru->include_all) {
3706 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
3707 (void *)atsr + atsr->header.length,
3708 &atsru->devices_cnt);
3709 if (atsru->devices_cnt && atsru->devices == NULL) {
3710 kfree(atsru);
3711 return -ENOMEM;
3712 }
3713 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003714
Jiang Liu0e242612014-02-19 14:07:34 +08003715 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003716
3717 return 0;
3718}
3719
Jiang Liu9bdc5312014-01-06 14:18:27 +08003720static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
3721{
3722 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
3723 kfree(atsru);
3724}
3725
3726static void intel_iommu_free_dmars(void)
3727{
3728 struct dmar_rmrr_unit *rmrru, *rmrr_n;
3729 struct dmar_atsr_unit *atsru, *atsr_n;
3730
3731 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
3732 list_del(&rmrru->list);
3733 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
3734 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003735 }
3736
Jiang Liu9bdc5312014-01-06 14:18:27 +08003737 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
3738 list_del(&atsru->list);
3739 intel_iommu_free_atsr(atsru);
3740 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003741}
3742
3743int dmar_find_matched_atsr_unit(struct pci_dev *dev)
3744{
Jiang Liub683b232014-02-19 14:07:32 +08003745 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003746 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00003747 struct pci_dev *bridge = NULL;
3748 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003749 struct acpi_dmar_atsr *atsr;
3750 struct dmar_atsr_unit *atsru;
3751
3752 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003753 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08003754 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003755 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08003756 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003757 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003758 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003759 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003760 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08003761 if (!bridge)
3762 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003763
Jiang Liu0e242612014-02-19 14:07:34 +08003764 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08003765 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3766 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
3767 if (atsr->segment != pci_domain_nr(dev->bus))
3768 continue;
3769
Jiang Liub683b232014-02-19 14:07:32 +08003770 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00003771 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08003772 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003773
3774 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08003775 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08003776 }
Jiang Liub683b232014-02-19 14:07:32 +08003777 ret = 0;
3778out:
Jiang Liu0e242612014-02-19 14:07:34 +08003779 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003780
Jiang Liub683b232014-02-19 14:07:32 +08003781 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003782}
3783
Jiang Liu59ce0512014-02-19 14:07:35 +08003784int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
3785{
3786 int ret = 0;
3787 struct dmar_rmrr_unit *rmrru;
3788 struct dmar_atsr_unit *atsru;
3789 struct acpi_dmar_atsr *atsr;
3790 struct acpi_dmar_reserved_memory *rmrr;
3791
3792 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
3793 return 0;
3794
3795 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
3796 rmrr = container_of(rmrru->hdr,
3797 struct acpi_dmar_reserved_memory, header);
3798 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
3799 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
3800 ((void *)rmrr) + rmrr->header.length,
3801 rmrr->segment, rmrru->devices,
3802 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08003803 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08003804 return ret;
3805 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08003806 dmar_remove_dev_scope(info, rmrr->segment,
3807 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08003808 }
3809 }
3810
3811 list_for_each_entry(atsru, &dmar_atsr_units, list) {
3812 if (atsru->include_all)
3813 continue;
3814
3815 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
3816 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
3817 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
3818 (void *)atsr + atsr->header.length,
3819 atsr->segment, atsru->devices,
3820 atsru->devices_cnt);
3821 if (ret > 0)
3822 break;
3823 else if(ret < 0)
3824 return ret;
3825 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
3826 if (dmar_remove_dev_scope(info, atsr->segment,
3827 atsru->devices, atsru->devices_cnt))
3828 break;
3829 }
3830 }
3831
3832 return 0;
3833}
3834
Fenghua Yu99dcade2009-11-11 07:23:06 -08003835/*
3836 * Here we only respond to action of unbound device from driver.
3837 *
3838 * Added device is not attached to its DMAR domain here yet. That will happen
3839 * when mapping the device to iova.
3840 */
3841static int device_notifier(struct notifier_block *nb,
3842 unsigned long action, void *data)
3843{
3844 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08003845 struct dmar_domain *domain;
3846
David Woodhouse3d891942014-03-06 15:59:26 +00003847 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00003848 return 0;
3849
Jiang Liu7e7dfab2014-02-19 14:07:23 +08003850 if (action != BUS_NOTIFY_UNBOUND_DRIVER &&
3851 action != BUS_NOTIFY_DEL_DEVICE)
3852 return 0;
3853
David Woodhouse1525a292014-03-06 16:19:30 +00003854 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08003855 if (!domain)
3856 return 0;
3857
Jiang Liu3a5670e2014-02-19 14:07:33 +08003858 down_read(&dmar_global_lock);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07003859 domain_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08003860 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08003861 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08003862 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07003863
Fenghua Yu99dcade2009-11-11 07:23:06 -08003864 return 0;
3865}
3866
3867static struct notifier_block device_nb = {
3868 .notifier_call = device_notifier,
3869};
3870
Jiang Liu75f05562014-02-19 14:07:37 +08003871static int intel_iommu_memory_notifier(struct notifier_block *nb,
3872 unsigned long val, void *v)
3873{
3874 struct memory_notify *mhp = v;
3875 unsigned long long start, end;
3876 unsigned long start_vpfn, last_vpfn;
3877
3878 switch (val) {
3879 case MEM_GOING_ONLINE:
3880 start = mhp->start_pfn << PAGE_SHIFT;
3881 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
3882 if (iommu_domain_identity_map(si_domain, start, end)) {
3883 pr_warn("dmar: failed to build identity map for [%llx-%llx]\n",
3884 start, end);
3885 return NOTIFY_BAD;
3886 }
3887 break;
3888
3889 case MEM_OFFLINE:
3890 case MEM_CANCEL_ONLINE:
3891 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
3892 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
3893 while (start_vpfn <= last_vpfn) {
3894 struct iova *iova;
3895 struct dmar_drhd_unit *drhd;
3896 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003897 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08003898
3899 iova = find_iova(&si_domain->iovad, start_vpfn);
3900 if (iova == NULL) {
3901 pr_debug("dmar: failed get IOVA for PFN %lx\n",
3902 start_vpfn);
3903 break;
3904 }
3905
3906 iova = split_and_remove_iova(&si_domain->iovad, iova,
3907 start_vpfn, last_vpfn);
3908 if (iova == NULL) {
3909 pr_warn("dmar: failed to split IOVA PFN [%lx-%lx]\n",
3910 start_vpfn, last_vpfn);
3911 return NOTIFY_BAD;
3912 }
3913
David Woodhouseea8ea462014-03-05 17:09:32 +00003914 freelist = domain_unmap(si_domain, iova->pfn_lo,
3915 iova->pfn_hi);
3916
Jiang Liu75f05562014-02-19 14:07:37 +08003917 rcu_read_lock();
3918 for_each_active_iommu(iommu, drhd)
3919 iommu_flush_iotlb_psi(iommu, si_domain->id,
3920 iova->pfn_lo,
David Woodhouseea8ea462014-03-05 17:09:32 +00003921 iova->pfn_hi - iova->pfn_lo + 1,
3922 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08003923 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00003924 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08003925
3926 start_vpfn = iova->pfn_hi + 1;
3927 free_iova_mem(iova);
3928 }
3929 break;
3930 }
3931
3932 return NOTIFY_OK;
3933}
3934
3935static struct notifier_block intel_iommu_memory_nb = {
3936 .notifier_call = intel_iommu_memory_notifier,
3937 .priority = 0
3938};
3939
Alex Williamsona5459cf2014-06-12 16:12:31 -06003940
3941static ssize_t intel_iommu_show_version(struct device *dev,
3942 struct device_attribute *attr,
3943 char *buf)
3944{
3945 struct intel_iommu *iommu = dev_get_drvdata(dev);
3946 u32 ver = readl(iommu->reg + DMAR_VER_REG);
3947 return sprintf(buf, "%d:%d\n",
3948 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
3949}
3950static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
3951
3952static ssize_t intel_iommu_show_address(struct device *dev,
3953 struct device_attribute *attr,
3954 char *buf)
3955{
3956 struct intel_iommu *iommu = dev_get_drvdata(dev);
3957 return sprintf(buf, "%llx\n", iommu->reg_phys);
3958}
3959static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
3960
3961static ssize_t intel_iommu_show_cap(struct device *dev,
3962 struct device_attribute *attr,
3963 char *buf)
3964{
3965 struct intel_iommu *iommu = dev_get_drvdata(dev);
3966 return sprintf(buf, "%llx\n", iommu->cap);
3967}
3968static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
3969
3970static ssize_t intel_iommu_show_ecap(struct device *dev,
3971 struct device_attribute *attr,
3972 char *buf)
3973{
3974 struct intel_iommu *iommu = dev_get_drvdata(dev);
3975 return sprintf(buf, "%llx\n", iommu->ecap);
3976}
3977static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
3978
3979static struct attribute *intel_iommu_attrs[] = {
3980 &dev_attr_version.attr,
3981 &dev_attr_address.attr,
3982 &dev_attr_cap.attr,
3983 &dev_attr_ecap.attr,
3984 NULL,
3985};
3986
3987static struct attribute_group intel_iommu_group = {
3988 .name = "intel-iommu",
3989 .attrs = intel_iommu_attrs,
3990};
3991
3992const struct attribute_group *intel_iommu_groups[] = {
3993 &intel_iommu_group,
3994 NULL,
3995};
3996
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003997int __init intel_iommu_init(void)
3998{
Jiang Liu9bdc5312014-01-06 14:18:27 +08003999 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004000 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004001 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004002
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004003 /* VT-d is required for a TXT/tboot launch, so enforce that */
4004 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004005
Jiang Liu3a5670e2014-02-19 14:07:33 +08004006 if (iommu_init_mempool()) {
4007 if (force_on)
4008 panic("tboot: Failed to initialize iommu memory\n");
4009 return -ENOMEM;
4010 }
4011
4012 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004013 if (dmar_table_init()) {
4014 if (force_on)
4015 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004016 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004017 }
4018
Takao Indoh3a93c842013-04-23 17:35:03 +09004019 /*
4020 * Disable translation if already enabled prior to OS handover.
4021 */
Jiang Liu7c919772014-01-06 14:18:18 +08004022 for_each_active_iommu(iommu, drhd)
Takao Indoh3a93c842013-04-23 17:35:03 +09004023 if (iommu->gcmd & DMA_GCMD_TE)
4024 iommu_disable_translation(iommu);
Takao Indoh3a93c842013-04-23 17:35:03 +09004025
Suresh Siddhac2c72862011-08-23 17:05:19 -07004026 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004027 if (force_on)
4028 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004029 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004030 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004031
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004032 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004033 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004034
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004035 if (list_empty(&dmar_rmrr_units))
4036 printk(KERN_INFO "DMAR: No RMRR found\n");
4037
4038 if (list_empty(&dmar_atsr_units))
4039 printk(KERN_INFO "DMAR: No ATSR found\n");
4040
Joseph Cihula51a63e62011-03-21 11:04:24 -07004041 if (dmar_init_reserved_ranges()) {
4042 if (force_on)
4043 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004044 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004045 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004046
4047 init_no_remapping_devices();
4048
Joseph Cihulab7792602011-05-03 00:08:37 -07004049 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004050 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004051 if (force_on)
4052 panic("tboot: Failed to initialize DMARs\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004053 printk(KERN_ERR "IOMMU: dmar init failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004054 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004055 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004056 up_write(&dmar_global_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004057 printk(KERN_INFO
4058 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
4059
mark gross5e0d2a62008-03-04 15:22:08 -08004060 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004061#ifdef CONFIG_SWIOTLB
4062 swiotlb = 0;
4063#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004064 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004065
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004066 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004067
Alex Williamsona5459cf2014-06-12 16:12:31 -06004068 for_each_active_iommu(iommu, drhd)
4069 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4070 intel_iommu_groups,
4071 iommu->name);
4072
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004073 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004074 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004075 if (si_domain && !hw_pass_through)
4076 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004077
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004078 intel_iommu_enabled = 1;
4079
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004080 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004081
4082out_free_reserved_range:
4083 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004084out_free_dmar:
4085 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004086 up_write(&dmar_global_lock);
4087 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004088 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004089}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004090
Alex Williamson579305f2014-07-03 09:51:43 -06004091static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque)
4092{
4093 struct intel_iommu *iommu = opaque;
4094
4095 iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff);
4096 return 0;
4097}
4098
4099/*
4100 * NB - intel-iommu lacks any sort of reference counting for the users of
4101 * dependent devices. If multiple endpoints have intersecting dependent
4102 * devices, unbinding the driver from any one of them will possibly leave
4103 * the others unable to operate.
4104 */
Han, Weidong3199aa62009-02-26 17:31:12 +08004105static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004106 struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004107{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004108 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004109 return;
4110
Alex Williamson579305f2014-07-03 09:51:43 -06004111 pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004112}
4113
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004114static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004115 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004116{
Yijing Wangbca2b912013-10-31 17:26:04 +08004117 struct device_domain_info *info, *tmp;
Weidong Hanc7151a82008-12-08 22:51:37 +08004118 struct intel_iommu *iommu;
4119 unsigned long flags;
4120 int found = 0;
David Woodhouse156baca2014-03-09 14:00:57 -07004121 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004122
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004123 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004124 if (!iommu)
4125 return;
4126
4127 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wangbca2b912013-10-31 17:26:04 +08004128 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004129 if (info->iommu == iommu && info->bus == bus &&
4130 info->devfn == devfn) {
David Woodhouse109b9b02012-05-25 17:43:02 +01004131 unlink_domain_info(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004132 spin_unlock_irqrestore(&device_domain_lock, flags);
4133
Yu Zhao93a23a72009-05-18 13:51:37 +08004134 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004135 iommu_detach_dev(iommu, info->bus, info->devfn);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004136 iommu_detach_dependent_devices(iommu, dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08004137 free_devinfo_mem(info);
4138
4139 spin_lock_irqsave(&device_domain_lock, flags);
4140
4141 if (found)
4142 break;
4143 else
4144 continue;
4145 }
4146
4147 /* if there is no other devices under the same iommu
4148 * owned by this domain, clear this iommu in iommu_bmp
4149 * update iommu count and coherency
4150 */
David Woodhouse8bbc4412014-03-09 13:52:37 -07004151 if (info->iommu == iommu)
Weidong Hanc7151a82008-12-08 22:51:37 +08004152 found = 1;
4153 }
4154
Roland Dreier3e7abe22011-07-20 06:22:21 -07004155 spin_unlock_irqrestore(&device_domain_lock, flags);
4156
Weidong Hanc7151a82008-12-08 22:51:37 +08004157 if (found == 0) {
4158 unsigned long tmp_flags;
4159 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
Mike Travis1b198bb2012-03-05 15:05:16 -08004160 clear_bit(iommu->seq_id, domain->iommu_bmp);
Weidong Hanc7151a82008-12-08 22:51:37 +08004161 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08004162 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08004163 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
Alex Williamsona97590e2011-03-04 14:52:16 -07004164
Jiang Liuab8dfe22014-07-11 14:19:27 +08004165 if (!domain_type_is_vm_or_si(domain)) {
Alex Williamson9b4554b2011-05-24 12:19:04 -04004166 spin_lock_irqsave(&iommu->lock, tmp_flags);
4167 clear_bit(domain->id, iommu->domain_ids);
4168 iommu->domains[domain->id] = NULL;
4169 spin_unlock_irqrestore(&iommu->lock, tmp_flags);
4170 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004171 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004172}
4173
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004174static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004175{
4176 int adjust_width;
4177
4178 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004179 domain_reserve_special_ranges(domain);
4180
4181 /* calculate AGAW */
4182 domain->gaw = guest_width;
4183 adjust_width = guestwidth_to_adjustwidth(guest_width);
4184 domain->agaw = width_to_agaw(adjust_width);
4185
Weidong Han5e98c4b2008-12-08 23:03:27 +08004186 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004187 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004188 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004189 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004190
4191 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004192 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004193 if (!domain->pgd)
4194 return -ENOMEM;
4195 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4196 return 0;
4197}
4198
Joerg Roedel5d450802008-12-03 14:52:32 +01004199static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004200{
Joerg Roedel5d450802008-12-03 14:52:32 +01004201 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004202
Jiang Liuab8dfe22014-07-11 14:19:27 +08004203 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004204 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03004205 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01004206 "intel_iommu_domain_init: dmar_domain == NULL\n");
4207 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03004208 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004209 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03004210 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01004211 "intel_iommu_domain_init() failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004212 domain_exit(dmar_domain);
Joerg Roedel5d450802008-12-03 14:52:32 +01004213 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03004214 }
Allen Kay8140a952011-10-14 12:32:17 -07004215 domain_update_iommu_cap(dmar_domain);
Joerg Roedel5d450802008-12-03 14:52:32 +01004216 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004217
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004218 domain->geometry.aperture_start = 0;
4219 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4220 domain->geometry.force_aperture = true;
4221
Joerg Roedel5d450802008-12-03 14:52:32 +01004222 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004223}
Kay, Allen M38717942008-09-09 18:37:29 +03004224
Joerg Roedel5d450802008-12-03 14:52:32 +01004225static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004226{
Joerg Roedel5d450802008-12-03 14:52:32 +01004227 struct dmar_domain *dmar_domain = domain->priv;
4228
4229 domain->priv = NULL;
Jiang Liu92d03cc2014-02-19 14:07:28 +08004230 domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004231}
Kay, Allen M38717942008-09-09 18:37:29 +03004232
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004233static int intel_iommu_attach_device(struct iommu_domain *domain,
4234 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004235{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004236 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004237 struct intel_iommu *iommu;
4238 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004239 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004240
David Woodhouse7207d8f2014-03-09 16:31:06 -07004241 /* normally dev is not mapped */
4242 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004243 struct dmar_domain *old_domain;
4244
David Woodhouse1525a292014-03-06 16:19:30 +00004245 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004246 if (old_domain) {
Jiang Liuab8dfe22014-07-11 14:19:27 +08004247 if (domain_type_is_vm_or_si(dmar_domain))
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004248 domain_remove_one_dev_info(old_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004249 else
4250 domain_remove_dev_info(old_domain);
4251 }
4252 }
4253
David Woodhouse156baca2014-03-09 14:00:57 -07004254 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004255 if (!iommu)
4256 return -ENODEV;
4257
4258 /* check if this iommu agaw is sufficient for max mapped address */
4259 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004260 if (addr_width > cap_mgaw(iommu->cap))
4261 addr_width = cap_mgaw(iommu->cap);
4262
4263 if (dmar_domain->max_addr > (1LL << addr_width)) {
4264 printk(KERN_ERR "%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004265 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004266 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004267 return -EFAULT;
4268 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004269 dmar_domain->gaw = addr_width;
4270
4271 /*
4272 * Knock out extra levels of page tables if necessary
4273 */
4274 while (iommu->agaw < dmar_domain->agaw) {
4275 struct dma_pte *pte;
4276
4277 pte = dmar_domain->pgd;
4278 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004279 dmar_domain->pgd = (struct dma_pte *)
4280 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004281 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004282 }
4283 dmar_domain->agaw--;
4284 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004285
David Woodhouse5913c9b2014-03-09 16:27:31 -07004286 return domain_add_dev_info(dmar_domain, dev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004287}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004288
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004289static void intel_iommu_detach_device(struct iommu_domain *domain,
4290 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004291{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004292 struct dmar_domain *dmar_domain = domain->priv;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004293
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004294 domain_remove_one_dev_info(dmar_domain, dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004295}
Kay, Allen M38717942008-09-09 18:37:29 +03004296
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004297static int intel_iommu_map(struct iommu_domain *domain,
4298 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004299 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004300{
Joerg Roedeldde57a22008-12-03 15:04:09 +01004301 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004302 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004303 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004304 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004305
Joerg Roedeldde57a22008-12-03 15:04:09 +01004306 if (iommu_prot & IOMMU_READ)
4307 prot |= DMA_PTE_READ;
4308 if (iommu_prot & IOMMU_WRITE)
4309 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08004310 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4311 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004312
David Woodhouse163cc522009-06-28 00:51:17 +01004313 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004314 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004315 u64 end;
4316
4317 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004318 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004319 if (end < max_addr) {
Tom Lyon8954da12010-05-17 08:19:52 +01004320 printk(KERN_ERR "%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004321 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004322 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004323 return -EFAULT;
4324 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004325 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004326 }
David Woodhousead051222009-06-28 14:22:28 +01004327 /* Round up size to next multiple of PAGE_SIZE, if it and
4328 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004329 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004330 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4331 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004332 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004333}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004334
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004335static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004336 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004337{
Joerg Roedeldde57a22008-12-03 15:04:09 +01004338 struct dmar_domain *dmar_domain = domain->priv;
David Woodhouseea8ea462014-03-05 17:09:32 +00004339 struct page *freelist = NULL;
4340 struct intel_iommu *iommu;
4341 unsigned long start_pfn, last_pfn;
4342 unsigned int npages;
4343 int iommu_id, num, ndomains, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004344
David Woodhouse5cf0a762014-03-19 16:07:49 +00004345 /* Cope with horrid API which requires us to unmap more than the
4346 size argument if it happens to be a large-page mapping. */
4347 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4348 BUG();
4349
4350 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4351 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4352
David Woodhouseea8ea462014-03-05 17:09:32 +00004353 start_pfn = iova >> VTD_PAGE_SHIFT;
4354 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4355
4356 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4357
4358 npages = last_pfn - start_pfn + 1;
4359
4360 for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) {
4361 iommu = g_iommus[iommu_id];
4362
4363 /*
4364 * find bit position of dmar_domain
4365 */
4366 ndomains = cap_ndoms(iommu->cap);
4367 for_each_set_bit(num, iommu->domain_ids, ndomains) {
4368 if (iommu->domains[num] == dmar_domain)
4369 iommu_flush_iotlb_psi(iommu, num, start_pfn,
4370 npages, !freelist, 0);
4371 }
4372
4373 }
4374
4375 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004376
David Woodhouse163cc522009-06-28 00:51:17 +01004377 if (dmar_domain->max_addr == iova + size)
4378 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004379
David Woodhouse5cf0a762014-03-19 16:07:49 +00004380 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004381}
Kay, Allen M38717942008-09-09 18:37:29 +03004382
Joerg Roedeld14d6572008-12-03 15:06:57 +01004383static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05304384 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004385{
Joerg Roedeld14d6572008-12-03 15:06:57 +01004386 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03004387 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004388 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004389 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004390
David Woodhouse5cf0a762014-03-19 16:07:49 +00004391 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004392 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004393 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004394
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004395 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004396}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004397
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004398static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
4399 unsigned long cap)
4400{
4401 struct dmar_domain *dmar_domain = domain->priv;
4402
4403 if (cap == IOMMU_CAP_CACHE_COHERENCY)
4404 return dmar_domain->iommu_snooping;
Tom Lyon323f99c2010-07-02 16:56:14 -04004405 if (cap == IOMMU_CAP_INTR_REMAP)
Suresh Siddha95a02e92012-03-30 11:47:07 -07004406 return irq_remapping_enabled;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004407
4408 return 0;
4409}
4410
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004411static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004412{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004413 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004414 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004415 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004416
Alex Williamsona5459cf2014-06-12 16:12:31 -06004417 iommu = device_to_iommu(dev, &bus, &devfn);
4418 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004419 return -ENODEV;
4420
Alex Williamsona5459cf2014-06-12 16:12:31 -06004421 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004422
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004423 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004424
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004425 if (IS_ERR(group))
4426 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004427
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004428 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004429 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004430}
4431
4432static void intel_iommu_remove_device(struct device *dev)
4433{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004434 struct intel_iommu *iommu;
4435 u8 bus, devfn;
4436
4437 iommu = device_to_iommu(dev, &bus, &devfn);
4438 if (!iommu)
4439 return;
4440
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004441 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004442
4443 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004444}
4445
Thierry Redingb22f6432014-06-27 09:03:12 +02004446static const struct iommu_ops intel_iommu_ops = {
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004447 .domain_init = intel_iommu_domain_init,
4448 .domain_destroy = intel_iommu_domain_destroy,
4449 .attach_dev = intel_iommu_attach_device,
4450 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004451 .map = intel_iommu_map,
4452 .unmap = intel_iommu_unmap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004453 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004454 .domain_has_cap = intel_iommu_domain_has_cap,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004455 .add_device = intel_iommu_add_device,
4456 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004457 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004458};
David Woodhouse9af88142009-02-13 23:18:03 +00004459
Daniel Vetter94526182013-01-20 23:50:13 +01004460static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4461{
4462 /* G4x/GM45 integrated gfx dmar support is totally busted. */
4463 printk(KERN_INFO "DMAR: Disabling IOMMU for graphics on this chipset\n");
4464 dmar_map_gfx = 0;
4465}
4466
4467DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4468DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4469DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4470DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4471DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4472DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4473DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4474
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004475static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004476{
4477 /*
4478 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004479 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004480 */
4481 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
4482 rwbf_quirk = 1;
4483}
4484
4485DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004486DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4487DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4488DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4489DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4490DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4491DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004492
Adam Jacksoneecfd572010-08-25 21:17:34 +01004493#define GGC 0x52
4494#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4495#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4496#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4497#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4498#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4499#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4500#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4501#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4502
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004503static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004504{
4505 unsigned short ggc;
4506
Adam Jacksoneecfd572010-08-25 21:17:34 +01004507 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004508 return;
4509
Adam Jacksoneecfd572010-08-25 21:17:34 +01004510 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
David Woodhouse9eecabc2010-09-21 22:28:23 +01004511 printk(KERN_INFO "DMAR: BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
4512 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004513 } else if (dmar_map_gfx) {
4514 /* we have to ensure the gfx device is idle before we flush */
4515 printk(KERN_INFO "DMAR: Disabling batched IOTLB flush on Ironlake\n");
4516 intel_iommu_strict = 1;
4517 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01004518}
4519DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
4520DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
4521DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
4522DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
4523
David Woodhousee0fc7e02009-09-30 09:12:17 -07004524/* On Tylersburg chipsets, some BIOSes have been known to enable the
4525 ISOCH DMAR unit for the Azalia sound device, but not give it any
4526 TLB entries, which causes it to deadlock. Check for that. We do
4527 this in a function called from init_dmars(), instead of in a PCI
4528 quirk, because we don't want to print the obnoxious "BIOS broken"
4529 message if VT-d is actually disabled.
4530*/
4531static void __init check_tylersburg_isoch(void)
4532{
4533 struct pci_dev *pdev;
4534 uint32_t vtisochctrl;
4535
4536 /* If there's no Azalia in the system anyway, forget it. */
4537 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
4538 if (!pdev)
4539 return;
4540 pci_dev_put(pdev);
4541
4542 /* System Management Registers. Might be hidden, in which case
4543 we can't do the sanity check. But that's OK, because the
4544 known-broken BIOSes _don't_ actually hide it, so far. */
4545 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
4546 if (!pdev)
4547 return;
4548
4549 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
4550 pci_dev_put(pdev);
4551 return;
4552 }
4553
4554 pci_dev_put(pdev);
4555
4556 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
4557 if (vtisochctrl & 1)
4558 return;
4559
4560 /* Drop all bits other than the number of TLB entries */
4561 vtisochctrl &= 0x1c;
4562
4563 /* If we have the recommended number of TLB entries (16), fine. */
4564 if (vtisochctrl == 0x10)
4565 return;
4566
4567 /* Zero TLB entries? You get to ride the short bus to school. */
4568 if (!vtisochctrl) {
4569 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
4570 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
4571 dmi_get_system_info(DMI_BIOS_VENDOR),
4572 dmi_get_system_info(DMI_BIOS_VERSION),
4573 dmi_get_system_info(DMI_PRODUCT_VERSION));
4574 iommu_identity_mapping |= IDENTMAP_AZALIA;
4575 return;
4576 }
4577
4578 printk(KERN_WARNING "DMAR: Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
4579 vtisochctrl);
4580}