blob: 0f258f0f5ac02f136017d4b484e8b3c084627793 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
mark gross5e0d2a62008-03-04 15:22:08 -080036#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030037#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010038#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010040#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070041#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100042#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020043#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080044#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070045#include <linux/dma-contiguous.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020046#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070047#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070048#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070050
Joerg Roedel078e1ee2012-09-26 12:44:43 +020051#include "irq_remapping.h"
52
Fenghua Yu5b6985c2008-10-16 18:02:32 -070053#define ROOT_SIZE VTD_PAGE_SIZE
54#define CONTEXT_SIZE VTD_PAGE_SIZE
55
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070056#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000057#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070059#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060
61#define IOAPIC_RANGE_START (0xfee00000)
62#define IOAPIC_RANGE_END (0xfeefffff)
63#define IOVA_START_ADDR (0x1000)
64
65#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
66
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070067#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080068#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070069
David Woodhouse2ebe3152009-09-19 07:34:04 -070070#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
71#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
72
73/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
74 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
75#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
76 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
77#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070078
Robin Murphy1b722502015-01-12 17:51:15 +000079/* IO virtual address start page frame number */
80#define IOVA_START_PFN (1)
81
Mark McLoughlinf27be032008-11-20 15:49:43 +000082#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070083#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070084#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080085
Andrew Mortondf08cdc2010-09-22 13:05:11 -070086/* page table handling */
87#define LEVEL_STRIDE (9)
88#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
89
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020090/*
91 * This bitmap is used to advertise the page sizes our hardware support
92 * to the IOMMU core, which will then use this information to split
93 * physically contiguous memory regions it is mapping into page sizes
94 * that we support.
95 *
96 * Traditionally the IOMMU core just handed us the mappings directly,
97 * after making sure the size is an order of a 4KiB page and that the
98 * mapping has natural alignment.
99 *
100 * To retain this behavior, we currently advertise that we support
101 * all page sizes that are an order of 4KiB.
102 *
103 * If at some point we'd like to utilize the IOMMU core's new behavior,
104 * we could change this to advertise the real page sizes we support.
105 */
106#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
107
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700108static inline int agaw_to_level(int agaw)
109{
110 return agaw + 2;
111}
112
113static inline int agaw_to_width(int agaw)
114{
Jiang Liu5c645b32014-01-06 14:18:12 +0800115 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700116}
117
118static inline int width_to_agaw(int width)
119{
Jiang Liu5c645b32014-01-06 14:18:12 +0800120 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700121}
122
123static inline unsigned int level_to_offset_bits(int level)
124{
125 return (level - 1) * LEVEL_STRIDE;
126}
127
128static inline int pfn_level_offset(unsigned long pfn, int level)
129{
130 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
131}
132
133static inline unsigned long level_mask(int level)
134{
135 return -1UL << level_to_offset_bits(level);
136}
137
138static inline unsigned long level_size(int level)
139{
140 return 1UL << level_to_offset_bits(level);
141}
142
143static inline unsigned long align_to_level(unsigned long pfn, int level)
144{
145 return (pfn + level_size(level) - 1) & level_mask(level);
146}
David Woodhousefd18de52009-05-10 23:57:41 +0100147
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100148static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
149{
Jiang Liu5c645b32014-01-06 14:18:12 +0800150 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100151}
152
David Woodhousedd4e8312009-06-27 16:21:20 +0100153/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
154 are never going to work. */
155static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
156{
157 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
158}
159
160static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
161{
162 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
163}
164static inline unsigned long page_to_dma_pfn(struct page *pg)
165{
166 return mm_to_dma_pfn(page_to_pfn(pg));
167}
168static inline unsigned long virt_to_dma_pfn(void *p)
169{
170 return page_to_dma_pfn(virt_to_page(p));
171}
172
Weidong Hand9630fe2008-12-08 11:06:32 +0800173/* global iommu list, set NULL for ignored DMAR units */
174static struct intel_iommu **g_iommus;
175
David Woodhousee0fc7e02009-09-30 09:12:17 -0700176static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000177static int rwbf_quirk;
178
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000179/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700180 * set to 1 to panic kernel if can't successfully enable VT-d
181 * (used when kernel is launched w/ TXT)
182 */
183static int force_on = 0;
184
185/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000186 * 0: Present
187 * 1-11: Reserved
188 * 12-63: Context Ptr (12 - (haw-1))
189 * 64-127: Reserved
190 */
191struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000192 u64 lo;
193 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000194};
195#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000196
Joerg Roedel091d42e2015-06-12 11:56:10 +0200197/*
198 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
199 * if marked present.
200 */
201static phys_addr_t root_entry_lctp(struct root_entry *re)
202{
203 if (!(re->lo & 1))
204 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000205
Joerg Roedel091d42e2015-06-12 11:56:10 +0200206 return re->lo & VTD_PAGE_MASK;
207}
208
209/*
210 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
211 * if marked present.
212 */
213static phys_addr_t root_entry_uctp(struct root_entry *re)
214{
215 if (!(re->hi & 1))
216 return 0;
217
218 return re->hi & VTD_PAGE_MASK;
219}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000220/*
221 * low 64 bits:
222 * 0: present
223 * 1: fault processing disable
224 * 2-3: translation type
225 * 12-63: address space root
226 * high 64 bits:
227 * 0-2: address width
228 * 3-6: aval
229 * 8-23: domain id
230 */
231struct context_entry {
232 u64 lo;
233 u64 hi;
234};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000235
Joerg Roedelcf484d02015-06-12 12:21:46 +0200236static inline void context_clear_pasid_enable(struct context_entry *context)
237{
238 context->lo &= ~(1ULL << 11);
239}
240
241static inline bool context_pasid_enabled(struct context_entry *context)
242{
243 return !!(context->lo & (1ULL << 11));
244}
245
246static inline void context_set_copied(struct context_entry *context)
247{
248 context->hi |= (1ull << 3);
249}
250
251static inline bool context_copied(struct context_entry *context)
252{
253 return !!(context->hi & (1ULL << 3));
254}
255
256static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000257{
258 return (context->lo & 1);
259}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200260
261static inline bool context_present(struct context_entry *context)
262{
263 return context_pasid_enabled(context) ?
264 __context_present(context) :
265 __context_present(context) && !context_copied(context);
266}
267
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000268static inline void context_set_present(struct context_entry *context)
269{
270 context->lo |= 1;
271}
272
273static inline void context_set_fault_enable(struct context_entry *context)
274{
275 context->lo &= (((u64)-1) << 2) | 1;
276}
277
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000278static inline void context_set_translation_type(struct context_entry *context,
279 unsigned long value)
280{
281 context->lo &= (((u64)-1) << 4) | 3;
282 context->lo |= (value & 3) << 2;
283}
284
285static inline void context_set_address_root(struct context_entry *context,
286 unsigned long value)
287{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800288 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000289 context->lo |= value & VTD_PAGE_MASK;
290}
291
292static inline void context_set_address_width(struct context_entry *context,
293 unsigned long value)
294{
295 context->hi |= value & 7;
296}
297
298static inline void context_set_domain_id(struct context_entry *context,
299 unsigned long value)
300{
301 context->hi |= (value & ((1 << 16) - 1)) << 8;
302}
303
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200304static inline int context_domain_id(struct context_entry *c)
305{
306 return((c->hi >> 8) & 0xffff);
307}
308
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000309static inline void context_clear_entry(struct context_entry *context)
310{
311 context->lo = 0;
312 context->hi = 0;
313}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000314
Mark McLoughlin622ba122008-11-20 15:49:46 +0000315/*
316 * 0: readable
317 * 1: writable
318 * 2-6: reserved
319 * 7: super page
Sheng Yang9cf066972009-03-18 15:33:07 +0800320 * 8-10: available
321 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000322 * 12-63: Host physcial address
323 */
324struct dma_pte {
325 u64 val;
326};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000327
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000328static inline void dma_clear_pte(struct dma_pte *pte)
329{
330 pte->val = 0;
331}
332
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000333static inline u64 dma_pte_addr(struct dma_pte *pte)
334{
David Woodhousec85994e2009-07-01 19:21:24 +0100335#ifdef CONFIG_64BIT
336 return pte->val & VTD_PAGE_MASK;
337#else
338 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100339 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100340#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000341}
342
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000343static inline bool dma_pte_present(struct dma_pte *pte)
344{
345 return (pte->val & 3) != 0;
346}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000347
Allen Kay4399c8b2011-10-14 12:32:46 -0700348static inline bool dma_pte_superpage(struct dma_pte *pte)
349{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200350 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700351}
352
David Woodhouse75e6bf92009-07-02 11:21:16 +0100353static inline int first_pte_in_page(struct dma_pte *pte)
354{
355 return !((unsigned long)pte & ~VTD_PAGE_MASK);
356}
357
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700358/*
359 * This domain is a statically identity mapping domain.
360 * 1. This domain creats a static 1:1 mapping to all usable memory.
361 * 2. It maps to each iommu if successful.
362 * 3. Each iommu mapps to this domain if successful.
363 */
David Woodhouse19943b02009-08-04 16:19:20 +0100364static struct dmar_domain *si_domain;
365static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700366
Joerg Roedel28ccce02015-07-21 14:45:31 +0200367/*
368 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800369 * across iommus may be owned in one domain, e.g. kvm guest.
370 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800371#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800372
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700373/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800374#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700375
Joerg Roedel29a27712015-07-21 17:17:12 +0200376#define for_each_domain_iommu(idx, domain) \
377 for (idx = 0; idx < g_num_of_iommus; idx++) \
378 if (domain->iommu_refcnt[idx])
379
Mark McLoughlin99126f72008-11-20 15:49:47 +0000380struct dmar_domain {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700381 int nid; /* node id */
Joerg Roedel29a27712015-07-21 17:17:12 +0200382
383 unsigned iommu_refcnt[DMAR_UNITS_SUPPORTED];
384 /* Refcount of devices per iommu */
385
Mark McLoughlin99126f72008-11-20 15:49:47 +0000386
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +0200387 u16 iommu_did[DMAR_UNITS_SUPPORTED];
388 /* Domain ids per IOMMU. Use u16 since
389 * domain ids are 16 bit wide according
390 * to VT-d spec, section 9.3 */
391
Joerg Roedel00a77de2015-03-26 13:43:08 +0100392 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000393 struct iova_domain iovad; /* iova's that belong to this domain */
394
395 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000396 int gaw; /* max guest address width */
397
398 /* adjusted guest address width, 0 is level 2 30-bit */
399 int agaw;
400
Weidong Han3b5410e2008-12-08 09:17:15 +0800401 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800402
403 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800404 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800405 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100406 int iommu_superpage;/* Level of superpages supported:
407 0 == 4KiB (no superpages), 1 == 2MiB,
408 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800409 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100410
411 struct iommu_domain domain; /* generic domain data structure for
412 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000413};
414
Mark McLoughlina647dac2008-11-20 15:49:48 +0000415/* PCI domain-device relationship */
416struct device_domain_info {
417 struct list_head link; /* link to domain siblings */
418 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100419 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000420 u8 devfn; /* PCI devfn number */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000421 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800422 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000423 struct dmar_domain *domain; /* pointer to domain */
424};
425
Jiang Liub94e4112014-02-19 14:07:25 +0800426struct dmar_rmrr_unit {
427 struct list_head list; /* list of rmrr units */
428 struct acpi_dmar_header *hdr; /* ACPI header */
429 u64 base_address; /* reserved base address*/
430 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000431 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800432 int devices_cnt; /* target device count */
433};
434
435struct dmar_atsr_unit {
436 struct list_head list; /* list of ATSR units */
437 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000438 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800439 int devices_cnt; /* target device count */
440 u8 include_all:1; /* include all ports */
441};
442
443static LIST_HEAD(dmar_atsr_units);
444static LIST_HEAD(dmar_rmrr_units);
445
446#define for_each_rmrr_units(rmrr) \
447 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
448
mark gross5e0d2a62008-03-04 15:22:08 -0800449static void flush_unmaps_timeout(unsigned long data);
450
Jiang Liub707cb02014-01-06 14:18:26 +0800451static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800452
mark gross80b20dd2008-04-18 13:53:58 -0700453#define HIGH_WATER_MARK 250
454struct deferred_flush_tables {
455 int next;
456 struct iova *iova[HIGH_WATER_MARK];
457 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000458 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700459};
460
461static struct deferred_flush_tables *deferred_flush;
462
mark gross5e0d2a62008-03-04 15:22:08 -0800463/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800464static int g_num_of_iommus;
465
466static DEFINE_SPINLOCK(async_umap_flush_lock);
467static LIST_HEAD(unmaps_to_do);
468
469static int timer_on;
470static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800471
Jiang Liu92d03cc2014-02-19 14:07:28 +0800472static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700473static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200474static void dmar_remove_one_dev_info(struct dmar_domain *domain,
475 struct device *dev);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200476static void domain_context_clear(struct intel_iommu *iommu,
477 struct device *dev);
Joerg Roedel55d94042015-07-22 16:50:40 +0200478static void __dmar_remove_one_dev_info(struct dmar_domain *domain,
479 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800480static int domain_detach_iommu(struct dmar_domain *domain,
481 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700482
Suresh Siddhad3f13812011-08-23 17:05:25 -0700483#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800484int dmar_disabled = 0;
485#else
486int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700487#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800488
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200489int intel_iommu_enabled = 0;
490EXPORT_SYMBOL_GPL(intel_iommu_enabled);
491
David Woodhouse2d9e6672010-06-15 10:57:57 +0100492static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700493static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800494static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100495static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100496static int intel_iommu_ecs = 1;
497
498/* We only actually use ECS when PASID support (on the new bit 40)
499 * is also advertised. Some early implementations — the ones with
500 * PASID support on bit 28 — have issues even when we *only* use
501 * extended root/context tables. */
502#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
503 ecap_pasid(iommu->ecap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700504
David Woodhousec0771df2011-10-14 20:59:46 +0100505int intel_iommu_gfx_mapped;
506EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
507
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700508#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
509static DEFINE_SPINLOCK(device_domain_lock);
510static LIST_HEAD(device_domain_list);
511
Thierry Redingb22f6432014-06-27 09:03:12 +0200512static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100513
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200514static bool translation_pre_enabled(struct intel_iommu *iommu)
515{
516 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
517}
518
Joerg Roedel091d42e2015-06-12 11:56:10 +0200519static void clear_translation_pre_enabled(struct intel_iommu *iommu)
520{
521 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
522}
523
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200524static void init_translation_status(struct intel_iommu *iommu)
525{
526 u32 gsts;
527
528 gsts = readl(iommu->reg + DMAR_GSTS_REG);
529 if (gsts & DMA_GSTS_TES)
530 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
531}
532
Joerg Roedel00a77de2015-03-26 13:43:08 +0100533/* Convert generic 'struct iommu_domain to private struct dmar_domain */
534static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
535{
536 return container_of(dom, struct dmar_domain, domain);
537}
538
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700539static int __init intel_iommu_setup(char *str)
540{
541 if (!str)
542 return -EINVAL;
543 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800544 if (!strncmp(str, "on", 2)) {
545 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200546 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800547 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700548 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200549 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700550 } else if (!strncmp(str, "igfx_off", 8)) {
551 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200552 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700553 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200554 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700555 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800556 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200557 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800558 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100559 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200560 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100561 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100562 } else if (!strncmp(str, "ecs_off", 7)) {
563 printk(KERN_INFO
564 "Intel-IOMMU: disable extended context table support\n");
565 intel_iommu_ecs = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700566 }
567
568 str += strcspn(str, ",");
569 while (*str == ',')
570 str++;
571 }
572 return 0;
573}
574__setup("intel_iommu=", intel_iommu_setup);
575
576static struct kmem_cache *iommu_domain_cache;
577static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700578
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200579static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
580{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200581 struct dmar_domain **domains;
582 int idx = did >> 8;
583
584 domains = iommu->domains[idx];
585 if (!domains)
586 return NULL;
587
588 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200589}
590
591static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
592 struct dmar_domain *domain)
593{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200594 struct dmar_domain **domains;
595 int idx = did >> 8;
596
597 if (!iommu->domains[idx]) {
598 size_t size = 256 * sizeof(struct dmar_domain *);
599 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
600 }
601
602 domains = iommu->domains[idx];
603 if (WARN_ON(!domains))
604 return;
605 else
606 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200607}
608
Suresh Siddha4c923d42009-10-02 11:01:24 -0700609static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700610{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700611 struct page *page;
612 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700613
Suresh Siddha4c923d42009-10-02 11:01:24 -0700614 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
615 if (page)
616 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700617 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700618}
619
620static inline void free_pgtable_page(void *vaddr)
621{
622 free_page((unsigned long)vaddr);
623}
624
625static inline void *alloc_domain_mem(void)
626{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900627 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700628}
629
Kay, Allen M38717942008-09-09 18:37:29 +0300630static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700631{
632 kmem_cache_free(iommu_domain_cache, vaddr);
633}
634
635static inline void * alloc_devinfo_mem(void)
636{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900637 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700638}
639
640static inline void free_devinfo_mem(void *vaddr)
641{
642 kmem_cache_free(iommu_devinfo_cache, vaddr);
643}
644
Jiang Liuab8dfe22014-07-11 14:19:27 +0800645static inline int domain_type_is_vm(struct dmar_domain *domain)
646{
647 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
648}
649
Joerg Roedel28ccce02015-07-21 14:45:31 +0200650static inline int domain_type_is_si(struct dmar_domain *domain)
651{
652 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
653}
654
Jiang Liuab8dfe22014-07-11 14:19:27 +0800655static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
656{
657 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
658 DOMAIN_FLAG_STATIC_IDENTITY);
659}
Weidong Han1b573682008-12-08 15:34:06 +0800660
Jiang Liu162d1b12014-07-11 14:19:35 +0800661static inline int domain_pfn_supported(struct dmar_domain *domain,
662 unsigned long pfn)
663{
664 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
665
666 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
667}
668
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700669static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800670{
671 unsigned long sagaw;
672 int agaw = -1;
673
674 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700675 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800676 agaw >= 0; agaw--) {
677 if (test_bit(agaw, &sagaw))
678 break;
679 }
680
681 return agaw;
682}
683
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700684/*
685 * Calculate max SAGAW for each iommu.
686 */
687int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
688{
689 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
690}
691
692/*
693 * calculate agaw for each iommu.
694 * "SAGAW" may be different across iommus, use a default agaw, and
695 * get a supported less agaw for iommus that don't support the default agaw.
696 */
697int iommu_calculate_agaw(struct intel_iommu *iommu)
698{
699 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
700}
701
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700702/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800703static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
704{
705 int iommu_id;
706
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700707 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800708 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200709 for_each_domain_iommu(iommu_id, domain)
710 break;
711
Weidong Han8c11e792008-12-08 15:29:22 +0800712 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
713 return NULL;
714
715 return g_iommus[iommu_id];
716}
717
Weidong Han8e6040972008-12-08 15:49:06 +0800718static void domain_update_iommu_coherency(struct dmar_domain *domain)
719{
David Woodhoused0501962014-03-11 17:10:29 -0700720 struct dmar_drhd_unit *drhd;
721 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100722 bool found = false;
723 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800724
David Woodhoused0501962014-03-11 17:10:29 -0700725 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800726
Joerg Roedel29a27712015-07-21 17:17:12 +0200727 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100728 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800729 if (!ecap_coherent(g_iommus[i]->ecap)) {
730 domain->iommu_coherency = 0;
731 break;
732 }
Weidong Han8e6040972008-12-08 15:49:06 +0800733 }
David Woodhoused0501962014-03-11 17:10:29 -0700734 if (found)
735 return;
736
737 /* No hardware attached; use lowest common denominator */
738 rcu_read_lock();
739 for_each_active_iommu(iommu, drhd) {
740 if (!ecap_coherent(iommu->ecap)) {
741 domain->iommu_coherency = 0;
742 break;
743 }
744 }
745 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800746}
747
Jiang Liu161f6932014-07-11 14:19:37 +0800748static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100749{
Allen Kay8140a952011-10-14 12:32:17 -0700750 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800751 struct intel_iommu *iommu;
752 int ret = 1;
753
754 rcu_read_lock();
755 for_each_active_iommu(iommu, drhd) {
756 if (iommu != skip) {
757 if (!ecap_sc_support(iommu->ecap)) {
758 ret = 0;
759 break;
760 }
761 }
762 }
763 rcu_read_unlock();
764
765 return ret;
766}
767
768static int domain_update_iommu_superpage(struct intel_iommu *skip)
769{
770 struct dmar_drhd_unit *drhd;
771 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700772 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100773
774 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800775 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100776 }
777
Allen Kay8140a952011-10-14 12:32:17 -0700778 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800779 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700780 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800781 if (iommu != skip) {
782 mask &= cap_super_page_val(iommu->cap);
783 if (!mask)
784 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100785 }
786 }
Jiang Liu0e242612014-02-19 14:07:34 +0800787 rcu_read_unlock();
788
Jiang Liu161f6932014-07-11 14:19:37 +0800789 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100790}
791
Sheng Yang58c610b2009-03-18 15:33:05 +0800792/* Some capabilities may be different across iommus */
793static void domain_update_iommu_cap(struct dmar_domain *domain)
794{
795 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800796 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
797 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800798}
799
David Woodhouse03ecc322015-02-13 14:35:21 +0000800static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
801 u8 bus, u8 devfn, int alloc)
802{
803 struct root_entry *root = &iommu->root_entry[bus];
804 struct context_entry *context;
805 u64 *entry;
806
David Woodhousec83b2f22015-06-12 10:15:49 +0100807 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000808 if (devfn >= 0x80) {
809 devfn -= 0x80;
810 entry = &root->hi;
811 }
812 devfn *= 2;
813 }
814 entry = &root->lo;
815 if (*entry & 1)
816 context = phys_to_virt(*entry & VTD_PAGE_MASK);
817 else {
818 unsigned long phy_addr;
819 if (!alloc)
820 return NULL;
821
822 context = alloc_pgtable_page(iommu->node);
823 if (!context)
824 return NULL;
825
826 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
827 phy_addr = virt_to_phys((void *)context);
828 *entry = phy_addr | 1;
829 __iommu_flush_cache(iommu, entry, sizeof(*entry));
830 }
831 return &context[devfn];
832}
833
David Woodhouse4ed6a542015-05-11 14:59:20 +0100834static int iommu_dummy(struct device *dev)
835{
836 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
837}
838
David Woodhouse156baca2014-03-09 14:00:57 -0700839static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800840{
841 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800842 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700843 struct device *tmp;
844 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800845 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800846 int i;
847
David Woodhouse4ed6a542015-05-11 14:59:20 +0100848 if (iommu_dummy(dev))
849 return NULL;
850
David Woodhouse156baca2014-03-09 14:00:57 -0700851 if (dev_is_pci(dev)) {
852 pdev = to_pci_dev(dev);
853 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100854 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700855 dev = &ACPI_COMPANION(dev)->dev;
856
Jiang Liu0e242612014-02-19 14:07:34 +0800857 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800858 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700859 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100860 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800861
Jiang Liub683b232014-02-19 14:07:32 +0800862 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700863 drhd->devices_cnt, i, tmp) {
864 if (tmp == dev) {
865 *bus = drhd->devices[i].bus;
866 *devfn = drhd->devices[i].devfn;
867 goto out;
868 }
869
870 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000871 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700872
873 ptmp = to_pci_dev(tmp);
874 if (ptmp->subordinate &&
875 ptmp->subordinate->number <= pdev->bus->number &&
876 ptmp->subordinate->busn_res.end >= pdev->bus->number)
877 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100878 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800879
David Woodhouse156baca2014-03-09 14:00:57 -0700880 if (pdev && drhd->include_all) {
881 got_pdev:
882 *bus = pdev->bus->number;
883 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800884 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700885 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800886 }
Jiang Liub683b232014-02-19 14:07:32 +0800887 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700888 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800889 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800890
Jiang Liub683b232014-02-19 14:07:32 +0800891 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800892}
893
Weidong Han5331fe62008-12-08 23:00:00 +0800894static void domain_flush_cache(struct dmar_domain *domain,
895 void *addr, int size)
896{
897 if (!domain->iommu_coherency)
898 clflush_cache_range(addr, size);
899}
900
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700901static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
902{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700903 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000904 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700905 unsigned long flags;
906
907 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000908 context = iommu_context_addr(iommu, bus, devfn, 0);
909 if (context)
910 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700911 spin_unlock_irqrestore(&iommu->lock, flags);
912 return ret;
913}
914
915static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
916{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700917 struct context_entry *context;
918 unsigned long flags;
919
920 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000921 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700922 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000923 context_clear_entry(context);
924 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700925 }
926 spin_unlock_irqrestore(&iommu->lock, flags);
927}
928
929static void free_context_table(struct intel_iommu *iommu)
930{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700931 int i;
932 unsigned long flags;
933 struct context_entry *context;
934
935 spin_lock_irqsave(&iommu->lock, flags);
936 if (!iommu->root_entry) {
937 goto out;
938 }
939 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000940 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700941 if (context)
942 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000943
David Woodhousec83b2f22015-06-12 10:15:49 +0100944 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000945 continue;
946
947 context = iommu_context_addr(iommu, i, 0x80, 0);
948 if (context)
949 free_pgtable_page(context);
950
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700951 }
952 free_pgtable_page(iommu->root_entry);
953 iommu->root_entry = NULL;
954out:
955 spin_unlock_irqrestore(&iommu->lock, flags);
956}
957
David Woodhouseb026fd22009-06-28 10:37:25 +0100958static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000959 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700960{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700961 struct dma_pte *parent, *pte = NULL;
962 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700963 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700964
965 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200966
Jiang Liu162d1b12014-07-11 14:19:35 +0800967 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200968 /* Address beyond IOMMU's addressing capabilities. */
969 return NULL;
970
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700971 parent = domain->pgd;
972
David Woodhouse5cf0a762014-03-19 16:07:49 +0000973 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700974 void *tmp_page;
975
David Woodhouseb026fd22009-06-28 10:37:25 +0100976 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700977 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000978 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100979 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000980 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700981 break;
982
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000983 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100984 uint64_t pteval;
985
Suresh Siddha4c923d42009-10-02 11:01:24 -0700986 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700987
David Woodhouse206a73c12009-07-01 19:30:28 +0100988 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700989 return NULL;
David Woodhouse206a73c12009-07-01 19:30:28 +0100990
David Woodhousec85994e2009-07-01 19:21:24 +0100991 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400992 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 +0800993 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100994 /* Someone else set it while we were thinking; use theirs. */
995 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800996 else
David Woodhousec85994e2009-07-01 19:21:24 +0100997 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700998 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000999 if (level == 1)
1000 break;
1001
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001002 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001003 level--;
1004 }
1005
David Woodhouse5cf0a762014-03-19 16:07:49 +00001006 if (!*target_level)
1007 *target_level = level;
1008
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001009 return pte;
1010}
1011
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001012
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001013/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001014static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1015 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001016 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001017{
1018 struct dma_pte *parent, *pte = NULL;
1019 int total = agaw_to_level(domain->agaw);
1020 int offset;
1021
1022 parent = domain->pgd;
1023 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001024 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001025 pte = &parent[offset];
1026 if (level == total)
1027 return pte;
1028
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001029 if (!dma_pte_present(pte)) {
1030 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001031 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001032 }
1033
Yijing Wange16922a2014-05-20 20:37:51 +08001034 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001035 *large_page = total;
1036 return pte;
1037 }
1038
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001039 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001040 total--;
1041 }
1042 return NULL;
1043}
1044
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001045/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001046static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +01001047 unsigned long start_pfn,
1048 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001049{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001050 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001051 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001052
Jiang Liu162d1b12014-07-11 14:19:35 +08001053 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1054 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001055 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001056
David Woodhouse04b18e62009-06-27 19:15:01 +01001057 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001058 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001059 large_page = 1;
1060 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001061 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001062 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001063 continue;
1064 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001065 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001066 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001067 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001068 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001069 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1070
David Woodhouse310a5ab2009-06-28 18:52:20 +01001071 domain_flush_cache(domain, first_pte,
1072 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001073
1074 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001075}
1076
Alex Williamson3269ee02013-06-15 10:27:19 -06001077static void dma_pte_free_level(struct dmar_domain *domain, int level,
1078 struct dma_pte *pte, unsigned long pfn,
1079 unsigned long start_pfn, unsigned long last_pfn)
1080{
1081 pfn = max(start_pfn, pfn);
1082 pte = &pte[pfn_level_offset(pfn, level)];
1083
1084 do {
1085 unsigned long level_pfn;
1086 struct dma_pte *level_pte;
1087
1088 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1089 goto next;
1090
1091 level_pfn = pfn & level_mask(level - 1);
1092 level_pte = phys_to_virt(dma_pte_addr(pte));
1093
1094 if (level > 2)
1095 dma_pte_free_level(domain, level - 1, level_pte,
1096 level_pfn, start_pfn, last_pfn);
1097
1098 /* If range covers entire pagetable, free it */
1099 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001100 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001101 dma_clear_pte(pte);
1102 domain_flush_cache(domain, pte, sizeof(*pte));
1103 free_pgtable_page(level_pte);
1104 }
1105next:
1106 pfn += level_size(level);
1107 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1108}
1109
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001110/* free page table pages. last level pte should already be cleared */
1111static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001112 unsigned long start_pfn,
1113 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001114{
Jiang Liu162d1b12014-07-11 14:19:35 +08001115 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1116 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001117 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001118
Jiang Liud41a4ad2014-07-11 14:19:34 +08001119 dma_pte_clear_range(domain, start_pfn, last_pfn);
1120
David Woodhousef3a0a522009-06-30 03:40:07 +01001121 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001122 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1123 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001124
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001125 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001126 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001127 free_pgtable_page(domain->pgd);
1128 domain->pgd = NULL;
1129 }
1130}
1131
David Woodhouseea8ea462014-03-05 17:09:32 +00001132/* When a page at a given level is being unlinked from its parent, we don't
1133 need to *modify* it at all. All we need to do is make a list of all the
1134 pages which can be freed just as soon as we've flushed the IOTLB and we
1135 know the hardware page-walk will no longer touch them.
1136 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1137 be freed. */
1138static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1139 int level, struct dma_pte *pte,
1140 struct page *freelist)
1141{
1142 struct page *pg;
1143
1144 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1145 pg->freelist = freelist;
1146 freelist = pg;
1147
1148 if (level == 1)
1149 return freelist;
1150
Jiang Liuadeb2592014-04-09 10:20:39 +08001151 pte = page_address(pg);
1152 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001153 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1154 freelist = dma_pte_list_pagetables(domain, level - 1,
1155 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001156 pte++;
1157 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001158
1159 return freelist;
1160}
1161
1162static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1163 struct dma_pte *pte, unsigned long pfn,
1164 unsigned long start_pfn,
1165 unsigned long last_pfn,
1166 struct page *freelist)
1167{
1168 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1169
1170 pfn = max(start_pfn, pfn);
1171 pte = &pte[pfn_level_offset(pfn, level)];
1172
1173 do {
1174 unsigned long level_pfn;
1175
1176 if (!dma_pte_present(pte))
1177 goto next;
1178
1179 level_pfn = pfn & level_mask(level);
1180
1181 /* If range covers entire pagetable, free it */
1182 if (start_pfn <= level_pfn &&
1183 last_pfn >= level_pfn + level_size(level) - 1) {
1184 /* These suborbinate page tables are going away entirely. Don't
1185 bother to clear them; we're just going to *free* them. */
1186 if (level > 1 && !dma_pte_superpage(pte))
1187 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1188
1189 dma_clear_pte(pte);
1190 if (!first_pte)
1191 first_pte = pte;
1192 last_pte = pte;
1193 } else if (level > 1) {
1194 /* Recurse down into a level that isn't *entirely* obsolete */
1195 freelist = dma_pte_clear_level(domain, level - 1,
1196 phys_to_virt(dma_pte_addr(pte)),
1197 level_pfn, start_pfn, last_pfn,
1198 freelist);
1199 }
1200next:
1201 pfn += level_size(level);
1202 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1203
1204 if (first_pte)
1205 domain_flush_cache(domain, first_pte,
1206 (void *)++last_pte - (void *)first_pte);
1207
1208 return freelist;
1209}
1210
1211/* We can't just free the pages because the IOMMU may still be walking
1212 the page tables, and may have cached the intermediate levels. The
1213 pages can only be freed after the IOTLB flush has been done. */
1214struct page *domain_unmap(struct dmar_domain *domain,
1215 unsigned long start_pfn,
1216 unsigned long last_pfn)
1217{
David Woodhouseea8ea462014-03-05 17:09:32 +00001218 struct page *freelist = NULL;
1219
Jiang Liu162d1b12014-07-11 14:19:35 +08001220 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1221 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001222 BUG_ON(start_pfn > last_pfn);
1223
1224 /* we don't need lock here; nobody else touches the iova range */
1225 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1226 domain->pgd, 0, start_pfn, last_pfn, NULL);
1227
1228 /* free pgd */
1229 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1230 struct page *pgd_page = virt_to_page(domain->pgd);
1231 pgd_page->freelist = freelist;
1232 freelist = pgd_page;
1233
1234 domain->pgd = NULL;
1235 }
1236
1237 return freelist;
1238}
1239
1240void dma_free_pagelist(struct page *freelist)
1241{
1242 struct page *pg;
1243
1244 while ((pg = freelist)) {
1245 freelist = pg->freelist;
1246 free_pgtable_page(page_address(pg));
1247 }
1248}
1249
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001250/* iommu handling */
1251static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1252{
1253 struct root_entry *root;
1254 unsigned long flags;
1255
Suresh Siddha4c923d42009-10-02 11:01:24 -07001256 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001257 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001258 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001259 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001260 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001261 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001262
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001263 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001264
1265 spin_lock_irqsave(&iommu->lock, flags);
1266 iommu->root_entry = root;
1267 spin_unlock_irqrestore(&iommu->lock, flags);
1268
1269 return 0;
1270}
1271
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001272static void iommu_set_root_entry(struct intel_iommu *iommu)
1273{
David Woodhouse03ecc322015-02-13 14:35:21 +00001274 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001275 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001276 unsigned long flag;
1277
David Woodhouse03ecc322015-02-13 14:35:21 +00001278 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001279 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001280 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001281
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001282 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001283 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001284
David Woodhousec416daa2009-05-10 20:30:58 +01001285 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001286
1287 /* Make sure hardware complete it */
1288 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001289 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001290
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001291 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292}
1293
1294static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1295{
1296 u32 val;
1297 unsigned long flag;
1298
David Woodhouse9af88142009-02-13 23:18:03 +00001299 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001300 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001301
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001302 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001303 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001304
1305 /* Make sure hardware complete it */
1306 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001307 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001308
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001309 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001310}
1311
1312/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001313static void __iommu_flush_context(struct intel_iommu *iommu,
1314 u16 did, u16 source_id, u8 function_mask,
1315 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001316{
1317 u64 val = 0;
1318 unsigned long flag;
1319
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001320 switch (type) {
1321 case DMA_CCMD_GLOBAL_INVL:
1322 val = DMA_CCMD_GLOBAL_INVL;
1323 break;
1324 case DMA_CCMD_DOMAIN_INVL:
1325 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1326 break;
1327 case DMA_CCMD_DEVICE_INVL:
1328 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1329 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1330 break;
1331 default:
1332 BUG();
1333 }
1334 val |= DMA_CCMD_ICC;
1335
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001336 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001337 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1338
1339 /* Make sure hardware complete it */
1340 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1341 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1342
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001343 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001344}
1345
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001346/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001347static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1348 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001349{
1350 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1351 u64 val = 0, val_iva = 0;
1352 unsigned long flag;
1353
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001354 switch (type) {
1355 case DMA_TLB_GLOBAL_FLUSH:
1356 /* global flush doesn't need set IVA_REG */
1357 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1358 break;
1359 case DMA_TLB_DSI_FLUSH:
1360 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1361 break;
1362 case DMA_TLB_PSI_FLUSH:
1363 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001364 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001365 val_iva = size_order | addr;
1366 break;
1367 default:
1368 BUG();
1369 }
1370 /* Note: set drain read/write */
1371#if 0
1372 /*
1373 * This is probably to be super secure.. Looks like we can
1374 * ignore it without any impact.
1375 */
1376 if (cap_read_drain(iommu->cap))
1377 val |= DMA_TLB_READ_DRAIN;
1378#endif
1379 if (cap_write_drain(iommu->cap))
1380 val |= DMA_TLB_WRITE_DRAIN;
1381
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001382 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001383 /* Note: Only uses first TLB reg currently */
1384 if (val_iva)
1385 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1386 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1387
1388 /* Make sure hardware complete it */
1389 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1390 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1391
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001392 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001393
1394 /* check IOTLB invalidation granularity */
1395 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001396 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001397 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001398 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001399 (unsigned long long)DMA_TLB_IIRG(type),
1400 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001401}
1402
David Woodhouse64ae8922014-03-09 12:52:30 -07001403static struct device_domain_info *
1404iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1405 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001406{
Quentin Lambert2f119c72015-02-06 10:59:53 +01001407 bool found = false;
Yu Zhao93a23a72009-05-18 13:51:37 +08001408 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001409 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001410
Joerg Roedel55d94042015-07-22 16:50:40 +02001411 assert_spin_locked(&device_domain_lock);
1412
Yu Zhao93a23a72009-05-18 13:51:37 +08001413 if (!ecap_dev_iotlb_support(iommu->ecap))
1414 return NULL;
1415
1416 if (!iommu->qi)
1417 return NULL;
1418
Yu Zhao93a23a72009-05-18 13:51:37 +08001419 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001420 if (info->iommu == iommu && info->bus == bus &&
1421 info->devfn == devfn) {
Quentin Lambert2f119c72015-02-06 10:59:53 +01001422 found = true;
Yu Zhao93a23a72009-05-18 13:51:37 +08001423 break;
1424 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001425
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001426 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001427 return NULL;
1428
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001429 pdev = to_pci_dev(info->dev);
1430
1431 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001432 return NULL;
1433
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001434 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001435 return NULL;
1436
Yu Zhao93a23a72009-05-18 13:51:37 +08001437 return info;
1438}
1439
1440static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1441{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001442 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001443 return;
1444
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001445 pci_enable_ats(to_pci_dev(info->dev), VTD_PAGE_SHIFT);
Yu Zhao93a23a72009-05-18 13:51:37 +08001446}
1447
1448static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1449{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001450 if (!info->dev || !dev_is_pci(info->dev) ||
1451 !pci_ats_enabled(to_pci_dev(info->dev)))
Yu Zhao93a23a72009-05-18 13:51:37 +08001452 return;
1453
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001454 pci_disable_ats(to_pci_dev(info->dev));
Yu Zhao93a23a72009-05-18 13:51:37 +08001455}
1456
1457static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1458 u64 addr, unsigned mask)
1459{
1460 u16 sid, qdep;
1461 unsigned long flags;
1462 struct device_domain_info *info;
1463
1464 spin_lock_irqsave(&device_domain_lock, flags);
1465 list_for_each_entry(info, &domain->devices, link) {
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001466 struct pci_dev *pdev;
1467 if (!info->dev || !dev_is_pci(info->dev))
1468 continue;
1469
1470 pdev = to_pci_dev(info->dev);
1471 if (!pci_ats_enabled(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001472 continue;
1473
1474 sid = info->bus << 8 | info->devfn;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001475 qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001476 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1477 }
1478 spin_unlock_irqrestore(&device_domain_lock, flags);
1479}
1480
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001481static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1482 struct dmar_domain *domain,
1483 unsigned long pfn, unsigned int pages,
1484 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001485{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001486 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001487 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001488 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001489
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001490 BUG_ON(pages == 0);
1491
David Woodhouseea8ea462014-03-05 17:09:32 +00001492 if (ih)
1493 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001494 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001495 * Fallback to domain selective flush if no PSI support or the size is
1496 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001497 * PSI requires page size to be 2 ^ x, and the base address is naturally
1498 * aligned to the size
1499 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001500 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1501 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001502 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001503 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001504 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001505 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001506
1507 /*
Nadav Amit82653632010-04-01 13:24:40 +03001508 * In caching mode, changes of pages from non-present to present require
1509 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001510 */
Nadav Amit82653632010-04-01 13:24:40 +03001511 if (!cap_caching_mode(iommu->cap) || !map)
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001512 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1513 addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001514}
1515
mark grossf8bab732008-02-08 04:18:38 -08001516static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1517{
1518 u32 pmen;
1519 unsigned long flags;
1520
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001521 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001522 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1523 pmen &= ~DMA_PMEN_EPM;
1524 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1525
1526 /* wait for the protected region status bit to clear */
1527 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1528 readl, !(pmen & DMA_PMEN_PRS), pmen);
1529
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001530 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001531}
1532
Jiang Liu2a41cce2014-07-11 14:19:33 +08001533static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001534{
1535 u32 sts;
1536 unsigned long flags;
1537
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001538 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001539 iommu->gcmd |= DMA_GCMD_TE;
1540 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001541
1542 /* Make sure hardware complete it */
1543 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001544 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001545
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001546 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001547}
1548
Jiang Liu2a41cce2014-07-11 14:19:33 +08001549static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001550{
1551 u32 sts;
1552 unsigned long flag;
1553
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001554 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001555 iommu->gcmd &= ~DMA_GCMD_TE;
1556 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1557
1558 /* Make sure hardware complete it */
1559 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001560 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001561
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001562 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001563}
1564
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001565
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001566static int iommu_init_domains(struct intel_iommu *iommu)
1567{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001568 u32 ndomains, nlongs;
1569 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001570
1571 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001572 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001573 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001574 nlongs = BITS_TO_LONGS(ndomains);
1575
Donald Dutile94a91b52009-08-20 16:51:34 -04001576 spin_lock_init(&iommu->lock);
1577
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001578 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1579 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001580 pr_err("%s: Allocating domain id array failed\n",
1581 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001582 return -ENOMEM;
1583 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001584
1585 size = ((ndomains >> 8) + 1) * sizeof(struct dmar_domain **);
1586 iommu->domains = kzalloc(size, GFP_KERNEL);
1587
1588 if (iommu->domains) {
1589 size = 256 * sizeof(struct dmar_domain *);
1590 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1591 }
1592
1593 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001594 pr_err("%s: Allocating domain array failed\n",
1595 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001596 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001597 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001598 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001599 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001600 return -ENOMEM;
1601 }
1602
Joerg Roedel8bf47812015-07-21 10:41:21 +02001603
1604
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001605 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001606 * If Caching mode is set, then invalid translations are tagged
1607 * with domain-id 0, hence we need to pre-allocate it. We also
1608 * use domain-id 0 as a marker for non-allocated domain-id, so
1609 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001610 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001611 set_bit(0, iommu->domain_ids);
1612
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001613 return 0;
1614}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001615
Jiang Liuffebeb42014-11-09 22:48:02 +08001616static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001617{
Joerg Roedel29a27712015-07-21 17:17:12 +02001618 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001619 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001620
Joerg Roedel29a27712015-07-21 17:17:12 +02001621 if (!iommu->domains || !iommu->domain_ids)
1622 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001623
Joerg Roedel55d94042015-07-22 16:50:40 +02001624 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001625 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1626 struct dmar_domain *domain;
1627
1628 if (info->iommu != iommu)
1629 continue;
1630
1631 if (!info->dev || !info->domain)
1632 continue;
1633
1634 domain = info->domain;
1635
Joerg Roedele6de0f82015-07-22 16:30:36 +02001636 dmar_remove_one_dev_info(domain, info->dev);
Joerg Roedel29a27712015-07-21 17:17:12 +02001637
1638 if (!domain_type_is_vm_or_si(domain))
1639 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001640 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001641 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001642
1643 if (iommu->gcmd & DMA_GCMD_TE)
1644 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001645}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001646
Jiang Liuffebeb42014-11-09 22:48:02 +08001647static void free_dmar_iommu(struct intel_iommu *iommu)
1648{
1649 if ((iommu->domains) && (iommu->domain_ids)) {
Joerg Roedel8bf47812015-07-21 10:41:21 +02001650 int elems = (cap_ndoms(iommu->cap) >> 8) + 1;
1651 int i;
1652
1653 for (i = 0; i < elems; i++)
1654 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001655 kfree(iommu->domains);
1656 kfree(iommu->domain_ids);
1657 iommu->domains = NULL;
1658 iommu->domain_ids = NULL;
1659 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001660
Weidong Hand9630fe2008-12-08 11:06:32 +08001661 g_iommus[iommu->seq_id] = NULL;
1662
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001663 /* free context mapping */
1664 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001665}
1666
Jiang Liuab8dfe22014-07-11 14:19:27 +08001667static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001668{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001669 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001670
1671 domain = alloc_domain_mem();
1672 if (!domain)
1673 return NULL;
1674
Jiang Liuab8dfe22014-07-11 14:19:27 +08001675 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001676 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001677 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001678 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001679
1680 return domain;
1681}
1682
Joerg Roedeld160aca2015-07-22 11:52:53 +02001683/* Must be called with iommu->lock */
1684static int domain_attach_iommu(struct dmar_domain *domain,
1685 struct intel_iommu *iommu)
Jiang Liufb170fb2014-07-11 14:19:28 +08001686{
Jiang Liufb170fb2014-07-11 14:19:28 +08001687 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001688 int num;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001689
Joerg Roedel55d94042015-07-22 16:50:40 +02001690 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001691 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001692
Joerg Roedel29a27712015-07-21 17:17:12 +02001693 domain->iommu_refcnt[iommu->seq_id] += 1;
1694 domain->iommu_count += 1;
1695 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001696 ndomains = cap_ndoms(iommu->cap);
1697 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1698
1699 if (num >= ndomains) {
1700 pr_err("%s: No free domain ids\n", iommu->name);
1701 domain->iommu_refcnt[iommu->seq_id] -= 1;
1702 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001703 return -ENOSPC;
Joerg Roedeld160aca2015-07-22 11:52:53 +02001704 }
1705
1706 set_bit(num, iommu->domain_ids);
1707 set_iommu_domain(iommu, num, domain);
1708
1709 domain->iommu_did[iommu->seq_id] = num;
1710 domain->nid = iommu->node;
1711
Jiang Liufb170fb2014-07-11 14:19:28 +08001712 domain_update_iommu_cap(domain);
1713 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001714
Joerg Roedel55d94042015-07-22 16:50:40 +02001715 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001716}
1717
1718static int domain_detach_iommu(struct dmar_domain *domain,
1719 struct intel_iommu *iommu)
1720{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001721 int num, count = INT_MAX;
Joerg Roedeld160aca2015-07-22 11:52:53 +02001722
Joerg Roedel55d94042015-07-22 16:50:40 +02001723 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001724 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001725
Joerg Roedel29a27712015-07-21 17:17:12 +02001726 domain->iommu_refcnt[iommu->seq_id] -= 1;
1727 count = --domain->iommu_count;
1728 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001729 num = domain->iommu_did[iommu->seq_id];
1730 clear_bit(num, iommu->domain_ids);
1731 set_iommu_domain(iommu, num, NULL);
1732
Jiang Liufb170fb2014-07-11 14:19:28 +08001733 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001734 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001735 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001736
1737 return count;
1738}
1739
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001740static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001741static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001742
Joseph Cihula51a63e62011-03-21 11:04:24 -07001743static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001744{
1745 struct pci_dev *pdev = NULL;
1746 struct iova *iova;
1747 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001748
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001749 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1750 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001751
Mark Gross8a443df2008-03-04 14:59:31 -08001752 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1753 &reserved_rbtree_key);
1754
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001755 /* IOAPIC ranges shouldn't be accessed by DMA */
1756 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1757 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001758 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001759 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001760 return -ENODEV;
1761 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001762
1763 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1764 for_each_pci_dev(pdev) {
1765 struct resource *r;
1766
1767 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1768 r = &pdev->resource[i];
1769 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1770 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001771 iova = reserve_iova(&reserved_iova_list,
1772 IOVA_PFN(r->start),
1773 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001774 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001775 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001776 return -ENODEV;
1777 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001778 }
1779 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001780 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001781}
1782
1783static void domain_reserve_special_ranges(struct dmar_domain *domain)
1784{
1785 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1786}
1787
1788static inline int guestwidth_to_adjustwidth(int gaw)
1789{
1790 int agaw;
1791 int r = (gaw - 12) % 9;
1792
1793 if (r == 0)
1794 agaw = gaw;
1795 else
1796 agaw = gaw + 9 - r;
1797 if (agaw > 64)
1798 agaw = 64;
1799 return agaw;
1800}
1801
Joerg Roedeldc534b22015-07-22 12:44:02 +02001802static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1803 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001804{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001805 int adjust_width, agaw;
1806 unsigned long sagaw;
1807
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001808 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1809 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001810 domain_reserve_special_ranges(domain);
1811
1812 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001813 if (guest_width > cap_mgaw(iommu->cap))
1814 guest_width = cap_mgaw(iommu->cap);
1815 domain->gaw = guest_width;
1816 adjust_width = guestwidth_to_adjustwidth(guest_width);
1817 agaw = width_to_agaw(adjust_width);
1818 sagaw = cap_sagaw(iommu->cap);
1819 if (!test_bit(agaw, &sagaw)) {
1820 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001821 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001822 agaw = find_next_bit(&sagaw, 5, agaw);
1823 if (agaw >= 5)
1824 return -ENODEV;
1825 }
1826 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001827
Weidong Han8e6040972008-12-08 15:49:06 +08001828 if (ecap_coherent(iommu->ecap))
1829 domain->iommu_coherency = 1;
1830 else
1831 domain->iommu_coherency = 0;
1832
Sheng Yang58c610b2009-03-18 15:33:05 +08001833 if (ecap_sc_support(iommu->ecap))
1834 domain->iommu_snooping = 1;
1835 else
1836 domain->iommu_snooping = 0;
1837
David Woodhouse214e39a2014-03-19 10:38:49 +00001838 if (intel_iommu_superpage)
1839 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1840 else
1841 domain->iommu_superpage = 0;
1842
Suresh Siddha4c923d42009-10-02 11:01:24 -07001843 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001844
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001845 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001846 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001847 if (!domain->pgd)
1848 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001849 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001850 return 0;
1851}
1852
1853static void domain_exit(struct dmar_domain *domain)
1854{
David Woodhouseea8ea462014-03-05 17:09:32 +00001855 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001856
1857 /* Domain 0 is reserved, so dont process it */
1858 if (!domain)
1859 return;
1860
Alex Williamson7b668352011-05-24 12:02:41 +01001861 /* Flush any lazy unmaps that may reference this domain */
1862 if (!intel_iommu_strict)
1863 flush_unmaps_timeout(0);
1864
Joerg Roedeld160aca2015-07-22 11:52:53 +02001865 /* Remove associated devices and clear attached or cached domains */
1866 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001867 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001868 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001869
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001870 /* destroy iovas */
1871 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001872
David Woodhouseea8ea462014-03-05 17:09:32 +00001873 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001874
David Woodhouseea8ea462014-03-05 17:09:32 +00001875 dma_free_pagelist(freelist);
1876
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001877 free_domain_mem(domain);
1878}
1879
David Woodhouse64ae8922014-03-09 12:52:30 -07001880static int domain_context_mapping_one(struct dmar_domain *domain,
1881 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001882 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001883{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001884 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001885 int translation = CONTEXT_TT_MULTI_LEVEL;
1886 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001887 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001888 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001889 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02001890 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001891
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001892 WARN_ON(did == 0);
1893
Joerg Roedel28ccce02015-07-21 14:45:31 +02001894 if (hw_pass_through && domain_type_is_si(domain))
1895 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001896
1897 pr_debug("Set context mapping for %02x:%02x.%d\n",
1898 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001899
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001900 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001901
Joerg Roedel55d94042015-07-22 16:50:40 +02001902 spin_lock_irqsave(&device_domain_lock, flags);
1903 spin_lock(&iommu->lock);
1904
1905 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00001906 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001907 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02001908 goto out_unlock;
1909
1910 ret = 0;
1911 if (context_present(context))
1912 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001913
Weidong Hanea6606b2008-12-08 23:08:15 +08001914 pgd = domain->pgd;
1915
Joerg Roedelde24e552015-07-21 14:53:04 +02001916 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001917 context_set_domain_id(context, did);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001918
Joerg Roedelde24e552015-07-21 14:53:04 +02001919 /*
1920 * Skip top levels of page tables for iommu which has less agaw
1921 * than default. Unnecessary for PT mode.
1922 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001923 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02001924 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02001925 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02001926 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02001927 if (!dma_pte_present(pgd))
1928 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02001929 }
1930
David Woodhouse64ae8922014-03-09 12:52:30 -07001931 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001932 translation = info ? CONTEXT_TT_DEV_IOTLB :
1933 CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02001934
Yu Zhao93a23a72009-05-18 13:51:37 +08001935 context_set_address_root(context, virt_to_phys(pgd));
1936 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02001937 } else {
1938 /*
1939 * In pass through mode, AW must be programmed to
1940 * indicate the largest AGAW value supported by
1941 * hardware. And ASR is ignored by hardware.
1942 */
1943 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001944 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001945
1946 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001947 context_set_fault_enable(context);
1948 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001949 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001950
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001951 /*
1952 * It's a non-present to present mapping. If hardware doesn't cache
1953 * non-present entry we only need to flush the write-buffer. If the
1954 * _does_ cache non-present entries, then it does so in the special
1955 * domain #0, which we have to flush:
1956 */
1957 if (cap_caching_mode(iommu->cap)) {
1958 iommu->flush.flush_context(iommu, 0,
1959 (((u16)bus) << 8) | devfn,
1960 DMA_CCMD_MASK_NOBIT,
1961 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001962 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001963 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001964 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001965 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001966 iommu_enable_dev_iotlb(info);
Joerg Roedel55d94042015-07-22 16:50:40 +02001967
1968 ret = 0;
1969
1970out_unlock:
1971 spin_unlock(&iommu->lock);
1972 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001973
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001974 return 0;
1975}
1976
Alex Williamson579305f2014-07-03 09:51:43 -06001977struct domain_context_mapping_data {
1978 struct dmar_domain *domain;
1979 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06001980};
1981
1982static int domain_context_mapping_cb(struct pci_dev *pdev,
1983 u16 alias, void *opaque)
1984{
1985 struct domain_context_mapping_data *data = opaque;
1986
1987 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001988 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06001989}
1990
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001991static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02001992domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001993{
David Woodhouse64ae8922014-03-09 12:52:30 -07001994 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07001995 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06001996 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001997
David Woodhousee1f167f2014-03-09 15:24:46 -07001998 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07001999 if (!iommu)
2000 return -ENODEV;
2001
Alex Williamson579305f2014-07-03 09:51:43 -06002002 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002003 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002004
2005 data.domain = domain;
2006 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002007
2008 return pci_for_each_dma_alias(to_pci_dev(dev),
2009 &domain_context_mapping_cb, &data);
2010}
2011
2012static int domain_context_mapped_cb(struct pci_dev *pdev,
2013 u16 alias, void *opaque)
2014{
2015 struct intel_iommu *iommu = opaque;
2016
2017 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002018}
2019
David Woodhousee1f167f2014-03-09 15:24:46 -07002020static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002021{
Weidong Han5331fe62008-12-08 23:00:00 +08002022 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002023 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002024
David Woodhousee1f167f2014-03-09 15:24:46 -07002025 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002026 if (!iommu)
2027 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002028
Alex Williamson579305f2014-07-03 09:51:43 -06002029 if (!dev_is_pci(dev))
2030 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002031
Alex Williamson579305f2014-07-03 09:51:43 -06002032 return !pci_for_each_dma_alias(to_pci_dev(dev),
2033 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002034}
2035
Fenghua Yuf5329592009-08-04 15:09:37 -07002036/* Returns a number of VTD pages, but aligned to MM page size */
2037static inline unsigned long aligned_nrpages(unsigned long host_addr,
2038 size_t size)
2039{
2040 host_addr &= ~PAGE_MASK;
2041 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2042}
2043
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002044/* Return largest possible superpage level for a given mapping */
2045static inline int hardware_largepage_caps(struct dmar_domain *domain,
2046 unsigned long iov_pfn,
2047 unsigned long phy_pfn,
2048 unsigned long pages)
2049{
2050 int support, level = 1;
2051 unsigned long pfnmerge;
2052
2053 support = domain->iommu_superpage;
2054
2055 /* To use a large page, the virtual *and* physical addresses
2056 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2057 of them will mean we have to use smaller pages. So just
2058 merge them and check both at once. */
2059 pfnmerge = iov_pfn | phy_pfn;
2060
2061 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2062 pages >>= VTD_STRIDE_SHIFT;
2063 if (!pages)
2064 break;
2065 pfnmerge >>= VTD_STRIDE_SHIFT;
2066 level++;
2067 support--;
2068 }
2069 return level;
2070}
2071
David Woodhouse9051aa02009-06-29 12:30:54 +01002072static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2073 struct scatterlist *sg, unsigned long phys_pfn,
2074 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002075{
2076 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002077 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002078 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002079 unsigned int largepage_lvl = 0;
2080 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002081
Jiang Liu162d1b12014-07-11 14:19:35 +08002082 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002083
2084 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2085 return -EINVAL;
2086
2087 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2088
Jiang Liucc4f14a2014-11-26 09:42:10 +08002089 if (!sg) {
2090 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002091 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2092 }
2093
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002094 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002095 uint64_t tmp;
2096
David Woodhousee1605492009-06-29 11:17:38 +01002097 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002098 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002099 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2100 sg->dma_length = sg->length;
2101 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002102 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002103 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002104
David Woodhousee1605492009-06-29 11:17:38 +01002105 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002106 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2107
David Woodhouse5cf0a762014-03-19 16:07:49 +00002108 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002109 if (!pte)
2110 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002111 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002112 if (largepage_lvl > 1) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002113 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002114 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2115 /*
2116 * Ensure that old small page tables are
2117 * removed to make room for superpage,
2118 * if they exist.
2119 */
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002120 dma_pte_free_pagetable(domain, iov_pfn,
Jiang Liud41a4ad2014-07-11 14:19:34 +08002121 iov_pfn + lvl_pages - 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002122 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002123 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002124 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002125
David Woodhousee1605492009-06-29 11:17:38 +01002126 }
2127 /* We don't need lock here, nobody else
2128 * touches the iova range
2129 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002130 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002131 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002132 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002133 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2134 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002135 if (dumps) {
2136 dumps--;
2137 debug_dma_dump_mappings(NULL);
2138 }
2139 WARN_ON(1);
2140 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002141
2142 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2143
2144 BUG_ON(nr_pages < lvl_pages);
2145 BUG_ON(sg_res < lvl_pages);
2146
2147 nr_pages -= lvl_pages;
2148 iov_pfn += lvl_pages;
2149 phys_pfn += lvl_pages;
2150 pteval += lvl_pages * VTD_PAGE_SIZE;
2151 sg_res -= lvl_pages;
2152
2153 /* If the next PTE would be the first in a new page, then we
2154 need to flush the cache on the entries we've just written.
2155 And then we'll need to recalculate 'pte', so clear it and
2156 let it get set again in the if (!pte) block above.
2157
2158 If we're done (!nr_pages) we need to flush the cache too.
2159
2160 Also if we've been setting superpages, we may need to
2161 recalculate 'pte' and switch back to smaller pages for the
2162 end of the mapping, if the trailing size is not enough to
2163 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002164 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002165 if (!nr_pages || first_pte_in_page(pte) ||
2166 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002167 domain_flush_cache(domain, first_pte,
2168 (void *)pte - (void *)first_pte);
2169 pte = NULL;
2170 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002171
2172 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002173 sg = sg_next(sg);
2174 }
2175 return 0;
2176}
2177
David Woodhouse9051aa02009-06-29 12:30:54 +01002178static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2179 struct scatterlist *sg, unsigned long nr_pages,
2180 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002181{
David Woodhouse9051aa02009-06-29 12:30:54 +01002182 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2183}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002184
David Woodhouse9051aa02009-06-29 12:30:54 +01002185static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2186 unsigned long phys_pfn, unsigned long nr_pages,
2187 int prot)
2188{
2189 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002190}
2191
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002192static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002193{
Weidong Hanc7151a82008-12-08 22:51:37 +08002194 if (!iommu)
2195 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002196
2197 clear_context_table(iommu, bus, devfn);
2198 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002199 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002200 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002201}
2202
David Woodhouse109b9b02012-05-25 17:43:02 +01002203static inline void unlink_domain_info(struct device_domain_info *info)
2204{
2205 assert_spin_locked(&device_domain_lock);
2206 list_del(&info->link);
2207 list_del(&info->global);
2208 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002209 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002210}
2211
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002212static void domain_remove_dev_info(struct dmar_domain *domain)
2213{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002214 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02002215 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002216
Joerg Roedel55d94042015-07-22 16:50:40 +02002217 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002218 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel55d94042015-07-22 16:50:40 +02002219 __dmar_remove_one_dev_info(domain, info->dev);
2220 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002221}
2222
2223/*
2224 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002225 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002226 */
David Woodhouse1525a292014-03-06 16:19:30 +00002227static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002228{
2229 struct device_domain_info *info;
2230
2231 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002232 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002233 if (info)
2234 return info->domain;
2235 return NULL;
2236}
2237
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002238static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002239dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2240{
2241 struct device_domain_info *info;
2242
2243 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002244 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002245 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002246 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002247
2248 return NULL;
2249}
2250
Joerg Roedel5db31562015-07-22 12:40:43 +02002251static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2252 int bus, int devfn,
2253 struct device *dev,
2254 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002255{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002256 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002257 struct device_domain_info *info;
2258 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002259 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002260
2261 info = alloc_devinfo_mem();
2262 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002263 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002264
Jiang Liu745f2582014-02-19 14:07:26 +08002265 info->bus = bus;
2266 info->devfn = devfn;
2267 info->dev = dev;
2268 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002269 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002270
2271 spin_lock_irqsave(&device_domain_lock, flags);
2272 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002273 found = find_domain(dev);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002274 else {
2275 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002276 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002277 if (info2)
2278 found = info2->domain;
2279 }
Jiang Liu745f2582014-02-19 14:07:26 +08002280 if (found) {
2281 spin_unlock_irqrestore(&device_domain_lock, flags);
2282 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002283 /* Caller must free the original domain */
2284 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002285 }
2286
Joerg Roedeld160aca2015-07-22 11:52:53 +02002287 spin_lock(&iommu->lock);
2288 ret = domain_attach_iommu(domain, iommu);
2289 spin_unlock(&iommu->lock);
2290
2291 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002292 spin_unlock_irqrestore(&device_domain_lock, flags);
2293 return NULL;
2294 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002295
David Woodhouseb718cd32014-03-09 13:11:33 -07002296 list_add(&info->link, &domain->devices);
2297 list_add(&info->global, &device_domain_list);
2298 if (dev)
2299 dev->archdata.iommu = info;
2300 spin_unlock_irqrestore(&device_domain_lock, flags);
2301
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002302 if (dev && domain_context_mapping(domain, dev)) {
2303 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002304 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002305 return NULL;
2306 }
2307
David Woodhouseb718cd32014-03-09 13:11:33 -07002308 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002309}
2310
Alex Williamson579305f2014-07-03 09:51:43 -06002311static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2312{
2313 *(u16 *)opaque = alias;
2314 return 0;
2315}
2316
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002317/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002318static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002319{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002320 struct device_domain_info *info = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002321 struct dmar_domain *domain, *tmp;
2322 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002323 unsigned long flags;
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002324 u16 dma_alias;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002325 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002326
David Woodhouse146922e2014-03-09 15:44:17 -07002327 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002328 if (domain)
2329 return domain;
2330
David Woodhouse146922e2014-03-09 15:44:17 -07002331 iommu = device_to_iommu(dev, &bus, &devfn);
2332 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002333 return NULL;
2334
2335 if (dev_is_pci(dev)) {
2336 struct pci_dev *pdev = to_pci_dev(dev);
2337
2338 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2339
2340 spin_lock_irqsave(&device_domain_lock, flags);
2341 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2342 PCI_BUS_NUM(dma_alias),
2343 dma_alias & 0xff);
2344 if (info) {
2345 iommu = info->iommu;
2346 domain = info->domain;
2347 }
2348 spin_unlock_irqrestore(&device_domain_lock, flags);
2349
2350 /* DMA alias already has a domain, uses it */
2351 if (info)
2352 goto found_domain;
2353 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002354
David Woodhouse146922e2014-03-09 15:44:17 -07002355 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002356 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002357 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002358 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002359 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002360 domain_exit(domain);
2361 return NULL;
2362 }
2363
2364 /* register PCI DMA alias device */
2365 if (dev_is_pci(dev)) {
Joerg Roedel5db31562015-07-22 12:40:43 +02002366 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2367 dma_alias & 0xff, NULL, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002368
2369 if (!tmp || tmp != domain) {
2370 domain_exit(domain);
2371 domain = tmp;
2372 }
2373
David Woodhouseb718cd32014-03-09 13:11:33 -07002374 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002375 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002376 }
2377
2378found_domain:
Joerg Roedel5db31562015-07-22 12:40:43 +02002379 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002380
2381 if (!tmp || tmp != domain) {
2382 domain_exit(domain);
2383 domain = tmp;
2384 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002385
2386 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002387}
2388
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002389static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002390#define IDENTMAP_ALL 1
2391#define IDENTMAP_GFX 2
2392#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002393
David Woodhouseb2132032009-06-26 18:50:28 +01002394static int iommu_domain_identity_map(struct dmar_domain *domain,
2395 unsigned long long start,
2396 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002397{
David Woodhousec5395d52009-06-28 16:35:56 +01002398 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2399 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002400
David Woodhousec5395d52009-06-28 16:35:56 +01002401 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2402 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002403 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002404 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002405 }
2406
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002407 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002408 /*
2409 * RMRR range might have overlap with physical memory range,
2410 * clear it first
2411 */
David Woodhousec5395d52009-06-28 16:35:56 +01002412 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002413
David Woodhousec5395d52009-06-28 16:35:56 +01002414 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2415 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002416 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002417}
2418
David Woodhouse0b9d9752014-03-09 15:48:15 -07002419static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002420 unsigned long long start,
2421 unsigned long long end)
2422{
2423 struct dmar_domain *domain;
2424 int ret;
2425
David Woodhouse0b9d9752014-03-09 15:48:15 -07002426 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002427 if (!domain)
2428 return -ENOMEM;
2429
David Woodhouse19943b02009-08-04 16:19:20 +01002430 /* For _hardware_ passthrough, don't bother. But for software
2431 passthrough, we do it anyway -- it may indicate a memory
2432 range which is reserved in E820, so which didn't get set
2433 up to start with in si_domain */
2434 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002435 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2436 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002437 return 0;
2438 }
2439
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002440 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2441 dev_name(dev), start, end);
2442
David Woodhouse5595b522009-12-02 09:21:55 +00002443 if (end < start) {
2444 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2445 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2446 dmi_get_system_info(DMI_BIOS_VENDOR),
2447 dmi_get_system_info(DMI_BIOS_VERSION),
2448 dmi_get_system_info(DMI_PRODUCT_VERSION));
2449 ret = -EIO;
2450 goto error;
2451 }
2452
David Woodhouse2ff729f2009-08-26 14:25:41 +01002453 if (end >> agaw_to_width(domain->agaw)) {
2454 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2455 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2456 agaw_to_width(domain->agaw),
2457 dmi_get_system_info(DMI_BIOS_VENDOR),
2458 dmi_get_system_info(DMI_BIOS_VERSION),
2459 dmi_get_system_info(DMI_PRODUCT_VERSION));
2460 ret = -EIO;
2461 goto error;
2462 }
David Woodhouse19943b02009-08-04 16:19:20 +01002463
David Woodhouseb2132032009-06-26 18:50:28 +01002464 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002465 if (ret)
2466 goto error;
2467
David Woodhouseb2132032009-06-26 18:50:28 +01002468 return 0;
2469
2470 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002471 domain_exit(domain);
2472 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002473}
2474
2475static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002476 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002477{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002478 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002479 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002480 return iommu_prepare_identity_map(dev, rmrr->base_address,
2481 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002482}
2483
Suresh Siddhad3f13812011-08-23 17:05:25 -07002484#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002485static inline void iommu_prepare_isa(void)
2486{
2487 struct pci_dev *pdev;
2488 int ret;
2489
2490 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2491 if (!pdev)
2492 return;
2493
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002494 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002495 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002496
2497 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002498 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002499
Yijing Wang9b27e822014-05-20 20:37:52 +08002500 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002501}
2502#else
2503static inline void iommu_prepare_isa(void)
2504{
2505 return;
2506}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002507#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002508
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002509static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002510
Matt Kraai071e1372009-08-23 22:30:22 -07002511static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002512{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002513 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002514
Jiang Liuab8dfe22014-07-11 14:19:27 +08002515 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002516 if (!si_domain)
2517 return -EFAULT;
2518
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002519 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2520 domain_exit(si_domain);
2521 return -EFAULT;
2522 }
2523
Joerg Roedel0dc79712015-07-21 15:40:06 +02002524 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002525
David Woodhouse19943b02009-08-04 16:19:20 +01002526 if (hw)
2527 return 0;
2528
David Woodhousec7ab48d2009-06-26 19:10:36 +01002529 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002530 unsigned long start_pfn, end_pfn;
2531 int i;
2532
2533 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2534 ret = iommu_domain_identity_map(si_domain,
2535 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2536 if (ret)
2537 return ret;
2538 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002539 }
2540
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002541 return 0;
2542}
2543
David Woodhouse9b226622014-03-09 14:03:28 -07002544static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002545{
2546 struct device_domain_info *info;
2547
2548 if (likely(!iommu_identity_mapping))
2549 return 0;
2550
David Woodhouse9b226622014-03-09 14:03:28 -07002551 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002552 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2553 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002554
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002555 return 0;
2556}
2557
Joerg Roedel28ccce02015-07-21 14:45:31 +02002558static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002559{
David Woodhouse0ac72662014-03-09 13:19:22 -07002560 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002561 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002562 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002563
David Woodhouse5913c9b2014-03-09 16:27:31 -07002564 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002565 if (!iommu)
2566 return -ENODEV;
2567
Joerg Roedel5db31562015-07-22 12:40:43 +02002568 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002569 if (ndomain != domain)
2570 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002571
2572 return 0;
2573}
2574
David Woodhouse0b9d9752014-03-09 15:48:15 -07002575static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002576{
2577 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002578 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002579 int i;
2580
Jiang Liu0e242612014-02-19 14:07:34 +08002581 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002582 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002583 /*
2584 * Return TRUE if this RMRR contains the device that
2585 * is passed in.
2586 */
2587 for_each_active_dev_scope(rmrr->devices,
2588 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002589 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002590 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002591 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002592 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002593 }
Jiang Liu0e242612014-02-19 14:07:34 +08002594 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002595 return false;
2596}
2597
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002598/*
2599 * There are a couple cases where we need to restrict the functionality of
2600 * devices associated with RMRRs. The first is when evaluating a device for
2601 * identity mapping because problems exist when devices are moved in and out
2602 * of domains and their respective RMRR information is lost. This means that
2603 * a device with associated RMRRs will never be in a "passthrough" domain.
2604 * The second is use of the device through the IOMMU API. This interface
2605 * expects to have full control of the IOVA space for the device. We cannot
2606 * satisfy both the requirement that RMRR access is maintained and have an
2607 * unencumbered IOVA space. We also have no ability to quiesce the device's
2608 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2609 * We therefore prevent devices associated with an RMRR from participating in
2610 * the IOMMU API, which eliminates them from device assignment.
2611 *
2612 * In both cases we assume that PCI USB devices with RMRRs have them largely
2613 * for historical reasons and that the RMRR space is not actively used post
2614 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002615 *
2616 * The same exception is made for graphics devices, with the requirement that
2617 * any use of the RMRR regions will be torn down before assigning the device
2618 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002619 */
2620static bool device_is_rmrr_locked(struct device *dev)
2621{
2622 if (!device_has_rmrr(dev))
2623 return false;
2624
2625 if (dev_is_pci(dev)) {
2626 struct pci_dev *pdev = to_pci_dev(dev);
2627
David Woodhouse18436af2015-03-25 15:05:47 +00002628 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002629 return false;
2630 }
2631
2632 return true;
2633}
2634
David Woodhouse3bdb2592014-03-09 16:03:08 -07002635static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002636{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002637
David Woodhouse3bdb2592014-03-09 16:03:08 -07002638 if (dev_is_pci(dev)) {
2639 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002640
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002641 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002642 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002643
David Woodhouse3bdb2592014-03-09 16:03:08 -07002644 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2645 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002646
David Woodhouse3bdb2592014-03-09 16:03:08 -07002647 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2648 return 1;
2649
2650 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2651 return 0;
2652
2653 /*
2654 * We want to start off with all devices in the 1:1 domain, and
2655 * take them out later if we find they can't access all of memory.
2656 *
2657 * However, we can't do this for PCI devices behind bridges,
2658 * because all PCI devices behind the same bridge will end up
2659 * with the same source-id on their transactions.
2660 *
2661 * Practically speaking, we can't change things around for these
2662 * devices at run-time, because we can't be sure there'll be no
2663 * DMA transactions in flight for any of their siblings.
2664 *
2665 * So PCI devices (unless they're on the root bus) as well as
2666 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2667 * the 1:1 domain, just in _case_ one of their siblings turns out
2668 * not to be able to map all of memory.
2669 */
2670 if (!pci_is_pcie(pdev)) {
2671 if (!pci_is_root_bus(pdev->bus))
2672 return 0;
2673 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2674 return 0;
2675 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2676 return 0;
2677 } else {
2678 if (device_has_rmrr(dev))
2679 return 0;
2680 }
David Woodhouse6941af22009-07-04 18:24:27 +01002681
David Woodhouse3dfc8132009-07-04 19:11:08 +01002682 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002683 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002684 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002685 * take them out of the 1:1 domain later.
2686 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002687 if (!startup) {
2688 /*
2689 * If the device's dma_mask is less than the system's memory
2690 * size then this is not a candidate for identity mapping.
2691 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002692 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002693
David Woodhouse3bdb2592014-03-09 16:03:08 -07002694 if (dev->coherent_dma_mask &&
2695 dev->coherent_dma_mask < dma_mask)
2696 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002697
David Woodhouse3bdb2592014-03-09 16:03:08 -07002698 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002699 }
David Woodhouse6941af22009-07-04 18:24:27 +01002700
2701 return 1;
2702}
2703
David Woodhousecf04eee2014-03-21 16:49:04 +00002704static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2705{
2706 int ret;
2707
2708 if (!iommu_should_identity_map(dev, 1))
2709 return 0;
2710
Joerg Roedel28ccce02015-07-21 14:45:31 +02002711 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002712 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002713 pr_info("%s identity mapping for device %s\n",
2714 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002715 else if (ret == -ENODEV)
2716 /* device not associated with an iommu */
2717 ret = 0;
2718
2719 return ret;
2720}
2721
2722
Matt Kraai071e1372009-08-23 22:30:22 -07002723static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002724{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002725 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002726 struct dmar_drhd_unit *drhd;
2727 struct intel_iommu *iommu;
2728 struct device *dev;
2729 int i;
2730 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002731
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002732 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002733 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2734 if (ret)
2735 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002736 }
2737
David Woodhousecf04eee2014-03-21 16:49:04 +00002738 for_each_active_iommu(iommu, drhd)
2739 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2740 struct acpi_device_physical_node *pn;
2741 struct acpi_device *adev;
2742
2743 if (dev->bus != &acpi_bus_type)
2744 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002745
David Woodhousecf04eee2014-03-21 16:49:04 +00002746 adev= to_acpi_device(dev);
2747 mutex_lock(&adev->physical_node_lock);
2748 list_for_each_entry(pn, &adev->physical_node_list, node) {
2749 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2750 if (ret)
2751 break;
2752 }
2753 mutex_unlock(&adev->physical_node_lock);
2754 if (ret)
2755 return ret;
2756 }
2757
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002758 return 0;
2759}
2760
Jiang Liuffebeb42014-11-09 22:48:02 +08002761static void intel_iommu_init_qi(struct intel_iommu *iommu)
2762{
2763 /*
2764 * Start from the sane iommu hardware state.
2765 * If the queued invalidation is already initialized by us
2766 * (for example, while enabling interrupt-remapping) then
2767 * we got the things already rolling from a sane state.
2768 */
2769 if (!iommu->qi) {
2770 /*
2771 * Clear any previous faults.
2772 */
2773 dmar_fault(-1, iommu);
2774 /*
2775 * Disable queued invalidation if supported and already enabled
2776 * before OS handover.
2777 */
2778 dmar_disable_qi(iommu);
2779 }
2780
2781 if (dmar_enable_qi(iommu)) {
2782 /*
2783 * Queued Invalidate not enabled, use Register Based Invalidate
2784 */
2785 iommu->flush.flush_context = __iommu_flush_context;
2786 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002787 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002788 iommu->name);
2789 } else {
2790 iommu->flush.flush_context = qi_flush_context;
2791 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002792 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002793 }
2794}
2795
Joerg Roedel091d42e2015-06-12 11:56:10 +02002796static int copy_context_table(struct intel_iommu *iommu,
2797 struct root_entry *old_re,
2798 struct context_entry **tbl,
2799 int bus, bool ext)
2800{
2801 struct context_entry *old_ce = NULL, *new_ce = NULL, ce;
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002802 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002803 phys_addr_t old_ce_phys;
2804
2805 tbl_idx = ext ? bus * 2 : bus;
2806
2807 for (devfn = 0; devfn < 256; devfn++) {
2808 /* First calculate the correct index */
2809 idx = (ext ? devfn * 2 : devfn) % 256;
2810
2811 if (idx == 0) {
2812 /* First save what we may have and clean up */
2813 if (new_ce) {
2814 tbl[tbl_idx] = new_ce;
2815 __iommu_flush_cache(iommu, new_ce,
2816 VTD_PAGE_SIZE);
2817 pos = 1;
2818 }
2819
2820 if (old_ce)
2821 iounmap(old_ce);
2822
2823 ret = 0;
2824 if (devfn < 0x80)
2825 old_ce_phys = root_entry_lctp(old_re);
2826 else
2827 old_ce_phys = root_entry_uctp(old_re);
2828
2829 if (!old_ce_phys) {
2830 if (ext && devfn == 0) {
2831 /* No LCTP, try UCTP */
2832 devfn = 0x7f;
2833 continue;
2834 } else {
2835 goto out;
2836 }
2837 }
2838
2839 ret = -ENOMEM;
2840 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2841 if (!old_ce)
2842 goto out;
2843
2844 new_ce = alloc_pgtable_page(iommu->node);
2845 if (!new_ce)
2846 goto out_unmap;
2847
2848 ret = 0;
2849 }
2850
2851 /* Now copy the context entry */
2852 ce = old_ce[idx];
2853
Joerg Roedelcf484d02015-06-12 12:21:46 +02002854 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002855 continue;
2856
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002857 did = context_domain_id(&ce);
2858 if (did >= 0 && did < cap_ndoms(iommu->cap))
2859 set_bit(did, iommu->domain_ids);
2860
Joerg Roedelcf484d02015-06-12 12:21:46 +02002861 /*
2862 * We need a marker for copied context entries. This
2863 * marker needs to work for the old format as well as
2864 * for extended context entries.
2865 *
2866 * Bit 67 of the context entry is used. In the old
2867 * format this bit is available to software, in the
2868 * extended format it is the PGE bit, but PGE is ignored
2869 * by HW if PASIDs are disabled (and thus still
2870 * available).
2871 *
2872 * So disable PASIDs first and then mark the entry
2873 * copied. This means that we don't copy PASID
2874 * translations from the old kernel, but this is fine as
2875 * faults there are not fatal.
2876 */
2877 context_clear_pasid_enable(&ce);
2878 context_set_copied(&ce);
2879
Joerg Roedel091d42e2015-06-12 11:56:10 +02002880 new_ce[idx] = ce;
2881 }
2882
2883 tbl[tbl_idx + pos] = new_ce;
2884
2885 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2886
2887out_unmap:
2888 iounmap(old_ce);
2889
2890out:
2891 return ret;
2892}
2893
2894static int copy_translation_tables(struct intel_iommu *iommu)
2895{
2896 struct context_entry **ctxt_tbls;
2897 struct root_entry *old_rt;
2898 phys_addr_t old_rt_phys;
2899 int ctxt_table_entries;
2900 unsigned long flags;
2901 u64 rtaddr_reg;
2902 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002903 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002904
2905 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2906 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002907 new_ext = !!ecap_ecs(iommu->ecap);
2908
2909 /*
2910 * The RTT bit can only be changed when translation is disabled,
2911 * but disabling translation means to open a window for data
2912 * corruption. So bail out and don't copy anything if we would
2913 * have to change the bit.
2914 */
2915 if (new_ext != ext)
2916 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002917
2918 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2919 if (!old_rt_phys)
2920 return -EINVAL;
2921
2922 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2923 if (!old_rt)
2924 return -ENOMEM;
2925
2926 /* This is too big for the stack - allocate it from slab */
2927 ctxt_table_entries = ext ? 512 : 256;
2928 ret = -ENOMEM;
2929 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
2930 if (!ctxt_tbls)
2931 goto out_unmap;
2932
2933 for (bus = 0; bus < 256; bus++) {
2934 ret = copy_context_table(iommu, &old_rt[bus],
2935 ctxt_tbls, bus, ext);
2936 if (ret) {
2937 pr_err("%s: Failed to copy context table for bus %d\n",
2938 iommu->name, bus);
2939 continue;
2940 }
2941 }
2942
2943 spin_lock_irqsave(&iommu->lock, flags);
2944
2945 /* Context tables are copied, now write them to the root_entry table */
2946 for (bus = 0; bus < 256; bus++) {
2947 int idx = ext ? bus * 2 : bus;
2948 u64 val;
2949
2950 if (ctxt_tbls[idx]) {
2951 val = virt_to_phys(ctxt_tbls[idx]) | 1;
2952 iommu->root_entry[bus].lo = val;
2953 }
2954
2955 if (!ext || !ctxt_tbls[idx + 1])
2956 continue;
2957
2958 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
2959 iommu->root_entry[bus].hi = val;
2960 }
2961
2962 spin_unlock_irqrestore(&iommu->lock, flags);
2963
2964 kfree(ctxt_tbls);
2965
2966 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
2967
2968 ret = 0;
2969
2970out_unmap:
2971 iounmap(old_rt);
2972
2973 return ret;
2974}
2975
Joseph Cihulab7792602011-05-03 00:08:37 -07002976static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002977{
2978 struct dmar_drhd_unit *drhd;
2979 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02002980 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00002981 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002982 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002983 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002984
2985 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002986 * for each drhd
2987 * allocate root
2988 * initialize and program root entry to not present
2989 * endfor
2990 */
2991 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002992 /*
2993 * lock not needed as this is only incremented in the single
2994 * threaded kernel __init code path all other access are read
2995 * only
2996 */
Jiang Liu78d8e702014-11-09 22:47:57 +08002997 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08002998 g_num_of_iommus++;
2999 continue;
3000 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003001 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003002 }
3003
Jiang Liuffebeb42014-11-09 22:48:02 +08003004 /* Preallocate enough resources for IOMMU hot-addition */
3005 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3006 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3007
Weidong Hand9630fe2008-12-08 11:06:32 +08003008 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3009 GFP_KERNEL);
3010 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003011 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003012 ret = -ENOMEM;
3013 goto error;
3014 }
3015
mark gross80b20dd2008-04-18 13:53:58 -07003016 deferred_flush = kzalloc(g_num_of_iommus *
3017 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3018 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003019 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003020 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003021 }
3022
Jiang Liu7c919772014-01-06 14:18:18 +08003023 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003024 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003025
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003026 intel_iommu_init_qi(iommu);
3027
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003028 ret = iommu_init_domains(iommu);
3029 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003030 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003031
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003032 init_translation_status(iommu);
3033
Joerg Roedel091d42e2015-06-12 11:56:10 +02003034 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3035 iommu_disable_translation(iommu);
3036 clear_translation_pre_enabled(iommu);
3037 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3038 iommu->name);
3039 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003040
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003041 /*
3042 * TBD:
3043 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003044 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003045 */
3046 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003047 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003048 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003049
Joerg Roedel091d42e2015-06-12 11:56:10 +02003050 if (translation_pre_enabled(iommu)) {
3051 pr_info("Translation already enabled - trying to copy translation structures\n");
3052
3053 ret = copy_translation_tables(iommu);
3054 if (ret) {
3055 /*
3056 * We found the IOMMU with translation
3057 * enabled - but failed to copy over the
3058 * old root-entry table. Try to proceed
3059 * by disabling translation now and
3060 * allocating a clean root-entry table.
3061 * This might cause DMAR faults, but
3062 * probably the dump will still succeed.
3063 */
3064 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3065 iommu->name);
3066 iommu_disable_translation(iommu);
3067 clear_translation_pre_enabled(iommu);
3068 } else {
3069 pr_info("Copied translation tables from previous kernel for %s\n",
3070 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003071 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003072 }
3073 }
3074
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003075 iommu_flush_write_buffer(iommu);
3076 iommu_set_root_entry(iommu);
3077 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3078 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3079
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003080 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003081 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003082 }
3083
David Woodhouse19943b02009-08-04 16:19:20 +01003084 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003085 iommu_identity_mapping |= IDENTMAP_ALL;
3086
Suresh Siddhad3f13812011-08-23 17:05:25 -07003087#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003088 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003089#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003090
Joerg Roedel86080cc2015-06-12 12:27:16 +02003091 if (iommu_identity_mapping) {
3092 ret = si_domain_init(hw_pass_through);
3093 if (ret)
3094 goto free_iommu;
3095 }
3096
David Woodhousee0fc7e02009-09-30 09:12:17 -07003097 check_tylersburg_isoch();
3098
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003099 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003100 * If we copied translations from a previous kernel in the kdump
3101 * case, we can not assign the devices to domains now, as that
3102 * would eliminate the old mappings. So skip this part and defer
3103 * the assignment to device driver initialization time.
3104 */
3105 if (copied_tables)
3106 goto domains_done;
3107
3108 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003109 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003110 * identity mappings for rmrr, gfx, and isa and may fall back to static
3111 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003112 */
David Woodhouse19943b02009-08-04 16:19:20 +01003113 if (iommu_identity_mapping) {
3114 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3115 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003116 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003117 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003118 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003119 }
David Woodhouse19943b02009-08-04 16:19:20 +01003120 /*
3121 * For each rmrr
3122 * for each dev attached to rmrr
3123 * do
3124 * locate drhd for dev, alloc domain for dev
3125 * allocate free domain
3126 * allocate page table entries for rmrr
3127 * if context not allocated for bus
3128 * allocate and init context
3129 * set present in root table for this bus
3130 * init context with domain, translation etc
3131 * endfor
3132 * endfor
3133 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003134 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003135 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003136 /* some BIOS lists non-exist devices in DMAR table. */
3137 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003138 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003139 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003140 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003141 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003142 }
3143 }
3144
3145 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003146
Joerg Roedela87f4912015-06-12 12:32:54 +02003147domains_done:
3148
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003149 /*
3150 * for each drhd
3151 * enable fault log
3152 * global invalidate context cache
3153 * global invalidate iotlb
3154 * enable translation
3155 */
Jiang Liu7c919772014-01-06 14:18:18 +08003156 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003157 if (drhd->ignored) {
3158 /*
3159 * we always have to disable PMRs or DMA may fail on
3160 * this device
3161 */
3162 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003163 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003164 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003165 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003166
3167 iommu_flush_write_buffer(iommu);
3168
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003169 ret = dmar_set_interrupt(iommu);
3170 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003171 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003172
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003173 if (!translation_pre_enabled(iommu))
3174 iommu_enable_translation(iommu);
3175
David Woodhouseb94996c2009-09-19 15:28:12 -07003176 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003177 }
3178
3179 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003180
3181free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003182 for_each_active_iommu(iommu, drhd) {
3183 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003184 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003185 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003186 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003187free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003188 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003189error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003190 return ret;
3191}
3192
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003193/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003194static struct iova *intel_alloc_iova(struct device *dev,
3195 struct dmar_domain *domain,
3196 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003197{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003198 struct iova *iova = NULL;
3199
David Woodhouse875764d2009-06-28 21:20:51 +01003200 /* Restrict dma_mask to the width that the iommu can handle */
3201 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
3202
3203 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003204 /*
3205 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003206 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003207 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003208 */
David Woodhouse875764d2009-06-28 21:20:51 +01003209 iova = alloc_iova(&domain->iovad, nrpages,
3210 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3211 if (iova)
3212 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003213 }
David Woodhouse875764d2009-06-28 21:20:51 +01003214 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3215 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003216 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003217 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003218 return NULL;
3219 }
3220
3221 return iova;
3222}
3223
David Woodhoused4b709f2014-03-09 16:07:40 -07003224static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003225{
3226 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003227
David Woodhoused4b709f2014-03-09 16:07:40 -07003228 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003229 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003230 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003231 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003232 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003233 }
3234
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003235 return domain;
3236}
3237
David Woodhoused4b709f2014-03-09 16:07:40 -07003238static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003239{
3240 struct device_domain_info *info;
3241
3242 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003243 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003244 if (likely(info))
3245 return info->domain;
3246
3247 return __get_valid_domain_for_dev(dev);
3248}
3249
David Woodhouseecb509e2014-03-09 16:29:55 -07003250/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003251static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003252{
3253 int found;
3254
David Woodhouse3d891942014-03-06 15:59:26 +00003255 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003256 return 1;
3257
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003258 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003259 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003260
David Woodhouse9b226622014-03-09 14:03:28 -07003261 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003262 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003263 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003264 return 1;
3265 else {
3266 /*
3267 * 32 bit DMA is removed from si_domain and fall back
3268 * to non-identity mapping.
3269 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003270 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003271 pr_info("32bit %s uses non-identity mapping\n",
3272 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003273 return 0;
3274 }
3275 } else {
3276 /*
3277 * In case of a detached 64 bit DMA device from vm, the device
3278 * is put into si_domain for identity mapping.
3279 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003280 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003281 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003282 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003283 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003284 pr_info("64bit %s uses identity mapping\n",
3285 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003286 return 1;
3287 }
3288 }
3289 }
3290
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003291 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003292}
3293
David Woodhouse5040a912014-03-09 16:14:00 -07003294static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003295 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003296{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003297 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003298 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003299 struct iova *iova;
3300 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003301 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003302 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003303 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003304
3305 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003306
David Woodhouse5040a912014-03-09 16:14:00 -07003307 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003308 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003309
David Woodhouse5040a912014-03-09 16:14:00 -07003310 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003311 if (!domain)
3312 return 0;
3313
Weidong Han8c11e792008-12-08 15:29:22 +08003314 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003315 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003316
David Woodhouse5040a912014-03-09 16:14:00 -07003317 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003318 if (!iova)
3319 goto error;
3320
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003321 /*
3322 * Check if DMAR supports zero-length reads on write only
3323 * mappings..
3324 */
3325 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003326 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003327 prot |= DMA_PTE_READ;
3328 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3329 prot |= DMA_PTE_WRITE;
3330 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003331 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003332 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003333 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003334 * is not a big problem
3335 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003336 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003337 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003338 if (ret)
3339 goto error;
3340
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003341 /* it's a non-present to present mapping. Only flush if caching mode */
3342 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003343 iommu_flush_iotlb_psi(iommu, domain,
3344 mm_to_dma_pfn(iova->pfn_lo),
3345 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003346 else
Weidong Han8c11e792008-12-08 15:29:22 +08003347 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003348
David Woodhouse03d6a242009-06-28 15:33:46 +01003349 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3350 start_paddr += paddr & ~PAGE_MASK;
3351 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003352
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003353error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003354 if (iova)
3355 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003356 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003357 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003358 return 0;
3359}
3360
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003361static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3362 unsigned long offset, size_t size,
3363 enum dma_data_direction dir,
3364 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003365{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003366 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003367 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003368}
3369
mark gross5e0d2a62008-03-04 15:22:08 -08003370static void flush_unmaps(void)
3371{
mark gross80b20dd2008-04-18 13:53:58 -07003372 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003373
mark gross5e0d2a62008-03-04 15:22:08 -08003374 timer_on = 0;
3375
3376 /* just flush them all */
3377 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003378 struct intel_iommu *iommu = g_iommus[i];
3379 if (!iommu)
3380 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003381
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003382 if (!deferred_flush[i].next)
3383 continue;
3384
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003385 /* In caching mode, global flushes turn emulation expensive */
3386 if (!cap_caching_mode(iommu->cap))
3387 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003388 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003389 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003390 unsigned long mask;
3391 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003392 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003393
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003394 /* On real hardware multiple invalidations are expensive */
3395 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003396 iommu_flush_iotlb_psi(iommu, domain,
Jiang Liua156ef92014-07-11 14:19:36 +08003397 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003398 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003399 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003400 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003401 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3402 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3403 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003404 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003405 if (deferred_flush[i].freelist[j])
3406 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003407 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003408 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003409 }
3410
mark gross5e0d2a62008-03-04 15:22:08 -08003411 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003412}
3413
3414static void flush_unmaps_timeout(unsigned long data)
3415{
mark gross80b20dd2008-04-18 13:53:58 -07003416 unsigned long flags;
3417
3418 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003419 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003420 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003421}
3422
David Woodhouseea8ea462014-03-05 17:09:32 +00003423static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003424{
3425 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003426 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003427 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003428
3429 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003430 if (list_size == HIGH_WATER_MARK)
3431 flush_unmaps();
3432
Weidong Han8c11e792008-12-08 15:29:22 +08003433 iommu = domain_get_iommu(dom);
3434 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003435
mark gross80b20dd2008-04-18 13:53:58 -07003436 next = deferred_flush[iommu_id].next;
3437 deferred_flush[iommu_id].domain[next] = dom;
3438 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003439 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003440 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003441
3442 if (!timer_on) {
3443 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3444 timer_on = 1;
3445 }
3446 list_size++;
3447 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3448}
3449
Jiang Liud41a4ad2014-07-11 14:19:34 +08003450static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003451{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003452 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003453 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003454 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003455 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003456 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003457
David Woodhouse73676832009-07-04 14:08:36 +01003458 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003459 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003460
David Woodhouse1525a292014-03-06 16:19:30 +00003461 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003462 BUG_ON(!domain);
3463
Weidong Han8c11e792008-12-08 15:29:22 +08003464 iommu = domain_get_iommu(domain);
3465
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003466 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003467 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3468 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003469 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003470
David Woodhoused794dc92009-06-28 00:27:49 +01003471 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3472 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003473
David Woodhoused794dc92009-06-28 00:27:49 +01003474 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003475 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003476
David Woodhouseea8ea462014-03-05 17:09:32 +00003477 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003478
mark gross5e0d2a62008-03-04 15:22:08 -08003479 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003480 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003481 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003482 /* free iova */
3483 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003484 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003485 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003486 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003487 /*
3488 * queue up the release of the unmap to save the 1/6th of the
3489 * cpu used up by the iotlb flush operation...
3490 */
mark gross5e0d2a62008-03-04 15:22:08 -08003491 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003492}
3493
Jiang Liud41a4ad2014-07-11 14:19:34 +08003494static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3495 size_t size, enum dma_data_direction dir,
3496 struct dma_attrs *attrs)
3497{
3498 intel_unmap(dev, dev_addr);
3499}
3500
David Woodhouse5040a912014-03-09 16:14:00 -07003501static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003502 dma_addr_t *dma_handle, gfp_t flags,
3503 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003504{
Akinobu Mita36746432014-06-04 16:06:51 -07003505 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003506 int order;
3507
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003508 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003509 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003510
David Woodhouse5040a912014-03-09 16:14:00 -07003511 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003512 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003513 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3514 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003515 flags |= GFP_DMA;
3516 else
3517 flags |= GFP_DMA32;
3518 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003519
Akinobu Mita36746432014-06-04 16:06:51 -07003520 if (flags & __GFP_WAIT) {
3521 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003522
Akinobu Mita36746432014-06-04 16:06:51 -07003523 page = dma_alloc_from_contiguous(dev, count, order);
3524 if (page && iommu_no_mapping(dev) &&
3525 page_to_phys(page) + size > dev->coherent_dma_mask) {
3526 dma_release_from_contiguous(dev, page, count);
3527 page = NULL;
3528 }
3529 }
3530
3531 if (!page)
3532 page = alloc_pages(flags, order);
3533 if (!page)
3534 return NULL;
3535 memset(page_address(page), 0, size);
3536
3537 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003538 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003539 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003540 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003541 return page_address(page);
3542 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3543 __free_pages(page, order);
3544
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003545 return NULL;
3546}
3547
David Woodhouse5040a912014-03-09 16:14:00 -07003548static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003549 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003550{
3551 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003552 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003553
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003554 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003555 order = get_order(size);
3556
Jiang Liud41a4ad2014-07-11 14:19:34 +08003557 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003558 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3559 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003560}
3561
David Woodhouse5040a912014-03-09 16:14:00 -07003562static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003563 int nelems, enum dma_data_direction dir,
3564 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003565{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003566 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003567}
3568
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003569static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003570 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003571{
3572 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003573 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003574
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003575 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003576 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003577 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003578 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003579 }
3580 return nelems;
3581}
3582
David Woodhouse5040a912014-03-09 16:14:00 -07003583static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003584 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003585{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003586 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003587 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003588 size_t size = 0;
3589 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003590 struct iova *iova = NULL;
3591 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003592 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003593 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003594 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003595
3596 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003597 if (iommu_no_mapping(dev))
3598 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003599
David Woodhouse5040a912014-03-09 16:14:00 -07003600 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003601 if (!domain)
3602 return 0;
3603
Weidong Han8c11e792008-12-08 15:29:22 +08003604 iommu = domain_get_iommu(domain);
3605
David Woodhouseb536d242009-06-28 14:49:31 +01003606 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003607 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003608
David Woodhouse5040a912014-03-09 16:14:00 -07003609 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3610 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003611 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003612 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003613 return 0;
3614 }
3615
3616 /*
3617 * Check if DMAR supports zero-length reads on write only
3618 * mappings..
3619 */
3620 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003621 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003622 prot |= DMA_PTE_READ;
3623 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3624 prot |= DMA_PTE_WRITE;
3625
David Woodhouseb536d242009-06-28 14:49:31 +01003626 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003627
Fenghua Yuf5329592009-08-04 15:09:37 -07003628 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003629 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003630 dma_pte_free_pagetable(domain, start_vpfn,
3631 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003632 __free_iova(&domain->iovad, iova);
3633 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003634 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003635
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003636 /* it's a non-present to present mapping. Only flush if caching mode */
3637 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003638 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003639 else
Weidong Han8c11e792008-12-08 15:29:22 +08003640 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003641
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003642 return nelems;
3643}
3644
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003645static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3646{
3647 return !dma_addr;
3648}
3649
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003650struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003651 .alloc = intel_alloc_coherent,
3652 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003653 .map_sg = intel_map_sg,
3654 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003655 .map_page = intel_map_page,
3656 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003657 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003658};
3659
3660static inline int iommu_domain_cache_init(void)
3661{
3662 int ret = 0;
3663
3664 iommu_domain_cache = kmem_cache_create("iommu_domain",
3665 sizeof(struct dmar_domain),
3666 0,
3667 SLAB_HWCACHE_ALIGN,
3668
3669 NULL);
3670 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003671 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003672 ret = -ENOMEM;
3673 }
3674
3675 return ret;
3676}
3677
3678static inline int iommu_devinfo_cache_init(void)
3679{
3680 int ret = 0;
3681
3682 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3683 sizeof(struct device_domain_info),
3684 0,
3685 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003686 NULL);
3687 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003688 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003689 ret = -ENOMEM;
3690 }
3691
3692 return ret;
3693}
3694
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003695static int __init iommu_init_mempool(void)
3696{
3697 int ret;
3698 ret = iommu_iova_cache_init();
3699 if (ret)
3700 return ret;
3701
3702 ret = iommu_domain_cache_init();
3703 if (ret)
3704 goto domain_error;
3705
3706 ret = iommu_devinfo_cache_init();
3707 if (!ret)
3708 return ret;
3709
3710 kmem_cache_destroy(iommu_domain_cache);
3711domain_error:
Robin Murphy85b45452015-01-12 17:51:14 +00003712 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003713
3714 return -ENOMEM;
3715}
3716
3717static void __init iommu_exit_mempool(void)
3718{
3719 kmem_cache_destroy(iommu_devinfo_cache);
3720 kmem_cache_destroy(iommu_domain_cache);
Robin Murphy85b45452015-01-12 17:51:14 +00003721 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003722}
3723
Dan Williams556ab452010-07-23 15:47:56 -07003724static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3725{
3726 struct dmar_drhd_unit *drhd;
3727 u32 vtbar;
3728 int rc;
3729
3730 /* We know that this device on this chipset has its own IOMMU.
3731 * If we find it under a different IOMMU, then the BIOS is lying
3732 * to us. Hope that the IOMMU for this device is actually
3733 * disabled, and it needs no translation...
3734 */
3735 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3736 if (rc) {
3737 /* "can't" happen */
3738 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3739 return;
3740 }
3741 vtbar &= 0xffff0000;
3742
3743 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3744 drhd = dmar_find_matched_drhd_unit(pdev);
3745 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3746 TAINT_FIRMWARE_WORKAROUND,
3747 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3748 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3749}
3750DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3751
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003752static void __init init_no_remapping_devices(void)
3753{
3754 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003755 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003756 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003757
3758 for_each_drhd_unit(drhd) {
3759 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003760 for_each_active_dev_scope(drhd->devices,
3761 drhd->devices_cnt, i, dev)
3762 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003763 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003764 if (i == drhd->devices_cnt)
3765 drhd->ignored = 1;
3766 }
3767 }
3768
Jiang Liu7c919772014-01-06 14:18:18 +08003769 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003770 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003771 continue;
3772
Jiang Liub683b232014-02-19 14:07:32 +08003773 for_each_active_dev_scope(drhd->devices,
3774 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003775 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003776 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003777 if (i < drhd->devices_cnt)
3778 continue;
3779
David Woodhousec0771df2011-10-14 20:59:46 +01003780 /* This IOMMU has *only* gfx devices. Either bypass it or
3781 set the gfx_mapped flag, as appropriate */
3782 if (dmar_map_gfx) {
3783 intel_iommu_gfx_mapped = 1;
3784 } else {
3785 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003786 for_each_active_dev_scope(drhd->devices,
3787 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003788 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003789 }
3790 }
3791}
3792
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003793#ifdef CONFIG_SUSPEND
3794static int init_iommu_hw(void)
3795{
3796 struct dmar_drhd_unit *drhd;
3797 struct intel_iommu *iommu = NULL;
3798
3799 for_each_active_iommu(iommu, drhd)
3800 if (iommu->qi)
3801 dmar_reenable_qi(iommu);
3802
Joseph Cihulab7792602011-05-03 00:08:37 -07003803 for_each_iommu(iommu, drhd) {
3804 if (drhd->ignored) {
3805 /*
3806 * we always have to disable PMRs or DMA may fail on
3807 * this device
3808 */
3809 if (force_on)
3810 iommu_disable_protect_mem_regions(iommu);
3811 continue;
3812 }
3813
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003814 iommu_flush_write_buffer(iommu);
3815
3816 iommu_set_root_entry(iommu);
3817
3818 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003819 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003820 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3821 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003822 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003823 }
3824
3825 return 0;
3826}
3827
3828static void iommu_flush_all(void)
3829{
3830 struct dmar_drhd_unit *drhd;
3831 struct intel_iommu *iommu;
3832
3833 for_each_active_iommu(iommu, drhd) {
3834 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003835 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003836 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003837 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003838 }
3839}
3840
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003841static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003842{
3843 struct dmar_drhd_unit *drhd;
3844 struct intel_iommu *iommu = NULL;
3845 unsigned long flag;
3846
3847 for_each_active_iommu(iommu, drhd) {
3848 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3849 GFP_ATOMIC);
3850 if (!iommu->iommu_state)
3851 goto nomem;
3852 }
3853
3854 iommu_flush_all();
3855
3856 for_each_active_iommu(iommu, drhd) {
3857 iommu_disable_translation(iommu);
3858
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003859 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003860
3861 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3862 readl(iommu->reg + DMAR_FECTL_REG);
3863 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3864 readl(iommu->reg + DMAR_FEDATA_REG);
3865 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3866 readl(iommu->reg + DMAR_FEADDR_REG);
3867 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3868 readl(iommu->reg + DMAR_FEUADDR_REG);
3869
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003870 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003871 }
3872 return 0;
3873
3874nomem:
3875 for_each_active_iommu(iommu, drhd)
3876 kfree(iommu->iommu_state);
3877
3878 return -ENOMEM;
3879}
3880
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003881static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003882{
3883 struct dmar_drhd_unit *drhd;
3884 struct intel_iommu *iommu = NULL;
3885 unsigned long flag;
3886
3887 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003888 if (force_on)
3889 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3890 else
3891 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003892 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003893 }
3894
3895 for_each_active_iommu(iommu, drhd) {
3896
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003897 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003898
3899 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3900 iommu->reg + DMAR_FECTL_REG);
3901 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3902 iommu->reg + DMAR_FEDATA_REG);
3903 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3904 iommu->reg + DMAR_FEADDR_REG);
3905 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3906 iommu->reg + DMAR_FEUADDR_REG);
3907
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003908 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003909 }
3910
3911 for_each_active_iommu(iommu, drhd)
3912 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003913}
3914
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003915static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003916 .resume = iommu_resume,
3917 .suspend = iommu_suspend,
3918};
3919
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003920static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003921{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003922 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003923}
3924
3925#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003926static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003927#endif /* CONFIG_PM */
3928
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003929
Jiang Liuc2a0b532014-11-09 22:47:56 +08003930int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003931{
3932 struct acpi_dmar_reserved_memory *rmrr;
3933 struct dmar_rmrr_unit *rmrru;
3934
3935 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3936 if (!rmrru)
3937 return -ENOMEM;
3938
3939 rmrru->hdr = header;
3940 rmrr = (struct acpi_dmar_reserved_memory *)header;
3941 rmrru->base_address = rmrr->base_address;
3942 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08003943 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
3944 ((void *)rmrr) + rmrr->header.length,
3945 &rmrru->devices_cnt);
3946 if (rmrru->devices_cnt && rmrru->devices == NULL) {
3947 kfree(rmrru);
3948 return -ENOMEM;
3949 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003950
Jiang Liu2e455282014-02-19 14:07:36 +08003951 list_add(&rmrru->list, &dmar_rmrr_units);
3952
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003953 return 0;
3954}
3955
Jiang Liu6b197242014-11-09 22:47:58 +08003956static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
3957{
3958 struct dmar_atsr_unit *atsru;
3959 struct acpi_dmar_atsr *tmp;
3960
3961 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3962 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
3963 if (atsr->segment != tmp->segment)
3964 continue;
3965 if (atsr->header.length != tmp->header.length)
3966 continue;
3967 if (memcmp(atsr, tmp, atsr->header.length) == 0)
3968 return atsru;
3969 }
3970
3971 return NULL;
3972}
3973
3974int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003975{
3976 struct acpi_dmar_atsr *atsr;
3977 struct dmar_atsr_unit *atsru;
3978
Jiang Liu6b197242014-11-09 22:47:58 +08003979 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
3980 return 0;
3981
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003982 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08003983 atsru = dmar_find_atsr(atsr);
3984 if (atsru)
3985 return 0;
3986
3987 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003988 if (!atsru)
3989 return -ENOMEM;
3990
Jiang Liu6b197242014-11-09 22:47:58 +08003991 /*
3992 * If memory is allocated from slab by ACPI _DSM method, we need to
3993 * copy the memory content because the memory buffer will be freed
3994 * on return.
3995 */
3996 atsru->hdr = (void *)(atsru + 1);
3997 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003998 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08003999 if (!atsru->include_all) {
4000 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4001 (void *)atsr + atsr->header.length,
4002 &atsru->devices_cnt);
4003 if (atsru->devices_cnt && atsru->devices == NULL) {
4004 kfree(atsru);
4005 return -ENOMEM;
4006 }
4007 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004008
Jiang Liu0e242612014-02-19 14:07:34 +08004009 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004010
4011 return 0;
4012}
4013
Jiang Liu9bdc5312014-01-06 14:18:27 +08004014static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4015{
4016 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4017 kfree(atsru);
4018}
4019
Jiang Liu6b197242014-11-09 22:47:58 +08004020int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4021{
4022 struct acpi_dmar_atsr *atsr;
4023 struct dmar_atsr_unit *atsru;
4024
4025 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4026 atsru = dmar_find_atsr(atsr);
4027 if (atsru) {
4028 list_del_rcu(&atsru->list);
4029 synchronize_rcu();
4030 intel_iommu_free_atsr(atsru);
4031 }
4032
4033 return 0;
4034}
4035
4036int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4037{
4038 int i;
4039 struct device *dev;
4040 struct acpi_dmar_atsr *atsr;
4041 struct dmar_atsr_unit *atsru;
4042
4043 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4044 atsru = dmar_find_atsr(atsr);
4045 if (!atsru)
4046 return 0;
4047
4048 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4049 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4050 i, dev)
4051 return -EBUSY;
4052
4053 return 0;
4054}
4055
Jiang Liuffebeb42014-11-09 22:48:02 +08004056static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4057{
4058 int sp, ret = 0;
4059 struct intel_iommu *iommu = dmaru->iommu;
4060
4061 if (g_iommus[iommu->seq_id])
4062 return 0;
4063
4064 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004065 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004066 iommu->name);
4067 return -ENXIO;
4068 }
4069 if (!ecap_sc_support(iommu->ecap) &&
4070 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004071 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004072 iommu->name);
4073 return -ENXIO;
4074 }
4075 sp = domain_update_iommu_superpage(iommu) - 1;
4076 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004077 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004078 iommu->name);
4079 return -ENXIO;
4080 }
4081
4082 /*
4083 * Disable translation if already enabled prior to OS handover.
4084 */
4085 if (iommu->gcmd & DMA_GCMD_TE)
4086 iommu_disable_translation(iommu);
4087
4088 g_iommus[iommu->seq_id] = iommu;
4089 ret = iommu_init_domains(iommu);
4090 if (ret == 0)
4091 ret = iommu_alloc_root_entry(iommu);
4092 if (ret)
4093 goto out;
4094
4095 if (dmaru->ignored) {
4096 /*
4097 * we always have to disable PMRs or DMA may fail on this device
4098 */
4099 if (force_on)
4100 iommu_disable_protect_mem_regions(iommu);
4101 return 0;
4102 }
4103
4104 intel_iommu_init_qi(iommu);
4105 iommu_flush_write_buffer(iommu);
4106 ret = dmar_set_interrupt(iommu);
4107 if (ret)
4108 goto disable_iommu;
4109
4110 iommu_set_root_entry(iommu);
4111 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4112 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4113 iommu_enable_translation(iommu);
4114
Jiang Liuffebeb42014-11-09 22:48:02 +08004115 iommu_disable_protect_mem_regions(iommu);
4116 return 0;
4117
4118disable_iommu:
4119 disable_dmar_iommu(iommu);
4120out:
4121 free_dmar_iommu(iommu);
4122 return ret;
4123}
4124
Jiang Liu6b197242014-11-09 22:47:58 +08004125int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4126{
Jiang Liuffebeb42014-11-09 22:48:02 +08004127 int ret = 0;
4128 struct intel_iommu *iommu = dmaru->iommu;
4129
4130 if (!intel_iommu_enabled)
4131 return 0;
4132 if (iommu == NULL)
4133 return -EINVAL;
4134
4135 if (insert) {
4136 ret = intel_iommu_add(dmaru);
4137 } else {
4138 disable_dmar_iommu(iommu);
4139 free_dmar_iommu(iommu);
4140 }
4141
4142 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004143}
4144
Jiang Liu9bdc5312014-01-06 14:18:27 +08004145static void intel_iommu_free_dmars(void)
4146{
4147 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4148 struct dmar_atsr_unit *atsru, *atsr_n;
4149
4150 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4151 list_del(&rmrru->list);
4152 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4153 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004154 }
4155
Jiang Liu9bdc5312014-01-06 14:18:27 +08004156 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4157 list_del(&atsru->list);
4158 intel_iommu_free_atsr(atsru);
4159 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004160}
4161
4162int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4163{
Jiang Liub683b232014-02-19 14:07:32 +08004164 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004165 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004166 struct pci_dev *bridge = NULL;
4167 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004168 struct acpi_dmar_atsr *atsr;
4169 struct dmar_atsr_unit *atsru;
4170
4171 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004172 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004173 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004174 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004175 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004176 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004177 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004178 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004179 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08004180 if (!bridge)
4181 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004182
Jiang Liu0e242612014-02-19 14:07:34 +08004183 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004184 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4185 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4186 if (atsr->segment != pci_domain_nr(dev->bus))
4187 continue;
4188
Jiang Liub683b232014-02-19 14:07:32 +08004189 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004190 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004191 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004192
4193 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004194 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004195 }
Jiang Liub683b232014-02-19 14:07:32 +08004196 ret = 0;
4197out:
Jiang Liu0e242612014-02-19 14:07:34 +08004198 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004199
Jiang Liub683b232014-02-19 14:07:32 +08004200 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004201}
4202
Jiang Liu59ce0512014-02-19 14:07:35 +08004203int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4204{
4205 int ret = 0;
4206 struct dmar_rmrr_unit *rmrru;
4207 struct dmar_atsr_unit *atsru;
4208 struct acpi_dmar_atsr *atsr;
4209 struct acpi_dmar_reserved_memory *rmrr;
4210
4211 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4212 return 0;
4213
4214 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4215 rmrr = container_of(rmrru->hdr,
4216 struct acpi_dmar_reserved_memory, header);
4217 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4218 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4219 ((void *)rmrr) + rmrr->header.length,
4220 rmrr->segment, rmrru->devices,
4221 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004222 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004223 return ret;
4224 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004225 dmar_remove_dev_scope(info, rmrr->segment,
4226 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004227 }
4228 }
4229
4230 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4231 if (atsru->include_all)
4232 continue;
4233
4234 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4235 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4236 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4237 (void *)atsr + atsr->header.length,
4238 atsr->segment, atsru->devices,
4239 atsru->devices_cnt);
4240 if (ret > 0)
4241 break;
4242 else if(ret < 0)
4243 return ret;
4244 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4245 if (dmar_remove_dev_scope(info, atsr->segment,
4246 atsru->devices, atsru->devices_cnt))
4247 break;
4248 }
4249 }
4250
4251 return 0;
4252}
4253
Fenghua Yu99dcade2009-11-11 07:23:06 -08004254/*
4255 * Here we only respond to action of unbound device from driver.
4256 *
4257 * Added device is not attached to its DMAR domain here yet. That will happen
4258 * when mapping the device to iova.
4259 */
4260static int device_notifier(struct notifier_block *nb,
4261 unsigned long action, void *data)
4262{
4263 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004264 struct dmar_domain *domain;
4265
David Woodhouse3d891942014-03-06 15:59:26 +00004266 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004267 return 0;
4268
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004269 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004270 return 0;
4271
David Woodhouse1525a292014-03-06 16:19:30 +00004272 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004273 if (!domain)
4274 return 0;
4275
Jiang Liu3a5670e2014-02-19 14:07:33 +08004276 down_read(&dmar_global_lock);
Joerg Roedele6de0f82015-07-22 16:30:36 +02004277 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004278 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004279 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08004280 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07004281
Fenghua Yu99dcade2009-11-11 07:23:06 -08004282 return 0;
4283}
4284
4285static struct notifier_block device_nb = {
4286 .notifier_call = device_notifier,
4287};
4288
Jiang Liu75f05562014-02-19 14:07:37 +08004289static int intel_iommu_memory_notifier(struct notifier_block *nb,
4290 unsigned long val, void *v)
4291{
4292 struct memory_notify *mhp = v;
4293 unsigned long long start, end;
4294 unsigned long start_vpfn, last_vpfn;
4295
4296 switch (val) {
4297 case MEM_GOING_ONLINE:
4298 start = mhp->start_pfn << PAGE_SHIFT;
4299 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4300 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004301 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004302 start, end);
4303 return NOTIFY_BAD;
4304 }
4305 break;
4306
4307 case MEM_OFFLINE:
4308 case MEM_CANCEL_ONLINE:
4309 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4310 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4311 while (start_vpfn <= last_vpfn) {
4312 struct iova *iova;
4313 struct dmar_drhd_unit *drhd;
4314 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004315 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004316
4317 iova = find_iova(&si_domain->iovad, start_vpfn);
4318 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004319 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004320 start_vpfn);
4321 break;
4322 }
4323
4324 iova = split_and_remove_iova(&si_domain->iovad, iova,
4325 start_vpfn, last_vpfn);
4326 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004327 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004328 start_vpfn, last_vpfn);
4329 return NOTIFY_BAD;
4330 }
4331
David Woodhouseea8ea462014-03-05 17:09:32 +00004332 freelist = domain_unmap(si_domain, iova->pfn_lo,
4333 iova->pfn_hi);
4334
Jiang Liu75f05562014-02-19 14:07:37 +08004335 rcu_read_lock();
4336 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004337 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004338 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004339 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004340 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004341 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004342
4343 start_vpfn = iova->pfn_hi + 1;
4344 free_iova_mem(iova);
4345 }
4346 break;
4347 }
4348
4349 return NOTIFY_OK;
4350}
4351
4352static struct notifier_block intel_iommu_memory_nb = {
4353 .notifier_call = intel_iommu_memory_notifier,
4354 .priority = 0
4355};
4356
Alex Williamsona5459cf2014-06-12 16:12:31 -06004357
4358static ssize_t intel_iommu_show_version(struct device *dev,
4359 struct device_attribute *attr,
4360 char *buf)
4361{
4362 struct intel_iommu *iommu = dev_get_drvdata(dev);
4363 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4364 return sprintf(buf, "%d:%d\n",
4365 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4366}
4367static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4368
4369static ssize_t intel_iommu_show_address(struct device *dev,
4370 struct device_attribute *attr,
4371 char *buf)
4372{
4373 struct intel_iommu *iommu = dev_get_drvdata(dev);
4374 return sprintf(buf, "%llx\n", iommu->reg_phys);
4375}
4376static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4377
4378static ssize_t intel_iommu_show_cap(struct device *dev,
4379 struct device_attribute *attr,
4380 char *buf)
4381{
4382 struct intel_iommu *iommu = dev_get_drvdata(dev);
4383 return sprintf(buf, "%llx\n", iommu->cap);
4384}
4385static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4386
4387static ssize_t intel_iommu_show_ecap(struct device *dev,
4388 struct device_attribute *attr,
4389 char *buf)
4390{
4391 struct intel_iommu *iommu = dev_get_drvdata(dev);
4392 return sprintf(buf, "%llx\n", iommu->ecap);
4393}
4394static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4395
Alex Williamson2238c082015-07-14 15:24:53 -06004396static ssize_t intel_iommu_show_ndoms(struct device *dev,
4397 struct device_attribute *attr,
4398 char *buf)
4399{
4400 struct intel_iommu *iommu = dev_get_drvdata(dev);
4401 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4402}
4403static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4404
4405static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4406 struct device_attribute *attr,
4407 char *buf)
4408{
4409 struct intel_iommu *iommu = dev_get_drvdata(dev);
4410 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4411 cap_ndoms(iommu->cap)));
4412}
4413static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4414
Alex Williamsona5459cf2014-06-12 16:12:31 -06004415static struct attribute *intel_iommu_attrs[] = {
4416 &dev_attr_version.attr,
4417 &dev_attr_address.attr,
4418 &dev_attr_cap.attr,
4419 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004420 &dev_attr_domains_supported.attr,
4421 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004422 NULL,
4423};
4424
4425static struct attribute_group intel_iommu_group = {
4426 .name = "intel-iommu",
4427 .attrs = intel_iommu_attrs,
4428};
4429
4430const struct attribute_group *intel_iommu_groups[] = {
4431 &intel_iommu_group,
4432 NULL,
4433};
4434
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004435int __init intel_iommu_init(void)
4436{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004437 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004438 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004439 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004440
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004441 /* VT-d is required for a TXT/tboot launch, so enforce that */
4442 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004443
Jiang Liu3a5670e2014-02-19 14:07:33 +08004444 if (iommu_init_mempool()) {
4445 if (force_on)
4446 panic("tboot: Failed to initialize iommu memory\n");
4447 return -ENOMEM;
4448 }
4449
4450 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004451 if (dmar_table_init()) {
4452 if (force_on)
4453 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004454 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004455 }
4456
Suresh Siddhac2c72862011-08-23 17:05:19 -07004457 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004458 if (force_on)
4459 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004460 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004461 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004462
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004463 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004464 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004465
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004466 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004467 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004468
4469 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004470 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004471
Joseph Cihula51a63e62011-03-21 11:04:24 -07004472 if (dmar_init_reserved_ranges()) {
4473 if (force_on)
4474 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004475 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004476 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004477
4478 init_no_remapping_devices();
4479
Joseph Cihulab7792602011-05-03 00:08:37 -07004480 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004481 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004482 if (force_on)
4483 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004484 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004485 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004486 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004487 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004488 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004489
mark gross5e0d2a62008-03-04 15:22:08 -08004490 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004491#ifdef CONFIG_SWIOTLB
4492 swiotlb = 0;
4493#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004494 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004495
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004496 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004497
Alex Williamsona5459cf2014-06-12 16:12:31 -06004498 for_each_active_iommu(iommu, drhd)
4499 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4500 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004501 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004502
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004503 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004504 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004505 if (si_domain && !hw_pass_through)
4506 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004507
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004508 intel_iommu_enabled = 1;
4509
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004510 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004511
4512out_free_reserved_range:
4513 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004514out_free_dmar:
4515 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004516 up_write(&dmar_global_lock);
4517 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004518 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004519}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004520
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004521static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004522{
4523 struct intel_iommu *iommu = opaque;
4524
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004525 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004526 return 0;
4527}
4528
4529/*
4530 * NB - intel-iommu lacks any sort of reference counting for the users of
4531 * dependent devices. If multiple endpoints have intersecting dependent
4532 * devices, unbinding the driver from any one of them will possibly leave
4533 * the others unable to operate.
4534 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004535static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004536{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004537 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004538 return;
4539
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004540 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004541}
4542
Joerg Roedel55d94042015-07-22 16:50:40 +02004543static void __dmar_remove_one_dev_info(struct dmar_domain *domain,
4544 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004545{
Joerg Roedelb608ac32015-07-21 18:19:08 +02004546 struct device_domain_info *info;
Weidong Hanc7151a82008-12-08 22:51:37 +08004547 struct intel_iommu *iommu;
4548 unsigned long flags;
David Woodhouse156baca2014-03-09 14:00:57 -07004549 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004550
Joerg Roedel55d94042015-07-22 16:50:40 +02004551 assert_spin_locked(&device_domain_lock);
4552
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004553 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004554 if (!iommu)
4555 return;
4556
Joerg Roedelb608ac32015-07-21 18:19:08 +02004557 info = dev->archdata.iommu;
4558
4559 if (WARN_ON(!info))
4560 return;
4561
Joerg Roedelb608ac32015-07-21 18:19:08 +02004562 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004563
Joerg Roedelb608ac32015-07-21 18:19:08 +02004564 iommu_disable_dev_iotlb(info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004565 domain_context_clear(iommu, dev);
Joerg Roedelb608ac32015-07-21 18:19:08 +02004566 free_devinfo_mem(info);
Joerg Roedelb608ac32015-07-21 18:19:08 +02004567
Joerg Roedeld160aca2015-07-22 11:52:53 +02004568 spin_lock_irqsave(&iommu->lock, flags);
4569 domain_detach_iommu(domain, iommu);
4570 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08004571}
4572
Joerg Roedel55d94042015-07-22 16:50:40 +02004573static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4574 struct device *dev)
4575{
4576 unsigned long flags;
4577
4578 spin_lock_irqsave(&device_domain_lock, flags);
4579 __dmar_remove_one_dev_info(domain, dev);
4580 spin_unlock_irqrestore(&device_domain_lock, flags);
4581}
4582
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004583static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004584{
4585 int adjust_width;
4586
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004587 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4588 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004589 domain_reserve_special_ranges(domain);
4590
4591 /* calculate AGAW */
4592 domain->gaw = guest_width;
4593 adjust_width = guestwidth_to_adjustwidth(guest_width);
4594 domain->agaw = width_to_agaw(adjust_width);
4595
Weidong Han5e98c4b2008-12-08 23:03:27 +08004596 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004597 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004598 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004599 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004600
4601 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004602 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004603 if (!domain->pgd)
4604 return -ENOMEM;
4605 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4606 return 0;
4607}
4608
Joerg Roedel00a77de2015-03-26 13:43:08 +01004609static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004610{
Joerg Roedel5d450802008-12-03 14:52:32 +01004611 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004612 struct iommu_domain *domain;
4613
4614 if (type != IOMMU_DOMAIN_UNMANAGED)
4615 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004616
Jiang Liuab8dfe22014-07-11 14:19:27 +08004617 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004618 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004619 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004620 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004621 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004622 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004623 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004624 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004625 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004626 }
Allen Kay8140a952011-10-14 12:32:17 -07004627 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004628
Joerg Roedel00a77de2015-03-26 13:43:08 +01004629 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004630 domain->geometry.aperture_start = 0;
4631 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4632 domain->geometry.force_aperture = true;
4633
Joerg Roedel00a77de2015-03-26 13:43:08 +01004634 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004635}
Kay, Allen M38717942008-09-09 18:37:29 +03004636
Joerg Roedel00a77de2015-03-26 13:43:08 +01004637static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004638{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004639 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004640}
Kay, Allen M38717942008-09-09 18:37:29 +03004641
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004642static int intel_iommu_attach_device(struct iommu_domain *domain,
4643 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004644{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004645 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004646 struct intel_iommu *iommu;
4647 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004648 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004649
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004650 if (device_is_rmrr_locked(dev)) {
4651 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4652 return -EPERM;
4653 }
4654
David Woodhouse7207d8f2014-03-09 16:31:06 -07004655 /* normally dev is not mapped */
4656 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004657 struct dmar_domain *old_domain;
4658
David Woodhouse1525a292014-03-06 16:19:30 +00004659 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004660 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02004661 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02004662 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004663 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01004664
4665 if (!domain_type_is_vm_or_si(old_domain) &&
4666 list_empty(&old_domain->devices))
4667 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004668 }
4669 }
4670
David Woodhouse156baca2014-03-09 14:00:57 -07004671 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004672 if (!iommu)
4673 return -ENODEV;
4674
4675 /* check if this iommu agaw is sufficient for max mapped address */
4676 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004677 if (addr_width > cap_mgaw(iommu->cap))
4678 addr_width = cap_mgaw(iommu->cap);
4679
4680 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004681 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004682 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004683 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004684 return -EFAULT;
4685 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004686 dmar_domain->gaw = addr_width;
4687
4688 /*
4689 * Knock out extra levels of page tables if necessary
4690 */
4691 while (iommu->agaw < dmar_domain->agaw) {
4692 struct dma_pte *pte;
4693
4694 pte = dmar_domain->pgd;
4695 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004696 dmar_domain->pgd = (struct dma_pte *)
4697 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004698 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004699 }
4700 dmar_domain->agaw--;
4701 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004702
Joerg Roedel28ccce02015-07-21 14:45:31 +02004703 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004704}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004705
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004706static void intel_iommu_detach_device(struct iommu_domain *domain,
4707 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004708{
Joerg Roedele6de0f82015-07-22 16:30:36 +02004709 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004710}
Kay, Allen M38717942008-09-09 18:37:29 +03004711
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004712static int intel_iommu_map(struct iommu_domain *domain,
4713 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004714 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004715{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004716 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004717 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004718 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004719 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004720
Joerg Roedeldde57a22008-12-03 15:04:09 +01004721 if (iommu_prot & IOMMU_READ)
4722 prot |= DMA_PTE_READ;
4723 if (iommu_prot & IOMMU_WRITE)
4724 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08004725 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4726 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004727
David Woodhouse163cc522009-06-28 00:51:17 +01004728 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004729 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004730 u64 end;
4731
4732 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004733 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004734 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004735 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004736 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004737 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004738 return -EFAULT;
4739 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004740 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004741 }
David Woodhousead051222009-06-28 14:22:28 +01004742 /* Round up size to next multiple of PAGE_SIZE, if it and
4743 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004744 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004745 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4746 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004747 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004748}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004749
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004750static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004751 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004752{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004753 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004754 struct page *freelist = NULL;
4755 struct intel_iommu *iommu;
4756 unsigned long start_pfn, last_pfn;
4757 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02004758 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004759
David Woodhouse5cf0a762014-03-19 16:07:49 +00004760 /* Cope with horrid API which requires us to unmap more than the
4761 size argument if it happens to be a large-page mapping. */
4762 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4763 BUG();
4764
4765 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4766 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4767
David Woodhouseea8ea462014-03-05 17:09:32 +00004768 start_pfn = iova >> VTD_PAGE_SHIFT;
4769 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4770
4771 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4772
4773 npages = last_pfn - start_pfn + 1;
4774
Joerg Roedel29a27712015-07-21 17:17:12 +02004775 for_each_domain_iommu(iommu_id, dmar_domain) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004776 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00004777
Joerg Roedel42e8c182015-07-21 15:50:02 +02004778 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
4779 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00004780 }
4781
4782 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004783
David Woodhouse163cc522009-06-28 00:51:17 +01004784 if (dmar_domain->max_addr == iova + size)
4785 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004786
David Woodhouse5cf0a762014-03-19 16:07:49 +00004787 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004788}
Kay, Allen M38717942008-09-09 18:37:29 +03004789
Joerg Roedeld14d6572008-12-03 15:06:57 +01004790static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05304791 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004792{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004793 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004794 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004795 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004796 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004797
David Woodhouse5cf0a762014-03-19 16:07:49 +00004798 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004799 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004800 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004801
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004802 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004803}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004804
Joerg Roedel5d587b82014-09-05 10:50:45 +02004805static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004806{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004807 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004808 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004809 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004810 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004811
Joerg Roedel5d587b82014-09-05 10:50:45 +02004812 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004813}
4814
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004815static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004816{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004817 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004818 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004819 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004820
Alex Williamsona5459cf2014-06-12 16:12:31 -06004821 iommu = device_to_iommu(dev, &bus, &devfn);
4822 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004823 return -ENODEV;
4824
Alex Williamsona5459cf2014-06-12 16:12:31 -06004825 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004826
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004827 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004828
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004829 if (IS_ERR(group))
4830 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004831
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004832 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004833 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004834}
4835
4836static void intel_iommu_remove_device(struct device *dev)
4837{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004838 struct intel_iommu *iommu;
4839 u8 bus, devfn;
4840
4841 iommu = device_to_iommu(dev, &bus, &devfn);
4842 if (!iommu)
4843 return;
4844
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004845 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004846
4847 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004848}
4849
Thierry Redingb22f6432014-06-27 09:03:12 +02004850static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004851 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004852 .domain_alloc = intel_iommu_domain_alloc,
4853 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004854 .attach_dev = intel_iommu_attach_device,
4855 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004856 .map = intel_iommu_map,
4857 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004858 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004859 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004860 .add_device = intel_iommu_add_device,
4861 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004862 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004863};
David Woodhouse9af88142009-02-13 23:18:03 +00004864
Daniel Vetter94526182013-01-20 23:50:13 +01004865static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4866{
4867 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004868 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004869 dmar_map_gfx = 0;
4870}
4871
4872DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4873DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4874DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4875DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4876DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4877DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4878DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4879
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004880static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004881{
4882 /*
4883 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004884 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004885 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004886 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00004887 rwbf_quirk = 1;
4888}
4889
4890DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004891DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4892DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4893DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4894DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4895DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4896DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004897
Adam Jacksoneecfd572010-08-25 21:17:34 +01004898#define GGC 0x52
4899#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4900#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4901#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4902#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4903#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4904#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4905#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4906#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4907
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004908static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004909{
4910 unsigned short ggc;
4911
Adam Jacksoneecfd572010-08-25 21:17:34 +01004912 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004913 return;
4914
Adam Jacksoneecfd572010-08-25 21:17:34 +01004915 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004916 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01004917 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004918 } else if (dmar_map_gfx) {
4919 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004920 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004921 intel_iommu_strict = 1;
4922 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01004923}
4924DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
4925DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
4926DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
4927DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
4928
David Woodhousee0fc7e02009-09-30 09:12:17 -07004929/* On Tylersburg chipsets, some BIOSes have been known to enable the
4930 ISOCH DMAR unit for the Azalia sound device, but not give it any
4931 TLB entries, which causes it to deadlock. Check for that. We do
4932 this in a function called from init_dmars(), instead of in a PCI
4933 quirk, because we don't want to print the obnoxious "BIOS broken"
4934 message if VT-d is actually disabled.
4935*/
4936static void __init check_tylersburg_isoch(void)
4937{
4938 struct pci_dev *pdev;
4939 uint32_t vtisochctrl;
4940
4941 /* If there's no Azalia in the system anyway, forget it. */
4942 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
4943 if (!pdev)
4944 return;
4945 pci_dev_put(pdev);
4946
4947 /* System Management Registers. Might be hidden, in which case
4948 we can't do the sanity check. But that's OK, because the
4949 known-broken BIOSes _don't_ actually hide it, so far. */
4950 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
4951 if (!pdev)
4952 return;
4953
4954 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
4955 pci_dev_put(pdev);
4956 return;
4957 }
4958
4959 pci_dev_put(pdev);
4960
4961 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
4962 if (vtisochctrl & 1)
4963 return;
4964
4965 /* Drop all bits other than the number of TLB entries */
4966 vtisochctrl &= 0x1c;
4967
4968 /* If we have the recommended number of TLB entries (16), fine. */
4969 if (vtisochctrl == 0x10)
4970 return;
4971
4972 /* Zero TLB entries? You get to ride the short bus to school. */
4973 if (!vtisochctrl) {
4974 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
4975 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
4976 dmi_get_system_info(DMI_BIOS_VENDOR),
4977 dmi_get_system_info(DMI_BIOS_VERSION),
4978 dmi_get_system_info(DMI_PRODUCT_VERSION));
4979 iommu_identity_mapping |= IDENTMAP_AZALIA;
4980 return;
4981 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004982
4983 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07004984 vtisochctrl);
4985}