blob: 60b66d27e655ec47a1f3ba484118a30a993faa73 [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 Yang9cf06692009-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 */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000391
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 McLoughlina647dacb2008-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 McLoughlina647dacb2008-11-20 15:49:48 +0000420 u8 devfn; /* PCI devfn number */
David Woodhouseb16d0cb2015-10-12 14:17:37 +0100421 u8 pasid_supported:3;
422 u8 pasid_enabled:1;
423 u8 pri_supported:1;
424 u8 pri_enabled:1;
425 u8 ats_supported:1;
426 u8 ats_enabled:1;
427 u8 ats_qdep;
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000428 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800429 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dacb2008-11-20 15:49:48 +0000430 struct dmar_domain *domain; /* pointer to domain */
431};
432
Jiang Liub94e4112014-02-19 14:07:25 +0800433struct dmar_rmrr_unit {
434 struct list_head list; /* list of rmrr units */
435 struct acpi_dmar_header *hdr; /* ACPI header */
436 u64 base_address; /* reserved base address*/
437 u64 end_address; /* reserved end address */
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};
441
442struct dmar_atsr_unit {
443 struct list_head list; /* list of ATSR units */
444 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000445 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800446 int devices_cnt; /* target device count */
447 u8 include_all:1; /* include all ports */
448};
449
450static LIST_HEAD(dmar_atsr_units);
451static LIST_HEAD(dmar_rmrr_units);
452
453#define for_each_rmrr_units(rmrr) \
454 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
455
mark gross5e0d2a62008-03-04 15:22:08 -0800456static void flush_unmaps_timeout(unsigned long data);
457
Jiang Liub707cb02014-01-06 14:18:26 +0800458static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800459
mark gross80b20dd2008-04-18 13:53:58 -0700460#define HIGH_WATER_MARK 250
461struct deferred_flush_tables {
462 int next;
463 struct iova *iova[HIGH_WATER_MARK];
464 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000465 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700466};
467
468static struct deferred_flush_tables *deferred_flush;
469
mark gross5e0d2a62008-03-04 15:22:08 -0800470/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800471static int g_num_of_iommus;
472
473static DEFINE_SPINLOCK(async_umap_flush_lock);
474static LIST_HEAD(unmaps_to_do);
475
476static int timer_on;
477static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800478
Jiang Liu92d03cc2014-02-19 14:07:28 +0800479static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700480static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200481static void dmar_remove_one_dev_info(struct dmar_domain *domain,
482 struct device *dev);
Joerg Roedel127c7612015-07-23 17:44:46 +0200483static void __dmar_remove_one_dev_info(struct device_domain_info *info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200484static void domain_context_clear(struct intel_iommu *iommu,
485 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800486static int domain_detach_iommu(struct dmar_domain *domain,
487 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700488
Suresh Siddhad3f13812011-08-23 17:05:25 -0700489#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800490int dmar_disabled = 0;
491#else
492int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700493#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800494
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200495int intel_iommu_enabled = 0;
496EXPORT_SYMBOL_GPL(intel_iommu_enabled);
497
David Woodhouse2d9e6672010-06-15 10:57:57 +0100498static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700499static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800500static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100501static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100502static int intel_iommu_ecs = 1;
David Woodhouseae853dd2015-09-09 11:58:59 +0100503static int intel_iommu_pasid28;
504static int iommu_identity_mapping;
505
506#define IDENTMAP_ALL 1
507#define IDENTMAP_GFX 2
508#define IDENTMAP_AZALIA 4
David Woodhousec83b2f22015-06-12 10:15:49 +0100509
510/* We only actually use ECS when PASID support (on the new bit 40)
511 * is also advertised. Some early implementations — the ones with
512 * PASID support on bit 28 — have issues even when we *only* use
513 * extended root/context tables. */
David Woodhouseae853dd2015-09-09 11:58:59 +0100514#define pasid_enabled(iommu) (ecap_pasid(iommu->ecap) || \
515 (intel_iommu_pasid28 && ecap_broken_pasid(iommu->ecap)))
David Woodhousec83b2f22015-06-12 10:15:49 +0100516#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
David Woodhouseae853dd2015-09-09 11:58:59 +0100517 pasid_enabled(iommu))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700518
David Woodhousec0771df2011-10-14 20:59:46 +0100519int intel_iommu_gfx_mapped;
520EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
521
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700522#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
523static DEFINE_SPINLOCK(device_domain_lock);
524static LIST_HEAD(device_domain_list);
525
Thierry Redingb22f6432014-06-27 09:03:12 +0200526static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100527
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200528static bool translation_pre_enabled(struct intel_iommu *iommu)
529{
530 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
531}
532
Joerg Roedel091d42e2015-06-12 11:56:10 +0200533static void clear_translation_pre_enabled(struct intel_iommu *iommu)
534{
535 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
536}
537
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200538static void init_translation_status(struct intel_iommu *iommu)
539{
540 u32 gsts;
541
542 gsts = readl(iommu->reg + DMAR_GSTS_REG);
543 if (gsts & DMA_GSTS_TES)
544 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
545}
546
Joerg Roedel00a77de2015-03-26 13:43:08 +0100547/* Convert generic 'struct iommu_domain to private struct dmar_domain */
548static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
549{
550 return container_of(dom, struct dmar_domain, domain);
551}
552
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700553static int __init intel_iommu_setup(char *str)
554{
555 if (!str)
556 return -EINVAL;
557 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800558 if (!strncmp(str, "on", 2)) {
559 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200560 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800561 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700562 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200563 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700564 } else if (!strncmp(str, "igfx_off", 8)) {
565 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200566 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700567 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200568 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700569 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800570 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200571 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800572 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100573 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200574 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100575 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100576 } else if (!strncmp(str, "ecs_off", 7)) {
577 printk(KERN_INFO
578 "Intel-IOMMU: disable extended context table support\n");
579 intel_iommu_ecs = 0;
David Woodhouseae853dd2015-09-09 11:58:59 +0100580 } else if (!strncmp(str, "pasid28", 7)) {
581 printk(KERN_INFO
582 "Intel-IOMMU: enable pre-production PASID support\n");
583 intel_iommu_pasid28 = 1;
584 iommu_identity_mapping |= IDENTMAP_GFX;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700585 }
586
587 str += strcspn(str, ",");
588 while (*str == ',')
589 str++;
590 }
591 return 0;
592}
593__setup("intel_iommu=", intel_iommu_setup);
594
595static struct kmem_cache *iommu_domain_cache;
596static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700597
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200598static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
599{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200600 struct dmar_domain **domains;
601 int idx = did >> 8;
602
603 domains = iommu->domains[idx];
604 if (!domains)
605 return NULL;
606
607 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200608}
609
610static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
611 struct dmar_domain *domain)
612{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200613 struct dmar_domain **domains;
614 int idx = did >> 8;
615
616 if (!iommu->domains[idx]) {
617 size_t size = 256 * sizeof(struct dmar_domain *);
618 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
619 }
620
621 domains = iommu->domains[idx];
622 if (WARN_ON(!domains))
623 return;
624 else
625 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200626}
627
Suresh Siddha4c923d42009-10-02 11:01:24 -0700628static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700629{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700630 struct page *page;
631 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700632
Suresh Siddha4c923d42009-10-02 11:01:24 -0700633 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
634 if (page)
635 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700636 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700637}
638
639static inline void free_pgtable_page(void *vaddr)
640{
641 free_page((unsigned long)vaddr);
642}
643
644static inline void *alloc_domain_mem(void)
645{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900646 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700647}
648
Kay, Allen M38717942008-09-09 18:37:29 +0300649static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700650{
651 kmem_cache_free(iommu_domain_cache, vaddr);
652}
653
654static inline void * alloc_devinfo_mem(void)
655{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900656 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700657}
658
659static inline void free_devinfo_mem(void *vaddr)
660{
661 kmem_cache_free(iommu_devinfo_cache, vaddr);
662}
663
Jiang Liuab8dfe22014-07-11 14:19:27 +0800664static inline int domain_type_is_vm(struct dmar_domain *domain)
665{
666 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
667}
668
Joerg Roedel28ccce02015-07-21 14:45:31 +0200669static inline int domain_type_is_si(struct dmar_domain *domain)
670{
671 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
672}
673
Jiang Liuab8dfe22014-07-11 14:19:27 +0800674static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
675{
676 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
677 DOMAIN_FLAG_STATIC_IDENTITY);
678}
Weidong Han1b573682008-12-08 15:34:06 +0800679
Jiang Liu162d1b12014-07-11 14:19:35 +0800680static inline int domain_pfn_supported(struct dmar_domain *domain,
681 unsigned long pfn)
682{
683 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
684
685 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
686}
687
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700688static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800689{
690 unsigned long sagaw;
691 int agaw = -1;
692
693 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700694 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800695 agaw >= 0; agaw--) {
696 if (test_bit(agaw, &sagaw))
697 break;
698 }
699
700 return agaw;
701}
702
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700703/*
704 * Calculate max SAGAW for each iommu.
705 */
706int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
707{
708 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
709}
710
711/*
712 * calculate agaw for each iommu.
713 * "SAGAW" may be different across iommus, use a default agaw, and
714 * get a supported less agaw for iommus that don't support the default agaw.
715 */
716int iommu_calculate_agaw(struct intel_iommu *iommu)
717{
718 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
719}
720
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700721/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800722static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
723{
724 int iommu_id;
725
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700726 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800727 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200728 for_each_domain_iommu(iommu_id, domain)
729 break;
730
Weidong Han8c11e792008-12-08 15:29:22 +0800731 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
732 return NULL;
733
734 return g_iommus[iommu_id];
735}
736
Weidong Han8e6040972008-12-08 15:49:06 +0800737static void domain_update_iommu_coherency(struct dmar_domain *domain)
738{
David Woodhoused0501962014-03-11 17:10:29 -0700739 struct dmar_drhd_unit *drhd;
740 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100741 bool found = false;
742 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800743
David Woodhoused0501962014-03-11 17:10:29 -0700744 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800745
Joerg Roedel29a27712015-07-21 17:17:12 +0200746 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100747 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800748 if (!ecap_coherent(g_iommus[i]->ecap)) {
749 domain->iommu_coherency = 0;
750 break;
751 }
Weidong Han8e6040972008-12-08 15:49:06 +0800752 }
David Woodhoused0501962014-03-11 17:10:29 -0700753 if (found)
754 return;
755
756 /* No hardware attached; use lowest common denominator */
757 rcu_read_lock();
758 for_each_active_iommu(iommu, drhd) {
759 if (!ecap_coherent(iommu->ecap)) {
760 domain->iommu_coherency = 0;
761 break;
762 }
763 }
764 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800765}
766
Jiang Liu161f6932014-07-11 14:19:37 +0800767static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100768{
Allen Kay8140a952011-10-14 12:32:17 -0700769 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800770 struct intel_iommu *iommu;
771 int ret = 1;
772
773 rcu_read_lock();
774 for_each_active_iommu(iommu, drhd) {
775 if (iommu != skip) {
776 if (!ecap_sc_support(iommu->ecap)) {
777 ret = 0;
778 break;
779 }
780 }
781 }
782 rcu_read_unlock();
783
784 return ret;
785}
786
787static int domain_update_iommu_superpage(struct intel_iommu *skip)
788{
789 struct dmar_drhd_unit *drhd;
790 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700791 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100792
793 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800794 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100795 }
796
Allen Kay8140a952011-10-14 12:32:17 -0700797 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800798 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700799 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800800 if (iommu != skip) {
801 mask &= cap_super_page_val(iommu->cap);
802 if (!mask)
803 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100804 }
805 }
Jiang Liu0e242612014-02-19 14:07:34 +0800806 rcu_read_unlock();
807
Jiang Liu161f6932014-07-11 14:19:37 +0800808 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100809}
810
Sheng Yang58c610b2009-03-18 15:33:05 +0800811/* Some capabilities may be different across iommus */
812static void domain_update_iommu_cap(struct dmar_domain *domain)
813{
814 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800815 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
816 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800817}
818
David Woodhouse03ecc322015-02-13 14:35:21 +0000819static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
820 u8 bus, u8 devfn, int alloc)
821{
822 struct root_entry *root = &iommu->root_entry[bus];
823 struct context_entry *context;
824 u64 *entry;
825
Joerg Roedel4df4eab2015-08-25 10:54:28 +0200826 entry = &root->lo;
David Woodhousec83b2f22015-06-12 10:15:49 +0100827 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000828 if (devfn >= 0x80) {
829 devfn -= 0x80;
830 entry = &root->hi;
831 }
832 devfn *= 2;
833 }
David Woodhouse03ecc322015-02-13 14:35:21 +0000834 if (*entry & 1)
835 context = phys_to_virt(*entry & VTD_PAGE_MASK);
836 else {
837 unsigned long phy_addr;
838 if (!alloc)
839 return NULL;
840
841 context = alloc_pgtable_page(iommu->node);
842 if (!context)
843 return NULL;
844
845 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
846 phy_addr = virt_to_phys((void *)context);
847 *entry = phy_addr | 1;
848 __iommu_flush_cache(iommu, entry, sizeof(*entry));
849 }
850 return &context[devfn];
851}
852
David Woodhouse4ed6a542015-05-11 14:59:20 +0100853static int iommu_dummy(struct device *dev)
854{
855 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
856}
857
David Woodhouse156baca2014-03-09 14:00:57 -0700858static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800859{
860 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800861 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700862 struct device *tmp;
863 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800864 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800865 int i;
866
David Woodhouse4ed6a542015-05-11 14:59:20 +0100867 if (iommu_dummy(dev))
868 return NULL;
869
David Woodhouse156baca2014-03-09 14:00:57 -0700870 if (dev_is_pci(dev)) {
871 pdev = to_pci_dev(dev);
872 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100873 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700874 dev = &ACPI_COMPANION(dev)->dev;
875
Jiang Liu0e242612014-02-19 14:07:34 +0800876 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800877 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700878 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100879 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800880
Jiang Liub683b232014-02-19 14:07:32 +0800881 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700882 drhd->devices_cnt, i, tmp) {
883 if (tmp == dev) {
884 *bus = drhd->devices[i].bus;
885 *devfn = drhd->devices[i].devfn;
886 goto out;
887 }
888
889 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000890 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700891
892 ptmp = to_pci_dev(tmp);
893 if (ptmp->subordinate &&
894 ptmp->subordinate->number <= pdev->bus->number &&
895 ptmp->subordinate->busn_res.end >= pdev->bus->number)
896 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100897 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800898
David Woodhouse156baca2014-03-09 14:00:57 -0700899 if (pdev && drhd->include_all) {
900 got_pdev:
901 *bus = pdev->bus->number;
902 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800903 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700904 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800905 }
Jiang Liub683b232014-02-19 14:07:32 +0800906 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700907 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800908 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800909
Jiang Liub683b232014-02-19 14:07:32 +0800910 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800911}
912
Weidong Han5331fe62008-12-08 23:00:00 +0800913static void domain_flush_cache(struct dmar_domain *domain,
914 void *addr, int size)
915{
916 if (!domain->iommu_coherency)
917 clflush_cache_range(addr, size);
918}
919
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700920static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
921{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700922 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000923 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700924 unsigned long flags;
925
926 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000927 context = iommu_context_addr(iommu, bus, devfn, 0);
928 if (context)
929 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700930 spin_unlock_irqrestore(&iommu->lock, flags);
931 return ret;
932}
933
934static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
935{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700936 struct context_entry *context;
937 unsigned long flags;
938
939 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000940 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700941 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000942 context_clear_entry(context);
943 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700944 }
945 spin_unlock_irqrestore(&iommu->lock, flags);
946}
947
948static void free_context_table(struct intel_iommu *iommu)
949{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700950 int i;
951 unsigned long flags;
952 struct context_entry *context;
953
954 spin_lock_irqsave(&iommu->lock, flags);
955 if (!iommu->root_entry) {
956 goto out;
957 }
958 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000959 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700960 if (context)
961 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000962
David Woodhousec83b2f22015-06-12 10:15:49 +0100963 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000964 continue;
965
966 context = iommu_context_addr(iommu, i, 0x80, 0);
967 if (context)
968 free_pgtable_page(context);
969
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700970 }
971 free_pgtable_page(iommu->root_entry);
972 iommu->root_entry = NULL;
973out:
974 spin_unlock_irqrestore(&iommu->lock, flags);
975}
976
David Woodhouseb026fd22009-06-28 10:37:25 +0100977static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000978 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700979{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700980 struct dma_pte *parent, *pte = NULL;
981 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700982 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700983
984 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200985
Jiang Liu162d1b12014-07-11 14:19:35 +0800986 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200987 /* Address beyond IOMMU's addressing capabilities. */
988 return NULL;
989
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700990 parent = domain->pgd;
991
David Woodhouse5cf0a762014-03-19 16:07:49 +0000992 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700993 void *tmp_page;
994
David Woodhouseb026fd22009-06-28 10:37:25 +0100995 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700996 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000997 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100998 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000999 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001000 break;
1001
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001002 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +01001003 uint64_t pteval;
1004
Suresh Siddha4c923d42009-10-02 11:01:24 -07001005 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001006
David Woodhouse206a73c2009-07-01 19:30:28 +01001007 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001008 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +01001009
David Woodhousec85994e2009-07-01 19:21:24 +01001010 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -04001011 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 +08001012 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +01001013 /* Someone else set it while we were thinking; use theirs. */
1014 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +08001015 else
David Woodhousec85994e2009-07-01 19:21:24 +01001016 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001017 }
David Woodhouse5cf0a762014-03-19 16:07:49 +00001018 if (level == 1)
1019 break;
1020
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001021 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001022 level--;
1023 }
1024
David Woodhouse5cf0a762014-03-19 16:07:49 +00001025 if (!*target_level)
1026 *target_level = level;
1027
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001028 return pte;
1029}
1030
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001031
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001032/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001033static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1034 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001035 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001036{
1037 struct dma_pte *parent, *pte = NULL;
1038 int total = agaw_to_level(domain->agaw);
1039 int offset;
1040
1041 parent = domain->pgd;
1042 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001043 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001044 pte = &parent[offset];
1045 if (level == total)
1046 return pte;
1047
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001048 if (!dma_pte_present(pte)) {
1049 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001050 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001051 }
1052
Yijing Wange16922a2014-05-20 20:37:51 +08001053 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001054 *large_page = total;
1055 return pte;
1056 }
1057
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001058 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001059 total--;
1060 }
1061 return NULL;
1062}
1063
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001064/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001065static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf52009-06-27 22:09:11 +01001066 unsigned long start_pfn,
1067 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001068{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001069 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001070 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001071
Jiang Liu162d1b12014-07-11 14:19:35 +08001072 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1073 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001074 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001075
David Woodhouse04b18e62009-06-27 19:15:01 +01001076 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001077 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001078 large_page = 1;
1079 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001080 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001081 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001082 continue;
1083 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001084 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001085 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001086 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001087 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001088 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1089
David Woodhouse310a5ab2009-06-28 18:52:20 +01001090 domain_flush_cache(domain, first_pte,
1091 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001092
1093 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001094}
1095
Alex Williamson3269ee02013-06-15 10:27:19 -06001096static void dma_pte_free_level(struct dmar_domain *domain, int level,
1097 struct dma_pte *pte, unsigned long pfn,
1098 unsigned long start_pfn, unsigned long last_pfn)
1099{
1100 pfn = max(start_pfn, pfn);
1101 pte = &pte[pfn_level_offset(pfn, level)];
1102
1103 do {
1104 unsigned long level_pfn;
1105 struct dma_pte *level_pte;
1106
1107 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1108 goto next;
1109
1110 level_pfn = pfn & level_mask(level - 1);
1111 level_pte = phys_to_virt(dma_pte_addr(pte));
1112
1113 if (level > 2)
1114 dma_pte_free_level(domain, level - 1, level_pte,
1115 level_pfn, start_pfn, last_pfn);
1116
1117 /* If range covers entire pagetable, free it */
1118 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001119 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001120 dma_clear_pte(pte);
1121 domain_flush_cache(domain, pte, sizeof(*pte));
1122 free_pgtable_page(level_pte);
1123 }
1124next:
1125 pfn += level_size(level);
1126 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1127}
1128
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001129/* free page table pages. last level pte should already be cleared */
1130static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001131 unsigned long start_pfn,
1132 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001133{
Jiang Liu162d1b12014-07-11 14:19:35 +08001134 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1135 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001136 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001137
Jiang Liud41a4ad2014-07-11 14:19:34 +08001138 dma_pte_clear_range(domain, start_pfn, last_pfn);
1139
David Woodhousef3a0a522009-06-30 03:40:07 +01001140 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001141 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1142 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001143
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001144 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001145 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001146 free_pgtable_page(domain->pgd);
1147 domain->pgd = NULL;
1148 }
1149}
1150
David Woodhouseea8ea462014-03-05 17:09:32 +00001151/* When a page at a given level is being unlinked from its parent, we don't
1152 need to *modify* it at all. All we need to do is make a list of all the
1153 pages which can be freed just as soon as we've flushed the IOTLB and we
1154 know the hardware page-walk will no longer touch them.
1155 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1156 be freed. */
1157static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1158 int level, struct dma_pte *pte,
1159 struct page *freelist)
1160{
1161 struct page *pg;
1162
1163 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1164 pg->freelist = freelist;
1165 freelist = pg;
1166
1167 if (level == 1)
1168 return freelist;
1169
Jiang Liuadeb2592014-04-09 10:20:39 +08001170 pte = page_address(pg);
1171 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001172 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1173 freelist = dma_pte_list_pagetables(domain, level - 1,
1174 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001175 pte++;
1176 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001177
1178 return freelist;
1179}
1180
1181static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1182 struct dma_pte *pte, unsigned long pfn,
1183 unsigned long start_pfn,
1184 unsigned long last_pfn,
1185 struct page *freelist)
1186{
1187 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1188
1189 pfn = max(start_pfn, pfn);
1190 pte = &pte[pfn_level_offset(pfn, level)];
1191
1192 do {
1193 unsigned long level_pfn;
1194
1195 if (!dma_pte_present(pte))
1196 goto next;
1197
1198 level_pfn = pfn & level_mask(level);
1199
1200 /* If range covers entire pagetable, free it */
1201 if (start_pfn <= level_pfn &&
1202 last_pfn >= level_pfn + level_size(level) - 1) {
1203 /* These suborbinate page tables are going away entirely. Don't
1204 bother to clear them; we're just going to *free* them. */
1205 if (level > 1 && !dma_pte_superpage(pte))
1206 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1207
1208 dma_clear_pte(pte);
1209 if (!first_pte)
1210 first_pte = pte;
1211 last_pte = pte;
1212 } else if (level > 1) {
1213 /* Recurse down into a level that isn't *entirely* obsolete */
1214 freelist = dma_pte_clear_level(domain, level - 1,
1215 phys_to_virt(dma_pte_addr(pte)),
1216 level_pfn, start_pfn, last_pfn,
1217 freelist);
1218 }
1219next:
1220 pfn += level_size(level);
1221 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1222
1223 if (first_pte)
1224 domain_flush_cache(domain, first_pte,
1225 (void *)++last_pte - (void *)first_pte);
1226
1227 return freelist;
1228}
1229
1230/* We can't just free the pages because the IOMMU may still be walking
1231 the page tables, and may have cached the intermediate levels. The
1232 pages can only be freed after the IOTLB flush has been done. */
Joerg Roedelb6904202015-08-13 11:32:18 +02001233static struct page *domain_unmap(struct dmar_domain *domain,
1234 unsigned long start_pfn,
1235 unsigned long last_pfn)
David Woodhouseea8ea462014-03-05 17:09:32 +00001236{
David Woodhouseea8ea462014-03-05 17:09:32 +00001237 struct page *freelist = NULL;
1238
Jiang Liu162d1b12014-07-11 14:19:35 +08001239 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1240 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001241 BUG_ON(start_pfn > last_pfn);
1242
1243 /* we don't need lock here; nobody else touches the iova range */
1244 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1245 domain->pgd, 0, start_pfn, last_pfn, NULL);
1246
1247 /* free pgd */
1248 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1249 struct page *pgd_page = virt_to_page(domain->pgd);
1250 pgd_page->freelist = freelist;
1251 freelist = pgd_page;
1252
1253 domain->pgd = NULL;
1254 }
1255
1256 return freelist;
1257}
1258
Joerg Roedelb6904202015-08-13 11:32:18 +02001259static void dma_free_pagelist(struct page *freelist)
David Woodhouseea8ea462014-03-05 17:09:32 +00001260{
1261 struct page *pg;
1262
1263 while ((pg = freelist)) {
1264 freelist = pg->freelist;
1265 free_pgtable_page(page_address(pg));
1266 }
1267}
1268
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001269/* iommu handling */
1270static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1271{
1272 struct root_entry *root;
1273 unsigned long flags;
1274
Suresh Siddha4c923d42009-10-02 11:01:24 -07001275 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001276 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001277 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001278 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001279 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001280 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001281
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001282 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001283
1284 spin_lock_irqsave(&iommu->lock, flags);
1285 iommu->root_entry = root;
1286 spin_unlock_irqrestore(&iommu->lock, flags);
1287
1288 return 0;
1289}
1290
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001291static void iommu_set_root_entry(struct intel_iommu *iommu)
1292{
David Woodhouse03ecc322015-02-13 14:35:21 +00001293 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001294 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001295 unsigned long flag;
1296
David Woodhouse03ecc322015-02-13 14:35:21 +00001297 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001298 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001299 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001300
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001301 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001302 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001303
David Woodhousec416daa2009-05-10 20:30:58 +01001304 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001305
1306 /* Make sure hardware complete it */
1307 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001308 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001309
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001310 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001311}
1312
1313static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1314{
1315 u32 val;
1316 unsigned long flag;
1317
David Woodhouse9af88142009-02-13 23:18:03 +00001318 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001319 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001320
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001321 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001322 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001323
1324 /* Make sure hardware complete it */
1325 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001326 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001327
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001328 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001329}
1330
1331/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001332static void __iommu_flush_context(struct intel_iommu *iommu,
1333 u16 did, u16 source_id, u8 function_mask,
1334 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001335{
1336 u64 val = 0;
1337 unsigned long flag;
1338
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001339 switch (type) {
1340 case DMA_CCMD_GLOBAL_INVL:
1341 val = DMA_CCMD_GLOBAL_INVL;
1342 break;
1343 case DMA_CCMD_DOMAIN_INVL:
1344 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1345 break;
1346 case DMA_CCMD_DEVICE_INVL:
1347 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1348 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1349 break;
1350 default:
1351 BUG();
1352 }
1353 val |= DMA_CCMD_ICC;
1354
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001355 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001356 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1357
1358 /* Make sure hardware complete it */
1359 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1360 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1361
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001362 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001363}
1364
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001365/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001366static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1367 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001368{
1369 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1370 u64 val = 0, val_iva = 0;
1371 unsigned long flag;
1372
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001373 switch (type) {
1374 case DMA_TLB_GLOBAL_FLUSH:
1375 /* global flush doesn't need set IVA_REG */
1376 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1377 break;
1378 case DMA_TLB_DSI_FLUSH:
1379 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1380 break;
1381 case DMA_TLB_PSI_FLUSH:
1382 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001383 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001384 val_iva = size_order | addr;
1385 break;
1386 default:
1387 BUG();
1388 }
1389 /* Note: set drain read/write */
1390#if 0
1391 /*
1392 * This is probably to be super secure.. Looks like we can
1393 * ignore it without any impact.
1394 */
1395 if (cap_read_drain(iommu->cap))
1396 val |= DMA_TLB_READ_DRAIN;
1397#endif
1398 if (cap_write_drain(iommu->cap))
1399 val |= DMA_TLB_WRITE_DRAIN;
1400
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001401 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001402 /* Note: Only uses first TLB reg currently */
1403 if (val_iva)
1404 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1405 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1406
1407 /* Make sure hardware complete it */
1408 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1409 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1410
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001411 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001412
1413 /* check IOTLB invalidation granularity */
1414 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001415 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001416 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001417 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001418 (unsigned long long)DMA_TLB_IIRG(type),
1419 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001420}
1421
David Woodhouse64ae8922014-03-09 12:52:30 -07001422static struct device_domain_info *
1423iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1424 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001425{
Yu Zhao93a23a72009-05-18 13:51:37 +08001426 struct device_domain_info *info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001427
Joerg Roedel55d94042015-07-22 16:50:40 +02001428 assert_spin_locked(&device_domain_lock);
1429
Yu Zhao93a23a72009-05-18 13:51:37 +08001430 if (!iommu->qi)
1431 return NULL;
1432
Yu Zhao93a23a72009-05-18 13:51:37 +08001433 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001434 if (info->iommu == iommu && info->bus == bus &&
1435 info->devfn == devfn) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001436 if (info->ats_supported && info->dev)
1437 return info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001438 break;
1439 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001440
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001441 return NULL;
Yu Zhao93a23a72009-05-18 13:51:37 +08001442}
1443
1444static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1445{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001446 struct pci_dev *pdev;
1447
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001448 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001449 return;
1450
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001451 pdev = to_pci_dev(info->dev);
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001452
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001453#ifdef CONFIG_INTEL_IOMMU_SVM
1454 /* The PCIe spec, in its wisdom, declares that the behaviour of
1455 the device if you enable PASID support after ATS support is
1456 undefined. So always enable PASID support on devices which
1457 have it, even if we can't yet know if we're ever going to
1458 use it. */
1459 if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
1460 info->pasid_enabled = 1;
1461
1462 if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
1463 info->pri_enabled = 1;
1464#endif
1465 if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
1466 info->ats_enabled = 1;
1467 info->ats_qdep = pci_ats_queue_depth(pdev);
1468 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001469}
1470
1471static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1472{
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001473 struct pci_dev *pdev;
1474
1475 if (dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001476 return;
1477
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001478 pdev = to_pci_dev(info->dev);
1479
1480 if (info->ats_enabled) {
1481 pci_disable_ats(pdev);
1482 info->ats_enabled = 0;
1483 }
1484#ifdef CONFIG_INTEL_IOMMU_SVM
1485 if (info->pri_enabled) {
1486 pci_disable_pri(pdev);
1487 info->pri_enabled = 0;
1488 }
1489 if (info->pasid_enabled) {
1490 pci_disable_pasid(pdev);
1491 info->pasid_enabled = 0;
1492 }
1493#endif
Yu Zhao93a23a72009-05-18 13:51:37 +08001494}
1495
1496static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1497 u64 addr, unsigned mask)
1498{
1499 u16 sid, qdep;
1500 unsigned long flags;
1501 struct device_domain_info *info;
1502
1503 spin_lock_irqsave(&device_domain_lock, flags);
1504 list_for_each_entry(info, &domain->devices, link) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001505 if (!info->ats_enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001506 continue;
1507
1508 sid = info->bus << 8 | info->devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001509 qdep = info->ats_qdep;
Yu Zhao93a23a72009-05-18 13:51:37 +08001510 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1511 }
1512 spin_unlock_irqrestore(&device_domain_lock, flags);
1513}
1514
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001515static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1516 struct dmar_domain *domain,
1517 unsigned long pfn, unsigned int pages,
1518 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001519{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001520 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001521 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001522 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001523
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001524 BUG_ON(pages == 0);
1525
David Woodhouseea8ea462014-03-05 17:09:32 +00001526 if (ih)
1527 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001528 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001529 * Fallback to domain selective flush if no PSI support or the size is
1530 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001531 * PSI requires page size to be 2 ^ x, and the base address is naturally
1532 * aligned to the size
1533 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001534 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1535 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001536 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001537 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001538 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001539 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001540
1541 /*
Nadav Amit82653632010-04-01 13:24:40 +03001542 * In caching mode, changes of pages from non-present to present require
1543 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001544 */
Nadav Amit82653632010-04-01 13:24:40 +03001545 if (!cap_caching_mode(iommu->cap) || !map)
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001546 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1547 addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001548}
1549
mark grossf8bab732008-02-08 04:18:38 -08001550static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1551{
1552 u32 pmen;
1553 unsigned long flags;
1554
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001555 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001556 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1557 pmen &= ~DMA_PMEN_EPM;
1558 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1559
1560 /* wait for the protected region status bit to clear */
1561 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1562 readl, !(pmen & DMA_PMEN_PRS), pmen);
1563
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001564 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001565}
1566
Jiang Liu2a41cce2014-07-11 14:19:33 +08001567static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001568{
1569 u32 sts;
1570 unsigned long flags;
1571
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001572 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001573 iommu->gcmd |= DMA_GCMD_TE;
1574 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001575
1576 /* Make sure hardware complete it */
1577 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001578 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001579
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001580 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001581}
1582
Jiang Liu2a41cce2014-07-11 14:19:33 +08001583static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001584{
1585 u32 sts;
1586 unsigned long flag;
1587
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001588 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001589 iommu->gcmd &= ~DMA_GCMD_TE;
1590 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1591
1592 /* Make sure hardware complete it */
1593 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001594 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001595
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001596 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001597}
1598
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001599
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001600static int iommu_init_domains(struct intel_iommu *iommu)
1601{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001602 u32 ndomains, nlongs;
1603 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001604
1605 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001606 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001607 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001608 nlongs = BITS_TO_LONGS(ndomains);
1609
Donald Dutile94a91b502009-08-20 16:51:34 -04001610 spin_lock_init(&iommu->lock);
1611
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001612 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1613 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001614 pr_err("%s: Allocating domain id array failed\n",
1615 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001616 return -ENOMEM;
1617 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001618
1619 size = ((ndomains >> 8) + 1) * sizeof(struct dmar_domain **);
1620 iommu->domains = kzalloc(size, GFP_KERNEL);
1621
1622 if (iommu->domains) {
1623 size = 256 * sizeof(struct dmar_domain *);
1624 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1625 }
1626
1627 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001628 pr_err("%s: Allocating domain array failed\n",
1629 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001630 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001631 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001632 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001633 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001634 return -ENOMEM;
1635 }
1636
Joerg Roedel8bf47812015-07-21 10:41:21 +02001637
1638
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001639 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001640 * If Caching mode is set, then invalid translations are tagged
1641 * with domain-id 0, hence we need to pre-allocate it. We also
1642 * use domain-id 0 as a marker for non-allocated domain-id, so
1643 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001644 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001645 set_bit(0, iommu->domain_ids);
1646
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001647 return 0;
1648}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001649
Jiang Liuffebeb42014-11-09 22:48:02 +08001650static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001651{
Joerg Roedel29a27712015-07-21 17:17:12 +02001652 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001653 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001654
Joerg Roedel29a27712015-07-21 17:17:12 +02001655 if (!iommu->domains || !iommu->domain_ids)
1656 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001657
Joerg Roedel55d94042015-07-22 16:50:40 +02001658 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001659 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1660 struct dmar_domain *domain;
1661
1662 if (info->iommu != iommu)
1663 continue;
1664
1665 if (!info->dev || !info->domain)
1666 continue;
1667
1668 domain = info->domain;
1669
Joerg Roedele6de0f82015-07-22 16:30:36 +02001670 dmar_remove_one_dev_info(domain, info->dev);
Joerg Roedel29a27712015-07-21 17:17:12 +02001671
1672 if (!domain_type_is_vm_or_si(domain))
1673 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001674 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001675 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001676
1677 if (iommu->gcmd & DMA_GCMD_TE)
1678 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001679}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001680
Jiang Liuffebeb42014-11-09 22:48:02 +08001681static void free_dmar_iommu(struct intel_iommu *iommu)
1682{
1683 if ((iommu->domains) && (iommu->domain_ids)) {
Joerg Roedel8bf47812015-07-21 10:41:21 +02001684 int elems = (cap_ndoms(iommu->cap) >> 8) + 1;
1685 int i;
1686
1687 for (i = 0; i < elems; i++)
1688 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001689 kfree(iommu->domains);
1690 kfree(iommu->domain_ids);
1691 iommu->domains = NULL;
1692 iommu->domain_ids = NULL;
1693 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001694
Weidong Hand9630fe2008-12-08 11:06:32 +08001695 g_iommus[iommu->seq_id] = NULL;
1696
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001697 /* free context mapping */
1698 free_context_table(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001699
1700#ifdef CONFIG_INTEL_IOMMU_SVM
1701 if (pasid_enabled(iommu))
1702 intel_svm_free_pasid_tables(iommu);
1703#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001704}
1705
Jiang Liuab8dfe22014-07-11 14:19:27 +08001706static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001707{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001708 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001709
1710 domain = alloc_domain_mem();
1711 if (!domain)
1712 return NULL;
1713
Jiang Liuab8dfe22014-07-11 14:19:27 +08001714 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001715 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001716 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001717 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001718
1719 return domain;
1720}
1721
Joerg Roedeld160aca2015-07-22 11:52:53 +02001722/* Must be called with iommu->lock */
1723static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001724 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001725{
Jiang Liu44bde612014-07-11 14:19:29 +08001726 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001727 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001728
Joerg Roedel55d94042015-07-22 16:50:40 +02001729 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001730 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001731
Joerg Roedel29a27712015-07-21 17:17:12 +02001732 domain->iommu_refcnt[iommu->seq_id] += 1;
1733 domain->iommu_count += 1;
1734 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001735 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001736 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1737
1738 if (num >= ndomains) {
1739 pr_err("%s: No free domain ids\n", iommu->name);
1740 domain->iommu_refcnt[iommu->seq_id] -= 1;
1741 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001742 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001743 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001744
Joerg Roedeld160aca2015-07-22 11:52:53 +02001745 set_bit(num, iommu->domain_ids);
1746 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001747
Joerg Roedeld160aca2015-07-22 11:52:53 +02001748 domain->iommu_did[iommu->seq_id] = num;
1749 domain->nid = iommu->node;
1750
Jiang Liufb170fb2014-07-11 14:19:28 +08001751 domain_update_iommu_cap(domain);
1752 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001753
Joerg Roedel55d94042015-07-22 16:50:40 +02001754 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001755}
1756
1757static int domain_detach_iommu(struct dmar_domain *domain,
1758 struct intel_iommu *iommu)
1759{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001760 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001761
Joerg Roedel55d94042015-07-22 16:50:40 +02001762 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001763 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001764
Joerg Roedel29a27712015-07-21 17:17:12 +02001765 domain->iommu_refcnt[iommu->seq_id] -= 1;
1766 count = --domain->iommu_count;
1767 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001768 num = domain->iommu_did[iommu->seq_id];
1769 clear_bit(num, iommu->domain_ids);
1770 set_iommu_domain(iommu, num, NULL);
1771
Jiang Liufb170fb2014-07-11 14:19:28 +08001772 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001773 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001774 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001775
1776 return count;
1777}
1778
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001779static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001780static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001781
Joseph Cihula51a63e62011-03-21 11:04:24 -07001782static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001783{
1784 struct pci_dev *pdev = NULL;
1785 struct iova *iova;
1786 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001787
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001788 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1789 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001790
Mark Gross8a443df2008-03-04 14:59:31 -08001791 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1792 &reserved_rbtree_key);
1793
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001794 /* IOAPIC ranges shouldn't be accessed by DMA */
1795 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1796 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001797 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001798 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001799 return -ENODEV;
1800 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001801
1802 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1803 for_each_pci_dev(pdev) {
1804 struct resource *r;
1805
1806 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1807 r = &pdev->resource[i];
1808 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1809 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001810 iova = reserve_iova(&reserved_iova_list,
1811 IOVA_PFN(r->start),
1812 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001813 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001814 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001815 return -ENODEV;
1816 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001817 }
1818 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001819 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001820}
1821
1822static void domain_reserve_special_ranges(struct dmar_domain *domain)
1823{
1824 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1825}
1826
1827static inline int guestwidth_to_adjustwidth(int gaw)
1828{
1829 int agaw;
1830 int r = (gaw - 12) % 9;
1831
1832 if (r == 0)
1833 agaw = gaw;
1834 else
1835 agaw = gaw + 9 - r;
1836 if (agaw > 64)
1837 agaw = 64;
1838 return agaw;
1839}
1840
Joerg Roedeldc534b22015-07-22 12:44:02 +02001841static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1842 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001843{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001844 int adjust_width, agaw;
1845 unsigned long sagaw;
1846
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001847 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1848 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001849 domain_reserve_special_ranges(domain);
1850
1851 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001852 if (guest_width > cap_mgaw(iommu->cap))
1853 guest_width = cap_mgaw(iommu->cap);
1854 domain->gaw = guest_width;
1855 adjust_width = guestwidth_to_adjustwidth(guest_width);
1856 agaw = width_to_agaw(adjust_width);
1857 sagaw = cap_sagaw(iommu->cap);
1858 if (!test_bit(agaw, &sagaw)) {
1859 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001860 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001861 agaw = find_next_bit(&sagaw, 5, agaw);
1862 if (agaw >= 5)
1863 return -ENODEV;
1864 }
1865 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001866
Weidong Han8e6040972008-12-08 15:49:06 +08001867 if (ecap_coherent(iommu->ecap))
1868 domain->iommu_coherency = 1;
1869 else
1870 domain->iommu_coherency = 0;
1871
Sheng Yang58c610b2009-03-18 15:33:05 +08001872 if (ecap_sc_support(iommu->ecap))
1873 domain->iommu_snooping = 1;
1874 else
1875 domain->iommu_snooping = 0;
1876
David Woodhouse214e39a2014-03-19 10:38:49 +00001877 if (intel_iommu_superpage)
1878 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1879 else
1880 domain->iommu_superpage = 0;
1881
Suresh Siddha4c923d42009-10-02 11:01:24 -07001882 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001883
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001884 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001885 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001886 if (!domain->pgd)
1887 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001888 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001889 return 0;
1890}
1891
1892static void domain_exit(struct dmar_domain *domain)
1893{
David Woodhouseea8ea462014-03-05 17:09:32 +00001894 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001895
1896 /* Domain 0 is reserved, so dont process it */
1897 if (!domain)
1898 return;
1899
Alex Williamson7b668352011-05-24 12:02:41 +01001900 /* Flush any lazy unmaps that may reference this domain */
1901 if (!intel_iommu_strict)
1902 flush_unmaps_timeout(0);
1903
Joerg Roedeld160aca2015-07-22 11:52:53 +02001904 /* Remove associated devices and clear attached or cached domains */
1905 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001906 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001907 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001908
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001909 /* destroy iovas */
1910 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001911
David Woodhouseea8ea462014-03-05 17:09:32 +00001912 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001913
David Woodhouseea8ea462014-03-05 17:09:32 +00001914 dma_free_pagelist(freelist);
1915
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001916 free_domain_mem(domain);
1917}
1918
David Woodhouse64ae8922014-03-09 12:52:30 -07001919static int domain_context_mapping_one(struct dmar_domain *domain,
1920 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001921 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001922{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001923 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001924 int translation = CONTEXT_TT_MULTI_LEVEL;
1925 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001926 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001927 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001928 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02001929 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001930
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001931 WARN_ON(did == 0);
1932
Joerg Roedel28ccce02015-07-21 14:45:31 +02001933 if (hw_pass_through && domain_type_is_si(domain))
1934 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001935
1936 pr_debug("Set context mapping for %02x:%02x.%d\n",
1937 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001938
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001939 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001940
Joerg Roedel55d94042015-07-22 16:50:40 +02001941 spin_lock_irqsave(&device_domain_lock, flags);
1942 spin_lock(&iommu->lock);
1943
1944 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00001945 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001946 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02001947 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001948
Joerg Roedel55d94042015-07-22 16:50:40 +02001949 ret = 0;
1950 if (context_present(context))
1951 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02001952
Weidong Hanea6606b2008-12-08 23:08:15 +08001953 pgd = domain->pgd;
1954
Joerg Roedelde24e552015-07-21 14:53:04 +02001955 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001956 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08001957
Joerg Roedelde24e552015-07-21 14:53:04 +02001958 /*
1959 * Skip top levels of page tables for iommu which has less agaw
1960 * than default. Unnecessary for PT mode.
1961 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001962 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02001963 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02001964 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02001965 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02001966 if (!dma_pte_present(pgd))
1967 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02001968 }
1969
David Woodhouse64ae8922014-03-09 12:52:30 -07001970 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001971 if (info && info->ats_supported)
1972 translation = CONTEXT_TT_DEV_IOTLB;
1973 else
1974 translation = CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02001975
Yu Zhao93a23a72009-05-18 13:51:37 +08001976 context_set_address_root(context, virt_to_phys(pgd));
1977 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02001978 } else {
1979 /*
1980 * In pass through mode, AW must be programmed to
1981 * indicate the largest AGAW value supported by
1982 * hardware. And ASR is ignored by hardware.
1983 */
1984 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001985 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001986
1987 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001988 context_set_fault_enable(context);
1989 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001990 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001991
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001992 /*
1993 * It's a non-present to present mapping. If hardware doesn't cache
1994 * non-present entry we only need to flush the write-buffer. If the
1995 * _does_ cache non-present entries, then it does so in the special
1996 * domain #0, which we have to flush:
1997 */
1998 if (cap_caching_mode(iommu->cap)) {
1999 iommu->flush.flush_context(iommu, 0,
2000 (((u16)bus) << 8) | devfn,
2001 DMA_CCMD_MASK_NOBIT,
2002 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002003 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002004 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002005 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002006 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002007 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08002008
Joerg Roedel55d94042015-07-22 16:50:40 +02002009 ret = 0;
2010
2011out_unlock:
2012 spin_unlock(&iommu->lock);
2013 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08002014
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002015 return 0;
2016}
2017
Alex Williamson579305f2014-07-03 09:51:43 -06002018struct domain_context_mapping_data {
2019 struct dmar_domain *domain;
2020 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002021};
2022
2023static int domain_context_mapping_cb(struct pci_dev *pdev,
2024 u16 alias, void *opaque)
2025{
2026 struct domain_context_mapping_data *data = opaque;
2027
2028 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002029 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002030}
2031
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002032static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002033domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002034{
David Woodhouse64ae8922014-03-09 12:52:30 -07002035 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002036 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002037 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002038
David Woodhousee1f167f2014-03-09 15:24:46 -07002039 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002040 if (!iommu)
2041 return -ENODEV;
2042
Alex Williamson579305f2014-07-03 09:51:43 -06002043 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002044 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002045
2046 data.domain = domain;
2047 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002048
2049 return pci_for_each_dma_alias(to_pci_dev(dev),
2050 &domain_context_mapping_cb, &data);
2051}
2052
2053static int domain_context_mapped_cb(struct pci_dev *pdev,
2054 u16 alias, void *opaque)
2055{
2056 struct intel_iommu *iommu = opaque;
2057
2058 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002059}
2060
David Woodhousee1f167f2014-03-09 15:24:46 -07002061static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002062{
Weidong Han5331fe62008-12-08 23:00:00 +08002063 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002064 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002065
David Woodhousee1f167f2014-03-09 15:24:46 -07002066 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002067 if (!iommu)
2068 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002069
Alex Williamson579305f2014-07-03 09:51:43 -06002070 if (!dev_is_pci(dev))
2071 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002072
Alex Williamson579305f2014-07-03 09:51:43 -06002073 return !pci_for_each_dma_alias(to_pci_dev(dev),
2074 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002075}
2076
Fenghua Yuf5329592009-08-04 15:09:37 -07002077/* Returns a number of VTD pages, but aligned to MM page size */
2078static inline unsigned long aligned_nrpages(unsigned long host_addr,
2079 size_t size)
2080{
2081 host_addr &= ~PAGE_MASK;
2082 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2083}
2084
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002085/* Return largest possible superpage level for a given mapping */
2086static inline int hardware_largepage_caps(struct dmar_domain *domain,
2087 unsigned long iov_pfn,
2088 unsigned long phy_pfn,
2089 unsigned long pages)
2090{
2091 int support, level = 1;
2092 unsigned long pfnmerge;
2093
2094 support = domain->iommu_superpage;
2095
2096 /* To use a large page, the virtual *and* physical addresses
2097 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2098 of them will mean we have to use smaller pages. So just
2099 merge them and check both at once. */
2100 pfnmerge = iov_pfn | phy_pfn;
2101
2102 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2103 pages >>= VTD_STRIDE_SHIFT;
2104 if (!pages)
2105 break;
2106 pfnmerge >>= VTD_STRIDE_SHIFT;
2107 level++;
2108 support--;
2109 }
2110 return level;
2111}
2112
David Woodhouse9051aa02009-06-29 12:30:54 +01002113static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2114 struct scatterlist *sg, unsigned long phys_pfn,
2115 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002116{
2117 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002118 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002119 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002120 unsigned int largepage_lvl = 0;
2121 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002122
Jiang Liu162d1b12014-07-11 14:19:35 +08002123 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002124
2125 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2126 return -EINVAL;
2127
2128 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2129
Jiang Liucc4f14a2014-11-26 09:42:10 +08002130 if (!sg) {
2131 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002132 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2133 }
2134
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002135 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002136 uint64_t tmp;
2137
David Woodhousee1605492009-06-29 11:17:38 +01002138 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002139 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002140 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2141 sg->dma_length = sg->length;
Dan Williamsdb0fa0c2015-08-17 08:13:26 -06002142 pteval = (sg_phys(sg) & PAGE_MASK) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002143 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002144 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002145
David Woodhousee1605492009-06-29 11:17:38 +01002146 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002147 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2148
David Woodhouse5cf0a762014-03-19 16:07:49 +00002149 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002150 if (!pte)
2151 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002152 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002153 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002154 unsigned long nr_superpages, end_pfn;
2155
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002156 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002157 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002158
2159 nr_superpages = sg_res / lvl_pages;
2160 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2161
Jiang Liud41a4ad2014-07-11 14:19:34 +08002162 /*
2163 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002164 * removed to make room for superpage(s).
Jiang Liud41a4ad2014-07-11 14:19:34 +08002165 */
Christian Zanderba2374f2015-06-10 09:41:45 -07002166 dma_pte_free_pagetable(domain, iov_pfn, end_pfn);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002167 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002168 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002169 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002170
David Woodhousee1605492009-06-29 11:17:38 +01002171 }
2172 /* We don't need lock here, nobody else
2173 * touches the iova range
2174 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002175 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002176 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002177 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002178 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2179 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002180 if (dumps) {
2181 dumps--;
2182 debug_dma_dump_mappings(NULL);
2183 }
2184 WARN_ON(1);
2185 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002186
2187 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2188
2189 BUG_ON(nr_pages < lvl_pages);
2190 BUG_ON(sg_res < lvl_pages);
2191
2192 nr_pages -= lvl_pages;
2193 iov_pfn += lvl_pages;
2194 phys_pfn += lvl_pages;
2195 pteval += lvl_pages * VTD_PAGE_SIZE;
2196 sg_res -= lvl_pages;
2197
2198 /* If the next PTE would be the first in a new page, then we
2199 need to flush the cache on the entries we've just written.
2200 And then we'll need to recalculate 'pte', so clear it and
2201 let it get set again in the if (!pte) block above.
2202
2203 If we're done (!nr_pages) we need to flush the cache too.
2204
2205 Also if we've been setting superpages, we may need to
2206 recalculate 'pte' and switch back to smaller pages for the
2207 end of the mapping, if the trailing size is not enough to
2208 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002209 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002210 if (!nr_pages || first_pte_in_page(pte) ||
2211 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002212 domain_flush_cache(domain, first_pte,
2213 (void *)pte - (void *)first_pte);
2214 pte = NULL;
2215 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002216
2217 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002218 sg = sg_next(sg);
2219 }
2220 return 0;
2221}
2222
David Woodhouse9051aa02009-06-29 12:30:54 +01002223static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2224 struct scatterlist *sg, unsigned long nr_pages,
2225 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002226{
David Woodhouse9051aa02009-06-29 12:30:54 +01002227 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2228}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002229
David Woodhouse9051aa02009-06-29 12:30:54 +01002230static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2231 unsigned long phys_pfn, unsigned long nr_pages,
2232 int prot)
2233{
2234 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002235}
2236
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002237static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002238{
Weidong Hanc7151a82008-12-08 22:51:37 +08002239 if (!iommu)
2240 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002241
2242 clear_context_table(iommu, bus, devfn);
2243 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002244 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002245 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002246}
2247
David Woodhouse109b9b02012-05-25 17:43:02 +01002248static inline void unlink_domain_info(struct device_domain_info *info)
2249{
2250 assert_spin_locked(&device_domain_lock);
2251 list_del(&info->link);
2252 list_del(&info->global);
2253 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002254 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002255}
2256
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002257static void domain_remove_dev_info(struct dmar_domain *domain)
2258{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002259 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002260 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002261
2262 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002263 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002264 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002265 spin_unlock_irqrestore(&device_domain_lock, flags);
2266}
2267
2268/*
2269 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002270 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002271 */
David Woodhouse1525a292014-03-06 16:19:30 +00002272static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002273{
2274 struct device_domain_info *info;
2275
2276 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002277 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002278 if (info)
2279 return info->domain;
2280 return NULL;
2281}
2282
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002283static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002284dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2285{
2286 struct device_domain_info *info;
2287
2288 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002289 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002290 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002291 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002292
2293 return NULL;
2294}
2295
Joerg Roedel5db31562015-07-22 12:40:43 +02002296static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2297 int bus, int devfn,
2298 struct device *dev,
2299 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002300{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002301 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002302 struct device_domain_info *info;
2303 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002304 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002305
2306 info = alloc_devinfo_mem();
2307 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002308 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002309
Jiang Liu745f2582014-02-19 14:07:26 +08002310 info->bus = bus;
2311 info->devfn = devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002312 info->ats_supported = info->pasid_supported = info->pri_supported = 0;
2313 info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
2314 info->ats_qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002315 info->dev = dev;
2316 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002317 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002318
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002319 if (dev && dev_is_pci(dev)) {
2320 struct pci_dev *pdev = to_pci_dev(info->dev);
2321
2322 if (ecap_dev_iotlb_support(iommu->ecap) &&
2323 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
2324 dmar_find_matched_atsr_unit(pdev))
2325 info->ats_supported = 1;
2326
2327 if (ecs_enabled(iommu)) {
2328 if (pasid_enabled(iommu)) {
2329 int features = pci_pasid_features(pdev);
2330 if (features >= 0)
2331 info->pasid_supported = features | 1;
2332 }
2333
2334 if (info->ats_supported && ecap_prs(iommu->ecap) &&
2335 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
2336 info->pri_supported = 1;
2337 }
2338 }
2339
Jiang Liu745f2582014-02-19 14:07:26 +08002340 spin_lock_irqsave(&device_domain_lock, flags);
2341 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002342 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002343
2344 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002345 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002346 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002347 if (info2) {
2348 found = info2->domain;
2349 info2->dev = dev;
2350 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002351 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002352
Jiang Liu745f2582014-02-19 14:07:26 +08002353 if (found) {
2354 spin_unlock_irqrestore(&device_domain_lock, flags);
2355 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002356 /* Caller must free the original domain */
2357 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002358 }
2359
Joerg Roedeld160aca2015-07-22 11:52:53 +02002360 spin_lock(&iommu->lock);
2361 ret = domain_attach_iommu(domain, iommu);
2362 spin_unlock(&iommu->lock);
2363
2364 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002365 spin_unlock_irqrestore(&device_domain_lock, flags);
2366 return NULL;
2367 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002368
David Woodhouseb718cd32014-03-09 13:11:33 -07002369 list_add(&info->link, &domain->devices);
2370 list_add(&info->global, &device_domain_list);
2371 if (dev)
2372 dev->archdata.iommu = info;
2373 spin_unlock_irqrestore(&device_domain_lock, flags);
2374
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002375 if (dev && domain_context_mapping(domain, dev)) {
2376 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002377 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002378 return NULL;
2379 }
2380
David Woodhouseb718cd32014-03-09 13:11:33 -07002381 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002382}
2383
Alex Williamson579305f2014-07-03 09:51:43 -06002384static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2385{
2386 *(u16 *)opaque = alias;
2387 return 0;
2388}
2389
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002390/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002391static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002392{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002393 struct device_domain_info *info = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002394 struct dmar_domain *domain, *tmp;
2395 struct intel_iommu *iommu;
Joerg Roedel08a7f452015-07-23 18:09:11 +02002396 u16 req_id, dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002397 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002398 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002399
David Woodhouse146922e2014-03-09 15:44:17 -07002400 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002401 if (domain)
2402 return domain;
2403
David Woodhouse146922e2014-03-09 15:44:17 -07002404 iommu = device_to_iommu(dev, &bus, &devfn);
2405 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002406 return NULL;
2407
Joerg Roedel08a7f452015-07-23 18:09:11 +02002408 req_id = ((u16)bus << 8) | devfn;
2409
Alex Williamson579305f2014-07-03 09:51:43 -06002410 if (dev_is_pci(dev)) {
2411 struct pci_dev *pdev = to_pci_dev(dev);
2412
2413 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2414
2415 spin_lock_irqsave(&device_domain_lock, flags);
2416 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2417 PCI_BUS_NUM(dma_alias),
2418 dma_alias & 0xff);
2419 if (info) {
2420 iommu = info->iommu;
2421 domain = info->domain;
2422 }
2423 spin_unlock_irqrestore(&device_domain_lock, flags);
2424
2425 /* DMA alias already has a domain, uses it */
2426 if (info)
2427 goto found_domain;
2428 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002429
David Woodhouse146922e2014-03-09 15:44:17 -07002430 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002431 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002432 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002433 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002434 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002435 domain_exit(domain);
2436 return NULL;
2437 }
2438
2439 /* register PCI DMA alias device */
Joerg Roedel08a7f452015-07-23 18:09:11 +02002440 if (req_id != dma_alias && dev_is_pci(dev)) {
Joerg Roedel5db31562015-07-22 12:40:43 +02002441 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2442 dma_alias & 0xff, NULL, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002443
2444 if (!tmp || tmp != domain) {
2445 domain_exit(domain);
2446 domain = tmp;
2447 }
2448
David Woodhouseb718cd32014-03-09 13:11:33 -07002449 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002450 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002451 }
2452
2453found_domain:
Joerg Roedel5db31562015-07-22 12:40:43 +02002454 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002455
2456 if (!tmp || tmp != domain) {
2457 domain_exit(domain);
2458 domain = tmp;
2459 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002460
2461 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002462}
2463
David Woodhouseb2132032009-06-26 18:50:28 +01002464static int iommu_domain_identity_map(struct dmar_domain *domain,
2465 unsigned long long start,
2466 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002467{
David Woodhousec5395d52009-06-28 16:35:56 +01002468 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2469 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002470
David Woodhousec5395d52009-06-28 16:35:56 +01002471 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2472 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002473 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002474 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002475 }
2476
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002477 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002478 /*
2479 * RMRR range might have overlap with physical memory range,
2480 * clear it first
2481 */
David Woodhousec5395d52009-06-28 16:35:56 +01002482 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002483
David Woodhousec5395d52009-06-28 16:35:56 +01002484 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2485 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002486 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002487}
2488
David Woodhouse0b9d9752014-03-09 15:48:15 -07002489static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002490 unsigned long long start,
2491 unsigned long long end)
2492{
2493 struct dmar_domain *domain;
2494 int ret;
2495
David Woodhouse0b9d9752014-03-09 15:48:15 -07002496 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002497 if (!domain)
2498 return -ENOMEM;
2499
David Woodhouse19943b02009-08-04 16:19:20 +01002500 /* For _hardware_ passthrough, don't bother. But for software
2501 passthrough, we do it anyway -- it may indicate a memory
2502 range which is reserved in E820, so which didn't get set
2503 up to start with in si_domain */
2504 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002505 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2506 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002507 return 0;
2508 }
2509
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002510 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2511 dev_name(dev), start, end);
2512
David Woodhouse5595b522009-12-02 09:21:55 +00002513 if (end < start) {
2514 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2515 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2516 dmi_get_system_info(DMI_BIOS_VENDOR),
2517 dmi_get_system_info(DMI_BIOS_VERSION),
2518 dmi_get_system_info(DMI_PRODUCT_VERSION));
2519 ret = -EIO;
2520 goto error;
2521 }
2522
David Woodhouse2ff729f2009-08-26 14:25:41 +01002523 if (end >> agaw_to_width(domain->agaw)) {
2524 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2525 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2526 agaw_to_width(domain->agaw),
2527 dmi_get_system_info(DMI_BIOS_VENDOR),
2528 dmi_get_system_info(DMI_BIOS_VERSION),
2529 dmi_get_system_info(DMI_PRODUCT_VERSION));
2530 ret = -EIO;
2531 goto error;
2532 }
David Woodhouse19943b02009-08-04 16:19:20 +01002533
David Woodhouseb2132032009-06-26 18:50:28 +01002534 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002535 if (ret)
2536 goto error;
2537
David Woodhouseb2132032009-06-26 18:50:28 +01002538 return 0;
2539
2540 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002541 domain_exit(domain);
2542 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002543}
2544
2545static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002546 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002547{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002548 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002549 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002550 return iommu_prepare_identity_map(dev, rmrr->base_address,
2551 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002552}
2553
Suresh Siddhad3f13812011-08-23 17:05:25 -07002554#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002555static inline void iommu_prepare_isa(void)
2556{
2557 struct pci_dev *pdev;
2558 int ret;
2559
2560 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2561 if (!pdev)
2562 return;
2563
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002564 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002565 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002566
2567 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002568 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002569
Yijing Wang9b27e822014-05-20 20:37:52 +08002570 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002571}
2572#else
2573static inline void iommu_prepare_isa(void)
2574{
2575 return;
2576}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002577#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002578
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002579static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002580
Matt Kraai071e1372009-08-23 22:30:22 -07002581static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002582{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002583 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002584
Jiang Liuab8dfe22014-07-11 14:19:27 +08002585 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002586 if (!si_domain)
2587 return -EFAULT;
2588
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002589 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2590 domain_exit(si_domain);
2591 return -EFAULT;
2592 }
2593
Joerg Roedel0dc79712015-07-21 15:40:06 +02002594 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002595
David Woodhouse19943b02009-08-04 16:19:20 +01002596 if (hw)
2597 return 0;
2598
David Woodhousec7ab48d2009-06-26 19:10:36 +01002599 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002600 unsigned long start_pfn, end_pfn;
2601 int i;
2602
2603 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2604 ret = iommu_domain_identity_map(si_domain,
2605 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2606 if (ret)
2607 return ret;
2608 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002609 }
2610
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002611 return 0;
2612}
2613
David Woodhouse9b226622014-03-09 14:03:28 -07002614static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002615{
2616 struct device_domain_info *info;
2617
2618 if (likely(!iommu_identity_mapping))
2619 return 0;
2620
David Woodhouse9b226622014-03-09 14:03:28 -07002621 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002622 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2623 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002624
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002625 return 0;
2626}
2627
Joerg Roedel28ccce02015-07-21 14:45:31 +02002628static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002629{
David Woodhouse0ac72662014-03-09 13:19:22 -07002630 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002631 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002632 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002633
David Woodhouse5913c9b2014-03-09 16:27:31 -07002634 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002635 if (!iommu)
2636 return -ENODEV;
2637
Joerg Roedel5db31562015-07-22 12:40:43 +02002638 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002639 if (ndomain != domain)
2640 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002641
2642 return 0;
2643}
2644
David Woodhouse0b9d9752014-03-09 15:48:15 -07002645static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002646{
2647 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002648 struct device *tmp;
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002649 int i;
2650
Jiang Liu0e242612014-02-19 14:07:34 +08002651 rcu_read_lock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002652 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002653 /*
2654 * Return TRUE if this RMRR contains the device that
2655 * is passed in.
2656 */
2657 for_each_active_dev_scope(rmrr->devices,
2658 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002659 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002660 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002661 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002662 }
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002663 }
Jiang Liu0e242612014-02-19 14:07:34 +08002664 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002665 return false;
2666}
2667
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002668/*
2669 * There are a couple cases where we need to restrict the functionality of
2670 * devices associated with RMRRs. The first is when evaluating a device for
2671 * identity mapping because problems exist when devices are moved in and out
2672 * of domains and their respective RMRR information is lost. This means that
2673 * a device with associated RMRRs will never be in a "passthrough" domain.
2674 * The second is use of the device through the IOMMU API. This interface
2675 * expects to have full control of the IOVA space for the device. We cannot
2676 * satisfy both the requirement that RMRR access is maintained and have an
2677 * unencumbered IOVA space. We also have no ability to quiesce the device's
2678 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2679 * We therefore prevent devices associated with an RMRR from participating in
2680 * the IOMMU API, which eliminates them from device assignment.
2681 *
2682 * In both cases we assume that PCI USB devices with RMRRs have them largely
2683 * for historical reasons and that the RMRR space is not actively used post
2684 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002685 *
2686 * The same exception is made for graphics devices, with the requirement that
2687 * any use of the RMRR regions will be torn down before assigning the device
2688 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002689 */
2690static bool device_is_rmrr_locked(struct device *dev)
2691{
2692 if (!device_has_rmrr(dev))
2693 return false;
2694
2695 if (dev_is_pci(dev)) {
2696 struct pci_dev *pdev = to_pci_dev(dev);
2697
David Woodhouse18436af2015-03-25 15:05:47 +00002698 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002699 return false;
2700 }
2701
2702 return true;
2703}
2704
David Woodhouse3bdb2592014-03-09 16:03:08 -07002705static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002706{
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002707
David Woodhouse3bdb2592014-03-09 16:03:08 -07002708 if (dev_is_pci(dev)) {
2709 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002710
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002711 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002712 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002713
David Woodhouse3bdb2592014-03-09 16:03:08 -07002714 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2715 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002716
David Woodhouse3bdb2592014-03-09 16:03:08 -07002717 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2718 return 1;
2719
2720 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2721 return 0;
2722
2723 /*
2724 * We want to start off with all devices in the 1:1 domain, and
2725 * take them out later if we find they can't access all of memory.
2726 *
2727 * However, we can't do this for PCI devices behind bridges,
2728 * because all PCI devices behind the same bridge will end up
2729 * with the same source-id on their transactions.
2730 *
2731 * Practically speaking, we can't change things around for these
2732 * devices at run-time, because we can't be sure there'll be no
2733 * DMA transactions in flight for any of their siblings.
2734 *
2735 * So PCI devices (unless they're on the root bus) as well as
2736 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2737 * the 1:1 domain, just in _case_ one of their siblings turns out
2738 * not to be able to map all of memory.
2739 */
2740 if (!pci_is_pcie(pdev)) {
2741 if (!pci_is_root_bus(pdev->bus))
2742 return 0;
2743 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2744 return 0;
2745 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2746 return 0;
2747 } else {
2748 if (device_has_rmrr(dev))
2749 return 0;
2750 }
David Woodhouse6941af22009-07-04 18:24:27 +01002751
David Woodhouse3dfc8132009-07-04 19:11:08 +01002752 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002753 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002754 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002755 * take them out of the 1:1 domain later.
2756 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002757 if (!startup) {
2758 /*
2759 * If the device's dma_mask is less than the system's memory
2760 * size then this is not a candidate for identity mapping.
2761 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002762 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002763
David Woodhouse3bdb2592014-03-09 16:03:08 -07002764 if (dev->coherent_dma_mask &&
2765 dev->coherent_dma_mask < dma_mask)
2766 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002767
David Woodhouse3bdb2592014-03-09 16:03:08 -07002768 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002769 }
David Woodhouse6941af22009-07-04 18:24:27 +01002770
2771 return 1;
2772}
2773
David Woodhousecf04eee2014-03-21 16:49:04 +00002774static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2775{
2776 int ret;
2777
2778 if (!iommu_should_identity_map(dev, 1))
2779 return 0;
2780
Joerg Roedel28ccce02015-07-21 14:45:31 +02002781 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002782 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002783 pr_info("%s identity mapping for device %s\n",
2784 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002785 else if (ret == -ENODEV)
2786 /* device not associated with an iommu */
2787 ret = 0;
2788
2789 return ret;
2790}
2791
2792
Matt Kraai071e1372009-08-23 22:30:22 -07002793static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002794{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002795 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002796 struct dmar_drhd_unit *drhd;
2797 struct intel_iommu *iommu;
2798 struct device *dev;
2799 int i;
2800 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002801
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002802 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002803 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2804 if (ret)
2805 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002806 }
2807
David Woodhousecf04eee2014-03-21 16:49:04 +00002808 for_each_active_iommu(iommu, drhd)
2809 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2810 struct acpi_device_physical_node *pn;
2811 struct acpi_device *adev;
2812
2813 if (dev->bus != &acpi_bus_type)
2814 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002815
David Woodhousecf04eee2014-03-21 16:49:04 +00002816 adev= to_acpi_device(dev);
2817 mutex_lock(&adev->physical_node_lock);
2818 list_for_each_entry(pn, &adev->physical_node_list, node) {
2819 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2820 if (ret)
2821 break;
2822 }
2823 mutex_unlock(&adev->physical_node_lock);
2824 if (ret)
2825 return ret;
2826 }
2827
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002828 return 0;
2829}
2830
Jiang Liuffebeb42014-11-09 22:48:02 +08002831static void intel_iommu_init_qi(struct intel_iommu *iommu)
2832{
2833 /*
2834 * Start from the sane iommu hardware state.
2835 * If the queued invalidation is already initialized by us
2836 * (for example, while enabling interrupt-remapping) then
2837 * we got the things already rolling from a sane state.
2838 */
2839 if (!iommu->qi) {
2840 /*
2841 * Clear any previous faults.
2842 */
2843 dmar_fault(-1, iommu);
2844 /*
2845 * Disable queued invalidation if supported and already enabled
2846 * before OS handover.
2847 */
2848 dmar_disable_qi(iommu);
2849 }
2850
2851 if (dmar_enable_qi(iommu)) {
2852 /*
2853 * Queued Invalidate not enabled, use Register Based Invalidate
2854 */
2855 iommu->flush.flush_context = __iommu_flush_context;
2856 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002857 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002858 iommu->name);
2859 } else {
2860 iommu->flush.flush_context = qi_flush_context;
2861 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002862 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002863 }
2864}
2865
Joerg Roedel091d42e2015-06-12 11:56:10 +02002866static int copy_context_table(struct intel_iommu *iommu,
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002867 struct root_entry __iomem *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02002868 struct context_entry **tbl,
2869 int bus, bool ext)
2870{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002871 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002872 struct context_entry __iomem *old_ce = NULL;
2873 struct context_entry *new_ce = NULL, ce;
2874 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002875 phys_addr_t old_ce_phys;
2876
2877 tbl_idx = ext ? bus * 2 : bus;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002878 memcpy_fromio(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002879
2880 for (devfn = 0; devfn < 256; devfn++) {
2881 /* First calculate the correct index */
2882 idx = (ext ? devfn * 2 : devfn) % 256;
2883
2884 if (idx == 0) {
2885 /* First save what we may have and clean up */
2886 if (new_ce) {
2887 tbl[tbl_idx] = new_ce;
2888 __iommu_flush_cache(iommu, new_ce,
2889 VTD_PAGE_SIZE);
2890 pos = 1;
2891 }
2892
2893 if (old_ce)
2894 iounmap(old_ce);
2895
2896 ret = 0;
2897 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002898 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02002899 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002900 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02002901
2902 if (!old_ce_phys) {
2903 if (ext && devfn == 0) {
2904 /* No LCTP, try UCTP */
2905 devfn = 0x7f;
2906 continue;
2907 } else {
2908 goto out;
2909 }
2910 }
2911
2912 ret = -ENOMEM;
2913 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2914 if (!old_ce)
2915 goto out;
2916
2917 new_ce = alloc_pgtable_page(iommu->node);
2918 if (!new_ce)
2919 goto out_unmap;
2920
2921 ret = 0;
2922 }
2923
2924 /* Now copy the context entry */
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002925 memcpy_fromio(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002926
Joerg Roedelcf484d02015-06-12 12:21:46 +02002927 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002928 continue;
2929
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002930 did = context_domain_id(&ce);
2931 if (did >= 0 && did < cap_ndoms(iommu->cap))
2932 set_bit(did, iommu->domain_ids);
2933
Joerg Roedelcf484d02015-06-12 12:21:46 +02002934 /*
2935 * We need a marker for copied context entries. This
2936 * marker needs to work for the old format as well as
2937 * for extended context entries.
2938 *
2939 * Bit 67 of the context entry is used. In the old
2940 * format this bit is available to software, in the
2941 * extended format it is the PGE bit, but PGE is ignored
2942 * by HW if PASIDs are disabled (and thus still
2943 * available).
2944 *
2945 * So disable PASIDs first and then mark the entry
2946 * copied. This means that we don't copy PASID
2947 * translations from the old kernel, but this is fine as
2948 * faults there are not fatal.
2949 */
2950 context_clear_pasid_enable(&ce);
2951 context_set_copied(&ce);
2952
Joerg Roedel091d42e2015-06-12 11:56:10 +02002953 new_ce[idx] = ce;
2954 }
2955
2956 tbl[tbl_idx + pos] = new_ce;
2957
2958 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2959
2960out_unmap:
2961 iounmap(old_ce);
2962
2963out:
2964 return ret;
2965}
2966
2967static int copy_translation_tables(struct intel_iommu *iommu)
2968{
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002969 struct root_entry __iomem *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002970 struct context_entry **ctxt_tbls;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002971 phys_addr_t old_rt_phys;
2972 int ctxt_table_entries;
2973 unsigned long flags;
2974 u64 rtaddr_reg;
2975 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002976 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002977
2978 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2979 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002980 new_ext = !!ecap_ecs(iommu->ecap);
2981
2982 /*
2983 * The RTT bit can only be changed when translation is disabled,
2984 * but disabling translation means to open a window for data
2985 * corruption. So bail out and don't copy anything if we would
2986 * have to change the bit.
2987 */
2988 if (new_ext != ext)
2989 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002990
2991 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2992 if (!old_rt_phys)
2993 return -EINVAL;
2994
2995 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2996 if (!old_rt)
2997 return -ENOMEM;
2998
2999 /* This is too big for the stack - allocate it from slab */
3000 ctxt_table_entries = ext ? 512 : 256;
3001 ret = -ENOMEM;
3002 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
3003 if (!ctxt_tbls)
3004 goto out_unmap;
3005
3006 for (bus = 0; bus < 256; bus++) {
3007 ret = copy_context_table(iommu, &old_rt[bus],
3008 ctxt_tbls, bus, ext);
3009 if (ret) {
3010 pr_err("%s: Failed to copy context table for bus %d\n",
3011 iommu->name, bus);
3012 continue;
3013 }
3014 }
3015
3016 spin_lock_irqsave(&iommu->lock, flags);
3017
3018 /* Context tables are copied, now write them to the root_entry table */
3019 for (bus = 0; bus < 256; bus++) {
3020 int idx = ext ? bus * 2 : bus;
3021 u64 val;
3022
3023 if (ctxt_tbls[idx]) {
3024 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3025 iommu->root_entry[bus].lo = val;
3026 }
3027
3028 if (!ext || !ctxt_tbls[idx + 1])
3029 continue;
3030
3031 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3032 iommu->root_entry[bus].hi = val;
3033 }
3034
3035 spin_unlock_irqrestore(&iommu->lock, flags);
3036
3037 kfree(ctxt_tbls);
3038
3039 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3040
3041 ret = 0;
3042
3043out_unmap:
3044 iounmap(old_rt);
3045
3046 return ret;
3047}
3048
Joseph Cihulab7792602011-05-03 00:08:37 -07003049static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003050{
3051 struct dmar_drhd_unit *drhd;
3052 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003053 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003054 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003055 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003056 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003057
3058 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003059 * for each drhd
3060 * allocate root
3061 * initialize and program root entry to not present
3062 * endfor
3063 */
3064 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003065 /*
3066 * lock not needed as this is only incremented in the single
3067 * threaded kernel __init code path all other access are read
3068 * only
3069 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003070 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003071 g_num_of_iommus++;
3072 continue;
3073 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003074 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003075 }
3076
Jiang Liuffebeb42014-11-09 22:48:02 +08003077 /* Preallocate enough resources for IOMMU hot-addition */
3078 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3079 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3080
Weidong Hand9630fe2008-12-08 11:06:32 +08003081 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3082 GFP_KERNEL);
3083 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003084 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003085 ret = -ENOMEM;
3086 goto error;
3087 }
3088
mark gross80b20dd2008-04-18 13:53:58 -07003089 deferred_flush = kzalloc(g_num_of_iommus *
3090 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3091 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003092 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003093 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003094 }
3095
Jiang Liu7c919772014-01-06 14:18:18 +08003096 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003097 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003098
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003099 intel_iommu_init_qi(iommu);
3100
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003101 ret = iommu_init_domains(iommu);
3102 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003103 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003104
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003105 init_translation_status(iommu);
3106
Joerg Roedel091d42e2015-06-12 11:56:10 +02003107 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3108 iommu_disable_translation(iommu);
3109 clear_translation_pre_enabled(iommu);
3110 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3111 iommu->name);
3112 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003113
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003114 /*
3115 * TBD:
3116 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003117 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003118 */
3119 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003120 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003121 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003122
Joerg Roedel091d42e2015-06-12 11:56:10 +02003123 if (translation_pre_enabled(iommu)) {
3124 pr_info("Translation already enabled - trying to copy translation structures\n");
3125
3126 ret = copy_translation_tables(iommu);
3127 if (ret) {
3128 /*
3129 * We found the IOMMU with translation
3130 * enabled - but failed to copy over the
3131 * old root-entry table. Try to proceed
3132 * by disabling translation now and
3133 * allocating a clean root-entry table.
3134 * This might cause DMAR faults, but
3135 * probably the dump will still succeed.
3136 */
3137 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3138 iommu->name);
3139 iommu_disable_translation(iommu);
3140 clear_translation_pre_enabled(iommu);
3141 } else {
3142 pr_info("Copied translation tables from previous kernel for %s\n",
3143 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003144 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003145 }
3146 }
3147
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003148 iommu_flush_write_buffer(iommu);
3149 iommu_set_root_entry(iommu);
3150 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3151 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3152
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003153 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003154 hw_pass_through = 0;
David Woodhouse8a94ade2015-03-24 14:54:56 +00003155#ifdef CONFIG_INTEL_IOMMU_SVM
3156 if (pasid_enabled(iommu))
3157 intel_svm_alloc_pasid_tables(iommu);
3158#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003159 }
3160
David Woodhouse19943b02009-08-04 16:19:20 +01003161 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003162 iommu_identity_mapping |= IDENTMAP_ALL;
3163
Suresh Siddhad3f13812011-08-23 17:05:25 -07003164#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003165 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003166#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003167
Joerg Roedel86080cc2015-06-12 12:27:16 +02003168 if (iommu_identity_mapping) {
3169 ret = si_domain_init(hw_pass_through);
3170 if (ret)
3171 goto free_iommu;
3172 }
3173
David Woodhousee0fc7e02009-09-30 09:12:17 -07003174 check_tylersburg_isoch();
3175
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003176 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003177 * If we copied translations from a previous kernel in the kdump
3178 * case, we can not assign the devices to domains now, as that
3179 * would eliminate the old mappings. So skip this part and defer
3180 * the assignment to device driver initialization time.
3181 */
3182 if (copied_tables)
3183 goto domains_done;
3184
3185 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003186 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003187 * identity mappings for rmrr, gfx, and isa and may fall back to static
3188 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003189 */
David Woodhouse19943b02009-08-04 16:19:20 +01003190 if (iommu_identity_mapping) {
3191 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3192 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003193 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003194 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003195 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003196 }
David Woodhouse19943b02009-08-04 16:19:20 +01003197 /*
3198 * For each rmrr
3199 * for each dev attached to rmrr
3200 * do
3201 * locate drhd for dev, alloc domain for dev
3202 * allocate free domain
3203 * allocate page table entries for rmrr
3204 * if context not allocated for bus
3205 * allocate and init context
3206 * set present in root table for this bus
3207 * init context with domain, translation etc
3208 * endfor
3209 * endfor
3210 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003211 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003212 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003213 /* some BIOS lists non-exist devices in DMAR table. */
3214 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003215 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003216 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003217 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003218 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003219 }
3220 }
3221
3222 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003223
Joerg Roedela87f4912015-06-12 12:32:54 +02003224domains_done:
3225
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003226 /*
3227 * for each drhd
3228 * enable fault log
3229 * global invalidate context cache
3230 * global invalidate iotlb
3231 * enable translation
3232 */
Jiang Liu7c919772014-01-06 14:18:18 +08003233 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003234 if (drhd->ignored) {
3235 /*
3236 * we always have to disable PMRs or DMA may fail on
3237 * this device
3238 */
3239 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003240 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003241 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003242 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003243
3244 iommu_flush_write_buffer(iommu);
3245
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003246 ret = dmar_set_interrupt(iommu);
3247 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003248 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003249
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003250 if (!translation_pre_enabled(iommu))
3251 iommu_enable_translation(iommu);
3252
David Woodhouseb94996c2009-09-19 15:28:12 -07003253 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003254 }
3255
3256 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003257
3258free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003259 for_each_active_iommu(iommu, drhd) {
3260 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003261 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003262 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003263 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003264free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003265 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003266error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003267 return ret;
3268}
3269
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003270/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003271static struct iova *intel_alloc_iova(struct device *dev,
3272 struct dmar_domain *domain,
3273 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003274{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003275 struct iova *iova = NULL;
3276
David Woodhouse875764d2009-06-28 21:20:51 +01003277 /* Restrict dma_mask to the width that the iommu can handle */
3278 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003279 /* Ensure we reserve the whole size-aligned region */
3280 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003281
3282 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003283 /*
3284 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003285 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003286 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003287 */
David Woodhouse875764d2009-06-28 21:20:51 +01003288 iova = alloc_iova(&domain->iovad, nrpages,
3289 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3290 if (iova)
3291 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003292 }
David Woodhouse875764d2009-06-28 21:20:51 +01003293 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3294 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003295 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003296 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003297 return NULL;
3298 }
3299
3300 return iova;
3301}
3302
David Woodhoused4b709f2014-03-09 16:07:40 -07003303static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003304{
3305 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003306
David Woodhoused4b709f2014-03-09 16:07:40 -07003307 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003308 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003309 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003310 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003311 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003312 }
3313
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003314 return domain;
3315}
3316
David Woodhoused4b709f2014-03-09 16:07:40 -07003317static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003318{
3319 struct device_domain_info *info;
3320
3321 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003322 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003323 if (likely(info))
3324 return info->domain;
3325
3326 return __get_valid_domain_for_dev(dev);
3327}
3328
David Woodhouseecb509e2014-03-09 16:29:55 -07003329/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003330static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003331{
3332 int found;
3333
David Woodhouse3d891942014-03-06 15:59:26 +00003334 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003335 return 1;
3336
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003337 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003338 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003339
David Woodhouse9b226622014-03-09 14:03:28 -07003340 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003341 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003342 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003343 return 1;
3344 else {
3345 /*
3346 * 32 bit DMA is removed from si_domain and fall back
3347 * to non-identity mapping.
3348 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003349 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003350 pr_info("32bit %s uses non-identity mapping\n",
3351 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003352 return 0;
3353 }
3354 } else {
3355 /*
3356 * In case of a detached 64 bit DMA device from vm, the device
3357 * is put into si_domain for identity mapping.
3358 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003359 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003360 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003361 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003362 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003363 pr_info("64bit %s uses identity mapping\n",
3364 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003365 return 1;
3366 }
3367 }
3368 }
3369
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003370 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003371}
3372
David Woodhouse5040a912014-03-09 16:14:00 -07003373static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003374 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003375{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003376 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003377 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003378 struct iova *iova;
3379 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003380 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003381 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003382 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003383
3384 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003385
David Woodhouse5040a912014-03-09 16:14:00 -07003386 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003387 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003388
David Woodhouse5040a912014-03-09 16:14:00 -07003389 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003390 if (!domain)
3391 return 0;
3392
Weidong Han8c11e792008-12-08 15:29:22 +08003393 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003394 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003395
David Woodhouse5040a912014-03-09 16:14:00 -07003396 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003397 if (!iova)
3398 goto error;
3399
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003400 /*
3401 * Check if DMAR supports zero-length reads on write only
3402 * mappings..
3403 */
3404 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003405 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003406 prot |= DMA_PTE_READ;
3407 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3408 prot |= DMA_PTE_WRITE;
3409 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003410 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003411 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003412 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003413 * is not a big problem
3414 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003415 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003416 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003417 if (ret)
3418 goto error;
3419
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003420 /* it's a non-present to present mapping. Only flush if caching mode */
3421 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003422 iommu_flush_iotlb_psi(iommu, domain,
3423 mm_to_dma_pfn(iova->pfn_lo),
3424 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003425 else
Weidong Han8c11e792008-12-08 15:29:22 +08003426 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003427
David Woodhouse03d6a242009-06-28 15:33:46 +01003428 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3429 start_paddr += paddr & ~PAGE_MASK;
3430 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003431
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003432error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003433 if (iova)
3434 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003435 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003436 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003437 return 0;
3438}
3439
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003440static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3441 unsigned long offset, size_t size,
3442 enum dma_data_direction dir,
3443 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003444{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003445 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003446 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003447}
3448
mark gross5e0d2a62008-03-04 15:22:08 -08003449static void flush_unmaps(void)
3450{
mark gross80b20dd2008-04-18 13:53:58 -07003451 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003452
mark gross5e0d2a62008-03-04 15:22:08 -08003453 timer_on = 0;
3454
3455 /* just flush them all */
3456 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003457 struct intel_iommu *iommu = g_iommus[i];
3458 if (!iommu)
3459 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003460
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003461 if (!deferred_flush[i].next)
3462 continue;
3463
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003464 /* In caching mode, global flushes turn emulation expensive */
3465 if (!cap_caching_mode(iommu->cap))
3466 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003467 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003468 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003469 unsigned long mask;
3470 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003471 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003472
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003473 /* On real hardware multiple invalidations are expensive */
3474 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003475 iommu_flush_iotlb_psi(iommu, domain,
Jiang Liua156ef92014-07-11 14:19:36 +08003476 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003477 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003478 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003479 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003480 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3481 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3482 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003483 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003484 if (deferred_flush[i].freelist[j])
3485 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003486 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003487 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003488 }
3489
mark gross5e0d2a62008-03-04 15:22:08 -08003490 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003491}
3492
3493static void flush_unmaps_timeout(unsigned long data)
3494{
mark gross80b20dd2008-04-18 13:53:58 -07003495 unsigned long flags;
3496
3497 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003498 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003499 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003500}
3501
David Woodhouseea8ea462014-03-05 17:09:32 +00003502static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003503{
3504 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003505 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003506 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003507
3508 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003509 if (list_size == HIGH_WATER_MARK)
3510 flush_unmaps();
3511
Weidong Han8c11e792008-12-08 15:29:22 +08003512 iommu = domain_get_iommu(dom);
3513 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003514
mark gross80b20dd2008-04-18 13:53:58 -07003515 next = deferred_flush[iommu_id].next;
3516 deferred_flush[iommu_id].domain[next] = dom;
3517 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003518 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003519 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003520
3521 if (!timer_on) {
3522 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3523 timer_on = 1;
3524 }
3525 list_size++;
3526 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3527}
3528
Jiang Liud41a4ad2014-07-11 14:19:34 +08003529static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003530{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003531 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003532 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003533 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003534 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003535 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003536
David Woodhouse73676832009-07-04 14:08:36 +01003537 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003538 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003539
David Woodhouse1525a292014-03-06 16:19:30 +00003540 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003541 BUG_ON(!domain);
3542
Weidong Han8c11e792008-12-08 15:29:22 +08003543 iommu = domain_get_iommu(domain);
3544
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003545 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003546 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3547 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003548 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003549
David Woodhoused794dc92009-06-28 00:27:49 +01003550 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3551 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003552
David Woodhoused794dc92009-06-28 00:27:49 +01003553 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003554 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003555
David Woodhouseea8ea462014-03-05 17:09:32 +00003556 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003557
mark gross5e0d2a62008-03-04 15:22:08 -08003558 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003559 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003560 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003561 /* free iova */
3562 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003563 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003564 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003565 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003566 /*
3567 * queue up the release of the unmap to save the 1/6th of the
3568 * cpu used up by the iotlb flush operation...
3569 */
mark gross5e0d2a62008-03-04 15:22:08 -08003570 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003571}
3572
Jiang Liud41a4ad2014-07-11 14:19:34 +08003573static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3574 size_t size, enum dma_data_direction dir,
3575 struct dma_attrs *attrs)
3576{
3577 intel_unmap(dev, dev_addr);
3578}
3579
David Woodhouse5040a912014-03-09 16:14:00 -07003580static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003581 dma_addr_t *dma_handle, gfp_t flags,
3582 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003583{
Akinobu Mita36746432014-06-04 16:06:51 -07003584 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003585 int order;
3586
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003587 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003588 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003589
David Woodhouse5040a912014-03-09 16:14:00 -07003590 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003591 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003592 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3593 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003594 flags |= GFP_DMA;
3595 else
3596 flags |= GFP_DMA32;
3597 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003598
Akinobu Mita36746432014-06-04 16:06:51 -07003599 if (flags & __GFP_WAIT) {
3600 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003601
Akinobu Mita36746432014-06-04 16:06:51 -07003602 page = dma_alloc_from_contiguous(dev, count, order);
3603 if (page && iommu_no_mapping(dev) &&
3604 page_to_phys(page) + size > dev->coherent_dma_mask) {
3605 dma_release_from_contiguous(dev, page, count);
3606 page = NULL;
3607 }
3608 }
3609
3610 if (!page)
3611 page = alloc_pages(flags, order);
3612 if (!page)
3613 return NULL;
3614 memset(page_address(page), 0, size);
3615
3616 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003617 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003618 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003619 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003620 return page_address(page);
3621 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3622 __free_pages(page, order);
3623
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003624 return NULL;
3625}
3626
David Woodhouse5040a912014-03-09 16:14:00 -07003627static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003628 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003629{
3630 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003631 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003632
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003633 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003634 order = get_order(size);
3635
Jiang Liud41a4ad2014-07-11 14:19:34 +08003636 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003637 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3638 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003639}
3640
David Woodhouse5040a912014-03-09 16:14:00 -07003641static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003642 int nelems, enum dma_data_direction dir,
3643 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003644{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003645 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003646}
3647
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003648static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003649 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003650{
3651 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003652 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003653
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003654 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003655 BUG_ON(!sg_page(sg));
Dan Williamsdb0fa0c2015-08-17 08:13:26 -06003656 sg->dma_address = sg_phys(sg);
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003657 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003658 }
3659 return nelems;
3660}
3661
David Woodhouse5040a912014-03-09 16:14:00 -07003662static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003663 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003664{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003665 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003666 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003667 size_t size = 0;
3668 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003669 struct iova *iova = NULL;
3670 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003671 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003672 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003673 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003674
3675 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003676 if (iommu_no_mapping(dev))
3677 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003678
David Woodhouse5040a912014-03-09 16:14:00 -07003679 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003680 if (!domain)
3681 return 0;
3682
Weidong Han8c11e792008-12-08 15:29:22 +08003683 iommu = domain_get_iommu(domain);
3684
David Woodhouseb536d242009-06-28 14:49:31 +01003685 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003686 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003687
David Woodhouse5040a912014-03-09 16:14:00 -07003688 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3689 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003690 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003691 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003692 return 0;
3693 }
3694
3695 /*
3696 * Check if DMAR supports zero-length reads on write only
3697 * mappings..
3698 */
3699 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003700 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003701 prot |= DMA_PTE_READ;
3702 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3703 prot |= DMA_PTE_WRITE;
3704
David Woodhouseb536d242009-06-28 14:49:31 +01003705 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003706
Fenghua Yuf5329592009-08-04 15:09:37 -07003707 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003708 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003709 dma_pte_free_pagetable(domain, start_vpfn,
3710 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003711 __free_iova(&domain->iovad, iova);
3712 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003713 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003714
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003715 /* it's a non-present to present mapping. Only flush if caching mode */
3716 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003717 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003718 else
Weidong Han8c11e792008-12-08 15:29:22 +08003719 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003720
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003721 return nelems;
3722}
3723
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003724static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3725{
3726 return !dma_addr;
3727}
3728
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003729struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003730 .alloc = intel_alloc_coherent,
3731 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003732 .map_sg = intel_map_sg,
3733 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003734 .map_page = intel_map_page,
3735 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003736 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003737};
3738
3739static inline int iommu_domain_cache_init(void)
3740{
3741 int ret = 0;
3742
3743 iommu_domain_cache = kmem_cache_create("iommu_domain",
3744 sizeof(struct dmar_domain),
3745 0,
3746 SLAB_HWCACHE_ALIGN,
3747
3748 NULL);
3749 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003750 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003751 ret = -ENOMEM;
3752 }
3753
3754 return ret;
3755}
3756
3757static inline int iommu_devinfo_cache_init(void)
3758{
3759 int ret = 0;
3760
3761 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3762 sizeof(struct device_domain_info),
3763 0,
3764 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003765 NULL);
3766 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003767 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003768 ret = -ENOMEM;
3769 }
3770
3771 return ret;
3772}
3773
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003774static int __init iommu_init_mempool(void)
3775{
3776 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003777 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003778 if (ret)
3779 return ret;
3780
3781 ret = iommu_domain_cache_init();
3782 if (ret)
3783 goto domain_error;
3784
3785 ret = iommu_devinfo_cache_init();
3786 if (!ret)
3787 return ret;
3788
3789 kmem_cache_destroy(iommu_domain_cache);
3790domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003791 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003792
3793 return -ENOMEM;
3794}
3795
3796static void __init iommu_exit_mempool(void)
3797{
3798 kmem_cache_destroy(iommu_devinfo_cache);
3799 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003800 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003801}
3802
Dan Williams556ab452010-07-23 15:47:56 -07003803static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3804{
3805 struct dmar_drhd_unit *drhd;
3806 u32 vtbar;
3807 int rc;
3808
3809 /* We know that this device on this chipset has its own IOMMU.
3810 * If we find it under a different IOMMU, then the BIOS is lying
3811 * to us. Hope that the IOMMU for this device is actually
3812 * disabled, and it needs no translation...
3813 */
3814 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3815 if (rc) {
3816 /* "can't" happen */
3817 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3818 return;
3819 }
3820 vtbar &= 0xffff0000;
3821
3822 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3823 drhd = dmar_find_matched_drhd_unit(pdev);
3824 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3825 TAINT_FIRMWARE_WORKAROUND,
3826 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3827 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3828}
3829DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3830
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003831static void __init init_no_remapping_devices(void)
3832{
3833 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003834 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003835 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003836
3837 for_each_drhd_unit(drhd) {
3838 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003839 for_each_active_dev_scope(drhd->devices,
3840 drhd->devices_cnt, i, dev)
3841 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003842 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003843 if (i == drhd->devices_cnt)
3844 drhd->ignored = 1;
3845 }
3846 }
3847
Jiang Liu7c919772014-01-06 14:18:18 +08003848 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003849 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003850 continue;
3851
Jiang Liub683b232014-02-19 14:07:32 +08003852 for_each_active_dev_scope(drhd->devices,
3853 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003854 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003855 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003856 if (i < drhd->devices_cnt)
3857 continue;
3858
David Woodhousec0771df2011-10-14 20:59:46 +01003859 /* This IOMMU has *only* gfx devices. Either bypass it or
3860 set the gfx_mapped flag, as appropriate */
3861 if (dmar_map_gfx) {
3862 intel_iommu_gfx_mapped = 1;
3863 } else {
3864 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003865 for_each_active_dev_scope(drhd->devices,
3866 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003867 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003868 }
3869 }
3870}
3871
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003872#ifdef CONFIG_SUSPEND
3873static int init_iommu_hw(void)
3874{
3875 struct dmar_drhd_unit *drhd;
3876 struct intel_iommu *iommu = NULL;
3877
3878 for_each_active_iommu(iommu, drhd)
3879 if (iommu->qi)
3880 dmar_reenable_qi(iommu);
3881
Joseph Cihulab7792602011-05-03 00:08:37 -07003882 for_each_iommu(iommu, drhd) {
3883 if (drhd->ignored) {
3884 /*
3885 * we always have to disable PMRs or DMA may fail on
3886 * this device
3887 */
3888 if (force_on)
3889 iommu_disable_protect_mem_regions(iommu);
3890 continue;
3891 }
3892
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003893 iommu_flush_write_buffer(iommu);
3894
3895 iommu_set_root_entry(iommu);
3896
3897 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003898 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003899 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3900 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003901 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003902 }
3903
3904 return 0;
3905}
3906
3907static void iommu_flush_all(void)
3908{
3909 struct dmar_drhd_unit *drhd;
3910 struct intel_iommu *iommu;
3911
3912 for_each_active_iommu(iommu, drhd) {
3913 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003914 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003915 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003916 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003917 }
3918}
3919
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003920static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003921{
3922 struct dmar_drhd_unit *drhd;
3923 struct intel_iommu *iommu = NULL;
3924 unsigned long flag;
3925
3926 for_each_active_iommu(iommu, drhd) {
3927 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3928 GFP_ATOMIC);
3929 if (!iommu->iommu_state)
3930 goto nomem;
3931 }
3932
3933 iommu_flush_all();
3934
3935 for_each_active_iommu(iommu, drhd) {
3936 iommu_disable_translation(iommu);
3937
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003938 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003939
3940 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3941 readl(iommu->reg + DMAR_FECTL_REG);
3942 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3943 readl(iommu->reg + DMAR_FEDATA_REG);
3944 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3945 readl(iommu->reg + DMAR_FEADDR_REG);
3946 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3947 readl(iommu->reg + DMAR_FEUADDR_REG);
3948
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003949 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003950 }
3951 return 0;
3952
3953nomem:
3954 for_each_active_iommu(iommu, drhd)
3955 kfree(iommu->iommu_state);
3956
3957 return -ENOMEM;
3958}
3959
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003960static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003961{
3962 struct dmar_drhd_unit *drhd;
3963 struct intel_iommu *iommu = NULL;
3964 unsigned long flag;
3965
3966 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003967 if (force_on)
3968 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3969 else
3970 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003971 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003972 }
3973
3974 for_each_active_iommu(iommu, drhd) {
3975
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003976 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003977
3978 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3979 iommu->reg + DMAR_FECTL_REG);
3980 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3981 iommu->reg + DMAR_FEDATA_REG);
3982 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3983 iommu->reg + DMAR_FEADDR_REG);
3984 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3985 iommu->reg + DMAR_FEUADDR_REG);
3986
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003987 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003988 }
3989
3990 for_each_active_iommu(iommu, drhd)
3991 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003992}
3993
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003994static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003995 .resume = iommu_resume,
3996 .suspend = iommu_suspend,
3997};
3998
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003999static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004000{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004001 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004002}
4003
4004#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004005static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004006#endif /* CONFIG_PM */
4007
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004008
Jiang Liuc2a0b532014-11-09 22:47:56 +08004009int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004010{
4011 struct acpi_dmar_reserved_memory *rmrr;
4012 struct dmar_rmrr_unit *rmrru;
4013
4014 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4015 if (!rmrru)
4016 return -ENOMEM;
4017
4018 rmrru->hdr = header;
4019 rmrr = (struct acpi_dmar_reserved_memory *)header;
4020 rmrru->base_address = rmrr->base_address;
4021 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08004022 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4023 ((void *)rmrr) + rmrr->header.length,
4024 &rmrru->devices_cnt);
4025 if (rmrru->devices_cnt && rmrru->devices == NULL) {
4026 kfree(rmrru);
4027 return -ENOMEM;
4028 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004029
Jiang Liu2e455282014-02-19 14:07:36 +08004030 list_add(&rmrru->list, &dmar_rmrr_units);
4031
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004032 return 0;
4033}
4034
Jiang Liu6b197242014-11-09 22:47:58 +08004035static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4036{
4037 struct dmar_atsr_unit *atsru;
4038 struct acpi_dmar_atsr *tmp;
4039
4040 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4041 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4042 if (atsr->segment != tmp->segment)
4043 continue;
4044 if (atsr->header.length != tmp->header.length)
4045 continue;
4046 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4047 return atsru;
4048 }
4049
4050 return NULL;
4051}
4052
4053int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004054{
4055 struct acpi_dmar_atsr *atsr;
4056 struct dmar_atsr_unit *atsru;
4057
Jiang Liu6b197242014-11-09 22:47:58 +08004058 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4059 return 0;
4060
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004061 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004062 atsru = dmar_find_atsr(atsr);
4063 if (atsru)
4064 return 0;
4065
4066 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004067 if (!atsru)
4068 return -ENOMEM;
4069
Jiang Liu6b197242014-11-09 22:47:58 +08004070 /*
4071 * If memory is allocated from slab by ACPI _DSM method, we need to
4072 * copy the memory content because the memory buffer will be freed
4073 * on return.
4074 */
4075 atsru->hdr = (void *)(atsru + 1);
4076 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004077 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004078 if (!atsru->include_all) {
4079 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4080 (void *)atsr + atsr->header.length,
4081 &atsru->devices_cnt);
4082 if (atsru->devices_cnt && atsru->devices == NULL) {
4083 kfree(atsru);
4084 return -ENOMEM;
4085 }
4086 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004087
Jiang Liu0e242612014-02-19 14:07:34 +08004088 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004089
4090 return 0;
4091}
4092
Jiang Liu9bdc5312014-01-06 14:18:27 +08004093static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4094{
4095 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4096 kfree(atsru);
4097}
4098
Jiang Liu6b197242014-11-09 22:47:58 +08004099int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4100{
4101 struct acpi_dmar_atsr *atsr;
4102 struct dmar_atsr_unit *atsru;
4103
4104 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4105 atsru = dmar_find_atsr(atsr);
4106 if (atsru) {
4107 list_del_rcu(&atsru->list);
4108 synchronize_rcu();
4109 intel_iommu_free_atsr(atsru);
4110 }
4111
4112 return 0;
4113}
4114
4115int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4116{
4117 int i;
4118 struct device *dev;
4119 struct acpi_dmar_atsr *atsr;
4120 struct dmar_atsr_unit *atsru;
4121
4122 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4123 atsru = dmar_find_atsr(atsr);
4124 if (!atsru)
4125 return 0;
4126
4127 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4128 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4129 i, dev)
4130 return -EBUSY;
4131
4132 return 0;
4133}
4134
Jiang Liuffebeb42014-11-09 22:48:02 +08004135static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4136{
4137 int sp, ret = 0;
4138 struct intel_iommu *iommu = dmaru->iommu;
4139
4140 if (g_iommus[iommu->seq_id])
4141 return 0;
4142
4143 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004144 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004145 iommu->name);
4146 return -ENXIO;
4147 }
4148 if (!ecap_sc_support(iommu->ecap) &&
4149 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004150 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004151 iommu->name);
4152 return -ENXIO;
4153 }
4154 sp = domain_update_iommu_superpage(iommu) - 1;
4155 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004156 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004157 iommu->name);
4158 return -ENXIO;
4159 }
4160
4161 /*
4162 * Disable translation if already enabled prior to OS handover.
4163 */
4164 if (iommu->gcmd & DMA_GCMD_TE)
4165 iommu_disable_translation(iommu);
4166
4167 g_iommus[iommu->seq_id] = iommu;
4168 ret = iommu_init_domains(iommu);
4169 if (ret == 0)
4170 ret = iommu_alloc_root_entry(iommu);
4171 if (ret)
4172 goto out;
4173
David Woodhouse8a94ade2015-03-24 14:54:56 +00004174#ifdef CONFIG_INTEL_IOMMU_SVM
4175 if (pasid_enabled(iommu))
4176 intel_svm_alloc_pasid_tables(iommu);
4177#endif
4178
Jiang Liuffebeb42014-11-09 22:48:02 +08004179 if (dmaru->ignored) {
4180 /*
4181 * we always have to disable PMRs or DMA may fail on this device
4182 */
4183 if (force_on)
4184 iommu_disable_protect_mem_regions(iommu);
4185 return 0;
4186 }
4187
4188 intel_iommu_init_qi(iommu);
4189 iommu_flush_write_buffer(iommu);
4190 ret = dmar_set_interrupt(iommu);
4191 if (ret)
4192 goto disable_iommu;
4193
4194 iommu_set_root_entry(iommu);
4195 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4196 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4197 iommu_enable_translation(iommu);
4198
Jiang Liuffebeb42014-11-09 22:48:02 +08004199 iommu_disable_protect_mem_regions(iommu);
4200 return 0;
4201
4202disable_iommu:
4203 disable_dmar_iommu(iommu);
4204out:
4205 free_dmar_iommu(iommu);
4206 return ret;
4207}
4208
Jiang Liu6b197242014-11-09 22:47:58 +08004209int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4210{
Jiang Liuffebeb42014-11-09 22:48:02 +08004211 int ret = 0;
4212 struct intel_iommu *iommu = dmaru->iommu;
4213
4214 if (!intel_iommu_enabled)
4215 return 0;
4216 if (iommu == NULL)
4217 return -EINVAL;
4218
4219 if (insert) {
4220 ret = intel_iommu_add(dmaru);
4221 } else {
4222 disable_dmar_iommu(iommu);
4223 free_dmar_iommu(iommu);
4224 }
4225
4226 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004227}
4228
Jiang Liu9bdc5312014-01-06 14:18:27 +08004229static void intel_iommu_free_dmars(void)
4230{
4231 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4232 struct dmar_atsr_unit *atsru, *atsr_n;
4233
4234 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4235 list_del(&rmrru->list);
4236 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4237 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004238 }
4239
Jiang Liu9bdc5312014-01-06 14:18:27 +08004240 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4241 list_del(&atsru->list);
4242 intel_iommu_free_atsr(atsru);
4243 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004244}
4245
4246int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4247{
Jiang Liub683b232014-02-19 14:07:32 +08004248 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004249 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004250 struct pci_dev *bridge = NULL;
4251 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004252 struct acpi_dmar_atsr *atsr;
4253 struct dmar_atsr_unit *atsru;
4254
4255 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004256 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004257 bridge = bus->self;
David Woodhoused14053b32015-10-15 09:28:06 +01004258 /* If it's an integrated device, allow ATS */
4259 if (!bridge)
4260 return 1;
4261 /* Connected via non-PCIe: no ATS */
4262 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004263 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004264 return 0;
David Woodhoused14053b32015-10-15 09:28:06 +01004265 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004266 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004267 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004268 }
4269
Jiang Liu0e242612014-02-19 14:07:34 +08004270 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004271 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4272 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4273 if (atsr->segment != pci_domain_nr(dev->bus))
4274 continue;
4275
Jiang Liub683b232014-02-19 14:07:32 +08004276 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004277 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004278 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004279
4280 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004281 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004282 }
Jiang Liub683b232014-02-19 14:07:32 +08004283 ret = 0;
4284out:
Jiang Liu0e242612014-02-19 14:07:34 +08004285 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004286
Jiang Liub683b232014-02-19 14:07:32 +08004287 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004288}
4289
Jiang Liu59ce0512014-02-19 14:07:35 +08004290int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4291{
4292 int ret = 0;
4293 struct dmar_rmrr_unit *rmrru;
4294 struct dmar_atsr_unit *atsru;
4295 struct acpi_dmar_atsr *atsr;
4296 struct acpi_dmar_reserved_memory *rmrr;
4297
4298 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4299 return 0;
4300
4301 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4302 rmrr = container_of(rmrru->hdr,
4303 struct acpi_dmar_reserved_memory, header);
4304 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4305 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4306 ((void *)rmrr) + rmrr->header.length,
4307 rmrr->segment, rmrru->devices,
4308 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004309 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004310 return ret;
4311 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004312 dmar_remove_dev_scope(info, rmrr->segment,
4313 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004314 }
4315 }
4316
4317 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4318 if (atsru->include_all)
4319 continue;
4320
4321 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4322 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4323 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4324 (void *)atsr + atsr->header.length,
4325 atsr->segment, atsru->devices,
4326 atsru->devices_cnt);
4327 if (ret > 0)
4328 break;
4329 else if(ret < 0)
4330 return ret;
4331 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4332 if (dmar_remove_dev_scope(info, atsr->segment,
4333 atsru->devices, atsru->devices_cnt))
4334 break;
4335 }
4336 }
4337
4338 return 0;
4339}
4340
Fenghua Yu99dcade2009-11-11 07:23:06 -08004341/*
4342 * Here we only respond to action of unbound device from driver.
4343 *
4344 * Added device is not attached to its DMAR domain here yet. That will happen
4345 * when mapping the device to iova.
4346 */
4347static int device_notifier(struct notifier_block *nb,
4348 unsigned long action, void *data)
4349{
4350 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004351 struct dmar_domain *domain;
4352
David Woodhouse3d891942014-03-06 15:59:26 +00004353 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004354 return 0;
4355
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004356 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004357 return 0;
4358
David Woodhouse1525a292014-03-06 16:19:30 +00004359 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004360 if (!domain)
4361 return 0;
4362
Joerg Roedele6de0f82015-07-22 16:30:36 +02004363 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004364 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004365 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004366
Fenghua Yu99dcade2009-11-11 07:23:06 -08004367 return 0;
4368}
4369
4370static struct notifier_block device_nb = {
4371 .notifier_call = device_notifier,
4372};
4373
Jiang Liu75f05562014-02-19 14:07:37 +08004374static int intel_iommu_memory_notifier(struct notifier_block *nb,
4375 unsigned long val, void *v)
4376{
4377 struct memory_notify *mhp = v;
4378 unsigned long long start, end;
4379 unsigned long start_vpfn, last_vpfn;
4380
4381 switch (val) {
4382 case MEM_GOING_ONLINE:
4383 start = mhp->start_pfn << PAGE_SHIFT;
4384 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4385 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004386 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004387 start, end);
4388 return NOTIFY_BAD;
4389 }
4390 break;
4391
4392 case MEM_OFFLINE:
4393 case MEM_CANCEL_ONLINE:
4394 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4395 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4396 while (start_vpfn <= last_vpfn) {
4397 struct iova *iova;
4398 struct dmar_drhd_unit *drhd;
4399 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004400 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004401
4402 iova = find_iova(&si_domain->iovad, start_vpfn);
4403 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004404 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004405 start_vpfn);
4406 break;
4407 }
4408
4409 iova = split_and_remove_iova(&si_domain->iovad, iova,
4410 start_vpfn, last_vpfn);
4411 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004412 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004413 start_vpfn, last_vpfn);
4414 return NOTIFY_BAD;
4415 }
4416
David Woodhouseea8ea462014-03-05 17:09:32 +00004417 freelist = domain_unmap(si_domain, iova->pfn_lo,
4418 iova->pfn_hi);
4419
Jiang Liu75f05562014-02-19 14:07:37 +08004420 rcu_read_lock();
4421 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004422 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004423 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004424 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004425 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004426 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004427
4428 start_vpfn = iova->pfn_hi + 1;
4429 free_iova_mem(iova);
4430 }
4431 break;
4432 }
4433
4434 return NOTIFY_OK;
4435}
4436
4437static struct notifier_block intel_iommu_memory_nb = {
4438 .notifier_call = intel_iommu_memory_notifier,
4439 .priority = 0
4440};
4441
Alex Williamsona5459cf2014-06-12 16:12:31 -06004442
4443static ssize_t intel_iommu_show_version(struct device *dev,
4444 struct device_attribute *attr,
4445 char *buf)
4446{
4447 struct intel_iommu *iommu = dev_get_drvdata(dev);
4448 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4449 return sprintf(buf, "%d:%d\n",
4450 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4451}
4452static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4453
4454static ssize_t intel_iommu_show_address(struct device *dev,
4455 struct device_attribute *attr,
4456 char *buf)
4457{
4458 struct intel_iommu *iommu = dev_get_drvdata(dev);
4459 return sprintf(buf, "%llx\n", iommu->reg_phys);
4460}
4461static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4462
4463static ssize_t intel_iommu_show_cap(struct device *dev,
4464 struct device_attribute *attr,
4465 char *buf)
4466{
4467 struct intel_iommu *iommu = dev_get_drvdata(dev);
4468 return sprintf(buf, "%llx\n", iommu->cap);
4469}
4470static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4471
4472static ssize_t intel_iommu_show_ecap(struct device *dev,
4473 struct device_attribute *attr,
4474 char *buf)
4475{
4476 struct intel_iommu *iommu = dev_get_drvdata(dev);
4477 return sprintf(buf, "%llx\n", iommu->ecap);
4478}
4479static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4480
Alex Williamson2238c082015-07-14 15:24:53 -06004481static ssize_t intel_iommu_show_ndoms(struct device *dev,
4482 struct device_attribute *attr,
4483 char *buf)
4484{
4485 struct intel_iommu *iommu = dev_get_drvdata(dev);
4486 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4487}
4488static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4489
4490static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4491 struct device_attribute *attr,
4492 char *buf)
4493{
4494 struct intel_iommu *iommu = dev_get_drvdata(dev);
4495 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4496 cap_ndoms(iommu->cap)));
4497}
4498static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4499
Alex Williamsona5459cf2014-06-12 16:12:31 -06004500static struct attribute *intel_iommu_attrs[] = {
4501 &dev_attr_version.attr,
4502 &dev_attr_address.attr,
4503 &dev_attr_cap.attr,
4504 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004505 &dev_attr_domains_supported.attr,
4506 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004507 NULL,
4508};
4509
4510static struct attribute_group intel_iommu_group = {
4511 .name = "intel-iommu",
4512 .attrs = intel_iommu_attrs,
4513};
4514
4515const struct attribute_group *intel_iommu_groups[] = {
4516 &intel_iommu_group,
4517 NULL,
4518};
4519
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004520int __init intel_iommu_init(void)
4521{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004522 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004523 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004524 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004525
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004526 /* VT-d is required for a TXT/tboot launch, so enforce that */
4527 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004528
Jiang Liu3a5670e2014-02-19 14:07:33 +08004529 if (iommu_init_mempool()) {
4530 if (force_on)
4531 panic("tboot: Failed to initialize iommu memory\n");
4532 return -ENOMEM;
4533 }
4534
4535 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004536 if (dmar_table_init()) {
4537 if (force_on)
4538 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004539 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004540 }
4541
Suresh Siddhac2c72862011-08-23 17:05:19 -07004542 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004543 if (force_on)
4544 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004545 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004546 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004547
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004548 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004549 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004550
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004551 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004552 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004553
4554 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004555 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004556
Joseph Cihula51a63e62011-03-21 11:04:24 -07004557 if (dmar_init_reserved_ranges()) {
4558 if (force_on)
4559 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004560 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004561 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004562
4563 init_no_remapping_devices();
4564
Joseph Cihulab7792602011-05-03 00:08:37 -07004565 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004566 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004567 if (force_on)
4568 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004569 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004570 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004571 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004572 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004573 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004574
mark gross5e0d2a62008-03-04 15:22:08 -08004575 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004576#ifdef CONFIG_SWIOTLB
4577 swiotlb = 0;
4578#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004579 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004580
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004581 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004582
Alex Williamsona5459cf2014-06-12 16:12:31 -06004583 for_each_active_iommu(iommu, drhd)
4584 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4585 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004586 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004587
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004588 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004589 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004590 if (si_domain && !hw_pass_through)
4591 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004592
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004593 intel_iommu_enabled = 1;
4594
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004595 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004596
4597out_free_reserved_range:
4598 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004599out_free_dmar:
4600 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004601 up_write(&dmar_global_lock);
4602 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004603 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004604}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004605
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004606static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004607{
4608 struct intel_iommu *iommu = opaque;
4609
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004610 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004611 return 0;
4612}
4613
4614/*
4615 * NB - intel-iommu lacks any sort of reference counting for the users of
4616 * dependent devices. If multiple endpoints have intersecting dependent
4617 * devices, unbinding the driver from any one of them will possibly leave
4618 * the others unable to operate.
4619 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004620static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004621{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004622 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004623 return;
4624
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004625 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004626}
4627
Joerg Roedel127c7612015-07-23 17:44:46 +02004628static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004629{
Weidong Hanc7151a82008-12-08 22:51:37 +08004630 struct intel_iommu *iommu;
4631 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004632
Joerg Roedel55d94042015-07-22 16:50:40 +02004633 assert_spin_locked(&device_domain_lock);
4634
Joerg Roedelb608ac32015-07-21 18:19:08 +02004635 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004636 return;
4637
Joerg Roedel127c7612015-07-23 17:44:46 +02004638 iommu = info->iommu;
4639
4640 if (info->dev) {
4641 iommu_disable_dev_iotlb(info);
4642 domain_context_clear(iommu, info->dev);
4643 }
4644
Joerg Roedelb608ac32015-07-21 18:19:08 +02004645 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004646
Joerg Roedeld160aca2015-07-22 11:52:53 +02004647 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004648 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004649 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004650
4651 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004652}
4653
Joerg Roedel55d94042015-07-22 16:50:40 +02004654static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4655 struct device *dev)
4656{
Joerg Roedel127c7612015-07-23 17:44:46 +02004657 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004658 unsigned long flags;
4659
Weidong Hanc7151a82008-12-08 22:51:37 +08004660 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004661 info = dev->archdata.iommu;
4662 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004663 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004664}
4665
4666static int md_domain_init(struct dmar_domain *domain, int guest_width)
4667{
4668 int adjust_width;
4669
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004670 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4671 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004672 domain_reserve_special_ranges(domain);
4673
4674 /* calculate AGAW */
4675 domain->gaw = guest_width;
4676 adjust_width = guestwidth_to_adjustwidth(guest_width);
4677 domain->agaw = width_to_agaw(adjust_width);
4678
Weidong Han5e98c4b2008-12-08 23:03:27 +08004679 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004680 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004681 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004682 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004683
4684 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004685 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004686 if (!domain->pgd)
4687 return -ENOMEM;
4688 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4689 return 0;
4690}
4691
Joerg Roedel00a77de2015-03-26 13:43:08 +01004692static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004693{
Joerg Roedel5d450802008-12-03 14:52:32 +01004694 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004695 struct iommu_domain *domain;
4696
4697 if (type != IOMMU_DOMAIN_UNMANAGED)
4698 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004699
Jiang Liuab8dfe22014-07-11 14:19:27 +08004700 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004701 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004702 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004703 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004704 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004705 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004706 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004707 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004708 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004709 }
Allen Kay8140a952011-10-14 12:32:17 -07004710 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004711
Joerg Roedel00a77de2015-03-26 13:43:08 +01004712 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004713 domain->geometry.aperture_start = 0;
4714 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4715 domain->geometry.force_aperture = true;
4716
Joerg Roedel00a77de2015-03-26 13:43:08 +01004717 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004718}
Kay, Allen M38717942008-09-09 18:37:29 +03004719
Joerg Roedel00a77de2015-03-26 13:43:08 +01004720static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004721{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004722 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004723}
Kay, Allen M38717942008-09-09 18:37:29 +03004724
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004725static int intel_iommu_attach_device(struct iommu_domain *domain,
4726 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004727{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004728 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004729 struct intel_iommu *iommu;
4730 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004731 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004732
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004733 if (device_is_rmrr_locked(dev)) {
4734 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4735 return -EPERM;
4736 }
4737
David Woodhouse7207d8f2014-03-09 16:31:06 -07004738 /* normally dev is not mapped */
4739 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004740 struct dmar_domain *old_domain;
4741
David Woodhouse1525a292014-03-06 16:19:30 +00004742 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004743 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02004744 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02004745 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004746 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01004747
4748 if (!domain_type_is_vm_or_si(old_domain) &&
4749 list_empty(&old_domain->devices))
4750 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004751 }
4752 }
4753
David Woodhouse156baca2014-03-09 14:00:57 -07004754 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004755 if (!iommu)
4756 return -ENODEV;
4757
4758 /* check if this iommu agaw is sufficient for max mapped address */
4759 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004760 if (addr_width > cap_mgaw(iommu->cap))
4761 addr_width = cap_mgaw(iommu->cap);
4762
4763 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004764 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004765 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004766 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004767 return -EFAULT;
4768 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004769 dmar_domain->gaw = addr_width;
4770
4771 /*
4772 * Knock out extra levels of page tables if necessary
4773 */
4774 while (iommu->agaw < dmar_domain->agaw) {
4775 struct dma_pte *pte;
4776
4777 pte = dmar_domain->pgd;
4778 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004779 dmar_domain->pgd = (struct dma_pte *)
4780 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004781 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004782 }
4783 dmar_domain->agaw--;
4784 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004785
Joerg Roedel28ccce02015-07-21 14:45:31 +02004786 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004787}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004788
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004789static void intel_iommu_detach_device(struct iommu_domain *domain,
4790 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004791{
Joerg Roedele6de0f82015-07-22 16:30:36 +02004792 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004793}
Kay, Allen M38717942008-09-09 18:37:29 +03004794
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004795static int intel_iommu_map(struct iommu_domain *domain,
4796 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004797 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004798{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004799 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004800 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004801 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004802 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004803
Joerg Roedeldde57a22008-12-03 15:04:09 +01004804 if (iommu_prot & IOMMU_READ)
4805 prot |= DMA_PTE_READ;
4806 if (iommu_prot & IOMMU_WRITE)
4807 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08004808 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4809 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004810
David Woodhouse163cc522009-06-28 00:51:17 +01004811 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004812 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004813 u64 end;
4814
4815 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004816 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004817 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004818 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004819 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004820 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004821 return -EFAULT;
4822 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004823 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004824 }
David Woodhousead051222009-06-28 14:22:28 +01004825 /* Round up size to next multiple of PAGE_SIZE, if it and
4826 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004827 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004828 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4829 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004830 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004831}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004832
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004833static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004834 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004835{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004836 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004837 struct page *freelist = NULL;
4838 struct intel_iommu *iommu;
4839 unsigned long start_pfn, last_pfn;
4840 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02004841 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004842
David Woodhouse5cf0a762014-03-19 16:07:49 +00004843 /* Cope with horrid API which requires us to unmap more than the
4844 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02004845 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00004846
4847 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4848 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4849
David Woodhouseea8ea462014-03-05 17:09:32 +00004850 start_pfn = iova >> VTD_PAGE_SHIFT;
4851 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4852
4853 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4854
4855 npages = last_pfn - start_pfn + 1;
4856
Joerg Roedel29a27712015-07-21 17:17:12 +02004857 for_each_domain_iommu(iommu_id, dmar_domain) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004858 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00004859
Joerg Roedel42e8c182015-07-21 15:50:02 +02004860 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
4861 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00004862 }
4863
4864 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004865
David Woodhouse163cc522009-06-28 00:51:17 +01004866 if (dmar_domain->max_addr == iova + size)
4867 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004868
David Woodhouse5cf0a762014-03-19 16:07:49 +00004869 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004870}
Kay, Allen M38717942008-09-09 18:37:29 +03004871
Joerg Roedeld14d6572008-12-03 15:06:57 +01004872static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05304873 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004874{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004875 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004876 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004877 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004878 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004879
David Woodhouse5cf0a762014-03-19 16:07:49 +00004880 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004881 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004882 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004883
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004884 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004885}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004886
Joerg Roedel5d587b82014-09-05 10:50:45 +02004887static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004888{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004889 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004890 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004891 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004892 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004893
Joerg Roedel5d587b82014-09-05 10:50:45 +02004894 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004895}
4896
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004897static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004898{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004899 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004900 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004901 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004902
Alex Williamsona5459cf2014-06-12 16:12:31 -06004903 iommu = device_to_iommu(dev, &bus, &devfn);
4904 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004905 return -ENODEV;
4906
Alex Williamsona5459cf2014-06-12 16:12:31 -06004907 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004908
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004909 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004910
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004911 if (IS_ERR(group))
4912 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004913
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004914 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004915 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004916}
4917
4918static void intel_iommu_remove_device(struct device *dev)
4919{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004920 struct intel_iommu *iommu;
4921 u8 bus, devfn;
4922
4923 iommu = device_to_iommu(dev, &bus, &devfn);
4924 if (!iommu)
4925 return;
4926
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004927 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004928
4929 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004930}
4931
David Woodhouse2f26e0a2015-09-09 11:40:47 +01004932#ifdef CONFIG_INTEL_IOMMU_SVM
4933int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
4934{
4935 struct device_domain_info *info;
4936 struct context_entry *context;
4937 struct dmar_domain *domain;
4938 unsigned long flags;
4939 u64 ctx_lo;
4940 int ret;
4941
4942 domain = get_valid_domain_for_dev(sdev->dev);
4943 if (!domain)
4944 return -EINVAL;
4945
4946 spin_lock_irqsave(&device_domain_lock, flags);
4947 spin_lock(&iommu->lock);
4948
4949 ret = -EINVAL;
4950 info = sdev->dev->archdata.iommu;
4951 if (!info || !info->pasid_supported)
4952 goto out;
4953
4954 context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
4955 if (WARN_ON(!context))
4956 goto out;
4957
4958 ctx_lo = context[0].lo;
4959
4960 sdev->did = domain->iommu_did[iommu->seq_id];
4961 sdev->sid = PCI_DEVID(info->bus, info->devfn);
4962
4963 if (!(ctx_lo & CONTEXT_PASIDE)) {
4964 context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
4965 context[1].lo = (u64)virt_to_phys(iommu->pasid_table) | ecap_pss(iommu->ecap);
4966 wmb();
4967 /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
4968 * extended to permit requests-with-PASID if the PASIDE bit
4969 * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
4970 * however, the PASIDE bit is ignored and requests-with-PASID
4971 * are unconditionally blocked. Which makes less sense.
4972 * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
4973 * "guest mode" translation types depending on whether ATS
4974 * is available or not. Annoyingly, we can't use the new
4975 * modes *unless* PASIDE is set. */
4976 if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
4977 ctx_lo &= ~CONTEXT_TT_MASK;
4978 if (info->ats_supported)
4979 ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
4980 else
4981 ctx_lo |= CONTEXT_TT_PT_PASID << 2;
4982 }
4983 ctx_lo |= CONTEXT_PASIDE;
4984 context[0].lo = ctx_lo;
4985 wmb();
4986 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
4987 DMA_CCMD_MASK_NOBIT,
4988 DMA_CCMD_DEVICE_INVL);
4989 }
4990
4991 /* Enable PASID support in the device, if it wasn't already */
4992 if (!info->pasid_enabled)
4993 iommu_enable_dev_iotlb(info);
4994
4995 if (info->ats_enabled) {
4996 sdev->dev_iotlb = 1;
4997 sdev->qdep = info->ats_qdep;
4998 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
4999 sdev->qdep = 0;
5000 }
5001 ret = 0;
5002
5003 out:
5004 spin_unlock(&iommu->lock);
5005 spin_unlock_irqrestore(&device_domain_lock, flags);
5006
5007 return ret;
5008}
5009
5010struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5011{
5012 struct intel_iommu *iommu;
5013 u8 bus, devfn;
5014
5015 if (iommu_dummy(dev)) {
5016 dev_warn(dev,
5017 "No IOMMU translation for device; cannot enable SVM\n");
5018 return NULL;
5019 }
5020
5021 iommu = device_to_iommu(dev, &bus, &devfn);
5022 if ((!iommu)) {
5023 dev_dbg(dev, "No IOMMU for device; cannot enable SVM\n");
5024 return NULL;
5025 }
5026
5027 if (!iommu->pasid_table) {
5028 dev_dbg(dev, "PASID not enabled on IOMMU; cannot enable SVM\n");
5029 return NULL;
5030 }
5031
5032 return iommu;
5033}
5034#endif /* CONFIG_INTEL_IOMMU_SVM */
5035
Thierry Redingb22f6432014-06-27 09:03:12 +02005036static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02005037 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01005038 .domain_alloc = intel_iommu_domain_alloc,
5039 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005040 .attach_dev = intel_iommu_attach_device,
5041 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005042 .map = intel_iommu_map,
5043 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07005044 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005045 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005046 .add_device = intel_iommu_add_device,
5047 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02005048 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005049};
David Woodhouse9af88142009-02-13 23:18:03 +00005050
Daniel Vetter94526182013-01-20 23:50:13 +01005051static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
5052{
5053 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005054 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01005055 dmar_map_gfx = 0;
5056}
5057
5058DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
5059DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
5060DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
5061DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
5062DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
5063DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
5064DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
5065
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005066static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00005067{
5068 /*
5069 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01005070 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00005071 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005072 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005073 rwbf_quirk = 1;
5074}
5075
5076DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005077DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5078DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5079DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5080DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5081DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5082DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005083
Adam Jacksoneecfd572010-08-25 21:17:34 +01005084#define GGC 0x52
5085#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5086#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5087#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5088#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5089#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5090#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5091#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5092#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5093
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005094static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005095{
5096 unsigned short ggc;
5097
Adam Jacksoneecfd572010-08-25 21:17:34 +01005098 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005099 return;
5100
Adam Jacksoneecfd572010-08-25 21:17:34 +01005101 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005102 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005103 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005104 } else if (dmar_map_gfx) {
5105 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005106 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005107 intel_iommu_strict = 1;
5108 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005109}
5110DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5111DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5112DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5113DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5114
David Woodhousee0fc7e02009-09-30 09:12:17 -07005115/* On Tylersburg chipsets, some BIOSes have been known to enable the
5116 ISOCH DMAR unit for the Azalia sound device, but not give it any
5117 TLB entries, which causes it to deadlock. Check for that. We do
5118 this in a function called from init_dmars(), instead of in a PCI
5119 quirk, because we don't want to print the obnoxious "BIOS broken"
5120 message if VT-d is actually disabled.
5121*/
5122static void __init check_tylersburg_isoch(void)
5123{
5124 struct pci_dev *pdev;
5125 uint32_t vtisochctrl;
5126
5127 /* If there's no Azalia in the system anyway, forget it. */
5128 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5129 if (!pdev)
5130 return;
5131 pci_dev_put(pdev);
5132
5133 /* System Management Registers. Might be hidden, in which case
5134 we can't do the sanity check. But that's OK, because the
5135 known-broken BIOSes _don't_ actually hide it, so far. */
5136 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5137 if (!pdev)
5138 return;
5139
5140 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5141 pci_dev_put(pdev);
5142 return;
5143 }
5144
5145 pci_dev_put(pdev);
5146
5147 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5148 if (vtisochctrl & 1)
5149 return;
5150
5151 /* Drop all bits other than the number of TLB entries */
5152 vtisochctrl &= 0x1c;
5153
5154 /* If we have the recommended number of TLB entries (16), fine. */
5155 if (vtisochctrl == 0x10)
5156 return;
5157
5158 /* Zero TLB entries? You get to ride the short bus to school. */
5159 if (!vtisochctrl) {
5160 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5161 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5162 dmi_get_system_info(DMI_BIOS_VENDOR),
5163 dmi_get_system_info(DMI_BIOS_VERSION),
5164 dmi_get_system_info(DMI_PRODUCT_VERSION));
5165 iommu_identity_mapping |= IDENTMAP_AZALIA;
5166 return;
5167 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005168
5169 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005170 vtisochctrl);
5171}