blob: 3965e73db51cef0f3d3b5593da18c9d9fe25fd9e [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>
Omer Pelegaa473242016-04-20 11:33:02 +030036#include <linux/cpu.h>
mark gross5e0d2a62008-03-04 15:22:08 -080037#include <linux/timer.h>
Dan Williamsdfddb962015-10-09 18:16:46 -040038#include <linux/io.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010040#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030041#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010042#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070043#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100044#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020045#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080046#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070047#include <linux/dma-contiguous.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020048#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070049#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070050#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090051#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070052
Joerg Roedel078e1ee2012-09-26 12:44:43 +020053#include "irq_remapping.h"
54
Fenghua Yu5b6985c2008-10-16 18:02:32 -070055#define ROOT_SIZE VTD_PAGE_SIZE
56#define CONTEXT_SIZE VTD_PAGE_SIZE
57
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000059#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070061#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070062
63#define IOAPIC_RANGE_START (0xfee00000)
64#define IOAPIC_RANGE_END (0xfeefffff)
65#define IOVA_START_ADDR (0x1000)
66
67#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
68
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070069#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080070#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070071
David Woodhouse2ebe3152009-09-19 07:34:04 -070072#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
73#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
74
75/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
76 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
77#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
78 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
79#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070080
Robin Murphy1b722502015-01-12 17:51:15 +000081/* IO virtual address start page frame number */
82#define IOVA_START_PFN (1)
83
Mark McLoughlinf27be032008-11-20 15:49:43 +000084#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070085#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070086#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080087
Andrew Mortondf08cdc2010-09-22 13:05:11 -070088/* page table handling */
89#define LEVEL_STRIDE (9)
90#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
91
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020092/*
93 * This bitmap is used to advertise the page sizes our hardware support
94 * to the IOMMU core, which will then use this information to split
95 * physically contiguous memory regions it is mapping into page sizes
96 * that we support.
97 *
98 * Traditionally the IOMMU core just handed us the mappings directly,
99 * after making sure the size is an order of a 4KiB page and that the
100 * mapping has natural alignment.
101 *
102 * To retain this behavior, we currently advertise that we support
103 * all page sizes that are an order of 4KiB.
104 *
105 * If at some point we'd like to utilize the IOMMU core's new behavior,
106 * we could change this to advertise the real page sizes we support.
107 */
108#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
109
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700110static inline int agaw_to_level(int agaw)
111{
112 return agaw + 2;
113}
114
115static inline int agaw_to_width(int agaw)
116{
Jiang Liu5c645b32014-01-06 14:18:12 +0800117 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700118}
119
120static inline int width_to_agaw(int width)
121{
Jiang Liu5c645b32014-01-06 14:18:12 +0800122 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700123}
124
125static inline unsigned int level_to_offset_bits(int level)
126{
127 return (level - 1) * LEVEL_STRIDE;
128}
129
130static inline int pfn_level_offset(unsigned long pfn, int level)
131{
132 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
133}
134
135static inline unsigned long level_mask(int level)
136{
137 return -1UL << level_to_offset_bits(level);
138}
139
140static inline unsigned long level_size(int level)
141{
142 return 1UL << level_to_offset_bits(level);
143}
144
145static inline unsigned long align_to_level(unsigned long pfn, int level)
146{
147 return (pfn + level_size(level) - 1) & level_mask(level);
148}
David Woodhousefd18de52009-05-10 23:57:41 +0100149
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100150static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
151{
Jiang Liu5c645b32014-01-06 14:18:12 +0800152 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100153}
154
David Woodhousedd4e8312009-06-27 16:21:20 +0100155/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
156 are never going to work. */
157static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
158{
159 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
160}
161
162static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
163{
164 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
165}
166static inline unsigned long page_to_dma_pfn(struct page *pg)
167{
168 return mm_to_dma_pfn(page_to_pfn(pg));
169}
170static inline unsigned long virt_to_dma_pfn(void *p)
171{
172 return page_to_dma_pfn(virt_to_page(p));
173}
174
Weidong Hand9630fe2008-12-08 11:06:32 +0800175/* global iommu list, set NULL for ignored DMAR units */
176static struct intel_iommu **g_iommus;
177
David Woodhousee0fc7e02009-09-30 09:12:17 -0700178static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000179static int rwbf_quirk;
180
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000181/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700182 * set to 1 to panic kernel if can't successfully enable VT-d
183 * (used when kernel is launched w/ TXT)
184 */
185static int force_on = 0;
186
187/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000188 * 0: Present
189 * 1-11: Reserved
190 * 12-63: Context Ptr (12 - (haw-1))
191 * 64-127: Reserved
192 */
193struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000194 u64 lo;
195 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000196};
197#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000198
Joerg Roedel091d42e2015-06-12 11:56:10 +0200199/*
200 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
201 * if marked present.
202 */
203static phys_addr_t root_entry_lctp(struct root_entry *re)
204{
205 if (!(re->lo & 1))
206 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000207
Joerg Roedel091d42e2015-06-12 11:56:10 +0200208 return re->lo & VTD_PAGE_MASK;
209}
210
211/*
212 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
213 * if marked present.
214 */
215static phys_addr_t root_entry_uctp(struct root_entry *re)
216{
217 if (!(re->hi & 1))
218 return 0;
219
220 return re->hi & VTD_PAGE_MASK;
221}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000222/*
223 * low 64 bits:
224 * 0: present
225 * 1: fault processing disable
226 * 2-3: translation type
227 * 12-63: address space root
228 * high 64 bits:
229 * 0-2: address width
230 * 3-6: aval
231 * 8-23: domain id
232 */
233struct context_entry {
234 u64 lo;
235 u64 hi;
236};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000237
Joerg Roedelcf484d02015-06-12 12:21:46 +0200238static inline void context_clear_pasid_enable(struct context_entry *context)
239{
240 context->lo &= ~(1ULL << 11);
241}
242
243static inline bool context_pasid_enabled(struct context_entry *context)
244{
245 return !!(context->lo & (1ULL << 11));
246}
247
248static inline void context_set_copied(struct context_entry *context)
249{
250 context->hi |= (1ull << 3);
251}
252
253static inline bool context_copied(struct context_entry *context)
254{
255 return !!(context->hi & (1ULL << 3));
256}
257
258static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000259{
260 return (context->lo & 1);
261}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200262
263static inline bool context_present(struct context_entry *context)
264{
265 return context_pasid_enabled(context) ?
266 __context_present(context) :
267 __context_present(context) && !context_copied(context);
268}
269
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000270static inline void context_set_present(struct context_entry *context)
271{
272 context->lo |= 1;
273}
274
275static inline void context_set_fault_enable(struct context_entry *context)
276{
277 context->lo &= (((u64)-1) << 2) | 1;
278}
279
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000280static inline void context_set_translation_type(struct context_entry *context,
281 unsigned long value)
282{
283 context->lo &= (((u64)-1) << 4) | 3;
284 context->lo |= (value & 3) << 2;
285}
286
287static inline void context_set_address_root(struct context_entry *context,
288 unsigned long value)
289{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800290 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000291 context->lo |= value & VTD_PAGE_MASK;
292}
293
294static inline void context_set_address_width(struct context_entry *context,
295 unsigned long value)
296{
297 context->hi |= value & 7;
298}
299
300static inline void context_set_domain_id(struct context_entry *context,
301 unsigned long value)
302{
303 context->hi |= (value & ((1 << 16) - 1)) << 8;
304}
305
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200306static inline int context_domain_id(struct context_entry *c)
307{
308 return((c->hi >> 8) & 0xffff);
309}
310
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000311static inline void context_clear_entry(struct context_entry *context)
312{
313 context->lo = 0;
314 context->hi = 0;
315}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000316
Mark McLoughlin622ba122008-11-20 15:49:46 +0000317/*
318 * 0: readable
319 * 1: writable
320 * 2-6: reserved
321 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800322 * 8-10: available
323 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000324 * 12-63: Host physcial address
325 */
326struct dma_pte {
327 u64 val;
328};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000329
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000330static inline void dma_clear_pte(struct dma_pte *pte)
331{
332 pte->val = 0;
333}
334
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000335static inline u64 dma_pte_addr(struct dma_pte *pte)
336{
David Woodhousec85994e2009-07-01 19:21:24 +0100337#ifdef CONFIG_64BIT
338 return pte->val & VTD_PAGE_MASK;
339#else
340 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100341 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100342#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000343}
344
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000345static inline bool dma_pte_present(struct dma_pte *pte)
346{
347 return (pte->val & 3) != 0;
348}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000349
Allen Kay4399c8b2011-10-14 12:32:46 -0700350static inline bool dma_pte_superpage(struct dma_pte *pte)
351{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200352 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700353}
354
David Woodhouse75e6bf92009-07-02 11:21:16 +0100355static inline int first_pte_in_page(struct dma_pte *pte)
356{
357 return !((unsigned long)pte & ~VTD_PAGE_MASK);
358}
359
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700360/*
361 * This domain is a statically identity mapping domain.
362 * 1. This domain creats a static 1:1 mapping to all usable memory.
363 * 2. It maps to each iommu if successful.
364 * 3. Each iommu mapps to this domain if successful.
365 */
David Woodhouse19943b02009-08-04 16:19:20 +0100366static struct dmar_domain *si_domain;
367static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700368
Joerg Roedel28ccce02015-07-21 14:45:31 +0200369/*
370 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800371 * across iommus may be owned in one domain, e.g. kvm guest.
372 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800373#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800374
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700375/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800376#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700377
Joerg Roedel29a27712015-07-21 17:17:12 +0200378#define for_each_domain_iommu(idx, domain) \
379 for (idx = 0; idx < g_num_of_iommus; idx++) \
380 if (domain->iommu_refcnt[idx])
381
Mark McLoughlin99126f72008-11-20 15:49:47 +0000382struct dmar_domain {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700383 int nid; /* node id */
Joerg Roedel29a27712015-07-21 17:17:12 +0200384
385 unsigned iommu_refcnt[DMAR_UNITS_SUPPORTED];
386 /* Refcount of devices per iommu */
387
Mark McLoughlin99126f72008-11-20 15:49:47 +0000388
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +0200389 u16 iommu_did[DMAR_UNITS_SUPPORTED];
390 /* Domain ids per IOMMU. Use u16 since
391 * domain ids are 16 bit wide according
392 * to VT-d spec, section 9.3 */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000393
Omer Peleg0824c592016-04-20 19:03:35 +0300394 bool has_iotlb_device;
Joerg Roedel00a77de2015-03-26 13:43:08 +0100395 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000396 struct iova_domain iovad; /* iova's that belong to this domain */
397
398 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000399 int gaw; /* max guest address width */
400
401 /* adjusted guest address width, 0 is level 2 30-bit */
402 int agaw;
403
Weidong Han3b5410e2008-12-08 09:17:15 +0800404 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800405
406 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800407 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800408 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100409 int iommu_superpage;/* Level of superpages supported:
410 0 == 4KiB (no superpages), 1 == 2MiB,
411 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800412 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100413
414 struct iommu_domain domain; /* generic domain data structure for
415 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000416};
417
Mark McLoughlina647dac2008-11-20 15:49:48 +0000418/* PCI domain-device relationship */
419struct device_domain_info {
420 struct list_head link; /* link to domain siblings */
421 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100422 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000423 u8 devfn; /* PCI devfn number */
David Woodhouseb16d0cb2015-10-12 14:17:37 +0100424 u8 pasid_supported:3;
425 u8 pasid_enabled:1;
426 u8 pri_supported:1;
427 u8 pri_enabled:1;
428 u8 ats_supported:1;
429 u8 ats_enabled:1;
430 u8 ats_qdep;
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000431 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800432 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000433 struct dmar_domain *domain; /* pointer to domain */
434};
435
Jiang Liub94e4112014-02-19 14:07:25 +0800436struct dmar_rmrr_unit {
437 struct list_head list; /* list of rmrr units */
438 struct acpi_dmar_header *hdr; /* ACPI header */
439 u64 base_address; /* reserved base address*/
440 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000441 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800442 int devices_cnt; /* target device count */
443};
444
445struct dmar_atsr_unit {
446 struct list_head list; /* list of ATSR units */
447 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000448 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800449 int devices_cnt; /* target device count */
450 u8 include_all:1; /* include all ports */
451};
452
453static LIST_HEAD(dmar_atsr_units);
454static LIST_HEAD(dmar_rmrr_units);
455
456#define for_each_rmrr_units(rmrr) \
457 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
458
mark gross5e0d2a62008-03-04 15:22:08 -0800459static void flush_unmaps_timeout(unsigned long data);
460
Omer Peleg314f1dc2016-04-20 11:32:45 +0300461struct deferred_flush_entry {
Omer Peleg2aac6302016-04-20 11:33:57 +0300462 unsigned long iova_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +0300463 unsigned long nrpages;
Omer Peleg314f1dc2016-04-20 11:32:45 +0300464 struct dmar_domain *domain;
465 struct page *freelist;
mark gross80b20dd2008-04-18 13:53:58 -0700466};
467
Omer Peleg314f1dc2016-04-20 11:32:45 +0300468#define HIGH_WATER_MARK 250
469struct deferred_flush_table {
470 int next;
471 struct deferred_flush_entry entries[HIGH_WATER_MARK];
472};
473
Omer Pelegaa473242016-04-20 11:33:02 +0300474struct deferred_flush_data {
475 spinlock_t lock;
476 int timer_on;
477 struct timer_list timer;
478 long size;
479 struct deferred_flush_table *tables;
480};
481
482DEFINE_PER_CPU(struct deferred_flush_data, deferred_flush);
mark gross80b20dd2008-04-18 13:53:58 -0700483
mark gross5e0d2a62008-03-04 15:22:08 -0800484/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800485static int g_num_of_iommus;
486
Jiang Liu92d03cc2014-02-19 14:07:28 +0800487static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700488static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200489static void dmar_remove_one_dev_info(struct dmar_domain *domain,
490 struct device *dev);
Joerg Roedel127c7612015-07-23 17:44:46 +0200491static void __dmar_remove_one_dev_info(struct device_domain_info *info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200492static void domain_context_clear(struct intel_iommu *iommu,
493 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800494static int domain_detach_iommu(struct dmar_domain *domain,
495 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700496
Suresh Siddhad3f13812011-08-23 17:05:25 -0700497#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800498int dmar_disabled = 0;
499#else
500int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700501#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800502
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200503int intel_iommu_enabled = 0;
504EXPORT_SYMBOL_GPL(intel_iommu_enabled);
505
David Woodhouse2d9e6672010-06-15 10:57:57 +0100506static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700507static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800508static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100509static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100510static int intel_iommu_ecs = 1;
David Woodhouseae853dd2015-09-09 11:58:59 +0100511static int intel_iommu_pasid28;
512static int iommu_identity_mapping;
David Woodhousec83b2f22015-06-12 10:15:49 +0100513
David Woodhouseae853dd2015-09-09 11:58:59 +0100514#define IDENTMAP_ALL 1
515#define IDENTMAP_GFX 2
516#define IDENTMAP_AZALIA 4
David Woodhousec83b2f22015-06-12 10:15:49 +0100517
David Woodhoused42fde72015-10-24 21:33:01 +0200518/* Broadwell and Skylake have broken ECS support — normal so-called "second
519 * level" translation of DMA requests-without-PASID doesn't actually happen
520 * unless you also set the NESTE bit in an extended context-entry. Which of
521 * course means that SVM doesn't work because it's trying to do nested
522 * translation of the physical addresses it finds in the process page tables,
523 * through the IOVA->phys mapping found in the "second level" page tables.
524 *
525 * The VT-d specification was retroactively changed to change the definition
526 * of the capability bits and pretend that Broadwell/Skylake never happened...
527 * but unfortunately the wrong bit was changed. It's ECS which is broken, but
528 * for some reason it was the PASID capability bit which was redefined (from
529 * bit 28 on BDW/SKL to bit 40 in future).
530 *
531 * So our test for ECS needs to eschew those implementations which set the old
532 * PASID capabiity bit 28, since those are the ones on which ECS is broken.
533 * Unless we are working around the 'pasid28' limitations, that is, by putting
534 * the device into passthrough mode for normal DMA and thus masking the bug.
535 */
David Woodhousec83b2f22015-06-12 10:15:49 +0100536#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
David Woodhoused42fde72015-10-24 21:33:01 +0200537 (intel_iommu_pasid28 || !ecap_broken_pasid(iommu->ecap)))
538/* PASID support is thus enabled if ECS is enabled and *either* of the old
539 * or new capability bits are set. */
540#define pasid_enabled(iommu) (ecs_enabled(iommu) && \
541 (ecap_pasid(iommu->ecap) || ecap_broken_pasid(iommu->ecap)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700542
David Woodhousec0771df2011-10-14 20:59:46 +0100543int intel_iommu_gfx_mapped;
544EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
545
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700546#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
547static DEFINE_SPINLOCK(device_domain_lock);
548static LIST_HEAD(device_domain_list);
549
Thierry Redingb22f6432014-06-27 09:03:12 +0200550static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100551
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200552static bool translation_pre_enabled(struct intel_iommu *iommu)
553{
554 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
555}
556
Joerg Roedel091d42e2015-06-12 11:56:10 +0200557static void clear_translation_pre_enabled(struct intel_iommu *iommu)
558{
559 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
560}
561
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200562static void init_translation_status(struct intel_iommu *iommu)
563{
564 u32 gsts;
565
566 gsts = readl(iommu->reg + DMAR_GSTS_REG);
567 if (gsts & DMA_GSTS_TES)
568 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
569}
570
Joerg Roedel00a77de2015-03-26 13:43:08 +0100571/* Convert generic 'struct iommu_domain to private struct dmar_domain */
572static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
573{
574 return container_of(dom, struct dmar_domain, domain);
575}
576
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700577static int __init intel_iommu_setup(char *str)
578{
579 if (!str)
580 return -EINVAL;
581 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800582 if (!strncmp(str, "on", 2)) {
583 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200584 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800585 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700586 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200587 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700588 } else if (!strncmp(str, "igfx_off", 8)) {
589 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200590 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700591 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200592 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700593 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800594 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200595 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800596 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100597 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200598 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100599 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100600 } else if (!strncmp(str, "ecs_off", 7)) {
601 printk(KERN_INFO
602 "Intel-IOMMU: disable extended context table support\n");
603 intel_iommu_ecs = 0;
David Woodhouseae853dd2015-09-09 11:58:59 +0100604 } else if (!strncmp(str, "pasid28", 7)) {
605 printk(KERN_INFO
606 "Intel-IOMMU: enable pre-production PASID support\n");
607 intel_iommu_pasid28 = 1;
608 iommu_identity_mapping |= IDENTMAP_GFX;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700609 }
610
611 str += strcspn(str, ",");
612 while (*str == ',')
613 str++;
614 }
615 return 0;
616}
617__setup("intel_iommu=", intel_iommu_setup);
618
619static struct kmem_cache *iommu_domain_cache;
620static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700621
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200622static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
623{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200624 struct dmar_domain **domains;
625 int idx = did >> 8;
626
627 domains = iommu->domains[idx];
628 if (!domains)
629 return NULL;
630
631 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200632}
633
634static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
635 struct dmar_domain *domain)
636{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200637 struct dmar_domain **domains;
638 int idx = did >> 8;
639
640 if (!iommu->domains[idx]) {
641 size_t size = 256 * sizeof(struct dmar_domain *);
642 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
643 }
644
645 domains = iommu->domains[idx];
646 if (WARN_ON(!domains))
647 return;
648 else
649 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200650}
651
Suresh Siddha4c923d42009-10-02 11:01:24 -0700652static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700653{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700654 struct page *page;
655 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700656
Suresh Siddha4c923d42009-10-02 11:01:24 -0700657 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
658 if (page)
659 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700660 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700661}
662
663static inline void free_pgtable_page(void *vaddr)
664{
665 free_page((unsigned long)vaddr);
666}
667
668static inline void *alloc_domain_mem(void)
669{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900670 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700671}
672
Kay, Allen M38717942008-09-09 18:37:29 +0300673static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700674{
675 kmem_cache_free(iommu_domain_cache, vaddr);
676}
677
678static inline void * alloc_devinfo_mem(void)
679{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900680 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700681}
682
683static inline void free_devinfo_mem(void *vaddr)
684{
685 kmem_cache_free(iommu_devinfo_cache, vaddr);
686}
687
Jiang Liuab8dfe22014-07-11 14:19:27 +0800688static inline int domain_type_is_vm(struct dmar_domain *domain)
689{
690 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
691}
692
Joerg Roedel28ccce02015-07-21 14:45:31 +0200693static inline int domain_type_is_si(struct dmar_domain *domain)
694{
695 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
696}
697
Jiang Liuab8dfe22014-07-11 14:19:27 +0800698static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
699{
700 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
701 DOMAIN_FLAG_STATIC_IDENTITY);
702}
Weidong Han1b573682008-12-08 15:34:06 +0800703
Jiang Liu162d1b12014-07-11 14:19:35 +0800704static inline int domain_pfn_supported(struct dmar_domain *domain,
705 unsigned long pfn)
706{
707 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
708
709 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
710}
711
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700712static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800713{
714 unsigned long sagaw;
715 int agaw = -1;
716
717 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700718 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800719 agaw >= 0; agaw--) {
720 if (test_bit(agaw, &sagaw))
721 break;
722 }
723
724 return agaw;
725}
726
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700727/*
728 * Calculate max SAGAW for each iommu.
729 */
730int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
731{
732 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
733}
734
735/*
736 * calculate agaw for each iommu.
737 * "SAGAW" may be different across iommus, use a default agaw, and
738 * get a supported less agaw for iommus that don't support the default agaw.
739 */
740int iommu_calculate_agaw(struct intel_iommu *iommu)
741{
742 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
743}
744
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700745/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800746static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
747{
748 int iommu_id;
749
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700750 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800751 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200752 for_each_domain_iommu(iommu_id, domain)
753 break;
754
Weidong Han8c11e792008-12-08 15:29:22 +0800755 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
756 return NULL;
757
758 return g_iommus[iommu_id];
759}
760
Weidong Han8e6040972008-12-08 15:49:06 +0800761static void domain_update_iommu_coherency(struct dmar_domain *domain)
762{
David Woodhoused0501962014-03-11 17:10:29 -0700763 struct dmar_drhd_unit *drhd;
764 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100765 bool found = false;
766 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800767
David Woodhoused0501962014-03-11 17:10:29 -0700768 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800769
Joerg Roedel29a27712015-07-21 17:17:12 +0200770 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100771 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800772 if (!ecap_coherent(g_iommus[i]->ecap)) {
773 domain->iommu_coherency = 0;
774 break;
775 }
Weidong Han8e6040972008-12-08 15:49:06 +0800776 }
David Woodhoused0501962014-03-11 17:10:29 -0700777 if (found)
778 return;
779
780 /* No hardware attached; use lowest common denominator */
781 rcu_read_lock();
782 for_each_active_iommu(iommu, drhd) {
783 if (!ecap_coherent(iommu->ecap)) {
784 domain->iommu_coherency = 0;
785 break;
786 }
787 }
788 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800789}
790
Jiang Liu161f6932014-07-11 14:19:37 +0800791static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100792{
Allen Kay8140a952011-10-14 12:32:17 -0700793 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800794 struct intel_iommu *iommu;
795 int ret = 1;
796
797 rcu_read_lock();
798 for_each_active_iommu(iommu, drhd) {
799 if (iommu != skip) {
800 if (!ecap_sc_support(iommu->ecap)) {
801 ret = 0;
802 break;
803 }
804 }
805 }
806 rcu_read_unlock();
807
808 return ret;
809}
810
811static int domain_update_iommu_superpage(struct intel_iommu *skip)
812{
813 struct dmar_drhd_unit *drhd;
814 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700815 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100816
817 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800818 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100819 }
820
Allen Kay8140a952011-10-14 12:32:17 -0700821 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800822 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700823 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800824 if (iommu != skip) {
825 mask &= cap_super_page_val(iommu->cap);
826 if (!mask)
827 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100828 }
829 }
Jiang Liu0e242612014-02-19 14:07:34 +0800830 rcu_read_unlock();
831
Jiang Liu161f6932014-07-11 14:19:37 +0800832 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100833}
834
Sheng Yang58c610b2009-03-18 15:33:05 +0800835/* Some capabilities may be different across iommus */
836static void domain_update_iommu_cap(struct dmar_domain *domain)
837{
838 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800839 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
840 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800841}
842
David Woodhouse03ecc322015-02-13 14:35:21 +0000843static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
844 u8 bus, u8 devfn, int alloc)
845{
846 struct root_entry *root = &iommu->root_entry[bus];
847 struct context_entry *context;
848 u64 *entry;
849
Joerg Roedel4df4eab2015-08-25 10:54:28 +0200850 entry = &root->lo;
David Woodhousec83b2f22015-06-12 10:15:49 +0100851 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000852 if (devfn >= 0x80) {
853 devfn -= 0x80;
854 entry = &root->hi;
855 }
856 devfn *= 2;
857 }
David Woodhouse03ecc322015-02-13 14:35:21 +0000858 if (*entry & 1)
859 context = phys_to_virt(*entry & VTD_PAGE_MASK);
860 else {
861 unsigned long phy_addr;
862 if (!alloc)
863 return NULL;
864
865 context = alloc_pgtable_page(iommu->node);
866 if (!context)
867 return NULL;
868
869 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
870 phy_addr = virt_to_phys((void *)context);
871 *entry = phy_addr | 1;
872 __iommu_flush_cache(iommu, entry, sizeof(*entry));
873 }
874 return &context[devfn];
875}
876
David Woodhouse4ed6a542015-05-11 14:59:20 +0100877static int iommu_dummy(struct device *dev)
878{
879 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
880}
881
David Woodhouse156baca2014-03-09 14:00:57 -0700882static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800883{
884 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800885 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700886 struct device *tmp;
887 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800888 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800889 int i;
890
David Woodhouse4ed6a542015-05-11 14:59:20 +0100891 if (iommu_dummy(dev))
892 return NULL;
893
David Woodhouse156baca2014-03-09 14:00:57 -0700894 if (dev_is_pci(dev)) {
895 pdev = to_pci_dev(dev);
896 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100897 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700898 dev = &ACPI_COMPANION(dev)->dev;
899
Jiang Liu0e242612014-02-19 14:07:34 +0800900 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800901 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700902 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100903 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800904
Jiang Liub683b232014-02-19 14:07:32 +0800905 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700906 drhd->devices_cnt, i, tmp) {
907 if (tmp == dev) {
908 *bus = drhd->devices[i].bus;
909 *devfn = drhd->devices[i].devfn;
910 goto out;
911 }
912
913 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000914 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700915
916 ptmp = to_pci_dev(tmp);
917 if (ptmp->subordinate &&
918 ptmp->subordinate->number <= pdev->bus->number &&
919 ptmp->subordinate->busn_res.end >= pdev->bus->number)
920 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100921 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800922
David Woodhouse156baca2014-03-09 14:00:57 -0700923 if (pdev && drhd->include_all) {
924 got_pdev:
925 *bus = pdev->bus->number;
926 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800927 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700928 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800929 }
Jiang Liub683b232014-02-19 14:07:32 +0800930 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700931 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800932 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800933
Jiang Liub683b232014-02-19 14:07:32 +0800934 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800935}
936
Weidong Han5331fe62008-12-08 23:00:00 +0800937static void domain_flush_cache(struct dmar_domain *domain,
938 void *addr, int size)
939{
940 if (!domain->iommu_coherency)
941 clflush_cache_range(addr, size);
942}
943
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700944static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
945{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700946 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000947 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700948 unsigned long flags;
949
950 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000951 context = iommu_context_addr(iommu, bus, devfn, 0);
952 if (context)
953 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700954 spin_unlock_irqrestore(&iommu->lock, flags);
955 return ret;
956}
957
958static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
959{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700960 struct context_entry *context;
961 unsigned long flags;
962
963 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000964 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700965 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000966 context_clear_entry(context);
967 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968 }
969 spin_unlock_irqrestore(&iommu->lock, flags);
970}
971
972static void free_context_table(struct intel_iommu *iommu)
973{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700974 int i;
975 unsigned long flags;
976 struct context_entry *context;
977
978 spin_lock_irqsave(&iommu->lock, flags);
979 if (!iommu->root_entry) {
980 goto out;
981 }
982 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000983 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700984 if (context)
985 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000986
David Woodhousec83b2f22015-06-12 10:15:49 +0100987 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000988 continue;
989
990 context = iommu_context_addr(iommu, i, 0x80, 0);
991 if (context)
992 free_pgtable_page(context);
993
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700994 }
995 free_pgtable_page(iommu->root_entry);
996 iommu->root_entry = NULL;
997out:
998 spin_unlock_irqrestore(&iommu->lock, flags);
999}
1000
David Woodhouseb026fd22009-06-28 10:37:25 +01001001static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +00001002 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001003{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001004 struct dma_pte *parent, *pte = NULL;
1005 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -07001006 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001007
1008 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +02001009
Jiang Liu162d1b12014-07-11 14:19:35 +08001010 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +02001011 /* Address beyond IOMMU's addressing capabilities. */
1012 return NULL;
1013
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001014 parent = domain->pgd;
1015
David Woodhouse5cf0a762014-03-19 16:07:49 +00001016 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001017 void *tmp_page;
1018
David Woodhouseb026fd22009-06-28 10:37:25 +01001019 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001020 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +00001021 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001022 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +00001023 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001024 break;
1025
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001026 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +01001027 uint64_t pteval;
1028
Suresh Siddha4c923d42009-10-02 11:01:24 -07001029 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001030
David Woodhouse206a73c2009-07-01 19:30:28 +01001031 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001032 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +01001033
David Woodhousec85994e2009-07-01 19:21:24 +01001034 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -04001035 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 +08001036 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +01001037 /* Someone else set it while we were thinking; use theirs. */
1038 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +08001039 else
David Woodhousec85994e2009-07-01 19:21:24 +01001040 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001041 }
David Woodhouse5cf0a762014-03-19 16:07:49 +00001042 if (level == 1)
1043 break;
1044
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001045 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001046 level--;
1047 }
1048
David Woodhouse5cf0a762014-03-19 16:07:49 +00001049 if (!*target_level)
1050 *target_level = level;
1051
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001052 return pte;
1053}
1054
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001055
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001056/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001057static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1058 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001059 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001060{
1061 struct dma_pte *parent, *pte = NULL;
1062 int total = agaw_to_level(domain->agaw);
1063 int offset;
1064
1065 parent = domain->pgd;
1066 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001067 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001068 pte = &parent[offset];
1069 if (level == total)
1070 return pte;
1071
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001072 if (!dma_pte_present(pte)) {
1073 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001074 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001075 }
1076
Yijing Wange16922a2014-05-20 20:37:51 +08001077 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001078 *large_page = total;
1079 return pte;
1080 }
1081
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001082 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001083 total--;
1084 }
1085 return NULL;
1086}
1087
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001088/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001089static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +01001090 unsigned long start_pfn,
1091 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001092{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001093 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001094 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001095
Jiang Liu162d1b12014-07-11 14:19:35 +08001096 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1097 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001098 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001099
David Woodhouse04b18e62009-06-27 19:15:01 +01001100 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001101 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001102 large_page = 1;
1103 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001104 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001105 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001106 continue;
1107 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001108 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001109 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001110 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001111 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001112 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1113
David Woodhouse310a5ab2009-06-28 18:52:20 +01001114 domain_flush_cache(domain, first_pte,
1115 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001116
1117 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001118}
1119
Alex Williamson3269ee02013-06-15 10:27:19 -06001120static void dma_pte_free_level(struct dmar_domain *domain, int level,
1121 struct dma_pte *pte, unsigned long pfn,
1122 unsigned long start_pfn, unsigned long last_pfn)
1123{
1124 pfn = max(start_pfn, pfn);
1125 pte = &pte[pfn_level_offset(pfn, level)];
1126
1127 do {
1128 unsigned long level_pfn;
1129 struct dma_pte *level_pte;
1130
1131 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1132 goto next;
1133
1134 level_pfn = pfn & level_mask(level - 1);
1135 level_pte = phys_to_virt(dma_pte_addr(pte));
1136
1137 if (level > 2)
1138 dma_pte_free_level(domain, level - 1, level_pte,
1139 level_pfn, start_pfn, last_pfn);
1140
1141 /* If range covers entire pagetable, free it */
1142 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001143 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001144 dma_clear_pte(pte);
1145 domain_flush_cache(domain, pte, sizeof(*pte));
1146 free_pgtable_page(level_pte);
1147 }
1148next:
1149 pfn += level_size(level);
1150 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1151}
1152
Michael S. Tsirkin3d1a2442016-03-23 20:34:19 +02001153/* clear last level (leaf) ptes and free page table pages. */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001154static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001155 unsigned long start_pfn,
1156 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001157{
Jiang Liu162d1b12014-07-11 14:19:35 +08001158 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1159 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001160 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001161
Jiang Liud41a4ad2014-07-11 14:19:34 +08001162 dma_pte_clear_range(domain, start_pfn, last_pfn);
1163
David Woodhousef3a0a522009-06-30 03:40:07 +01001164 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001165 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1166 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001167
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001168 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001169 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001170 free_pgtable_page(domain->pgd);
1171 domain->pgd = NULL;
1172 }
1173}
1174
David Woodhouseea8ea462014-03-05 17:09:32 +00001175/* When a page at a given level is being unlinked from its parent, we don't
1176 need to *modify* it at all. All we need to do is make a list of all the
1177 pages which can be freed just as soon as we've flushed the IOTLB and we
1178 know the hardware page-walk will no longer touch them.
1179 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1180 be freed. */
1181static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1182 int level, struct dma_pte *pte,
1183 struct page *freelist)
1184{
1185 struct page *pg;
1186
1187 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1188 pg->freelist = freelist;
1189 freelist = pg;
1190
1191 if (level == 1)
1192 return freelist;
1193
Jiang Liuadeb25902014-04-09 10:20:39 +08001194 pte = page_address(pg);
1195 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001196 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1197 freelist = dma_pte_list_pagetables(domain, level - 1,
1198 pte, freelist);
Jiang Liuadeb25902014-04-09 10:20:39 +08001199 pte++;
1200 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001201
1202 return freelist;
1203}
1204
1205static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1206 struct dma_pte *pte, unsigned long pfn,
1207 unsigned long start_pfn,
1208 unsigned long last_pfn,
1209 struct page *freelist)
1210{
1211 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1212
1213 pfn = max(start_pfn, pfn);
1214 pte = &pte[pfn_level_offset(pfn, level)];
1215
1216 do {
1217 unsigned long level_pfn;
1218
1219 if (!dma_pte_present(pte))
1220 goto next;
1221
1222 level_pfn = pfn & level_mask(level);
1223
1224 /* If range covers entire pagetable, free it */
1225 if (start_pfn <= level_pfn &&
1226 last_pfn >= level_pfn + level_size(level) - 1) {
1227 /* These suborbinate page tables are going away entirely. Don't
1228 bother to clear them; we're just going to *free* them. */
1229 if (level > 1 && !dma_pte_superpage(pte))
1230 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1231
1232 dma_clear_pte(pte);
1233 if (!first_pte)
1234 first_pte = pte;
1235 last_pte = pte;
1236 } else if (level > 1) {
1237 /* Recurse down into a level that isn't *entirely* obsolete */
1238 freelist = dma_pte_clear_level(domain, level - 1,
1239 phys_to_virt(dma_pte_addr(pte)),
1240 level_pfn, start_pfn, last_pfn,
1241 freelist);
1242 }
1243next:
1244 pfn += level_size(level);
1245 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1246
1247 if (first_pte)
1248 domain_flush_cache(domain, first_pte,
1249 (void *)++last_pte - (void *)first_pte);
1250
1251 return freelist;
1252}
1253
1254/* We can't just free the pages because the IOMMU may still be walking
1255 the page tables, and may have cached the intermediate levels. The
1256 pages can only be freed after the IOTLB flush has been done. */
Joerg Roedelb6904202015-08-13 11:32:18 +02001257static struct page *domain_unmap(struct dmar_domain *domain,
1258 unsigned long start_pfn,
1259 unsigned long last_pfn)
David Woodhouseea8ea462014-03-05 17:09:32 +00001260{
David Woodhouseea8ea462014-03-05 17:09:32 +00001261 struct page *freelist = NULL;
1262
Jiang Liu162d1b12014-07-11 14:19:35 +08001263 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1264 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001265 BUG_ON(start_pfn > last_pfn);
1266
1267 /* we don't need lock here; nobody else touches the iova range */
1268 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1269 domain->pgd, 0, start_pfn, last_pfn, NULL);
1270
1271 /* free pgd */
1272 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1273 struct page *pgd_page = virt_to_page(domain->pgd);
1274 pgd_page->freelist = freelist;
1275 freelist = pgd_page;
1276
1277 domain->pgd = NULL;
1278 }
1279
1280 return freelist;
1281}
1282
Joerg Roedelb6904202015-08-13 11:32:18 +02001283static void dma_free_pagelist(struct page *freelist)
David Woodhouseea8ea462014-03-05 17:09:32 +00001284{
1285 struct page *pg;
1286
1287 while ((pg = freelist)) {
1288 freelist = pg->freelist;
1289 free_pgtable_page(page_address(pg));
1290 }
1291}
1292
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001293/* iommu handling */
1294static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1295{
1296 struct root_entry *root;
1297 unsigned long flags;
1298
Suresh Siddha4c923d42009-10-02 11:01:24 -07001299 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001300 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001301 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001302 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001303 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001304 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001305
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001306 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001307
1308 spin_lock_irqsave(&iommu->lock, flags);
1309 iommu->root_entry = root;
1310 spin_unlock_irqrestore(&iommu->lock, flags);
1311
1312 return 0;
1313}
1314
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001315static void iommu_set_root_entry(struct intel_iommu *iommu)
1316{
David Woodhouse03ecc322015-02-13 14:35:21 +00001317 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001318 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001319 unsigned long flag;
1320
David Woodhouse03ecc322015-02-13 14:35:21 +00001321 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001322 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001323 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001324
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001325 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001326 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001327
David Woodhousec416daa2009-05-10 20:30:58 +01001328 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001329
1330 /* Make sure hardware complete it */
1331 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001332 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001333
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001334 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001335}
1336
1337static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1338{
1339 u32 val;
1340 unsigned long flag;
1341
David Woodhouse9af88142009-02-13 23:18:03 +00001342 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001343 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001344
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001345 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001346 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001347
1348 /* Make sure hardware complete it */
1349 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001350 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001351
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001352 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001353}
1354
1355/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001356static void __iommu_flush_context(struct intel_iommu *iommu,
1357 u16 did, u16 source_id, u8 function_mask,
1358 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001359{
1360 u64 val = 0;
1361 unsigned long flag;
1362
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001363 switch (type) {
1364 case DMA_CCMD_GLOBAL_INVL:
1365 val = DMA_CCMD_GLOBAL_INVL;
1366 break;
1367 case DMA_CCMD_DOMAIN_INVL:
1368 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1369 break;
1370 case DMA_CCMD_DEVICE_INVL:
1371 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1372 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1373 break;
1374 default:
1375 BUG();
1376 }
1377 val |= DMA_CCMD_ICC;
1378
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001379 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001380 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1381
1382 /* Make sure hardware complete it */
1383 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1384 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1385
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001386 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001387}
1388
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001389/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001390static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1391 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001392{
1393 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1394 u64 val = 0, val_iva = 0;
1395 unsigned long flag;
1396
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001397 switch (type) {
1398 case DMA_TLB_GLOBAL_FLUSH:
1399 /* global flush doesn't need set IVA_REG */
1400 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1401 break;
1402 case DMA_TLB_DSI_FLUSH:
1403 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1404 break;
1405 case DMA_TLB_PSI_FLUSH:
1406 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001407 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001408 val_iva = size_order | addr;
1409 break;
1410 default:
1411 BUG();
1412 }
1413 /* Note: set drain read/write */
1414#if 0
1415 /*
1416 * This is probably to be super secure.. Looks like we can
1417 * ignore it without any impact.
1418 */
1419 if (cap_read_drain(iommu->cap))
1420 val |= DMA_TLB_READ_DRAIN;
1421#endif
1422 if (cap_write_drain(iommu->cap))
1423 val |= DMA_TLB_WRITE_DRAIN;
1424
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001425 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001426 /* Note: Only uses first TLB reg currently */
1427 if (val_iva)
1428 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1429 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1430
1431 /* Make sure hardware complete it */
1432 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1433 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1434
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001435 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436
1437 /* check IOTLB invalidation granularity */
1438 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001439 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001440 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001441 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001442 (unsigned long long)DMA_TLB_IIRG(type),
1443 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001444}
1445
David Woodhouse64ae8922014-03-09 12:52:30 -07001446static struct device_domain_info *
1447iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1448 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001449{
Yu Zhao93a23a72009-05-18 13:51:37 +08001450 struct device_domain_info *info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001451
Joerg Roedel55d94042015-07-22 16:50:40 +02001452 assert_spin_locked(&device_domain_lock);
1453
Yu Zhao93a23a72009-05-18 13:51:37 +08001454 if (!iommu->qi)
1455 return NULL;
1456
Yu Zhao93a23a72009-05-18 13:51:37 +08001457 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001458 if (info->iommu == iommu && info->bus == bus &&
1459 info->devfn == devfn) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001460 if (info->ats_supported && info->dev)
1461 return info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001462 break;
1463 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001464
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001465 return NULL;
Yu Zhao93a23a72009-05-18 13:51:37 +08001466}
1467
Omer Peleg0824c592016-04-20 19:03:35 +03001468static void domain_update_iotlb(struct dmar_domain *domain)
1469{
1470 struct device_domain_info *info;
1471 bool has_iotlb_device = false;
1472
1473 assert_spin_locked(&device_domain_lock);
1474
1475 list_for_each_entry(info, &domain->devices, link) {
1476 struct pci_dev *pdev;
1477
1478 if (!info->dev || !dev_is_pci(info->dev))
1479 continue;
1480
1481 pdev = to_pci_dev(info->dev);
1482 if (pdev->ats_enabled) {
1483 has_iotlb_device = true;
1484 break;
1485 }
1486 }
1487
1488 domain->has_iotlb_device = has_iotlb_device;
1489}
1490
Yu Zhao93a23a72009-05-18 13:51:37 +08001491static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1492{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001493 struct pci_dev *pdev;
1494
Omer Peleg0824c592016-04-20 19:03:35 +03001495 assert_spin_locked(&device_domain_lock);
1496
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001497 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001498 return;
1499
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001500 pdev = to_pci_dev(info->dev);
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001501
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001502#ifdef CONFIG_INTEL_IOMMU_SVM
1503 /* The PCIe spec, in its wisdom, declares that the behaviour of
1504 the device if you enable PASID support after ATS support is
1505 undefined. So always enable PASID support on devices which
1506 have it, even if we can't yet know if we're ever going to
1507 use it. */
1508 if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
1509 info->pasid_enabled = 1;
1510
1511 if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
1512 info->pri_enabled = 1;
1513#endif
1514 if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
1515 info->ats_enabled = 1;
Omer Peleg0824c592016-04-20 19:03:35 +03001516 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001517 info->ats_qdep = pci_ats_queue_depth(pdev);
1518 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001519}
1520
1521static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1522{
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001523 struct pci_dev *pdev;
1524
Omer Peleg0824c592016-04-20 19:03:35 +03001525 assert_spin_locked(&device_domain_lock);
1526
Jeremy McNicollda972fb2016-01-14 21:33:06 -08001527 if (!dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001528 return;
1529
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001530 pdev = to_pci_dev(info->dev);
1531
1532 if (info->ats_enabled) {
1533 pci_disable_ats(pdev);
1534 info->ats_enabled = 0;
Omer Peleg0824c592016-04-20 19:03:35 +03001535 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001536 }
1537#ifdef CONFIG_INTEL_IOMMU_SVM
1538 if (info->pri_enabled) {
1539 pci_disable_pri(pdev);
1540 info->pri_enabled = 0;
1541 }
1542 if (info->pasid_enabled) {
1543 pci_disable_pasid(pdev);
1544 info->pasid_enabled = 0;
1545 }
1546#endif
Yu Zhao93a23a72009-05-18 13:51:37 +08001547}
1548
1549static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1550 u64 addr, unsigned mask)
1551{
1552 u16 sid, qdep;
1553 unsigned long flags;
1554 struct device_domain_info *info;
1555
Omer Peleg0824c592016-04-20 19:03:35 +03001556 if (!domain->has_iotlb_device)
1557 return;
1558
Yu Zhao93a23a72009-05-18 13:51:37 +08001559 spin_lock_irqsave(&device_domain_lock, flags);
1560 list_for_each_entry(info, &domain->devices, link) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001561 if (!info->ats_enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001562 continue;
1563
1564 sid = info->bus << 8 | info->devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001565 qdep = info->ats_qdep;
Yu Zhao93a23a72009-05-18 13:51:37 +08001566 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1567 }
1568 spin_unlock_irqrestore(&device_domain_lock, flags);
1569}
1570
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001571static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1572 struct dmar_domain *domain,
1573 unsigned long pfn, unsigned int pages,
1574 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001575{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001576 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001577 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001578 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001579
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001580 BUG_ON(pages == 0);
1581
David Woodhouseea8ea462014-03-05 17:09:32 +00001582 if (ih)
1583 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001584 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001585 * Fallback to domain selective flush if no PSI support or the size is
1586 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001587 * PSI requires page size to be 2 ^ x, and the base address is naturally
1588 * aligned to the size
1589 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001590 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1591 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001592 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001593 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001594 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001595 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001596
1597 /*
Nadav Amit82653632010-04-01 13:24:40 +03001598 * In caching mode, changes of pages from non-present to present require
1599 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001600 */
Nadav Amit82653632010-04-01 13:24:40 +03001601 if (!cap_caching_mode(iommu->cap) || !map)
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001602 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1603 addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001604}
1605
mark grossf8bab732008-02-08 04:18:38 -08001606static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1607{
1608 u32 pmen;
1609 unsigned long flags;
1610
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001611 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001612 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1613 pmen &= ~DMA_PMEN_EPM;
1614 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1615
1616 /* wait for the protected region status bit to clear */
1617 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1618 readl, !(pmen & DMA_PMEN_PRS), pmen);
1619
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001620 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001621}
1622
Jiang Liu2a41cce2014-07-11 14:19:33 +08001623static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001624{
1625 u32 sts;
1626 unsigned long flags;
1627
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001628 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001629 iommu->gcmd |= DMA_GCMD_TE;
1630 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001631
1632 /* Make sure hardware complete it */
1633 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001634 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001635
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001636 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001637}
1638
Jiang Liu2a41cce2014-07-11 14:19:33 +08001639static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001640{
1641 u32 sts;
1642 unsigned long flag;
1643
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001644 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001645 iommu->gcmd &= ~DMA_GCMD_TE;
1646 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1647
1648 /* Make sure hardware complete it */
1649 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001650 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001651
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001652 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001653}
1654
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001655
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001656static int iommu_init_domains(struct intel_iommu *iommu)
1657{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001658 u32 ndomains, nlongs;
1659 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001660
1661 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001662 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001663 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001664 nlongs = BITS_TO_LONGS(ndomains);
1665
Donald Dutile94a91b52009-08-20 16:51:34 -04001666 spin_lock_init(&iommu->lock);
1667
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001668 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1669 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001670 pr_err("%s: Allocating domain id array failed\n",
1671 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001672 return -ENOMEM;
1673 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001674
Wei Yang86f004c2016-05-21 02:41:51 +00001675 size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001676 iommu->domains = kzalloc(size, GFP_KERNEL);
1677
1678 if (iommu->domains) {
1679 size = 256 * sizeof(struct dmar_domain *);
1680 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1681 }
1682
1683 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001684 pr_err("%s: Allocating domain array failed\n",
1685 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001686 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001687 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001688 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001689 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001690 return -ENOMEM;
1691 }
1692
Joerg Roedel8bf47812015-07-21 10:41:21 +02001693
1694
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001695 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001696 * If Caching mode is set, then invalid translations are tagged
1697 * with domain-id 0, hence we need to pre-allocate it. We also
1698 * use domain-id 0 as a marker for non-allocated domain-id, so
1699 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001700 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001701 set_bit(0, iommu->domain_ids);
1702
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001703 return 0;
1704}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001705
Jiang Liuffebeb42014-11-09 22:48:02 +08001706static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001707{
Joerg Roedel29a27712015-07-21 17:17:12 +02001708 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001709 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710
Joerg Roedel29a27712015-07-21 17:17:12 +02001711 if (!iommu->domains || !iommu->domain_ids)
1712 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001713
Joerg Roedelbea64032016-11-08 15:08:26 +01001714again:
Joerg Roedel55d94042015-07-22 16:50:40 +02001715 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001716 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1717 struct dmar_domain *domain;
1718
1719 if (info->iommu != iommu)
1720 continue;
1721
1722 if (!info->dev || !info->domain)
1723 continue;
1724
1725 domain = info->domain;
1726
Joerg Roedelbea64032016-11-08 15:08:26 +01001727 __dmar_remove_one_dev_info(info);
Joerg Roedel29a27712015-07-21 17:17:12 +02001728
Joerg Roedelbea64032016-11-08 15:08:26 +01001729 if (!domain_type_is_vm_or_si(domain)) {
1730 /*
1731 * The domain_exit() function can't be called under
1732 * device_domain_lock, as it takes this lock itself.
1733 * So release the lock here and re-run the loop
1734 * afterwards.
1735 */
1736 spin_unlock_irqrestore(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001737 domain_exit(domain);
Joerg Roedelbea64032016-11-08 15:08:26 +01001738 goto again;
1739 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001740 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001741 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001742
1743 if (iommu->gcmd & DMA_GCMD_TE)
1744 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001745}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001746
Jiang Liuffebeb42014-11-09 22:48:02 +08001747static void free_dmar_iommu(struct intel_iommu *iommu)
1748{
1749 if ((iommu->domains) && (iommu->domain_ids)) {
Wei Yang86f004c2016-05-21 02:41:51 +00001750 int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001751 int i;
1752
1753 for (i = 0; i < elems; i++)
1754 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001755 kfree(iommu->domains);
1756 kfree(iommu->domain_ids);
1757 iommu->domains = NULL;
1758 iommu->domain_ids = NULL;
1759 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001760
Weidong Hand9630fe2008-12-08 11:06:32 +08001761 g_iommus[iommu->seq_id] = NULL;
1762
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001763 /* free context mapping */
1764 free_context_table(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001765
1766#ifdef CONFIG_INTEL_IOMMU_SVM
David Woodhousea222a7f2015-10-07 23:35:18 +01001767 if (pasid_enabled(iommu)) {
1768 if (ecap_prs(iommu->ecap))
1769 intel_svm_finish_prq(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001770 intel_svm_free_pasid_tables(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01001771 }
David Woodhouse8a94ade2015-03-24 14:54:56 +00001772#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001773}
1774
Jiang Liuab8dfe22014-07-11 14:19:27 +08001775static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001776{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001777 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001778
1779 domain = alloc_domain_mem();
1780 if (!domain)
1781 return NULL;
1782
Jiang Liuab8dfe22014-07-11 14:19:27 +08001783 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001784 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001785 domain->flags = flags;
Omer Peleg0824c592016-04-20 19:03:35 +03001786 domain->has_iotlb_device = false;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001787 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001788
1789 return domain;
1790}
1791
Joerg Roedeld160aca2015-07-22 11:52:53 +02001792/* Must be called with iommu->lock */
1793static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001794 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001795{
Jiang Liu44bde612014-07-11 14:19:29 +08001796 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001797 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001798
Joerg Roedel55d94042015-07-22 16:50:40 +02001799 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001800 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001801
Joerg Roedel29a27712015-07-21 17:17:12 +02001802 domain->iommu_refcnt[iommu->seq_id] += 1;
1803 domain->iommu_count += 1;
1804 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001805 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001806 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1807
1808 if (num >= ndomains) {
1809 pr_err("%s: No free domain ids\n", iommu->name);
1810 domain->iommu_refcnt[iommu->seq_id] -= 1;
1811 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001812 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001813 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001814
Joerg Roedeld160aca2015-07-22 11:52:53 +02001815 set_bit(num, iommu->domain_ids);
1816 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001817
Joerg Roedeld160aca2015-07-22 11:52:53 +02001818 domain->iommu_did[iommu->seq_id] = num;
1819 domain->nid = iommu->node;
1820
Jiang Liufb170fb2014-07-11 14:19:28 +08001821 domain_update_iommu_cap(domain);
1822 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001823
Joerg Roedel55d94042015-07-22 16:50:40 +02001824 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001825}
1826
1827static int domain_detach_iommu(struct dmar_domain *domain,
1828 struct intel_iommu *iommu)
1829{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001830 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001831
Joerg Roedel55d94042015-07-22 16:50:40 +02001832 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001833 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001834
Joerg Roedel29a27712015-07-21 17:17:12 +02001835 domain->iommu_refcnt[iommu->seq_id] -= 1;
1836 count = --domain->iommu_count;
1837 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001838 num = domain->iommu_did[iommu->seq_id];
1839 clear_bit(num, iommu->domain_ids);
1840 set_iommu_domain(iommu, num, NULL);
1841
Jiang Liufb170fb2014-07-11 14:19:28 +08001842 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001843 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001844 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001845
1846 return count;
1847}
1848
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001849static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001850static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001851
Joseph Cihula51a63e62011-03-21 11:04:24 -07001852static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001853{
1854 struct pci_dev *pdev = NULL;
1855 struct iova *iova;
1856 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001857
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001858 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1859 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001860
Mark Gross8a443df2008-03-04 14:59:31 -08001861 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1862 &reserved_rbtree_key);
1863
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001864 /* IOAPIC ranges shouldn't be accessed by DMA */
1865 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1866 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001867 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001868 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001869 return -ENODEV;
1870 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001871
1872 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1873 for_each_pci_dev(pdev) {
1874 struct resource *r;
1875
1876 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1877 r = &pdev->resource[i];
1878 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1879 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001880 iova = reserve_iova(&reserved_iova_list,
1881 IOVA_PFN(r->start),
1882 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001883 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001884 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001885 return -ENODEV;
1886 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001887 }
1888 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001889 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001890}
1891
1892static void domain_reserve_special_ranges(struct dmar_domain *domain)
1893{
1894 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1895}
1896
1897static inline int guestwidth_to_adjustwidth(int gaw)
1898{
1899 int agaw;
1900 int r = (gaw - 12) % 9;
1901
1902 if (r == 0)
1903 agaw = gaw;
1904 else
1905 agaw = gaw + 9 - r;
1906 if (agaw > 64)
1907 agaw = 64;
1908 return agaw;
1909}
1910
Joerg Roedeldc534b22015-07-22 12:44:02 +02001911static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1912 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001913{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001914 int adjust_width, agaw;
1915 unsigned long sagaw;
1916
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001917 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1918 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001919 domain_reserve_special_ranges(domain);
1920
1921 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001922 if (guest_width > cap_mgaw(iommu->cap))
1923 guest_width = cap_mgaw(iommu->cap);
1924 domain->gaw = guest_width;
1925 adjust_width = guestwidth_to_adjustwidth(guest_width);
1926 agaw = width_to_agaw(adjust_width);
1927 sagaw = cap_sagaw(iommu->cap);
1928 if (!test_bit(agaw, &sagaw)) {
1929 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001930 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001931 agaw = find_next_bit(&sagaw, 5, agaw);
1932 if (agaw >= 5)
1933 return -ENODEV;
1934 }
1935 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001936
Weidong Han8e6040972008-12-08 15:49:06 +08001937 if (ecap_coherent(iommu->ecap))
1938 domain->iommu_coherency = 1;
1939 else
1940 domain->iommu_coherency = 0;
1941
Sheng Yang58c610b2009-03-18 15:33:05 +08001942 if (ecap_sc_support(iommu->ecap))
1943 domain->iommu_snooping = 1;
1944 else
1945 domain->iommu_snooping = 0;
1946
David Woodhouse214e39a2014-03-19 10:38:49 +00001947 if (intel_iommu_superpage)
1948 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1949 else
1950 domain->iommu_superpage = 0;
1951
Suresh Siddha4c923d42009-10-02 11:01:24 -07001952 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001953
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001954 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001955 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001956 if (!domain->pgd)
1957 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001958 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001959 return 0;
1960}
1961
1962static void domain_exit(struct dmar_domain *domain)
1963{
David Woodhouseea8ea462014-03-05 17:09:32 +00001964 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001965
1966 /* Domain 0 is reserved, so dont process it */
1967 if (!domain)
1968 return;
1969
Alex Williamson7b668352011-05-24 12:02:41 +01001970 /* Flush any lazy unmaps that may reference this domain */
Omer Pelegaa473242016-04-20 11:33:02 +03001971 if (!intel_iommu_strict) {
1972 int cpu;
1973
1974 for_each_possible_cpu(cpu)
1975 flush_unmaps_timeout(cpu);
1976 }
Alex Williamson7b668352011-05-24 12:02:41 +01001977
Joerg Roedeld160aca2015-07-22 11:52:53 +02001978 /* Remove associated devices and clear attached or cached domains */
1979 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001980 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001981 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001982
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001983 /* destroy iovas */
1984 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001985
David Woodhouseea8ea462014-03-05 17:09:32 +00001986 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001987
David Woodhouseea8ea462014-03-05 17:09:32 +00001988 dma_free_pagelist(freelist);
1989
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001990 free_domain_mem(domain);
1991}
1992
David Woodhouse64ae8922014-03-09 12:52:30 -07001993static int domain_context_mapping_one(struct dmar_domain *domain,
1994 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001995 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001996{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001997 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001998 int translation = CONTEXT_TT_MULTI_LEVEL;
1999 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002000 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002001 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08002002 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02002003 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02002004
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002005 WARN_ON(did == 0);
2006
Joerg Roedel28ccce02015-07-21 14:45:31 +02002007 if (hw_pass_through && domain_type_is_si(domain))
2008 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002009
2010 pr_debug("Set context mapping for %02x:%02x.%d\n",
2011 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002012
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002013 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08002014
Joerg Roedel55d94042015-07-22 16:50:40 +02002015 spin_lock_irqsave(&device_domain_lock, flags);
2016 spin_lock(&iommu->lock);
2017
2018 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00002019 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002020 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02002021 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002022
Joerg Roedel55d94042015-07-22 16:50:40 +02002023 ret = 0;
2024 if (context_present(context))
2025 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02002026
Weidong Hanea6606b2008-12-08 23:08:15 +08002027 pgd = domain->pgd;
2028
Joerg Roedelde24e552015-07-21 14:53:04 +02002029 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002030 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08002031
Joerg Roedelde24e552015-07-21 14:53:04 +02002032 /*
2033 * Skip top levels of page tables for iommu which has less agaw
2034 * than default. Unnecessary for PT mode.
2035 */
Yu Zhao93a23a72009-05-18 13:51:37 +08002036 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02002037 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02002038 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02002039 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02002040 if (!dma_pte_present(pgd))
2041 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02002042 }
2043
David Woodhouse64ae8922014-03-09 12:52:30 -07002044 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002045 if (info && info->ats_supported)
2046 translation = CONTEXT_TT_DEV_IOTLB;
2047 else
2048 translation = CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02002049
Yu Zhao93a23a72009-05-18 13:51:37 +08002050 context_set_address_root(context, virt_to_phys(pgd));
2051 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02002052 } else {
2053 /*
2054 * In pass through mode, AW must be programmed to
2055 * indicate the largest AGAW value supported by
2056 * hardware. And ASR is ignored by hardware.
2057 */
2058 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08002059 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002060
2061 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00002062 context_set_fault_enable(context);
2063 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08002064 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002065
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002066 /*
2067 * It's a non-present to present mapping. If hardware doesn't cache
2068 * non-present entry we only need to flush the write-buffer. If the
2069 * _does_ cache non-present entries, then it does so in the special
2070 * domain #0, which we have to flush:
2071 */
2072 if (cap_caching_mode(iommu->cap)) {
2073 iommu->flush.flush_context(iommu, 0,
2074 (((u16)bus) << 8) | devfn,
2075 DMA_CCMD_MASK_NOBIT,
2076 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002077 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002078 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002079 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002080 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002081 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08002082
Joerg Roedel55d94042015-07-22 16:50:40 +02002083 ret = 0;
2084
2085out_unlock:
2086 spin_unlock(&iommu->lock);
2087 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08002088
Wei Yang5c365d12016-07-13 13:53:21 +00002089 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002090}
2091
Alex Williamson579305f2014-07-03 09:51:43 -06002092struct domain_context_mapping_data {
2093 struct dmar_domain *domain;
2094 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002095};
2096
2097static int domain_context_mapping_cb(struct pci_dev *pdev,
2098 u16 alias, void *opaque)
2099{
2100 struct domain_context_mapping_data *data = opaque;
2101
2102 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002103 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002104}
2105
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002106static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002107domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002108{
David Woodhouse64ae8922014-03-09 12:52:30 -07002109 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002110 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002111 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002112
David Woodhousee1f167f2014-03-09 15:24:46 -07002113 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002114 if (!iommu)
2115 return -ENODEV;
2116
Alex Williamson579305f2014-07-03 09:51:43 -06002117 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002118 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002119
2120 data.domain = domain;
2121 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002122
2123 return pci_for_each_dma_alias(to_pci_dev(dev),
2124 &domain_context_mapping_cb, &data);
2125}
2126
2127static int domain_context_mapped_cb(struct pci_dev *pdev,
2128 u16 alias, void *opaque)
2129{
2130 struct intel_iommu *iommu = opaque;
2131
2132 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002133}
2134
David Woodhousee1f167f2014-03-09 15:24:46 -07002135static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002136{
Weidong Han5331fe62008-12-08 23:00:00 +08002137 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002138 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002139
David Woodhousee1f167f2014-03-09 15:24:46 -07002140 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002141 if (!iommu)
2142 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002143
Alex Williamson579305f2014-07-03 09:51:43 -06002144 if (!dev_is_pci(dev))
2145 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002146
Alex Williamson579305f2014-07-03 09:51:43 -06002147 return !pci_for_each_dma_alias(to_pci_dev(dev),
2148 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002149}
2150
Fenghua Yuf5329592009-08-04 15:09:37 -07002151/* Returns a number of VTD pages, but aligned to MM page size */
2152static inline unsigned long aligned_nrpages(unsigned long host_addr,
2153 size_t size)
2154{
2155 host_addr &= ~PAGE_MASK;
2156 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2157}
2158
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002159/* Return largest possible superpage level for a given mapping */
2160static inline int hardware_largepage_caps(struct dmar_domain *domain,
2161 unsigned long iov_pfn,
2162 unsigned long phy_pfn,
2163 unsigned long pages)
2164{
2165 int support, level = 1;
2166 unsigned long pfnmerge;
2167
2168 support = domain->iommu_superpage;
2169
2170 /* To use a large page, the virtual *and* physical addresses
2171 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2172 of them will mean we have to use smaller pages. So just
2173 merge them and check both at once. */
2174 pfnmerge = iov_pfn | phy_pfn;
2175
2176 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2177 pages >>= VTD_STRIDE_SHIFT;
2178 if (!pages)
2179 break;
2180 pfnmerge >>= VTD_STRIDE_SHIFT;
2181 level++;
2182 support--;
2183 }
2184 return level;
2185}
2186
David Woodhouse9051aa02009-06-29 12:30:54 +01002187static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2188 struct scatterlist *sg, unsigned long phys_pfn,
2189 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002190{
2191 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002192 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002193 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002194 unsigned int largepage_lvl = 0;
2195 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002196
Jiang Liu162d1b12014-07-11 14:19:35 +08002197 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002198
2199 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2200 return -EINVAL;
2201
2202 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2203
Jiang Liucc4f14a2014-11-26 09:42:10 +08002204 if (!sg) {
2205 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002206 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2207 }
2208
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002209 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002210 uint64_t tmp;
2211
David Woodhousee1605492009-06-29 11:17:38 +01002212 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002213 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002214 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2215 sg->dma_length = sg->length;
Dan Williams3e6110f2015-12-15 12:54:06 -08002216 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002217 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002218 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002219
David Woodhousee1605492009-06-29 11:17:38 +01002220 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002221 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2222
David Woodhouse5cf0a762014-03-19 16:07:49 +00002223 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002224 if (!pte)
2225 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002226 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002227 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002228 unsigned long nr_superpages, end_pfn;
2229
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002230 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002231 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002232
2233 nr_superpages = sg_res / lvl_pages;
2234 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2235
Jiang Liud41a4ad2014-07-11 14:19:34 +08002236 /*
2237 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002238 * removed to make room for superpage(s).
Jiang Liud41a4ad2014-07-11 14:19:34 +08002239 */
Christian Zanderba2374f2015-06-10 09:41:45 -07002240 dma_pte_free_pagetable(domain, iov_pfn, end_pfn);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002241 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002242 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002243 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002244
David Woodhousee1605492009-06-29 11:17:38 +01002245 }
2246 /* We don't need lock here, nobody else
2247 * touches the iova range
2248 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002249 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002250 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002251 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002252 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2253 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002254 if (dumps) {
2255 dumps--;
2256 debug_dma_dump_mappings(NULL);
2257 }
2258 WARN_ON(1);
2259 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002260
2261 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2262
2263 BUG_ON(nr_pages < lvl_pages);
2264 BUG_ON(sg_res < lvl_pages);
2265
2266 nr_pages -= lvl_pages;
2267 iov_pfn += lvl_pages;
2268 phys_pfn += lvl_pages;
2269 pteval += lvl_pages * VTD_PAGE_SIZE;
2270 sg_res -= lvl_pages;
2271
2272 /* If the next PTE would be the first in a new page, then we
2273 need to flush the cache on the entries we've just written.
2274 And then we'll need to recalculate 'pte', so clear it and
2275 let it get set again in the if (!pte) block above.
2276
2277 If we're done (!nr_pages) we need to flush the cache too.
2278
2279 Also if we've been setting superpages, we may need to
2280 recalculate 'pte' and switch back to smaller pages for the
2281 end of the mapping, if the trailing size is not enough to
2282 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002283 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002284 if (!nr_pages || first_pte_in_page(pte) ||
2285 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002286 domain_flush_cache(domain, first_pte,
2287 (void *)pte - (void *)first_pte);
2288 pte = NULL;
2289 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002290
2291 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002292 sg = sg_next(sg);
2293 }
2294 return 0;
2295}
2296
David Woodhouse9051aa02009-06-29 12:30:54 +01002297static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2298 struct scatterlist *sg, unsigned long nr_pages,
2299 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002300{
David Woodhouse9051aa02009-06-29 12:30:54 +01002301 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2302}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002303
David Woodhouse9051aa02009-06-29 12:30:54 +01002304static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2305 unsigned long phys_pfn, unsigned long nr_pages,
2306 int prot)
2307{
2308 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002309}
2310
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002311static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002312{
Weidong Hanc7151a82008-12-08 22:51:37 +08002313 if (!iommu)
2314 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002315
2316 clear_context_table(iommu, bus, devfn);
2317 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002318 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002319 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002320}
2321
David Woodhouse109b9b02012-05-25 17:43:02 +01002322static inline void unlink_domain_info(struct device_domain_info *info)
2323{
2324 assert_spin_locked(&device_domain_lock);
2325 list_del(&info->link);
2326 list_del(&info->global);
2327 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002328 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002329}
2330
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002331static void domain_remove_dev_info(struct dmar_domain *domain)
2332{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002333 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002334 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002335
2336 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002337 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002338 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002339 spin_unlock_irqrestore(&device_domain_lock, flags);
2340}
2341
2342/*
2343 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002344 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002345 */
David Woodhouse1525a292014-03-06 16:19:30 +00002346static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002347{
2348 struct device_domain_info *info;
2349
2350 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002351 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002352 if (info)
2353 return info->domain;
2354 return NULL;
2355}
2356
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002357static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002358dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2359{
2360 struct device_domain_info *info;
2361
2362 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002363 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002364 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002365 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002366
2367 return NULL;
2368}
2369
Joerg Roedel5db31562015-07-22 12:40:43 +02002370static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2371 int bus, int devfn,
2372 struct device *dev,
2373 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002374{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002375 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002376 struct device_domain_info *info;
2377 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002378 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002379
2380 info = alloc_devinfo_mem();
2381 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002382 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002383
Jiang Liu745f2582014-02-19 14:07:26 +08002384 info->bus = bus;
2385 info->devfn = devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002386 info->ats_supported = info->pasid_supported = info->pri_supported = 0;
2387 info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
2388 info->ats_qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002389 info->dev = dev;
2390 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002391 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002392
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002393 if (dev && dev_is_pci(dev)) {
2394 struct pci_dev *pdev = to_pci_dev(info->dev);
2395
2396 if (ecap_dev_iotlb_support(iommu->ecap) &&
2397 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
2398 dmar_find_matched_atsr_unit(pdev))
2399 info->ats_supported = 1;
2400
2401 if (ecs_enabled(iommu)) {
2402 if (pasid_enabled(iommu)) {
2403 int features = pci_pasid_features(pdev);
2404 if (features >= 0)
2405 info->pasid_supported = features | 1;
2406 }
2407
2408 if (info->ats_supported && ecap_prs(iommu->ecap) &&
2409 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
2410 info->pri_supported = 1;
2411 }
2412 }
2413
Jiang Liu745f2582014-02-19 14:07:26 +08002414 spin_lock_irqsave(&device_domain_lock, flags);
2415 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002416 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002417
2418 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002419 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002420 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002421 if (info2) {
2422 found = info2->domain;
2423 info2->dev = dev;
2424 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002425 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002426
Jiang Liu745f2582014-02-19 14:07:26 +08002427 if (found) {
2428 spin_unlock_irqrestore(&device_domain_lock, flags);
2429 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002430 /* Caller must free the original domain */
2431 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002432 }
2433
Joerg Roedeld160aca2015-07-22 11:52:53 +02002434 spin_lock(&iommu->lock);
2435 ret = domain_attach_iommu(domain, iommu);
2436 spin_unlock(&iommu->lock);
2437
2438 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002439 spin_unlock_irqrestore(&device_domain_lock, flags);
Sudip Mukherjee499f3aa2015-09-18 16:27:07 +05302440 free_devinfo_mem(info);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002441 return NULL;
2442 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002443
David Woodhouseb718cd32014-03-09 13:11:33 -07002444 list_add(&info->link, &domain->devices);
2445 list_add(&info->global, &device_domain_list);
2446 if (dev)
2447 dev->archdata.iommu = info;
2448 spin_unlock_irqrestore(&device_domain_lock, flags);
2449
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002450 if (dev && domain_context_mapping(domain, dev)) {
2451 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002452 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002453 return NULL;
2454 }
2455
David Woodhouseb718cd32014-03-09 13:11:33 -07002456 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002457}
2458
Alex Williamson579305f2014-07-03 09:51:43 -06002459static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2460{
2461 *(u16 *)opaque = alias;
2462 return 0;
2463}
2464
Joerg Roedel76208352016-08-25 14:25:12 +02002465static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002466{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002467 struct device_domain_info *info = NULL;
Joerg Roedel76208352016-08-25 14:25:12 +02002468 struct dmar_domain *domain = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002469 struct intel_iommu *iommu;
Joerg Roedel08a7f452015-07-23 18:09:11 +02002470 u16 req_id, dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002471 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002472 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002473
David Woodhouse146922e2014-03-09 15:44:17 -07002474 iommu = device_to_iommu(dev, &bus, &devfn);
2475 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002476 return NULL;
2477
Joerg Roedel08a7f452015-07-23 18:09:11 +02002478 req_id = ((u16)bus << 8) | devfn;
2479
Alex Williamson579305f2014-07-03 09:51:43 -06002480 if (dev_is_pci(dev)) {
2481 struct pci_dev *pdev = to_pci_dev(dev);
2482
2483 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2484
2485 spin_lock_irqsave(&device_domain_lock, flags);
2486 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2487 PCI_BUS_NUM(dma_alias),
2488 dma_alias & 0xff);
2489 if (info) {
2490 iommu = info->iommu;
2491 domain = info->domain;
2492 }
2493 spin_unlock_irqrestore(&device_domain_lock, flags);
2494
Joerg Roedel76208352016-08-25 14:25:12 +02002495 /* DMA alias already has a domain, use it */
Alex Williamson579305f2014-07-03 09:51:43 -06002496 if (info)
Joerg Roedel76208352016-08-25 14:25:12 +02002497 goto out;
Alex Williamson579305f2014-07-03 09:51:43 -06002498 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002499
David Woodhouse146922e2014-03-09 15:44:17 -07002500 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002501 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002502 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002503 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002504 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002505 domain_exit(domain);
2506 return NULL;
2507 }
2508
Joerg Roedel76208352016-08-25 14:25:12 +02002509out:
Alex Williamson579305f2014-07-03 09:51:43 -06002510
Joerg Roedel76208352016-08-25 14:25:12 +02002511 return domain;
2512}
2513
2514static struct dmar_domain *set_domain_for_dev(struct device *dev,
2515 struct dmar_domain *domain)
2516{
2517 struct intel_iommu *iommu;
2518 struct dmar_domain *tmp;
2519 u16 req_id, dma_alias;
2520 u8 bus, devfn;
2521
2522 iommu = device_to_iommu(dev, &bus, &devfn);
2523 if (!iommu)
2524 return NULL;
2525
2526 req_id = ((u16)bus << 8) | devfn;
2527
2528 if (dev_is_pci(dev)) {
2529 struct pci_dev *pdev = to_pci_dev(dev);
2530
2531 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2532
2533 /* register PCI DMA alias device */
2534 if (req_id != dma_alias) {
2535 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2536 dma_alias & 0xff, NULL, domain);
2537
2538 if (!tmp || tmp != domain)
2539 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002540 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002541 }
2542
Joerg Roedel5db31562015-07-22 12:40:43 +02002543 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Joerg Roedel76208352016-08-25 14:25:12 +02002544 if (!tmp || tmp != domain)
2545 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002546
Joerg Roedel76208352016-08-25 14:25:12 +02002547 return domain;
2548}
2549
2550static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
2551{
2552 struct dmar_domain *domain, *tmp;
2553
2554 domain = find_domain(dev);
2555 if (domain)
2556 goto out;
2557
2558 domain = find_or_alloc_domain(dev, gaw);
2559 if (!domain)
2560 goto out;
2561
2562 tmp = set_domain_for_dev(dev, domain);
2563 if (!tmp || domain != tmp) {
Alex Williamson579305f2014-07-03 09:51:43 -06002564 domain_exit(domain);
2565 domain = tmp;
2566 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002567
Joerg Roedel76208352016-08-25 14:25:12 +02002568out:
2569
David Woodhouseb718cd32014-03-09 13:11:33 -07002570 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002571}
2572
David Woodhouseb2132032009-06-26 18:50:28 +01002573static int iommu_domain_identity_map(struct dmar_domain *domain,
2574 unsigned long long start,
2575 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002576{
David Woodhousec5395d52009-06-28 16:35:56 +01002577 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2578 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002579
David Woodhousec5395d52009-06-28 16:35:56 +01002580 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2581 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002582 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002583 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002584 }
2585
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002586 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002587 /*
2588 * RMRR range might have overlap with physical memory range,
2589 * clear it first
2590 */
David Woodhousec5395d52009-06-28 16:35:56 +01002591 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002592
David Woodhousec5395d52009-06-28 16:35:56 +01002593 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2594 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002595 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002596}
2597
Joerg Roedeld66ce542015-09-23 19:00:10 +02002598static int domain_prepare_identity_map(struct device *dev,
2599 struct dmar_domain *domain,
2600 unsigned long long start,
2601 unsigned long long end)
David Woodhouseb2132032009-06-26 18:50:28 +01002602{
David Woodhouse19943b02009-08-04 16:19:20 +01002603 /* For _hardware_ passthrough, don't bother. But for software
2604 passthrough, we do it anyway -- it may indicate a memory
2605 range which is reserved in E820, so which didn't get set
2606 up to start with in si_domain */
2607 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002608 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2609 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002610 return 0;
2611 }
2612
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002613 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2614 dev_name(dev), start, end);
2615
David Woodhouse5595b522009-12-02 09:21:55 +00002616 if (end < start) {
2617 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2618 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2619 dmi_get_system_info(DMI_BIOS_VENDOR),
2620 dmi_get_system_info(DMI_BIOS_VERSION),
2621 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002622 return -EIO;
David Woodhouse5595b522009-12-02 09:21:55 +00002623 }
2624
David Woodhouse2ff729f2009-08-26 14:25:41 +01002625 if (end >> agaw_to_width(domain->agaw)) {
2626 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2627 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2628 agaw_to_width(domain->agaw),
2629 dmi_get_system_info(DMI_BIOS_VENDOR),
2630 dmi_get_system_info(DMI_BIOS_VERSION),
2631 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002632 return -EIO;
David Woodhouse2ff729f2009-08-26 14:25:41 +01002633 }
David Woodhouse19943b02009-08-04 16:19:20 +01002634
Joerg Roedeld66ce542015-09-23 19:00:10 +02002635 return iommu_domain_identity_map(domain, start, end);
2636}
2637
2638static int iommu_prepare_identity_map(struct device *dev,
2639 unsigned long long start,
2640 unsigned long long end)
2641{
2642 struct dmar_domain *domain;
2643 int ret;
2644
2645 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
2646 if (!domain)
2647 return -ENOMEM;
2648
2649 ret = domain_prepare_identity_map(dev, domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002650 if (ret)
Joerg Roedeld66ce542015-09-23 19:00:10 +02002651 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002652
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002653 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002654}
2655
2656static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002657 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002658{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002659 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002660 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002661 return iommu_prepare_identity_map(dev, rmrr->base_address,
2662 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002663}
2664
Suresh Siddhad3f13812011-08-23 17:05:25 -07002665#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002666static inline void iommu_prepare_isa(void)
2667{
2668 struct pci_dev *pdev;
2669 int ret;
2670
2671 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2672 if (!pdev)
2673 return;
2674
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002675 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002676 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002677
2678 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002679 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002680
Yijing Wang9b27e822014-05-20 20:37:52 +08002681 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002682}
2683#else
2684static inline void iommu_prepare_isa(void)
2685{
2686 return;
2687}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002688#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002689
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002690static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002691
Matt Kraai071e1372009-08-23 22:30:22 -07002692static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002693{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002694 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002695
Jiang Liuab8dfe22014-07-11 14:19:27 +08002696 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002697 if (!si_domain)
2698 return -EFAULT;
2699
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002700 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2701 domain_exit(si_domain);
2702 return -EFAULT;
2703 }
2704
Joerg Roedel0dc79712015-07-21 15:40:06 +02002705 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002706
David Woodhouse19943b02009-08-04 16:19:20 +01002707 if (hw)
2708 return 0;
2709
David Woodhousec7ab48d2009-06-26 19:10:36 +01002710 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002711 unsigned long start_pfn, end_pfn;
2712 int i;
2713
2714 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2715 ret = iommu_domain_identity_map(si_domain,
2716 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2717 if (ret)
2718 return ret;
2719 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002720 }
2721
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002722 return 0;
2723}
2724
David Woodhouse9b226622014-03-09 14:03:28 -07002725static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002726{
2727 struct device_domain_info *info;
2728
2729 if (likely(!iommu_identity_mapping))
2730 return 0;
2731
David Woodhouse9b226622014-03-09 14:03:28 -07002732 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002733 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2734 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002735
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002736 return 0;
2737}
2738
Joerg Roedel28ccce02015-07-21 14:45:31 +02002739static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002740{
David Woodhouse0ac72662014-03-09 13:19:22 -07002741 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002742 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002743 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002744
David Woodhouse5913c9b2014-03-09 16:27:31 -07002745 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002746 if (!iommu)
2747 return -ENODEV;
2748
Joerg Roedel5db31562015-07-22 12:40:43 +02002749 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002750 if (ndomain != domain)
2751 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002752
2753 return 0;
2754}
2755
David Woodhouse0b9d9752014-03-09 15:48:15 -07002756static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002757{
2758 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002759 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002760 int i;
2761
Jiang Liu0e242612014-02-19 14:07:34 +08002762 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002763 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002764 /*
2765 * Return TRUE if this RMRR contains the device that
2766 * is passed in.
2767 */
2768 for_each_active_dev_scope(rmrr->devices,
2769 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002770 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002771 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002772 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002773 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002774 }
Jiang Liu0e242612014-02-19 14:07:34 +08002775 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002776 return false;
2777}
2778
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002779/*
2780 * There are a couple cases where we need to restrict the functionality of
2781 * devices associated with RMRRs. The first is when evaluating a device for
2782 * identity mapping because problems exist when devices are moved in and out
2783 * of domains and their respective RMRR information is lost. This means that
2784 * a device with associated RMRRs will never be in a "passthrough" domain.
2785 * The second is use of the device through the IOMMU API. This interface
2786 * expects to have full control of the IOVA space for the device. We cannot
2787 * satisfy both the requirement that RMRR access is maintained and have an
2788 * unencumbered IOVA space. We also have no ability to quiesce the device's
2789 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2790 * We therefore prevent devices associated with an RMRR from participating in
2791 * the IOMMU API, which eliminates them from device assignment.
2792 *
2793 * In both cases we assume that PCI USB devices with RMRRs have them largely
2794 * for historical reasons and that the RMRR space is not actively used post
2795 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002796 *
2797 * The same exception is made for graphics devices, with the requirement that
2798 * any use of the RMRR regions will be torn down before assigning the device
2799 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002800 */
2801static bool device_is_rmrr_locked(struct device *dev)
2802{
2803 if (!device_has_rmrr(dev))
2804 return false;
2805
2806 if (dev_is_pci(dev)) {
2807 struct pci_dev *pdev = to_pci_dev(dev);
2808
David Woodhouse18436af2015-03-25 15:05:47 +00002809 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002810 return false;
2811 }
2812
2813 return true;
2814}
2815
David Woodhouse3bdb2592014-03-09 16:03:08 -07002816static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002817{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002818
David Woodhouse3bdb2592014-03-09 16:03:08 -07002819 if (dev_is_pci(dev)) {
2820 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002821
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002822 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002823 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002824
David Woodhouse3bdb2592014-03-09 16:03:08 -07002825 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2826 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002827
David Woodhouse3bdb2592014-03-09 16:03:08 -07002828 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2829 return 1;
2830
2831 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2832 return 0;
2833
2834 /*
2835 * We want to start off with all devices in the 1:1 domain, and
2836 * take them out later if we find they can't access all of memory.
2837 *
2838 * However, we can't do this for PCI devices behind bridges,
2839 * because all PCI devices behind the same bridge will end up
2840 * with the same source-id on their transactions.
2841 *
2842 * Practically speaking, we can't change things around for these
2843 * devices at run-time, because we can't be sure there'll be no
2844 * DMA transactions in flight for any of their siblings.
2845 *
2846 * So PCI devices (unless they're on the root bus) as well as
2847 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2848 * the 1:1 domain, just in _case_ one of their siblings turns out
2849 * not to be able to map all of memory.
2850 */
2851 if (!pci_is_pcie(pdev)) {
2852 if (!pci_is_root_bus(pdev->bus))
2853 return 0;
2854 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2855 return 0;
2856 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2857 return 0;
2858 } else {
2859 if (device_has_rmrr(dev))
2860 return 0;
2861 }
David Woodhouse6941af22009-07-04 18:24:27 +01002862
David Woodhouse3dfc8132009-07-04 19:11:08 +01002863 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002864 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002865 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002866 * take them out of the 1:1 domain later.
2867 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002868 if (!startup) {
2869 /*
2870 * If the device's dma_mask is less than the system's memory
2871 * size then this is not a candidate for identity mapping.
2872 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002873 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002874
David Woodhouse3bdb2592014-03-09 16:03:08 -07002875 if (dev->coherent_dma_mask &&
2876 dev->coherent_dma_mask < dma_mask)
2877 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002878
David Woodhouse3bdb2592014-03-09 16:03:08 -07002879 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002880 }
David Woodhouse6941af22009-07-04 18:24:27 +01002881
2882 return 1;
2883}
2884
David Woodhousecf04eee2014-03-21 16:49:04 +00002885static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2886{
2887 int ret;
2888
2889 if (!iommu_should_identity_map(dev, 1))
2890 return 0;
2891
Joerg Roedel28ccce02015-07-21 14:45:31 +02002892 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002893 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002894 pr_info("%s identity mapping for device %s\n",
2895 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002896 else if (ret == -ENODEV)
2897 /* device not associated with an iommu */
2898 ret = 0;
2899
2900 return ret;
2901}
2902
2903
Matt Kraai071e1372009-08-23 22:30:22 -07002904static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002905{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002906 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002907 struct dmar_drhd_unit *drhd;
2908 struct intel_iommu *iommu;
2909 struct device *dev;
2910 int i;
2911 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002912
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002913 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002914 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2915 if (ret)
2916 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002917 }
2918
David Woodhousecf04eee2014-03-21 16:49:04 +00002919 for_each_active_iommu(iommu, drhd)
2920 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2921 struct acpi_device_physical_node *pn;
2922 struct acpi_device *adev;
2923
2924 if (dev->bus != &acpi_bus_type)
2925 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002926
David Woodhousecf04eee2014-03-21 16:49:04 +00002927 adev= to_acpi_device(dev);
2928 mutex_lock(&adev->physical_node_lock);
2929 list_for_each_entry(pn, &adev->physical_node_list, node) {
2930 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2931 if (ret)
2932 break;
2933 }
2934 mutex_unlock(&adev->physical_node_lock);
2935 if (ret)
2936 return ret;
2937 }
2938
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002939 return 0;
2940}
2941
Jiang Liuffebeb42014-11-09 22:48:02 +08002942static void intel_iommu_init_qi(struct intel_iommu *iommu)
2943{
2944 /*
2945 * Start from the sane iommu hardware state.
2946 * If the queued invalidation is already initialized by us
2947 * (for example, while enabling interrupt-remapping) then
2948 * we got the things already rolling from a sane state.
2949 */
2950 if (!iommu->qi) {
2951 /*
2952 * Clear any previous faults.
2953 */
2954 dmar_fault(-1, iommu);
2955 /*
2956 * Disable queued invalidation if supported and already enabled
2957 * before OS handover.
2958 */
2959 dmar_disable_qi(iommu);
2960 }
2961
2962 if (dmar_enable_qi(iommu)) {
2963 /*
2964 * Queued Invalidate not enabled, use Register Based Invalidate
2965 */
2966 iommu->flush.flush_context = __iommu_flush_context;
2967 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002968 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002969 iommu->name);
2970 } else {
2971 iommu->flush.flush_context = qi_flush_context;
2972 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002973 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002974 }
2975}
2976
Joerg Roedel091d42e2015-06-12 11:56:10 +02002977static int copy_context_table(struct intel_iommu *iommu,
Dan Williamsdfddb962015-10-09 18:16:46 -04002978 struct root_entry *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02002979 struct context_entry **tbl,
2980 int bus, bool ext)
2981{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002982 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002983 struct context_entry *new_ce = NULL, ce;
Dan Williamsdfddb962015-10-09 18:16:46 -04002984 struct context_entry *old_ce = NULL;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002985 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002986 phys_addr_t old_ce_phys;
2987
2988 tbl_idx = ext ? bus * 2 : bus;
Dan Williamsdfddb962015-10-09 18:16:46 -04002989 memcpy(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002990
2991 for (devfn = 0; devfn < 256; devfn++) {
2992 /* First calculate the correct index */
2993 idx = (ext ? devfn * 2 : devfn) % 256;
2994
2995 if (idx == 0) {
2996 /* First save what we may have and clean up */
2997 if (new_ce) {
2998 tbl[tbl_idx] = new_ce;
2999 __iommu_flush_cache(iommu, new_ce,
3000 VTD_PAGE_SIZE);
3001 pos = 1;
3002 }
3003
3004 if (old_ce)
3005 iounmap(old_ce);
3006
3007 ret = 0;
3008 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003009 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003010 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003011 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003012
3013 if (!old_ce_phys) {
3014 if (ext && devfn == 0) {
3015 /* No LCTP, try UCTP */
3016 devfn = 0x7f;
3017 continue;
3018 } else {
3019 goto out;
3020 }
3021 }
3022
3023 ret = -ENOMEM;
Dan Williamsdfddb962015-10-09 18:16:46 -04003024 old_ce = memremap(old_ce_phys, PAGE_SIZE,
3025 MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003026 if (!old_ce)
3027 goto out;
3028
3029 new_ce = alloc_pgtable_page(iommu->node);
3030 if (!new_ce)
3031 goto out_unmap;
3032
3033 ret = 0;
3034 }
3035
3036 /* Now copy the context entry */
Dan Williamsdfddb962015-10-09 18:16:46 -04003037 memcpy(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003038
Joerg Roedelcf484d02015-06-12 12:21:46 +02003039 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02003040 continue;
3041
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003042 did = context_domain_id(&ce);
3043 if (did >= 0 && did < cap_ndoms(iommu->cap))
3044 set_bit(did, iommu->domain_ids);
3045
Joerg Roedelcf484d02015-06-12 12:21:46 +02003046 /*
3047 * We need a marker for copied context entries. This
3048 * marker needs to work for the old format as well as
3049 * for extended context entries.
3050 *
3051 * Bit 67 of the context entry is used. In the old
3052 * format this bit is available to software, in the
3053 * extended format it is the PGE bit, but PGE is ignored
3054 * by HW if PASIDs are disabled (and thus still
3055 * available).
3056 *
3057 * So disable PASIDs first and then mark the entry
3058 * copied. This means that we don't copy PASID
3059 * translations from the old kernel, but this is fine as
3060 * faults there are not fatal.
3061 */
3062 context_clear_pasid_enable(&ce);
3063 context_set_copied(&ce);
3064
Joerg Roedel091d42e2015-06-12 11:56:10 +02003065 new_ce[idx] = ce;
3066 }
3067
3068 tbl[tbl_idx + pos] = new_ce;
3069
3070 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
3071
3072out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003073 memunmap(old_ce);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003074
3075out:
3076 return ret;
3077}
3078
3079static int copy_translation_tables(struct intel_iommu *iommu)
3080{
3081 struct context_entry **ctxt_tbls;
Dan Williamsdfddb962015-10-09 18:16:46 -04003082 struct root_entry *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003083 phys_addr_t old_rt_phys;
3084 int ctxt_table_entries;
3085 unsigned long flags;
3086 u64 rtaddr_reg;
3087 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02003088 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003089
3090 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
3091 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02003092 new_ext = !!ecap_ecs(iommu->ecap);
3093
3094 /*
3095 * The RTT bit can only be changed when translation is disabled,
3096 * but disabling translation means to open a window for data
3097 * corruption. So bail out and don't copy anything if we would
3098 * have to change the bit.
3099 */
3100 if (new_ext != ext)
3101 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003102
3103 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
3104 if (!old_rt_phys)
3105 return -EINVAL;
3106
Dan Williamsdfddb962015-10-09 18:16:46 -04003107 old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003108 if (!old_rt)
3109 return -ENOMEM;
3110
3111 /* This is too big for the stack - allocate it from slab */
3112 ctxt_table_entries = ext ? 512 : 256;
3113 ret = -ENOMEM;
3114 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
3115 if (!ctxt_tbls)
3116 goto out_unmap;
3117
3118 for (bus = 0; bus < 256; bus++) {
3119 ret = copy_context_table(iommu, &old_rt[bus],
3120 ctxt_tbls, bus, ext);
3121 if (ret) {
3122 pr_err("%s: Failed to copy context table for bus %d\n",
3123 iommu->name, bus);
3124 continue;
3125 }
3126 }
3127
3128 spin_lock_irqsave(&iommu->lock, flags);
3129
3130 /* Context tables are copied, now write them to the root_entry table */
3131 for (bus = 0; bus < 256; bus++) {
3132 int idx = ext ? bus * 2 : bus;
3133 u64 val;
3134
3135 if (ctxt_tbls[idx]) {
3136 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3137 iommu->root_entry[bus].lo = val;
3138 }
3139
3140 if (!ext || !ctxt_tbls[idx + 1])
3141 continue;
3142
3143 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3144 iommu->root_entry[bus].hi = val;
3145 }
3146
3147 spin_unlock_irqrestore(&iommu->lock, flags);
3148
3149 kfree(ctxt_tbls);
3150
3151 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3152
3153 ret = 0;
3154
3155out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003156 memunmap(old_rt);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003157
3158 return ret;
3159}
3160
Joseph Cihulab7792602011-05-03 00:08:37 -07003161static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003162{
3163 struct dmar_drhd_unit *drhd;
3164 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003165 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003166 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003167 struct intel_iommu *iommu;
Omer Pelegaa473242016-04-20 11:33:02 +03003168 int i, ret, cpu;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003169
3170 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003171 * for each drhd
3172 * allocate root
3173 * initialize and program root entry to not present
3174 * endfor
3175 */
3176 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003177 /*
3178 * lock not needed as this is only incremented in the single
3179 * threaded kernel __init code path all other access are read
3180 * only
3181 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003182 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003183 g_num_of_iommus++;
3184 continue;
3185 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003186 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003187 }
3188
Jiang Liuffebeb42014-11-09 22:48:02 +08003189 /* Preallocate enough resources for IOMMU hot-addition */
3190 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3191 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3192
Weidong Hand9630fe2008-12-08 11:06:32 +08003193 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3194 GFP_KERNEL);
3195 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003196 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003197 ret = -ENOMEM;
3198 goto error;
3199 }
3200
Omer Pelegaa473242016-04-20 11:33:02 +03003201 for_each_possible_cpu(cpu) {
3202 struct deferred_flush_data *dfd = per_cpu_ptr(&deferred_flush,
3203 cpu);
3204
3205 dfd->tables = kzalloc(g_num_of_iommus *
3206 sizeof(struct deferred_flush_table),
3207 GFP_KERNEL);
3208 if (!dfd->tables) {
3209 ret = -ENOMEM;
3210 goto free_g_iommus;
3211 }
3212
3213 spin_lock_init(&dfd->lock);
3214 setup_timer(&dfd->timer, flush_unmaps_timeout, cpu);
mark gross5e0d2a62008-03-04 15:22:08 -08003215 }
3216
Jiang Liu7c919772014-01-06 14:18:18 +08003217 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003218 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003219
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003220 intel_iommu_init_qi(iommu);
3221
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003222 ret = iommu_init_domains(iommu);
3223 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003224 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003225
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003226 init_translation_status(iommu);
3227
Joerg Roedel091d42e2015-06-12 11:56:10 +02003228 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3229 iommu_disable_translation(iommu);
3230 clear_translation_pre_enabled(iommu);
3231 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3232 iommu->name);
3233 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003234
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003235 /*
3236 * TBD:
3237 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003238 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003239 */
3240 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003241 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003242 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003243
Joerg Roedel091d42e2015-06-12 11:56:10 +02003244 if (translation_pre_enabled(iommu)) {
3245 pr_info("Translation already enabled - trying to copy translation structures\n");
3246
3247 ret = copy_translation_tables(iommu);
3248 if (ret) {
3249 /*
3250 * We found the IOMMU with translation
3251 * enabled - but failed to copy over the
3252 * old root-entry table. Try to proceed
3253 * by disabling translation now and
3254 * allocating a clean root-entry table.
3255 * This might cause DMAR faults, but
3256 * probably the dump will still succeed.
3257 */
3258 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3259 iommu->name);
3260 iommu_disable_translation(iommu);
3261 clear_translation_pre_enabled(iommu);
3262 } else {
3263 pr_info("Copied translation tables from previous kernel for %s\n",
3264 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003265 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003266 }
3267 }
3268
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003269 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003270 hw_pass_through = 0;
David Woodhouse8a94ade2015-03-24 14:54:56 +00003271#ifdef CONFIG_INTEL_IOMMU_SVM
3272 if (pasid_enabled(iommu))
3273 intel_svm_alloc_pasid_tables(iommu);
3274#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003275 }
3276
Joerg Roedela4c34ff2016-06-17 11:29:48 +02003277 /*
3278 * Now that qi is enabled on all iommus, set the root entry and flush
3279 * caches. This is required on some Intel X58 chipsets, otherwise the
3280 * flush_context function will loop forever and the boot hangs.
3281 */
3282 for_each_active_iommu(iommu, drhd) {
3283 iommu_flush_write_buffer(iommu);
3284 iommu_set_root_entry(iommu);
3285 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3286 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3287 }
3288
David Woodhouse19943b02009-08-04 16:19:20 +01003289 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003290 iommu_identity_mapping |= IDENTMAP_ALL;
3291
Suresh Siddhad3f13812011-08-23 17:05:25 -07003292#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003293 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003294#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003295
Joerg Roedel86080cc2015-06-12 12:27:16 +02003296 if (iommu_identity_mapping) {
3297 ret = si_domain_init(hw_pass_through);
3298 if (ret)
3299 goto free_iommu;
3300 }
3301
David Woodhousee0fc7e02009-09-30 09:12:17 -07003302 check_tylersburg_isoch();
3303
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003304 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003305 * If we copied translations from a previous kernel in the kdump
3306 * case, we can not assign the devices to domains now, as that
3307 * would eliminate the old mappings. So skip this part and defer
3308 * the assignment to device driver initialization time.
3309 */
3310 if (copied_tables)
3311 goto domains_done;
3312
3313 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003314 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003315 * identity mappings for rmrr, gfx, and isa and may fall back to static
3316 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003317 */
David Woodhouse19943b02009-08-04 16:19:20 +01003318 if (iommu_identity_mapping) {
3319 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3320 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003321 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003322 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003323 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003324 }
David Woodhouse19943b02009-08-04 16:19:20 +01003325 /*
3326 * For each rmrr
3327 * for each dev attached to rmrr
3328 * do
3329 * locate drhd for dev, alloc domain for dev
3330 * allocate free domain
3331 * allocate page table entries for rmrr
3332 * if context not allocated for bus
3333 * allocate and init context
3334 * set present in root table for this bus
3335 * init context with domain, translation etc
3336 * endfor
3337 * endfor
3338 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003339 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003340 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003341 /* some BIOS lists non-exist devices in DMAR table. */
3342 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003343 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003344 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003345 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003346 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003347 }
3348 }
3349
3350 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003351
Joerg Roedela87f4912015-06-12 12:32:54 +02003352domains_done:
3353
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003354 /*
3355 * for each drhd
3356 * enable fault log
3357 * global invalidate context cache
3358 * global invalidate iotlb
3359 * enable translation
3360 */
Jiang Liu7c919772014-01-06 14:18:18 +08003361 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003362 if (drhd->ignored) {
3363 /*
3364 * we always have to disable PMRs or DMA may fail on
3365 * this device
3366 */
3367 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003368 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003369 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003370 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003371
3372 iommu_flush_write_buffer(iommu);
3373
David Woodhousea222a7f2015-10-07 23:35:18 +01003374#ifdef CONFIG_INTEL_IOMMU_SVM
3375 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
3376 ret = intel_svm_enable_prq(iommu);
3377 if (ret)
3378 goto free_iommu;
3379 }
3380#endif
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003381 ret = dmar_set_interrupt(iommu);
3382 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003383 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003384
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003385 if (!translation_pre_enabled(iommu))
3386 iommu_enable_translation(iommu);
3387
David Woodhouseb94996c2009-09-19 15:28:12 -07003388 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003389 }
3390
3391 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003392
3393free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003394 for_each_active_iommu(iommu, drhd) {
3395 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003396 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003397 }
Jiang Liu989d51f2014-02-19 14:07:21 +08003398free_g_iommus:
Omer Pelegaa473242016-04-20 11:33:02 +03003399 for_each_possible_cpu(cpu)
3400 kfree(per_cpu_ptr(&deferred_flush, cpu)->tables);
Weidong Hand9630fe2008-12-08 11:06:32 +08003401 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003402error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003403 return ret;
3404}
3405
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003406/* This takes a number of _MM_ pages, not VTD pages */
Omer Peleg2aac6302016-04-20 11:33:57 +03003407static unsigned long intel_alloc_iova(struct device *dev,
David Woodhouse875764d2009-06-28 21:20:51 +01003408 struct dmar_domain *domain,
3409 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003410{
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003411 unsigned long iova_pfn = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003412
David Woodhouse875764d2009-06-28 21:20:51 +01003413 /* Restrict dma_mask to the width that the iommu can handle */
3414 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003415 /* Ensure we reserve the whole size-aligned region */
3416 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003417
3418 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003419 /*
3420 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003421 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003422 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003423 */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003424 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
3425 IOVA_PFN(DMA_BIT_MASK(32)));
3426 if (iova_pfn)
3427 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003428 }
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003429 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, IOVA_PFN(dma_mask));
3430 if (unlikely(!iova_pfn)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003431 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003432 nrpages, dev_name(dev));
Omer Peleg2aac6302016-04-20 11:33:57 +03003433 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003434 }
3435
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003436 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003437}
3438
David Woodhoused4b709f2014-03-09 16:07:40 -07003439static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003440{
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003441 struct dmar_domain *domain, *tmp;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003442 struct dmar_rmrr_unit *rmrr;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003443 struct device *i_dev;
3444 int i, ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003445
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003446 domain = find_domain(dev);
3447 if (domain)
3448 goto out;
3449
3450 domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
3451 if (!domain)
3452 goto out;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003453
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003454 /* We have a new domain - setup possible RMRRs for the device */
3455 rcu_read_lock();
3456 for_each_rmrr_units(rmrr) {
3457 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
3458 i, i_dev) {
3459 if (i_dev != dev)
3460 continue;
3461
3462 ret = domain_prepare_identity_map(dev, domain,
3463 rmrr->base_address,
3464 rmrr->end_address);
3465 if (ret)
3466 dev_err(dev, "Mapping reserved region failed\n");
3467 }
3468 }
3469 rcu_read_unlock();
3470
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003471 tmp = set_domain_for_dev(dev, domain);
3472 if (!tmp || domain != tmp) {
3473 domain_exit(domain);
3474 domain = tmp;
3475 }
3476
3477out:
3478
3479 if (!domain)
3480 pr_err("Allocating domain for %s failed\n", dev_name(dev));
3481
3482
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003483 return domain;
3484}
3485
David Woodhoused4b709f2014-03-09 16:07:40 -07003486static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003487{
3488 struct device_domain_info *info;
3489
3490 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003491 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003492 if (likely(info))
3493 return info->domain;
3494
3495 return __get_valid_domain_for_dev(dev);
3496}
3497
David Woodhouseecb509e2014-03-09 16:29:55 -07003498/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003499static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003500{
3501 int found;
3502
David Woodhouse3d891942014-03-06 15:59:26 +00003503 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003504 return 1;
3505
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003506 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003507 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003508
David Woodhouse9b226622014-03-09 14:03:28 -07003509 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003510 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003511 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003512 return 1;
3513 else {
3514 /*
3515 * 32 bit DMA is removed from si_domain and fall back
3516 * to non-identity mapping.
3517 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003518 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003519 pr_info("32bit %s uses non-identity mapping\n",
3520 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003521 return 0;
3522 }
3523 } else {
3524 /*
3525 * In case of a detached 64 bit DMA device from vm, the device
3526 * is put into si_domain for identity mapping.
3527 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003528 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003529 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003530 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003531 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003532 pr_info("64bit %s uses identity mapping\n",
3533 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003534 return 1;
3535 }
3536 }
3537 }
3538
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003539 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003540}
3541
David Woodhouse5040a912014-03-09 16:14:00 -07003542static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003543 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003544{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003545 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003546 phys_addr_t start_paddr;
Omer Peleg2aac6302016-04-20 11:33:57 +03003547 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003548 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003549 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003550 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003551 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003552
3553 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003554
David Woodhouse5040a912014-03-09 16:14:00 -07003555 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003556 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003557
David Woodhouse5040a912014-03-09 16:14:00 -07003558 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003559 if (!domain)
3560 return 0;
3561
Weidong Han8c11e792008-12-08 15:29:22 +08003562 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003563 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003564
Omer Peleg2aac6302016-04-20 11:33:57 +03003565 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
3566 if (!iova_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003567 goto error;
3568
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003569 /*
3570 * Check if DMAR supports zero-length reads on write only
3571 * mappings..
3572 */
3573 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003574 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003575 prot |= DMA_PTE_READ;
3576 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3577 prot |= DMA_PTE_WRITE;
3578 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003579 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003580 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003581 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003582 * is not a big problem
3583 */
Omer Peleg2aac6302016-04-20 11:33:57 +03003584 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003585 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003586 if (ret)
3587 goto error;
3588
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003589 /* it's a non-present to present mapping. Only flush if caching mode */
3590 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003591 iommu_flush_iotlb_psi(iommu, domain,
Omer Peleg2aac6302016-04-20 11:33:57 +03003592 mm_to_dma_pfn(iova_pfn),
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003593 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003594 else
Weidong Han8c11e792008-12-08 15:29:22 +08003595 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003596
Omer Peleg2aac6302016-04-20 11:33:57 +03003597 start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
David Woodhouse03d6a242009-06-28 15:33:46 +01003598 start_paddr += paddr & ~PAGE_MASK;
3599 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003600
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003601error:
Omer Peleg2aac6302016-04-20 11:33:57 +03003602 if (iova_pfn)
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003603 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003604 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003605 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003606 return 0;
3607}
3608
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003609static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3610 unsigned long offset, size_t size,
3611 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003612 unsigned long attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003613{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003614 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003615 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003616}
3617
Omer Pelegaa473242016-04-20 11:33:02 +03003618static void flush_unmaps(struct deferred_flush_data *flush_data)
mark gross5e0d2a62008-03-04 15:22:08 -08003619{
mark gross80b20dd2008-04-18 13:53:58 -07003620 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003621
Omer Pelegaa473242016-04-20 11:33:02 +03003622 flush_data->timer_on = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003623
3624 /* just flush them all */
3625 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003626 struct intel_iommu *iommu = g_iommus[i];
Omer Pelegaa473242016-04-20 11:33:02 +03003627 struct deferred_flush_table *flush_table =
3628 &flush_data->tables[i];
Weidong Hana2bb8452008-12-08 11:24:12 +08003629 if (!iommu)
3630 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003631
Omer Pelegaa473242016-04-20 11:33:02 +03003632 if (!flush_table->next)
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003633 continue;
3634
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003635 /* In caching mode, global flushes turn emulation expensive */
3636 if (!cap_caching_mode(iommu->cap))
3637 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003638 DMA_TLB_GLOBAL_FLUSH);
Omer Pelegaa473242016-04-20 11:33:02 +03003639 for (j = 0; j < flush_table->next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003640 unsigned long mask;
Omer Peleg314f1dc2016-04-20 11:32:45 +03003641 struct deferred_flush_entry *entry =
Omer Pelegaa473242016-04-20 11:33:02 +03003642 &flush_table->entries[j];
Omer Peleg2aac6302016-04-20 11:33:57 +03003643 unsigned long iova_pfn = entry->iova_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +03003644 unsigned long nrpages = entry->nrpages;
Omer Peleg314f1dc2016-04-20 11:32:45 +03003645 struct dmar_domain *domain = entry->domain;
3646 struct page *freelist = entry->freelist;
Yu Zhao93a23a72009-05-18 13:51:37 +08003647
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003648 /* On real hardware multiple invalidations are expensive */
3649 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003650 iommu_flush_iotlb_psi(iommu, domain,
Omer Peleg2aac6302016-04-20 11:33:57 +03003651 mm_to_dma_pfn(iova_pfn),
Omer Peleg769530e2016-04-20 11:33:25 +03003652 nrpages, !freelist, 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003653 else {
Omer Peleg769530e2016-04-20 11:33:25 +03003654 mask = ilog2(nrpages);
Omer Peleg314f1dc2016-04-20 11:32:45 +03003655 iommu_flush_dev_iotlb(domain,
Omer Peleg2aac6302016-04-20 11:33:57 +03003656 (uint64_t)iova_pfn << PAGE_SHIFT, mask);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003657 }
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003658 free_iova_fast(&domain->iovad, iova_pfn, nrpages);
Omer Peleg314f1dc2016-04-20 11:32:45 +03003659 if (freelist)
3660 dma_free_pagelist(freelist);
mark gross80b20dd2008-04-18 13:53:58 -07003661 }
Omer Pelegaa473242016-04-20 11:33:02 +03003662 flush_table->next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003663 }
3664
Omer Pelegaa473242016-04-20 11:33:02 +03003665 flush_data->size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003666}
3667
Omer Pelegaa473242016-04-20 11:33:02 +03003668static void flush_unmaps_timeout(unsigned long cpuid)
mark gross5e0d2a62008-03-04 15:22:08 -08003669{
Omer Pelegaa473242016-04-20 11:33:02 +03003670 struct deferred_flush_data *flush_data = per_cpu_ptr(&deferred_flush, cpuid);
mark gross80b20dd2008-04-18 13:53:58 -07003671 unsigned long flags;
3672
Omer Pelegaa473242016-04-20 11:33:02 +03003673 spin_lock_irqsave(&flush_data->lock, flags);
3674 flush_unmaps(flush_data);
3675 spin_unlock_irqrestore(&flush_data->lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003676}
3677
Omer Peleg2aac6302016-04-20 11:33:57 +03003678static void add_unmap(struct dmar_domain *dom, unsigned long iova_pfn,
Omer Peleg769530e2016-04-20 11:33:25 +03003679 unsigned long nrpages, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003680{
3681 unsigned long flags;
Omer Peleg314f1dc2016-04-20 11:32:45 +03003682 int entry_id, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003683 struct intel_iommu *iommu;
Omer Peleg314f1dc2016-04-20 11:32:45 +03003684 struct deferred_flush_entry *entry;
Omer Pelegaa473242016-04-20 11:33:02 +03003685 struct deferred_flush_data *flush_data;
3686 unsigned int cpuid;
mark gross5e0d2a62008-03-04 15:22:08 -08003687
Omer Pelegaa473242016-04-20 11:33:02 +03003688 cpuid = get_cpu();
3689 flush_data = per_cpu_ptr(&deferred_flush, cpuid);
3690
3691 /* Flush all CPUs' entries to avoid deferring too much. If
3692 * this becomes a bottleneck, can just flush us, and rely on
3693 * flush timer for the rest.
3694 */
3695 if (flush_data->size == HIGH_WATER_MARK) {
3696 int cpu;
3697
3698 for_each_online_cpu(cpu)
3699 flush_unmaps_timeout(cpu);
3700 }
3701
3702 spin_lock_irqsave(&flush_data->lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003703
Weidong Han8c11e792008-12-08 15:29:22 +08003704 iommu = domain_get_iommu(dom);
3705 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003706
Omer Pelegaa473242016-04-20 11:33:02 +03003707 entry_id = flush_data->tables[iommu_id].next;
3708 ++(flush_data->tables[iommu_id].next);
mark gross5e0d2a62008-03-04 15:22:08 -08003709
Omer Pelegaa473242016-04-20 11:33:02 +03003710 entry = &flush_data->tables[iommu_id].entries[entry_id];
Omer Peleg314f1dc2016-04-20 11:32:45 +03003711 entry->domain = dom;
Omer Peleg2aac6302016-04-20 11:33:57 +03003712 entry->iova_pfn = iova_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +03003713 entry->nrpages = nrpages;
Omer Peleg314f1dc2016-04-20 11:32:45 +03003714 entry->freelist = freelist;
mark gross5e0d2a62008-03-04 15:22:08 -08003715
Omer Pelegaa473242016-04-20 11:33:02 +03003716 if (!flush_data->timer_on) {
3717 mod_timer(&flush_data->timer, jiffies + msecs_to_jiffies(10));
3718 flush_data->timer_on = 1;
mark gross5e0d2a62008-03-04 15:22:08 -08003719 }
Omer Pelegaa473242016-04-20 11:33:02 +03003720 flush_data->size++;
3721 spin_unlock_irqrestore(&flush_data->lock, flags);
3722
3723 put_cpu();
mark gross5e0d2a62008-03-04 15:22:08 -08003724}
3725
Omer Peleg769530e2016-04-20 11:33:25 +03003726static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003727{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003728 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003729 unsigned long start_pfn, last_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +03003730 unsigned long nrpages;
Omer Peleg2aac6302016-04-20 11:33:57 +03003731 unsigned long iova_pfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003732 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003733 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003734
David Woodhouse73676832009-07-04 14:08:36 +01003735 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003736 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003737
David Woodhouse1525a292014-03-06 16:19:30 +00003738 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003739 BUG_ON(!domain);
3740
Weidong Han8c11e792008-12-08 15:29:22 +08003741 iommu = domain_get_iommu(domain);
3742
Omer Peleg2aac6302016-04-20 11:33:57 +03003743 iova_pfn = IOVA_PFN(dev_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003744
Omer Peleg769530e2016-04-20 11:33:25 +03003745 nrpages = aligned_nrpages(dev_addr, size);
Omer Peleg2aac6302016-04-20 11:33:57 +03003746 start_pfn = mm_to_dma_pfn(iova_pfn);
Omer Peleg769530e2016-04-20 11:33:25 +03003747 last_pfn = start_pfn + nrpages - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003748
David Woodhoused794dc92009-06-28 00:27:49 +01003749 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003750 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003751
David Woodhouseea8ea462014-03-05 17:09:32 +00003752 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003753
mark gross5e0d2a62008-03-04 15:22:08 -08003754 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003755 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
Omer Peleg769530e2016-04-20 11:33:25 +03003756 nrpages, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003757 /* free iova */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003758 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
David Woodhouseea8ea462014-03-05 17:09:32 +00003759 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003760 } else {
Omer Peleg2aac6302016-04-20 11:33:57 +03003761 add_unmap(domain, iova_pfn, nrpages, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003762 /*
3763 * queue up the release of the unmap to save the 1/6th of the
3764 * cpu used up by the iotlb flush operation...
3765 */
mark gross5e0d2a62008-03-04 15:22:08 -08003766 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003767}
3768
Jiang Liud41a4ad2014-07-11 14:19:34 +08003769static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3770 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003771 unsigned long attrs)
Jiang Liud41a4ad2014-07-11 14:19:34 +08003772{
Omer Peleg769530e2016-04-20 11:33:25 +03003773 intel_unmap(dev, dev_addr, size);
Jiang Liud41a4ad2014-07-11 14:19:34 +08003774}
3775
David Woodhouse5040a912014-03-09 16:14:00 -07003776static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003777 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003778 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003779{
Akinobu Mita36746432014-06-04 16:06:51 -07003780 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003781 int order;
3782
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003783 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003784 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003785
David Woodhouse5040a912014-03-09 16:14:00 -07003786 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003787 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003788 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3789 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003790 flags |= GFP_DMA;
3791 else
3792 flags |= GFP_DMA32;
3793 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003794
Mel Gormand0164ad2015-11-06 16:28:21 -08003795 if (gfpflags_allow_blocking(flags)) {
Akinobu Mita36746432014-06-04 16:06:51 -07003796 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003797
Akinobu Mita36746432014-06-04 16:06:51 -07003798 page = dma_alloc_from_contiguous(dev, count, order);
3799 if (page && iommu_no_mapping(dev) &&
3800 page_to_phys(page) + size > dev->coherent_dma_mask) {
3801 dma_release_from_contiguous(dev, page, count);
3802 page = NULL;
3803 }
3804 }
3805
3806 if (!page)
3807 page = alloc_pages(flags, order);
3808 if (!page)
3809 return NULL;
3810 memset(page_address(page), 0, size);
3811
3812 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003813 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003814 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003815 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003816 return page_address(page);
3817 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3818 __free_pages(page, order);
3819
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003820 return NULL;
3821}
3822
David Woodhouse5040a912014-03-09 16:14:00 -07003823static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003824 dma_addr_t dma_handle, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003825{
3826 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003827 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003828
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003829 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003830 order = get_order(size);
3831
Omer Peleg769530e2016-04-20 11:33:25 +03003832 intel_unmap(dev, dma_handle, size);
Akinobu Mita36746432014-06-04 16:06:51 -07003833 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3834 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003835}
3836
David Woodhouse5040a912014-03-09 16:14:00 -07003837static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003838 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003839 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003840{
Omer Peleg769530e2016-04-20 11:33:25 +03003841 dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK;
3842 unsigned long nrpages = 0;
3843 struct scatterlist *sg;
3844 int i;
3845
3846 for_each_sg(sglist, sg, nelems, i) {
3847 nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg));
3848 }
3849
3850 intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003851}
3852
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003853static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003854 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003855{
3856 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003857 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003858
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003859 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003860 BUG_ON(!sg_page(sg));
Dan Williams3e6110f2015-12-15 12:54:06 -08003861 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003862 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003863 }
3864 return nelems;
3865}
3866
David Woodhouse5040a912014-03-09 16:14:00 -07003867static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003868 enum dma_data_direction dir, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003869{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003870 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003871 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003872 size_t size = 0;
3873 int prot = 0;
Omer Peleg2aac6302016-04-20 11:33:57 +03003874 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003875 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003876 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003877 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003878 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003879
3880 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003881 if (iommu_no_mapping(dev))
3882 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003883
David Woodhouse5040a912014-03-09 16:14:00 -07003884 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003885 if (!domain)
3886 return 0;
3887
Weidong Han8c11e792008-12-08 15:29:22 +08003888 iommu = domain_get_iommu(domain);
3889
David Woodhouseb536d242009-06-28 14:49:31 +01003890 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003891 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003892
Omer Peleg2aac6302016-04-20 11:33:57 +03003893 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
David Woodhouse5040a912014-03-09 16:14:00 -07003894 *dev->dma_mask);
Omer Peleg2aac6302016-04-20 11:33:57 +03003895 if (!iova_pfn) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003896 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003897 return 0;
3898 }
3899
3900 /*
3901 * Check if DMAR supports zero-length reads on write only
3902 * mappings..
3903 */
3904 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003905 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003906 prot |= DMA_PTE_READ;
3907 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3908 prot |= DMA_PTE_WRITE;
3909
Omer Peleg2aac6302016-04-20 11:33:57 +03003910 start_vpfn = mm_to_dma_pfn(iova_pfn);
David Woodhousee1605492009-06-29 11:17:38 +01003911
Fenghua Yuf5329592009-08-04 15:09:37 -07003912 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003913 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003914 dma_pte_free_pagetable(domain, start_vpfn,
3915 start_vpfn + size - 1);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003916 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
David Woodhousee1605492009-06-29 11:17:38 +01003917 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003918 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003919
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003920 /* it's a non-present to present mapping. Only flush if caching mode */
3921 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003922 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003923 else
Weidong Han8c11e792008-12-08 15:29:22 +08003924 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003925
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003926 return nelems;
3927}
3928
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003929static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3930{
3931 return !dma_addr;
3932}
3933
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003934struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003935 .alloc = intel_alloc_coherent,
3936 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003937 .map_sg = intel_map_sg,
3938 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003939 .map_page = intel_map_page,
3940 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003941 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003942};
3943
3944static inline int iommu_domain_cache_init(void)
3945{
3946 int ret = 0;
3947
3948 iommu_domain_cache = kmem_cache_create("iommu_domain",
3949 sizeof(struct dmar_domain),
3950 0,
3951 SLAB_HWCACHE_ALIGN,
3952
3953 NULL);
3954 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003955 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003956 ret = -ENOMEM;
3957 }
3958
3959 return ret;
3960}
3961
3962static inline int iommu_devinfo_cache_init(void)
3963{
3964 int ret = 0;
3965
3966 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3967 sizeof(struct device_domain_info),
3968 0,
3969 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003970 NULL);
3971 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003972 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003973 ret = -ENOMEM;
3974 }
3975
3976 return ret;
3977}
3978
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003979static int __init iommu_init_mempool(void)
3980{
3981 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003982 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003983 if (ret)
3984 return ret;
3985
3986 ret = iommu_domain_cache_init();
3987 if (ret)
3988 goto domain_error;
3989
3990 ret = iommu_devinfo_cache_init();
3991 if (!ret)
3992 return ret;
3993
3994 kmem_cache_destroy(iommu_domain_cache);
3995domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003996 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003997
3998 return -ENOMEM;
3999}
4000
4001static void __init iommu_exit_mempool(void)
4002{
4003 kmem_cache_destroy(iommu_devinfo_cache);
4004 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03004005 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004006}
4007
Dan Williams556ab452010-07-23 15:47:56 -07004008static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
4009{
4010 struct dmar_drhd_unit *drhd;
4011 u32 vtbar;
4012 int rc;
4013
4014 /* We know that this device on this chipset has its own IOMMU.
4015 * If we find it under a different IOMMU, then the BIOS is lying
4016 * to us. Hope that the IOMMU for this device is actually
4017 * disabled, and it needs no translation...
4018 */
4019 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
4020 if (rc) {
4021 /* "can't" happen */
4022 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
4023 return;
4024 }
4025 vtbar &= 0xffff0000;
4026
4027 /* we know that the this iommu should be at offset 0xa000 from vtbar */
4028 drhd = dmar_find_matched_drhd_unit(pdev);
4029 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
4030 TAINT_FIRMWARE_WORKAROUND,
4031 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
4032 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
4033}
4034DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
4035
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004036static void __init init_no_remapping_devices(void)
4037{
4038 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00004039 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08004040 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004041
4042 for_each_drhd_unit(drhd) {
4043 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08004044 for_each_active_dev_scope(drhd->devices,
4045 drhd->devices_cnt, i, dev)
4046 break;
David Woodhouse832bd852014-03-07 15:08:36 +00004047 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004048 if (i == drhd->devices_cnt)
4049 drhd->ignored = 1;
4050 }
4051 }
4052
Jiang Liu7c919772014-01-06 14:18:18 +08004053 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08004054 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004055 continue;
4056
Jiang Liub683b232014-02-19 14:07:32 +08004057 for_each_active_dev_scope(drhd->devices,
4058 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00004059 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004060 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004061 if (i < drhd->devices_cnt)
4062 continue;
4063
David Woodhousec0771df2011-10-14 20:59:46 +01004064 /* This IOMMU has *only* gfx devices. Either bypass it or
4065 set the gfx_mapped flag, as appropriate */
4066 if (dmar_map_gfx) {
4067 intel_iommu_gfx_mapped = 1;
4068 } else {
4069 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08004070 for_each_active_dev_scope(drhd->devices,
4071 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00004072 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004073 }
4074 }
4075}
4076
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004077#ifdef CONFIG_SUSPEND
4078static int init_iommu_hw(void)
4079{
4080 struct dmar_drhd_unit *drhd;
4081 struct intel_iommu *iommu = NULL;
4082
4083 for_each_active_iommu(iommu, drhd)
4084 if (iommu->qi)
4085 dmar_reenable_qi(iommu);
4086
Joseph Cihulab7792602011-05-03 00:08:37 -07004087 for_each_iommu(iommu, drhd) {
4088 if (drhd->ignored) {
4089 /*
4090 * we always have to disable PMRs or DMA may fail on
4091 * this device
4092 */
4093 if (force_on)
4094 iommu_disable_protect_mem_regions(iommu);
4095 continue;
4096 }
4097
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004098 iommu_flush_write_buffer(iommu);
4099
4100 iommu_set_root_entry(iommu);
4101
4102 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004103 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08004104 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4105 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07004106 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004107 }
4108
4109 return 0;
4110}
4111
4112static void iommu_flush_all(void)
4113{
4114 struct dmar_drhd_unit *drhd;
4115 struct intel_iommu *iommu;
4116
4117 for_each_active_iommu(iommu, drhd) {
4118 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004119 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004120 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004121 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004122 }
4123}
4124
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004125static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004126{
4127 struct dmar_drhd_unit *drhd;
4128 struct intel_iommu *iommu = NULL;
4129 unsigned long flag;
4130
4131 for_each_active_iommu(iommu, drhd) {
4132 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
4133 GFP_ATOMIC);
4134 if (!iommu->iommu_state)
4135 goto nomem;
4136 }
4137
4138 iommu_flush_all();
4139
4140 for_each_active_iommu(iommu, drhd) {
4141 iommu_disable_translation(iommu);
4142
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004143 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004144
4145 iommu->iommu_state[SR_DMAR_FECTL_REG] =
4146 readl(iommu->reg + DMAR_FECTL_REG);
4147 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
4148 readl(iommu->reg + DMAR_FEDATA_REG);
4149 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
4150 readl(iommu->reg + DMAR_FEADDR_REG);
4151 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
4152 readl(iommu->reg + DMAR_FEUADDR_REG);
4153
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004154 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004155 }
4156 return 0;
4157
4158nomem:
4159 for_each_active_iommu(iommu, drhd)
4160 kfree(iommu->iommu_state);
4161
4162 return -ENOMEM;
4163}
4164
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004165static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004166{
4167 struct dmar_drhd_unit *drhd;
4168 struct intel_iommu *iommu = NULL;
4169 unsigned long flag;
4170
4171 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07004172 if (force_on)
4173 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
4174 else
4175 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004176 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004177 }
4178
4179 for_each_active_iommu(iommu, drhd) {
4180
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004181 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004182
4183 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
4184 iommu->reg + DMAR_FECTL_REG);
4185 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
4186 iommu->reg + DMAR_FEDATA_REG);
4187 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
4188 iommu->reg + DMAR_FEADDR_REG);
4189 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
4190 iommu->reg + DMAR_FEUADDR_REG);
4191
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004192 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004193 }
4194
4195 for_each_active_iommu(iommu, drhd)
4196 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004197}
4198
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004199static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004200 .resume = iommu_resume,
4201 .suspend = iommu_suspend,
4202};
4203
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004204static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004205{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004206 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004207}
4208
4209#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004210static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004211#endif /* CONFIG_PM */
4212
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004213
Jiang Liuc2a0b532014-11-09 22:47:56 +08004214int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004215{
4216 struct acpi_dmar_reserved_memory *rmrr;
4217 struct dmar_rmrr_unit *rmrru;
4218
4219 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4220 if (!rmrru)
4221 return -ENOMEM;
4222
4223 rmrru->hdr = header;
4224 rmrr = (struct acpi_dmar_reserved_memory *)header;
4225 rmrru->base_address = rmrr->base_address;
4226 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08004227 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4228 ((void *)rmrr) + rmrr->header.length,
4229 &rmrru->devices_cnt);
4230 if (rmrru->devices_cnt && rmrru->devices == NULL) {
4231 kfree(rmrru);
4232 return -ENOMEM;
4233 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004234
Jiang Liu2e455282014-02-19 14:07:36 +08004235 list_add(&rmrru->list, &dmar_rmrr_units);
4236
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004237 return 0;
4238}
4239
Jiang Liu6b197242014-11-09 22:47:58 +08004240static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4241{
4242 struct dmar_atsr_unit *atsru;
4243 struct acpi_dmar_atsr *tmp;
4244
4245 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4246 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4247 if (atsr->segment != tmp->segment)
4248 continue;
4249 if (atsr->header.length != tmp->header.length)
4250 continue;
4251 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4252 return atsru;
4253 }
4254
4255 return NULL;
4256}
4257
4258int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004259{
4260 struct acpi_dmar_atsr *atsr;
4261 struct dmar_atsr_unit *atsru;
4262
Jiang Liu6b197242014-11-09 22:47:58 +08004263 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4264 return 0;
4265
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004266 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004267 atsru = dmar_find_atsr(atsr);
4268 if (atsru)
4269 return 0;
4270
4271 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004272 if (!atsru)
4273 return -ENOMEM;
4274
Jiang Liu6b197242014-11-09 22:47:58 +08004275 /*
4276 * If memory is allocated from slab by ACPI _DSM method, we need to
4277 * copy the memory content because the memory buffer will be freed
4278 * on return.
4279 */
4280 atsru->hdr = (void *)(atsru + 1);
4281 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004282 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004283 if (!atsru->include_all) {
4284 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4285 (void *)atsr + atsr->header.length,
4286 &atsru->devices_cnt);
4287 if (atsru->devices_cnt && atsru->devices == NULL) {
4288 kfree(atsru);
4289 return -ENOMEM;
4290 }
4291 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004292
Jiang Liu0e242612014-02-19 14:07:34 +08004293 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004294
4295 return 0;
4296}
4297
Jiang Liu9bdc5312014-01-06 14:18:27 +08004298static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4299{
4300 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4301 kfree(atsru);
4302}
4303
Jiang Liu6b197242014-11-09 22:47:58 +08004304int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4305{
4306 struct acpi_dmar_atsr *atsr;
4307 struct dmar_atsr_unit *atsru;
4308
4309 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4310 atsru = dmar_find_atsr(atsr);
4311 if (atsru) {
4312 list_del_rcu(&atsru->list);
4313 synchronize_rcu();
4314 intel_iommu_free_atsr(atsru);
4315 }
4316
4317 return 0;
4318}
4319
4320int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4321{
4322 int i;
4323 struct device *dev;
4324 struct acpi_dmar_atsr *atsr;
4325 struct dmar_atsr_unit *atsru;
4326
4327 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4328 atsru = dmar_find_atsr(atsr);
4329 if (!atsru)
4330 return 0;
4331
Linus Torvalds194dc872016-07-27 20:03:31 -07004332 if (!atsru->include_all && atsru->devices && atsru->devices_cnt) {
Jiang Liu6b197242014-11-09 22:47:58 +08004333 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4334 i, dev)
4335 return -EBUSY;
Linus Torvalds194dc872016-07-27 20:03:31 -07004336 }
Jiang Liu6b197242014-11-09 22:47:58 +08004337
4338 return 0;
4339}
4340
Jiang Liuffebeb42014-11-09 22:48:02 +08004341static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4342{
4343 int sp, ret = 0;
4344 struct intel_iommu *iommu = dmaru->iommu;
4345
4346 if (g_iommus[iommu->seq_id])
4347 return 0;
4348
4349 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004350 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004351 iommu->name);
4352 return -ENXIO;
4353 }
4354 if (!ecap_sc_support(iommu->ecap) &&
4355 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004356 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004357 iommu->name);
4358 return -ENXIO;
4359 }
4360 sp = domain_update_iommu_superpage(iommu) - 1;
4361 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004362 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004363 iommu->name);
4364 return -ENXIO;
4365 }
4366
4367 /*
4368 * Disable translation if already enabled prior to OS handover.
4369 */
4370 if (iommu->gcmd & DMA_GCMD_TE)
4371 iommu_disable_translation(iommu);
4372
4373 g_iommus[iommu->seq_id] = iommu;
4374 ret = iommu_init_domains(iommu);
4375 if (ret == 0)
4376 ret = iommu_alloc_root_entry(iommu);
4377 if (ret)
4378 goto out;
4379
David Woodhouse8a94ade2015-03-24 14:54:56 +00004380#ifdef CONFIG_INTEL_IOMMU_SVM
4381 if (pasid_enabled(iommu))
4382 intel_svm_alloc_pasid_tables(iommu);
4383#endif
4384
Jiang Liuffebeb42014-11-09 22:48:02 +08004385 if (dmaru->ignored) {
4386 /*
4387 * we always have to disable PMRs or DMA may fail on this device
4388 */
4389 if (force_on)
4390 iommu_disable_protect_mem_regions(iommu);
4391 return 0;
4392 }
4393
4394 intel_iommu_init_qi(iommu);
4395 iommu_flush_write_buffer(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01004396
4397#ifdef CONFIG_INTEL_IOMMU_SVM
4398 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
4399 ret = intel_svm_enable_prq(iommu);
4400 if (ret)
4401 goto disable_iommu;
4402 }
4403#endif
Jiang Liuffebeb42014-11-09 22:48:02 +08004404 ret = dmar_set_interrupt(iommu);
4405 if (ret)
4406 goto disable_iommu;
4407
4408 iommu_set_root_entry(iommu);
4409 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4410 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4411 iommu_enable_translation(iommu);
4412
Jiang Liuffebeb42014-11-09 22:48:02 +08004413 iommu_disable_protect_mem_regions(iommu);
4414 return 0;
4415
4416disable_iommu:
4417 disable_dmar_iommu(iommu);
4418out:
4419 free_dmar_iommu(iommu);
4420 return ret;
4421}
4422
Jiang Liu6b197242014-11-09 22:47:58 +08004423int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4424{
Jiang Liuffebeb42014-11-09 22:48:02 +08004425 int ret = 0;
4426 struct intel_iommu *iommu = dmaru->iommu;
4427
4428 if (!intel_iommu_enabled)
4429 return 0;
4430 if (iommu == NULL)
4431 return -EINVAL;
4432
4433 if (insert) {
4434 ret = intel_iommu_add(dmaru);
4435 } else {
4436 disable_dmar_iommu(iommu);
4437 free_dmar_iommu(iommu);
4438 }
4439
4440 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004441}
4442
Jiang Liu9bdc5312014-01-06 14:18:27 +08004443static void intel_iommu_free_dmars(void)
4444{
4445 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4446 struct dmar_atsr_unit *atsru, *atsr_n;
4447
4448 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4449 list_del(&rmrru->list);
4450 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4451 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004452 }
4453
Jiang Liu9bdc5312014-01-06 14:18:27 +08004454 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4455 list_del(&atsru->list);
4456 intel_iommu_free_atsr(atsru);
4457 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004458}
4459
4460int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4461{
Jiang Liub683b232014-02-19 14:07:32 +08004462 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004463 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004464 struct pci_dev *bridge = NULL;
4465 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004466 struct acpi_dmar_atsr *atsr;
4467 struct dmar_atsr_unit *atsru;
4468
4469 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004470 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004471 bridge = bus->self;
David Woodhoused14053b32015-10-15 09:28:06 +01004472 /* If it's an integrated device, allow ATS */
4473 if (!bridge)
4474 return 1;
4475 /* Connected via non-PCIe: no ATS */
4476 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004477 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004478 return 0;
David Woodhoused14053b32015-10-15 09:28:06 +01004479 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004480 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004481 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004482 }
4483
Jiang Liu0e242612014-02-19 14:07:34 +08004484 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004485 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4486 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4487 if (atsr->segment != pci_domain_nr(dev->bus))
4488 continue;
4489
Jiang Liub683b232014-02-19 14:07:32 +08004490 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004491 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004492 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004493
4494 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004495 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004496 }
Jiang Liub683b232014-02-19 14:07:32 +08004497 ret = 0;
4498out:
Jiang Liu0e242612014-02-19 14:07:34 +08004499 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004500
Jiang Liub683b232014-02-19 14:07:32 +08004501 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004502}
4503
Jiang Liu59ce0512014-02-19 14:07:35 +08004504int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4505{
4506 int ret = 0;
4507 struct dmar_rmrr_unit *rmrru;
4508 struct dmar_atsr_unit *atsru;
4509 struct acpi_dmar_atsr *atsr;
4510 struct acpi_dmar_reserved_memory *rmrr;
4511
4512 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4513 return 0;
4514
4515 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4516 rmrr = container_of(rmrru->hdr,
4517 struct acpi_dmar_reserved_memory, header);
4518 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4519 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4520 ((void *)rmrr) + rmrr->header.length,
4521 rmrr->segment, rmrru->devices,
4522 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004523 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004524 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004525 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004526 dmar_remove_dev_scope(info, rmrr->segment,
4527 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004528 }
4529 }
4530
4531 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4532 if (atsru->include_all)
4533 continue;
4534
4535 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4536 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4537 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4538 (void *)atsr + atsr->header.length,
4539 atsr->segment, atsru->devices,
4540 atsru->devices_cnt);
4541 if (ret > 0)
4542 break;
4543 else if(ret < 0)
4544 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004545 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu59ce0512014-02-19 14:07:35 +08004546 if (dmar_remove_dev_scope(info, atsr->segment,
4547 atsru->devices, atsru->devices_cnt))
4548 break;
4549 }
4550 }
4551
4552 return 0;
4553}
4554
Fenghua Yu99dcade2009-11-11 07:23:06 -08004555/*
4556 * Here we only respond to action of unbound device from driver.
4557 *
4558 * Added device is not attached to its DMAR domain here yet. That will happen
4559 * when mapping the device to iova.
4560 */
4561static int device_notifier(struct notifier_block *nb,
4562 unsigned long action, void *data)
4563{
4564 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004565 struct dmar_domain *domain;
4566
David Woodhouse3d891942014-03-06 15:59:26 +00004567 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004568 return 0;
4569
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004570 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004571 return 0;
4572
David Woodhouse1525a292014-03-06 16:19:30 +00004573 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004574 if (!domain)
4575 return 0;
4576
Joerg Roedele6de0f82015-07-22 16:30:36 +02004577 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004578 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004579 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004580
Fenghua Yu99dcade2009-11-11 07:23:06 -08004581 return 0;
4582}
4583
4584static struct notifier_block device_nb = {
4585 .notifier_call = device_notifier,
4586};
4587
Jiang Liu75f05562014-02-19 14:07:37 +08004588static int intel_iommu_memory_notifier(struct notifier_block *nb,
4589 unsigned long val, void *v)
4590{
4591 struct memory_notify *mhp = v;
4592 unsigned long long start, end;
4593 unsigned long start_vpfn, last_vpfn;
4594
4595 switch (val) {
4596 case MEM_GOING_ONLINE:
4597 start = mhp->start_pfn << PAGE_SHIFT;
4598 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4599 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004600 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004601 start, end);
4602 return NOTIFY_BAD;
4603 }
4604 break;
4605
4606 case MEM_OFFLINE:
4607 case MEM_CANCEL_ONLINE:
4608 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4609 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4610 while (start_vpfn <= last_vpfn) {
4611 struct iova *iova;
4612 struct dmar_drhd_unit *drhd;
4613 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004614 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004615
4616 iova = find_iova(&si_domain->iovad, start_vpfn);
4617 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004618 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004619 start_vpfn);
4620 break;
4621 }
4622
4623 iova = split_and_remove_iova(&si_domain->iovad, iova,
4624 start_vpfn, last_vpfn);
4625 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004626 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004627 start_vpfn, last_vpfn);
4628 return NOTIFY_BAD;
4629 }
4630
David Woodhouseea8ea462014-03-05 17:09:32 +00004631 freelist = domain_unmap(si_domain, iova->pfn_lo,
4632 iova->pfn_hi);
4633
Jiang Liu75f05562014-02-19 14:07:37 +08004634 rcu_read_lock();
4635 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004636 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004637 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004638 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004639 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004640 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004641
4642 start_vpfn = iova->pfn_hi + 1;
4643 free_iova_mem(iova);
4644 }
4645 break;
4646 }
4647
4648 return NOTIFY_OK;
4649}
4650
4651static struct notifier_block intel_iommu_memory_nb = {
4652 .notifier_call = intel_iommu_memory_notifier,
4653 .priority = 0
4654};
4655
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004656static void free_all_cpu_cached_iovas(unsigned int cpu)
4657{
4658 int i;
4659
4660 for (i = 0; i < g_num_of_iommus; i++) {
4661 struct intel_iommu *iommu = g_iommus[i];
4662 struct dmar_domain *domain;
Aaron Campbell0caa7612016-07-02 21:23:24 -03004663 int did;
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004664
4665 if (!iommu)
4666 continue;
4667
Jan Niehusmann3bd4f912016-06-06 14:20:11 +02004668 for (did = 0; did < cap_ndoms(iommu->cap); did++) {
Aaron Campbell0caa7612016-07-02 21:23:24 -03004669 domain = get_iommu_domain(iommu, (u16)did);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004670
4671 if (!domain)
4672 continue;
4673 free_cpu_cached_iovas(cpu, &domain->iovad);
4674 }
4675 }
4676}
4677
Omer Pelegaa473242016-04-20 11:33:02 +03004678static int intel_iommu_cpu_notifier(struct notifier_block *nfb,
4679 unsigned long action, void *v)
4680{
4681 unsigned int cpu = (unsigned long)v;
4682
4683 switch (action) {
4684 case CPU_DEAD:
4685 case CPU_DEAD_FROZEN:
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004686 free_all_cpu_cached_iovas(cpu);
Omer Pelegaa473242016-04-20 11:33:02 +03004687 flush_unmaps_timeout(cpu);
4688 break;
4689 }
4690 return NOTIFY_OK;
4691}
4692
4693static struct notifier_block intel_iommu_cpu_nb = {
4694 .notifier_call = intel_iommu_cpu_notifier,
4695};
Alex Williamsona5459cf2014-06-12 16:12:31 -06004696
4697static ssize_t intel_iommu_show_version(struct device *dev,
4698 struct device_attribute *attr,
4699 char *buf)
4700{
4701 struct intel_iommu *iommu = dev_get_drvdata(dev);
4702 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4703 return sprintf(buf, "%d:%d\n",
4704 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4705}
4706static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4707
4708static ssize_t intel_iommu_show_address(struct device *dev,
4709 struct device_attribute *attr,
4710 char *buf)
4711{
4712 struct intel_iommu *iommu = dev_get_drvdata(dev);
4713 return sprintf(buf, "%llx\n", iommu->reg_phys);
4714}
4715static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4716
4717static ssize_t intel_iommu_show_cap(struct device *dev,
4718 struct device_attribute *attr,
4719 char *buf)
4720{
4721 struct intel_iommu *iommu = dev_get_drvdata(dev);
4722 return sprintf(buf, "%llx\n", iommu->cap);
4723}
4724static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4725
4726static ssize_t intel_iommu_show_ecap(struct device *dev,
4727 struct device_attribute *attr,
4728 char *buf)
4729{
4730 struct intel_iommu *iommu = dev_get_drvdata(dev);
4731 return sprintf(buf, "%llx\n", iommu->ecap);
4732}
4733static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4734
Alex Williamson2238c082015-07-14 15:24:53 -06004735static ssize_t intel_iommu_show_ndoms(struct device *dev,
4736 struct device_attribute *attr,
4737 char *buf)
4738{
4739 struct intel_iommu *iommu = dev_get_drvdata(dev);
4740 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4741}
4742static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4743
4744static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4745 struct device_attribute *attr,
4746 char *buf)
4747{
4748 struct intel_iommu *iommu = dev_get_drvdata(dev);
4749 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4750 cap_ndoms(iommu->cap)));
4751}
4752static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4753
Alex Williamsona5459cf2014-06-12 16:12:31 -06004754static struct attribute *intel_iommu_attrs[] = {
4755 &dev_attr_version.attr,
4756 &dev_attr_address.attr,
4757 &dev_attr_cap.attr,
4758 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004759 &dev_attr_domains_supported.attr,
4760 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004761 NULL,
4762};
4763
4764static struct attribute_group intel_iommu_group = {
4765 .name = "intel-iommu",
4766 .attrs = intel_iommu_attrs,
4767};
4768
4769const struct attribute_group *intel_iommu_groups[] = {
4770 &intel_iommu_group,
4771 NULL,
4772};
4773
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004774int __init intel_iommu_init(void)
4775{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004776 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004777 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004778 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004779
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004780 /* VT-d is required for a TXT/tboot launch, so enforce that */
4781 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004782
Jiang Liu3a5670e2014-02-19 14:07:33 +08004783 if (iommu_init_mempool()) {
4784 if (force_on)
4785 panic("tboot: Failed to initialize iommu memory\n");
4786 return -ENOMEM;
4787 }
4788
4789 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004790 if (dmar_table_init()) {
4791 if (force_on)
4792 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004793 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004794 }
4795
Suresh Siddhac2c72862011-08-23 17:05:19 -07004796 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004797 if (force_on)
4798 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004799 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004800 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004801
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004802 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004803 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004804
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004805 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004806 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004807
4808 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004809 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004810
Joseph Cihula51a63e62011-03-21 11:04:24 -07004811 if (dmar_init_reserved_ranges()) {
4812 if (force_on)
4813 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004814 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004815 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004816
4817 init_no_remapping_devices();
4818
Joseph Cihulab7792602011-05-03 00:08:37 -07004819 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004820 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004821 if (force_on)
4822 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004823 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004824 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004825 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004826 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004827 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004828
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004829#ifdef CONFIG_SWIOTLB
4830 swiotlb = 0;
4831#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004832 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004833
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004834 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004835
Alex Williamsona5459cf2014-06-12 16:12:31 -06004836 for_each_active_iommu(iommu, drhd)
4837 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4838 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004839 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004840
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004841 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004842 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004843 if (si_domain && !hw_pass_through)
4844 register_memory_notifier(&intel_iommu_memory_nb);
Omer Pelegaa473242016-04-20 11:33:02 +03004845 register_hotcpu_notifier(&intel_iommu_cpu_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004846
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004847 intel_iommu_enabled = 1;
4848
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004849 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004850
4851out_free_reserved_range:
4852 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004853out_free_dmar:
4854 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004855 up_write(&dmar_global_lock);
4856 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004857 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004858}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004859
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004860static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004861{
4862 struct intel_iommu *iommu = opaque;
4863
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004864 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004865 return 0;
4866}
4867
4868/*
4869 * NB - intel-iommu lacks any sort of reference counting for the users of
4870 * dependent devices. If multiple endpoints have intersecting dependent
4871 * devices, unbinding the driver from any one of them will possibly leave
4872 * the others unable to operate.
4873 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004874static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004875{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004876 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004877 return;
4878
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004879 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004880}
4881
Joerg Roedel127c7612015-07-23 17:44:46 +02004882static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004883{
Weidong Hanc7151a82008-12-08 22:51:37 +08004884 struct intel_iommu *iommu;
4885 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004886
Joerg Roedel55d94042015-07-22 16:50:40 +02004887 assert_spin_locked(&device_domain_lock);
4888
Joerg Roedelb608ac32015-07-21 18:19:08 +02004889 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004890 return;
4891
Joerg Roedel127c7612015-07-23 17:44:46 +02004892 iommu = info->iommu;
4893
4894 if (info->dev) {
4895 iommu_disable_dev_iotlb(info);
4896 domain_context_clear(iommu, info->dev);
4897 }
4898
Joerg Roedelb608ac32015-07-21 18:19:08 +02004899 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004900
Joerg Roedeld160aca2015-07-22 11:52:53 +02004901 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004902 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004903 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004904
4905 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004906}
4907
Joerg Roedel55d94042015-07-22 16:50:40 +02004908static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4909 struct device *dev)
4910{
Joerg Roedel127c7612015-07-23 17:44:46 +02004911 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004912 unsigned long flags;
4913
Weidong Hanc7151a82008-12-08 22:51:37 +08004914 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004915 info = dev->archdata.iommu;
4916 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004917 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004918}
4919
4920static int md_domain_init(struct dmar_domain *domain, int guest_width)
4921{
4922 int adjust_width;
4923
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004924 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4925 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004926 domain_reserve_special_ranges(domain);
4927
4928 /* calculate AGAW */
4929 domain->gaw = guest_width;
4930 adjust_width = guestwidth_to_adjustwidth(guest_width);
4931 domain->agaw = width_to_agaw(adjust_width);
4932
Weidong Han5e98c4b2008-12-08 23:03:27 +08004933 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004934 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004935 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004936 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004937
4938 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004939 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004940 if (!domain->pgd)
4941 return -ENOMEM;
4942 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4943 return 0;
4944}
4945
Joerg Roedel00a77de2015-03-26 13:43:08 +01004946static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004947{
Joerg Roedel5d450802008-12-03 14:52:32 +01004948 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004949 struct iommu_domain *domain;
4950
4951 if (type != IOMMU_DOMAIN_UNMANAGED)
4952 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004953
Jiang Liuab8dfe22014-07-11 14:19:27 +08004954 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004955 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004956 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004957 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004958 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004959 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004960 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004961 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004962 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004963 }
Allen Kay8140a952011-10-14 12:32:17 -07004964 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004965
Joerg Roedel00a77de2015-03-26 13:43:08 +01004966 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004967 domain->geometry.aperture_start = 0;
4968 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4969 domain->geometry.force_aperture = true;
4970
Joerg Roedel00a77de2015-03-26 13:43:08 +01004971 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004972}
Kay, Allen M38717942008-09-09 18:37:29 +03004973
Joerg Roedel00a77de2015-03-26 13:43:08 +01004974static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004975{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004976 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004977}
Kay, Allen M38717942008-09-09 18:37:29 +03004978
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004979static int intel_iommu_attach_device(struct iommu_domain *domain,
4980 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004981{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004982 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004983 struct intel_iommu *iommu;
4984 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004985 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004986
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004987 if (device_is_rmrr_locked(dev)) {
4988 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4989 return -EPERM;
4990 }
4991
David Woodhouse7207d8f2014-03-09 16:31:06 -07004992 /* normally dev is not mapped */
4993 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004994 struct dmar_domain *old_domain;
4995
David Woodhouse1525a292014-03-06 16:19:30 +00004996 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004997 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02004998 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02004999 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02005000 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01005001
5002 if (!domain_type_is_vm_or_si(old_domain) &&
5003 list_empty(&old_domain->devices))
5004 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005005 }
5006 }
5007
David Woodhouse156baca2014-03-09 14:00:57 -07005008 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005009 if (!iommu)
5010 return -ENODEV;
5011
5012 /* check if this iommu agaw is sufficient for max mapped address */
5013 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01005014 if (addr_width > cap_mgaw(iommu->cap))
5015 addr_width = cap_mgaw(iommu->cap);
5016
5017 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005018 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005019 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01005020 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005021 return -EFAULT;
5022 }
Tom Lyona99c47a2010-05-17 08:20:45 +01005023 dmar_domain->gaw = addr_width;
5024
5025 /*
5026 * Knock out extra levels of page tables if necessary
5027 */
5028 while (iommu->agaw < dmar_domain->agaw) {
5029 struct dma_pte *pte;
5030
5031 pte = dmar_domain->pgd;
5032 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08005033 dmar_domain->pgd = (struct dma_pte *)
5034 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01005035 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01005036 }
5037 dmar_domain->agaw--;
5038 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005039
Joerg Roedel28ccce02015-07-21 14:45:31 +02005040 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005041}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005042
Joerg Roedel4c5478c2008-12-03 14:58:24 +01005043static void intel_iommu_detach_device(struct iommu_domain *domain,
5044 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03005045{
Joerg Roedele6de0f82015-07-22 16:30:36 +02005046 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03005047}
Kay, Allen M38717942008-09-09 18:37:29 +03005048
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005049static int intel_iommu_map(struct iommu_domain *domain,
5050 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005051 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03005052{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005053 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005054 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005055 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005056 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005057
Joerg Roedeldde57a22008-12-03 15:04:09 +01005058 if (iommu_prot & IOMMU_READ)
5059 prot |= DMA_PTE_READ;
5060 if (iommu_prot & IOMMU_WRITE)
5061 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08005062 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
5063 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005064
David Woodhouse163cc522009-06-28 00:51:17 +01005065 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005066 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005067 u64 end;
5068
5069 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01005070 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005071 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005072 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005073 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01005074 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005075 return -EFAULT;
5076 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01005077 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005078 }
David Woodhousead051222009-06-28 14:22:28 +01005079 /* Round up size to next multiple of PAGE_SIZE, if it and
5080 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01005081 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01005082 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
5083 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005084 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03005085}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005086
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005087static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00005088 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005089{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005090 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00005091 struct page *freelist = NULL;
5092 struct intel_iommu *iommu;
5093 unsigned long start_pfn, last_pfn;
5094 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02005095 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01005096
David Woodhouse5cf0a762014-03-19 16:07:49 +00005097 /* Cope with horrid API which requires us to unmap more than the
5098 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02005099 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00005100
5101 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
5102 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
5103
David Woodhouseea8ea462014-03-05 17:09:32 +00005104 start_pfn = iova >> VTD_PAGE_SHIFT;
5105 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
5106
5107 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
5108
5109 npages = last_pfn - start_pfn + 1;
5110
Joerg Roedel29a27712015-07-21 17:17:12 +02005111 for_each_domain_iommu(iommu_id, dmar_domain) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02005112 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00005113
Joerg Roedel42e8c182015-07-21 15:50:02 +02005114 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
5115 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00005116 }
5117
5118 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005119
David Woodhouse163cc522009-06-28 00:51:17 +01005120 if (dmar_domain->max_addr == iova + size)
5121 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005122
David Woodhouse5cf0a762014-03-19 16:07:49 +00005123 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005124}
Kay, Allen M38717942008-09-09 18:37:29 +03005125
Joerg Roedeld14d6572008-12-03 15:06:57 +01005126static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05305127 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03005128{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005129 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03005130 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00005131 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005132 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03005133
David Woodhouse5cf0a762014-03-19 16:07:49 +00005134 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03005135 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005136 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03005137
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005138 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03005139}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005140
Joerg Roedel5d587b82014-09-05 10:50:45 +02005141static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005142{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005143 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005144 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04005145 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005146 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005147
Joerg Roedel5d587b82014-09-05 10:50:45 +02005148 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005149}
5150
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005151static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005152{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005153 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005154 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07005155 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04005156
Alex Williamsona5459cf2014-06-12 16:12:31 -06005157 iommu = device_to_iommu(dev, &bus, &devfn);
5158 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005159 return -ENODEV;
5160
Alex Williamsona5459cf2014-06-12 16:12:31 -06005161 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005162
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005163 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06005164
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005165 if (IS_ERR(group))
5166 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005167
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005168 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005169 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005170}
5171
5172static void intel_iommu_remove_device(struct device *dev)
5173{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005174 struct intel_iommu *iommu;
5175 u8 bus, devfn;
5176
5177 iommu = device_to_iommu(dev, &bus, &devfn);
5178 if (!iommu)
5179 return;
5180
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005181 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06005182
5183 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005184}
5185
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005186#ifdef CONFIG_INTEL_IOMMU_SVM
5187int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
5188{
5189 struct device_domain_info *info;
5190 struct context_entry *context;
5191 struct dmar_domain *domain;
5192 unsigned long flags;
5193 u64 ctx_lo;
5194 int ret;
5195
5196 domain = get_valid_domain_for_dev(sdev->dev);
5197 if (!domain)
5198 return -EINVAL;
5199
5200 spin_lock_irqsave(&device_domain_lock, flags);
5201 spin_lock(&iommu->lock);
5202
5203 ret = -EINVAL;
5204 info = sdev->dev->archdata.iommu;
5205 if (!info || !info->pasid_supported)
5206 goto out;
5207
5208 context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
5209 if (WARN_ON(!context))
5210 goto out;
5211
5212 ctx_lo = context[0].lo;
5213
5214 sdev->did = domain->iommu_did[iommu->seq_id];
5215 sdev->sid = PCI_DEVID(info->bus, info->devfn);
5216
5217 if (!(ctx_lo & CONTEXT_PASIDE)) {
5218 context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
5219 context[1].lo = (u64)virt_to_phys(iommu->pasid_table) | ecap_pss(iommu->ecap);
5220 wmb();
5221 /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
5222 * extended to permit requests-with-PASID if the PASIDE bit
5223 * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
5224 * however, the PASIDE bit is ignored and requests-with-PASID
5225 * are unconditionally blocked. Which makes less sense.
5226 * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
5227 * "guest mode" translation types depending on whether ATS
5228 * is available or not. Annoyingly, we can't use the new
5229 * modes *unless* PASIDE is set. */
5230 if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
5231 ctx_lo &= ~CONTEXT_TT_MASK;
5232 if (info->ats_supported)
5233 ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
5234 else
5235 ctx_lo |= CONTEXT_TT_PT_PASID << 2;
5236 }
5237 ctx_lo |= CONTEXT_PASIDE;
David Woodhouse907fea32015-10-13 14:11:13 +01005238 if (iommu->pasid_state_table)
5239 ctx_lo |= CONTEXT_DINVE;
David Woodhousea222a7f2015-10-07 23:35:18 +01005240 if (info->pri_supported)
5241 ctx_lo |= CONTEXT_PRS;
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005242 context[0].lo = ctx_lo;
5243 wmb();
5244 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
5245 DMA_CCMD_MASK_NOBIT,
5246 DMA_CCMD_DEVICE_INVL);
5247 }
5248
5249 /* Enable PASID support in the device, if it wasn't already */
5250 if (!info->pasid_enabled)
5251 iommu_enable_dev_iotlb(info);
5252
5253 if (info->ats_enabled) {
5254 sdev->dev_iotlb = 1;
5255 sdev->qdep = info->ats_qdep;
5256 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
5257 sdev->qdep = 0;
5258 }
5259 ret = 0;
5260
5261 out:
5262 spin_unlock(&iommu->lock);
5263 spin_unlock_irqrestore(&device_domain_lock, flags);
5264
5265 return ret;
5266}
5267
5268struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5269{
5270 struct intel_iommu *iommu;
5271 u8 bus, devfn;
5272
5273 if (iommu_dummy(dev)) {
5274 dev_warn(dev,
5275 "No IOMMU translation for device; cannot enable SVM\n");
5276 return NULL;
5277 }
5278
5279 iommu = device_to_iommu(dev, &bus, &devfn);
5280 if ((!iommu)) {
Sudeep Duttb9997e32015-10-18 20:54:37 -07005281 dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005282 return NULL;
5283 }
5284
5285 if (!iommu->pasid_table) {
Sudeep Duttb9997e32015-10-18 20:54:37 -07005286 dev_err(dev, "PASID not enabled on IOMMU; cannot enable SVM\n");
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005287 return NULL;
5288 }
5289
5290 return iommu;
5291}
5292#endif /* CONFIG_INTEL_IOMMU_SVM */
5293
Thierry Redingb22f6432014-06-27 09:03:12 +02005294static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02005295 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01005296 .domain_alloc = intel_iommu_domain_alloc,
5297 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005298 .attach_dev = intel_iommu_attach_device,
5299 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005300 .map = intel_iommu_map,
5301 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07005302 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005303 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005304 .add_device = intel_iommu_add_device,
5305 .remove_device = intel_iommu_remove_device,
Joerg Roedela960fad2015-10-21 23:51:39 +02005306 .device_group = pci_device_group,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02005307 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005308};
David Woodhouse9af88142009-02-13 23:18:03 +00005309
Daniel Vetter94526182013-01-20 23:50:13 +01005310static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
5311{
5312 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005313 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01005314 dmar_map_gfx = 0;
5315}
5316
5317DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
5318DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
5319DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
5320DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
5321DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
5322DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
5323DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
5324
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005325static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00005326{
5327 /*
5328 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01005329 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00005330 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005331 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005332 rwbf_quirk = 1;
5333}
5334
5335DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005336DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5337DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5338DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5339DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5340DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5341DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005342
Adam Jacksoneecfd572010-08-25 21:17:34 +01005343#define GGC 0x52
5344#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5345#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5346#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5347#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5348#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5349#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5350#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5351#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5352
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005353static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005354{
5355 unsigned short ggc;
5356
Adam Jacksoneecfd572010-08-25 21:17:34 +01005357 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005358 return;
5359
Adam Jacksoneecfd572010-08-25 21:17:34 +01005360 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005361 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005362 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005363 } else if (dmar_map_gfx) {
5364 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005365 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005366 intel_iommu_strict = 1;
5367 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005368}
5369DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5370DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5371DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5372DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5373
David Woodhousee0fc7e02009-09-30 09:12:17 -07005374/* On Tylersburg chipsets, some BIOSes have been known to enable the
5375 ISOCH DMAR unit for the Azalia sound device, but not give it any
5376 TLB entries, which causes it to deadlock. Check for that. We do
5377 this in a function called from init_dmars(), instead of in a PCI
5378 quirk, because we don't want to print the obnoxious "BIOS broken"
5379 message if VT-d is actually disabled.
5380*/
5381static void __init check_tylersburg_isoch(void)
5382{
5383 struct pci_dev *pdev;
5384 uint32_t vtisochctrl;
5385
5386 /* If there's no Azalia in the system anyway, forget it. */
5387 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5388 if (!pdev)
5389 return;
5390 pci_dev_put(pdev);
5391
5392 /* System Management Registers. Might be hidden, in which case
5393 we can't do the sanity check. But that's OK, because the
5394 known-broken BIOSes _don't_ actually hide it, so far. */
5395 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5396 if (!pdev)
5397 return;
5398
5399 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5400 pci_dev_put(pdev);
5401 return;
5402 }
5403
5404 pci_dev_put(pdev);
5405
5406 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5407 if (vtisochctrl & 1)
5408 return;
5409
5410 /* Drop all bits other than the number of TLB entries */
5411 vtisochctrl &= 0x1c;
5412
5413 /* If we have the recommended number of TLB entries (16), fine. */
5414 if (vtisochctrl == 0x10)
5415 return;
5416
5417 /* Zero TLB entries? You get to ride the short bus to school. */
5418 if (!vtisochctrl) {
5419 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5420 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5421 dmi_get_system_info(DMI_BIOS_VENDOR),
5422 dmi_get_system_info(DMI_BIOS_VERSION),
5423 dmi_get_system_info(DMI_PRODUCT_VERSION));
5424 iommu_identity_mapping |= IDENTMAP_AZALIA;
5425 return;
5426 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005427
5428 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005429 vtisochctrl);
5430}