blob: 68d71f81dfa0ab7e8392d490cfd913589039c3ff [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
mark gross5e0d2a62008-03-04 15:22:08 -080036#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030037#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010038#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010040#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070041#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100042#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020043#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080044#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070045#include <linux/dma-contiguous.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020046#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070047#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070048#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070050
Joerg Roedel078e1ee2012-09-26 12:44:43 +020051#include "irq_remapping.h"
52
Fenghua Yu5b6985c2008-10-16 18:02:32 -070053#define ROOT_SIZE VTD_PAGE_SIZE
54#define CONTEXT_SIZE VTD_PAGE_SIZE
55
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070056#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000057#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070059#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060
61#define IOAPIC_RANGE_START (0xfee00000)
62#define IOAPIC_RANGE_END (0xfeefffff)
63#define IOVA_START_ADDR (0x1000)
64
65#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
66
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070067#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080068#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070069
David Woodhouse2ebe3152009-09-19 07:34:04 -070070#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
71#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
72
73/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
74 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
75#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
76 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
77#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070078
Robin Murphy1b722502015-01-12 17:51:15 +000079/* IO virtual address start page frame number */
80#define IOVA_START_PFN (1)
81
Mark McLoughlinf27be032008-11-20 15:49:43 +000082#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070083#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070084#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080085
Andrew Mortondf08cdc2010-09-22 13:05:11 -070086/* page table handling */
87#define LEVEL_STRIDE (9)
88#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
89
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020090/*
91 * This bitmap is used to advertise the page sizes our hardware support
92 * to the IOMMU core, which will then use this information to split
93 * physically contiguous memory regions it is mapping into page sizes
94 * that we support.
95 *
96 * Traditionally the IOMMU core just handed us the mappings directly,
97 * after making sure the size is an order of a 4KiB page and that the
98 * mapping has natural alignment.
99 *
100 * To retain this behavior, we currently advertise that we support
101 * all page sizes that are an order of 4KiB.
102 *
103 * If at some point we'd like to utilize the IOMMU core's new behavior,
104 * we could change this to advertise the real page sizes we support.
105 */
106#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
107
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700108static inline int agaw_to_level(int agaw)
109{
110 return agaw + 2;
111}
112
113static inline int agaw_to_width(int agaw)
114{
Jiang Liu5c645b32014-01-06 14:18:12 +0800115 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700116}
117
118static inline int width_to_agaw(int width)
119{
Jiang Liu5c645b32014-01-06 14:18:12 +0800120 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700121}
122
123static inline unsigned int level_to_offset_bits(int level)
124{
125 return (level - 1) * LEVEL_STRIDE;
126}
127
128static inline int pfn_level_offset(unsigned long pfn, int level)
129{
130 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
131}
132
133static inline unsigned long level_mask(int level)
134{
135 return -1UL << level_to_offset_bits(level);
136}
137
138static inline unsigned long level_size(int level)
139{
140 return 1UL << level_to_offset_bits(level);
141}
142
143static inline unsigned long align_to_level(unsigned long pfn, int level)
144{
145 return (pfn + level_size(level) - 1) & level_mask(level);
146}
David Woodhousefd18de52009-05-10 23:57:41 +0100147
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100148static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
149{
Jiang Liu5c645b32014-01-06 14:18:12 +0800150 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100151}
152
David Woodhousedd4e8312009-06-27 16:21:20 +0100153/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
154 are never going to work. */
155static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
156{
157 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
158}
159
160static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
161{
162 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
163}
164static inline unsigned long page_to_dma_pfn(struct page *pg)
165{
166 return mm_to_dma_pfn(page_to_pfn(pg));
167}
168static inline unsigned long virt_to_dma_pfn(void *p)
169{
170 return page_to_dma_pfn(virt_to_page(p));
171}
172
Weidong Hand9630fe2008-12-08 11:06:32 +0800173/* global iommu list, set NULL for ignored DMAR units */
174static struct intel_iommu **g_iommus;
175
David Woodhousee0fc7e02009-09-30 09:12:17 -0700176static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000177static int rwbf_quirk;
178
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000179/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700180 * set to 1 to panic kernel if can't successfully enable VT-d
181 * (used when kernel is launched w/ TXT)
182 */
183static int force_on = 0;
184
185/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000186 * 0: Present
187 * 1-11: Reserved
188 * 12-63: Context Ptr (12 - (haw-1))
189 * 64-127: Reserved
190 */
191struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000192 u64 lo;
193 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000194};
195#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000196
Joerg Roedel091d42e2015-06-12 11:56:10 +0200197/*
198 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
199 * if marked present.
200 */
201static phys_addr_t root_entry_lctp(struct root_entry *re)
202{
203 if (!(re->lo & 1))
204 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000205
Joerg Roedel091d42e2015-06-12 11:56:10 +0200206 return re->lo & VTD_PAGE_MASK;
207}
208
209/*
210 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
211 * if marked present.
212 */
213static phys_addr_t root_entry_uctp(struct root_entry *re)
214{
215 if (!(re->hi & 1))
216 return 0;
217
218 return re->hi & VTD_PAGE_MASK;
219}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000220/*
221 * low 64 bits:
222 * 0: present
223 * 1: fault processing disable
224 * 2-3: translation type
225 * 12-63: address space root
226 * high 64 bits:
227 * 0-2: address width
228 * 3-6: aval
229 * 8-23: domain id
230 */
231struct context_entry {
232 u64 lo;
233 u64 hi;
234};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000235
Joerg Roedelcf484d02015-06-12 12:21:46 +0200236static inline void context_clear_pasid_enable(struct context_entry *context)
237{
238 context->lo &= ~(1ULL << 11);
239}
240
241static inline bool context_pasid_enabled(struct context_entry *context)
242{
243 return !!(context->lo & (1ULL << 11));
244}
245
246static inline void context_set_copied(struct context_entry *context)
247{
248 context->hi |= (1ull << 3);
249}
250
251static inline bool context_copied(struct context_entry *context)
252{
253 return !!(context->hi & (1ULL << 3));
254}
255
256static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000257{
258 return (context->lo & 1);
259}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200260
261static inline bool context_present(struct context_entry *context)
262{
263 return context_pasid_enabled(context) ?
264 __context_present(context) :
265 __context_present(context) && !context_copied(context);
266}
267
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000268static inline void context_set_present(struct context_entry *context)
269{
270 context->lo |= 1;
271}
272
273static inline void context_set_fault_enable(struct context_entry *context)
274{
275 context->lo &= (((u64)-1) << 2) | 1;
276}
277
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000278static inline void context_set_translation_type(struct context_entry *context,
279 unsigned long value)
280{
281 context->lo &= (((u64)-1) << 4) | 3;
282 context->lo |= (value & 3) << 2;
283}
284
285static inline void context_set_address_root(struct context_entry *context,
286 unsigned long value)
287{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800288 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000289 context->lo |= value & VTD_PAGE_MASK;
290}
291
292static inline void context_set_address_width(struct context_entry *context,
293 unsigned long value)
294{
295 context->hi |= value & 7;
296}
297
298static inline void context_set_domain_id(struct context_entry *context,
299 unsigned long value)
300{
301 context->hi |= (value & ((1 << 16) - 1)) << 8;
302}
303
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200304static inline int context_domain_id(struct context_entry *c)
305{
306 return((c->hi >> 8) & 0xffff);
307}
308
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000309static inline void context_clear_entry(struct context_entry *context)
310{
311 context->lo = 0;
312 context->hi = 0;
313}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000314
Mark McLoughlin622ba122008-11-20 15:49:46 +0000315/*
316 * 0: readable
317 * 1: writable
318 * 2-6: reserved
319 * 7: super page
Sheng Yang9cf066972009-03-18 15:33:07 +0800320 * 8-10: available
321 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000322 * 12-63: Host physcial address
323 */
324struct dma_pte {
325 u64 val;
326};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000327
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000328static inline void dma_clear_pte(struct dma_pte *pte)
329{
330 pte->val = 0;
331}
332
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000333static inline u64 dma_pte_addr(struct dma_pte *pte)
334{
David Woodhousec85994e2009-07-01 19:21:24 +0100335#ifdef CONFIG_64BIT
336 return pte->val & VTD_PAGE_MASK;
337#else
338 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100339 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100340#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000341}
342
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000343static inline bool dma_pte_present(struct dma_pte *pte)
344{
345 return (pte->val & 3) != 0;
346}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000347
Allen Kay4399c8b2011-10-14 12:32:46 -0700348static inline bool dma_pte_superpage(struct dma_pte *pte)
349{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200350 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700351}
352
David Woodhouse75e6bf92009-07-02 11:21:16 +0100353static inline int first_pte_in_page(struct dma_pte *pte)
354{
355 return !((unsigned long)pte & ~VTD_PAGE_MASK);
356}
357
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700358/*
359 * This domain is a statically identity mapping domain.
360 * 1. This domain creats a static 1:1 mapping to all usable memory.
361 * 2. It maps to each iommu if successful.
362 * 3. Each iommu mapps to this domain if successful.
363 */
David Woodhouse19943b02009-08-04 16:19:20 +0100364static struct dmar_domain *si_domain;
365static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700366
Joerg Roedel28ccce02015-07-21 14:45:31 +0200367/*
368 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800369 * across iommus may be owned in one domain, e.g. kvm guest.
370 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800371#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800372
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700373/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800374#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700375
Joerg Roedel29a27712015-07-21 17:17:12 +0200376#define for_each_domain_iommu(idx, domain) \
377 for (idx = 0; idx < g_num_of_iommus; idx++) \
378 if (domain->iommu_refcnt[idx])
379
Mark McLoughlin99126f72008-11-20 15:49:47 +0000380struct dmar_domain {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700381 int nid; /* node id */
Joerg Roedel29a27712015-07-21 17:17:12 +0200382
383 unsigned iommu_refcnt[DMAR_UNITS_SUPPORTED];
384 /* Refcount of devices per iommu */
385
Mark McLoughlin99126f72008-11-20 15:49:47 +0000386
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +0200387 u16 iommu_did[DMAR_UNITS_SUPPORTED];
388 /* Domain ids per IOMMU. Use u16 since
389 * domain ids are 16 bit wide according
390 * to VT-d spec, section 9.3 */
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 McLoughlina647dac2008-11-20 15:49:48 +0000415/* PCI domain-device relationship */
416struct device_domain_info {
417 struct list_head link; /* link to domain siblings */
418 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100419 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000420 u8 devfn; /* PCI devfn number */
David 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 McLoughlina647dac2008-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 Woodhouse206a73c12009-07-01 19:30:28 +01001007 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001008 return NULL;
David Woodhouse206a73c12009-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 Woodhouse595badf2009-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 Dutile94a91b52009-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
David Woodhousea222a7f2015-10-07 23:35:18 +01001701 if (pasid_enabled(iommu)) {
1702 if (ecap_prs(iommu->ecap))
1703 intel_svm_finish_prq(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001704 intel_svm_free_pasid_tables(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01001705 }
David Woodhouse8a94ade2015-03-24 14:54:56 +00001706#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001707}
1708
Jiang Liuab8dfe22014-07-11 14:19:27 +08001709static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001711 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001712
1713 domain = alloc_domain_mem();
1714 if (!domain)
1715 return NULL;
1716
Jiang Liuab8dfe22014-07-11 14:19:27 +08001717 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001718 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001719 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001720 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001721
1722 return domain;
1723}
1724
Joerg Roedeld160aca2015-07-22 11:52:53 +02001725/* Must be called with iommu->lock */
1726static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001727 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001728{
Jiang Liu44bde612014-07-11 14:19:29 +08001729 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001730 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001731
Joerg Roedel55d94042015-07-22 16:50:40 +02001732 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001733 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001734
Joerg Roedel29a27712015-07-21 17:17:12 +02001735 domain->iommu_refcnt[iommu->seq_id] += 1;
1736 domain->iommu_count += 1;
1737 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001738 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001739 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1740
1741 if (num >= ndomains) {
1742 pr_err("%s: No free domain ids\n", iommu->name);
1743 domain->iommu_refcnt[iommu->seq_id] -= 1;
1744 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001745 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001746 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001747
Joerg Roedeld160aca2015-07-22 11:52:53 +02001748 set_bit(num, iommu->domain_ids);
1749 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001750
Joerg Roedeld160aca2015-07-22 11:52:53 +02001751 domain->iommu_did[iommu->seq_id] = num;
1752 domain->nid = iommu->node;
1753
Jiang Liufb170fb2014-07-11 14:19:28 +08001754 domain_update_iommu_cap(domain);
1755 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001756
Joerg Roedel55d94042015-07-22 16:50:40 +02001757 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001758}
1759
1760static int domain_detach_iommu(struct dmar_domain *domain,
1761 struct intel_iommu *iommu)
1762{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001763 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001764
Joerg Roedel55d94042015-07-22 16:50:40 +02001765 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001766 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001767
Joerg Roedel29a27712015-07-21 17:17:12 +02001768 domain->iommu_refcnt[iommu->seq_id] -= 1;
1769 count = --domain->iommu_count;
1770 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001771 num = domain->iommu_did[iommu->seq_id];
1772 clear_bit(num, iommu->domain_ids);
1773 set_iommu_domain(iommu, num, NULL);
1774
Jiang Liufb170fb2014-07-11 14:19:28 +08001775 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001776 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001777 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001778
1779 return count;
1780}
1781
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001782static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001783static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001784
Joseph Cihula51a63e62011-03-21 11:04:24 -07001785static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001786{
1787 struct pci_dev *pdev = NULL;
1788 struct iova *iova;
1789 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001790
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001791 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1792 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001793
Mark Gross8a443df2008-03-04 14:59:31 -08001794 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1795 &reserved_rbtree_key);
1796
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001797 /* IOAPIC ranges shouldn't be accessed by DMA */
1798 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1799 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001800 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001801 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001802 return -ENODEV;
1803 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001804
1805 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1806 for_each_pci_dev(pdev) {
1807 struct resource *r;
1808
1809 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1810 r = &pdev->resource[i];
1811 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1812 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001813 iova = reserve_iova(&reserved_iova_list,
1814 IOVA_PFN(r->start),
1815 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001816 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001817 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001818 return -ENODEV;
1819 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001820 }
1821 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001822 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001823}
1824
1825static void domain_reserve_special_ranges(struct dmar_domain *domain)
1826{
1827 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1828}
1829
1830static inline int guestwidth_to_adjustwidth(int gaw)
1831{
1832 int agaw;
1833 int r = (gaw - 12) % 9;
1834
1835 if (r == 0)
1836 agaw = gaw;
1837 else
1838 agaw = gaw + 9 - r;
1839 if (agaw > 64)
1840 agaw = 64;
1841 return agaw;
1842}
1843
Joerg Roedeldc534b22015-07-22 12:44:02 +02001844static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1845 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001846{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001847 int adjust_width, agaw;
1848 unsigned long sagaw;
1849
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001850 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1851 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001852 domain_reserve_special_ranges(domain);
1853
1854 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001855 if (guest_width > cap_mgaw(iommu->cap))
1856 guest_width = cap_mgaw(iommu->cap);
1857 domain->gaw = guest_width;
1858 adjust_width = guestwidth_to_adjustwidth(guest_width);
1859 agaw = width_to_agaw(adjust_width);
1860 sagaw = cap_sagaw(iommu->cap);
1861 if (!test_bit(agaw, &sagaw)) {
1862 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001863 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001864 agaw = find_next_bit(&sagaw, 5, agaw);
1865 if (agaw >= 5)
1866 return -ENODEV;
1867 }
1868 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869
Weidong Han8e6040972008-12-08 15:49:06 +08001870 if (ecap_coherent(iommu->ecap))
1871 domain->iommu_coherency = 1;
1872 else
1873 domain->iommu_coherency = 0;
1874
Sheng Yang58c610b2009-03-18 15:33:05 +08001875 if (ecap_sc_support(iommu->ecap))
1876 domain->iommu_snooping = 1;
1877 else
1878 domain->iommu_snooping = 0;
1879
David Woodhouse214e39a2014-03-19 10:38:49 +00001880 if (intel_iommu_superpage)
1881 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1882 else
1883 domain->iommu_superpage = 0;
1884
Suresh Siddha4c923d42009-10-02 11:01:24 -07001885 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001886
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001887 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001888 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001889 if (!domain->pgd)
1890 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001891 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001892 return 0;
1893}
1894
1895static void domain_exit(struct dmar_domain *domain)
1896{
David Woodhouseea8ea462014-03-05 17:09:32 +00001897 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001898
1899 /* Domain 0 is reserved, so dont process it */
1900 if (!domain)
1901 return;
1902
Alex Williamson7b668352011-05-24 12:02:41 +01001903 /* Flush any lazy unmaps that may reference this domain */
1904 if (!intel_iommu_strict)
1905 flush_unmaps_timeout(0);
1906
Joerg Roedeld160aca2015-07-22 11:52:53 +02001907 /* Remove associated devices and clear attached or cached domains */
1908 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001909 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001910 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001911
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001912 /* destroy iovas */
1913 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001914
David Woodhouseea8ea462014-03-05 17:09:32 +00001915 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001916
David Woodhouseea8ea462014-03-05 17:09:32 +00001917 dma_free_pagelist(freelist);
1918
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001919 free_domain_mem(domain);
1920}
1921
David Woodhouse64ae8922014-03-09 12:52:30 -07001922static int domain_context_mapping_one(struct dmar_domain *domain,
1923 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001924 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001925{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001926 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001927 int translation = CONTEXT_TT_MULTI_LEVEL;
1928 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001929 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001930 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001931 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02001932 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001933
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001934 WARN_ON(did == 0);
1935
Joerg Roedel28ccce02015-07-21 14:45:31 +02001936 if (hw_pass_through && domain_type_is_si(domain))
1937 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001938
1939 pr_debug("Set context mapping for %02x:%02x.%d\n",
1940 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001941
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001942 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001943
Joerg Roedel55d94042015-07-22 16:50:40 +02001944 spin_lock_irqsave(&device_domain_lock, flags);
1945 spin_lock(&iommu->lock);
1946
1947 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00001948 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001949 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02001950 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001951
Joerg Roedel55d94042015-07-22 16:50:40 +02001952 ret = 0;
1953 if (context_present(context))
1954 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02001955
Weidong Hanea6606b2008-12-08 23:08:15 +08001956 pgd = domain->pgd;
1957
Joerg Roedelde24e552015-07-21 14:53:04 +02001958 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001959 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08001960
Joerg Roedelde24e552015-07-21 14:53:04 +02001961 /*
1962 * Skip top levels of page tables for iommu which has less agaw
1963 * than default. Unnecessary for PT mode.
1964 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001965 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02001966 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02001967 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02001968 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02001969 if (!dma_pte_present(pgd))
1970 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02001971 }
1972
David Woodhouse64ae8922014-03-09 12:52:30 -07001973 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001974 if (info && info->ats_supported)
1975 translation = CONTEXT_TT_DEV_IOTLB;
1976 else
1977 translation = CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02001978
Yu Zhao93a23a72009-05-18 13:51:37 +08001979 context_set_address_root(context, virt_to_phys(pgd));
1980 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02001981 } else {
1982 /*
1983 * In pass through mode, AW must be programmed to
1984 * indicate the largest AGAW value supported by
1985 * hardware. And ASR is ignored by hardware.
1986 */
1987 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001988 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001989
1990 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001991 context_set_fault_enable(context);
1992 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001993 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001994
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001995 /*
1996 * It's a non-present to present mapping. If hardware doesn't cache
1997 * non-present entry we only need to flush the write-buffer. If the
1998 * _does_ cache non-present entries, then it does so in the special
1999 * domain #0, which we have to flush:
2000 */
2001 if (cap_caching_mode(iommu->cap)) {
2002 iommu->flush.flush_context(iommu, 0,
2003 (((u16)bus) << 8) | devfn,
2004 DMA_CCMD_MASK_NOBIT,
2005 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002006 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002007 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002008 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002009 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002010 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08002011
Joerg Roedel55d94042015-07-22 16:50:40 +02002012 ret = 0;
2013
2014out_unlock:
2015 spin_unlock(&iommu->lock);
2016 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08002017
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002018 return 0;
2019}
2020
Alex Williamson579305f2014-07-03 09:51:43 -06002021struct domain_context_mapping_data {
2022 struct dmar_domain *domain;
2023 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002024};
2025
2026static int domain_context_mapping_cb(struct pci_dev *pdev,
2027 u16 alias, void *opaque)
2028{
2029 struct domain_context_mapping_data *data = opaque;
2030
2031 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002032 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002033}
2034
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002035static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002036domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002037{
David Woodhouse64ae8922014-03-09 12:52:30 -07002038 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002039 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002040 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002041
David Woodhousee1f167f2014-03-09 15:24:46 -07002042 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002043 if (!iommu)
2044 return -ENODEV;
2045
Alex Williamson579305f2014-07-03 09:51:43 -06002046 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002047 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002048
2049 data.domain = domain;
2050 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002051
2052 return pci_for_each_dma_alias(to_pci_dev(dev),
2053 &domain_context_mapping_cb, &data);
2054}
2055
2056static int domain_context_mapped_cb(struct pci_dev *pdev,
2057 u16 alias, void *opaque)
2058{
2059 struct intel_iommu *iommu = opaque;
2060
2061 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002062}
2063
David Woodhousee1f167f2014-03-09 15:24:46 -07002064static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002065{
Weidong Han5331fe62008-12-08 23:00:00 +08002066 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002067 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002068
David Woodhousee1f167f2014-03-09 15:24:46 -07002069 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002070 if (!iommu)
2071 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002072
Alex Williamson579305f2014-07-03 09:51:43 -06002073 if (!dev_is_pci(dev))
2074 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002075
Alex Williamson579305f2014-07-03 09:51:43 -06002076 return !pci_for_each_dma_alias(to_pci_dev(dev),
2077 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002078}
2079
Fenghua Yuf5329592009-08-04 15:09:37 -07002080/* Returns a number of VTD pages, but aligned to MM page size */
2081static inline unsigned long aligned_nrpages(unsigned long host_addr,
2082 size_t size)
2083{
2084 host_addr &= ~PAGE_MASK;
2085 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2086}
2087
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002088/* Return largest possible superpage level for a given mapping */
2089static inline int hardware_largepage_caps(struct dmar_domain *domain,
2090 unsigned long iov_pfn,
2091 unsigned long phy_pfn,
2092 unsigned long pages)
2093{
2094 int support, level = 1;
2095 unsigned long pfnmerge;
2096
2097 support = domain->iommu_superpage;
2098
2099 /* To use a large page, the virtual *and* physical addresses
2100 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2101 of them will mean we have to use smaller pages. So just
2102 merge them and check both at once. */
2103 pfnmerge = iov_pfn | phy_pfn;
2104
2105 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2106 pages >>= VTD_STRIDE_SHIFT;
2107 if (!pages)
2108 break;
2109 pfnmerge >>= VTD_STRIDE_SHIFT;
2110 level++;
2111 support--;
2112 }
2113 return level;
2114}
2115
David Woodhouse9051aa02009-06-29 12:30:54 +01002116static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2117 struct scatterlist *sg, unsigned long phys_pfn,
2118 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002119{
2120 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002121 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002122 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002123 unsigned int largepage_lvl = 0;
2124 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002125
Jiang Liu162d1b12014-07-11 14:19:35 +08002126 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002127
2128 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2129 return -EINVAL;
2130
2131 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2132
Jiang Liucc4f14a2014-11-26 09:42:10 +08002133 if (!sg) {
2134 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002135 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2136 }
2137
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002138 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002139 uint64_t tmp;
2140
David Woodhousee1605492009-06-29 11:17:38 +01002141 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002142 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002143 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2144 sg->dma_length = sg->length;
Dan Williamsdb0fa0c2015-08-17 08:13:26 -06002145 pteval = (sg_phys(sg) & PAGE_MASK) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002146 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002147 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002148
David Woodhousee1605492009-06-29 11:17:38 +01002149 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002150 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2151
David Woodhouse5cf0a762014-03-19 16:07:49 +00002152 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002153 if (!pte)
2154 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002155 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002156 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002157 unsigned long nr_superpages, end_pfn;
2158
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002159 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002160 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002161
2162 nr_superpages = sg_res / lvl_pages;
2163 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2164
Jiang Liud41a4ad2014-07-11 14:19:34 +08002165 /*
2166 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002167 * removed to make room for superpage(s).
Jiang Liud41a4ad2014-07-11 14:19:34 +08002168 */
Christian Zanderba2374f2015-06-10 09:41:45 -07002169 dma_pte_free_pagetable(domain, iov_pfn, end_pfn);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002170 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002171 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002172 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002173
David Woodhousee1605492009-06-29 11:17:38 +01002174 }
2175 /* We don't need lock here, nobody else
2176 * touches the iova range
2177 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002178 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002179 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002180 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002181 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2182 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002183 if (dumps) {
2184 dumps--;
2185 debug_dma_dump_mappings(NULL);
2186 }
2187 WARN_ON(1);
2188 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002189
2190 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2191
2192 BUG_ON(nr_pages < lvl_pages);
2193 BUG_ON(sg_res < lvl_pages);
2194
2195 nr_pages -= lvl_pages;
2196 iov_pfn += lvl_pages;
2197 phys_pfn += lvl_pages;
2198 pteval += lvl_pages * VTD_PAGE_SIZE;
2199 sg_res -= lvl_pages;
2200
2201 /* If the next PTE would be the first in a new page, then we
2202 need to flush the cache on the entries we've just written.
2203 And then we'll need to recalculate 'pte', so clear it and
2204 let it get set again in the if (!pte) block above.
2205
2206 If we're done (!nr_pages) we need to flush the cache too.
2207
2208 Also if we've been setting superpages, we may need to
2209 recalculate 'pte' and switch back to smaller pages for the
2210 end of the mapping, if the trailing size is not enough to
2211 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002212 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002213 if (!nr_pages || first_pte_in_page(pte) ||
2214 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002215 domain_flush_cache(domain, first_pte,
2216 (void *)pte - (void *)first_pte);
2217 pte = NULL;
2218 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002219
2220 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002221 sg = sg_next(sg);
2222 }
2223 return 0;
2224}
2225
David Woodhouse9051aa02009-06-29 12:30:54 +01002226static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2227 struct scatterlist *sg, unsigned long nr_pages,
2228 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002229{
David Woodhouse9051aa02009-06-29 12:30:54 +01002230 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2231}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002232
David Woodhouse9051aa02009-06-29 12:30:54 +01002233static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2234 unsigned long phys_pfn, unsigned long nr_pages,
2235 int prot)
2236{
2237 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002238}
2239
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002240static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002241{
Weidong Hanc7151a82008-12-08 22:51:37 +08002242 if (!iommu)
2243 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002244
2245 clear_context_table(iommu, bus, devfn);
2246 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002247 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002248 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002249}
2250
David Woodhouse109b9b02012-05-25 17:43:02 +01002251static inline void unlink_domain_info(struct device_domain_info *info)
2252{
2253 assert_spin_locked(&device_domain_lock);
2254 list_del(&info->link);
2255 list_del(&info->global);
2256 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002257 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002258}
2259
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002260static void domain_remove_dev_info(struct dmar_domain *domain)
2261{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002262 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002263 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002264
2265 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002266 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002267 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002268 spin_unlock_irqrestore(&device_domain_lock, flags);
2269}
2270
2271/*
2272 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002273 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002274 */
David Woodhouse1525a292014-03-06 16:19:30 +00002275static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002276{
2277 struct device_domain_info *info;
2278
2279 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002280 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002281 if (info)
2282 return info->domain;
2283 return NULL;
2284}
2285
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002286static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002287dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2288{
2289 struct device_domain_info *info;
2290
2291 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002292 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002293 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002294 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002295
2296 return NULL;
2297}
2298
Joerg Roedel5db31562015-07-22 12:40:43 +02002299static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2300 int bus, int devfn,
2301 struct device *dev,
2302 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002303{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002304 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002305 struct device_domain_info *info;
2306 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002307 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002308
2309 info = alloc_devinfo_mem();
2310 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002311 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002312
Jiang Liu745f2582014-02-19 14:07:26 +08002313 info->bus = bus;
2314 info->devfn = devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002315 info->ats_supported = info->pasid_supported = info->pri_supported = 0;
2316 info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
2317 info->ats_qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002318 info->dev = dev;
2319 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002320 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002321
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002322 if (dev && dev_is_pci(dev)) {
2323 struct pci_dev *pdev = to_pci_dev(info->dev);
2324
2325 if (ecap_dev_iotlb_support(iommu->ecap) &&
2326 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
2327 dmar_find_matched_atsr_unit(pdev))
2328 info->ats_supported = 1;
2329
2330 if (ecs_enabled(iommu)) {
2331 if (pasid_enabled(iommu)) {
2332 int features = pci_pasid_features(pdev);
2333 if (features >= 0)
2334 info->pasid_supported = features | 1;
2335 }
2336
2337 if (info->ats_supported && ecap_prs(iommu->ecap) &&
2338 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
2339 info->pri_supported = 1;
2340 }
2341 }
2342
Jiang Liu745f2582014-02-19 14:07:26 +08002343 spin_lock_irqsave(&device_domain_lock, flags);
2344 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002345 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002346
2347 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002348 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002349 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002350 if (info2) {
2351 found = info2->domain;
2352 info2->dev = dev;
2353 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002354 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002355
Jiang Liu745f2582014-02-19 14:07:26 +08002356 if (found) {
2357 spin_unlock_irqrestore(&device_domain_lock, flags);
2358 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002359 /* Caller must free the original domain */
2360 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002361 }
2362
Joerg Roedeld160aca2015-07-22 11:52:53 +02002363 spin_lock(&iommu->lock);
2364 ret = domain_attach_iommu(domain, iommu);
2365 spin_unlock(&iommu->lock);
2366
2367 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002368 spin_unlock_irqrestore(&device_domain_lock, flags);
2369 return NULL;
2370 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002371
David Woodhouseb718cd32014-03-09 13:11:33 -07002372 list_add(&info->link, &domain->devices);
2373 list_add(&info->global, &device_domain_list);
2374 if (dev)
2375 dev->archdata.iommu = info;
2376 spin_unlock_irqrestore(&device_domain_lock, flags);
2377
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002378 if (dev && domain_context_mapping(domain, dev)) {
2379 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002380 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002381 return NULL;
2382 }
2383
David Woodhouseb718cd32014-03-09 13:11:33 -07002384 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002385}
2386
Alex Williamson579305f2014-07-03 09:51:43 -06002387static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2388{
2389 *(u16 *)opaque = alias;
2390 return 0;
2391}
2392
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002393/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002394static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002395{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002396 struct device_domain_info *info = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002397 struct dmar_domain *domain, *tmp;
2398 struct intel_iommu *iommu;
Joerg Roedel08a7f452015-07-23 18:09:11 +02002399 u16 req_id, dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002400 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002401 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002402
David Woodhouse146922e2014-03-09 15:44:17 -07002403 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002404 if (domain)
2405 return domain;
2406
David Woodhouse146922e2014-03-09 15:44:17 -07002407 iommu = device_to_iommu(dev, &bus, &devfn);
2408 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002409 return NULL;
2410
Joerg Roedel08a7f452015-07-23 18:09:11 +02002411 req_id = ((u16)bus << 8) | devfn;
2412
Alex Williamson579305f2014-07-03 09:51:43 -06002413 if (dev_is_pci(dev)) {
2414 struct pci_dev *pdev = to_pci_dev(dev);
2415
2416 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2417
2418 spin_lock_irqsave(&device_domain_lock, flags);
2419 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2420 PCI_BUS_NUM(dma_alias),
2421 dma_alias & 0xff);
2422 if (info) {
2423 iommu = info->iommu;
2424 domain = info->domain;
2425 }
2426 spin_unlock_irqrestore(&device_domain_lock, flags);
2427
2428 /* DMA alias already has a domain, uses it */
2429 if (info)
2430 goto found_domain;
2431 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002432
David Woodhouse146922e2014-03-09 15:44:17 -07002433 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002434 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002435 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002436 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002437 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002438 domain_exit(domain);
2439 return NULL;
2440 }
2441
2442 /* register PCI DMA alias device */
Joerg Roedel08a7f452015-07-23 18:09:11 +02002443 if (req_id != dma_alias && dev_is_pci(dev)) {
Joerg Roedel5db31562015-07-22 12:40:43 +02002444 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2445 dma_alias & 0xff, NULL, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002446
2447 if (!tmp || tmp != domain) {
2448 domain_exit(domain);
2449 domain = tmp;
2450 }
2451
David Woodhouseb718cd32014-03-09 13:11:33 -07002452 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002453 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002454 }
2455
2456found_domain:
Joerg Roedel5db31562015-07-22 12:40:43 +02002457 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002458
2459 if (!tmp || tmp != domain) {
2460 domain_exit(domain);
2461 domain = tmp;
2462 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002463
2464 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002465}
2466
David Woodhouseb2132032009-06-26 18:50:28 +01002467static int iommu_domain_identity_map(struct dmar_domain *domain,
2468 unsigned long long start,
2469 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002470{
David Woodhousec5395d52009-06-28 16:35:56 +01002471 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2472 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002473
David Woodhousec5395d52009-06-28 16:35:56 +01002474 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2475 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002476 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002477 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002478 }
2479
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002480 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002481 /*
2482 * RMRR range might have overlap with physical memory range,
2483 * clear it first
2484 */
David Woodhousec5395d52009-06-28 16:35:56 +01002485 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002486
David Woodhousec5395d52009-06-28 16:35:56 +01002487 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2488 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002489 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002490}
2491
David Woodhouse0b9d9752014-03-09 15:48:15 -07002492static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002493 unsigned long long start,
2494 unsigned long long end)
2495{
2496 struct dmar_domain *domain;
2497 int ret;
2498
David Woodhouse0b9d9752014-03-09 15:48:15 -07002499 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002500 if (!domain)
2501 return -ENOMEM;
2502
David Woodhouse19943b02009-08-04 16:19:20 +01002503 /* For _hardware_ passthrough, don't bother. But for software
2504 passthrough, we do it anyway -- it may indicate a memory
2505 range which is reserved in E820, so which didn't get set
2506 up to start with in si_domain */
2507 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002508 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2509 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002510 return 0;
2511 }
2512
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002513 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2514 dev_name(dev), start, end);
2515
David Woodhouse5595b522009-12-02 09:21:55 +00002516 if (end < start) {
2517 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2518 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2519 dmi_get_system_info(DMI_BIOS_VENDOR),
2520 dmi_get_system_info(DMI_BIOS_VERSION),
2521 dmi_get_system_info(DMI_PRODUCT_VERSION));
2522 ret = -EIO;
2523 goto error;
2524 }
2525
David Woodhouse2ff729f2009-08-26 14:25:41 +01002526 if (end >> agaw_to_width(domain->agaw)) {
2527 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2528 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2529 agaw_to_width(domain->agaw),
2530 dmi_get_system_info(DMI_BIOS_VENDOR),
2531 dmi_get_system_info(DMI_BIOS_VERSION),
2532 dmi_get_system_info(DMI_PRODUCT_VERSION));
2533 ret = -EIO;
2534 goto error;
2535 }
David Woodhouse19943b02009-08-04 16:19:20 +01002536
David Woodhouseb2132032009-06-26 18:50:28 +01002537 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002538 if (ret)
2539 goto error;
2540
David Woodhouseb2132032009-06-26 18:50:28 +01002541 return 0;
2542
2543 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002544 domain_exit(domain);
2545 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002546}
2547
2548static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002549 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002550{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002551 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002552 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002553 return iommu_prepare_identity_map(dev, rmrr->base_address,
2554 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002555}
2556
Suresh Siddhad3f13812011-08-23 17:05:25 -07002557#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002558static inline void iommu_prepare_isa(void)
2559{
2560 struct pci_dev *pdev;
2561 int ret;
2562
2563 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2564 if (!pdev)
2565 return;
2566
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002567 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002568 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002569
2570 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002571 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002572
Yijing Wang9b27e822014-05-20 20:37:52 +08002573 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002574}
2575#else
2576static inline void iommu_prepare_isa(void)
2577{
2578 return;
2579}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002580#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002581
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002582static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002583
Matt Kraai071e1372009-08-23 22:30:22 -07002584static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002585{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002586 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002587
Jiang Liuab8dfe22014-07-11 14:19:27 +08002588 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002589 if (!si_domain)
2590 return -EFAULT;
2591
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002592 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2593 domain_exit(si_domain);
2594 return -EFAULT;
2595 }
2596
Joerg Roedel0dc79712015-07-21 15:40:06 +02002597 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002598
David Woodhouse19943b02009-08-04 16:19:20 +01002599 if (hw)
2600 return 0;
2601
David Woodhousec7ab48d2009-06-26 19:10:36 +01002602 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002603 unsigned long start_pfn, end_pfn;
2604 int i;
2605
2606 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2607 ret = iommu_domain_identity_map(si_domain,
2608 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2609 if (ret)
2610 return ret;
2611 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002612 }
2613
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002614 return 0;
2615}
2616
David Woodhouse9b226622014-03-09 14:03:28 -07002617static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002618{
2619 struct device_domain_info *info;
2620
2621 if (likely(!iommu_identity_mapping))
2622 return 0;
2623
David Woodhouse9b226622014-03-09 14:03:28 -07002624 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002625 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2626 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002627
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002628 return 0;
2629}
2630
Joerg Roedel28ccce02015-07-21 14:45:31 +02002631static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002632{
David Woodhouse0ac72662014-03-09 13:19:22 -07002633 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002634 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002635 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002636
David Woodhouse5913c9b2014-03-09 16:27:31 -07002637 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002638 if (!iommu)
2639 return -ENODEV;
2640
Joerg Roedel5db31562015-07-22 12:40:43 +02002641 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002642 if (ndomain != domain)
2643 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002644
2645 return 0;
2646}
2647
David Woodhouse0b9d9752014-03-09 15:48:15 -07002648static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002649{
2650 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002651 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002652 int i;
2653
Jiang Liu0e242612014-02-19 14:07:34 +08002654 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002655 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002656 /*
2657 * Return TRUE if this RMRR contains the device that
2658 * is passed in.
2659 */
2660 for_each_active_dev_scope(rmrr->devices,
2661 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002662 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002663 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002664 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002665 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002666 }
Jiang Liu0e242612014-02-19 14:07:34 +08002667 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002668 return false;
2669}
2670
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002671/*
2672 * There are a couple cases where we need to restrict the functionality of
2673 * devices associated with RMRRs. The first is when evaluating a device for
2674 * identity mapping because problems exist when devices are moved in and out
2675 * of domains and their respective RMRR information is lost. This means that
2676 * a device with associated RMRRs will never be in a "passthrough" domain.
2677 * The second is use of the device through the IOMMU API. This interface
2678 * expects to have full control of the IOVA space for the device. We cannot
2679 * satisfy both the requirement that RMRR access is maintained and have an
2680 * unencumbered IOVA space. We also have no ability to quiesce the device's
2681 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2682 * We therefore prevent devices associated with an RMRR from participating in
2683 * the IOMMU API, which eliminates them from device assignment.
2684 *
2685 * In both cases we assume that PCI USB devices with RMRRs have them largely
2686 * for historical reasons and that the RMRR space is not actively used post
2687 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002688 *
2689 * The same exception is made for graphics devices, with the requirement that
2690 * any use of the RMRR regions will be torn down before assigning the device
2691 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002692 */
2693static bool device_is_rmrr_locked(struct device *dev)
2694{
2695 if (!device_has_rmrr(dev))
2696 return false;
2697
2698 if (dev_is_pci(dev)) {
2699 struct pci_dev *pdev = to_pci_dev(dev);
2700
David Woodhouse18436af2015-03-25 15:05:47 +00002701 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002702 return false;
2703 }
2704
2705 return true;
2706}
2707
David Woodhouse3bdb2592014-03-09 16:03:08 -07002708static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002709{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002710
David Woodhouse3bdb2592014-03-09 16:03:08 -07002711 if (dev_is_pci(dev)) {
2712 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002713
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002714 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002715 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002716
David Woodhouse3bdb2592014-03-09 16:03:08 -07002717 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2718 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002719
David Woodhouse3bdb2592014-03-09 16:03:08 -07002720 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2721 return 1;
2722
2723 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2724 return 0;
2725
2726 /*
2727 * We want to start off with all devices in the 1:1 domain, and
2728 * take them out later if we find they can't access all of memory.
2729 *
2730 * However, we can't do this for PCI devices behind bridges,
2731 * because all PCI devices behind the same bridge will end up
2732 * with the same source-id on their transactions.
2733 *
2734 * Practically speaking, we can't change things around for these
2735 * devices at run-time, because we can't be sure there'll be no
2736 * DMA transactions in flight for any of their siblings.
2737 *
2738 * So PCI devices (unless they're on the root bus) as well as
2739 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2740 * the 1:1 domain, just in _case_ one of their siblings turns out
2741 * not to be able to map all of memory.
2742 */
2743 if (!pci_is_pcie(pdev)) {
2744 if (!pci_is_root_bus(pdev->bus))
2745 return 0;
2746 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2747 return 0;
2748 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2749 return 0;
2750 } else {
2751 if (device_has_rmrr(dev))
2752 return 0;
2753 }
David Woodhouse6941af22009-07-04 18:24:27 +01002754
David Woodhouse3dfc8132009-07-04 19:11:08 +01002755 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002756 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002757 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002758 * take them out of the 1:1 domain later.
2759 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002760 if (!startup) {
2761 /*
2762 * If the device's dma_mask is less than the system's memory
2763 * size then this is not a candidate for identity mapping.
2764 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002765 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002766
David Woodhouse3bdb2592014-03-09 16:03:08 -07002767 if (dev->coherent_dma_mask &&
2768 dev->coherent_dma_mask < dma_mask)
2769 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002770
David Woodhouse3bdb2592014-03-09 16:03:08 -07002771 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002772 }
David Woodhouse6941af22009-07-04 18:24:27 +01002773
2774 return 1;
2775}
2776
David Woodhousecf04eee2014-03-21 16:49:04 +00002777static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2778{
2779 int ret;
2780
2781 if (!iommu_should_identity_map(dev, 1))
2782 return 0;
2783
Joerg Roedel28ccce02015-07-21 14:45:31 +02002784 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002785 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002786 pr_info("%s identity mapping for device %s\n",
2787 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002788 else if (ret == -ENODEV)
2789 /* device not associated with an iommu */
2790 ret = 0;
2791
2792 return ret;
2793}
2794
2795
Matt Kraai071e1372009-08-23 22:30:22 -07002796static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002797{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002798 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002799 struct dmar_drhd_unit *drhd;
2800 struct intel_iommu *iommu;
2801 struct device *dev;
2802 int i;
2803 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002804
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002805 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002806 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2807 if (ret)
2808 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002809 }
2810
David Woodhousecf04eee2014-03-21 16:49:04 +00002811 for_each_active_iommu(iommu, drhd)
2812 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2813 struct acpi_device_physical_node *pn;
2814 struct acpi_device *adev;
2815
2816 if (dev->bus != &acpi_bus_type)
2817 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002818
David Woodhousecf04eee2014-03-21 16:49:04 +00002819 adev= to_acpi_device(dev);
2820 mutex_lock(&adev->physical_node_lock);
2821 list_for_each_entry(pn, &adev->physical_node_list, node) {
2822 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2823 if (ret)
2824 break;
2825 }
2826 mutex_unlock(&adev->physical_node_lock);
2827 if (ret)
2828 return ret;
2829 }
2830
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002831 return 0;
2832}
2833
Jiang Liuffebeb42014-11-09 22:48:02 +08002834static void intel_iommu_init_qi(struct intel_iommu *iommu)
2835{
2836 /*
2837 * Start from the sane iommu hardware state.
2838 * If the queued invalidation is already initialized by us
2839 * (for example, while enabling interrupt-remapping) then
2840 * we got the things already rolling from a sane state.
2841 */
2842 if (!iommu->qi) {
2843 /*
2844 * Clear any previous faults.
2845 */
2846 dmar_fault(-1, iommu);
2847 /*
2848 * Disable queued invalidation if supported and already enabled
2849 * before OS handover.
2850 */
2851 dmar_disable_qi(iommu);
2852 }
2853
2854 if (dmar_enable_qi(iommu)) {
2855 /*
2856 * Queued Invalidate not enabled, use Register Based Invalidate
2857 */
2858 iommu->flush.flush_context = __iommu_flush_context;
2859 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002860 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002861 iommu->name);
2862 } else {
2863 iommu->flush.flush_context = qi_flush_context;
2864 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002865 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002866 }
2867}
2868
Joerg Roedel091d42e2015-06-12 11:56:10 +02002869static int copy_context_table(struct intel_iommu *iommu,
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002870 struct root_entry __iomem *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02002871 struct context_entry **tbl,
2872 int bus, bool ext)
2873{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002874 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002875 struct context_entry __iomem *old_ce = NULL;
2876 struct context_entry *new_ce = NULL, ce;
2877 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002878 phys_addr_t old_ce_phys;
2879
2880 tbl_idx = ext ? bus * 2 : bus;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002881 memcpy_fromio(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002882
2883 for (devfn = 0; devfn < 256; devfn++) {
2884 /* First calculate the correct index */
2885 idx = (ext ? devfn * 2 : devfn) % 256;
2886
2887 if (idx == 0) {
2888 /* First save what we may have and clean up */
2889 if (new_ce) {
2890 tbl[tbl_idx] = new_ce;
2891 __iommu_flush_cache(iommu, new_ce,
2892 VTD_PAGE_SIZE);
2893 pos = 1;
2894 }
2895
2896 if (old_ce)
2897 iounmap(old_ce);
2898
2899 ret = 0;
2900 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002901 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02002902 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002903 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02002904
2905 if (!old_ce_phys) {
2906 if (ext && devfn == 0) {
2907 /* No LCTP, try UCTP */
2908 devfn = 0x7f;
2909 continue;
2910 } else {
2911 goto out;
2912 }
2913 }
2914
2915 ret = -ENOMEM;
2916 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2917 if (!old_ce)
2918 goto out;
2919
2920 new_ce = alloc_pgtable_page(iommu->node);
2921 if (!new_ce)
2922 goto out_unmap;
2923
2924 ret = 0;
2925 }
2926
2927 /* Now copy the context entry */
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002928 memcpy_fromio(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002929
Joerg Roedelcf484d02015-06-12 12:21:46 +02002930 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002931 continue;
2932
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002933 did = context_domain_id(&ce);
2934 if (did >= 0 && did < cap_ndoms(iommu->cap))
2935 set_bit(did, iommu->domain_ids);
2936
Joerg Roedelcf484d02015-06-12 12:21:46 +02002937 /*
2938 * We need a marker for copied context entries. This
2939 * marker needs to work for the old format as well as
2940 * for extended context entries.
2941 *
2942 * Bit 67 of the context entry is used. In the old
2943 * format this bit is available to software, in the
2944 * extended format it is the PGE bit, but PGE is ignored
2945 * by HW if PASIDs are disabled (and thus still
2946 * available).
2947 *
2948 * So disable PASIDs first and then mark the entry
2949 * copied. This means that we don't copy PASID
2950 * translations from the old kernel, but this is fine as
2951 * faults there are not fatal.
2952 */
2953 context_clear_pasid_enable(&ce);
2954 context_set_copied(&ce);
2955
Joerg Roedel091d42e2015-06-12 11:56:10 +02002956 new_ce[idx] = ce;
2957 }
2958
2959 tbl[tbl_idx + pos] = new_ce;
2960
2961 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2962
2963out_unmap:
2964 iounmap(old_ce);
2965
2966out:
2967 return ret;
2968}
2969
2970static int copy_translation_tables(struct intel_iommu *iommu)
2971{
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002972 struct root_entry __iomem *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002973 struct context_entry **ctxt_tbls;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002974 phys_addr_t old_rt_phys;
2975 int ctxt_table_entries;
2976 unsigned long flags;
2977 u64 rtaddr_reg;
2978 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002979 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002980
2981 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2982 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002983 new_ext = !!ecap_ecs(iommu->ecap);
2984
2985 /*
2986 * The RTT bit can only be changed when translation is disabled,
2987 * but disabling translation means to open a window for data
2988 * corruption. So bail out and don't copy anything if we would
2989 * have to change the bit.
2990 */
2991 if (new_ext != ext)
2992 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002993
2994 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2995 if (!old_rt_phys)
2996 return -EINVAL;
2997
2998 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2999 if (!old_rt)
3000 return -ENOMEM;
3001
3002 /* This is too big for the stack - allocate it from slab */
3003 ctxt_table_entries = ext ? 512 : 256;
3004 ret = -ENOMEM;
3005 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
3006 if (!ctxt_tbls)
3007 goto out_unmap;
3008
3009 for (bus = 0; bus < 256; bus++) {
3010 ret = copy_context_table(iommu, &old_rt[bus],
3011 ctxt_tbls, bus, ext);
3012 if (ret) {
3013 pr_err("%s: Failed to copy context table for bus %d\n",
3014 iommu->name, bus);
3015 continue;
3016 }
3017 }
3018
3019 spin_lock_irqsave(&iommu->lock, flags);
3020
3021 /* Context tables are copied, now write them to the root_entry table */
3022 for (bus = 0; bus < 256; bus++) {
3023 int idx = ext ? bus * 2 : bus;
3024 u64 val;
3025
3026 if (ctxt_tbls[idx]) {
3027 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3028 iommu->root_entry[bus].lo = val;
3029 }
3030
3031 if (!ext || !ctxt_tbls[idx + 1])
3032 continue;
3033
3034 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3035 iommu->root_entry[bus].hi = val;
3036 }
3037
3038 spin_unlock_irqrestore(&iommu->lock, flags);
3039
3040 kfree(ctxt_tbls);
3041
3042 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3043
3044 ret = 0;
3045
3046out_unmap:
3047 iounmap(old_rt);
3048
3049 return ret;
3050}
3051
Joseph Cihulab7792602011-05-03 00:08:37 -07003052static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003053{
3054 struct dmar_drhd_unit *drhd;
3055 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003056 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003057 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003058 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003059 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003060
3061 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003062 * for each drhd
3063 * allocate root
3064 * initialize and program root entry to not present
3065 * endfor
3066 */
3067 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003068 /*
3069 * lock not needed as this is only incremented in the single
3070 * threaded kernel __init code path all other access are read
3071 * only
3072 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003073 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003074 g_num_of_iommus++;
3075 continue;
3076 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003077 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003078 }
3079
Jiang Liuffebeb42014-11-09 22:48:02 +08003080 /* Preallocate enough resources for IOMMU hot-addition */
3081 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3082 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3083
Weidong Hand9630fe2008-12-08 11:06:32 +08003084 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3085 GFP_KERNEL);
3086 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003087 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003088 ret = -ENOMEM;
3089 goto error;
3090 }
3091
mark gross80b20dd2008-04-18 13:53:58 -07003092 deferred_flush = kzalloc(g_num_of_iommus *
3093 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3094 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003095 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003096 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003097 }
3098
Jiang Liu7c919772014-01-06 14:18:18 +08003099 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003100 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003101
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003102 intel_iommu_init_qi(iommu);
3103
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003104 ret = iommu_init_domains(iommu);
3105 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003106 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003107
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003108 init_translation_status(iommu);
3109
Joerg Roedel091d42e2015-06-12 11:56:10 +02003110 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3111 iommu_disable_translation(iommu);
3112 clear_translation_pre_enabled(iommu);
3113 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3114 iommu->name);
3115 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003116
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003117 /*
3118 * TBD:
3119 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003120 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003121 */
3122 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003123 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003124 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003125
Joerg Roedel091d42e2015-06-12 11:56:10 +02003126 if (translation_pre_enabled(iommu)) {
3127 pr_info("Translation already enabled - trying to copy translation structures\n");
3128
3129 ret = copy_translation_tables(iommu);
3130 if (ret) {
3131 /*
3132 * We found the IOMMU with translation
3133 * enabled - but failed to copy over the
3134 * old root-entry table. Try to proceed
3135 * by disabling translation now and
3136 * allocating a clean root-entry table.
3137 * This might cause DMAR faults, but
3138 * probably the dump will still succeed.
3139 */
3140 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3141 iommu->name);
3142 iommu_disable_translation(iommu);
3143 clear_translation_pre_enabled(iommu);
3144 } else {
3145 pr_info("Copied translation tables from previous kernel for %s\n",
3146 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003147 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003148 }
3149 }
3150
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003151 iommu_flush_write_buffer(iommu);
3152 iommu_set_root_entry(iommu);
3153 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3154 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3155
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003156 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003157 hw_pass_through = 0;
David Woodhouse8a94ade2015-03-24 14:54:56 +00003158#ifdef CONFIG_INTEL_IOMMU_SVM
3159 if (pasid_enabled(iommu))
3160 intel_svm_alloc_pasid_tables(iommu);
3161#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003162 }
3163
David Woodhouse19943b02009-08-04 16:19:20 +01003164 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003165 iommu_identity_mapping |= IDENTMAP_ALL;
3166
Suresh Siddhad3f13812011-08-23 17:05:25 -07003167#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003168 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003169#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003170
Joerg Roedel86080cc2015-06-12 12:27:16 +02003171 if (iommu_identity_mapping) {
3172 ret = si_domain_init(hw_pass_through);
3173 if (ret)
3174 goto free_iommu;
3175 }
3176
David Woodhousee0fc7e02009-09-30 09:12:17 -07003177 check_tylersburg_isoch();
3178
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003179 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003180 * If we copied translations from a previous kernel in the kdump
3181 * case, we can not assign the devices to domains now, as that
3182 * would eliminate the old mappings. So skip this part and defer
3183 * the assignment to device driver initialization time.
3184 */
3185 if (copied_tables)
3186 goto domains_done;
3187
3188 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003189 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003190 * identity mappings for rmrr, gfx, and isa and may fall back to static
3191 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003192 */
David Woodhouse19943b02009-08-04 16:19:20 +01003193 if (iommu_identity_mapping) {
3194 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3195 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003196 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003197 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003198 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003199 }
David Woodhouse19943b02009-08-04 16:19:20 +01003200 /*
3201 * For each rmrr
3202 * for each dev attached to rmrr
3203 * do
3204 * locate drhd for dev, alloc domain for dev
3205 * allocate free domain
3206 * allocate page table entries for rmrr
3207 * if context not allocated for bus
3208 * allocate and init context
3209 * set present in root table for this bus
3210 * init context with domain, translation etc
3211 * endfor
3212 * endfor
3213 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003214 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003215 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003216 /* some BIOS lists non-exist devices in DMAR table. */
3217 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003218 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003219 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003220 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003221 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003222 }
3223 }
3224
3225 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003226
Joerg Roedela87f4912015-06-12 12:32:54 +02003227domains_done:
3228
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003229 /*
3230 * for each drhd
3231 * enable fault log
3232 * global invalidate context cache
3233 * global invalidate iotlb
3234 * enable translation
3235 */
Jiang Liu7c919772014-01-06 14:18:18 +08003236 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003237 if (drhd->ignored) {
3238 /*
3239 * we always have to disable PMRs or DMA may fail on
3240 * this device
3241 */
3242 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003243 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003244 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003245 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003246
3247 iommu_flush_write_buffer(iommu);
3248
David Woodhousea222a7f2015-10-07 23:35:18 +01003249#ifdef CONFIG_INTEL_IOMMU_SVM
3250 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
3251 ret = intel_svm_enable_prq(iommu);
3252 if (ret)
3253 goto free_iommu;
3254 }
3255#endif
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003256 ret = dmar_set_interrupt(iommu);
3257 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003258 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003259
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003260 if (!translation_pre_enabled(iommu))
3261 iommu_enable_translation(iommu);
3262
David Woodhouseb94996c2009-09-19 15:28:12 -07003263 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003264 }
3265
3266 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003267
3268free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003269 for_each_active_iommu(iommu, drhd) {
3270 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003271 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003272 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003273 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003274free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003275 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003276error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003277 return ret;
3278}
3279
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003280/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003281static struct iova *intel_alloc_iova(struct device *dev,
3282 struct dmar_domain *domain,
3283 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003284{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003285 struct iova *iova = NULL;
3286
David Woodhouse875764d2009-06-28 21:20:51 +01003287 /* Restrict dma_mask to the width that the iommu can handle */
3288 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003289 /* Ensure we reserve the whole size-aligned region */
3290 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003291
3292 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003293 /*
3294 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003295 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003296 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003297 */
David Woodhouse875764d2009-06-28 21:20:51 +01003298 iova = alloc_iova(&domain->iovad, nrpages,
3299 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3300 if (iova)
3301 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003302 }
David Woodhouse875764d2009-06-28 21:20:51 +01003303 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3304 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003305 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003306 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003307 return NULL;
3308 }
3309
3310 return iova;
3311}
3312
David Woodhoused4b709f2014-03-09 16:07:40 -07003313static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003314{
3315 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003316
David Woodhoused4b709f2014-03-09 16:07:40 -07003317 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003318 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003319 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003320 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003321 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003322 }
3323
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003324 return domain;
3325}
3326
David Woodhoused4b709f2014-03-09 16:07:40 -07003327static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003328{
3329 struct device_domain_info *info;
3330
3331 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003332 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003333 if (likely(info))
3334 return info->domain;
3335
3336 return __get_valid_domain_for_dev(dev);
3337}
3338
David Woodhouseecb509e2014-03-09 16:29:55 -07003339/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003340static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003341{
3342 int found;
3343
David Woodhouse3d891942014-03-06 15:59:26 +00003344 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003345 return 1;
3346
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003347 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003348 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003349
David Woodhouse9b226622014-03-09 14:03:28 -07003350 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003351 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003352 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003353 return 1;
3354 else {
3355 /*
3356 * 32 bit DMA is removed from si_domain and fall back
3357 * to non-identity mapping.
3358 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003359 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003360 pr_info("32bit %s uses non-identity mapping\n",
3361 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003362 return 0;
3363 }
3364 } else {
3365 /*
3366 * In case of a detached 64 bit DMA device from vm, the device
3367 * is put into si_domain for identity mapping.
3368 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003369 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003370 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003371 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003372 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003373 pr_info("64bit %s uses identity mapping\n",
3374 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003375 return 1;
3376 }
3377 }
3378 }
3379
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003380 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003381}
3382
David Woodhouse5040a912014-03-09 16:14:00 -07003383static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003384 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003385{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003386 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003387 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003388 struct iova *iova;
3389 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003390 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003391 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003392 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003393
3394 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003395
David Woodhouse5040a912014-03-09 16:14:00 -07003396 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003397 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003398
David Woodhouse5040a912014-03-09 16:14:00 -07003399 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003400 if (!domain)
3401 return 0;
3402
Weidong Han8c11e792008-12-08 15:29:22 +08003403 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003404 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003405
David Woodhouse5040a912014-03-09 16:14:00 -07003406 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003407 if (!iova)
3408 goto error;
3409
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003410 /*
3411 * Check if DMAR supports zero-length reads on write only
3412 * mappings..
3413 */
3414 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003415 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003416 prot |= DMA_PTE_READ;
3417 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3418 prot |= DMA_PTE_WRITE;
3419 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003420 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003421 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003422 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003423 * is not a big problem
3424 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003425 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003426 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003427 if (ret)
3428 goto error;
3429
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003430 /* it's a non-present to present mapping. Only flush if caching mode */
3431 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003432 iommu_flush_iotlb_psi(iommu, domain,
3433 mm_to_dma_pfn(iova->pfn_lo),
3434 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003435 else
Weidong Han8c11e792008-12-08 15:29:22 +08003436 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003437
David Woodhouse03d6a242009-06-28 15:33:46 +01003438 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3439 start_paddr += paddr & ~PAGE_MASK;
3440 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003441
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003442error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003443 if (iova)
3444 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003445 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003446 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003447 return 0;
3448}
3449
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003450static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3451 unsigned long offset, size_t size,
3452 enum dma_data_direction dir,
3453 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003454{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003455 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003456 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003457}
3458
mark gross5e0d2a62008-03-04 15:22:08 -08003459static void flush_unmaps(void)
3460{
mark gross80b20dd2008-04-18 13:53:58 -07003461 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003462
mark gross5e0d2a62008-03-04 15:22:08 -08003463 timer_on = 0;
3464
3465 /* just flush them all */
3466 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003467 struct intel_iommu *iommu = g_iommus[i];
3468 if (!iommu)
3469 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003470
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003471 if (!deferred_flush[i].next)
3472 continue;
3473
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003474 /* In caching mode, global flushes turn emulation expensive */
3475 if (!cap_caching_mode(iommu->cap))
3476 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003477 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003478 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003479 unsigned long mask;
3480 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003481 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003482
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003483 /* On real hardware multiple invalidations are expensive */
3484 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003485 iommu_flush_iotlb_psi(iommu, domain,
Jiang Liua156ef92014-07-11 14:19:36 +08003486 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003487 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003488 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003489 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003490 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3491 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3492 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003493 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003494 if (deferred_flush[i].freelist[j])
3495 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003496 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003497 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003498 }
3499
mark gross5e0d2a62008-03-04 15:22:08 -08003500 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003501}
3502
3503static void flush_unmaps_timeout(unsigned long data)
3504{
mark gross80b20dd2008-04-18 13:53:58 -07003505 unsigned long flags;
3506
3507 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003508 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003509 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003510}
3511
David Woodhouseea8ea462014-03-05 17:09:32 +00003512static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003513{
3514 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003515 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003516 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003517
3518 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003519 if (list_size == HIGH_WATER_MARK)
3520 flush_unmaps();
3521
Weidong Han8c11e792008-12-08 15:29:22 +08003522 iommu = domain_get_iommu(dom);
3523 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003524
mark gross80b20dd2008-04-18 13:53:58 -07003525 next = deferred_flush[iommu_id].next;
3526 deferred_flush[iommu_id].domain[next] = dom;
3527 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003528 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003529 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003530
3531 if (!timer_on) {
3532 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3533 timer_on = 1;
3534 }
3535 list_size++;
3536 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3537}
3538
Jiang Liud41a4ad2014-07-11 14:19:34 +08003539static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003540{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003541 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003542 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003543 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003544 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003545 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003546
David Woodhouse73676832009-07-04 14:08:36 +01003547 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003548 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003549
David Woodhouse1525a292014-03-06 16:19:30 +00003550 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003551 BUG_ON(!domain);
3552
Weidong Han8c11e792008-12-08 15:29:22 +08003553 iommu = domain_get_iommu(domain);
3554
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003555 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003556 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3557 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003558 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003559
David Woodhoused794dc92009-06-28 00:27:49 +01003560 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3561 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003562
David Woodhoused794dc92009-06-28 00:27:49 +01003563 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003564 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003565
David Woodhouseea8ea462014-03-05 17:09:32 +00003566 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003567
mark gross5e0d2a62008-03-04 15:22:08 -08003568 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003569 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003570 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003571 /* free iova */
3572 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003573 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003574 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003575 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003576 /*
3577 * queue up the release of the unmap to save the 1/6th of the
3578 * cpu used up by the iotlb flush operation...
3579 */
mark gross5e0d2a62008-03-04 15:22:08 -08003580 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003581}
3582
Jiang Liud41a4ad2014-07-11 14:19:34 +08003583static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3584 size_t size, enum dma_data_direction dir,
3585 struct dma_attrs *attrs)
3586{
3587 intel_unmap(dev, dev_addr);
3588}
3589
David Woodhouse5040a912014-03-09 16:14:00 -07003590static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003591 dma_addr_t *dma_handle, gfp_t flags,
3592 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003593{
Akinobu Mita36746432014-06-04 16:06:51 -07003594 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003595 int order;
3596
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003597 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003598 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003599
David Woodhouse5040a912014-03-09 16:14:00 -07003600 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003601 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003602 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3603 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003604 flags |= GFP_DMA;
3605 else
3606 flags |= GFP_DMA32;
3607 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003608
Akinobu Mita36746432014-06-04 16:06:51 -07003609 if (flags & __GFP_WAIT) {
3610 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003611
Akinobu Mita36746432014-06-04 16:06:51 -07003612 page = dma_alloc_from_contiguous(dev, count, order);
3613 if (page && iommu_no_mapping(dev) &&
3614 page_to_phys(page) + size > dev->coherent_dma_mask) {
3615 dma_release_from_contiguous(dev, page, count);
3616 page = NULL;
3617 }
3618 }
3619
3620 if (!page)
3621 page = alloc_pages(flags, order);
3622 if (!page)
3623 return NULL;
3624 memset(page_address(page), 0, size);
3625
3626 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003627 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003628 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003629 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003630 return page_address(page);
3631 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3632 __free_pages(page, order);
3633
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003634 return NULL;
3635}
3636
David Woodhouse5040a912014-03-09 16:14:00 -07003637static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003638 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003639{
3640 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003641 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003642
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003643 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003644 order = get_order(size);
3645
Jiang Liud41a4ad2014-07-11 14:19:34 +08003646 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003647 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3648 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003649}
3650
David Woodhouse5040a912014-03-09 16:14:00 -07003651static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003652 int nelems, enum dma_data_direction dir,
3653 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003654{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003655 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003656}
3657
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003658static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003659 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003660{
3661 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003662 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003663
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003664 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003665 BUG_ON(!sg_page(sg));
Dan Williamsdb0fa0c2015-08-17 08:13:26 -06003666 sg->dma_address = sg_phys(sg);
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003667 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003668 }
3669 return nelems;
3670}
3671
David Woodhouse5040a912014-03-09 16:14:00 -07003672static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003673 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003674{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003675 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003676 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003677 size_t size = 0;
3678 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003679 struct iova *iova = NULL;
3680 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003681 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003682 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003683 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003684
3685 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003686 if (iommu_no_mapping(dev))
3687 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003688
David Woodhouse5040a912014-03-09 16:14:00 -07003689 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003690 if (!domain)
3691 return 0;
3692
Weidong Han8c11e792008-12-08 15:29:22 +08003693 iommu = domain_get_iommu(domain);
3694
David Woodhouseb536d242009-06-28 14:49:31 +01003695 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003696 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003697
David Woodhouse5040a912014-03-09 16:14:00 -07003698 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3699 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003700 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003701 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003702 return 0;
3703 }
3704
3705 /*
3706 * Check if DMAR supports zero-length reads on write only
3707 * mappings..
3708 */
3709 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003710 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003711 prot |= DMA_PTE_READ;
3712 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3713 prot |= DMA_PTE_WRITE;
3714
David Woodhouseb536d242009-06-28 14:49:31 +01003715 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003716
Fenghua Yuf5329592009-08-04 15:09:37 -07003717 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003718 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003719 dma_pte_free_pagetable(domain, start_vpfn,
3720 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003721 __free_iova(&domain->iovad, iova);
3722 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003723 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003724
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003725 /* it's a non-present to present mapping. Only flush if caching mode */
3726 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003727 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003728 else
Weidong Han8c11e792008-12-08 15:29:22 +08003729 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003730
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003731 return nelems;
3732}
3733
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003734static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3735{
3736 return !dma_addr;
3737}
3738
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003739struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003740 .alloc = intel_alloc_coherent,
3741 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003742 .map_sg = intel_map_sg,
3743 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003744 .map_page = intel_map_page,
3745 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003746 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003747};
3748
3749static inline int iommu_domain_cache_init(void)
3750{
3751 int ret = 0;
3752
3753 iommu_domain_cache = kmem_cache_create("iommu_domain",
3754 sizeof(struct dmar_domain),
3755 0,
3756 SLAB_HWCACHE_ALIGN,
3757
3758 NULL);
3759 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003760 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003761 ret = -ENOMEM;
3762 }
3763
3764 return ret;
3765}
3766
3767static inline int iommu_devinfo_cache_init(void)
3768{
3769 int ret = 0;
3770
3771 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3772 sizeof(struct device_domain_info),
3773 0,
3774 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003775 NULL);
3776 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003777 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003778 ret = -ENOMEM;
3779 }
3780
3781 return ret;
3782}
3783
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003784static int __init iommu_init_mempool(void)
3785{
3786 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003787 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003788 if (ret)
3789 return ret;
3790
3791 ret = iommu_domain_cache_init();
3792 if (ret)
3793 goto domain_error;
3794
3795 ret = iommu_devinfo_cache_init();
3796 if (!ret)
3797 return ret;
3798
3799 kmem_cache_destroy(iommu_domain_cache);
3800domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003801 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003802
3803 return -ENOMEM;
3804}
3805
3806static void __init iommu_exit_mempool(void)
3807{
3808 kmem_cache_destroy(iommu_devinfo_cache);
3809 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003810 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003811}
3812
Dan Williams556ab452010-07-23 15:47:56 -07003813static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3814{
3815 struct dmar_drhd_unit *drhd;
3816 u32 vtbar;
3817 int rc;
3818
3819 /* We know that this device on this chipset has its own IOMMU.
3820 * If we find it under a different IOMMU, then the BIOS is lying
3821 * to us. Hope that the IOMMU for this device is actually
3822 * disabled, and it needs no translation...
3823 */
3824 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3825 if (rc) {
3826 /* "can't" happen */
3827 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3828 return;
3829 }
3830 vtbar &= 0xffff0000;
3831
3832 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3833 drhd = dmar_find_matched_drhd_unit(pdev);
3834 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3835 TAINT_FIRMWARE_WORKAROUND,
3836 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3837 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3838}
3839DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3840
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003841static void __init init_no_remapping_devices(void)
3842{
3843 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003844 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003845 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003846
3847 for_each_drhd_unit(drhd) {
3848 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003849 for_each_active_dev_scope(drhd->devices,
3850 drhd->devices_cnt, i, dev)
3851 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003852 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003853 if (i == drhd->devices_cnt)
3854 drhd->ignored = 1;
3855 }
3856 }
3857
Jiang Liu7c919772014-01-06 14:18:18 +08003858 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003859 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003860 continue;
3861
Jiang Liub683b232014-02-19 14:07:32 +08003862 for_each_active_dev_scope(drhd->devices,
3863 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003864 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003865 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003866 if (i < drhd->devices_cnt)
3867 continue;
3868
David Woodhousec0771df2011-10-14 20:59:46 +01003869 /* This IOMMU has *only* gfx devices. Either bypass it or
3870 set the gfx_mapped flag, as appropriate */
3871 if (dmar_map_gfx) {
3872 intel_iommu_gfx_mapped = 1;
3873 } else {
3874 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003875 for_each_active_dev_scope(drhd->devices,
3876 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003877 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003878 }
3879 }
3880}
3881
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003882#ifdef CONFIG_SUSPEND
3883static int init_iommu_hw(void)
3884{
3885 struct dmar_drhd_unit *drhd;
3886 struct intel_iommu *iommu = NULL;
3887
3888 for_each_active_iommu(iommu, drhd)
3889 if (iommu->qi)
3890 dmar_reenable_qi(iommu);
3891
Joseph Cihulab7792602011-05-03 00:08:37 -07003892 for_each_iommu(iommu, drhd) {
3893 if (drhd->ignored) {
3894 /*
3895 * we always have to disable PMRs or DMA may fail on
3896 * this device
3897 */
3898 if (force_on)
3899 iommu_disable_protect_mem_regions(iommu);
3900 continue;
3901 }
3902
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003903 iommu_flush_write_buffer(iommu);
3904
3905 iommu_set_root_entry(iommu);
3906
3907 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003908 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003909 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3910 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003911 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003912 }
3913
3914 return 0;
3915}
3916
3917static void iommu_flush_all(void)
3918{
3919 struct dmar_drhd_unit *drhd;
3920 struct intel_iommu *iommu;
3921
3922 for_each_active_iommu(iommu, drhd) {
3923 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003924 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003925 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003926 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003927 }
3928}
3929
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003930static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003931{
3932 struct dmar_drhd_unit *drhd;
3933 struct intel_iommu *iommu = NULL;
3934 unsigned long flag;
3935
3936 for_each_active_iommu(iommu, drhd) {
3937 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3938 GFP_ATOMIC);
3939 if (!iommu->iommu_state)
3940 goto nomem;
3941 }
3942
3943 iommu_flush_all();
3944
3945 for_each_active_iommu(iommu, drhd) {
3946 iommu_disable_translation(iommu);
3947
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003948 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003949
3950 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3951 readl(iommu->reg + DMAR_FECTL_REG);
3952 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3953 readl(iommu->reg + DMAR_FEDATA_REG);
3954 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3955 readl(iommu->reg + DMAR_FEADDR_REG);
3956 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3957 readl(iommu->reg + DMAR_FEUADDR_REG);
3958
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003959 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003960 }
3961 return 0;
3962
3963nomem:
3964 for_each_active_iommu(iommu, drhd)
3965 kfree(iommu->iommu_state);
3966
3967 return -ENOMEM;
3968}
3969
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003970static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003971{
3972 struct dmar_drhd_unit *drhd;
3973 struct intel_iommu *iommu = NULL;
3974 unsigned long flag;
3975
3976 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003977 if (force_on)
3978 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3979 else
3980 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003981 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003982 }
3983
3984 for_each_active_iommu(iommu, drhd) {
3985
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003986 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003987
3988 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3989 iommu->reg + DMAR_FECTL_REG);
3990 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3991 iommu->reg + DMAR_FEDATA_REG);
3992 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3993 iommu->reg + DMAR_FEADDR_REG);
3994 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3995 iommu->reg + DMAR_FEUADDR_REG);
3996
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003997 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003998 }
3999
4000 for_each_active_iommu(iommu, drhd)
4001 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004002}
4003
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004004static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004005 .resume = iommu_resume,
4006 .suspend = iommu_suspend,
4007};
4008
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004009static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004010{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004011 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004012}
4013
4014#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004015static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004016#endif /* CONFIG_PM */
4017
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004018
Jiang Liuc2a0b532014-11-09 22:47:56 +08004019int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004020{
4021 struct acpi_dmar_reserved_memory *rmrr;
4022 struct dmar_rmrr_unit *rmrru;
4023
4024 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4025 if (!rmrru)
4026 return -ENOMEM;
4027
4028 rmrru->hdr = header;
4029 rmrr = (struct acpi_dmar_reserved_memory *)header;
4030 rmrru->base_address = rmrr->base_address;
4031 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08004032 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4033 ((void *)rmrr) + rmrr->header.length,
4034 &rmrru->devices_cnt);
4035 if (rmrru->devices_cnt && rmrru->devices == NULL) {
4036 kfree(rmrru);
4037 return -ENOMEM;
4038 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004039
Jiang Liu2e455282014-02-19 14:07:36 +08004040 list_add(&rmrru->list, &dmar_rmrr_units);
4041
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004042 return 0;
4043}
4044
Jiang Liu6b197242014-11-09 22:47:58 +08004045static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4046{
4047 struct dmar_atsr_unit *atsru;
4048 struct acpi_dmar_atsr *tmp;
4049
4050 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4051 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4052 if (atsr->segment != tmp->segment)
4053 continue;
4054 if (atsr->header.length != tmp->header.length)
4055 continue;
4056 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4057 return atsru;
4058 }
4059
4060 return NULL;
4061}
4062
4063int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004064{
4065 struct acpi_dmar_atsr *atsr;
4066 struct dmar_atsr_unit *atsru;
4067
Jiang Liu6b197242014-11-09 22:47:58 +08004068 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4069 return 0;
4070
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004071 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004072 atsru = dmar_find_atsr(atsr);
4073 if (atsru)
4074 return 0;
4075
4076 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004077 if (!atsru)
4078 return -ENOMEM;
4079
Jiang Liu6b197242014-11-09 22:47:58 +08004080 /*
4081 * If memory is allocated from slab by ACPI _DSM method, we need to
4082 * copy the memory content because the memory buffer will be freed
4083 * on return.
4084 */
4085 atsru->hdr = (void *)(atsru + 1);
4086 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004087 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004088 if (!atsru->include_all) {
4089 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4090 (void *)atsr + atsr->header.length,
4091 &atsru->devices_cnt);
4092 if (atsru->devices_cnt && atsru->devices == NULL) {
4093 kfree(atsru);
4094 return -ENOMEM;
4095 }
4096 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004097
Jiang Liu0e242612014-02-19 14:07:34 +08004098 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004099
4100 return 0;
4101}
4102
Jiang Liu9bdc5312014-01-06 14:18:27 +08004103static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4104{
4105 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4106 kfree(atsru);
4107}
4108
Jiang Liu6b197242014-11-09 22:47:58 +08004109int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4110{
4111 struct acpi_dmar_atsr *atsr;
4112 struct dmar_atsr_unit *atsru;
4113
4114 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4115 atsru = dmar_find_atsr(atsr);
4116 if (atsru) {
4117 list_del_rcu(&atsru->list);
4118 synchronize_rcu();
4119 intel_iommu_free_atsr(atsru);
4120 }
4121
4122 return 0;
4123}
4124
4125int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4126{
4127 int i;
4128 struct device *dev;
4129 struct acpi_dmar_atsr *atsr;
4130 struct dmar_atsr_unit *atsru;
4131
4132 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4133 atsru = dmar_find_atsr(atsr);
4134 if (!atsru)
4135 return 0;
4136
4137 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4138 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4139 i, dev)
4140 return -EBUSY;
4141
4142 return 0;
4143}
4144
Jiang Liuffebeb42014-11-09 22:48:02 +08004145static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4146{
4147 int sp, ret = 0;
4148 struct intel_iommu *iommu = dmaru->iommu;
4149
4150 if (g_iommus[iommu->seq_id])
4151 return 0;
4152
4153 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004154 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004155 iommu->name);
4156 return -ENXIO;
4157 }
4158 if (!ecap_sc_support(iommu->ecap) &&
4159 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004160 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004161 iommu->name);
4162 return -ENXIO;
4163 }
4164 sp = domain_update_iommu_superpage(iommu) - 1;
4165 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004166 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004167 iommu->name);
4168 return -ENXIO;
4169 }
4170
4171 /*
4172 * Disable translation if already enabled prior to OS handover.
4173 */
4174 if (iommu->gcmd & DMA_GCMD_TE)
4175 iommu_disable_translation(iommu);
4176
4177 g_iommus[iommu->seq_id] = iommu;
4178 ret = iommu_init_domains(iommu);
4179 if (ret == 0)
4180 ret = iommu_alloc_root_entry(iommu);
4181 if (ret)
4182 goto out;
4183
David Woodhouse8a94ade2015-03-24 14:54:56 +00004184#ifdef CONFIG_INTEL_IOMMU_SVM
4185 if (pasid_enabled(iommu))
4186 intel_svm_alloc_pasid_tables(iommu);
4187#endif
4188
Jiang Liuffebeb42014-11-09 22:48:02 +08004189 if (dmaru->ignored) {
4190 /*
4191 * we always have to disable PMRs or DMA may fail on this device
4192 */
4193 if (force_on)
4194 iommu_disable_protect_mem_regions(iommu);
4195 return 0;
4196 }
4197
4198 intel_iommu_init_qi(iommu);
4199 iommu_flush_write_buffer(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01004200
4201#ifdef CONFIG_INTEL_IOMMU_SVM
4202 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
4203 ret = intel_svm_enable_prq(iommu);
4204 if (ret)
4205 goto disable_iommu;
4206 }
4207#endif
Jiang Liuffebeb42014-11-09 22:48:02 +08004208 ret = dmar_set_interrupt(iommu);
4209 if (ret)
4210 goto disable_iommu;
4211
4212 iommu_set_root_entry(iommu);
4213 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4214 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4215 iommu_enable_translation(iommu);
4216
Jiang Liuffebeb42014-11-09 22:48:02 +08004217 iommu_disable_protect_mem_regions(iommu);
4218 return 0;
4219
4220disable_iommu:
4221 disable_dmar_iommu(iommu);
4222out:
4223 free_dmar_iommu(iommu);
4224 return ret;
4225}
4226
Jiang Liu6b197242014-11-09 22:47:58 +08004227int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4228{
Jiang Liuffebeb42014-11-09 22:48:02 +08004229 int ret = 0;
4230 struct intel_iommu *iommu = dmaru->iommu;
4231
4232 if (!intel_iommu_enabled)
4233 return 0;
4234 if (iommu == NULL)
4235 return -EINVAL;
4236
4237 if (insert) {
4238 ret = intel_iommu_add(dmaru);
4239 } else {
4240 disable_dmar_iommu(iommu);
4241 free_dmar_iommu(iommu);
4242 }
4243
4244 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004245}
4246
Jiang Liu9bdc5312014-01-06 14:18:27 +08004247static void intel_iommu_free_dmars(void)
4248{
4249 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4250 struct dmar_atsr_unit *atsru, *atsr_n;
4251
4252 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4253 list_del(&rmrru->list);
4254 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4255 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004256 }
4257
Jiang Liu9bdc5312014-01-06 14:18:27 +08004258 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4259 list_del(&atsru->list);
4260 intel_iommu_free_atsr(atsru);
4261 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004262}
4263
4264int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4265{
Jiang Liub683b232014-02-19 14:07:32 +08004266 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004267 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004268 struct pci_dev *bridge = NULL;
4269 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004270 struct acpi_dmar_atsr *atsr;
4271 struct dmar_atsr_unit *atsru;
4272
4273 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004274 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004275 bridge = bus->self;
David Woodhoused14053b2015-10-15 09:28:06 +01004276 /* If it's an integrated device, allow ATS */
4277 if (!bridge)
4278 return 1;
4279 /* Connected via non-PCIe: no ATS */
4280 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004281 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004282 return 0;
David Woodhoused14053b2015-10-15 09:28:06 +01004283 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004284 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004285 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004286 }
4287
Jiang Liu0e242612014-02-19 14:07:34 +08004288 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004289 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4290 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4291 if (atsr->segment != pci_domain_nr(dev->bus))
4292 continue;
4293
Jiang Liub683b232014-02-19 14:07:32 +08004294 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004295 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004296 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004297
4298 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004299 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004300 }
Jiang Liub683b232014-02-19 14:07:32 +08004301 ret = 0;
4302out:
Jiang Liu0e242612014-02-19 14:07:34 +08004303 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004304
Jiang Liub683b232014-02-19 14:07:32 +08004305 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004306}
4307
Jiang Liu59ce0512014-02-19 14:07:35 +08004308int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4309{
4310 int ret = 0;
4311 struct dmar_rmrr_unit *rmrru;
4312 struct dmar_atsr_unit *atsru;
4313 struct acpi_dmar_atsr *atsr;
4314 struct acpi_dmar_reserved_memory *rmrr;
4315
4316 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4317 return 0;
4318
4319 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4320 rmrr = container_of(rmrru->hdr,
4321 struct acpi_dmar_reserved_memory, header);
4322 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4323 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4324 ((void *)rmrr) + rmrr->header.length,
4325 rmrr->segment, rmrru->devices,
4326 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004327 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004328 return ret;
4329 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004330 dmar_remove_dev_scope(info, rmrr->segment,
4331 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004332 }
4333 }
4334
4335 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4336 if (atsru->include_all)
4337 continue;
4338
4339 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4340 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4341 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4342 (void *)atsr + atsr->header.length,
4343 atsr->segment, atsru->devices,
4344 atsru->devices_cnt);
4345 if (ret > 0)
4346 break;
4347 else if(ret < 0)
4348 return ret;
4349 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4350 if (dmar_remove_dev_scope(info, atsr->segment,
4351 atsru->devices, atsru->devices_cnt))
4352 break;
4353 }
4354 }
4355
4356 return 0;
4357}
4358
Fenghua Yu99dcade2009-11-11 07:23:06 -08004359/*
4360 * Here we only respond to action of unbound device from driver.
4361 *
4362 * Added device is not attached to its DMAR domain here yet. That will happen
4363 * when mapping the device to iova.
4364 */
4365static int device_notifier(struct notifier_block *nb,
4366 unsigned long action, void *data)
4367{
4368 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004369 struct dmar_domain *domain;
4370
David Woodhouse3d891942014-03-06 15:59:26 +00004371 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004372 return 0;
4373
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004374 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004375 return 0;
4376
David Woodhouse1525a292014-03-06 16:19:30 +00004377 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004378 if (!domain)
4379 return 0;
4380
Joerg Roedele6de0f82015-07-22 16:30:36 +02004381 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004382 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004383 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004384
Fenghua Yu99dcade2009-11-11 07:23:06 -08004385 return 0;
4386}
4387
4388static struct notifier_block device_nb = {
4389 .notifier_call = device_notifier,
4390};
4391
Jiang Liu75f05562014-02-19 14:07:37 +08004392static int intel_iommu_memory_notifier(struct notifier_block *nb,
4393 unsigned long val, void *v)
4394{
4395 struct memory_notify *mhp = v;
4396 unsigned long long start, end;
4397 unsigned long start_vpfn, last_vpfn;
4398
4399 switch (val) {
4400 case MEM_GOING_ONLINE:
4401 start = mhp->start_pfn << PAGE_SHIFT;
4402 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4403 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004404 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004405 start, end);
4406 return NOTIFY_BAD;
4407 }
4408 break;
4409
4410 case MEM_OFFLINE:
4411 case MEM_CANCEL_ONLINE:
4412 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4413 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4414 while (start_vpfn <= last_vpfn) {
4415 struct iova *iova;
4416 struct dmar_drhd_unit *drhd;
4417 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004418 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004419
4420 iova = find_iova(&si_domain->iovad, start_vpfn);
4421 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004422 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004423 start_vpfn);
4424 break;
4425 }
4426
4427 iova = split_and_remove_iova(&si_domain->iovad, iova,
4428 start_vpfn, last_vpfn);
4429 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004430 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004431 start_vpfn, last_vpfn);
4432 return NOTIFY_BAD;
4433 }
4434
David Woodhouseea8ea462014-03-05 17:09:32 +00004435 freelist = domain_unmap(si_domain, iova->pfn_lo,
4436 iova->pfn_hi);
4437
Jiang Liu75f05562014-02-19 14:07:37 +08004438 rcu_read_lock();
4439 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004440 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004441 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004442 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004443 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004444 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004445
4446 start_vpfn = iova->pfn_hi + 1;
4447 free_iova_mem(iova);
4448 }
4449 break;
4450 }
4451
4452 return NOTIFY_OK;
4453}
4454
4455static struct notifier_block intel_iommu_memory_nb = {
4456 .notifier_call = intel_iommu_memory_notifier,
4457 .priority = 0
4458};
4459
Alex Williamsona5459cf2014-06-12 16:12:31 -06004460
4461static ssize_t intel_iommu_show_version(struct device *dev,
4462 struct device_attribute *attr,
4463 char *buf)
4464{
4465 struct intel_iommu *iommu = dev_get_drvdata(dev);
4466 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4467 return sprintf(buf, "%d:%d\n",
4468 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4469}
4470static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4471
4472static ssize_t intel_iommu_show_address(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->reg_phys);
4478}
4479static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4480
4481static ssize_t intel_iommu_show_cap(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, "%llx\n", iommu->cap);
4487}
4488static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4489
4490static ssize_t intel_iommu_show_ecap(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, "%llx\n", iommu->ecap);
4496}
4497static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4498
Alex Williamson2238c082015-07-14 15:24:53 -06004499static ssize_t intel_iommu_show_ndoms(struct device *dev,
4500 struct device_attribute *attr,
4501 char *buf)
4502{
4503 struct intel_iommu *iommu = dev_get_drvdata(dev);
4504 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4505}
4506static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4507
4508static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4509 struct device_attribute *attr,
4510 char *buf)
4511{
4512 struct intel_iommu *iommu = dev_get_drvdata(dev);
4513 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4514 cap_ndoms(iommu->cap)));
4515}
4516static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4517
Alex Williamsona5459cf2014-06-12 16:12:31 -06004518static struct attribute *intel_iommu_attrs[] = {
4519 &dev_attr_version.attr,
4520 &dev_attr_address.attr,
4521 &dev_attr_cap.attr,
4522 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004523 &dev_attr_domains_supported.attr,
4524 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004525 NULL,
4526};
4527
4528static struct attribute_group intel_iommu_group = {
4529 .name = "intel-iommu",
4530 .attrs = intel_iommu_attrs,
4531};
4532
4533const struct attribute_group *intel_iommu_groups[] = {
4534 &intel_iommu_group,
4535 NULL,
4536};
4537
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004538int __init intel_iommu_init(void)
4539{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004540 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004541 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004542 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004543
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004544 /* VT-d is required for a TXT/tboot launch, so enforce that */
4545 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004546
Jiang Liu3a5670e2014-02-19 14:07:33 +08004547 if (iommu_init_mempool()) {
4548 if (force_on)
4549 panic("tboot: Failed to initialize iommu memory\n");
4550 return -ENOMEM;
4551 }
4552
4553 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004554 if (dmar_table_init()) {
4555 if (force_on)
4556 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004557 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004558 }
4559
Suresh Siddhac2c72862011-08-23 17:05:19 -07004560 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004561 if (force_on)
4562 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004563 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004564 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004565
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004566 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004567 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004568
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004569 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004570 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004571
4572 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004573 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004574
Joseph Cihula51a63e62011-03-21 11:04:24 -07004575 if (dmar_init_reserved_ranges()) {
4576 if (force_on)
4577 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004578 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004579 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004580
4581 init_no_remapping_devices();
4582
Joseph Cihulab7792602011-05-03 00:08:37 -07004583 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004584 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004585 if (force_on)
4586 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004587 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004588 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004589 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004590 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004591 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004592
mark gross5e0d2a62008-03-04 15:22:08 -08004593 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004594#ifdef CONFIG_SWIOTLB
4595 swiotlb = 0;
4596#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004597 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004598
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004599 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004600
Alex Williamsona5459cf2014-06-12 16:12:31 -06004601 for_each_active_iommu(iommu, drhd)
4602 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4603 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004604 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004605
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004606 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004607 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004608 if (si_domain && !hw_pass_through)
4609 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004610
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004611 intel_iommu_enabled = 1;
4612
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004613 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004614
4615out_free_reserved_range:
4616 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004617out_free_dmar:
4618 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004619 up_write(&dmar_global_lock);
4620 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004621 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004622}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004623
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004624static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004625{
4626 struct intel_iommu *iommu = opaque;
4627
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004628 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004629 return 0;
4630}
4631
4632/*
4633 * NB - intel-iommu lacks any sort of reference counting for the users of
4634 * dependent devices. If multiple endpoints have intersecting dependent
4635 * devices, unbinding the driver from any one of them will possibly leave
4636 * the others unable to operate.
4637 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004638static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004639{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004640 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004641 return;
4642
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004643 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004644}
4645
Joerg Roedel127c7612015-07-23 17:44:46 +02004646static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004647{
Weidong Hanc7151a82008-12-08 22:51:37 +08004648 struct intel_iommu *iommu;
4649 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004650
Joerg Roedel55d94042015-07-22 16:50:40 +02004651 assert_spin_locked(&device_domain_lock);
4652
Joerg Roedelb608ac32015-07-21 18:19:08 +02004653 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004654 return;
4655
Joerg Roedel127c7612015-07-23 17:44:46 +02004656 iommu = info->iommu;
4657
4658 if (info->dev) {
4659 iommu_disable_dev_iotlb(info);
4660 domain_context_clear(iommu, info->dev);
4661 }
4662
Joerg Roedelb608ac32015-07-21 18:19:08 +02004663 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004664
Joerg Roedeld160aca2015-07-22 11:52:53 +02004665 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004666 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004667 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004668
4669 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004670}
4671
Joerg Roedel55d94042015-07-22 16:50:40 +02004672static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4673 struct device *dev)
4674{
Joerg Roedel127c7612015-07-23 17:44:46 +02004675 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004676 unsigned long flags;
4677
Weidong Hanc7151a82008-12-08 22:51:37 +08004678 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004679 info = dev->archdata.iommu;
4680 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004681 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004682}
4683
4684static int md_domain_init(struct dmar_domain *domain, int guest_width)
4685{
4686 int adjust_width;
4687
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004688 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4689 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004690 domain_reserve_special_ranges(domain);
4691
4692 /* calculate AGAW */
4693 domain->gaw = guest_width;
4694 adjust_width = guestwidth_to_adjustwidth(guest_width);
4695 domain->agaw = width_to_agaw(adjust_width);
4696
Weidong Han5e98c4b2008-12-08 23:03:27 +08004697 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004698 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004699 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004700 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004701
4702 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004703 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004704 if (!domain->pgd)
4705 return -ENOMEM;
4706 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4707 return 0;
4708}
4709
Joerg Roedel00a77de2015-03-26 13:43:08 +01004710static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004711{
Joerg Roedel5d450802008-12-03 14:52:32 +01004712 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004713 struct iommu_domain *domain;
4714
4715 if (type != IOMMU_DOMAIN_UNMANAGED)
4716 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004717
Jiang Liuab8dfe22014-07-11 14:19:27 +08004718 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004719 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004720 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004721 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004722 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004723 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004724 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004725 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004726 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004727 }
Allen Kay8140a952011-10-14 12:32:17 -07004728 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004729
Joerg Roedel00a77de2015-03-26 13:43:08 +01004730 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004731 domain->geometry.aperture_start = 0;
4732 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4733 domain->geometry.force_aperture = true;
4734
Joerg Roedel00a77de2015-03-26 13:43:08 +01004735 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004736}
Kay, Allen M38717942008-09-09 18:37:29 +03004737
Joerg Roedel00a77de2015-03-26 13:43:08 +01004738static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004739{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004740 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004741}
Kay, Allen M38717942008-09-09 18:37:29 +03004742
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004743static int intel_iommu_attach_device(struct iommu_domain *domain,
4744 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004745{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004746 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004747 struct intel_iommu *iommu;
4748 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004749 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004750
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004751 if (device_is_rmrr_locked(dev)) {
4752 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4753 return -EPERM;
4754 }
4755
David Woodhouse7207d8f2014-03-09 16:31:06 -07004756 /* normally dev is not mapped */
4757 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004758 struct dmar_domain *old_domain;
4759
David Woodhouse1525a292014-03-06 16:19:30 +00004760 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004761 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02004762 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02004763 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004764 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01004765
4766 if (!domain_type_is_vm_or_si(old_domain) &&
4767 list_empty(&old_domain->devices))
4768 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004769 }
4770 }
4771
David Woodhouse156baca2014-03-09 14:00:57 -07004772 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004773 if (!iommu)
4774 return -ENODEV;
4775
4776 /* check if this iommu agaw is sufficient for max mapped address */
4777 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004778 if (addr_width > cap_mgaw(iommu->cap))
4779 addr_width = cap_mgaw(iommu->cap);
4780
4781 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004782 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004783 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004784 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004785 return -EFAULT;
4786 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004787 dmar_domain->gaw = addr_width;
4788
4789 /*
4790 * Knock out extra levels of page tables if necessary
4791 */
4792 while (iommu->agaw < dmar_domain->agaw) {
4793 struct dma_pte *pte;
4794
4795 pte = dmar_domain->pgd;
4796 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004797 dmar_domain->pgd = (struct dma_pte *)
4798 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004799 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004800 }
4801 dmar_domain->agaw--;
4802 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004803
Joerg Roedel28ccce02015-07-21 14:45:31 +02004804 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004805}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004806
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004807static void intel_iommu_detach_device(struct iommu_domain *domain,
4808 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004809{
Joerg Roedele6de0f82015-07-22 16:30:36 +02004810 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004811}
Kay, Allen M38717942008-09-09 18:37:29 +03004812
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004813static int intel_iommu_map(struct iommu_domain *domain,
4814 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004815 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004816{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004817 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004818 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004819 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004820 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004821
Joerg Roedeldde57a22008-12-03 15:04:09 +01004822 if (iommu_prot & IOMMU_READ)
4823 prot |= DMA_PTE_READ;
4824 if (iommu_prot & IOMMU_WRITE)
4825 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08004826 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4827 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004828
David Woodhouse163cc522009-06-28 00:51:17 +01004829 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004830 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004831 u64 end;
4832
4833 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004834 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004835 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004836 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004837 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004838 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004839 return -EFAULT;
4840 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004841 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004842 }
David Woodhousead051222009-06-28 14:22:28 +01004843 /* Round up size to next multiple of PAGE_SIZE, if it and
4844 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004845 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004846 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4847 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004848 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004849}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004850
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004851static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004852 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004853{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004854 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004855 struct page *freelist = NULL;
4856 struct intel_iommu *iommu;
4857 unsigned long start_pfn, last_pfn;
4858 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02004859 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004860
David Woodhouse5cf0a762014-03-19 16:07:49 +00004861 /* Cope with horrid API which requires us to unmap more than the
4862 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02004863 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00004864
4865 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4866 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4867
David Woodhouseea8ea462014-03-05 17:09:32 +00004868 start_pfn = iova >> VTD_PAGE_SHIFT;
4869 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4870
4871 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4872
4873 npages = last_pfn - start_pfn + 1;
4874
Joerg Roedel29a27712015-07-21 17:17:12 +02004875 for_each_domain_iommu(iommu_id, dmar_domain) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004876 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00004877
Joerg Roedel42e8c182015-07-21 15:50:02 +02004878 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
4879 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00004880 }
4881
4882 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004883
David Woodhouse163cc522009-06-28 00:51:17 +01004884 if (dmar_domain->max_addr == iova + size)
4885 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004886
David Woodhouse5cf0a762014-03-19 16:07:49 +00004887 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004888}
Kay, Allen M38717942008-09-09 18:37:29 +03004889
Joerg Roedeld14d6572008-12-03 15:06:57 +01004890static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05304891 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004892{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004893 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004894 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004895 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004896 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004897
David Woodhouse5cf0a762014-03-19 16:07:49 +00004898 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004899 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004900 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004901
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004902 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004903}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004904
Joerg Roedel5d587b82014-09-05 10:50:45 +02004905static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004906{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004907 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004908 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004909 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004910 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004911
Joerg Roedel5d587b82014-09-05 10:50:45 +02004912 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004913}
4914
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004915static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004916{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004917 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004918 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004919 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004920
Alex Williamsona5459cf2014-06-12 16:12:31 -06004921 iommu = device_to_iommu(dev, &bus, &devfn);
4922 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004923 return -ENODEV;
4924
Alex Williamsona5459cf2014-06-12 16:12:31 -06004925 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004926
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004927 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004928
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004929 if (IS_ERR(group))
4930 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004931
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004932 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004933 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004934}
4935
4936static void intel_iommu_remove_device(struct device *dev)
4937{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004938 struct intel_iommu *iommu;
4939 u8 bus, devfn;
4940
4941 iommu = device_to_iommu(dev, &bus, &devfn);
4942 if (!iommu)
4943 return;
4944
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004945 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004946
4947 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004948}
4949
David Woodhouse2f26e0a2015-09-09 11:40:47 +01004950#ifdef CONFIG_INTEL_IOMMU_SVM
4951int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
4952{
4953 struct device_domain_info *info;
4954 struct context_entry *context;
4955 struct dmar_domain *domain;
4956 unsigned long flags;
4957 u64 ctx_lo;
4958 int ret;
4959
4960 domain = get_valid_domain_for_dev(sdev->dev);
4961 if (!domain)
4962 return -EINVAL;
4963
4964 spin_lock_irqsave(&device_domain_lock, flags);
4965 spin_lock(&iommu->lock);
4966
4967 ret = -EINVAL;
4968 info = sdev->dev->archdata.iommu;
4969 if (!info || !info->pasid_supported)
4970 goto out;
4971
4972 context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
4973 if (WARN_ON(!context))
4974 goto out;
4975
4976 ctx_lo = context[0].lo;
4977
4978 sdev->did = domain->iommu_did[iommu->seq_id];
4979 sdev->sid = PCI_DEVID(info->bus, info->devfn);
4980
4981 if (!(ctx_lo & CONTEXT_PASIDE)) {
4982 context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
4983 context[1].lo = (u64)virt_to_phys(iommu->pasid_table) | ecap_pss(iommu->ecap);
4984 wmb();
4985 /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
4986 * extended to permit requests-with-PASID if the PASIDE bit
4987 * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
4988 * however, the PASIDE bit is ignored and requests-with-PASID
4989 * are unconditionally blocked. Which makes less sense.
4990 * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
4991 * "guest mode" translation types depending on whether ATS
4992 * is available or not. Annoyingly, we can't use the new
4993 * modes *unless* PASIDE is set. */
4994 if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
4995 ctx_lo &= ~CONTEXT_TT_MASK;
4996 if (info->ats_supported)
4997 ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
4998 else
4999 ctx_lo |= CONTEXT_TT_PT_PASID << 2;
5000 }
5001 ctx_lo |= CONTEXT_PASIDE;
David Woodhouse907fea32015-10-13 14:11:13 +01005002 if (iommu->pasid_state_table)
5003 ctx_lo |= CONTEXT_DINVE;
David Woodhousea222a7f2015-10-07 23:35:18 +01005004 if (info->pri_supported)
5005 ctx_lo |= CONTEXT_PRS;
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005006 context[0].lo = ctx_lo;
5007 wmb();
5008 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
5009 DMA_CCMD_MASK_NOBIT,
5010 DMA_CCMD_DEVICE_INVL);
5011 }
5012
5013 /* Enable PASID support in the device, if it wasn't already */
5014 if (!info->pasid_enabled)
5015 iommu_enable_dev_iotlb(info);
5016
5017 if (info->ats_enabled) {
5018 sdev->dev_iotlb = 1;
5019 sdev->qdep = info->ats_qdep;
5020 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
5021 sdev->qdep = 0;
5022 }
5023 ret = 0;
5024
5025 out:
5026 spin_unlock(&iommu->lock);
5027 spin_unlock_irqrestore(&device_domain_lock, flags);
5028
5029 return ret;
5030}
5031
5032struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5033{
5034 struct intel_iommu *iommu;
5035 u8 bus, devfn;
5036
5037 if (iommu_dummy(dev)) {
5038 dev_warn(dev,
5039 "No IOMMU translation for device; cannot enable SVM\n");
5040 return NULL;
5041 }
5042
5043 iommu = device_to_iommu(dev, &bus, &devfn);
5044 if ((!iommu)) {
5045 dev_dbg(dev, "No IOMMU for device; cannot enable SVM\n");
5046 return NULL;
5047 }
5048
5049 if (!iommu->pasid_table) {
5050 dev_dbg(dev, "PASID not enabled on IOMMU; cannot enable SVM\n");
5051 return NULL;
5052 }
5053
5054 return iommu;
5055}
5056#endif /* CONFIG_INTEL_IOMMU_SVM */
5057
Thierry Redingb22f6432014-06-27 09:03:12 +02005058static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02005059 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01005060 .domain_alloc = intel_iommu_domain_alloc,
5061 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005062 .attach_dev = intel_iommu_attach_device,
5063 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005064 .map = intel_iommu_map,
5065 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07005066 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005067 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005068 .add_device = intel_iommu_add_device,
5069 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02005070 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005071};
David Woodhouse9af88142009-02-13 23:18:03 +00005072
Daniel Vetter94526182013-01-20 23:50:13 +01005073static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
5074{
5075 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005076 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01005077 dmar_map_gfx = 0;
5078}
5079
5080DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
5081DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
5082DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
5083DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
5084DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
5085DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
5086DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
5087
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005088static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00005089{
5090 /*
5091 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01005092 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00005093 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005094 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005095 rwbf_quirk = 1;
5096}
5097
5098DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005099DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5100DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5101DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5102DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5103DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5104DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005105
Adam Jacksoneecfd572010-08-25 21:17:34 +01005106#define GGC 0x52
5107#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5108#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5109#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5110#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5111#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5112#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5113#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5114#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5115
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005116static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005117{
5118 unsigned short ggc;
5119
Adam Jacksoneecfd572010-08-25 21:17:34 +01005120 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005121 return;
5122
Adam Jacksoneecfd572010-08-25 21:17:34 +01005123 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005124 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005125 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005126 } else if (dmar_map_gfx) {
5127 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005128 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005129 intel_iommu_strict = 1;
5130 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005131}
5132DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5133DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5134DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5135DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5136
David Woodhousee0fc7e02009-09-30 09:12:17 -07005137/* On Tylersburg chipsets, some BIOSes have been known to enable the
5138 ISOCH DMAR unit for the Azalia sound device, but not give it any
5139 TLB entries, which causes it to deadlock. Check for that. We do
5140 this in a function called from init_dmars(), instead of in a PCI
5141 quirk, because we don't want to print the obnoxious "BIOS broken"
5142 message if VT-d is actually disabled.
5143*/
5144static void __init check_tylersburg_isoch(void)
5145{
5146 struct pci_dev *pdev;
5147 uint32_t vtisochctrl;
5148
5149 /* If there's no Azalia in the system anyway, forget it. */
5150 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5151 if (!pdev)
5152 return;
5153 pci_dev_put(pdev);
5154
5155 /* System Management Registers. Might be hidden, in which case
5156 we can't do the sanity check. But that's OK, because the
5157 known-broken BIOSes _don't_ actually hide it, so far. */
5158 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5159 if (!pdev)
5160 return;
5161
5162 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5163 pci_dev_put(pdev);
5164 return;
5165 }
5166
5167 pci_dev_put(pdev);
5168
5169 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5170 if (vtisochctrl & 1)
5171 return;
5172
5173 /* Drop all bits other than the number of TLB entries */
5174 vtisochctrl &= 0x1c;
5175
5176 /* If we have the recommended number of TLB entries (16), fine. */
5177 if (vtisochctrl == 0x10)
5178 return;
5179
5180 /* Zero TLB entries? You get to ride the short bus to school. */
5181 if (!vtisochctrl) {
5182 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5183 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5184 dmi_get_system_info(DMI_BIOS_VENDOR),
5185 dmi_get_system_info(DMI_BIOS_VERSION),
5186 dmi_get_system_info(DMI_PRODUCT_VERSION));
5187 iommu_identity_mapping |= IDENTMAP_AZALIA;
5188 return;
5189 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005190
5191 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005192 vtisochctrl);
5193}