blob: 91f0c3d5a6acaeb473d3eafade95d62029c23e64 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
mark gross5e0d2a62008-03-04 15:22:08 -080036#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030037#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010038#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010040#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070041#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100042#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020043#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080044#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070045#include <linux/dma-contiguous.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020046#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070047#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070048#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070050
Joerg Roedel078e1ee2012-09-26 12:44:43 +020051#include "irq_remapping.h"
52
Fenghua Yu5b6985c2008-10-16 18:02:32 -070053#define ROOT_SIZE VTD_PAGE_SIZE
54#define CONTEXT_SIZE VTD_PAGE_SIZE
55
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070056#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000057#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070059#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060
61#define IOAPIC_RANGE_START (0xfee00000)
62#define IOAPIC_RANGE_END (0xfeefffff)
63#define IOVA_START_ADDR (0x1000)
64
65#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
66
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070067#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080068#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070069
David Woodhouse2ebe3152009-09-19 07:34:04 -070070#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
71#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
72
73/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
74 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
75#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
76 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
77#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070078
Robin Murphy1b722502015-01-12 17:51:15 +000079/* IO virtual address start page frame number */
80#define IOVA_START_PFN (1)
81
Mark McLoughlinf27be032008-11-20 15:49:43 +000082#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070083#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070084#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080085
Andrew Mortondf08cdc2010-09-22 13:05:11 -070086/* page table handling */
87#define LEVEL_STRIDE (9)
88#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
89
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020090/*
91 * This bitmap is used to advertise the page sizes our hardware support
92 * to the IOMMU core, which will then use this information to split
93 * physically contiguous memory regions it is mapping into page sizes
94 * that we support.
95 *
96 * Traditionally the IOMMU core just handed us the mappings directly,
97 * after making sure the size is an order of a 4KiB page and that the
98 * mapping has natural alignment.
99 *
100 * To retain this behavior, we currently advertise that we support
101 * all page sizes that are an order of 4KiB.
102 *
103 * If at some point we'd like to utilize the IOMMU core's new behavior,
104 * we could change this to advertise the real page sizes we support.
105 */
106#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
107
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700108static inline int agaw_to_level(int agaw)
109{
110 return agaw + 2;
111}
112
113static inline int agaw_to_width(int agaw)
114{
Jiang Liu5c645b32014-01-06 14:18:12 +0800115 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700116}
117
118static inline int width_to_agaw(int width)
119{
Jiang Liu5c645b32014-01-06 14:18:12 +0800120 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700121}
122
123static inline unsigned int level_to_offset_bits(int level)
124{
125 return (level - 1) * LEVEL_STRIDE;
126}
127
128static inline int pfn_level_offset(unsigned long pfn, int level)
129{
130 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
131}
132
133static inline unsigned long level_mask(int level)
134{
135 return -1UL << level_to_offset_bits(level);
136}
137
138static inline unsigned long level_size(int level)
139{
140 return 1UL << level_to_offset_bits(level);
141}
142
143static inline unsigned long align_to_level(unsigned long pfn, int level)
144{
145 return (pfn + level_size(level) - 1) & level_mask(level);
146}
David Woodhousefd18de52009-05-10 23:57:41 +0100147
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100148static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
149{
Jiang Liu5c645b32014-01-06 14:18:12 +0800150 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100151}
152
David Woodhousedd4e8312009-06-27 16:21:20 +0100153/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
154 are never going to work. */
155static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
156{
157 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
158}
159
160static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
161{
162 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
163}
164static inline unsigned long page_to_dma_pfn(struct page *pg)
165{
166 return mm_to_dma_pfn(page_to_pfn(pg));
167}
168static inline unsigned long virt_to_dma_pfn(void *p)
169{
170 return page_to_dma_pfn(virt_to_page(p));
171}
172
Weidong Hand9630fe2008-12-08 11:06:32 +0800173/* global iommu list, set NULL for ignored DMAR units */
174static struct intel_iommu **g_iommus;
175
David Woodhousee0fc7e02009-09-30 09:12:17 -0700176static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000177static int rwbf_quirk;
178
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000179/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700180 * set to 1 to panic kernel if can't successfully enable VT-d
181 * (used when kernel is launched w/ TXT)
182 */
183static int force_on = 0;
184
185/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000186 * 0: Present
187 * 1-11: Reserved
188 * 12-63: Context Ptr (12 - (haw-1))
189 * 64-127: Reserved
190 */
191struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000192 u64 lo;
193 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000194};
195#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000196
Joerg Roedel091d42e2015-06-12 11:56:10 +0200197/*
198 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
199 * if marked present.
200 */
201static phys_addr_t root_entry_lctp(struct root_entry *re)
202{
203 if (!(re->lo & 1))
204 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000205
Joerg Roedel091d42e2015-06-12 11:56:10 +0200206 return re->lo & VTD_PAGE_MASK;
207}
208
209/*
210 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
211 * if marked present.
212 */
213static phys_addr_t root_entry_uctp(struct root_entry *re)
214{
215 if (!(re->hi & 1))
216 return 0;
217
218 return re->hi & VTD_PAGE_MASK;
219}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000220/*
221 * low 64 bits:
222 * 0: present
223 * 1: fault processing disable
224 * 2-3: translation type
225 * 12-63: address space root
226 * high 64 bits:
227 * 0-2: address width
228 * 3-6: aval
229 * 8-23: domain id
230 */
231struct context_entry {
232 u64 lo;
233 u64 hi;
234};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000235
Joerg Roedelcf484d02015-06-12 12:21:46 +0200236static inline void context_clear_pasid_enable(struct context_entry *context)
237{
238 context->lo &= ~(1ULL << 11);
239}
240
241static inline bool context_pasid_enabled(struct context_entry *context)
242{
243 return !!(context->lo & (1ULL << 11));
244}
245
246static inline void context_set_copied(struct context_entry *context)
247{
248 context->hi |= (1ull << 3);
249}
250
251static inline bool context_copied(struct context_entry *context)
252{
253 return !!(context->hi & (1ULL << 3));
254}
255
256static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000257{
258 return (context->lo & 1);
259}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200260
261static inline bool context_present(struct context_entry *context)
262{
263 return context_pasid_enabled(context) ?
264 __context_present(context) :
265 __context_present(context) && !context_copied(context);
266}
267
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000268static inline void context_set_present(struct context_entry *context)
269{
270 context->lo |= 1;
271}
272
273static inline void context_set_fault_enable(struct context_entry *context)
274{
275 context->lo &= (((u64)-1) << 2) | 1;
276}
277
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000278static inline void context_set_translation_type(struct context_entry *context,
279 unsigned long value)
280{
281 context->lo &= (((u64)-1) << 4) | 3;
282 context->lo |= (value & 3) << 2;
283}
284
285static inline void context_set_address_root(struct context_entry *context,
286 unsigned long value)
287{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800288 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000289 context->lo |= value & VTD_PAGE_MASK;
290}
291
292static inline void context_set_address_width(struct context_entry *context,
293 unsigned long value)
294{
295 context->hi |= value & 7;
296}
297
298static inline void context_set_domain_id(struct context_entry *context,
299 unsigned long value)
300{
301 context->hi |= (value & ((1 << 16) - 1)) << 8;
302}
303
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200304static inline int context_domain_id(struct context_entry *c)
305{
306 return((c->hi >> 8) & 0xffff);
307}
308
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000309static inline void context_clear_entry(struct context_entry *context)
310{
311 context->lo = 0;
312 context->hi = 0;
313}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000314
Mark McLoughlin622ba122008-11-20 15:49:46 +0000315/*
316 * 0: readable
317 * 1: writable
318 * 2-6: reserved
319 * 7: super page
Sheng Yang9cf066972009-03-18 15:33:07 +0800320 * 8-10: available
321 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000322 * 12-63: Host physcial address
323 */
324struct dma_pte {
325 u64 val;
326};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000327
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000328static inline void dma_clear_pte(struct dma_pte *pte)
329{
330 pte->val = 0;
331}
332
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000333static inline u64 dma_pte_addr(struct dma_pte *pte)
334{
David Woodhousec85994e2009-07-01 19:21:24 +0100335#ifdef CONFIG_64BIT
336 return pte->val & VTD_PAGE_MASK;
337#else
338 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100339 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100340#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000341}
342
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000343static inline bool dma_pte_present(struct dma_pte *pte)
344{
345 return (pte->val & 3) != 0;
346}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000347
Allen Kay4399c8b2011-10-14 12:32:46 -0700348static inline bool dma_pte_superpage(struct dma_pte *pte)
349{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200350 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700351}
352
David Woodhouse75e6bf92009-07-02 11:21:16 +0100353static inline int first_pte_in_page(struct dma_pte *pte)
354{
355 return !((unsigned long)pte & ~VTD_PAGE_MASK);
356}
357
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700358/*
359 * This domain is a statically identity mapping domain.
360 * 1. This domain creats a static 1:1 mapping to all usable memory.
361 * 2. It maps to each iommu if successful.
362 * 3. Each iommu mapps to this domain if successful.
363 */
David Woodhouse19943b02009-08-04 16:19:20 +0100364static struct dmar_domain *si_domain;
365static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700366
Joerg Roedel28ccce02015-07-21 14:45:31 +0200367/*
368 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800369 * across iommus may be owned in one domain, e.g. kvm guest.
370 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800371#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800372
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700373/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800374#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700375
Mark McLoughlin99126f72008-11-20 15:49:47 +0000376struct dmar_domain {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700377 int nid; /* node id */
Jiang Liu78d8e702014-11-09 22:47:57 +0800378 DECLARE_BITMAP(iommu_bmp, DMAR_UNITS_SUPPORTED);
Mike Travis1b198bb2012-03-05 15:05:16 -0800379 /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000380
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +0200381 u16 iommu_did[DMAR_UNITS_SUPPORTED];
382 /* Domain ids per IOMMU. Use u16 since
383 * domain ids are 16 bit wide according
384 * to VT-d spec, section 9.3 */
385
Joerg Roedel00a77de2015-03-26 13:43:08 +0100386 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000387 struct iova_domain iovad; /* iova's that belong to this domain */
388
389 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000390 int gaw; /* max guest address width */
391
392 /* adjusted guest address width, 0 is level 2 30-bit */
393 int agaw;
394
Weidong Han3b5410e2008-12-08 09:17:15 +0800395 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800396
397 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800398 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800399 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100400 int iommu_superpage;/* Level of superpages supported:
401 0 == 4KiB (no superpages), 1 == 2MiB,
402 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanc7151a82008-12-08 22:51:37 +0800403 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800404 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100405
406 struct iommu_domain domain; /* generic domain data structure for
407 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000408};
409
Mark McLoughlina647dac2008-11-20 15:49:48 +0000410/* PCI domain-device relationship */
411struct device_domain_info {
412 struct list_head link; /* link to domain siblings */
413 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100414 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000415 u8 devfn; /* PCI devfn number */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000416 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800417 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000418 struct dmar_domain *domain; /* pointer to domain */
419};
420
Jiang Liub94e4112014-02-19 14:07:25 +0800421struct dmar_rmrr_unit {
422 struct list_head list; /* list of rmrr units */
423 struct acpi_dmar_header *hdr; /* ACPI header */
424 u64 base_address; /* reserved base address*/
425 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000426 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800427 int devices_cnt; /* target device count */
428};
429
430struct dmar_atsr_unit {
431 struct list_head list; /* list of ATSR units */
432 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000433 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800434 int devices_cnt; /* target device count */
435 u8 include_all:1; /* include all ports */
436};
437
438static LIST_HEAD(dmar_atsr_units);
439static LIST_HEAD(dmar_rmrr_units);
440
441#define for_each_rmrr_units(rmrr) \
442 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
443
mark gross5e0d2a62008-03-04 15:22:08 -0800444static void flush_unmaps_timeout(unsigned long data);
445
Jiang Liub707cb02014-01-06 14:18:26 +0800446static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800447
mark gross80b20dd2008-04-18 13:53:58 -0700448#define HIGH_WATER_MARK 250
449struct deferred_flush_tables {
450 int next;
451 struct iova *iova[HIGH_WATER_MARK];
452 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000453 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700454};
455
456static struct deferred_flush_tables *deferred_flush;
457
mark gross5e0d2a62008-03-04 15:22:08 -0800458/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800459static int g_num_of_iommus;
460
461static DEFINE_SPINLOCK(async_umap_flush_lock);
462static LIST_HEAD(unmaps_to_do);
463
464static int timer_on;
465static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800466
Jiang Liu92d03cc2014-02-19 14:07:28 +0800467static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700468static void domain_remove_dev_info(struct dmar_domain *domain);
Jiang Liub94e4112014-02-19 14:07:25 +0800469static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -0700470 struct device *dev);
Jiang Liu92d03cc2014-02-19 14:07:28 +0800471static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000472 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800473static int domain_detach_iommu(struct dmar_domain *domain,
474 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700475
Suresh Siddhad3f13812011-08-23 17:05:25 -0700476#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800477int dmar_disabled = 0;
478#else
479int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700480#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800481
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200482int intel_iommu_enabled = 0;
483EXPORT_SYMBOL_GPL(intel_iommu_enabled);
484
David Woodhouse2d9e6672010-06-15 10:57:57 +0100485static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700486static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800487static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100488static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100489static int intel_iommu_ecs = 1;
490
491/* We only actually use ECS when PASID support (on the new bit 40)
492 * is also advertised. Some early implementations — the ones with
493 * PASID support on bit 28 — have issues even when we *only* use
494 * extended root/context tables. */
495#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
496 ecap_pasid(iommu->ecap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700497
David Woodhousec0771df2011-10-14 20:59:46 +0100498int intel_iommu_gfx_mapped;
499EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
500
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700501#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
502static DEFINE_SPINLOCK(device_domain_lock);
503static LIST_HEAD(device_domain_list);
504
Thierry Redingb22f6432014-06-27 09:03:12 +0200505static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100506
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200507static bool translation_pre_enabled(struct intel_iommu *iommu)
508{
509 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
510}
511
Joerg Roedel091d42e2015-06-12 11:56:10 +0200512static void clear_translation_pre_enabled(struct intel_iommu *iommu)
513{
514 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
515}
516
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200517static void init_translation_status(struct intel_iommu *iommu)
518{
519 u32 gsts;
520
521 gsts = readl(iommu->reg + DMAR_GSTS_REG);
522 if (gsts & DMA_GSTS_TES)
523 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
524}
525
Joerg Roedel00a77de2015-03-26 13:43:08 +0100526/* Convert generic 'struct iommu_domain to private struct dmar_domain */
527static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
528{
529 return container_of(dom, struct dmar_domain, domain);
530}
531
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700532static int __init intel_iommu_setup(char *str)
533{
534 if (!str)
535 return -EINVAL;
536 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800537 if (!strncmp(str, "on", 2)) {
538 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200539 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800540 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700541 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200542 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700543 } else if (!strncmp(str, "igfx_off", 8)) {
544 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200545 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700546 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200547 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700548 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800549 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200550 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800551 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100552 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200553 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100554 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100555 } else if (!strncmp(str, "ecs_off", 7)) {
556 printk(KERN_INFO
557 "Intel-IOMMU: disable extended context table support\n");
558 intel_iommu_ecs = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700559 }
560
561 str += strcspn(str, ",");
562 while (*str == ',')
563 str++;
564 }
565 return 0;
566}
567__setup("intel_iommu=", intel_iommu_setup);
568
569static struct kmem_cache *iommu_domain_cache;
570static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700571
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200572static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
573{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200574 struct dmar_domain **domains;
575 int idx = did >> 8;
576
577 domains = iommu->domains[idx];
578 if (!domains)
579 return NULL;
580
581 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200582}
583
584static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
585 struct dmar_domain *domain)
586{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200587 struct dmar_domain **domains;
588 int idx = did >> 8;
589
590 if (!iommu->domains[idx]) {
591 size_t size = 256 * sizeof(struct dmar_domain *);
592 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
593 }
594
595 domains = iommu->domains[idx];
596 if (WARN_ON(!domains))
597 return;
598 else
599 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200600}
601
Suresh Siddha4c923d42009-10-02 11:01:24 -0700602static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700603{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700604 struct page *page;
605 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700606
Suresh Siddha4c923d42009-10-02 11:01:24 -0700607 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
608 if (page)
609 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700610 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700611}
612
613static inline void free_pgtable_page(void *vaddr)
614{
615 free_page((unsigned long)vaddr);
616}
617
618static inline void *alloc_domain_mem(void)
619{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900620 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700621}
622
Kay, Allen M38717942008-09-09 18:37:29 +0300623static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700624{
625 kmem_cache_free(iommu_domain_cache, vaddr);
626}
627
628static inline void * alloc_devinfo_mem(void)
629{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900630 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700631}
632
633static inline void free_devinfo_mem(void *vaddr)
634{
635 kmem_cache_free(iommu_devinfo_cache, vaddr);
636}
637
Jiang Liuab8dfe22014-07-11 14:19:27 +0800638static inline int domain_type_is_vm(struct dmar_domain *domain)
639{
640 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
641}
642
Joerg Roedel28ccce02015-07-21 14:45:31 +0200643static inline int domain_type_is_si(struct dmar_domain *domain)
644{
645 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
646}
647
Jiang Liuab8dfe22014-07-11 14:19:27 +0800648static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
649{
650 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
651 DOMAIN_FLAG_STATIC_IDENTITY);
652}
Weidong Han1b573682008-12-08 15:34:06 +0800653
Jiang Liu162d1b12014-07-11 14:19:35 +0800654static inline int domain_pfn_supported(struct dmar_domain *domain,
655 unsigned long pfn)
656{
657 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
658
659 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
660}
661
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700662static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800663{
664 unsigned long sagaw;
665 int agaw = -1;
666
667 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700668 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800669 agaw >= 0; agaw--) {
670 if (test_bit(agaw, &sagaw))
671 break;
672 }
673
674 return agaw;
675}
676
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700677/*
678 * Calculate max SAGAW for each iommu.
679 */
680int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
681{
682 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
683}
684
685/*
686 * calculate agaw for each iommu.
687 * "SAGAW" may be different across iommus, use a default agaw, and
688 * get a supported less agaw for iommus that don't support the default agaw.
689 */
690int iommu_calculate_agaw(struct intel_iommu *iommu)
691{
692 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
693}
694
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700695/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800696static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
697{
698 int iommu_id;
699
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700700 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800701 BUG_ON(domain_type_is_vm_or_si(domain));
Mike Travis1b198bb2012-03-05 15:05:16 -0800702 iommu_id = find_first_bit(domain->iommu_bmp, g_num_of_iommus);
Weidong Han8c11e792008-12-08 15:29:22 +0800703 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
704 return NULL;
705
706 return g_iommus[iommu_id];
707}
708
Weidong Han8e6040972008-12-08 15:49:06 +0800709static void domain_update_iommu_coherency(struct dmar_domain *domain)
710{
David Woodhoused0501962014-03-11 17:10:29 -0700711 struct dmar_drhd_unit *drhd;
712 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100713 bool found = false;
714 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800715
David Woodhoused0501962014-03-11 17:10:29 -0700716 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800717
Mike Travis1b198bb2012-03-05 15:05:16 -0800718 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100719 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800720 if (!ecap_coherent(g_iommus[i]->ecap)) {
721 domain->iommu_coherency = 0;
722 break;
723 }
Weidong Han8e6040972008-12-08 15:49:06 +0800724 }
David Woodhoused0501962014-03-11 17:10:29 -0700725 if (found)
726 return;
727
728 /* No hardware attached; use lowest common denominator */
729 rcu_read_lock();
730 for_each_active_iommu(iommu, drhd) {
731 if (!ecap_coherent(iommu->ecap)) {
732 domain->iommu_coherency = 0;
733 break;
734 }
735 }
736 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800737}
738
Jiang Liu161f6932014-07-11 14:19:37 +0800739static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100740{
Allen Kay8140a952011-10-14 12:32:17 -0700741 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800742 struct intel_iommu *iommu;
743 int ret = 1;
744
745 rcu_read_lock();
746 for_each_active_iommu(iommu, drhd) {
747 if (iommu != skip) {
748 if (!ecap_sc_support(iommu->ecap)) {
749 ret = 0;
750 break;
751 }
752 }
753 }
754 rcu_read_unlock();
755
756 return ret;
757}
758
759static int domain_update_iommu_superpage(struct intel_iommu *skip)
760{
761 struct dmar_drhd_unit *drhd;
762 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700763 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100764
765 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800766 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100767 }
768
Allen Kay8140a952011-10-14 12:32:17 -0700769 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800770 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700771 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800772 if (iommu != skip) {
773 mask &= cap_super_page_val(iommu->cap);
774 if (!mask)
775 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100776 }
777 }
Jiang Liu0e242612014-02-19 14:07:34 +0800778 rcu_read_unlock();
779
Jiang Liu161f6932014-07-11 14:19:37 +0800780 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100781}
782
Sheng Yang58c610b2009-03-18 15:33:05 +0800783/* Some capabilities may be different across iommus */
784static void domain_update_iommu_cap(struct dmar_domain *domain)
785{
786 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800787 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
788 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800789}
790
David Woodhouse03ecc322015-02-13 14:35:21 +0000791static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
792 u8 bus, u8 devfn, int alloc)
793{
794 struct root_entry *root = &iommu->root_entry[bus];
795 struct context_entry *context;
796 u64 *entry;
797
David Woodhousec83b2f22015-06-12 10:15:49 +0100798 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000799 if (devfn >= 0x80) {
800 devfn -= 0x80;
801 entry = &root->hi;
802 }
803 devfn *= 2;
804 }
805 entry = &root->lo;
806 if (*entry & 1)
807 context = phys_to_virt(*entry & VTD_PAGE_MASK);
808 else {
809 unsigned long phy_addr;
810 if (!alloc)
811 return NULL;
812
813 context = alloc_pgtable_page(iommu->node);
814 if (!context)
815 return NULL;
816
817 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
818 phy_addr = virt_to_phys((void *)context);
819 *entry = phy_addr | 1;
820 __iommu_flush_cache(iommu, entry, sizeof(*entry));
821 }
822 return &context[devfn];
823}
824
David Woodhouse4ed6a542015-05-11 14:59:20 +0100825static int iommu_dummy(struct device *dev)
826{
827 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
828}
829
David Woodhouse156baca2014-03-09 14:00:57 -0700830static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800831{
832 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800833 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700834 struct device *tmp;
835 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800836 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800837 int i;
838
David Woodhouse4ed6a542015-05-11 14:59:20 +0100839 if (iommu_dummy(dev))
840 return NULL;
841
David Woodhouse156baca2014-03-09 14:00:57 -0700842 if (dev_is_pci(dev)) {
843 pdev = to_pci_dev(dev);
844 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100845 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700846 dev = &ACPI_COMPANION(dev)->dev;
847
Jiang Liu0e242612014-02-19 14:07:34 +0800848 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800849 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700850 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100851 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800852
Jiang Liub683b232014-02-19 14:07:32 +0800853 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700854 drhd->devices_cnt, i, tmp) {
855 if (tmp == dev) {
856 *bus = drhd->devices[i].bus;
857 *devfn = drhd->devices[i].devfn;
858 goto out;
859 }
860
861 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000862 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700863
864 ptmp = to_pci_dev(tmp);
865 if (ptmp->subordinate &&
866 ptmp->subordinate->number <= pdev->bus->number &&
867 ptmp->subordinate->busn_res.end >= pdev->bus->number)
868 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100869 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800870
David Woodhouse156baca2014-03-09 14:00:57 -0700871 if (pdev && drhd->include_all) {
872 got_pdev:
873 *bus = pdev->bus->number;
874 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800875 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700876 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800877 }
Jiang Liub683b232014-02-19 14:07:32 +0800878 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700879 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800880 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800881
Jiang Liub683b232014-02-19 14:07:32 +0800882 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800883}
884
Weidong Han5331fe62008-12-08 23:00:00 +0800885static void domain_flush_cache(struct dmar_domain *domain,
886 void *addr, int size)
887{
888 if (!domain->iommu_coherency)
889 clflush_cache_range(addr, size);
890}
891
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700892static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
893{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700894 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000895 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700896 unsigned long flags;
897
898 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000899 context = iommu_context_addr(iommu, bus, devfn, 0);
900 if (context)
901 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700902 spin_unlock_irqrestore(&iommu->lock, flags);
903 return ret;
904}
905
906static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
907{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700908 struct context_entry *context;
909 unsigned long flags;
910
911 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000912 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700913 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000914 context_clear_entry(context);
915 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700916 }
917 spin_unlock_irqrestore(&iommu->lock, flags);
918}
919
920static void free_context_table(struct intel_iommu *iommu)
921{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700922 int i;
923 unsigned long flags;
924 struct context_entry *context;
925
926 spin_lock_irqsave(&iommu->lock, flags);
927 if (!iommu->root_entry) {
928 goto out;
929 }
930 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000931 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700932 if (context)
933 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000934
David Woodhousec83b2f22015-06-12 10:15:49 +0100935 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000936 continue;
937
938 context = iommu_context_addr(iommu, i, 0x80, 0);
939 if (context)
940 free_pgtable_page(context);
941
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700942 }
943 free_pgtable_page(iommu->root_entry);
944 iommu->root_entry = NULL;
945out:
946 spin_unlock_irqrestore(&iommu->lock, flags);
947}
948
David Woodhouseb026fd22009-06-28 10:37:25 +0100949static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000950 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700951{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700952 struct dma_pte *parent, *pte = NULL;
953 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700954 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700955
956 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200957
Jiang Liu162d1b12014-07-11 14:19:35 +0800958 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200959 /* Address beyond IOMMU's addressing capabilities. */
960 return NULL;
961
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700962 parent = domain->pgd;
963
David Woodhouse5cf0a762014-03-19 16:07:49 +0000964 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700965 void *tmp_page;
966
David Woodhouseb026fd22009-06-28 10:37:25 +0100967 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000969 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100970 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000971 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972 break;
973
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000974 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100975 uint64_t pteval;
976
Suresh Siddha4c923d42009-10-02 11:01:24 -0700977 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700978
David Woodhouse206a73c12009-07-01 19:30:28 +0100979 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700980 return NULL;
David Woodhouse206a73c12009-07-01 19:30:28 +0100981
David Woodhousec85994e2009-07-01 19:21:24 +0100982 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400983 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 +0800984 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100985 /* Someone else set it while we were thinking; use theirs. */
986 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800987 else
David Woodhousec85994e2009-07-01 19:21:24 +0100988 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700989 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000990 if (level == 1)
991 break;
992
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000993 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700994 level--;
995 }
996
David Woodhouse5cf0a762014-03-19 16:07:49 +0000997 if (!*target_level)
998 *target_level = level;
999
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001000 return pte;
1001}
1002
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001003
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001004/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001005static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1006 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001007 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001008{
1009 struct dma_pte *parent, *pte = NULL;
1010 int total = agaw_to_level(domain->agaw);
1011 int offset;
1012
1013 parent = domain->pgd;
1014 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001015 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001016 pte = &parent[offset];
1017 if (level == total)
1018 return pte;
1019
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001020 if (!dma_pte_present(pte)) {
1021 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001022 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001023 }
1024
Yijing Wange16922a2014-05-20 20:37:51 +08001025 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001026 *large_page = total;
1027 return pte;
1028 }
1029
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001030 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001031 total--;
1032 }
1033 return NULL;
1034}
1035
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001036/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001037static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +01001038 unsigned long start_pfn,
1039 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001040{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001041 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001042 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001043
Jiang Liu162d1b12014-07-11 14:19:35 +08001044 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1045 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001046 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001047
David Woodhouse04b18e62009-06-27 19:15:01 +01001048 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001049 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001050 large_page = 1;
1051 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001052 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001053 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001054 continue;
1055 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001056 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001057 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001058 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001059 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001060 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1061
David Woodhouse310a5ab2009-06-28 18:52:20 +01001062 domain_flush_cache(domain, first_pte,
1063 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001064
1065 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001066}
1067
Alex Williamson3269ee02013-06-15 10:27:19 -06001068static void dma_pte_free_level(struct dmar_domain *domain, int level,
1069 struct dma_pte *pte, unsigned long pfn,
1070 unsigned long start_pfn, unsigned long last_pfn)
1071{
1072 pfn = max(start_pfn, pfn);
1073 pte = &pte[pfn_level_offset(pfn, level)];
1074
1075 do {
1076 unsigned long level_pfn;
1077 struct dma_pte *level_pte;
1078
1079 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1080 goto next;
1081
1082 level_pfn = pfn & level_mask(level - 1);
1083 level_pte = phys_to_virt(dma_pte_addr(pte));
1084
1085 if (level > 2)
1086 dma_pte_free_level(domain, level - 1, level_pte,
1087 level_pfn, start_pfn, last_pfn);
1088
1089 /* If range covers entire pagetable, free it */
1090 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001091 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001092 dma_clear_pte(pte);
1093 domain_flush_cache(domain, pte, sizeof(*pte));
1094 free_pgtable_page(level_pte);
1095 }
1096next:
1097 pfn += level_size(level);
1098 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1099}
1100
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001101/* free page table pages. last level pte should already be cleared */
1102static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001103 unsigned long start_pfn,
1104 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001105{
Jiang Liu162d1b12014-07-11 14:19:35 +08001106 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1107 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001108 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001109
Jiang Liud41a4ad2014-07-11 14:19:34 +08001110 dma_pte_clear_range(domain, start_pfn, last_pfn);
1111
David Woodhousef3a0a522009-06-30 03:40:07 +01001112 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001113 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1114 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001115
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001116 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001117 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001118 free_pgtable_page(domain->pgd);
1119 domain->pgd = NULL;
1120 }
1121}
1122
David Woodhouseea8ea462014-03-05 17:09:32 +00001123/* When a page at a given level is being unlinked from its parent, we don't
1124 need to *modify* it at all. All we need to do is make a list of all the
1125 pages which can be freed just as soon as we've flushed the IOTLB and we
1126 know the hardware page-walk will no longer touch them.
1127 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1128 be freed. */
1129static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1130 int level, struct dma_pte *pte,
1131 struct page *freelist)
1132{
1133 struct page *pg;
1134
1135 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1136 pg->freelist = freelist;
1137 freelist = pg;
1138
1139 if (level == 1)
1140 return freelist;
1141
Jiang Liuadeb2592014-04-09 10:20:39 +08001142 pte = page_address(pg);
1143 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001144 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1145 freelist = dma_pte_list_pagetables(domain, level - 1,
1146 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001147 pte++;
1148 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001149
1150 return freelist;
1151}
1152
1153static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1154 struct dma_pte *pte, unsigned long pfn,
1155 unsigned long start_pfn,
1156 unsigned long last_pfn,
1157 struct page *freelist)
1158{
1159 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1160
1161 pfn = max(start_pfn, pfn);
1162 pte = &pte[pfn_level_offset(pfn, level)];
1163
1164 do {
1165 unsigned long level_pfn;
1166
1167 if (!dma_pte_present(pte))
1168 goto next;
1169
1170 level_pfn = pfn & level_mask(level);
1171
1172 /* If range covers entire pagetable, free it */
1173 if (start_pfn <= level_pfn &&
1174 last_pfn >= level_pfn + level_size(level) - 1) {
1175 /* These suborbinate page tables are going away entirely. Don't
1176 bother to clear them; we're just going to *free* them. */
1177 if (level > 1 && !dma_pte_superpage(pte))
1178 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1179
1180 dma_clear_pte(pte);
1181 if (!first_pte)
1182 first_pte = pte;
1183 last_pte = pte;
1184 } else if (level > 1) {
1185 /* Recurse down into a level that isn't *entirely* obsolete */
1186 freelist = dma_pte_clear_level(domain, level - 1,
1187 phys_to_virt(dma_pte_addr(pte)),
1188 level_pfn, start_pfn, last_pfn,
1189 freelist);
1190 }
1191next:
1192 pfn += level_size(level);
1193 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1194
1195 if (first_pte)
1196 domain_flush_cache(domain, first_pte,
1197 (void *)++last_pte - (void *)first_pte);
1198
1199 return freelist;
1200}
1201
1202/* We can't just free the pages because the IOMMU may still be walking
1203 the page tables, and may have cached the intermediate levels. The
1204 pages can only be freed after the IOTLB flush has been done. */
1205struct page *domain_unmap(struct dmar_domain *domain,
1206 unsigned long start_pfn,
1207 unsigned long last_pfn)
1208{
David Woodhouseea8ea462014-03-05 17:09:32 +00001209 struct page *freelist = NULL;
1210
Jiang Liu162d1b12014-07-11 14:19:35 +08001211 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1212 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001213 BUG_ON(start_pfn > last_pfn);
1214
1215 /* we don't need lock here; nobody else touches the iova range */
1216 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1217 domain->pgd, 0, start_pfn, last_pfn, NULL);
1218
1219 /* free pgd */
1220 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1221 struct page *pgd_page = virt_to_page(domain->pgd);
1222 pgd_page->freelist = freelist;
1223 freelist = pgd_page;
1224
1225 domain->pgd = NULL;
1226 }
1227
1228 return freelist;
1229}
1230
1231void dma_free_pagelist(struct page *freelist)
1232{
1233 struct page *pg;
1234
1235 while ((pg = freelist)) {
1236 freelist = pg->freelist;
1237 free_pgtable_page(page_address(pg));
1238 }
1239}
1240
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241/* iommu handling */
1242static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1243{
1244 struct root_entry *root;
1245 unsigned long flags;
1246
Suresh Siddha4c923d42009-10-02 11:01:24 -07001247 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001248 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001249 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001250 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001251 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001252 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001253
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001254 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001255
1256 spin_lock_irqsave(&iommu->lock, flags);
1257 iommu->root_entry = root;
1258 spin_unlock_irqrestore(&iommu->lock, flags);
1259
1260 return 0;
1261}
1262
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001263static void iommu_set_root_entry(struct intel_iommu *iommu)
1264{
David Woodhouse03ecc322015-02-13 14:35:21 +00001265 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001266 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001267 unsigned long flag;
1268
David Woodhouse03ecc322015-02-13 14:35:21 +00001269 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001270 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001271 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001272
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001273 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001274 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001275
David Woodhousec416daa2009-05-10 20:30:58 +01001276 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001277
1278 /* Make sure hardware complete it */
1279 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001280 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001281
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001282 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001283}
1284
1285static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1286{
1287 u32 val;
1288 unsigned long flag;
1289
David Woodhouse9af88142009-02-13 23:18:03 +00001290 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001291 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001293 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001294 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001295
1296 /* Make sure hardware complete it */
1297 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001298 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001299
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001300 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001301}
1302
1303/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001304static void __iommu_flush_context(struct intel_iommu *iommu,
1305 u16 did, u16 source_id, u8 function_mask,
1306 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001307{
1308 u64 val = 0;
1309 unsigned long flag;
1310
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001311 switch (type) {
1312 case DMA_CCMD_GLOBAL_INVL:
1313 val = DMA_CCMD_GLOBAL_INVL;
1314 break;
1315 case DMA_CCMD_DOMAIN_INVL:
1316 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1317 break;
1318 case DMA_CCMD_DEVICE_INVL:
1319 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1320 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1321 break;
1322 default:
1323 BUG();
1324 }
1325 val |= DMA_CCMD_ICC;
1326
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001327 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001328 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1329
1330 /* Make sure hardware complete it */
1331 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1332 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1333
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
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001337/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001338static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1339 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001340{
1341 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1342 u64 val = 0, val_iva = 0;
1343 unsigned long flag;
1344
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001345 switch (type) {
1346 case DMA_TLB_GLOBAL_FLUSH:
1347 /* global flush doesn't need set IVA_REG */
1348 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1349 break;
1350 case DMA_TLB_DSI_FLUSH:
1351 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1352 break;
1353 case DMA_TLB_PSI_FLUSH:
1354 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001355 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001356 val_iva = size_order | addr;
1357 break;
1358 default:
1359 BUG();
1360 }
1361 /* Note: set drain read/write */
1362#if 0
1363 /*
1364 * This is probably to be super secure.. Looks like we can
1365 * ignore it without any impact.
1366 */
1367 if (cap_read_drain(iommu->cap))
1368 val |= DMA_TLB_READ_DRAIN;
1369#endif
1370 if (cap_write_drain(iommu->cap))
1371 val |= DMA_TLB_WRITE_DRAIN;
1372
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001373 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001374 /* Note: Only uses first TLB reg currently */
1375 if (val_iva)
1376 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1377 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1378
1379 /* Make sure hardware complete it */
1380 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1381 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1382
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001383 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001384
1385 /* check IOTLB invalidation granularity */
1386 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001387 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001388 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001389 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001390 (unsigned long long)DMA_TLB_IIRG(type),
1391 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001392}
1393
David Woodhouse64ae8922014-03-09 12:52:30 -07001394static struct device_domain_info *
1395iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1396 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001397{
Quentin Lambert2f119c72015-02-06 10:59:53 +01001398 bool found = false;
Yu Zhao93a23a72009-05-18 13:51:37 +08001399 unsigned long flags;
1400 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001401 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001402
1403 if (!ecap_dev_iotlb_support(iommu->ecap))
1404 return NULL;
1405
1406 if (!iommu->qi)
1407 return NULL;
1408
1409 spin_lock_irqsave(&device_domain_lock, flags);
1410 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001411 if (info->iommu == iommu && info->bus == bus &&
1412 info->devfn == devfn) {
Quentin Lambert2f119c72015-02-06 10:59:53 +01001413 found = true;
Yu Zhao93a23a72009-05-18 13:51:37 +08001414 break;
1415 }
1416 spin_unlock_irqrestore(&device_domain_lock, flags);
1417
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001418 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001419 return NULL;
1420
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001421 pdev = to_pci_dev(info->dev);
1422
1423 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001424 return NULL;
1425
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001426 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001427 return NULL;
1428
Yu Zhao93a23a72009-05-18 13:51:37 +08001429 return info;
1430}
1431
1432static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1433{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001434 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001435 return;
1436
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001437 pci_enable_ats(to_pci_dev(info->dev), VTD_PAGE_SHIFT);
Yu Zhao93a23a72009-05-18 13:51:37 +08001438}
1439
1440static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1441{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001442 if (!info->dev || !dev_is_pci(info->dev) ||
1443 !pci_ats_enabled(to_pci_dev(info->dev)))
Yu Zhao93a23a72009-05-18 13:51:37 +08001444 return;
1445
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001446 pci_disable_ats(to_pci_dev(info->dev));
Yu Zhao93a23a72009-05-18 13:51:37 +08001447}
1448
1449static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1450 u64 addr, unsigned mask)
1451{
1452 u16 sid, qdep;
1453 unsigned long flags;
1454 struct device_domain_info *info;
1455
1456 spin_lock_irqsave(&device_domain_lock, flags);
1457 list_for_each_entry(info, &domain->devices, link) {
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001458 struct pci_dev *pdev;
1459 if (!info->dev || !dev_is_pci(info->dev))
1460 continue;
1461
1462 pdev = to_pci_dev(info->dev);
1463 if (!pci_ats_enabled(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001464 continue;
1465
1466 sid = info->bus << 8 | info->devfn;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001467 qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001468 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1469 }
1470 spin_unlock_irqrestore(&device_domain_lock, flags);
1471}
1472
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001473static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1474 struct dmar_domain *domain,
1475 unsigned long pfn, unsigned int pages,
1476 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001477{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001478 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001479 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001480 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001481
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001482 BUG_ON(pages == 0);
1483
David Woodhouseea8ea462014-03-05 17:09:32 +00001484 if (ih)
1485 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001486 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001487 * Fallback to domain selective flush if no PSI support or the size is
1488 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001489 * PSI requires page size to be 2 ^ x, and the base address is naturally
1490 * aligned to the size
1491 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001492 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1493 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001494 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001495 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001496 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001497 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001498
1499 /*
Nadav Amit82653632010-04-01 13:24:40 +03001500 * In caching mode, changes of pages from non-present to present require
1501 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001502 */
Nadav Amit82653632010-04-01 13:24:40 +03001503 if (!cap_caching_mode(iommu->cap) || !map)
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001504 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1505 addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001506}
1507
mark grossf8bab732008-02-08 04:18:38 -08001508static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1509{
1510 u32 pmen;
1511 unsigned long flags;
1512
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001513 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001514 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1515 pmen &= ~DMA_PMEN_EPM;
1516 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1517
1518 /* wait for the protected region status bit to clear */
1519 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1520 readl, !(pmen & DMA_PMEN_PRS), pmen);
1521
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001522 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001523}
1524
Jiang Liu2a41cce2014-07-11 14:19:33 +08001525static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001526{
1527 u32 sts;
1528 unsigned long flags;
1529
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001530 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001531 iommu->gcmd |= DMA_GCMD_TE;
1532 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001533
1534 /* Make sure hardware complete it */
1535 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001536 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001537
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001538 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001539}
1540
Jiang Liu2a41cce2014-07-11 14:19:33 +08001541static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001542{
1543 u32 sts;
1544 unsigned long flag;
1545
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001546 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001547 iommu->gcmd &= ~DMA_GCMD_TE;
1548 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1549
1550 /* Make sure hardware complete it */
1551 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001552 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001553
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001554 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001555}
1556
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001557
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001558static int iommu_init_domains(struct intel_iommu *iommu)
1559{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001560 u32 ndomains, nlongs;
1561 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001562
1563 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001564 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001565 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001566 nlongs = BITS_TO_LONGS(ndomains);
1567
Donald Dutile94a91b52009-08-20 16:51:34 -04001568 spin_lock_init(&iommu->lock);
1569
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001570 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1571 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001572 pr_err("%s: Allocating domain id array failed\n",
1573 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001574 return -ENOMEM;
1575 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001576
1577 size = ((ndomains >> 8) + 1) * sizeof(struct dmar_domain **);
1578 iommu->domains = kzalloc(size, GFP_KERNEL);
1579
1580 if (iommu->domains) {
1581 size = 256 * sizeof(struct dmar_domain *);
1582 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1583 }
1584
1585 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001586 pr_err("%s: Allocating domain array failed\n",
1587 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001588 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001589 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001590 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001591 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001592 return -ENOMEM;
1593 }
1594
Joerg Roedel8bf47812015-07-21 10:41:21 +02001595
1596
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001597 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001598 * If Caching mode is set, then invalid translations are tagged
1599 * with domain-id 0, hence we need to pre-allocate it. We also
1600 * use domain-id 0 as a marker for non-allocated domain-id, so
1601 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001602 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001603 set_bit(0, iommu->domain_ids);
1604
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001605 return 0;
1606}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001607
Jiang Liuffebeb42014-11-09 22:48:02 +08001608static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001609{
1610 struct dmar_domain *domain;
Jiang Liu2a46ddf2014-07-11 14:19:30 +08001611 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001612
Donald Dutile94a91b52009-08-20 16:51:34 -04001613 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001614 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Jiang Liua4eaa862014-02-19 14:07:30 +08001615 /*
1616 * Domain id 0 is reserved for invalid translation
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001617 * if hardware supports caching mode and used as
1618 * a non-allocated marker.
Jiang Liua4eaa862014-02-19 14:07:30 +08001619 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001620 if (i == 0)
Jiang Liua4eaa862014-02-19 14:07:30 +08001621 continue;
1622
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001623 domain = get_iommu_domain(iommu, i);
Donald Dutile94a91b52009-08-20 16:51:34 -04001624 clear_bit(i, iommu->domain_ids);
Jiang Liu129ad282014-07-11 14:19:31 +08001625 if (domain_detach_iommu(domain, iommu) == 0 &&
1626 !domain_type_is_vm(domain))
Jiang Liu92d03cc2014-02-19 14:07:28 +08001627 domain_exit(domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001628 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001629 }
1630
1631 if (iommu->gcmd & DMA_GCMD_TE)
1632 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001633}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001634
Jiang Liuffebeb42014-11-09 22:48:02 +08001635static void free_dmar_iommu(struct intel_iommu *iommu)
1636{
1637 if ((iommu->domains) && (iommu->domain_ids)) {
Joerg Roedel8bf47812015-07-21 10:41:21 +02001638 int elems = (cap_ndoms(iommu->cap) >> 8) + 1;
1639 int i;
1640
1641 for (i = 0; i < elems; i++)
1642 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001643 kfree(iommu->domains);
1644 kfree(iommu->domain_ids);
1645 iommu->domains = NULL;
1646 iommu->domain_ids = NULL;
1647 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001648
Weidong Hand9630fe2008-12-08 11:06:32 +08001649 g_iommus[iommu->seq_id] = NULL;
1650
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001651 /* free context mapping */
1652 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001653}
1654
Jiang Liuab8dfe22014-07-11 14:19:27 +08001655static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001656{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001657 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001658
1659 domain = alloc_domain_mem();
1660 if (!domain)
1661 return NULL;
1662
Jiang Liuab8dfe22014-07-11 14:19:27 +08001663 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001664 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001665 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001666 spin_lock_init(&domain->iommu_lock);
1667 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001668
1669 return domain;
1670}
1671
Jiang Liufb170fb2014-07-11 14:19:28 +08001672static int __iommu_attach_domain(struct dmar_domain *domain,
1673 struct intel_iommu *iommu)
1674{
1675 int num;
1676 unsigned long ndomains;
1677
Joerg Roedele2411422015-07-21 11:18:21 +02001678 num = domain->iommu_did[iommu->seq_id];
1679 if (num)
1680 return num;
1681
Jiang Liufb170fb2014-07-11 14:19:28 +08001682 ndomains = cap_ndoms(iommu->cap);
Joerg Roedele2411422015-07-21 11:18:21 +02001683 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1684
Jiang Liufb170fb2014-07-11 14:19:28 +08001685 if (num < ndomains) {
1686 set_bit(num, iommu->domain_ids);
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001687 set_iommu_domain(iommu, num, domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001688 domain->iommu_did[iommu->seq_id] = num;
Jiang Liufb170fb2014-07-11 14:19:28 +08001689 } else {
1690 num = -ENOSPC;
1691 }
1692
Joerg Roedele2411422015-07-21 11:18:21 +02001693 if (num < 0)
1694 pr_err("%s: No free domain ids\n", iommu->name);
1695
Jiang Liufb170fb2014-07-11 14:19:28 +08001696 return num;
1697}
1698
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001699static int iommu_attach_domain(struct dmar_domain *domain,
1700 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001701{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001702 int num;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001703 unsigned long flags;
1704
Weidong Han8c11e792008-12-08 15:29:22 +08001705 spin_lock_irqsave(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001706 num = __iommu_attach_domain(domain, iommu);
Jiang Liu44bde612014-07-11 14:19:29 +08001707 spin_unlock_irqrestore(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001708
Jiang Liufb170fb2014-07-11 14:19:28 +08001709 return num;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001710}
1711
1712static void iommu_detach_domain(struct dmar_domain *domain,
1713 struct intel_iommu *iommu)
1714{
1715 unsigned long flags;
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001716 int num;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001717
1718 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001719
1720 num = domain->iommu_did[iommu->seq_id];
1721
1722 if (num == 0)
1723 return;
1724
1725 clear_bit(num, iommu->domain_ids);
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001726 set_iommu_domain(iommu, num, NULL);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001727
Weidong Han8c11e792008-12-08 15:29:22 +08001728 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001729}
1730
Jiang Liufb170fb2014-07-11 14:19:28 +08001731static void domain_attach_iommu(struct dmar_domain *domain,
1732 struct intel_iommu *iommu)
1733{
1734 unsigned long flags;
1735
1736 spin_lock_irqsave(&domain->iommu_lock, flags);
1737 if (!test_and_set_bit(iommu->seq_id, domain->iommu_bmp)) {
1738 domain->iommu_count++;
1739 if (domain->iommu_count == 1)
1740 domain->nid = iommu->node;
1741 domain_update_iommu_cap(domain);
1742 }
1743 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1744}
1745
1746static int domain_detach_iommu(struct dmar_domain *domain,
1747 struct intel_iommu *iommu)
1748{
1749 unsigned long flags;
1750 int count = INT_MAX;
1751
1752 spin_lock_irqsave(&domain->iommu_lock, flags);
1753 if (test_and_clear_bit(iommu->seq_id, domain->iommu_bmp)) {
1754 count = --domain->iommu_count;
1755 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001756 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001757 }
1758 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1759
1760 return count;
1761}
1762
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001763static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001764static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001765
Joseph Cihula51a63e62011-03-21 11:04:24 -07001766static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001767{
1768 struct pci_dev *pdev = NULL;
1769 struct iova *iova;
1770 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001771
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001772 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1773 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001774
Mark Gross8a443df2008-03-04 14:59:31 -08001775 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1776 &reserved_rbtree_key);
1777
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001778 /* IOAPIC ranges shouldn't be accessed by DMA */
1779 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1780 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001781 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001782 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001783 return -ENODEV;
1784 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001785
1786 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1787 for_each_pci_dev(pdev) {
1788 struct resource *r;
1789
1790 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1791 r = &pdev->resource[i];
1792 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1793 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001794 iova = reserve_iova(&reserved_iova_list,
1795 IOVA_PFN(r->start),
1796 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001797 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001798 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001799 return -ENODEV;
1800 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001801 }
1802 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001803 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001804}
1805
1806static void domain_reserve_special_ranges(struct dmar_domain *domain)
1807{
1808 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1809}
1810
1811static inline int guestwidth_to_adjustwidth(int gaw)
1812{
1813 int agaw;
1814 int r = (gaw - 12) % 9;
1815
1816 if (r == 0)
1817 agaw = gaw;
1818 else
1819 agaw = gaw + 9 - r;
1820 if (agaw > 64)
1821 agaw = 64;
1822 return agaw;
1823}
1824
1825static int domain_init(struct dmar_domain *domain, int guest_width)
1826{
1827 struct intel_iommu *iommu;
1828 int adjust_width, agaw;
1829 unsigned long sagaw;
1830
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001831 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1832 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001833 domain_reserve_special_ranges(domain);
1834
1835 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001836 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001837 if (guest_width > cap_mgaw(iommu->cap))
1838 guest_width = cap_mgaw(iommu->cap);
1839 domain->gaw = guest_width;
1840 adjust_width = guestwidth_to_adjustwidth(guest_width);
1841 agaw = width_to_agaw(adjust_width);
1842 sagaw = cap_sagaw(iommu->cap);
1843 if (!test_bit(agaw, &sagaw)) {
1844 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001845 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001846 agaw = find_next_bit(&sagaw, 5, agaw);
1847 if (agaw >= 5)
1848 return -ENODEV;
1849 }
1850 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001851
Weidong Han8e6040972008-12-08 15:49:06 +08001852 if (ecap_coherent(iommu->ecap))
1853 domain->iommu_coherency = 1;
1854 else
1855 domain->iommu_coherency = 0;
1856
Sheng Yang58c610b2009-03-18 15:33:05 +08001857 if (ecap_sc_support(iommu->ecap))
1858 domain->iommu_snooping = 1;
1859 else
1860 domain->iommu_snooping = 0;
1861
David Woodhouse214e39a2014-03-19 10:38:49 +00001862 if (intel_iommu_superpage)
1863 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1864 else
1865 domain->iommu_superpage = 0;
1866
Suresh Siddha4c923d42009-10-02 11:01:24 -07001867 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001868
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001870 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001871 if (!domain->pgd)
1872 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001873 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001874 return 0;
1875}
1876
1877static void domain_exit(struct dmar_domain *domain)
1878{
Alex Williamson46ebb7a2015-07-14 14:48:53 -06001879 struct dmar_drhd_unit *drhd;
1880 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00001881 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001882
1883 /* Domain 0 is reserved, so dont process it */
1884 if (!domain)
1885 return;
1886
Alex Williamson7b668352011-05-24 12:02:41 +01001887 /* Flush any lazy unmaps that may reference this domain */
1888 if (!intel_iommu_strict)
1889 flush_unmaps_timeout(0);
1890
Jiang Liu92d03cc2014-02-19 14:07:28 +08001891 /* remove associated devices */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001892 domain_remove_dev_info(domain);
Jiang Liu92d03cc2014-02-19 14:07:28 +08001893
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001894 /* destroy iovas */
1895 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001896
David Woodhouseea8ea462014-03-05 17:09:32 +00001897 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001898
Jiang Liu92d03cc2014-02-19 14:07:28 +08001899 /* clear attached or cached domains */
Jiang Liu0e242612014-02-19 14:07:34 +08001900 rcu_read_lock();
Alex Williamson46ebb7a2015-07-14 14:48:53 -06001901 for_each_active_iommu(iommu, drhd)
1902 if (domain_type_is_vm(domain) ||
1903 test_bit(iommu->seq_id, domain->iommu_bmp))
1904 iommu_detach_domain(domain, iommu);
Jiang Liu0e242612014-02-19 14:07:34 +08001905 rcu_read_unlock();
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001906
David Woodhouseea8ea462014-03-05 17:09:32 +00001907 dma_free_pagelist(freelist);
1908
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001909 free_domain_mem(domain);
1910}
1911
David Woodhouse64ae8922014-03-09 12:52:30 -07001912static int domain_context_mapping_one(struct dmar_domain *domain,
1913 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001914 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001915{
Joerg Roedel28ccce02015-07-21 14:45:31 +02001916 int translation = CONTEXT_TT_MULTI_LEVEL;
1917 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001918 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001919 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001920 struct dma_pte *pgd;
Weidong Hanea6606b2008-12-08 23:08:15 +08001921 int id;
1922 int agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001923
1924 if (hw_pass_through && domain_type_is_si(domain))
1925 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001926
1927 pr_debug("Set context mapping for %02x:%02x.%d\n",
1928 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001929
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001930 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001931
David Woodhouse03ecc322015-02-13 14:35:21 +00001932 spin_lock_irqsave(&iommu->lock, flags);
1933 context = iommu_context_addr(iommu, bus, devfn, 1);
1934 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001935 if (!context)
1936 return -ENOMEM;
1937 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001938 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001939 spin_unlock_irqrestore(&iommu->lock, flags);
1940 return 0;
1941 }
1942
Weidong Hanea6606b2008-12-08 23:08:15 +08001943 pgd = domain->pgd;
1944
Joerg Roedelde24e552015-07-21 14:53:04 +02001945 id = __iommu_attach_domain(domain, iommu);
1946 if (id < 0) {
1947 spin_unlock_irqrestore(&iommu->lock, flags);
1948 pr_err("%s: No free domain ids\n", iommu->name);
1949 return -EFAULT;
Weidong Hanea6606b2008-12-08 23:08:15 +08001950 }
1951
Joerg Roedelde24e552015-07-21 14:53:04 +02001952 context_clear_entry(context);
Weidong Hanea6606b2008-12-08 23:08:15 +08001953 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001954
Joerg Roedelde24e552015-07-21 14:53:04 +02001955 /*
1956 * Skip top levels of page tables for iommu which has less agaw
1957 * than default. Unnecessary for PT mode.
1958 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001959 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02001960 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1961 pgd = phys_to_virt(dma_pte_addr(pgd));
1962 if (!dma_pte_present(pgd)) {
1963 spin_unlock_irqrestore(&iommu->lock, flags);
1964 return -ENOMEM;
1965 }
1966 }
1967
David Woodhouse64ae8922014-03-09 12:52:30 -07001968 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001969 translation = info ? CONTEXT_TT_DEV_IOTLB :
1970 CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02001971
Yu Zhao93a23a72009-05-18 13:51:37 +08001972 context_set_address_root(context, virt_to_phys(pgd));
1973 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02001974 } else {
1975 /*
1976 * In pass through mode, AW must be programmed to
1977 * indicate the largest AGAW value supported by
1978 * hardware. And ASR is ignored by hardware.
1979 */
1980 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001981 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001982
1983 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001984 context_set_fault_enable(context);
1985 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001986 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001987
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001988 /*
1989 * It's a non-present to present mapping. If hardware doesn't cache
1990 * non-present entry we only need to flush the write-buffer. If the
1991 * _does_ cache non-present entries, then it does so in the special
1992 * domain #0, which we have to flush:
1993 */
1994 if (cap_caching_mode(iommu->cap)) {
1995 iommu->flush.flush_context(iommu, 0,
1996 (((u16)bus) << 8) | devfn,
1997 DMA_CCMD_MASK_NOBIT,
1998 DMA_CCMD_DEVICE_INVL);
Jiang Liu18fd7792014-07-11 14:19:26 +08001999 iommu->flush.flush_iotlb(iommu, id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002000 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002001 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002002 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002003 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002004 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08002005
Jiang Liufb170fb2014-07-11 14:19:28 +08002006 domain_attach_iommu(domain, iommu);
2007
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002008 return 0;
2009}
2010
Alex Williamson579305f2014-07-03 09:51:43 -06002011struct domain_context_mapping_data {
2012 struct dmar_domain *domain;
2013 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002014};
2015
2016static int domain_context_mapping_cb(struct pci_dev *pdev,
2017 u16 alias, void *opaque)
2018{
2019 struct domain_context_mapping_data *data = opaque;
2020
2021 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002022 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002023}
2024
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002025static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002026domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002027{
David Woodhouse64ae8922014-03-09 12:52:30 -07002028 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002029 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002030 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002031
David Woodhousee1f167f2014-03-09 15:24:46 -07002032 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002033 if (!iommu)
2034 return -ENODEV;
2035
Alex Williamson579305f2014-07-03 09:51:43 -06002036 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002037 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002038
2039 data.domain = domain;
2040 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002041
2042 return pci_for_each_dma_alias(to_pci_dev(dev),
2043 &domain_context_mapping_cb, &data);
2044}
2045
2046static int domain_context_mapped_cb(struct pci_dev *pdev,
2047 u16 alias, void *opaque)
2048{
2049 struct intel_iommu *iommu = opaque;
2050
2051 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002052}
2053
David Woodhousee1f167f2014-03-09 15:24:46 -07002054static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002055{
Weidong Han5331fe62008-12-08 23:00:00 +08002056 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002057 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002058
David Woodhousee1f167f2014-03-09 15:24:46 -07002059 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002060 if (!iommu)
2061 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002062
Alex Williamson579305f2014-07-03 09:51:43 -06002063 if (!dev_is_pci(dev))
2064 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002065
Alex Williamson579305f2014-07-03 09:51:43 -06002066 return !pci_for_each_dma_alias(to_pci_dev(dev),
2067 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002068}
2069
Fenghua Yuf5329592009-08-04 15:09:37 -07002070/* Returns a number of VTD pages, but aligned to MM page size */
2071static inline unsigned long aligned_nrpages(unsigned long host_addr,
2072 size_t size)
2073{
2074 host_addr &= ~PAGE_MASK;
2075 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2076}
2077
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002078/* Return largest possible superpage level for a given mapping */
2079static inline int hardware_largepage_caps(struct dmar_domain *domain,
2080 unsigned long iov_pfn,
2081 unsigned long phy_pfn,
2082 unsigned long pages)
2083{
2084 int support, level = 1;
2085 unsigned long pfnmerge;
2086
2087 support = domain->iommu_superpage;
2088
2089 /* To use a large page, the virtual *and* physical addresses
2090 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2091 of them will mean we have to use smaller pages. So just
2092 merge them and check both at once. */
2093 pfnmerge = iov_pfn | phy_pfn;
2094
2095 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2096 pages >>= VTD_STRIDE_SHIFT;
2097 if (!pages)
2098 break;
2099 pfnmerge >>= VTD_STRIDE_SHIFT;
2100 level++;
2101 support--;
2102 }
2103 return level;
2104}
2105
David Woodhouse9051aa02009-06-29 12:30:54 +01002106static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2107 struct scatterlist *sg, unsigned long phys_pfn,
2108 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002109{
2110 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002111 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002112 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002113 unsigned int largepage_lvl = 0;
2114 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002115
Jiang Liu162d1b12014-07-11 14:19:35 +08002116 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002117
2118 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2119 return -EINVAL;
2120
2121 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2122
Jiang Liucc4f14a2014-11-26 09:42:10 +08002123 if (!sg) {
2124 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002125 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2126 }
2127
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002128 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002129 uint64_t tmp;
2130
David Woodhousee1605492009-06-29 11:17:38 +01002131 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002132 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002133 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2134 sg->dma_length = sg->length;
2135 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002136 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002137 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002138
David Woodhousee1605492009-06-29 11:17:38 +01002139 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002140 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2141
David Woodhouse5cf0a762014-03-19 16:07:49 +00002142 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002143 if (!pte)
2144 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002145 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002146 if (largepage_lvl > 1) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002147 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002148 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2149 /*
2150 * Ensure that old small page tables are
2151 * removed to make room for superpage,
2152 * if they exist.
2153 */
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002154 dma_pte_free_pagetable(domain, iov_pfn,
Jiang Liud41a4ad2014-07-11 14:19:34 +08002155 iov_pfn + lvl_pages - 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002156 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002157 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002158 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002159
David Woodhousee1605492009-06-29 11:17:38 +01002160 }
2161 /* We don't need lock here, nobody else
2162 * touches the iova range
2163 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002164 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002165 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002166 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002167 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2168 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002169 if (dumps) {
2170 dumps--;
2171 debug_dma_dump_mappings(NULL);
2172 }
2173 WARN_ON(1);
2174 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002175
2176 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2177
2178 BUG_ON(nr_pages < lvl_pages);
2179 BUG_ON(sg_res < lvl_pages);
2180
2181 nr_pages -= lvl_pages;
2182 iov_pfn += lvl_pages;
2183 phys_pfn += lvl_pages;
2184 pteval += lvl_pages * VTD_PAGE_SIZE;
2185 sg_res -= lvl_pages;
2186
2187 /* If the next PTE would be the first in a new page, then we
2188 need to flush the cache on the entries we've just written.
2189 And then we'll need to recalculate 'pte', so clear it and
2190 let it get set again in the if (!pte) block above.
2191
2192 If we're done (!nr_pages) we need to flush the cache too.
2193
2194 Also if we've been setting superpages, we may need to
2195 recalculate 'pte' and switch back to smaller pages for the
2196 end of the mapping, if the trailing size is not enough to
2197 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002198 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002199 if (!nr_pages || first_pte_in_page(pte) ||
2200 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002201 domain_flush_cache(domain, first_pte,
2202 (void *)pte - (void *)first_pte);
2203 pte = NULL;
2204 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002205
2206 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002207 sg = sg_next(sg);
2208 }
2209 return 0;
2210}
2211
David Woodhouse9051aa02009-06-29 12:30:54 +01002212static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2213 struct scatterlist *sg, unsigned long nr_pages,
2214 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002215{
David Woodhouse9051aa02009-06-29 12:30:54 +01002216 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2217}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002218
David Woodhouse9051aa02009-06-29 12:30:54 +01002219static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2220 unsigned long phys_pfn, unsigned long nr_pages,
2221 int prot)
2222{
2223 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002224}
2225
Weidong Hanc7151a82008-12-08 22:51:37 +08002226static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002227{
Weidong Hanc7151a82008-12-08 22:51:37 +08002228 if (!iommu)
2229 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002230
2231 clear_context_table(iommu, bus, devfn);
2232 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002233 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002234 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002235}
2236
David Woodhouse109b9b02012-05-25 17:43:02 +01002237static inline void unlink_domain_info(struct device_domain_info *info)
2238{
2239 assert_spin_locked(&device_domain_lock);
2240 list_del(&info->link);
2241 list_del(&info->global);
2242 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002243 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002244}
2245
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002246static void domain_remove_dev_info(struct dmar_domain *domain)
2247{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002248 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002249 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002250
2251 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wang3a74ca02014-05-20 20:37:47 +08002252 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhouse109b9b02012-05-25 17:43:02 +01002253 unlink_domain_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002254 spin_unlock_irqrestore(&device_domain_lock, flags);
2255
Yu Zhao93a23a72009-05-18 13:51:37 +08002256 iommu_disable_dev_iotlb(info);
David Woodhouse7c7faa12014-03-09 13:33:06 -07002257 iommu_detach_dev(info->iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002258
Jiang Liuab8dfe22014-07-11 14:19:27 +08002259 if (domain_type_is_vm(domain)) {
David Woodhouse7c7faa12014-03-09 13:33:06 -07002260 iommu_detach_dependent_devices(info->iommu, info->dev);
Jiang Liufb170fb2014-07-11 14:19:28 +08002261 domain_detach_iommu(domain, info->iommu);
Jiang Liu92d03cc2014-02-19 14:07:28 +08002262 }
2263
2264 free_devinfo_mem(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002265 spin_lock_irqsave(&device_domain_lock, flags);
2266 }
2267 spin_unlock_irqrestore(&device_domain_lock, flags);
2268}
2269
2270/*
2271 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002272 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002273 */
David Woodhouse1525a292014-03-06 16:19:30 +00002274static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002275{
2276 struct device_domain_info *info;
2277
2278 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002279 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002280 if (info)
2281 return info->domain;
2282 return NULL;
2283}
2284
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002285static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002286dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2287{
2288 struct device_domain_info *info;
2289
2290 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002291 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002292 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002293 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002294
2295 return NULL;
2296}
2297
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002298static struct dmar_domain *dmar_insert_dev_info(struct intel_iommu *iommu,
David Woodhouse41e80dca2014-03-09 13:55:54 -07002299 int bus, int devfn,
David Woodhouseb718cd32014-03-09 13:11:33 -07002300 struct device *dev,
2301 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002302{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002303 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002304 struct device_domain_info *info;
2305 unsigned long flags;
2306
2307 info = alloc_devinfo_mem();
2308 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002309 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002310
Jiang Liu745f2582014-02-19 14:07:26 +08002311 info->bus = bus;
2312 info->devfn = devfn;
2313 info->dev = dev;
2314 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002315 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002316
2317 spin_lock_irqsave(&device_domain_lock, flags);
2318 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002319 found = find_domain(dev);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002320 else {
2321 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002322 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002323 if (info2)
2324 found = info2->domain;
2325 }
Jiang Liu745f2582014-02-19 14:07:26 +08002326 if (found) {
2327 spin_unlock_irqrestore(&device_domain_lock, flags);
2328 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002329 /* Caller must free the original domain */
2330 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002331 }
2332
David Woodhouseb718cd32014-03-09 13:11:33 -07002333 list_add(&info->link, &domain->devices);
2334 list_add(&info->global, &device_domain_list);
2335 if (dev)
2336 dev->archdata.iommu = info;
2337 spin_unlock_irqrestore(&device_domain_lock, flags);
2338
2339 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002340}
2341
Alex Williamson579305f2014-07-03 09:51:43 -06002342static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2343{
2344 *(u16 *)opaque = alias;
2345 return 0;
2346}
2347
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002348/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002349static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002350{
Alex Williamson579305f2014-07-03 09:51:43 -06002351 struct dmar_domain *domain, *tmp;
2352 struct intel_iommu *iommu;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002353 struct device_domain_info *info;
Alex Williamson579305f2014-07-03 09:51:43 -06002354 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002355 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002356 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002357
David Woodhouse146922e2014-03-09 15:44:17 -07002358 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002359 if (domain)
2360 return domain;
2361
David Woodhouse146922e2014-03-09 15:44:17 -07002362 iommu = device_to_iommu(dev, &bus, &devfn);
2363 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002364 return NULL;
2365
2366 if (dev_is_pci(dev)) {
2367 struct pci_dev *pdev = to_pci_dev(dev);
2368
2369 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2370
2371 spin_lock_irqsave(&device_domain_lock, flags);
2372 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2373 PCI_BUS_NUM(dma_alias),
2374 dma_alias & 0xff);
2375 if (info) {
2376 iommu = info->iommu;
2377 domain = info->domain;
2378 }
2379 spin_unlock_irqrestore(&device_domain_lock, flags);
2380
2381 /* DMA alias already has a domain, uses it */
2382 if (info)
2383 goto found_domain;
2384 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002385
David Woodhouse146922e2014-03-09 15:44:17 -07002386 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002387 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002388 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002389 return NULL;
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002390 if (iommu_attach_domain(domain, iommu) < 0) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07002391 free_domain_mem(domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002392 return NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002393 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002394 domain_attach_iommu(domain, iommu);
Alex Williamson579305f2014-07-03 09:51:43 -06002395 if (domain_init(domain, gaw)) {
2396 domain_exit(domain);
2397 return NULL;
2398 }
2399
2400 /* register PCI DMA alias device */
2401 if (dev_is_pci(dev)) {
2402 tmp = dmar_insert_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2403 dma_alias & 0xff, NULL, domain);
2404
2405 if (!tmp || tmp != domain) {
2406 domain_exit(domain);
2407 domain = tmp;
2408 }
2409
David Woodhouseb718cd32014-03-09 13:11:33 -07002410 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002411 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002412 }
2413
2414found_domain:
Alex Williamson579305f2014-07-03 09:51:43 -06002415 tmp = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
2416
2417 if (!tmp || tmp != domain) {
2418 domain_exit(domain);
2419 domain = tmp;
2420 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002421
2422 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002423}
2424
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002425static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002426#define IDENTMAP_ALL 1
2427#define IDENTMAP_GFX 2
2428#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002429
David Woodhouseb2132032009-06-26 18:50:28 +01002430static int iommu_domain_identity_map(struct dmar_domain *domain,
2431 unsigned long long start,
2432 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002433{
David Woodhousec5395d52009-06-28 16:35:56 +01002434 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2435 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002436
David Woodhousec5395d52009-06-28 16:35:56 +01002437 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2438 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002439 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002440 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002441 }
2442
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002443 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002444 /*
2445 * RMRR range might have overlap with physical memory range,
2446 * clear it first
2447 */
David Woodhousec5395d52009-06-28 16:35:56 +01002448 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002449
David Woodhousec5395d52009-06-28 16:35:56 +01002450 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2451 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002452 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002453}
2454
David Woodhouse0b9d9752014-03-09 15:48:15 -07002455static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002456 unsigned long long start,
2457 unsigned long long end)
2458{
2459 struct dmar_domain *domain;
2460 int ret;
2461
David Woodhouse0b9d9752014-03-09 15:48:15 -07002462 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002463 if (!domain)
2464 return -ENOMEM;
2465
David Woodhouse19943b02009-08-04 16:19:20 +01002466 /* For _hardware_ passthrough, don't bother. But for software
2467 passthrough, we do it anyway -- it may indicate a memory
2468 range which is reserved in E820, so which didn't get set
2469 up to start with in si_domain */
2470 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002471 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2472 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002473 return 0;
2474 }
2475
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002476 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2477 dev_name(dev), start, end);
2478
David Woodhouse5595b522009-12-02 09:21:55 +00002479 if (end < start) {
2480 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2481 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2482 dmi_get_system_info(DMI_BIOS_VENDOR),
2483 dmi_get_system_info(DMI_BIOS_VERSION),
2484 dmi_get_system_info(DMI_PRODUCT_VERSION));
2485 ret = -EIO;
2486 goto error;
2487 }
2488
David Woodhouse2ff729f2009-08-26 14:25:41 +01002489 if (end >> agaw_to_width(domain->agaw)) {
2490 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2491 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2492 agaw_to_width(domain->agaw),
2493 dmi_get_system_info(DMI_BIOS_VENDOR),
2494 dmi_get_system_info(DMI_BIOS_VERSION),
2495 dmi_get_system_info(DMI_PRODUCT_VERSION));
2496 ret = -EIO;
2497 goto error;
2498 }
David Woodhouse19943b02009-08-04 16:19:20 +01002499
David Woodhouseb2132032009-06-26 18:50:28 +01002500 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002501 if (ret)
2502 goto error;
2503
2504 /* context entry init */
Joerg Roedel28ccce02015-07-21 14:45:31 +02002505 ret = domain_context_mapping(domain, dev);
David Woodhouseb2132032009-06-26 18:50:28 +01002506 if (ret)
2507 goto error;
2508
2509 return 0;
2510
2511 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002512 domain_exit(domain);
2513 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002514}
2515
2516static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002517 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002518{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002519 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002520 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002521 return iommu_prepare_identity_map(dev, rmrr->base_address,
2522 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002523}
2524
Suresh Siddhad3f13812011-08-23 17:05:25 -07002525#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002526static inline void iommu_prepare_isa(void)
2527{
2528 struct pci_dev *pdev;
2529 int ret;
2530
2531 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2532 if (!pdev)
2533 return;
2534
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002535 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002536 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002537
2538 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002539 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002540
Yijing Wang9b27e822014-05-20 20:37:52 +08002541 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002542}
2543#else
2544static inline void iommu_prepare_isa(void)
2545{
2546 return;
2547}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002548#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002549
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002550static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002551
Matt Kraai071e1372009-08-23 22:30:22 -07002552static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002553{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002554 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002555
Jiang Liuab8dfe22014-07-11 14:19:27 +08002556 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002557 if (!si_domain)
2558 return -EFAULT;
2559
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002560 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2561 domain_exit(si_domain);
2562 return -EFAULT;
2563 }
2564
Joerg Roedel0dc79712015-07-21 15:40:06 +02002565 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002566
David Woodhouse19943b02009-08-04 16:19:20 +01002567 if (hw)
2568 return 0;
2569
David Woodhousec7ab48d2009-06-26 19:10:36 +01002570 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002571 unsigned long start_pfn, end_pfn;
2572 int i;
2573
2574 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2575 ret = iommu_domain_identity_map(si_domain,
2576 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2577 if (ret)
2578 return ret;
2579 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002580 }
2581
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002582 return 0;
2583}
2584
David Woodhouse9b226622014-03-09 14:03:28 -07002585static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002586{
2587 struct device_domain_info *info;
2588
2589 if (likely(!iommu_identity_mapping))
2590 return 0;
2591
David Woodhouse9b226622014-03-09 14:03:28 -07002592 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002593 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2594 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002595
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002596 return 0;
2597}
2598
Joerg Roedel28ccce02015-07-21 14:45:31 +02002599static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002600{
David Woodhouse0ac72662014-03-09 13:19:22 -07002601 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002602 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002603 u8 bus, devfn;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002604 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002605
David Woodhouse5913c9b2014-03-09 16:27:31 -07002606 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002607 if (!iommu)
2608 return -ENODEV;
2609
David Woodhouse5913c9b2014-03-09 16:27:31 -07002610 ndomain = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002611 if (ndomain != domain)
2612 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002613
Joerg Roedel28ccce02015-07-21 14:45:31 +02002614 ret = domain_context_mapping(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002615 if (ret) {
David Woodhouse5913c9b2014-03-09 16:27:31 -07002616 domain_remove_one_dev_info(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002617 return ret;
2618 }
2619
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002620 return 0;
2621}
2622
David Woodhouse0b9d9752014-03-09 15:48:15 -07002623static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002624{
2625 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002626 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002627 int i;
2628
Jiang Liu0e242612014-02-19 14:07:34 +08002629 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002630 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002631 /*
2632 * Return TRUE if this RMRR contains the device that
2633 * is passed in.
2634 */
2635 for_each_active_dev_scope(rmrr->devices,
2636 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002637 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002638 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002639 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002640 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002641 }
Jiang Liu0e242612014-02-19 14:07:34 +08002642 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002643 return false;
2644}
2645
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002646/*
2647 * There are a couple cases where we need to restrict the functionality of
2648 * devices associated with RMRRs. The first is when evaluating a device for
2649 * identity mapping because problems exist when devices are moved in and out
2650 * of domains and their respective RMRR information is lost. This means that
2651 * a device with associated RMRRs will never be in a "passthrough" domain.
2652 * The second is use of the device through the IOMMU API. This interface
2653 * expects to have full control of the IOVA space for the device. We cannot
2654 * satisfy both the requirement that RMRR access is maintained and have an
2655 * unencumbered IOVA space. We also have no ability to quiesce the device's
2656 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2657 * We therefore prevent devices associated with an RMRR from participating in
2658 * the IOMMU API, which eliminates them from device assignment.
2659 *
2660 * In both cases we assume that PCI USB devices with RMRRs have them largely
2661 * for historical reasons and that the RMRR space is not actively used post
2662 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002663 *
2664 * The same exception is made for graphics devices, with the requirement that
2665 * any use of the RMRR regions will be torn down before assigning the device
2666 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002667 */
2668static bool device_is_rmrr_locked(struct device *dev)
2669{
2670 if (!device_has_rmrr(dev))
2671 return false;
2672
2673 if (dev_is_pci(dev)) {
2674 struct pci_dev *pdev = to_pci_dev(dev);
2675
David Woodhouse18436af2015-03-25 15:05:47 +00002676 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002677 return false;
2678 }
2679
2680 return true;
2681}
2682
David Woodhouse3bdb2592014-03-09 16:03:08 -07002683static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002684{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002685
David Woodhouse3bdb2592014-03-09 16:03:08 -07002686 if (dev_is_pci(dev)) {
2687 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002688
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002689 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002690 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002691
David Woodhouse3bdb2592014-03-09 16:03:08 -07002692 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2693 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002694
David Woodhouse3bdb2592014-03-09 16:03:08 -07002695 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2696 return 1;
2697
2698 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2699 return 0;
2700
2701 /*
2702 * We want to start off with all devices in the 1:1 domain, and
2703 * take them out later if we find they can't access all of memory.
2704 *
2705 * However, we can't do this for PCI devices behind bridges,
2706 * because all PCI devices behind the same bridge will end up
2707 * with the same source-id on their transactions.
2708 *
2709 * Practically speaking, we can't change things around for these
2710 * devices at run-time, because we can't be sure there'll be no
2711 * DMA transactions in flight for any of their siblings.
2712 *
2713 * So PCI devices (unless they're on the root bus) as well as
2714 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2715 * the 1:1 domain, just in _case_ one of their siblings turns out
2716 * not to be able to map all of memory.
2717 */
2718 if (!pci_is_pcie(pdev)) {
2719 if (!pci_is_root_bus(pdev->bus))
2720 return 0;
2721 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2722 return 0;
2723 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2724 return 0;
2725 } else {
2726 if (device_has_rmrr(dev))
2727 return 0;
2728 }
David Woodhouse6941af22009-07-04 18:24:27 +01002729
David Woodhouse3dfc8132009-07-04 19:11:08 +01002730 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002731 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002732 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002733 * take them out of the 1:1 domain later.
2734 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002735 if (!startup) {
2736 /*
2737 * If the device's dma_mask is less than the system's memory
2738 * size then this is not a candidate for identity mapping.
2739 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002740 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002741
David Woodhouse3bdb2592014-03-09 16:03:08 -07002742 if (dev->coherent_dma_mask &&
2743 dev->coherent_dma_mask < dma_mask)
2744 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002745
David Woodhouse3bdb2592014-03-09 16:03:08 -07002746 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002747 }
David Woodhouse6941af22009-07-04 18:24:27 +01002748
2749 return 1;
2750}
2751
David Woodhousecf04eee2014-03-21 16:49:04 +00002752static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2753{
2754 int ret;
2755
2756 if (!iommu_should_identity_map(dev, 1))
2757 return 0;
2758
Joerg Roedel28ccce02015-07-21 14:45:31 +02002759 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002760 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002761 pr_info("%s identity mapping for device %s\n",
2762 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002763 else if (ret == -ENODEV)
2764 /* device not associated with an iommu */
2765 ret = 0;
2766
2767 return ret;
2768}
2769
2770
Matt Kraai071e1372009-08-23 22:30:22 -07002771static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002772{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002773 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002774 struct dmar_drhd_unit *drhd;
2775 struct intel_iommu *iommu;
2776 struct device *dev;
2777 int i;
2778 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002779
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002780 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002781 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2782 if (ret)
2783 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002784 }
2785
David Woodhousecf04eee2014-03-21 16:49:04 +00002786 for_each_active_iommu(iommu, drhd)
2787 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2788 struct acpi_device_physical_node *pn;
2789 struct acpi_device *adev;
2790
2791 if (dev->bus != &acpi_bus_type)
2792 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002793
David Woodhousecf04eee2014-03-21 16:49:04 +00002794 adev= to_acpi_device(dev);
2795 mutex_lock(&adev->physical_node_lock);
2796 list_for_each_entry(pn, &adev->physical_node_list, node) {
2797 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2798 if (ret)
2799 break;
2800 }
2801 mutex_unlock(&adev->physical_node_lock);
2802 if (ret)
2803 return ret;
2804 }
2805
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002806 return 0;
2807}
2808
Jiang Liuffebeb42014-11-09 22:48:02 +08002809static void intel_iommu_init_qi(struct intel_iommu *iommu)
2810{
2811 /*
2812 * Start from the sane iommu hardware state.
2813 * If the queued invalidation is already initialized by us
2814 * (for example, while enabling interrupt-remapping) then
2815 * we got the things already rolling from a sane state.
2816 */
2817 if (!iommu->qi) {
2818 /*
2819 * Clear any previous faults.
2820 */
2821 dmar_fault(-1, iommu);
2822 /*
2823 * Disable queued invalidation if supported and already enabled
2824 * before OS handover.
2825 */
2826 dmar_disable_qi(iommu);
2827 }
2828
2829 if (dmar_enable_qi(iommu)) {
2830 /*
2831 * Queued Invalidate not enabled, use Register Based Invalidate
2832 */
2833 iommu->flush.flush_context = __iommu_flush_context;
2834 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002835 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002836 iommu->name);
2837 } else {
2838 iommu->flush.flush_context = qi_flush_context;
2839 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002840 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002841 }
2842}
2843
Joerg Roedel091d42e2015-06-12 11:56:10 +02002844static int copy_context_table(struct intel_iommu *iommu,
2845 struct root_entry *old_re,
2846 struct context_entry **tbl,
2847 int bus, bool ext)
2848{
2849 struct context_entry *old_ce = NULL, *new_ce = NULL, ce;
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002850 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002851 phys_addr_t old_ce_phys;
2852
2853 tbl_idx = ext ? bus * 2 : bus;
2854
2855 for (devfn = 0; devfn < 256; devfn++) {
2856 /* First calculate the correct index */
2857 idx = (ext ? devfn * 2 : devfn) % 256;
2858
2859 if (idx == 0) {
2860 /* First save what we may have and clean up */
2861 if (new_ce) {
2862 tbl[tbl_idx] = new_ce;
2863 __iommu_flush_cache(iommu, new_ce,
2864 VTD_PAGE_SIZE);
2865 pos = 1;
2866 }
2867
2868 if (old_ce)
2869 iounmap(old_ce);
2870
2871 ret = 0;
2872 if (devfn < 0x80)
2873 old_ce_phys = root_entry_lctp(old_re);
2874 else
2875 old_ce_phys = root_entry_uctp(old_re);
2876
2877 if (!old_ce_phys) {
2878 if (ext && devfn == 0) {
2879 /* No LCTP, try UCTP */
2880 devfn = 0x7f;
2881 continue;
2882 } else {
2883 goto out;
2884 }
2885 }
2886
2887 ret = -ENOMEM;
2888 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2889 if (!old_ce)
2890 goto out;
2891
2892 new_ce = alloc_pgtable_page(iommu->node);
2893 if (!new_ce)
2894 goto out_unmap;
2895
2896 ret = 0;
2897 }
2898
2899 /* Now copy the context entry */
2900 ce = old_ce[idx];
2901
Joerg Roedelcf484d02015-06-12 12:21:46 +02002902 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002903 continue;
2904
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002905 did = context_domain_id(&ce);
2906 if (did >= 0 && did < cap_ndoms(iommu->cap))
2907 set_bit(did, iommu->domain_ids);
2908
Joerg Roedelcf484d02015-06-12 12:21:46 +02002909 /*
2910 * We need a marker for copied context entries. This
2911 * marker needs to work for the old format as well as
2912 * for extended context entries.
2913 *
2914 * Bit 67 of the context entry is used. In the old
2915 * format this bit is available to software, in the
2916 * extended format it is the PGE bit, but PGE is ignored
2917 * by HW if PASIDs are disabled (and thus still
2918 * available).
2919 *
2920 * So disable PASIDs first and then mark the entry
2921 * copied. This means that we don't copy PASID
2922 * translations from the old kernel, but this is fine as
2923 * faults there are not fatal.
2924 */
2925 context_clear_pasid_enable(&ce);
2926 context_set_copied(&ce);
2927
Joerg Roedel091d42e2015-06-12 11:56:10 +02002928 new_ce[idx] = ce;
2929 }
2930
2931 tbl[tbl_idx + pos] = new_ce;
2932
2933 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2934
2935out_unmap:
2936 iounmap(old_ce);
2937
2938out:
2939 return ret;
2940}
2941
2942static int copy_translation_tables(struct intel_iommu *iommu)
2943{
2944 struct context_entry **ctxt_tbls;
2945 struct root_entry *old_rt;
2946 phys_addr_t old_rt_phys;
2947 int ctxt_table_entries;
2948 unsigned long flags;
2949 u64 rtaddr_reg;
2950 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002951 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002952
2953 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2954 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002955 new_ext = !!ecap_ecs(iommu->ecap);
2956
2957 /*
2958 * The RTT bit can only be changed when translation is disabled,
2959 * but disabling translation means to open a window for data
2960 * corruption. So bail out and don't copy anything if we would
2961 * have to change the bit.
2962 */
2963 if (new_ext != ext)
2964 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002965
2966 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2967 if (!old_rt_phys)
2968 return -EINVAL;
2969
2970 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2971 if (!old_rt)
2972 return -ENOMEM;
2973
2974 /* This is too big for the stack - allocate it from slab */
2975 ctxt_table_entries = ext ? 512 : 256;
2976 ret = -ENOMEM;
2977 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
2978 if (!ctxt_tbls)
2979 goto out_unmap;
2980
2981 for (bus = 0; bus < 256; bus++) {
2982 ret = copy_context_table(iommu, &old_rt[bus],
2983 ctxt_tbls, bus, ext);
2984 if (ret) {
2985 pr_err("%s: Failed to copy context table for bus %d\n",
2986 iommu->name, bus);
2987 continue;
2988 }
2989 }
2990
2991 spin_lock_irqsave(&iommu->lock, flags);
2992
2993 /* Context tables are copied, now write them to the root_entry table */
2994 for (bus = 0; bus < 256; bus++) {
2995 int idx = ext ? bus * 2 : bus;
2996 u64 val;
2997
2998 if (ctxt_tbls[idx]) {
2999 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3000 iommu->root_entry[bus].lo = val;
3001 }
3002
3003 if (!ext || !ctxt_tbls[idx + 1])
3004 continue;
3005
3006 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3007 iommu->root_entry[bus].hi = val;
3008 }
3009
3010 spin_unlock_irqrestore(&iommu->lock, flags);
3011
3012 kfree(ctxt_tbls);
3013
3014 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3015
3016 ret = 0;
3017
3018out_unmap:
3019 iounmap(old_rt);
3020
3021 return ret;
3022}
3023
Joseph Cihulab7792602011-05-03 00:08:37 -07003024static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003025{
3026 struct dmar_drhd_unit *drhd;
3027 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003028 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003029 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003030 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003031 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003032
3033 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003034 * for each drhd
3035 * allocate root
3036 * initialize and program root entry to not present
3037 * endfor
3038 */
3039 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003040 /*
3041 * lock not needed as this is only incremented in the single
3042 * threaded kernel __init code path all other access are read
3043 * only
3044 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003045 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003046 g_num_of_iommus++;
3047 continue;
3048 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003049 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003050 }
3051
Jiang Liuffebeb42014-11-09 22:48:02 +08003052 /* Preallocate enough resources for IOMMU hot-addition */
3053 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3054 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3055
Weidong Hand9630fe2008-12-08 11:06:32 +08003056 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3057 GFP_KERNEL);
3058 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003059 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003060 ret = -ENOMEM;
3061 goto error;
3062 }
3063
mark gross80b20dd2008-04-18 13:53:58 -07003064 deferred_flush = kzalloc(g_num_of_iommus *
3065 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3066 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003067 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003068 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003069 }
3070
Jiang Liu7c919772014-01-06 14:18:18 +08003071 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003072 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003073
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003074 intel_iommu_init_qi(iommu);
3075
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003076 ret = iommu_init_domains(iommu);
3077 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003078 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003079
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003080 init_translation_status(iommu);
3081
Joerg Roedel091d42e2015-06-12 11:56:10 +02003082 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3083 iommu_disable_translation(iommu);
3084 clear_translation_pre_enabled(iommu);
3085 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3086 iommu->name);
3087 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003088
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003089 /*
3090 * TBD:
3091 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003092 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003093 */
3094 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003095 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003096 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003097
Joerg Roedel091d42e2015-06-12 11:56:10 +02003098 if (translation_pre_enabled(iommu)) {
3099 pr_info("Translation already enabled - trying to copy translation structures\n");
3100
3101 ret = copy_translation_tables(iommu);
3102 if (ret) {
3103 /*
3104 * We found the IOMMU with translation
3105 * enabled - but failed to copy over the
3106 * old root-entry table. Try to proceed
3107 * by disabling translation now and
3108 * allocating a clean root-entry table.
3109 * This might cause DMAR faults, but
3110 * probably the dump will still succeed.
3111 */
3112 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3113 iommu->name);
3114 iommu_disable_translation(iommu);
3115 clear_translation_pre_enabled(iommu);
3116 } else {
3117 pr_info("Copied translation tables from previous kernel for %s\n",
3118 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003119 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003120 }
3121 }
3122
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003123 iommu_flush_write_buffer(iommu);
3124 iommu_set_root_entry(iommu);
3125 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3126 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3127
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003128 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003129 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003130 }
3131
David Woodhouse19943b02009-08-04 16:19:20 +01003132 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003133 iommu_identity_mapping |= IDENTMAP_ALL;
3134
Suresh Siddhad3f13812011-08-23 17:05:25 -07003135#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003136 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003137#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003138
Joerg Roedel86080cc2015-06-12 12:27:16 +02003139 if (iommu_identity_mapping) {
3140 ret = si_domain_init(hw_pass_through);
3141 if (ret)
3142 goto free_iommu;
3143 }
3144
David Woodhousee0fc7e02009-09-30 09:12:17 -07003145 check_tylersburg_isoch();
3146
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003147 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003148 * If we copied translations from a previous kernel in the kdump
3149 * case, we can not assign the devices to domains now, as that
3150 * would eliminate the old mappings. So skip this part and defer
3151 * the assignment to device driver initialization time.
3152 */
3153 if (copied_tables)
3154 goto domains_done;
3155
3156 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003157 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003158 * identity mappings for rmrr, gfx, and isa and may fall back to static
3159 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003160 */
David Woodhouse19943b02009-08-04 16:19:20 +01003161 if (iommu_identity_mapping) {
3162 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3163 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003164 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003165 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003166 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003167 }
David Woodhouse19943b02009-08-04 16:19:20 +01003168 /*
3169 * For each rmrr
3170 * for each dev attached to rmrr
3171 * do
3172 * locate drhd for dev, alloc domain for dev
3173 * allocate free domain
3174 * allocate page table entries for rmrr
3175 * if context not allocated for bus
3176 * allocate and init context
3177 * set present in root table for this bus
3178 * init context with domain, translation etc
3179 * endfor
3180 * endfor
3181 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003182 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003183 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003184 /* some BIOS lists non-exist devices in DMAR table. */
3185 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003186 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003187 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003188 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003189 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003190 }
3191 }
3192
3193 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003194
Joerg Roedela87f4912015-06-12 12:32:54 +02003195domains_done:
3196
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003197 /*
3198 * for each drhd
3199 * enable fault log
3200 * global invalidate context cache
3201 * global invalidate iotlb
3202 * enable translation
3203 */
Jiang Liu7c919772014-01-06 14:18:18 +08003204 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003205 if (drhd->ignored) {
3206 /*
3207 * we always have to disable PMRs or DMA may fail on
3208 * this device
3209 */
3210 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003211 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003212 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003213 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003214
3215 iommu_flush_write_buffer(iommu);
3216
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003217 ret = dmar_set_interrupt(iommu);
3218 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003219 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003220
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003221 if (!translation_pre_enabled(iommu))
3222 iommu_enable_translation(iommu);
3223
David Woodhouseb94996c2009-09-19 15:28:12 -07003224 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003225 }
3226
3227 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003228
3229free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003230 for_each_active_iommu(iommu, drhd) {
3231 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003232 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003233 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003234 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003235free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003236 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003237error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003238 return ret;
3239}
3240
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003241/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003242static struct iova *intel_alloc_iova(struct device *dev,
3243 struct dmar_domain *domain,
3244 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003245{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003246 struct iova *iova = NULL;
3247
David Woodhouse875764d2009-06-28 21:20:51 +01003248 /* Restrict dma_mask to the width that the iommu can handle */
3249 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
3250
3251 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003252 /*
3253 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003254 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003255 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003256 */
David Woodhouse875764d2009-06-28 21:20:51 +01003257 iova = alloc_iova(&domain->iovad, nrpages,
3258 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3259 if (iova)
3260 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003261 }
David Woodhouse875764d2009-06-28 21:20:51 +01003262 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3263 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003264 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003265 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003266 return NULL;
3267 }
3268
3269 return iova;
3270}
3271
David Woodhoused4b709f2014-03-09 16:07:40 -07003272static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003273{
3274 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003275 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003276
David Woodhoused4b709f2014-03-09 16:07:40 -07003277 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003278 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003279 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003280 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003281 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003282 }
3283
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003284 /* make sure context mapping is ok */
David Woodhoused4b709f2014-03-09 16:07:40 -07003285 if (unlikely(!domain_context_mapped(dev))) {
Joerg Roedel28ccce02015-07-21 14:45:31 +02003286 ret = domain_context_mapping(domain, dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003287 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003288 pr_err("Domain context map for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003289 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003290 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003291 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003292 }
3293
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003294 return domain;
3295}
3296
David Woodhoused4b709f2014-03-09 16:07:40 -07003297static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003298{
3299 struct device_domain_info *info;
3300
3301 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003302 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003303 if (likely(info))
3304 return info->domain;
3305
3306 return __get_valid_domain_for_dev(dev);
3307}
3308
David Woodhouseecb509e2014-03-09 16:29:55 -07003309/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003310static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003311{
3312 int found;
3313
David Woodhouse3d891942014-03-06 15:59:26 +00003314 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003315 return 1;
3316
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003317 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003318 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003319
David Woodhouse9b226622014-03-09 14:03:28 -07003320 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003321 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003322 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003323 return 1;
3324 else {
3325 /*
3326 * 32 bit DMA is removed from si_domain and fall back
3327 * to non-identity mapping.
3328 */
David Woodhousebf9c9ed2014-03-09 16:19:13 -07003329 domain_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003330 pr_info("32bit %s uses non-identity mapping\n",
3331 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003332 return 0;
3333 }
3334 } else {
3335 /*
3336 * In case of a detached 64 bit DMA device from vm, the device
3337 * is put into si_domain for identity mapping.
3338 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003339 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003340 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003341 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003342 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003343 pr_info("64bit %s uses identity mapping\n",
3344 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003345 return 1;
3346 }
3347 }
3348 }
3349
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003350 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003351}
3352
David Woodhouse5040a912014-03-09 16:14:00 -07003353static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003354 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003355{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003356 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003357 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003358 struct iova *iova;
3359 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003360 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003361 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003362 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003363
3364 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003365
David Woodhouse5040a912014-03-09 16:14:00 -07003366 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003367 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003368
David Woodhouse5040a912014-03-09 16:14:00 -07003369 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003370 if (!domain)
3371 return 0;
3372
Weidong Han8c11e792008-12-08 15:29:22 +08003373 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003374 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003375
David Woodhouse5040a912014-03-09 16:14:00 -07003376 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003377 if (!iova)
3378 goto error;
3379
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003380 /*
3381 * Check if DMAR supports zero-length reads on write only
3382 * mappings..
3383 */
3384 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003385 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003386 prot |= DMA_PTE_READ;
3387 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3388 prot |= DMA_PTE_WRITE;
3389 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003390 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003391 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003392 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003393 * is not a big problem
3394 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003395 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003396 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003397 if (ret)
3398 goto error;
3399
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003400 /* it's a non-present to present mapping. Only flush if caching mode */
3401 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003402 iommu_flush_iotlb_psi(iommu, domain,
3403 mm_to_dma_pfn(iova->pfn_lo),
3404 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003405 else
Weidong Han8c11e792008-12-08 15:29:22 +08003406 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003407
David Woodhouse03d6a242009-06-28 15:33:46 +01003408 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3409 start_paddr += paddr & ~PAGE_MASK;
3410 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003411
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003412error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003413 if (iova)
3414 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003415 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003416 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003417 return 0;
3418}
3419
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003420static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3421 unsigned long offset, size_t size,
3422 enum dma_data_direction dir,
3423 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003424{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003425 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003426 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003427}
3428
mark gross5e0d2a62008-03-04 15:22:08 -08003429static void flush_unmaps(void)
3430{
mark gross80b20dd2008-04-18 13:53:58 -07003431 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003432
mark gross5e0d2a62008-03-04 15:22:08 -08003433 timer_on = 0;
3434
3435 /* just flush them all */
3436 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003437 struct intel_iommu *iommu = g_iommus[i];
3438 if (!iommu)
3439 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003440
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003441 if (!deferred_flush[i].next)
3442 continue;
3443
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003444 /* In caching mode, global flushes turn emulation expensive */
3445 if (!cap_caching_mode(iommu->cap))
3446 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003447 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003448 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003449 unsigned long mask;
3450 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003451 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003452
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003453 /* On real hardware multiple invalidations are expensive */
3454 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003455 iommu_flush_iotlb_psi(iommu, domain,
Jiang Liua156ef92014-07-11 14:19:36 +08003456 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003457 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003458 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003459 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003460 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3461 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3462 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003463 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003464 if (deferred_flush[i].freelist[j])
3465 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003466 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003467 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003468 }
3469
mark gross5e0d2a62008-03-04 15:22:08 -08003470 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003471}
3472
3473static void flush_unmaps_timeout(unsigned long data)
3474{
mark gross80b20dd2008-04-18 13:53:58 -07003475 unsigned long flags;
3476
3477 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003478 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003479 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003480}
3481
David Woodhouseea8ea462014-03-05 17:09:32 +00003482static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003483{
3484 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003485 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003486 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003487
3488 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003489 if (list_size == HIGH_WATER_MARK)
3490 flush_unmaps();
3491
Weidong Han8c11e792008-12-08 15:29:22 +08003492 iommu = domain_get_iommu(dom);
3493 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003494
mark gross80b20dd2008-04-18 13:53:58 -07003495 next = deferred_flush[iommu_id].next;
3496 deferred_flush[iommu_id].domain[next] = dom;
3497 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003498 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003499 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003500
3501 if (!timer_on) {
3502 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3503 timer_on = 1;
3504 }
3505 list_size++;
3506 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3507}
3508
Jiang Liud41a4ad2014-07-11 14:19:34 +08003509static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003510{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003511 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003512 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003513 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003514 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003515 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003516
David Woodhouse73676832009-07-04 14:08:36 +01003517 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003518 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003519
David Woodhouse1525a292014-03-06 16:19:30 +00003520 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003521 BUG_ON(!domain);
3522
Weidong Han8c11e792008-12-08 15:29:22 +08003523 iommu = domain_get_iommu(domain);
3524
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003525 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003526 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3527 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003528 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003529
David Woodhoused794dc92009-06-28 00:27:49 +01003530 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3531 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003532
David Woodhoused794dc92009-06-28 00:27:49 +01003533 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003534 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003535
David Woodhouseea8ea462014-03-05 17:09:32 +00003536 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003537
mark gross5e0d2a62008-03-04 15:22:08 -08003538 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003539 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003540 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003541 /* free iova */
3542 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003543 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003544 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003545 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003546 /*
3547 * queue up the release of the unmap to save the 1/6th of the
3548 * cpu used up by the iotlb flush operation...
3549 */
mark gross5e0d2a62008-03-04 15:22:08 -08003550 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003551}
3552
Jiang Liud41a4ad2014-07-11 14:19:34 +08003553static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3554 size_t size, enum dma_data_direction dir,
3555 struct dma_attrs *attrs)
3556{
3557 intel_unmap(dev, dev_addr);
3558}
3559
David Woodhouse5040a912014-03-09 16:14:00 -07003560static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003561 dma_addr_t *dma_handle, gfp_t flags,
3562 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003563{
Akinobu Mita36746432014-06-04 16:06:51 -07003564 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003565 int order;
3566
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003567 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003568 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003569
David Woodhouse5040a912014-03-09 16:14:00 -07003570 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003571 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003572 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3573 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003574 flags |= GFP_DMA;
3575 else
3576 flags |= GFP_DMA32;
3577 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003578
Akinobu Mita36746432014-06-04 16:06:51 -07003579 if (flags & __GFP_WAIT) {
3580 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003581
Akinobu Mita36746432014-06-04 16:06:51 -07003582 page = dma_alloc_from_contiguous(dev, count, order);
3583 if (page && iommu_no_mapping(dev) &&
3584 page_to_phys(page) + size > dev->coherent_dma_mask) {
3585 dma_release_from_contiguous(dev, page, count);
3586 page = NULL;
3587 }
3588 }
3589
3590 if (!page)
3591 page = alloc_pages(flags, order);
3592 if (!page)
3593 return NULL;
3594 memset(page_address(page), 0, size);
3595
3596 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003597 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003598 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003599 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003600 return page_address(page);
3601 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3602 __free_pages(page, order);
3603
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003604 return NULL;
3605}
3606
David Woodhouse5040a912014-03-09 16:14:00 -07003607static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003608 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003609{
3610 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003611 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003612
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003613 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003614 order = get_order(size);
3615
Jiang Liud41a4ad2014-07-11 14:19:34 +08003616 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003617 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3618 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003619}
3620
David Woodhouse5040a912014-03-09 16:14:00 -07003621static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003622 int nelems, enum dma_data_direction dir,
3623 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003624{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003625 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003626}
3627
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003628static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003629 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003630{
3631 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003632 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003633
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003634 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003635 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003636 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003637 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003638 }
3639 return nelems;
3640}
3641
David Woodhouse5040a912014-03-09 16:14:00 -07003642static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003643 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003644{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003645 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003646 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003647 size_t size = 0;
3648 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003649 struct iova *iova = NULL;
3650 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003651 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003652 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003653 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003654
3655 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003656 if (iommu_no_mapping(dev))
3657 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003658
David Woodhouse5040a912014-03-09 16:14:00 -07003659 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003660 if (!domain)
3661 return 0;
3662
Weidong Han8c11e792008-12-08 15:29:22 +08003663 iommu = domain_get_iommu(domain);
3664
David Woodhouseb536d242009-06-28 14:49:31 +01003665 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003666 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003667
David Woodhouse5040a912014-03-09 16:14:00 -07003668 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3669 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003670 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003671 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003672 return 0;
3673 }
3674
3675 /*
3676 * Check if DMAR supports zero-length reads on write only
3677 * mappings..
3678 */
3679 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003680 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003681 prot |= DMA_PTE_READ;
3682 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3683 prot |= DMA_PTE_WRITE;
3684
David Woodhouseb536d242009-06-28 14:49:31 +01003685 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003686
Fenghua Yuf5329592009-08-04 15:09:37 -07003687 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003688 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003689 dma_pte_free_pagetable(domain, start_vpfn,
3690 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003691 __free_iova(&domain->iovad, iova);
3692 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003693 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003694
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003695 /* it's a non-present to present mapping. Only flush if caching mode */
3696 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003697 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003698 else
Weidong Han8c11e792008-12-08 15:29:22 +08003699 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003700
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003701 return nelems;
3702}
3703
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003704static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3705{
3706 return !dma_addr;
3707}
3708
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003709struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003710 .alloc = intel_alloc_coherent,
3711 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003712 .map_sg = intel_map_sg,
3713 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003714 .map_page = intel_map_page,
3715 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003716 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003717};
3718
3719static inline int iommu_domain_cache_init(void)
3720{
3721 int ret = 0;
3722
3723 iommu_domain_cache = kmem_cache_create("iommu_domain",
3724 sizeof(struct dmar_domain),
3725 0,
3726 SLAB_HWCACHE_ALIGN,
3727
3728 NULL);
3729 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003730 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003731 ret = -ENOMEM;
3732 }
3733
3734 return ret;
3735}
3736
3737static inline int iommu_devinfo_cache_init(void)
3738{
3739 int ret = 0;
3740
3741 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3742 sizeof(struct device_domain_info),
3743 0,
3744 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003745 NULL);
3746 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003747 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003748 ret = -ENOMEM;
3749 }
3750
3751 return ret;
3752}
3753
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003754static int __init iommu_init_mempool(void)
3755{
3756 int ret;
3757 ret = iommu_iova_cache_init();
3758 if (ret)
3759 return ret;
3760
3761 ret = iommu_domain_cache_init();
3762 if (ret)
3763 goto domain_error;
3764
3765 ret = iommu_devinfo_cache_init();
3766 if (!ret)
3767 return ret;
3768
3769 kmem_cache_destroy(iommu_domain_cache);
3770domain_error:
Robin Murphy85b45452015-01-12 17:51:14 +00003771 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003772
3773 return -ENOMEM;
3774}
3775
3776static void __init iommu_exit_mempool(void)
3777{
3778 kmem_cache_destroy(iommu_devinfo_cache);
3779 kmem_cache_destroy(iommu_domain_cache);
Robin Murphy85b45452015-01-12 17:51:14 +00003780 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003781}
3782
Dan Williams556ab452010-07-23 15:47:56 -07003783static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3784{
3785 struct dmar_drhd_unit *drhd;
3786 u32 vtbar;
3787 int rc;
3788
3789 /* We know that this device on this chipset has its own IOMMU.
3790 * If we find it under a different IOMMU, then the BIOS is lying
3791 * to us. Hope that the IOMMU for this device is actually
3792 * disabled, and it needs no translation...
3793 */
3794 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3795 if (rc) {
3796 /* "can't" happen */
3797 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3798 return;
3799 }
3800 vtbar &= 0xffff0000;
3801
3802 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3803 drhd = dmar_find_matched_drhd_unit(pdev);
3804 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3805 TAINT_FIRMWARE_WORKAROUND,
3806 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3807 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3808}
3809DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3810
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003811static void __init init_no_remapping_devices(void)
3812{
3813 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003814 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003815 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003816
3817 for_each_drhd_unit(drhd) {
3818 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003819 for_each_active_dev_scope(drhd->devices,
3820 drhd->devices_cnt, i, dev)
3821 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003822 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003823 if (i == drhd->devices_cnt)
3824 drhd->ignored = 1;
3825 }
3826 }
3827
Jiang Liu7c919772014-01-06 14:18:18 +08003828 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003829 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003830 continue;
3831
Jiang Liub683b232014-02-19 14:07:32 +08003832 for_each_active_dev_scope(drhd->devices,
3833 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003834 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003835 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003836 if (i < drhd->devices_cnt)
3837 continue;
3838
David Woodhousec0771df2011-10-14 20:59:46 +01003839 /* This IOMMU has *only* gfx devices. Either bypass it or
3840 set the gfx_mapped flag, as appropriate */
3841 if (dmar_map_gfx) {
3842 intel_iommu_gfx_mapped = 1;
3843 } else {
3844 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003845 for_each_active_dev_scope(drhd->devices,
3846 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003847 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003848 }
3849 }
3850}
3851
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003852#ifdef CONFIG_SUSPEND
3853static int init_iommu_hw(void)
3854{
3855 struct dmar_drhd_unit *drhd;
3856 struct intel_iommu *iommu = NULL;
3857
3858 for_each_active_iommu(iommu, drhd)
3859 if (iommu->qi)
3860 dmar_reenable_qi(iommu);
3861
Joseph Cihulab7792602011-05-03 00:08:37 -07003862 for_each_iommu(iommu, drhd) {
3863 if (drhd->ignored) {
3864 /*
3865 * we always have to disable PMRs or DMA may fail on
3866 * this device
3867 */
3868 if (force_on)
3869 iommu_disable_protect_mem_regions(iommu);
3870 continue;
3871 }
3872
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003873 iommu_flush_write_buffer(iommu);
3874
3875 iommu_set_root_entry(iommu);
3876
3877 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003878 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003879 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3880 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003881 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003882 }
3883
3884 return 0;
3885}
3886
3887static void iommu_flush_all(void)
3888{
3889 struct dmar_drhd_unit *drhd;
3890 struct intel_iommu *iommu;
3891
3892 for_each_active_iommu(iommu, drhd) {
3893 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003894 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003895 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003896 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003897 }
3898}
3899
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003900static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003901{
3902 struct dmar_drhd_unit *drhd;
3903 struct intel_iommu *iommu = NULL;
3904 unsigned long flag;
3905
3906 for_each_active_iommu(iommu, drhd) {
3907 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3908 GFP_ATOMIC);
3909 if (!iommu->iommu_state)
3910 goto nomem;
3911 }
3912
3913 iommu_flush_all();
3914
3915 for_each_active_iommu(iommu, drhd) {
3916 iommu_disable_translation(iommu);
3917
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003918 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003919
3920 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3921 readl(iommu->reg + DMAR_FECTL_REG);
3922 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3923 readl(iommu->reg + DMAR_FEDATA_REG);
3924 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3925 readl(iommu->reg + DMAR_FEADDR_REG);
3926 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3927 readl(iommu->reg + DMAR_FEUADDR_REG);
3928
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003929 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003930 }
3931 return 0;
3932
3933nomem:
3934 for_each_active_iommu(iommu, drhd)
3935 kfree(iommu->iommu_state);
3936
3937 return -ENOMEM;
3938}
3939
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003940static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003941{
3942 struct dmar_drhd_unit *drhd;
3943 struct intel_iommu *iommu = NULL;
3944 unsigned long flag;
3945
3946 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003947 if (force_on)
3948 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3949 else
3950 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003951 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003952 }
3953
3954 for_each_active_iommu(iommu, drhd) {
3955
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003956 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003957
3958 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3959 iommu->reg + DMAR_FECTL_REG);
3960 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3961 iommu->reg + DMAR_FEDATA_REG);
3962 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3963 iommu->reg + DMAR_FEADDR_REG);
3964 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3965 iommu->reg + DMAR_FEUADDR_REG);
3966
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003967 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003968 }
3969
3970 for_each_active_iommu(iommu, drhd)
3971 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003972}
3973
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003974static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003975 .resume = iommu_resume,
3976 .suspend = iommu_suspend,
3977};
3978
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003979static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003980{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003981 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003982}
3983
3984#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003985static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003986#endif /* CONFIG_PM */
3987
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003988
Jiang Liuc2a0b532014-11-09 22:47:56 +08003989int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003990{
3991 struct acpi_dmar_reserved_memory *rmrr;
3992 struct dmar_rmrr_unit *rmrru;
3993
3994 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3995 if (!rmrru)
3996 return -ENOMEM;
3997
3998 rmrru->hdr = header;
3999 rmrr = (struct acpi_dmar_reserved_memory *)header;
4000 rmrru->base_address = rmrr->base_address;
4001 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08004002 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4003 ((void *)rmrr) + rmrr->header.length,
4004 &rmrru->devices_cnt);
4005 if (rmrru->devices_cnt && rmrru->devices == NULL) {
4006 kfree(rmrru);
4007 return -ENOMEM;
4008 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004009
Jiang Liu2e455282014-02-19 14:07:36 +08004010 list_add(&rmrru->list, &dmar_rmrr_units);
4011
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004012 return 0;
4013}
4014
Jiang Liu6b197242014-11-09 22:47:58 +08004015static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4016{
4017 struct dmar_atsr_unit *atsru;
4018 struct acpi_dmar_atsr *tmp;
4019
4020 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4021 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4022 if (atsr->segment != tmp->segment)
4023 continue;
4024 if (atsr->header.length != tmp->header.length)
4025 continue;
4026 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4027 return atsru;
4028 }
4029
4030 return NULL;
4031}
4032
4033int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004034{
4035 struct acpi_dmar_atsr *atsr;
4036 struct dmar_atsr_unit *atsru;
4037
Jiang Liu6b197242014-11-09 22:47:58 +08004038 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4039 return 0;
4040
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004041 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004042 atsru = dmar_find_atsr(atsr);
4043 if (atsru)
4044 return 0;
4045
4046 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004047 if (!atsru)
4048 return -ENOMEM;
4049
Jiang Liu6b197242014-11-09 22:47:58 +08004050 /*
4051 * If memory is allocated from slab by ACPI _DSM method, we need to
4052 * copy the memory content because the memory buffer will be freed
4053 * on return.
4054 */
4055 atsru->hdr = (void *)(atsru + 1);
4056 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004057 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004058 if (!atsru->include_all) {
4059 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4060 (void *)atsr + atsr->header.length,
4061 &atsru->devices_cnt);
4062 if (atsru->devices_cnt && atsru->devices == NULL) {
4063 kfree(atsru);
4064 return -ENOMEM;
4065 }
4066 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004067
Jiang Liu0e242612014-02-19 14:07:34 +08004068 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004069
4070 return 0;
4071}
4072
Jiang Liu9bdc5312014-01-06 14:18:27 +08004073static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4074{
4075 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4076 kfree(atsru);
4077}
4078
Jiang Liu6b197242014-11-09 22:47:58 +08004079int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4080{
4081 struct acpi_dmar_atsr *atsr;
4082 struct dmar_atsr_unit *atsru;
4083
4084 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4085 atsru = dmar_find_atsr(atsr);
4086 if (atsru) {
4087 list_del_rcu(&atsru->list);
4088 synchronize_rcu();
4089 intel_iommu_free_atsr(atsru);
4090 }
4091
4092 return 0;
4093}
4094
4095int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4096{
4097 int i;
4098 struct device *dev;
4099 struct acpi_dmar_atsr *atsr;
4100 struct dmar_atsr_unit *atsru;
4101
4102 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4103 atsru = dmar_find_atsr(atsr);
4104 if (!atsru)
4105 return 0;
4106
4107 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4108 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4109 i, dev)
4110 return -EBUSY;
4111
4112 return 0;
4113}
4114
Jiang Liuffebeb42014-11-09 22:48:02 +08004115static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4116{
4117 int sp, ret = 0;
4118 struct intel_iommu *iommu = dmaru->iommu;
4119
4120 if (g_iommus[iommu->seq_id])
4121 return 0;
4122
4123 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004124 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004125 iommu->name);
4126 return -ENXIO;
4127 }
4128 if (!ecap_sc_support(iommu->ecap) &&
4129 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004130 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004131 iommu->name);
4132 return -ENXIO;
4133 }
4134 sp = domain_update_iommu_superpage(iommu) - 1;
4135 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004136 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004137 iommu->name);
4138 return -ENXIO;
4139 }
4140
4141 /*
4142 * Disable translation if already enabled prior to OS handover.
4143 */
4144 if (iommu->gcmd & DMA_GCMD_TE)
4145 iommu_disable_translation(iommu);
4146
4147 g_iommus[iommu->seq_id] = iommu;
4148 ret = iommu_init_domains(iommu);
4149 if (ret == 0)
4150 ret = iommu_alloc_root_entry(iommu);
4151 if (ret)
4152 goto out;
4153
4154 if (dmaru->ignored) {
4155 /*
4156 * we always have to disable PMRs or DMA may fail on this device
4157 */
4158 if (force_on)
4159 iommu_disable_protect_mem_regions(iommu);
4160 return 0;
4161 }
4162
4163 intel_iommu_init_qi(iommu);
4164 iommu_flush_write_buffer(iommu);
4165 ret = dmar_set_interrupt(iommu);
4166 if (ret)
4167 goto disable_iommu;
4168
4169 iommu_set_root_entry(iommu);
4170 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4171 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4172 iommu_enable_translation(iommu);
4173
Jiang Liuffebeb42014-11-09 22:48:02 +08004174 iommu_disable_protect_mem_regions(iommu);
4175 return 0;
4176
4177disable_iommu:
4178 disable_dmar_iommu(iommu);
4179out:
4180 free_dmar_iommu(iommu);
4181 return ret;
4182}
4183
Jiang Liu6b197242014-11-09 22:47:58 +08004184int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4185{
Jiang Liuffebeb42014-11-09 22:48:02 +08004186 int ret = 0;
4187 struct intel_iommu *iommu = dmaru->iommu;
4188
4189 if (!intel_iommu_enabled)
4190 return 0;
4191 if (iommu == NULL)
4192 return -EINVAL;
4193
4194 if (insert) {
4195 ret = intel_iommu_add(dmaru);
4196 } else {
4197 disable_dmar_iommu(iommu);
4198 free_dmar_iommu(iommu);
4199 }
4200
4201 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004202}
4203
Jiang Liu9bdc5312014-01-06 14:18:27 +08004204static void intel_iommu_free_dmars(void)
4205{
4206 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4207 struct dmar_atsr_unit *atsru, *atsr_n;
4208
4209 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4210 list_del(&rmrru->list);
4211 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4212 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004213 }
4214
Jiang Liu9bdc5312014-01-06 14:18:27 +08004215 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4216 list_del(&atsru->list);
4217 intel_iommu_free_atsr(atsru);
4218 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004219}
4220
4221int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4222{
Jiang Liub683b232014-02-19 14:07:32 +08004223 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004224 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004225 struct pci_dev *bridge = NULL;
4226 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004227 struct acpi_dmar_atsr *atsr;
4228 struct dmar_atsr_unit *atsru;
4229
4230 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004231 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004232 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004233 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004234 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004235 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004236 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004237 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004238 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08004239 if (!bridge)
4240 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004241
Jiang Liu0e242612014-02-19 14:07:34 +08004242 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004243 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4244 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4245 if (atsr->segment != pci_domain_nr(dev->bus))
4246 continue;
4247
Jiang Liub683b232014-02-19 14:07:32 +08004248 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004249 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004250 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004251
4252 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004253 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004254 }
Jiang Liub683b232014-02-19 14:07:32 +08004255 ret = 0;
4256out:
Jiang Liu0e242612014-02-19 14:07:34 +08004257 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004258
Jiang Liub683b232014-02-19 14:07:32 +08004259 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004260}
4261
Jiang Liu59ce0512014-02-19 14:07:35 +08004262int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4263{
4264 int ret = 0;
4265 struct dmar_rmrr_unit *rmrru;
4266 struct dmar_atsr_unit *atsru;
4267 struct acpi_dmar_atsr *atsr;
4268 struct acpi_dmar_reserved_memory *rmrr;
4269
4270 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4271 return 0;
4272
4273 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4274 rmrr = container_of(rmrru->hdr,
4275 struct acpi_dmar_reserved_memory, header);
4276 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4277 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4278 ((void *)rmrr) + rmrr->header.length,
4279 rmrr->segment, rmrru->devices,
4280 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004281 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004282 return ret;
4283 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004284 dmar_remove_dev_scope(info, rmrr->segment,
4285 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004286 }
4287 }
4288
4289 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4290 if (atsru->include_all)
4291 continue;
4292
4293 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4294 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4295 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4296 (void *)atsr + atsr->header.length,
4297 atsr->segment, atsru->devices,
4298 atsru->devices_cnt);
4299 if (ret > 0)
4300 break;
4301 else if(ret < 0)
4302 return ret;
4303 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4304 if (dmar_remove_dev_scope(info, atsr->segment,
4305 atsru->devices, atsru->devices_cnt))
4306 break;
4307 }
4308 }
4309
4310 return 0;
4311}
4312
Fenghua Yu99dcade2009-11-11 07:23:06 -08004313/*
4314 * Here we only respond to action of unbound device from driver.
4315 *
4316 * Added device is not attached to its DMAR domain here yet. That will happen
4317 * when mapping the device to iova.
4318 */
4319static int device_notifier(struct notifier_block *nb,
4320 unsigned long action, void *data)
4321{
4322 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004323 struct dmar_domain *domain;
4324
David Woodhouse3d891942014-03-06 15:59:26 +00004325 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004326 return 0;
4327
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004328 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004329 return 0;
4330
David Woodhouse1525a292014-03-06 16:19:30 +00004331 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004332 if (!domain)
4333 return 0;
4334
Jiang Liu3a5670e2014-02-19 14:07:33 +08004335 down_read(&dmar_global_lock);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004336 domain_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004337 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004338 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08004339 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07004340
Fenghua Yu99dcade2009-11-11 07:23:06 -08004341 return 0;
4342}
4343
4344static struct notifier_block device_nb = {
4345 .notifier_call = device_notifier,
4346};
4347
Jiang Liu75f05562014-02-19 14:07:37 +08004348static int intel_iommu_memory_notifier(struct notifier_block *nb,
4349 unsigned long val, void *v)
4350{
4351 struct memory_notify *mhp = v;
4352 unsigned long long start, end;
4353 unsigned long start_vpfn, last_vpfn;
4354
4355 switch (val) {
4356 case MEM_GOING_ONLINE:
4357 start = mhp->start_pfn << PAGE_SHIFT;
4358 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4359 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004360 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004361 start, end);
4362 return NOTIFY_BAD;
4363 }
4364 break;
4365
4366 case MEM_OFFLINE:
4367 case MEM_CANCEL_ONLINE:
4368 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4369 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4370 while (start_vpfn <= last_vpfn) {
4371 struct iova *iova;
4372 struct dmar_drhd_unit *drhd;
4373 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004374 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004375
4376 iova = find_iova(&si_domain->iovad, start_vpfn);
4377 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004378 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004379 start_vpfn);
4380 break;
4381 }
4382
4383 iova = split_and_remove_iova(&si_domain->iovad, iova,
4384 start_vpfn, last_vpfn);
4385 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004386 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004387 start_vpfn, last_vpfn);
4388 return NOTIFY_BAD;
4389 }
4390
David Woodhouseea8ea462014-03-05 17:09:32 +00004391 freelist = domain_unmap(si_domain, iova->pfn_lo,
4392 iova->pfn_hi);
4393
Jiang Liu75f05562014-02-19 14:07:37 +08004394 rcu_read_lock();
4395 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004396 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004397 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004398 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004399 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004400 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004401
4402 start_vpfn = iova->pfn_hi + 1;
4403 free_iova_mem(iova);
4404 }
4405 break;
4406 }
4407
4408 return NOTIFY_OK;
4409}
4410
4411static struct notifier_block intel_iommu_memory_nb = {
4412 .notifier_call = intel_iommu_memory_notifier,
4413 .priority = 0
4414};
4415
Alex Williamsona5459cf2014-06-12 16:12:31 -06004416
4417static ssize_t intel_iommu_show_version(struct device *dev,
4418 struct device_attribute *attr,
4419 char *buf)
4420{
4421 struct intel_iommu *iommu = dev_get_drvdata(dev);
4422 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4423 return sprintf(buf, "%d:%d\n",
4424 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4425}
4426static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4427
4428static ssize_t intel_iommu_show_address(struct device *dev,
4429 struct device_attribute *attr,
4430 char *buf)
4431{
4432 struct intel_iommu *iommu = dev_get_drvdata(dev);
4433 return sprintf(buf, "%llx\n", iommu->reg_phys);
4434}
4435static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4436
4437static ssize_t intel_iommu_show_cap(struct device *dev,
4438 struct device_attribute *attr,
4439 char *buf)
4440{
4441 struct intel_iommu *iommu = dev_get_drvdata(dev);
4442 return sprintf(buf, "%llx\n", iommu->cap);
4443}
4444static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4445
4446static ssize_t intel_iommu_show_ecap(struct device *dev,
4447 struct device_attribute *attr,
4448 char *buf)
4449{
4450 struct intel_iommu *iommu = dev_get_drvdata(dev);
4451 return sprintf(buf, "%llx\n", iommu->ecap);
4452}
4453static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4454
Alex Williamson2238c082015-07-14 15:24:53 -06004455static ssize_t intel_iommu_show_ndoms(struct device *dev,
4456 struct device_attribute *attr,
4457 char *buf)
4458{
4459 struct intel_iommu *iommu = dev_get_drvdata(dev);
4460 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4461}
4462static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4463
4464static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4465 struct device_attribute *attr,
4466 char *buf)
4467{
4468 struct intel_iommu *iommu = dev_get_drvdata(dev);
4469 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4470 cap_ndoms(iommu->cap)));
4471}
4472static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4473
Alex Williamsona5459cf2014-06-12 16:12:31 -06004474static struct attribute *intel_iommu_attrs[] = {
4475 &dev_attr_version.attr,
4476 &dev_attr_address.attr,
4477 &dev_attr_cap.attr,
4478 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004479 &dev_attr_domains_supported.attr,
4480 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004481 NULL,
4482};
4483
4484static struct attribute_group intel_iommu_group = {
4485 .name = "intel-iommu",
4486 .attrs = intel_iommu_attrs,
4487};
4488
4489const struct attribute_group *intel_iommu_groups[] = {
4490 &intel_iommu_group,
4491 NULL,
4492};
4493
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004494int __init intel_iommu_init(void)
4495{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004496 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004497 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004498 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004499
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004500 /* VT-d is required for a TXT/tboot launch, so enforce that */
4501 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004502
Jiang Liu3a5670e2014-02-19 14:07:33 +08004503 if (iommu_init_mempool()) {
4504 if (force_on)
4505 panic("tboot: Failed to initialize iommu memory\n");
4506 return -ENOMEM;
4507 }
4508
4509 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004510 if (dmar_table_init()) {
4511 if (force_on)
4512 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004513 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004514 }
4515
Suresh Siddhac2c72862011-08-23 17:05:19 -07004516 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004517 if (force_on)
4518 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004519 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004520 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004521
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004522 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004523 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004524
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004525 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004526 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004527
4528 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004529 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004530
Joseph Cihula51a63e62011-03-21 11:04:24 -07004531 if (dmar_init_reserved_ranges()) {
4532 if (force_on)
4533 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004534 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004535 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004536
4537 init_no_remapping_devices();
4538
Joseph Cihulab7792602011-05-03 00:08:37 -07004539 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004540 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004541 if (force_on)
4542 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004543 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004544 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004545 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004546 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004547 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004548
mark gross5e0d2a62008-03-04 15:22:08 -08004549 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004550#ifdef CONFIG_SWIOTLB
4551 swiotlb = 0;
4552#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004553 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004554
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004555 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004556
Alex Williamsona5459cf2014-06-12 16:12:31 -06004557 for_each_active_iommu(iommu, drhd)
4558 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4559 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004560 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004561
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004562 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004563 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004564 if (si_domain && !hw_pass_through)
4565 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004566
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004567 intel_iommu_enabled = 1;
4568
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004569 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004570
4571out_free_reserved_range:
4572 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004573out_free_dmar:
4574 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004575 up_write(&dmar_global_lock);
4576 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004577 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004578}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004579
Alex Williamson579305f2014-07-03 09:51:43 -06004580static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque)
4581{
4582 struct intel_iommu *iommu = opaque;
4583
4584 iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff);
4585 return 0;
4586}
4587
4588/*
4589 * NB - intel-iommu lacks any sort of reference counting for the users of
4590 * dependent devices. If multiple endpoints have intersecting dependent
4591 * devices, unbinding the driver from any one of them will possibly leave
4592 * the others unable to operate.
4593 */
Han, Weidong3199aa62009-02-26 17:31:12 +08004594static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004595 struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004596{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004597 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004598 return;
4599
Alex Williamson579305f2014-07-03 09:51:43 -06004600 pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004601}
4602
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004603static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004604 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004605{
Yijing Wangbca2b912013-10-31 17:26:04 +08004606 struct device_domain_info *info, *tmp;
Weidong Hanc7151a82008-12-08 22:51:37 +08004607 struct intel_iommu *iommu;
4608 unsigned long flags;
Quentin Lambert2f119c72015-02-06 10:59:53 +01004609 bool found = false;
David Woodhouse156baca2014-03-09 14:00:57 -07004610 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004611
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004612 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004613 if (!iommu)
4614 return;
4615
4616 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wangbca2b912013-10-31 17:26:04 +08004617 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004618 if (info->iommu == iommu && info->bus == bus &&
4619 info->devfn == devfn) {
David Woodhouse109b9b02012-05-25 17:43:02 +01004620 unlink_domain_info(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004621 spin_unlock_irqrestore(&device_domain_lock, flags);
4622
Yu Zhao93a23a72009-05-18 13:51:37 +08004623 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004624 iommu_detach_dev(iommu, info->bus, info->devfn);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004625 iommu_detach_dependent_devices(iommu, dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08004626 free_devinfo_mem(info);
4627
4628 spin_lock_irqsave(&device_domain_lock, flags);
4629
4630 if (found)
4631 break;
4632 else
4633 continue;
4634 }
4635
4636 /* if there is no other devices under the same iommu
4637 * owned by this domain, clear this iommu in iommu_bmp
4638 * update iommu count and coherency
4639 */
David Woodhouse8bbc4412014-03-09 13:52:37 -07004640 if (info->iommu == iommu)
Quentin Lambert2f119c72015-02-06 10:59:53 +01004641 found = true;
Weidong Hanc7151a82008-12-08 22:51:37 +08004642 }
4643
Roland Dreier3e7abe22011-07-20 06:22:21 -07004644 spin_unlock_irqrestore(&device_domain_lock, flags);
4645
Weidong Hanc7151a82008-12-08 22:51:37 +08004646 if (found == 0) {
Jiang Liufb170fb2014-07-11 14:19:28 +08004647 domain_detach_iommu(domain, iommu);
4648 if (!domain_type_is_vm_or_si(domain))
4649 iommu_detach_domain(domain, iommu);
Weidong Hanc7151a82008-12-08 22:51:37 +08004650 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004651}
4652
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004653static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004654{
4655 int adjust_width;
4656
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004657 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4658 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004659 domain_reserve_special_ranges(domain);
4660
4661 /* calculate AGAW */
4662 domain->gaw = guest_width;
4663 adjust_width = guestwidth_to_adjustwidth(guest_width);
4664 domain->agaw = width_to_agaw(adjust_width);
4665
Weidong Han5e98c4b2008-12-08 23:03:27 +08004666 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004667 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004668 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004669 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004670
4671 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004672 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004673 if (!domain->pgd)
4674 return -ENOMEM;
4675 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4676 return 0;
4677}
4678
Joerg Roedel00a77de2015-03-26 13:43:08 +01004679static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004680{
Joerg Roedel5d450802008-12-03 14:52:32 +01004681 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004682 struct iommu_domain *domain;
4683
4684 if (type != IOMMU_DOMAIN_UNMANAGED)
4685 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004686
Jiang Liuab8dfe22014-07-11 14:19:27 +08004687 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004688 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004689 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004690 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004691 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004692 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004693 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004694 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004695 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004696 }
Allen Kay8140a952011-10-14 12:32:17 -07004697 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004698
Joerg Roedel00a77de2015-03-26 13:43:08 +01004699 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004700 domain->geometry.aperture_start = 0;
4701 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4702 domain->geometry.force_aperture = true;
4703
Joerg Roedel00a77de2015-03-26 13:43:08 +01004704 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004705}
Kay, Allen M38717942008-09-09 18:37:29 +03004706
Joerg Roedel00a77de2015-03-26 13:43:08 +01004707static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004708{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004709 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004710}
Kay, Allen M38717942008-09-09 18:37:29 +03004711
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004712static int intel_iommu_attach_device(struct iommu_domain *domain,
4713 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004714{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004715 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004716 struct intel_iommu *iommu;
4717 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004718 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004719
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004720 if (device_is_rmrr_locked(dev)) {
4721 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4722 return -EPERM;
4723 }
4724
David Woodhouse7207d8f2014-03-09 16:31:06 -07004725 /* normally dev is not mapped */
4726 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004727 struct dmar_domain *old_domain;
4728
David Woodhouse1525a292014-03-06 16:19:30 +00004729 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004730 if (old_domain) {
Jiang Liuab8dfe22014-07-11 14:19:27 +08004731 if (domain_type_is_vm_or_si(dmar_domain))
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004732 domain_remove_one_dev_info(old_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004733 else
4734 domain_remove_dev_info(old_domain);
Joerg Roedel62c22162014-12-09 12:56:45 +01004735
4736 if (!domain_type_is_vm_or_si(old_domain) &&
4737 list_empty(&old_domain->devices))
4738 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004739 }
4740 }
4741
David Woodhouse156baca2014-03-09 14:00:57 -07004742 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004743 if (!iommu)
4744 return -ENODEV;
4745
4746 /* check if this iommu agaw is sufficient for max mapped address */
4747 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004748 if (addr_width > cap_mgaw(iommu->cap))
4749 addr_width = cap_mgaw(iommu->cap);
4750
4751 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004752 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004753 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004754 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004755 return -EFAULT;
4756 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004757 dmar_domain->gaw = addr_width;
4758
4759 /*
4760 * Knock out extra levels of page tables if necessary
4761 */
4762 while (iommu->agaw < dmar_domain->agaw) {
4763 struct dma_pte *pte;
4764
4765 pte = dmar_domain->pgd;
4766 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004767 dmar_domain->pgd = (struct dma_pte *)
4768 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004769 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004770 }
4771 dmar_domain->agaw--;
4772 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004773
Joerg Roedel28ccce02015-07-21 14:45:31 +02004774 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004775}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004776
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004777static void intel_iommu_detach_device(struct iommu_domain *domain,
4778 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004779{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004780 domain_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004781}
Kay, Allen M38717942008-09-09 18:37:29 +03004782
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004783static int intel_iommu_map(struct iommu_domain *domain,
4784 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004785 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004786{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004787 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004788 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004789 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004790 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004791
Joerg Roedeldde57a22008-12-03 15:04:09 +01004792 if (iommu_prot & IOMMU_READ)
4793 prot |= DMA_PTE_READ;
4794 if (iommu_prot & IOMMU_WRITE)
4795 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08004796 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4797 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004798
David Woodhouse163cc522009-06-28 00:51:17 +01004799 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004800 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004801 u64 end;
4802
4803 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004804 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004805 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004806 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004807 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004808 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004809 return -EFAULT;
4810 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004811 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004812 }
David Woodhousead051222009-06-28 14:22:28 +01004813 /* Round up size to next multiple of PAGE_SIZE, if it and
4814 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004815 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004816 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4817 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004818 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004819}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004820
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004821static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004822 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004823{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004824 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004825 struct page *freelist = NULL;
4826 struct intel_iommu *iommu;
4827 unsigned long start_pfn, last_pfn;
4828 unsigned int npages;
4829 int iommu_id, num, ndomains, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004830
David Woodhouse5cf0a762014-03-19 16:07:49 +00004831 /* Cope with horrid API which requires us to unmap more than the
4832 size argument if it happens to be a large-page mapping. */
4833 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4834 BUG();
4835
4836 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4837 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4838
David Woodhouseea8ea462014-03-05 17:09:32 +00004839 start_pfn = iova >> VTD_PAGE_SHIFT;
4840 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4841
4842 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4843
4844 npages = last_pfn - start_pfn + 1;
4845
4846 for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004847 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00004848
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004849 /*
4850 * find bit position of dmar_domain
4851 */
4852 ndomains = cap_ndoms(iommu->cap);
4853 for_each_set_bit(num, iommu->domain_ids, ndomains) {
4854 if (get_iommu_domain(iommu, num) == dmar_domain)
4855 iommu_flush_iotlb_psi(iommu, dmar_domain,
4856 start_pfn, npages,
4857 !freelist, 0);
4858 }
David Woodhouseea8ea462014-03-05 17:09:32 +00004859
4860 }
4861
4862 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004863
David Woodhouse163cc522009-06-28 00:51:17 +01004864 if (dmar_domain->max_addr == iova + size)
4865 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004866
David Woodhouse5cf0a762014-03-19 16:07:49 +00004867 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004868}
Kay, Allen M38717942008-09-09 18:37:29 +03004869
Joerg Roedeld14d6572008-12-03 15:06:57 +01004870static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05304871 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004872{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004873 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004874 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004875 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004876 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004877
David Woodhouse5cf0a762014-03-19 16:07:49 +00004878 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004879 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004880 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004881
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004882 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004883}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004884
Joerg Roedel5d587b82014-09-05 10:50:45 +02004885static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004886{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004887 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004888 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004889 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004890 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004891
Joerg Roedel5d587b82014-09-05 10:50:45 +02004892 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004893}
4894
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004895static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004896{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004897 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004898 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004899 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004900
Alex Williamsona5459cf2014-06-12 16:12:31 -06004901 iommu = device_to_iommu(dev, &bus, &devfn);
4902 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004903 return -ENODEV;
4904
Alex Williamsona5459cf2014-06-12 16:12:31 -06004905 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004906
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004907 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004908
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004909 if (IS_ERR(group))
4910 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004911
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004912 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004913 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004914}
4915
4916static void intel_iommu_remove_device(struct device *dev)
4917{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004918 struct intel_iommu *iommu;
4919 u8 bus, devfn;
4920
4921 iommu = device_to_iommu(dev, &bus, &devfn);
4922 if (!iommu)
4923 return;
4924
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004925 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004926
4927 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004928}
4929
Thierry Redingb22f6432014-06-27 09:03:12 +02004930static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004931 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004932 .domain_alloc = intel_iommu_domain_alloc,
4933 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004934 .attach_dev = intel_iommu_attach_device,
4935 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004936 .map = intel_iommu_map,
4937 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004938 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004939 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004940 .add_device = intel_iommu_add_device,
4941 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004942 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004943};
David Woodhouse9af88142009-02-13 23:18:03 +00004944
Daniel Vetter94526182013-01-20 23:50:13 +01004945static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4946{
4947 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004948 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004949 dmar_map_gfx = 0;
4950}
4951
4952DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4953DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4954DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4955DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4956DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4957DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4958DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4959
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004960static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004961{
4962 /*
4963 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004964 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004965 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004966 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00004967 rwbf_quirk = 1;
4968}
4969
4970DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004971DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4972DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4973DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4974DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4975DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4976DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004977
Adam Jacksoneecfd572010-08-25 21:17:34 +01004978#define GGC 0x52
4979#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4980#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4981#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4982#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4983#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4984#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4985#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4986#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4987
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004988static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004989{
4990 unsigned short ggc;
4991
Adam Jacksoneecfd572010-08-25 21:17:34 +01004992 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004993 return;
4994
Adam Jacksoneecfd572010-08-25 21:17:34 +01004995 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004996 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01004997 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004998 } else if (dmar_map_gfx) {
4999 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005000 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005001 intel_iommu_strict = 1;
5002 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005003}
5004DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5005DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5006DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5007DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5008
David Woodhousee0fc7e02009-09-30 09:12:17 -07005009/* On Tylersburg chipsets, some BIOSes have been known to enable the
5010 ISOCH DMAR unit for the Azalia sound device, but not give it any
5011 TLB entries, which causes it to deadlock. Check for that. We do
5012 this in a function called from init_dmars(), instead of in a PCI
5013 quirk, because we don't want to print the obnoxious "BIOS broken"
5014 message if VT-d is actually disabled.
5015*/
5016static void __init check_tylersburg_isoch(void)
5017{
5018 struct pci_dev *pdev;
5019 uint32_t vtisochctrl;
5020
5021 /* If there's no Azalia in the system anyway, forget it. */
5022 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5023 if (!pdev)
5024 return;
5025 pci_dev_put(pdev);
5026
5027 /* System Management Registers. Might be hidden, in which case
5028 we can't do the sanity check. But that's OK, because the
5029 known-broken BIOSes _don't_ actually hide it, so far. */
5030 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5031 if (!pdev)
5032 return;
5033
5034 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5035 pci_dev_put(pdev);
5036 return;
5037 }
5038
5039 pci_dev_put(pdev);
5040
5041 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5042 if (vtisochctrl & 1)
5043 return;
5044
5045 /* Drop all bits other than the number of TLB entries */
5046 vtisochctrl &= 0x1c;
5047
5048 /* If we have the recommended number of TLB entries (16), fine. */
5049 if (vtisochctrl == 0x10)
5050 return;
5051
5052 /* Zero TLB entries? You get to ride the short bus to school. */
5053 if (!vtisochctrl) {
5054 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5055 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5056 dmi_get_system_info(DMI_BIOS_VENDOR),
5057 dmi_get_system_info(DMI_BIOS_VERSION),
5058 dmi_get_system_info(DMI_PRODUCT_VERSION));
5059 iommu_identity_mapping |= IDENTMAP_AZALIA;
5060 return;
5061 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005062
5063 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005064 vtisochctrl);
5065}