blob: 380b4e2f76cdd70c2d72c3fb966dc906d9056168 [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 {
377 int id; /* domain id */
Suresh Siddha4c923d42009-10-02 11:01:24 -0700378 int nid; /* node id */
Jiang Liu78d8e702014-11-09 22:47:57 +0800379 DECLARE_BITMAP(iommu_bmp, DMAR_UNITS_SUPPORTED);
Mike Travis1b198bb2012-03-05 15:05:16 -0800380 /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000381
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +0200382 u16 iommu_did[DMAR_UNITS_SUPPORTED];
383 /* Domain ids per IOMMU. Use u16 since
384 * domain ids are 16 bit wide according
385 * to VT-d spec, section 9.3 */
386
Joerg Roedel00a77de2015-03-26 13:43:08 +0100387 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000388 struct iova_domain iovad; /* iova's that belong to this domain */
389
390 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000391 int gaw; /* max guest address width */
392
393 /* adjusted guest address width, 0 is level 2 30-bit */
394 int agaw;
395
Weidong Han3b5410e2008-12-08 09:17:15 +0800396 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800397
398 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800399 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800400 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100401 int iommu_superpage;/* Level of superpages supported:
402 0 == 4KiB (no superpages), 1 == 2MiB,
403 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanc7151a82008-12-08 22:51:37 +0800404 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800405 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100406
407 struct iommu_domain domain; /* generic domain data structure for
408 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000409};
410
Mark McLoughlina647dac2008-11-20 15:49:48 +0000411/* PCI domain-device relationship */
412struct device_domain_info {
413 struct list_head link; /* link to domain siblings */
414 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100415 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000416 u8 devfn; /* PCI devfn number */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000417 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800418 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000419 struct dmar_domain *domain; /* pointer to domain */
420};
421
Jiang Liub94e4112014-02-19 14:07:25 +0800422struct dmar_rmrr_unit {
423 struct list_head list; /* list of rmrr units */
424 struct acpi_dmar_header *hdr; /* ACPI header */
425 u64 base_address; /* reserved base address*/
426 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000427 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800428 int devices_cnt; /* target device count */
429};
430
431struct dmar_atsr_unit {
432 struct list_head list; /* list of ATSR units */
433 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000434 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800435 int devices_cnt; /* target device count */
436 u8 include_all:1; /* include all ports */
437};
438
439static LIST_HEAD(dmar_atsr_units);
440static LIST_HEAD(dmar_rmrr_units);
441
442#define for_each_rmrr_units(rmrr) \
443 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
444
mark gross5e0d2a62008-03-04 15:22:08 -0800445static void flush_unmaps_timeout(unsigned long data);
446
Jiang Liub707cb02014-01-06 14:18:26 +0800447static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800448
mark gross80b20dd2008-04-18 13:53:58 -0700449#define HIGH_WATER_MARK 250
450struct deferred_flush_tables {
451 int next;
452 struct iova *iova[HIGH_WATER_MARK];
453 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000454 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700455};
456
457static struct deferred_flush_tables *deferred_flush;
458
mark gross5e0d2a62008-03-04 15:22:08 -0800459/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800460static int g_num_of_iommus;
461
462static DEFINE_SPINLOCK(async_umap_flush_lock);
463static LIST_HEAD(unmaps_to_do);
464
465static int timer_on;
466static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800467
Jiang Liu92d03cc2014-02-19 14:07:28 +0800468static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700469static void domain_remove_dev_info(struct dmar_domain *domain);
Jiang Liub94e4112014-02-19 14:07:25 +0800470static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -0700471 struct device *dev);
Jiang Liu92d03cc2014-02-19 14:07:28 +0800472static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000473 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800474static int domain_detach_iommu(struct dmar_domain *domain,
475 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700476
Suresh Siddhad3f13812011-08-23 17:05:25 -0700477#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800478int dmar_disabled = 0;
479#else
480int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700481#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800482
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200483int intel_iommu_enabled = 0;
484EXPORT_SYMBOL_GPL(intel_iommu_enabled);
485
David Woodhouse2d9e6672010-06-15 10:57:57 +0100486static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700487static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800488static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100489static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100490static int intel_iommu_ecs = 1;
491
492/* We only actually use ECS when PASID support (on the new bit 40)
493 * is also advertised. Some early implementations — the ones with
494 * PASID support on bit 28 — have issues even when we *only* use
495 * extended root/context tables. */
496#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
497 ecap_pasid(iommu->ecap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700498
David Woodhousec0771df2011-10-14 20:59:46 +0100499int intel_iommu_gfx_mapped;
500EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
501
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700502#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
503static DEFINE_SPINLOCK(device_domain_lock);
504static LIST_HEAD(device_domain_list);
505
Thierry Redingb22f6432014-06-27 09:03:12 +0200506static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100507
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200508static bool translation_pre_enabled(struct intel_iommu *iommu)
509{
510 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
511}
512
Joerg Roedel091d42e2015-06-12 11:56:10 +0200513static void clear_translation_pre_enabled(struct intel_iommu *iommu)
514{
515 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
516}
517
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200518static void init_translation_status(struct intel_iommu *iommu)
519{
520 u32 gsts;
521
522 gsts = readl(iommu->reg + DMAR_GSTS_REG);
523 if (gsts & DMA_GSTS_TES)
524 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
525}
526
Joerg Roedel00a77de2015-03-26 13:43:08 +0100527/* Convert generic 'struct iommu_domain to private struct dmar_domain */
528static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
529{
530 return container_of(dom, struct dmar_domain, domain);
531}
532
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700533static int __init intel_iommu_setup(char *str)
534{
535 if (!str)
536 return -EINVAL;
537 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800538 if (!strncmp(str, "on", 2)) {
539 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200540 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800541 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700542 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200543 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700544 } else if (!strncmp(str, "igfx_off", 8)) {
545 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200546 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700547 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200548 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700549 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800550 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200551 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800552 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100553 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200554 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100555 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100556 } else if (!strncmp(str, "ecs_off", 7)) {
557 printk(KERN_INFO
558 "Intel-IOMMU: disable extended context table support\n");
559 intel_iommu_ecs = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700560 }
561
562 str += strcspn(str, ",");
563 while (*str == ',')
564 str++;
565 }
566 return 0;
567}
568__setup("intel_iommu=", intel_iommu_setup);
569
570static struct kmem_cache *iommu_domain_cache;
571static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700572
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200573static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
574{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200575 struct dmar_domain **domains;
576 int idx = did >> 8;
577
578 domains = iommu->domains[idx];
579 if (!domains)
580 return NULL;
581
582 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200583}
584
585static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
586 struct dmar_domain *domain)
587{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200588 struct dmar_domain **domains;
589 int idx = did >> 8;
590
591 if (!iommu->domains[idx]) {
592 size_t size = 256 * sizeof(struct dmar_domain *);
593 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
594 }
595
596 domains = iommu->domains[idx];
597 if (WARN_ON(!domains))
598 return;
599 else
600 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200601}
602
Suresh Siddha4c923d42009-10-02 11:01:24 -0700603static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700604{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700605 struct page *page;
606 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700607
Suresh Siddha4c923d42009-10-02 11:01:24 -0700608 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
609 if (page)
610 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700611 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700612}
613
614static inline void free_pgtable_page(void *vaddr)
615{
616 free_page((unsigned long)vaddr);
617}
618
619static inline void *alloc_domain_mem(void)
620{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900621 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700622}
623
Kay, Allen M38717942008-09-09 18:37:29 +0300624static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700625{
626 kmem_cache_free(iommu_domain_cache, vaddr);
627}
628
629static inline void * alloc_devinfo_mem(void)
630{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900631 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700632}
633
634static inline void free_devinfo_mem(void *vaddr)
635{
636 kmem_cache_free(iommu_devinfo_cache, vaddr);
637}
638
Jiang Liuab8dfe22014-07-11 14:19:27 +0800639static inline int domain_type_is_vm(struct dmar_domain *domain)
640{
641 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
642}
643
Joerg Roedel28ccce02015-07-21 14:45:31 +0200644static inline int domain_type_is_si(struct dmar_domain *domain)
645{
646 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
647}
648
Jiang Liuab8dfe22014-07-11 14:19:27 +0800649static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
650{
651 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
652 DOMAIN_FLAG_STATIC_IDENTITY);
653}
Weidong Han1b573682008-12-08 15:34:06 +0800654
Jiang Liu162d1b12014-07-11 14:19:35 +0800655static inline int domain_pfn_supported(struct dmar_domain *domain,
656 unsigned long pfn)
657{
658 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
659
660 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
661}
662
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700663static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800664{
665 unsigned long sagaw;
666 int agaw = -1;
667
668 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700669 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800670 agaw >= 0; agaw--) {
671 if (test_bit(agaw, &sagaw))
672 break;
673 }
674
675 return agaw;
676}
677
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700678/*
679 * Calculate max SAGAW for each iommu.
680 */
681int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
682{
683 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
684}
685
686/*
687 * calculate agaw for each iommu.
688 * "SAGAW" may be different across iommus, use a default agaw, and
689 * get a supported less agaw for iommus that don't support the default agaw.
690 */
691int iommu_calculate_agaw(struct intel_iommu *iommu)
692{
693 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
694}
695
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700696/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800697static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
698{
699 int iommu_id;
700
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700701 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800702 BUG_ON(domain_type_is_vm_or_si(domain));
Mike Travis1b198bb2012-03-05 15:05:16 -0800703 iommu_id = find_first_bit(domain->iommu_bmp, g_num_of_iommus);
Weidong Han8c11e792008-12-08 15:29:22 +0800704 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
705 return NULL;
706
707 return g_iommus[iommu_id];
708}
709
Weidong Han8e6040972008-12-08 15:49:06 +0800710static void domain_update_iommu_coherency(struct dmar_domain *domain)
711{
David Woodhoused0501962014-03-11 17:10:29 -0700712 struct dmar_drhd_unit *drhd;
713 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100714 bool found = false;
715 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800716
David Woodhoused0501962014-03-11 17:10:29 -0700717 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800718
Mike Travis1b198bb2012-03-05 15:05:16 -0800719 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100720 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800721 if (!ecap_coherent(g_iommus[i]->ecap)) {
722 domain->iommu_coherency = 0;
723 break;
724 }
Weidong Han8e6040972008-12-08 15:49:06 +0800725 }
David Woodhoused0501962014-03-11 17:10:29 -0700726 if (found)
727 return;
728
729 /* No hardware attached; use lowest common denominator */
730 rcu_read_lock();
731 for_each_active_iommu(iommu, drhd) {
732 if (!ecap_coherent(iommu->ecap)) {
733 domain->iommu_coherency = 0;
734 break;
735 }
736 }
737 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800738}
739
Jiang Liu161f6932014-07-11 14:19:37 +0800740static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100741{
Allen Kay8140a952011-10-14 12:32:17 -0700742 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800743 struct intel_iommu *iommu;
744 int ret = 1;
745
746 rcu_read_lock();
747 for_each_active_iommu(iommu, drhd) {
748 if (iommu != skip) {
749 if (!ecap_sc_support(iommu->ecap)) {
750 ret = 0;
751 break;
752 }
753 }
754 }
755 rcu_read_unlock();
756
757 return ret;
758}
759
760static int domain_update_iommu_superpage(struct intel_iommu *skip)
761{
762 struct dmar_drhd_unit *drhd;
763 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700764 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100765
766 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800767 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100768 }
769
Allen Kay8140a952011-10-14 12:32:17 -0700770 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800771 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700772 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800773 if (iommu != skip) {
774 mask &= cap_super_page_val(iommu->cap);
775 if (!mask)
776 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100777 }
778 }
Jiang Liu0e242612014-02-19 14:07:34 +0800779 rcu_read_unlock();
780
Jiang Liu161f6932014-07-11 14:19:37 +0800781 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100782}
783
Sheng Yang58c610b2009-03-18 15:33:05 +0800784/* Some capabilities may be different across iommus */
785static void domain_update_iommu_cap(struct dmar_domain *domain)
786{
787 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800788 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
789 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800790}
791
David Woodhouse03ecc322015-02-13 14:35:21 +0000792static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
793 u8 bus, u8 devfn, int alloc)
794{
795 struct root_entry *root = &iommu->root_entry[bus];
796 struct context_entry *context;
797 u64 *entry;
798
David Woodhousec83b2f22015-06-12 10:15:49 +0100799 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000800 if (devfn >= 0x80) {
801 devfn -= 0x80;
802 entry = &root->hi;
803 }
804 devfn *= 2;
805 }
806 entry = &root->lo;
807 if (*entry & 1)
808 context = phys_to_virt(*entry & VTD_PAGE_MASK);
809 else {
810 unsigned long phy_addr;
811 if (!alloc)
812 return NULL;
813
814 context = alloc_pgtable_page(iommu->node);
815 if (!context)
816 return NULL;
817
818 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
819 phy_addr = virt_to_phys((void *)context);
820 *entry = phy_addr | 1;
821 __iommu_flush_cache(iommu, entry, sizeof(*entry));
822 }
823 return &context[devfn];
824}
825
David Woodhouse4ed6a542015-05-11 14:59:20 +0100826static int iommu_dummy(struct device *dev)
827{
828 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
829}
830
David Woodhouse156baca2014-03-09 14:00:57 -0700831static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800832{
833 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800834 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700835 struct device *tmp;
836 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800837 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800838 int i;
839
David Woodhouse4ed6a542015-05-11 14:59:20 +0100840 if (iommu_dummy(dev))
841 return NULL;
842
David Woodhouse156baca2014-03-09 14:00:57 -0700843 if (dev_is_pci(dev)) {
844 pdev = to_pci_dev(dev);
845 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100846 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700847 dev = &ACPI_COMPANION(dev)->dev;
848
Jiang Liu0e242612014-02-19 14:07:34 +0800849 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800850 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700851 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100852 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800853
Jiang Liub683b232014-02-19 14:07:32 +0800854 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700855 drhd->devices_cnt, i, tmp) {
856 if (tmp == dev) {
857 *bus = drhd->devices[i].bus;
858 *devfn = drhd->devices[i].devfn;
859 goto out;
860 }
861
862 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000863 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700864
865 ptmp = to_pci_dev(tmp);
866 if (ptmp->subordinate &&
867 ptmp->subordinate->number <= pdev->bus->number &&
868 ptmp->subordinate->busn_res.end >= pdev->bus->number)
869 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100870 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800871
David Woodhouse156baca2014-03-09 14:00:57 -0700872 if (pdev && drhd->include_all) {
873 got_pdev:
874 *bus = pdev->bus->number;
875 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800876 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700877 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800878 }
Jiang Liub683b232014-02-19 14:07:32 +0800879 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700880 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800881 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800882
Jiang Liub683b232014-02-19 14:07:32 +0800883 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800884}
885
Weidong Han5331fe62008-12-08 23:00:00 +0800886static void domain_flush_cache(struct dmar_domain *domain,
887 void *addr, int size)
888{
889 if (!domain->iommu_coherency)
890 clflush_cache_range(addr, size);
891}
892
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700893static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
894{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700895 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000896 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700897 unsigned long flags;
898
899 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000900 context = iommu_context_addr(iommu, bus, devfn, 0);
901 if (context)
902 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700903 spin_unlock_irqrestore(&iommu->lock, flags);
904 return ret;
905}
906
907static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
908{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700909 struct context_entry *context;
910 unsigned long flags;
911
912 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000913 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700914 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000915 context_clear_entry(context);
916 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700917 }
918 spin_unlock_irqrestore(&iommu->lock, flags);
919}
920
921static void free_context_table(struct intel_iommu *iommu)
922{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700923 int i;
924 unsigned long flags;
925 struct context_entry *context;
926
927 spin_lock_irqsave(&iommu->lock, flags);
928 if (!iommu->root_entry) {
929 goto out;
930 }
931 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000932 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700933 if (context)
934 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000935
David Woodhousec83b2f22015-06-12 10:15:49 +0100936 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000937 continue;
938
939 context = iommu_context_addr(iommu, i, 0x80, 0);
940 if (context)
941 free_pgtable_page(context);
942
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700943 }
944 free_pgtable_page(iommu->root_entry);
945 iommu->root_entry = NULL;
946out:
947 spin_unlock_irqrestore(&iommu->lock, flags);
948}
949
David Woodhouseb026fd22009-06-28 10:37:25 +0100950static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000951 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700952{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700953 struct dma_pte *parent, *pte = NULL;
954 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700955 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700956
957 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200958
Jiang Liu162d1b12014-07-11 14:19:35 +0800959 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200960 /* Address beyond IOMMU's addressing capabilities. */
961 return NULL;
962
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700963 parent = domain->pgd;
964
David Woodhouse5cf0a762014-03-19 16:07:49 +0000965 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700966 void *tmp_page;
967
David Woodhouseb026fd22009-06-28 10:37:25 +0100968 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700969 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000970 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100971 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000972 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700973 break;
974
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000975 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100976 uint64_t pteval;
977
Suresh Siddha4c923d42009-10-02 11:01:24 -0700978 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700979
David Woodhouse206a73c12009-07-01 19:30:28 +0100980 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700981 return NULL;
David Woodhouse206a73c12009-07-01 19:30:28 +0100982
David Woodhousec85994e2009-07-01 19:21:24 +0100983 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400984 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 +0800985 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100986 /* Someone else set it while we were thinking; use theirs. */
987 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800988 else
David Woodhousec85994e2009-07-01 19:21:24 +0100989 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700990 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000991 if (level == 1)
992 break;
993
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000994 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995 level--;
996 }
997
David Woodhouse5cf0a762014-03-19 16:07:49 +0000998 if (!*target_level)
999 *target_level = level;
1000
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001001 return pte;
1002}
1003
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001004
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001005/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001006static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1007 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001008 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001009{
1010 struct dma_pte *parent, *pte = NULL;
1011 int total = agaw_to_level(domain->agaw);
1012 int offset;
1013
1014 parent = domain->pgd;
1015 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001016 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001017 pte = &parent[offset];
1018 if (level == total)
1019 return pte;
1020
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001021 if (!dma_pte_present(pte)) {
1022 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001023 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001024 }
1025
Yijing Wange16922a2014-05-20 20:37:51 +08001026 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001027 *large_page = total;
1028 return pte;
1029 }
1030
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001031 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001032 total--;
1033 }
1034 return NULL;
1035}
1036
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001037/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001038static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +01001039 unsigned long start_pfn,
1040 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001041{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001042 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001043 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001044
Jiang Liu162d1b12014-07-11 14:19:35 +08001045 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1046 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001047 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001048
David Woodhouse04b18e62009-06-27 19:15:01 +01001049 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001050 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001051 large_page = 1;
1052 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001053 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001054 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001055 continue;
1056 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001057 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001058 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001059 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001060 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001061 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1062
David Woodhouse310a5ab2009-06-28 18:52:20 +01001063 domain_flush_cache(domain, first_pte,
1064 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001065
1066 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001067}
1068
Alex Williamson3269ee02013-06-15 10:27:19 -06001069static void dma_pte_free_level(struct dmar_domain *domain, int level,
1070 struct dma_pte *pte, unsigned long pfn,
1071 unsigned long start_pfn, unsigned long last_pfn)
1072{
1073 pfn = max(start_pfn, pfn);
1074 pte = &pte[pfn_level_offset(pfn, level)];
1075
1076 do {
1077 unsigned long level_pfn;
1078 struct dma_pte *level_pte;
1079
1080 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1081 goto next;
1082
1083 level_pfn = pfn & level_mask(level - 1);
1084 level_pte = phys_to_virt(dma_pte_addr(pte));
1085
1086 if (level > 2)
1087 dma_pte_free_level(domain, level - 1, level_pte,
1088 level_pfn, start_pfn, last_pfn);
1089
1090 /* If range covers entire pagetable, free it */
1091 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001092 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001093 dma_clear_pte(pte);
1094 domain_flush_cache(domain, pte, sizeof(*pte));
1095 free_pgtable_page(level_pte);
1096 }
1097next:
1098 pfn += level_size(level);
1099 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1100}
1101
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001102/* free page table pages. last level pte should already be cleared */
1103static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001104 unsigned long start_pfn,
1105 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001106{
Jiang Liu162d1b12014-07-11 14:19:35 +08001107 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1108 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001109 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001110
Jiang Liud41a4ad2014-07-11 14:19:34 +08001111 dma_pte_clear_range(domain, start_pfn, last_pfn);
1112
David Woodhousef3a0a522009-06-30 03:40:07 +01001113 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001114 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1115 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001116
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001117 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001118 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001119 free_pgtable_page(domain->pgd);
1120 domain->pgd = NULL;
1121 }
1122}
1123
David Woodhouseea8ea462014-03-05 17:09:32 +00001124/* When a page at a given level is being unlinked from its parent, we don't
1125 need to *modify* it at all. All we need to do is make a list of all the
1126 pages which can be freed just as soon as we've flushed the IOTLB and we
1127 know the hardware page-walk will no longer touch them.
1128 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1129 be freed. */
1130static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1131 int level, struct dma_pte *pte,
1132 struct page *freelist)
1133{
1134 struct page *pg;
1135
1136 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1137 pg->freelist = freelist;
1138 freelist = pg;
1139
1140 if (level == 1)
1141 return freelist;
1142
Jiang Liuadeb2592014-04-09 10:20:39 +08001143 pte = page_address(pg);
1144 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001145 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1146 freelist = dma_pte_list_pagetables(domain, level - 1,
1147 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001148 pte++;
1149 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001150
1151 return freelist;
1152}
1153
1154static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1155 struct dma_pte *pte, unsigned long pfn,
1156 unsigned long start_pfn,
1157 unsigned long last_pfn,
1158 struct page *freelist)
1159{
1160 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1161
1162 pfn = max(start_pfn, pfn);
1163 pte = &pte[pfn_level_offset(pfn, level)];
1164
1165 do {
1166 unsigned long level_pfn;
1167
1168 if (!dma_pte_present(pte))
1169 goto next;
1170
1171 level_pfn = pfn & level_mask(level);
1172
1173 /* If range covers entire pagetable, free it */
1174 if (start_pfn <= level_pfn &&
1175 last_pfn >= level_pfn + level_size(level) - 1) {
1176 /* These suborbinate page tables are going away entirely. Don't
1177 bother to clear them; we're just going to *free* them. */
1178 if (level > 1 && !dma_pte_superpage(pte))
1179 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1180
1181 dma_clear_pte(pte);
1182 if (!first_pte)
1183 first_pte = pte;
1184 last_pte = pte;
1185 } else if (level > 1) {
1186 /* Recurse down into a level that isn't *entirely* obsolete */
1187 freelist = dma_pte_clear_level(domain, level - 1,
1188 phys_to_virt(dma_pte_addr(pte)),
1189 level_pfn, start_pfn, last_pfn,
1190 freelist);
1191 }
1192next:
1193 pfn += level_size(level);
1194 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1195
1196 if (first_pte)
1197 domain_flush_cache(domain, first_pte,
1198 (void *)++last_pte - (void *)first_pte);
1199
1200 return freelist;
1201}
1202
1203/* We can't just free the pages because the IOMMU may still be walking
1204 the page tables, and may have cached the intermediate levels. The
1205 pages can only be freed after the IOTLB flush has been done. */
1206struct page *domain_unmap(struct dmar_domain *domain,
1207 unsigned long start_pfn,
1208 unsigned long last_pfn)
1209{
David Woodhouseea8ea462014-03-05 17:09:32 +00001210 struct page *freelist = NULL;
1211
Jiang Liu162d1b12014-07-11 14:19:35 +08001212 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1213 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001214 BUG_ON(start_pfn > last_pfn);
1215
1216 /* we don't need lock here; nobody else touches the iova range */
1217 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1218 domain->pgd, 0, start_pfn, last_pfn, NULL);
1219
1220 /* free pgd */
1221 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1222 struct page *pgd_page = virt_to_page(domain->pgd);
1223 pgd_page->freelist = freelist;
1224 freelist = pgd_page;
1225
1226 domain->pgd = NULL;
1227 }
1228
1229 return freelist;
1230}
1231
1232void dma_free_pagelist(struct page *freelist)
1233{
1234 struct page *pg;
1235
1236 while ((pg = freelist)) {
1237 freelist = pg->freelist;
1238 free_pgtable_page(page_address(pg));
1239 }
1240}
1241
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001242/* iommu handling */
1243static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1244{
1245 struct root_entry *root;
1246 unsigned long flags;
1247
Suresh Siddha4c923d42009-10-02 11:01:24 -07001248 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001249 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001250 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001251 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001252 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001253 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001254
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001255 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001256
1257 spin_lock_irqsave(&iommu->lock, flags);
1258 iommu->root_entry = root;
1259 spin_unlock_irqrestore(&iommu->lock, flags);
1260
1261 return 0;
1262}
1263
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001264static void iommu_set_root_entry(struct intel_iommu *iommu)
1265{
David Woodhouse03ecc322015-02-13 14:35:21 +00001266 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001267 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001268 unsigned long flag;
1269
David Woodhouse03ecc322015-02-13 14:35:21 +00001270 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001271 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001272 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001273
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001274 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001275 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001276
David Woodhousec416daa2009-05-10 20:30:58 +01001277 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001278
1279 /* Make sure hardware complete it */
1280 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001281 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001282
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001283 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001284}
1285
1286static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1287{
1288 u32 val;
1289 unsigned long flag;
1290
David Woodhouse9af88142009-02-13 23:18:03 +00001291 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001293
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001294 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001295 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001296
1297 /* Make sure hardware complete it */
1298 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001299 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001300
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001301 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001302}
1303
1304/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001305static void __iommu_flush_context(struct intel_iommu *iommu,
1306 u16 did, u16 source_id, u8 function_mask,
1307 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001308{
1309 u64 val = 0;
1310 unsigned long flag;
1311
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001312 switch (type) {
1313 case DMA_CCMD_GLOBAL_INVL:
1314 val = DMA_CCMD_GLOBAL_INVL;
1315 break;
1316 case DMA_CCMD_DOMAIN_INVL:
1317 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1318 break;
1319 case DMA_CCMD_DEVICE_INVL:
1320 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1321 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1322 break;
1323 default:
1324 BUG();
1325 }
1326 val |= DMA_CCMD_ICC;
1327
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001328 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001329 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1330
1331 /* Make sure hardware complete it */
1332 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1333 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1334
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001335 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001336}
1337
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001338/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001339static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1340 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001341{
1342 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1343 u64 val = 0, val_iva = 0;
1344 unsigned long flag;
1345
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001346 switch (type) {
1347 case DMA_TLB_GLOBAL_FLUSH:
1348 /* global flush doesn't need set IVA_REG */
1349 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1350 break;
1351 case DMA_TLB_DSI_FLUSH:
1352 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1353 break;
1354 case DMA_TLB_PSI_FLUSH:
1355 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001356 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001357 val_iva = size_order | addr;
1358 break;
1359 default:
1360 BUG();
1361 }
1362 /* Note: set drain read/write */
1363#if 0
1364 /*
1365 * This is probably to be super secure.. Looks like we can
1366 * ignore it without any impact.
1367 */
1368 if (cap_read_drain(iommu->cap))
1369 val |= DMA_TLB_READ_DRAIN;
1370#endif
1371 if (cap_write_drain(iommu->cap))
1372 val |= DMA_TLB_WRITE_DRAIN;
1373
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001374 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001375 /* Note: Only uses first TLB reg currently */
1376 if (val_iva)
1377 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1378 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1379
1380 /* Make sure hardware complete it */
1381 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1382 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1383
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001384 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001385
1386 /* check IOTLB invalidation granularity */
1387 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001388 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001389 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001390 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001391 (unsigned long long)DMA_TLB_IIRG(type),
1392 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001393}
1394
David Woodhouse64ae8922014-03-09 12:52:30 -07001395static struct device_domain_info *
1396iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1397 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001398{
Quentin Lambert2f119c72015-02-06 10:59:53 +01001399 bool found = false;
Yu Zhao93a23a72009-05-18 13:51:37 +08001400 unsigned long flags;
1401 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001402 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001403
1404 if (!ecap_dev_iotlb_support(iommu->ecap))
1405 return NULL;
1406
1407 if (!iommu->qi)
1408 return NULL;
1409
1410 spin_lock_irqsave(&device_domain_lock, flags);
1411 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001412 if (info->iommu == iommu && info->bus == bus &&
1413 info->devfn == devfn) {
Quentin Lambert2f119c72015-02-06 10:59:53 +01001414 found = true;
Yu Zhao93a23a72009-05-18 13:51:37 +08001415 break;
1416 }
1417 spin_unlock_irqrestore(&device_domain_lock, flags);
1418
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001419 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001420 return NULL;
1421
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001422 pdev = to_pci_dev(info->dev);
1423
1424 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001425 return NULL;
1426
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001427 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001428 return NULL;
1429
Yu Zhao93a23a72009-05-18 13:51:37 +08001430 return info;
1431}
1432
1433static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1434{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001435 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001436 return;
1437
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001438 pci_enable_ats(to_pci_dev(info->dev), VTD_PAGE_SHIFT);
Yu Zhao93a23a72009-05-18 13:51:37 +08001439}
1440
1441static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1442{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001443 if (!info->dev || !dev_is_pci(info->dev) ||
1444 !pci_ats_enabled(to_pci_dev(info->dev)))
Yu Zhao93a23a72009-05-18 13:51:37 +08001445 return;
1446
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001447 pci_disable_ats(to_pci_dev(info->dev));
Yu Zhao93a23a72009-05-18 13:51:37 +08001448}
1449
1450static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1451 u64 addr, unsigned mask)
1452{
1453 u16 sid, qdep;
1454 unsigned long flags;
1455 struct device_domain_info *info;
1456
1457 spin_lock_irqsave(&device_domain_lock, flags);
1458 list_for_each_entry(info, &domain->devices, link) {
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001459 struct pci_dev *pdev;
1460 if (!info->dev || !dev_is_pci(info->dev))
1461 continue;
1462
1463 pdev = to_pci_dev(info->dev);
1464 if (!pci_ats_enabled(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001465 continue;
1466
1467 sid = info->bus << 8 | info->devfn;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001468 qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001469 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1470 }
1471 spin_unlock_irqrestore(&device_domain_lock, flags);
1472}
1473
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001474static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1475 struct dmar_domain *domain,
1476 unsigned long pfn, unsigned int pages,
1477 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001478{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001479 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001480 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001481 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001482
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001483 BUG_ON(pages == 0);
1484
David Woodhouseea8ea462014-03-05 17:09:32 +00001485 if (ih)
1486 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001487 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001488 * Fallback to domain selective flush if no PSI support or the size is
1489 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001490 * PSI requires page size to be 2 ^ x, and the base address is naturally
1491 * aligned to the size
1492 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001493 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1494 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001495 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001496 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001497 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001498 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001499
1500 /*
Nadav Amit82653632010-04-01 13:24:40 +03001501 * In caching mode, changes of pages from non-present to present require
1502 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001503 */
Nadav Amit82653632010-04-01 13:24:40 +03001504 if (!cap_caching_mode(iommu->cap) || !map)
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001505 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1506 addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001507}
1508
mark grossf8bab732008-02-08 04:18:38 -08001509static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1510{
1511 u32 pmen;
1512 unsigned long flags;
1513
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001514 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001515 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1516 pmen &= ~DMA_PMEN_EPM;
1517 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1518
1519 /* wait for the protected region status bit to clear */
1520 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1521 readl, !(pmen & DMA_PMEN_PRS), pmen);
1522
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001523 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001524}
1525
Jiang Liu2a41cce2014-07-11 14:19:33 +08001526static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001527{
1528 u32 sts;
1529 unsigned long flags;
1530
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001531 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001532 iommu->gcmd |= DMA_GCMD_TE;
1533 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001534
1535 /* Make sure hardware complete it */
1536 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001537 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001538
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001539 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001540}
1541
Jiang Liu2a41cce2014-07-11 14:19:33 +08001542static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001543{
1544 u32 sts;
1545 unsigned long flag;
1546
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001547 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001548 iommu->gcmd &= ~DMA_GCMD_TE;
1549 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1550
1551 /* Make sure hardware complete it */
1552 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001553 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001554
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001555 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001556}
1557
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001558
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001559static int iommu_init_domains(struct intel_iommu *iommu)
1560{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001561 u32 ndomains, nlongs;
1562 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001563
1564 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001565 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001566 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001567 nlongs = BITS_TO_LONGS(ndomains);
1568
Donald Dutile94a91b52009-08-20 16:51:34 -04001569 spin_lock_init(&iommu->lock);
1570
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001571 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1572 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001573 pr_err("%s: Allocating domain id array failed\n",
1574 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001575 return -ENOMEM;
1576 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001577
1578 size = ((ndomains >> 8) + 1) * sizeof(struct dmar_domain **);
1579 iommu->domains = kzalloc(size, GFP_KERNEL);
1580
1581 if (iommu->domains) {
1582 size = 256 * sizeof(struct dmar_domain *);
1583 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1584 }
1585
1586 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001587 pr_err("%s: Allocating domain array failed\n",
1588 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001589 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001590 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001591 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001592 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001593 return -ENOMEM;
1594 }
1595
Joerg Roedel8bf47812015-07-21 10:41:21 +02001596
1597
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001598 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001599 * If Caching mode is set, then invalid translations are tagged
1600 * with domain-id 0, hence we need to pre-allocate it. We also
1601 * use domain-id 0 as a marker for non-allocated domain-id, so
1602 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001603 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001604 set_bit(0, iommu->domain_ids);
1605
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001606 return 0;
1607}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001608
Jiang Liuffebeb42014-11-09 22:48:02 +08001609static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001610{
1611 struct dmar_domain *domain;
Jiang Liu2a46ddf2014-07-11 14:19:30 +08001612 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001613
Donald Dutile94a91b52009-08-20 16:51:34 -04001614 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001615 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Jiang Liua4eaa862014-02-19 14:07:30 +08001616 /*
1617 * Domain id 0 is reserved for invalid translation
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001618 * if hardware supports caching mode and used as
1619 * a non-allocated marker.
Jiang Liua4eaa862014-02-19 14:07:30 +08001620 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001621 if (i == 0)
Jiang Liua4eaa862014-02-19 14:07:30 +08001622 continue;
1623
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001624 domain = get_iommu_domain(iommu, i);
Donald Dutile94a91b52009-08-20 16:51:34 -04001625 clear_bit(i, iommu->domain_ids);
Jiang Liu129ad282014-07-11 14:19:31 +08001626 if (domain_detach_iommu(domain, iommu) == 0 &&
1627 !domain_type_is_vm(domain))
Jiang Liu92d03cc2014-02-19 14:07:28 +08001628 domain_exit(domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001629 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001630 }
1631
1632 if (iommu->gcmd & DMA_GCMD_TE)
1633 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001634}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001635
Jiang Liuffebeb42014-11-09 22:48:02 +08001636static void free_dmar_iommu(struct intel_iommu *iommu)
1637{
1638 if ((iommu->domains) && (iommu->domain_ids)) {
Joerg Roedel8bf47812015-07-21 10:41:21 +02001639 int elems = (cap_ndoms(iommu->cap) >> 8) + 1;
1640 int i;
1641
1642 for (i = 0; i < elems; i++)
1643 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001644 kfree(iommu->domains);
1645 kfree(iommu->domain_ids);
1646 iommu->domains = NULL;
1647 iommu->domain_ids = NULL;
1648 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001649
Weidong Hand9630fe2008-12-08 11:06:32 +08001650 g_iommus[iommu->seq_id] = NULL;
1651
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001652 /* free context mapping */
1653 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001654}
1655
Jiang Liuab8dfe22014-07-11 14:19:27 +08001656static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001657{
Jiang Liu92d03cc2014-02-19 14:07:28 +08001658 /* domain id for virtual machine, it won't be set in context */
1659 static atomic_t vm_domid = ATOMIC_INIT(0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001660 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001661
1662 domain = alloc_domain_mem();
1663 if (!domain)
1664 return NULL;
1665
Jiang Liuab8dfe22014-07-11 14:19:27 +08001666 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001667 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001668 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001669 spin_lock_init(&domain->iommu_lock);
1670 INIT_LIST_HEAD(&domain->devices);
Jiang Liuab8dfe22014-07-11 14:19:27 +08001671 if (flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Jiang Liu92d03cc2014-02-19 14:07:28 +08001672 domain->id = atomic_inc_return(&vm_domid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001673
1674 return domain;
1675}
1676
Jiang Liufb170fb2014-07-11 14:19:28 +08001677static int __iommu_attach_domain(struct dmar_domain *domain,
1678 struct intel_iommu *iommu)
1679{
1680 int num;
1681 unsigned long ndomains;
1682
Joerg Roedele2411422015-07-21 11:18:21 +02001683 num = domain->iommu_did[iommu->seq_id];
1684 if (num)
1685 return num;
1686
Jiang Liufb170fb2014-07-11 14:19:28 +08001687 ndomains = cap_ndoms(iommu->cap);
Joerg Roedele2411422015-07-21 11:18:21 +02001688 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1689
Jiang Liufb170fb2014-07-11 14:19:28 +08001690 if (num < ndomains) {
1691 set_bit(num, iommu->domain_ids);
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001692 set_iommu_domain(iommu, num, domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001693 domain->iommu_did[iommu->seq_id] = num;
Jiang Liufb170fb2014-07-11 14:19:28 +08001694 } else {
1695 num = -ENOSPC;
1696 }
1697
Joerg Roedele2411422015-07-21 11:18:21 +02001698 if (num < 0)
1699 pr_err("%s: No free domain ids\n", iommu->name);
1700
Jiang Liufb170fb2014-07-11 14:19:28 +08001701 return num;
1702}
1703
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001704static int iommu_attach_domain(struct dmar_domain *domain,
1705 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001706{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001707 int num;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001708 unsigned long flags;
1709
Weidong Han8c11e792008-12-08 15:29:22 +08001710 spin_lock_irqsave(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001711 num = __iommu_attach_domain(domain, iommu);
Jiang Liu44bde612014-07-11 14:19:29 +08001712 spin_unlock_irqrestore(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001713
Jiang Liufb170fb2014-07-11 14:19:28 +08001714 return num;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001715}
1716
1717static void iommu_detach_domain(struct dmar_domain *domain,
1718 struct intel_iommu *iommu)
1719{
1720 unsigned long flags;
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001721 int num;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001722
1723 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001724
1725 num = domain->iommu_did[iommu->seq_id];
1726
1727 if (num == 0)
1728 return;
1729
1730 clear_bit(num, iommu->domain_ids);
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001731 set_iommu_domain(iommu, num, NULL);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001732
Weidong Han8c11e792008-12-08 15:29:22 +08001733 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001734}
1735
Jiang Liufb170fb2014-07-11 14:19:28 +08001736static void domain_attach_iommu(struct dmar_domain *domain,
1737 struct intel_iommu *iommu)
1738{
1739 unsigned long flags;
1740
1741 spin_lock_irqsave(&domain->iommu_lock, flags);
1742 if (!test_and_set_bit(iommu->seq_id, domain->iommu_bmp)) {
1743 domain->iommu_count++;
1744 if (domain->iommu_count == 1)
1745 domain->nid = iommu->node;
1746 domain_update_iommu_cap(domain);
1747 }
1748 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1749}
1750
1751static int domain_detach_iommu(struct dmar_domain *domain,
1752 struct intel_iommu *iommu)
1753{
1754 unsigned long flags;
1755 int count = INT_MAX;
1756
1757 spin_lock_irqsave(&domain->iommu_lock, flags);
1758 if (test_and_clear_bit(iommu->seq_id, domain->iommu_bmp)) {
1759 count = --domain->iommu_count;
1760 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001761 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001762 }
1763 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1764
1765 return count;
1766}
1767
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001768static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001769static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001770
Joseph Cihula51a63e62011-03-21 11:04:24 -07001771static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001772{
1773 struct pci_dev *pdev = NULL;
1774 struct iova *iova;
1775 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001776
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001777 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1778 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001779
Mark Gross8a443df2008-03-04 14:59:31 -08001780 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1781 &reserved_rbtree_key);
1782
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001783 /* IOAPIC ranges shouldn't be accessed by DMA */
1784 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1785 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001786 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001787 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001788 return -ENODEV;
1789 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001790
1791 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1792 for_each_pci_dev(pdev) {
1793 struct resource *r;
1794
1795 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1796 r = &pdev->resource[i];
1797 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1798 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001799 iova = reserve_iova(&reserved_iova_list,
1800 IOVA_PFN(r->start),
1801 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001802 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001803 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001804 return -ENODEV;
1805 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001806 }
1807 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001808 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001809}
1810
1811static void domain_reserve_special_ranges(struct dmar_domain *domain)
1812{
1813 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1814}
1815
1816static inline int guestwidth_to_adjustwidth(int gaw)
1817{
1818 int agaw;
1819 int r = (gaw - 12) % 9;
1820
1821 if (r == 0)
1822 agaw = gaw;
1823 else
1824 agaw = gaw + 9 - r;
1825 if (agaw > 64)
1826 agaw = 64;
1827 return agaw;
1828}
1829
1830static int domain_init(struct dmar_domain *domain, int guest_width)
1831{
1832 struct intel_iommu *iommu;
1833 int adjust_width, agaw;
1834 unsigned long sagaw;
1835
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001836 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1837 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001838 domain_reserve_special_ranges(domain);
1839
1840 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001841 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001842 if (guest_width > cap_mgaw(iommu->cap))
1843 guest_width = cap_mgaw(iommu->cap);
1844 domain->gaw = guest_width;
1845 adjust_width = guestwidth_to_adjustwidth(guest_width);
1846 agaw = width_to_agaw(adjust_width);
1847 sagaw = cap_sagaw(iommu->cap);
1848 if (!test_bit(agaw, &sagaw)) {
1849 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001850 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001851 agaw = find_next_bit(&sagaw, 5, agaw);
1852 if (agaw >= 5)
1853 return -ENODEV;
1854 }
1855 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001856
Weidong Han8e6040972008-12-08 15:49:06 +08001857 if (ecap_coherent(iommu->ecap))
1858 domain->iommu_coherency = 1;
1859 else
1860 domain->iommu_coherency = 0;
1861
Sheng Yang58c610b2009-03-18 15:33:05 +08001862 if (ecap_sc_support(iommu->ecap))
1863 domain->iommu_snooping = 1;
1864 else
1865 domain->iommu_snooping = 0;
1866
David Woodhouse214e39a2014-03-19 10:38:49 +00001867 if (intel_iommu_superpage)
1868 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1869 else
1870 domain->iommu_superpage = 0;
1871
Suresh Siddha4c923d42009-10-02 11:01:24 -07001872 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001873
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001874 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001875 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001876 if (!domain->pgd)
1877 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001878 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001879 return 0;
1880}
1881
1882static void domain_exit(struct dmar_domain *domain)
1883{
Alex Williamson46ebb7a2015-07-14 14:48:53 -06001884 struct dmar_drhd_unit *drhd;
1885 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00001886 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001887
1888 /* Domain 0 is reserved, so dont process it */
1889 if (!domain)
1890 return;
1891
Alex Williamson7b668352011-05-24 12:02:41 +01001892 /* Flush any lazy unmaps that may reference this domain */
1893 if (!intel_iommu_strict)
1894 flush_unmaps_timeout(0);
1895
Jiang Liu92d03cc2014-02-19 14:07:28 +08001896 /* remove associated devices */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001897 domain_remove_dev_info(domain);
Jiang Liu92d03cc2014-02-19 14:07:28 +08001898
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001899 /* destroy iovas */
1900 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001901
David Woodhouseea8ea462014-03-05 17:09:32 +00001902 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001903
Jiang Liu92d03cc2014-02-19 14:07:28 +08001904 /* clear attached or cached domains */
Jiang Liu0e242612014-02-19 14:07:34 +08001905 rcu_read_lock();
Alex Williamson46ebb7a2015-07-14 14:48:53 -06001906 for_each_active_iommu(iommu, drhd)
1907 if (domain_type_is_vm(domain) ||
1908 test_bit(iommu->seq_id, domain->iommu_bmp))
1909 iommu_detach_domain(domain, iommu);
Jiang Liu0e242612014-02-19 14:07:34 +08001910 rcu_read_unlock();
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001911
David Woodhouseea8ea462014-03-05 17:09:32 +00001912 dma_free_pagelist(freelist);
1913
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001914 free_domain_mem(domain);
1915}
1916
David Woodhouse64ae8922014-03-09 12:52:30 -07001917static int domain_context_mapping_one(struct dmar_domain *domain,
1918 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001919 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001920{
Joerg Roedel28ccce02015-07-21 14:45:31 +02001921 int translation = CONTEXT_TT_MULTI_LEVEL;
1922 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001923 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001924 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001925 struct dma_pte *pgd;
Weidong Hanea6606b2008-12-08 23:08:15 +08001926 int id;
1927 int agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001928
1929 if (hw_pass_through && domain_type_is_si(domain))
1930 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001931
1932 pr_debug("Set context mapping for %02x:%02x.%d\n",
1933 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001934
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001935 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001936
David Woodhouse03ecc322015-02-13 14:35:21 +00001937 spin_lock_irqsave(&iommu->lock, flags);
1938 context = iommu_context_addr(iommu, bus, devfn, 1);
1939 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001940 if (!context)
1941 return -ENOMEM;
1942 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001943 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001944 spin_unlock_irqrestore(&iommu->lock, flags);
1945 return 0;
1946 }
1947
Weidong Hanea6606b2008-12-08 23:08:15 +08001948 pgd = domain->pgd;
1949
Joerg Roedelde24e552015-07-21 14:53:04 +02001950 id = __iommu_attach_domain(domain, iommu);
1951 if (id < 0) {
1952 spin_unlock_irqrestore(&iommu->lock, flags);
1953 pr_err("%s: No free domain ids\n", iommu->name);
1954 return -EFAULT;
Weidong Hanea6606b2008-12-08 23:08:15 +08001955 }
1956
Joerg Roedelde24e552015-07-21 14:53:04 +02001957 context_clear_entry(context);
Weidong Hanea6606b2008-12-08 23:08:15 +08001958 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001959
Joerg Roedelde24e552015-07-21 14:53:04 +02001960 /*
1961 * Skip top levels of page tables for iommu which has less agaw
1962 * than default. Unnecessary for PT mode.
1963 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001964 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02001965 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1966 pgd = phys_to_virt(dma_pte_addr(pgd));
1967 if (!dma_pte_present(pgd)) {
1968 spin_unlock_irqrestore(&iommu->lock, flags);
1969 return -ENOMEM;
1970 }
1971 }
1972
David Woodhouse64ae8922014-03-09 12:52:30 -07001973 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001974 translation = info ? CONTEXT_TT_DEV_IOTLB :
1975 CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02001976
Yu Zhao93a23a72009-05-18 13:51:37 +08001977 context_set_address_root(context, virt_to_phys(pgd));
1978 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02001979 } else {
1980 /*
1981 * In pass through mode, AW must be programmed to
1982 * indicate the largest AGAW value supported by
1983 * hardware. And ASR is ignored by hardware.
1984 */
1985 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001986 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001987
1988 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001989 context_set_fault_enable(context);
1990 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001991 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001992
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001993 /*
1994 * It's a non-present to present mapping. If hardware doesn't cache
1995 * non-present entry we only need to flush the write-buffer. If the
1996 * _does_ cache non-present entries, then it does so in the special
1997 * domain #0, which we have to flush:
1998 */
1999 if (cap_caching_mode(iommu->cap)) {
2000 iommu->flush.flush_context(iommu, 0,
2001 (((u16)bus) << 8) | devfn,
2002 DMA_CCMD_MASK_NOBIT,
2003 DMA_CCMD_DEVICE_INVL);
Jiang Liu18fd7792014-07-11 14:19:26 +08002004 iommu->flush.flush_iotlb(iommu, id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002005 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002006 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002007 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002008 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002009 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08002010
Jiang Liufb170fb2014-07-11 14:19:28 +08002011 domain_attach_iommu(domain, iommu);
2012
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002013 return 0;
2014}
2015
Alex Williamson579305f2014-07-03 09:51:43 -06002016struct domain_context_mapping_data {
2017 struct dmar_domain *domain;
2018 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002019};
2020
2021static int domain_context_mapping_cb(struct pci_dev *pdev,
2022 u16 alias, void *opaque)
2023{
2024 struct domain_context_mapping_data *data = opaque;
2025
2026 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002027 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002028}
2029
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002030static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002031domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002032{
David Woodhouse64ae8922014-03-09 12:52:30 -07002033 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002034 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002035 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002036
David Woodhousee1f167f2014-03-09 15:24:46 -07002037 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002038 if (!iommu)
2039 return -ENODEV;
2040
Alex Williamson579305f2014-07-03 09:51:43 -06002041 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002042 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002043
2044 data.domain = domain;
2045 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002046
2047 return pci_for_each_dma_alias(to_pci_dev(dev),
2048 &domain_context_mapping_cb, &data);
2049}
2050
2051static int domain_context_mapped_cb(struct pci_dev *pdev,
2052 u16 alias, void *opaque)
2053{
2054 struct intel_iommu *iommu = opaque;
2055
2056 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002057}
2058
David Woodhousee1f167f2014-03-09 15:24:46 -07002059static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002060{
Weidong Han5331fe62008-12-08 23:00:00 +08002061 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002062 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002063
David Woodhousee1f167f2014-03-09 15:24:46 -07002064 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002065 if (!iommu)
2066 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002067
Alex Williamson579305f2014-07-03 09:51:43 -06002068 if (!dev_is_pci(dev))
2069 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002070
Alex Williamson579305f2014-07-03 09:51:43 -06002071 return !pci_for_each_dma_alias(to_pci_dev(dev),
2072 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002073}
2074
Fenghua Yuf5329592009-08-04 15:09:37 -07002075/* Returns a number of VTD pages, but aligned to MM page size */
2076static inline unsigned long aligned_nrpages(unsigned long host_addr,
2077 size_t size)
2078{
2079 host_addr &= ~PAGE_MASK;
2080 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2081}
2082
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002083/* Return largest possible superpage level for a given mapping */
2084static inline int hardware_largepage_caps(struct dmar_domain *domain,
2085 unsigned long iov_pfn,
2086 unsigned long phy_pfn,
2087 unsigned long pages)
2088{
2089 int support, level = 1;
2090 unsigned long pfnmerge;
2091
2092 support = domain->iommu_superpage;
2093
2094 /* To use a large page, the virtual *and* physical addresses
2095 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2096 of them will mean we have to use smaller pages. So just
2097 merge them and check both at once. */
2098 pfnmerge = iov_pfn | phy_pfn;
2099
2100 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2101 pages >>= VTD_STRIDE_SHIFT;
2102 if (!pages)
2103 break;
2104 pfnmerge >>= VTD_STRIDE_SHIFT;
2105 level++;
2106 support--;
2107 }
2108 return level;
2109}
2110
David Woodhouse9051aa02009-06-29 12:30:54 +01002111static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2112 struct scatterlist *sg, unsigned long phys_pfn,
2113 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002114{
2115 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002116 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002117 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002118 unsigned int largepage_lvl = 0;
2119 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002120
Jiang Liu162d1b12014-07-11 14:19:35 +08002121 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002122
2123 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2124 return -EINVAL;
2125
2126 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2127
Jiang Liucc4f14a2014-11-26 09:42:10 +08002128 if (!sg) {
2129 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002130 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2131 }
2132
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002133 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002134 uint64_t tmp;
2135
David Woodhousee1605492009-06-29 11:17:38 +01002136 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002137 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002138 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2139 sg->dma_length = sg->length;
2140 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002141 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002142 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002143
David Woodhousee1605492009-06-29 11:17:38 +01002144 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002145 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2146
David Woodhouse5cf0a762014-03-19 16:07:49 +00002147 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002148 if (!pte)
2149 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002150 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002151 if (largepage_lvl > 1) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002152 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002153 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2154 /*
2155 * Ensure that old small page tables are
2156 * removed to make room for superpage,
2157 * if they exist.
2158 */
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002159 dma_pte_free_pagetable(domain, iov_pfn,
Jiang Liud41a4ad2014-07-11 14:19:34 +08002160 iov_pfn + lvl_pages - 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002161 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002162 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002163 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002164
David Woodhousee1605492009-06-29 11:17:38 +01002165 }
2166 /* We don't need lock here, nobody else
2167 * touches the iova range
2168 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002169 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002170 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002171 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002172 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2173 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002174 if (dumps) {
2175 dumps--;
2176 debug_dma_dump_mappings(NULL);
2177 }
2178 WARN_ON(1);
2179 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002180
2181 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2182
2183 BUG_ON(nr_pages < lvl_pages);
2184 BUG_ON(sg_res < lvl_pages);
2185
2186 nr_pages -= lvl_pages;
2187 iov_pfn += lvl_pages;
2188 phys_pfn += lvl_pages;
2189 pteval += lvl_pages * VTD_PAGE_SIZE;
2190 sg_res -= lvl_pages;
2191
2192 /* If the next PTE would be the first in a new page, then we
2193 need to flush the cache on the entries we've just written.
2194 And then we'll need to recalculate 'pte', so clear it and
2195 let it get set again in the if (!pte) block above.
2196
2197 If we're done (!nr_pages) we need to flush the cache too.
2198
2199 Also if we've been setting superpages, we may need to
2200 recalculate 'pte' and switch back to smaller pages for the
2201 end of the mapping, if the trailing size is not enough to
2202 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002203 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002204 if (!nr_pages || first_pte_in_page(pte) ||
2205 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002206 domain_flush_cache(domain, first_pte,
2207 (void *)pte - (void *)first_pte);
2208 pte = NULL;
2209 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002210
2211 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002212 sg = sg_next(sg);
2213 }
2214 return 0;
2215}
2216
David Woodhouse9051aa02009-06-29 12:30:54 +01002217static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2218 struct scatterlist *sg, unsigned long nr_pages,
2219 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002220{
David Woodhouse9051aa02009-06-29 12:30:54 +01002221 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2222}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002223
David Woodhouse9051aa02009-06-29 12:30:54 +01002224static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2225 unsigned long phys_pfn, unsigned long nr_pages,
2226 int prot)
2227{
2228 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002229}
2230
Weidong Hanc7151a82008-12-08 22:51:37 +08002231static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002232{
Weidong Hanc7151a82008-12-08 22:51:37 +08002233 if (!iommu)
2234 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002235
2236 clear_context_table(iommu, bus, devfn);
2237 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002238 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002239 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002240}
2241
David Woodhouse109b9b02012-05-25 17:43:02 +01002242static inline void unlink_domain_info(struct device_domain_info *info)
2243{
2244 assert_spin_locked(&device_domain_lock);
2245 list_del(&info->link);
2246 list_del(&info->global);
2247 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002248 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002249}
2250
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002251static void domain_remove_dev_info(struct dmar_domain *domain)
2252{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002253 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002254 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002255
2256 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wang3a74ca02014-05-20 20:37:47 +08002257 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhouse109b9b02012-05-25 17:43:02 +01002258 unlink_domain_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002259 spin_unlock_irqrestore(&device_domain_lock, flags);
2260
Yu Zhao93a23a72009-05-18 13:51:37 +08002261 iommu_disable_dev_iotlb(info);
David Woodhouse7c7faa12014-03-09 13:33:06 -07002262 iommu_detach_dev(info->iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002263
Jiang Liuab8dfe22014-07-11 14:19:27 +08002264 if (domain_type_is_vm(domain)) {
David Woodhouse7c7faa12014-03-09 13:33:06 -07002265 iommu_detach_dependent_devices(info->iommu, info->dev);
Jiang Liufb170fb2014-07-11 14:19:28 +08002266 domain_detach_iommu(domain, info->iommu);
Jiang Liu92d03cc2014-02-19 14:07:28 +08002267 }
2268
2269 free_devinfo_mem(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002270 spin_lock_irqsave(&device_domain_lock, flags);
2271 }
2272 spin_unlock_irqrestore(&device_domain_lock, flags);
2273}
2274
2275/*
2276 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002277 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002278 */
David Woodhouse1525a292014-03-06 16:19:30 +00002279static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002280{
2281 struct device_domain_info *info;
2282
2283 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002284 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002285 if (info)
2286 return info->domain;
2287 return NULL;
2288}
2289
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002290static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002291dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2292{
2293 struct device_domain_info *info;
2294
2295 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002296 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002297 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002298 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002299
2300 return NULL;
2301}
2302
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002303static struct dmar_domain *dmar_insert_dev_info(struct intel_iommu *iommu,
David Woodhouse41e80dca2014-03-09 13:55:54 -07002304 int bus, int devfn,
David Woodhouseb718cd32014-03-09 13:11:33 -07002305 struct device *dev,
2306 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002307{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002308 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002309 struct device_domain_info *info;
2310 unsigned long flags;
2311
2312 info = alloc_devinfo_mem();
2313 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002314 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002315
Jiang Liu745f2582014-02-19 14:07:26 +08002316 info->bus = bus;
2317 info->devfn = devfn;
2318 info->dev = dev;
2319 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002320 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002321
2322 spin_lock_irqsave(&device_domain_lock, flags);
2323 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002324 found = find_domain(dev);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002325 else {
2326 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002327 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002328 if (info2)
2329 found = info2->domain;
2330 }
Jiang Liu745f2582014-02-19 14:07:26 +08002331 if (found) {
2332 spin_unlock_irqrestore(&device_domain_lock, flags);
2333 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002334 /* Caller must free the original domain */
2335 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002336 }
2337
David Woodhouseb718cd32014-03-09 13:11:33 -07002338 list_add(&info->link, &domain->devices);
2339 list_add(&info->global, &device_domain_list);
2340 if (dev)
2341 dev->archdata.iommu = info;
2342 spin_unlock_irqrestore(&device_domain_lock, flags);
2343
2344 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002345}
2346
Alex Williamson579305f2014-07-03 09:51:43 -06002347static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2348{
2349 *(u16 *)opaque = alias;
2350 return 0;
2351}
2352
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002353/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002354static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002355{
Alex Williamson579305f2014-07-03 09:51:43 -06002356 struct dmar_domain *domain, *tmp;
2357 struct intel_iommu *iommu;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002358 struct device_domain_info *info;
Alex Williamson579305f2014-07-03 09:51:43 -06002359 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002360 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002361 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002362
David Woodhouse146922e2014-03-09 15:44:17 -07002363 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002364 if (domain)
2365 return domain;
2366
David Woodhouse146922e2014-03-09 15:44:17 -07002367 iommu = device_to_iommu(dev, &bus, &devfn);
2368 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002369 return NULL;
2370
2371 if (dev_is_pci(dev)) {
2372 struct pci_dev *pdev = to_pci_dev(dev);
2373
2374 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2375
2376 spin_lock_irqsave(&device_domain_lock, flags);
2377 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2378 PCI_BUS_NUM(dma_alias),
2379 dma_alias & 0xff);
2380 if (info) {
2381 iommu = info->iommu;
2382 domain = info->domain;
2383 }
2384 spin_unlock_irqrestore(&device_domain_lock, flags);
2385
2386 /* DMA alias already has a domain, uses it */
2387 if (info)
2388 goto found_domain;
2389 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002390
David Woodhouse146922e2014-03-09 15:44:17 -07002391 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002392 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002393 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002394 return NULL;
Jiang Liu44bde612014-07-11 14:19:29 +08002395 domain->id = iommu_attach_domain(domain, iommu);
2396 if (domain->id < 0) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07002397 free_domain_mem(domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002398 return NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002399 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002400 domain_attach_iommu(domain, iommu);
Alex Williamson579305f2014-07-03 09:51:43 -06002401 if (domain_init(domain, gaw)) {
2402 domain_exit(domain);
2403 return NULL;
2404 }
2405
2406 /* register PCI DMA alias device */
2407 if (dev_is_pci(dev)) {
2408 tmp = dmar_insert_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2409 dma_alias & 0xff, NULL, domain);
2410
2411 if (!tmp || tmp != domain) {
2412 domain_exit(domain);
2413 domain = tmp;
2414 }
2415
David Woodhouseb718cd32014-03-09 13:11:33 -07002416 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002417 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002418 }
2419
2420found_domain:
Alex Williamson579305f2014-07-03 09:51:43 -06002421 tmp = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
2422
2423 if (!tmp || tmp != domain) {
2424 domain_exit(domain);
2425 domain = tmp;
2426 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002427
2428 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002429}
2430
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002431static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002432#define IDENTMAP_ALL 1
2433#define IDENTMAP_GFX 2
2434#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002435
David Woodhouseb2132032009-06-26 18:50:28 +01002436static int iommu_domain_identity_map(struct dmar_domain *domain,
2437 unsigned long long start,
2438 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002439{
David Woodhousec5395d52009-06-28 16:35:56 +01002440 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2441 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002442
David Woodhousec5395d52009-06-28 16:35:56 +01002443 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2444 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002445 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002446 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002447 }
2448
David Woodhousec5395d52009-06-28 16:35:56 +01002449 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
2450 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002451 /*
2452 * RMRR range might have overlap with physical memory range,
2453 * clear it first
2454 */
David Woodhousec5395d52009-06-28 16:35:56 +01002455 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002456
David Woodhousec5395d52009-06-28 16:35:56 +01002457 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2458 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002459 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002460}
2461
David Woodhouse0b9d9752014-03-09 15:48:15 -07002462static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002463 unsigned long long start,
2464 unsigned long long end)
2465{
2466 struct dmar_domain *domain;
2467 int ret;
2468
David Woodhouse0b9d9752014-03-09 15:48:15 -07002469 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002470 if (!domain)
2471 return -ENOMEM;
2472
David Woodhouse19943b02009-08-04 16:19:20 +01002473 /* For _hardware_ passthrough, don't bother. But for software
2474 passthrough, we do it anyway -- it may indicate a memory
2475 range which is reserved in E820, so which didn't get set
2476 up to start with in si_domain */
2477 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002478 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2479 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002480 return 0;
2481 }
2482
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002483 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2484 dev_name(dev), start, end);
2485
David Woodhouse5595b522009-12-02 09:21:55 +00002486 if (end < start) {
2487 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2488 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2489 dmi_get_system_info(DMI_BIOS_VENDOR),
2490 dmi_get_system_info(DMI_BIOS_VERSION),
2491 dmi_get_system_info(DMI_PRODUCT_VERSION));
2492 ret = -EIO;
2493 goto error;
2494 }
2495
David Woodhouse2ff729f2009-08-26 14:25:41 +01002496 if (end >> agaw_to_width(domain->agaw)) {
2497 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2498 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2499 agaw_to_width(domain->agaw),
2500 dmi_get_system_info(DMI_BIOS_VENDOR),
2501 dmi_get_system_info(DMI_BIOS_VERSION),
2502 dmi_get_system_info(DMI_PRODUCT_VERSION));
2503 ret = -EIO;
2504 goto error;
2505 }
David Woodhouse19943b02009-08-04 16:19:20 +01002506
David Woodhouseb2132032009-06-26 18:50:28 +01002507 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002508 if (ret)
2509 goto error;
2510
2511 /* context entry init */
Joerg Roedel28ccce02015-07-21 14:45:31 +02002512 ret = domain_context_mapping(domain, dev);
David Woodhouseb2132032009-06-26 18:50:28 +01002513 if (ret)
2514 goto error;
2515
2516 return 0;
2517
2518 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002519 domain_exit(domain);
2520 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002521}
2522
2523static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002524 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002525{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002526 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002527 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002528 return iommu_prepare_identity_map(dev, rmrr->base_address,
2529 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002530}
2531
Suresh Siddhad3f13812011-08-23 17:05:25 -07002532#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002533static inline void iommu_prepare_isa(void)
2534{
2535 struct pci_dev *pdev;
2536 int ret;
2537
2538 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2539 if (!pdev)
2540 return;
2541
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002542 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002543 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002544
2545 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002546 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002547
Yijing Wang9b27e822014-05-20 20:37:52 +08002548 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002549}
2550#else
2551static inline void iommu_prepare_isa(void)
2552{
2553 return;
2554}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002555#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002556
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002557static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002558
Matt Kraai071e1372009-08-23 22:30:22 -07002559static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002560{
2561 struct dmar_drhd_unit *drhd;
2562 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002563 int nid, ret = 0;
Jiang Liu44bde612014-07-11 14:19:29 +08002564 bool first = true;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002565
Jiang Liuab8dfe22014-07-11 14:19:27 +08002566 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002567 if (!si_domain)
2568 return -EFAULT;
2569
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002570 for_each_active_iommu(iommu, drhd) {
2571 ret = iommu_attach_domain(si_domain, iommu);
Jiang Liufb170fb2014-07-11 14:19:28 +08002572 if (ret < 0) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002573 domain_exit(si_domain);
2574 return -EFAULT;
Jiang Liu44bde612014-07-11 14:19:29 +08002575 } else if (first) {
2576 si_domain->id = ret;
2577 first = false;
2578 } else if (si_domain->id != ret) {
2579 domain_exit(si_domain);
2580 return -EFAULT;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002581 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002582 domain_attach_iommu(si_domain, iommu);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002583 }
2584
2585 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2586 domain_exit(si_domain);
2587 return -EFAULT;
2588 }
2589
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002590 pr_debug("Identity mapping domain is domain %d\n",
Jiang Liu9544c002014-01-06 14:18:13 +08002591 si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002592
David Woodhouse19943b02009-08-04 16:19:20 +01002593 if (hw)
2594 return 0;
2595
David Woodhousec7ab48d2009-06-26 19:10:36 +01002596 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002597 unsigned long start_pfn, end_pfn;
2598 int i;
2599
2600 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2601 ret = iommu_domain_identity_map(si_domain,
2602 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2603 if (ret)
2604 return ret;
2605 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002606 }
2607
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002608 return 0;
2609}
2610
David Woodhouse9b226622014-03-09 14:03:28 -07002611static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002612{
2613 struct device_domain_info *info;
2614
2615 if (likely(!iommu_identity_mapping))
2616 return 0;
2617
David Woodhouse9b226622014-03-09 14:03:28 -07002618 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002619 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2620 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002621
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002622 return 0;
2623}
2624
Joerg Roedel28ccce02015-07-21 14:45:31 +02002625static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002626{
David Woodhouse0ac72662014-03-09 13:19:22 -07002627 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002628 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002629 u8 bus, devfn;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002630 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002631
David Woodhouse5913c9b2014-03-09 16:27:31 -07002632 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002633 if (!iommu)
2634 return -ENODEV;
2635
David Woodhouse5913c9b2014-03-09 16:27:31 -07002636 ndomain = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002637 if (ndomain != domain)
2638 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002639
Joerg Roedel28ccce02015-07-21 14:45:31 +02002640 ret = domain_context_mapping(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002641 if (ret) {
David Woodhouse5913c9b2014-03-09 16:27:31 -07002642 domain_remove_one_dev_info(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002643 return ret;
2644 }
2645
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002646 return 0;
2647}
2648
David Woodhouse0b9d9752014-03-09 15:48:15 -07002649static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002650{
2651 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002652 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002653 int i;
2654
Jiang Liu0e242612014-02-19 14:07:34 +08002655 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002656 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002657 /*
2658 * Return TRUE if this RMRR contains the device that
2659 * is passed in.
2660 */
2661 for_each_active_dev_scope(rmrr->devices,
2662 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002663 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002664 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002665 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002666 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002667 }
Jiang Liu0e242612014-02-19 14:07:34 +08002668 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002669 return false;
2670}
2671
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002672/*
2673 * There are a couple cases where we need to restrict the functionality of
2674 * devices associated with RMRRs. The first is when evaluating a device for
2675 * identity mapping because problems exist when devices are moved in and out
2676 * of domains and their respective RMRR information is lost. This means that
2677 * a device with associated RMRRs will never be in a "passthrough" domain.
2678 * The second is use of the device through the IOMMU API. This interface
2679 * expects to have full control of the IOVA space for the device. We cannot
2680 * satisfy both the requirement that RMRR access is maintained and have an
2681 * unencumbered IOVA space. We also have no ability to quiesce the device's
2682 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2683 * We therefore prevent devices associated with an RMRR from participating in
2684 * the IOMMU API, which eliminates them from device assignment.
2685 *
2686 * In both cases we assume that PCI USB devices with RMRRs have them largely
2687 * for historical reasons and that the RMRR space is not actively used post
2688 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002689 *
2690 * The same exception is made for graphics devices, with the requirement that
2691 * any use of the RMRR regions will be torn down before assigning the device
2692 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002693 */
2694static bool device_is_rmrr_locked(struct device *dev)
2695{
2696 if (!device_has_rmrr(dev))
2697 return false;
2698
2699 if (dev_is_pci(dev)) {
2700 struct pci_dev *pdev = to_pci_dev(dev);
2701
David Woodhouse18436af2015-03-25 15:05:47 +00002702 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002703 return false;
2704 }
2705
2706 return true;
2707}
2708
David Woodhouse3bdb2592014-03-09 16:03:08 -07002709static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002710{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002711
David Woodhouse3bdb2592014-03-09 16:03:08 -07002712 if (dev_is_pci(dev)) {
2713 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002714
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002715 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002716 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002717
David Woodhouse3bdb2592014-03-09 16:03:08 -07002718 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2719 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002720
David Woodhouse3bdb2592014-03-09 16:03:08 -07002721 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2722 return 1;
2723
2724 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2725 return 0;
2726
2727 /*
2728 * We want to start off with all devices in the 1:1 domain, and
2729 * take them out later if we find they can't access all of memory.
2730 *
2731 * However, we can't do this for PCI devices behind bridges,
2732 * because all PCI devices behind the same bridge will end up
2733 * with the same source-id on their transactions.
2734 *
2735 * Practically speaking, we can't change things around for these
2736 * devices at run-time, because we can't be sure there'll be no
2737 * DMA transactions in flight for any of their siblings.
2738 *
2739 * So PCI devices (unless they're on the root bus) as well as
2740 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2741 * the 1:1 domain, just in _case_ one of their siblings turns out
2742 * not to be able to map all of memory.
2743 */
2744 if (!pci_is_pcie(pdev)) {
2745 if (!pci_is_root_bus(pdev->bus))
2746 return 0;
2747 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2748 return 0;
2749 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2750 return 0;
2751 } else {
2752 if (device_has_rmrr(dev))
2753 return 0;
2754 }
David Woodhouse6941af22009-07-04 18:24:27 +01002755
David Woodhouse3dfc8132009-07-04 19:11:08 +01002756 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002757 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002758 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002759 * take them out of the 1:1 domain later.
2760 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002761 if (!startup) {
2762 /*
2763 * If the device's dma_mask is less than the system's memory
2764 * size then this is not a candidate for identity mapping.
2765 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002766 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002767
David Woodhouse3bdb2592014-03-09 16:03:08 -07002768 if (dev->coherent_dma_mask &&
2769 dev->coherent_dma_mask < dma_mask)
2770 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002771
David Woodhouse3bdb2592014-03-09 16:03:08 -07002772 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002773 }
David Woodhouse6941af22009-07-04 18:24:27 +01002774
2775 return 1;
2776}
2777
David Woodhousecf04eee2014-03-21 16:49:04 +00002778static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2779{
2780 int ret;
2781
2782 if (!iommu_should_identity_map(dev, 1))
2783 return 0;
2784
Joerg Roedel28ccce02015-07-21 14:45:31 +02002785 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002786 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002787 pr_info("%s identity mapping for device %s\n",
2788 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002789 else if (ret == -ENODEV)
2790 /* device not associated with an iommu */
2791 ret = 0;
2792
2793 return ret;
2794}
2795
2796
Matt Kraai071e1372009-08-23 22:30:22 -07002797static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002798{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002799 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002800 struct dmar_drhd_unit *drhd;
2801 struct intel_iommu *iommu;
2802 struct device *dev;
2803 int i;
2804 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002805
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002806 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002807 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2808 if (ret)
2809 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002810 }
2811
David Woodhousecf04eee2014-03-21 16:49:04 +00002812 for_each_active_iommu(iommu, drhd)
2813 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2814 struct acpi_device_physical_node *pn;
2815 struct acpi_device *adev;
2816
2817 if (dev->bus != &acpi_bus_type)
2818 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002819
David Woodhousecf04eee2014-03-21 16:49:04 +00002820 adev= to_acpi_device(dev);
2821 mutex_lock(&adev->physical_node_lock);
2822 list_for_each_entry(pn, &adev->physical_node_list, node) {
2823 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2824 if (ret)
2825 break;
2826 }
2827 mutex_unlock(&adev->physical_node_lock);
2828 if (ret)
2829 return ret;
2830 }
2831
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002832 return 0;
2833}
2834
Jiang Liuffebeb42014-11-09 22:48:02 +08002835static void intel_iommu_init_qi(struct intel_iommu *iommu)
2836{
2837 /*
2838 * Start from the sane iommu hardware state.
2839 * If the queued invalidation is already initialized by us
2840 * (for example, while enabling interrupt-remapping) then
2841 * we got the things already rolling from a sane state.
2842 */
2843 if (!iommu->qi) {
2844 /*
2845 * Clear any previous faults.
2846 */
2847 dmar_fault(-1, iommu);
2848 /*
2849 * Disable queued invalidation if supported and already enabled
2850 * before OS handover.
2851 */
2852 dmar_disable_qi(iommu);
2853 }
2854
2855 if (dmar_enable_qi(iommu)) {
2856 /*
2857 * Queued Invalidate not enabled, use Register Based Invalidate
2858 */
2859 iommu->flush.flush_context = __iommu_flush_context;
2860 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002861 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002862 iommu->name);
2863 } else {
2864 iommu->flush.flush_context = qi_flush_context;
2865 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002866 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002867 }
2868}
2869
Joerg Roedel091d42e2015-06-12 11:56:10 +02002870static int copy_context_table(struct intel_iommu *iommu,
2871 struct root_entry *old_re,
2872 struct context_entry **tbl,
2873 int bus, bool ext)
2874{
2875 struct context_entry *old_ce = NULL, *new_ce = NULL, ce;
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002876 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002877 phys_addr_t old_ce_phys;
2878
2879 tbl_idx = ext ? bus * 2 : bus;
2880
2881 for (devfn = 0; devfn < 256; devfn++) {
2882 /* First calculate the correct index */
2883 idx = (ext ? devfn * 2 : devfn) % 256;
2884
2885 if (idx == 0) {
2886 /* First save what we may have and clean up */
2887 if (new_ce) {
2888 tbl[tbl_idx] = new_ce;
2889 __iommu_flush_cache(iommu, new_ce,
2890 VTD_PAGE_SIZE);
2891 pos = 1;
2892 }
2893
2894 if (old_ce)
2895 iounmap(old_ce);
2896
2897 ret = 0;
2898 if (devfn < 0x80)
2899 old_ce_phys = root_entry_lctp(old_re);
2900 else
2901 old_ce_phys = root_entry_uctp(old_re);
2902
2903 if (!old_ce_phys) {
2904 if (ext && devfn == 0) {
2905 /* No LCTP, try UCTP */
2906 devfn = 0x7f;
2907 continue;
2908 } else {
2909 goto out;
2910 }
2911 }
2912
2913 ret = -ENOMEM;
2914 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2915 if (!old_ce)
2916 goto out;
2917
2918 new_ce = alloc_pgtable_page(iommu->node);
2919 if (!new_ce)
2920 goto out_unmap;
2921
2922 ret = 0;
2923 }
2924
2925 /* Now copy the context entry */
2926 ce = old_ce[idx];
2927
Joerg Roedelcf484d02015-06-12 12:21:46 +02002928 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002929 continue;
2930
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002931 did = context_domain_id(&ce);
2932 if (did >= 0 && did < cap_ndoms(iommu->cap))
2933 set_bit(did, iommu->domain_ids);
2934
Joerg Roedelcf484d02015-06-12 12:21:46 +02002935 /*
2936 * We need a marker for copied context entries. This
2937 * marker needs to work for the old format as well as
2938 * for extended context entries.
2939 *
2940 * Bit 67 of the context entry is used. In the old
2941 * format this bit is available to software, in the
2942 * extended format it is the PGE bit, but PGE is ignored
2943 * by HW if PASIDs are disabled (and thus still
2944 * available).
2945 *
2946 * So disable PASIDs first and then mark the entry
2947 * copied. This means that we don't copy PASID
2948 * translations from the old kernel, but this is fine as
2949 * faults there are not fatal.
2950 */
2951 context_clear_pasid_enable(&ce);
2952 context_set_copied(&ce);
2953
Joerg Roedel091d42e2015-06-12 11:56:10 +02002954 new_ce[idx] = ce;
2955 }
2956
2957 tbl[tbl_idx + pos] = new_ce;
2958
2959 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2960
2961out_unmap:
2962 iounmap(old_ce);
2963
2964out:
2965 return ret;
2966}
2967
2968static int copy_translation_tables(struct intel_iommu *iommu)
2969{
2970 struct context_entry **ctxt_tbls;
2971 struct root_entry *old_rt;
2972 phys_addr_t old_rt_phys;
2973 int ctxt_table_entries;
2974 unsigned long flags;
2975 u64 rtaddr_reg;
2976 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002977 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002978
2979 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2980 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002981 new_ext = !!ecap_ecs(iommu->ecap);
2982
2983 /*
2984 * The RTT bit can only be changed when translation is disabled,
2985 * but disabling translation means to open a window for data
2986 * corruption. So bail out and don't copy anything if we would
2987 * have to change the bit.
2988 */
2989 if (new_ext != ext)
2990 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002991
2992 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2993 if (!old_rt_phys)
2994 return -EINVAL;
2995
2996 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2997 if (!old_rt)
2998 return -ENOMEM;
2999
3000 /* This is too big for the stack - allocate it from slab */
3001 ctxt_table_entries = ext ? 512 : 256;
3002 ret = -ENOMEM;
3003 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
3004 if (!ctxt_tbls)
3005 goto out_unmap;
3006
3007 for (bus = 0; bus < 256; bus++) {
3008 ret = copy_context_table(iommu, &old_rt[bus],
3009 ctxt_tbls, bus, ext);
3010 if (ret) {
3011 pr_err("%s: Failed to copy context table for bus %d\n",
3012 iommu->name, bus);
3013 continue;
3014 }
3015 }
3016
3017 spin_lock_irqsave(&iommu->lock, flags);
3018
3019 /* Context tables are copied, now write them to the root_entry table */
3020 for (bus = 0; bus < 256; bus++) {
3021 int idx = ext ? bus * 2 : bus;
3022 u64 val;
3023
3024 if (ctxt_tbls[idx]) {
3025 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3026 iommu->root_entry[bus].lo = val;
3027 }
3028
3029 if (!ext || !ctxt_tbls[idx + 1])
3030 continue;
3031
3032 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3033 iommu->root_entry[bus].hi = val;
3034 }
3035
3036 spin_unlock_irqrestore(&iommu->lock, flags);
3037
3038 kfree(ctxt_tbls);
3039
3040 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3041
3042 ret = 0;
3043
3044out_unmap:
3045 iounmap(old_rt);
3046
3047 return ret;
3048}
3049
Joseph Cihulab7792602011-05-03 00:08:37 -07003050static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003051{
3052 struct dmar_drhd_unit *drhd;
3053 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003054 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003055 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003056 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003057 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003058
3059 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003060 * for each drhd
3061 * allocate root
3062 * initialize and program root entry to not present
3063 * endfor
3064 */
3065 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003066 /*
3067 * lock not needed as this is only incremented in the single
3068 * threaded kernel __init code path all other access are read
3069 * only
3070 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003071 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003072 g_num_of_iommus++;
3073 continue;
3074 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003075 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003076 }
3077
Jiang Liuffebeb42014-11-09 22:48:02 +08003078 /* Preallocate enough resources for IOMMU hot-addition */
3079 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3080 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3081
Weidong Hand9630fe2008-12-08 11:06:32 +08003082 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3083 GFP_KERNEL);
3084 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003085 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003086 ret = -ENOMEM;
3087 goto error;
3088 }
3089
mark gross80b20dd2008-04-18 13:53:58 -07003090 deferred_flush = kzalloc(g_num_of_iommus *
3091 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3092 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003093 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003094 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003095 }
3096
Jiang Liu7c919772014-01-06 14:18:18 +08003097 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003098 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003099
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003100 intel_iommu_init_qi(iommu);
3101
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003102 ret = iommu_init_domains(iommu);
3103 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003104 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003105
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003106 init_translation_status(iommu);
3107
Joerg Roedel091d42e2015-06-12 11:56:10 +02003108 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3109 iommu_disable_translation(iommu);
3110 clear_translation_pre_enabled(iommu);
3111 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3112 iommu->name);
3113 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003114
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003115 /*
3116 * TBD:
3117 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003118 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003119 */
3120 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003121 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003122 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003123
Joerg Roedel091d42e2015-06-12 11:56:10 +02003124 if (translation_pre_enabled(iommu)) {
3125 pr_info("Translation already enabled - trying to copy translation structures\n");
3126
3127 ret = copy_translation_tables(iommu);
3128 if (ret) {
3129 /*
3130 * We found the IOMMU with translation
3131 * enabled - but failed to copy over the
3132 * old root-entry table. Try to proceed
3133 * by disabling translation now and
3134 * allocating a clean root-entry table.
3135 * This might cause DMAR faults, but
3136 * probably the dump will still succeed.
3137 */
3138 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3139 iommu->name);
3140 iommu_disable_translation(iommu);
3141 clear_translation_pre_enabled(iommu);
3142 } else {
3143 pr_info("Copied translation tables from previous kernel for %s\n",
3144 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003145 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003146 }
3147 }
3148
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003149 iommu_flush_write_buffer(iommu);
3150 iommu_set_root_entry(iommu);
3151 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3152 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3153
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003154 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003155 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003156 }
3157
David Woodhouse19943b02009-08-04 16:19:20 +01003158 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003159 iommu_identity_mapping |= IDENTMAP_ALL;
3160
Suresh Siddhad3f13812011-08-23 17:05:25 -07003161#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003162 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003163#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003164
Joerg Roedel86080cc2015-06-12 12:27:16 +02003165 if (iommu_identity_mapping) {
3166 ret = si_domain_init(hw_pass_through);
3167 if (ret)
3168 goto free_iommu;
3169 }
3170
David Woodhousee0fc7e02009-09-30 09:12:17 -07003171 check_tylersburg_isoch();
3172
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003173 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003174 * If we copied translations from a previous kernel in the kdump
3175 * case, we can not assign the devices to domains now, as that
3176 * would eliminate the old mappings. So skip this part and defer
3177 * the assignment to device driver initialization time.
3178 */
3179 if (copied_tables)
3180 goto domains_done;
3181
3182 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003183 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003184 * identity mappings for rmrr, gfx, and isa and may fall back to static
3185 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003186 */
David Woodhouse19943b02009-08-04 16:19:20 +01003187 if (iommu_identity_mapping) {
3188 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3189 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003190 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003191 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003192 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003193 }
David Woodhouse19943b02009-08-04 16:19:20 +01003194 /*
3195 * For each rmrr
3196 * for each dev attached to rmrr
3197 * do
3198 * locate drhd for dev, alloc domain for dev
3199 * allocate free domain
3200 * allocate page table entries for rmrr
3201 * if context not allocated for bus
3202 * allocate and init context
3203 * set present in root table for this bus
3204 * init context with domain, translation etc
3205 * endfor
3206 * endfor
3207 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003208 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003209 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003210 /* some BIOS lists non-exist devices in DMAR table. */
3211 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003212 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003213 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003214 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003215 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003216 }
3217 }
3218
3219 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003220
Joerg Roedela87f4912015-06-12 12:32:54 +02003221domains_done:
3222
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003223 /*
3224 * for each drhd
3225 * enable fault log
3226 * global invalidate context cache
3227 * global invalidate iotlb
3228 * enable translation
3229 */
Jiang Liu7c919772014-01-06 14:18:18 +08003230 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003231 if (drhd->ignored) {
3232 /*
3233 * we always have to disable PMRs or DMA may fail on
3234 * this device
3235 */
3236 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003237 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003238 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003239 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003240
3241 iommu_flush_write_buffer(iommu);
3242
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003243 ret = dmar_set_interrupt(iommu);
3244 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003245 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003246
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003247 if (!translation_pre_enabled(iommu))
3248 iommu_enable_translation(iommu);
3249
David Woodhouseb94996c2009-09-19 15:28:12 -07003250 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003251 }
3252
3253 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003254
3255free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003256 for_each_active_iommu(iommu, drhd) {
3257 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003258 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003259 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003260 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003261free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003262 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003263error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003264 return ret;
3265}
3266
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003267/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003268static struct iova *intel_alloc_iova(struct device *dev,
3269 struct dmar_domain *domain,
3270 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003271{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003272 struct iova *iova = NULL;
3273
David Woodhouse875764d2009-06-28 21:20:51 +01003274 /* Restrict dma_mask to the width that the iommu can handle */
3275 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
3276
3277 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003278 /*
3279 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003280 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003281 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003282 */
David Woodhouse875764d2009-06-28 21:20:51 +01003283 iova = alloc_iova(&domain->iovad, nrpages,
3284 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3285 if (iova)
3286 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003287 }
David Woodhouse875764d2009-06-28 21:20:51 +01003288 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3289 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003290 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003291 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003292 return NULL;
3293 }
3294
3295 return iova;
3296}
3297
David Woodhoused4b709f2014-03-09 16:07:40 -07003298static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003299{
3300 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003301 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003302
David Woodhoused4b709f2014-03-09 16:07:40 -07003303 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003304 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003305 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003306 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003307 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003308 }
3309
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003310 /* make sure context mapping is ok */
David Woodhoused4b709f2014-03-09 16:07:40 -07003311 if (unlikely(!domain_context_mapped(dev))) {
Joerg Roedel28ccce02015-07-21 14:45:31 +02003312 ret = domain_context_mapping(domain, dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003313 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003314 pr_err("Domain context map for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003315 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003316 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003317 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003318 }
3319
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003320 return domain;
3321}
3322
David Woodhoused4b709f2014-03-09 16:07:40 -07003323static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003324{
3325 struct device_domain_info *info;
3326
3327 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003328 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003329 if (likely(info))
3330 return info->domain;
3331
3332 return __get_valid_domain_for_dev(dev);
3333}
3334
David Woodhouseecb509e2014-03-09 16:29:55 -07003335/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003336static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003337{
3338 int found;
3339
David Woodhouse3d891942014-03-06 15:59:26 +00003340 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003341 return 1;
3342
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003343 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003344 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003345
David Woodhouse9b226622014-03-09 14:03:28 -07003346 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003347 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003348 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003349 return 1;
3350 else {
3351 /*
3352 * 32 bit DMA is removed from si_domain and fall back
3353 * to non-identity mapping.
3354 */
David Woodhousebf9c9ed2014-03-09 16:19:13 -07003355 domain_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003356 pr_info("32bit %s uses non-identity mapping\n",
3357 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003358 return 0;
3359 }
3360 } else {
3361 /*
3362 * In case of a detached 64 bit DMA device from vm, the device
3363 * is put into si_domain for identity mapping.
3364 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003365 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003366 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003367 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003368 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003369 pr_info("64bit %s uses identity mapping\n",
3370 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003371 return 1;
3372 }
3373 }
3374 }
3375
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003376 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003377}
3378
David Woodhouse5040a912014-03-09 16:14:00 -07003379static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003380 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003381{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003382 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003383 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003384 struct iova *iova;
3385 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003386 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003387 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003388 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003389
3390 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003391
David Woodhouse5040a912014-03-09 16:14:00 -07003392 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003393 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003394
David Woodhouse5040a912014-03-09 16:14:00 -07003395 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003396 if (!domain)
3397 return 0;
3398
Weidong Han8c11e792008-12-08 15:29:22 +08003399 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003400 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003401
David Woodhouse5040a912014-03-09 16:14:00 -07003402 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003403 if (!iova)
3404 goto error;
3405
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003406 /*
3407 * Check if DMAR supports zero-length reads on write only
3408 * mappings..
3409 */
3410 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003411 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003412 prot |= DMA_PTE_READ;
3413 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3414 prot |= DMA_PTE_WRITE;
3415 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003416 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003417 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003418 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003419 * is not a big problem
3420 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003421 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003422 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003423 if (ret)
3424 goto error;
3425
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003426 /* it's a non-present to present mapping. Only flush if caching mode */
3427 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003428 iommu_flush_iotlb_psi(iommu, domain,
3429 mm_to_dma_pfn(iova->pfn_lo),
3430 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003431 else
Weidong Han8c11e792008-12-08 15:29:22 +08003432 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003433
David Woodhouse03d6a242009-06-28 15:33:46 +01003434 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3435 start_paddr += paddr & ~PAGE_MASK;
3436 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003437
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003438error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003439 if (iova)
3440 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003441 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003442 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003443 return 0;
3444}
3445
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003446static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3447 unsigned long offset, size_t size,
3448 enum dma_data_direction dir,
3449 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003450{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003451 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003452 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003453}
3454
mark gross5e0d2a62008-03-04 15:22:08 -08003455static void flush_unmaps(void)
3456{
mark gross80b20dd2008-04-18 13:53:58 -07003457 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003458
mark gross5e0d2a62008-03-04 15:22:08 -08003459 timer_on = 0;
3460
3461 /* just flush them all */
3462 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003463 struct intel_iommu *iommu = g_iommus[i];
3464 if (!iommu)
3465 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003466
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003467 if (!deferred_flush[i].next)
3468 continue;
3469
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003470 /* In caching mode, global flushes turn emulation expensive */
3471 if (!cap_caching_mode(iommu->cap))
3472 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003473 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003474 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003475 unsigned long mask;
3476 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003477 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003478
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003479 /* On real hardware multiple invalidations are expensive */
3480 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003481 iommu_flush_iotlb_psi(iommu, domain,
Jiang Liua156ef92014-07-11 14:19:36 +08003482 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003483 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003484 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003485 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003486 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3487 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3488 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003489 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003490 if (deferred_flush[i].freelist[j])
3491 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003492 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003493 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003494 }
3495
mark gross5e0d2a62008-03-04 15:22:08 -08003496 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003497}
3498
3499static void flush_unmaps_timeout(unsigned long data)
3500{
mark gross80b20dd2008-04-18 13:53:58 -07003501 unsigned long flags;
3502
3503 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003504 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003505 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003506}
3507
David Woodhouseea8ea462014-03-05 17:09:32 +00003508static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003509{
3510 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003511 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003512 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003513
3514 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003515 if (list_size == HIGH_WATER_MARK)
3516 flush_unmaps();
3517
Weidong Han8c11e792008-12-08 15:29:22 +08003518 iommu = domain_get_iommu(dom);
3519 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003520
mark gross80b20dd2008-04-18 13:53:58 -07003521 next = deferred_flush[iommu_id].next;
3522 deferred_flush[iommu_id].domain[next] = dom;
3523 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003524 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003525 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003526
3527 if (!timer_on) {
3528 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3529 timer_on = 1;
3530 }
3531 list_size++;
3532 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3533}
3534
Jiang Liud41a4ad2014-07-11 14:19:34 +08003535static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003536{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003537 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003538 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003539 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003540 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003541 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003542
David Woodhouse73676832009-07-04 14:08:36 +01003543 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003544 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003545
David Woodhouse1525a292014-03-06 16:19:30 +00003546 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003547 BUG_ON(!domain);
3548
Weidong Han8c11e792008-12-08 15:29:22 +08003549 iommu = domain_get_iommu(domain);
3550
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003551 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003552 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3553 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003554 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003555
David Woodhoused794dc92009-06-28 00:27:49 +01003556 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3557 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003558
David Woodhoused794dc92009-06-28 00:27:49 +01003559 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003560 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003561
David Woodhouseea8ea462014-03-05 17:09:32 +00003562 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003563
mark gross5e0d2a62008-03-04 15:22:08 -08003564 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003565 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003566 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003567 /* free iova */
3568 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003569 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003570 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003571 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003572 /*
3573 * queue up the release of the unmap to save the 1/6th of the
3574 * cpu used up by the iotlb flush operation...
3575 */
mark gross5e0d2a62008-03-04 15:22:08 -08003576 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003577}
3578
Jiang Liud41a4ad2014-07-11 14:19:34 +08003579static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3580 size_t size, enum dma_data_direction dir,
3581 struct dma_attrs *attrs)
3582{
3583 intel_unmap(dev, dev_addr);
3584}
3585
David Woodhouse5040a912014-03-09 16:14:00 -07003586static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003587 dma_addr_t *dma_handle, gfp_t flags,
3588 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003589{
Akinobu Mita36746432014-06-04 16:06:51 -07003590 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003591 int order;
3592
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003593 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003594 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003595
David Woodhouse5040a912014-03-09 16:14:00 -07003596 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003597 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003598 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3599 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003600 flags |= GFP_DMA;
3601 else
3602 flags |= GFP_DMA32;
3603 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003604
Akinobu Mita36746432014-06-04 16:06:51 -07003605 if (flags & __GFP_WAIT) {
3606 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003607
Akinobu Mita36746432014-06-04 16:06:51 -07003608 page = dma_alloc_from_contiguous(dev, count, order);
3609 if (page && iommu_no_mapping(dev) &&
3610 page_to_phys(page) + size > dev->coherent_dma_mask) {
3611 dma_release_from_contiguous(dev, page, count);
3612 page = NULL;
3613 }
3614 }
3615
3616 if (!page)
3617 page = alloc_pages(flags, order);
3618 if (!page)
3619 return NULL;
3620 memset(page_address(page), 0, size);
3621
3622 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003623 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003624 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003625 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003626 return page_address(page);
3627 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3628 __free_pages(page, order);
3629
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003630 return NULL;
3631}
3632
David Woodhouse5040a912014-03-09 16:14:00 -07003633static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003634 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003635{
3636 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003637 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003638
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003639 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003640 order = get_order(size);
3641
Jiang Liud41a4ad2014-07-11 14:19:34 +08003642 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003643 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3644 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003645}
3646
David Woodhouse5040a912014-03-09 16:14:00 -07003647static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003648 int nelems, enum dma_data_direction dir,
3649 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003650{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003651 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003652}
3653
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003654static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003655 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003656{
3657 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003658 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003659
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003660 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003661 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003662 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003663 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003664 }
3665 return nelems;
3666}
3667
David Woodhouse5040a912014-03-09 16:14:00 -07003668static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003669 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003670{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003671 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003672 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003673 size_t size = 0;
3674 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003675 struct iova *iova = NULL;
3676 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003677 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003678 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003679 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003680
3681 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003682 if (iommu_no_mapping(dev))
3683 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003684
David Woodhouse5040a912014-03-09 16:14:00 -07003685 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003686 if (!domain)
3687 return 0;
3688
Weidong Han8c11e792008-12-08 15:29:22 +08003689 iommu = domain_get_iommu(domain);
3690
David Woodhouseb536d242009-06-28 14:49:31 +01003691 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003692 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003693
David Woodhouse5040a912014-03-09 16:14:00 -07003694 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3695 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003696 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003697 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003698 return 0;
3699 }
3700
3701 /*
3702 * Check if DMAR supports zero-length reads on write only
3703 * mappings..
3704 */
3705 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003706 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003707 prot |= DMA_PTE_READ;
3708 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3709 prot |= DMA_PTE_WRITE;
3710
David Woodhouseb536d242009-06-28 14:49:31 +01003711 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003712
Fenghua Yuf5329592009-08-04 15:09:37 -07003713 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003714 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003715 dma_pte_free_pagetable(domain, start_vpfn,
3716 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003717 __free_iova(&domain->iovad, iova);
3718 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003719 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003720
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003721 /* it's a non-present to present mapping. Only flush if caching mode */
3722 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003723 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003724 else
Weidong Han8c11e792008-12-08 15:29:22 +08003725 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003726
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003727 return nelems;
3728}
3729
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003730static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3731{
3732 return !dma_addr;
3733}
3734
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003735struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003736 .alloc = intel_alloc_coherent,
3737 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003738 .map_sg = intel_map_sg,
3739 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003740 .map_page = intel_map_page,
3741 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003742 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003743};
3744
3745static inline int iommu_domain_cache_init(void)
3746{
3747 int ret = 0;
3748
3749 iommu_domain_cache = kmem_cache_create("iommu_domain",
3750 sizeof(struct dmar_domain),
3751 0,
3752 SLAB_HWCACHE_ALIGN,
3753
3754 NULL);
3755 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003756 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003757 ret = -ENOMEM;
3758 }
3759
3760 return ret;
3761}
3762
3763static inline int iommu_devinfo_cache_init(void)
3764{
3765 int ret = 0;
3766
3767 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3768 sizeof(struct device_domain_info),
3769 0,
3770 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003771 NULL);
3772 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003773 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003774 ret = -ENOMEM;
3775 }
3776
3777 return ret;
3778}
3779
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003780static int __init iommu_init_mempool(void)
3781{
3782 int ret;
3783 ret = iommu_iova_cache_init();
3784 if (ret)
3785 return ret;
3786
3787 ret = iommu_domain_cache_init();
3788 if (ret)
3789 goto domain_error;
3790
3791 ret = iommu_devinfo_cache_init();
3792 if (!ret)
3793 return ret;
3794
3795 kmem_cache_destroy(iommu_domain_cache);
3796domain_error:
Robin Murphy85b45452015-01-12 17:51:14 +00003797 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003798
3799 return -ENOMEM;
3800}
3801
3802static void __init iommu_exit_mempool(void)
3803{
3804 kmem_cache_destroy(iommu_devinfo_cache);
3805 kmem_cache_destroy(iommu_domain_cache);
Robin Murphy85b45452015-01-12 17:51:14 +00003806 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003807}
3808
Dan Williams556ab452010-07-23 15:47:56 -07003809static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3810{
3811 struct dmar_drhd_unit *drhd;
3812 u32 vtbar;
3813 int rc;
3814
3815 /* We know that this device on this chipset has its own IOMMU.
3816 * If we find it under a different IOMMU, then the BIOS is lying
3817 * to us. Hope that the IOMMU for this device is actually
3818 * disabled, and it needs no translation...
3819 */
3820 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3821 if (rc) {
3822 /* "can't" happen */
3823 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3824 return;
3825 }
3826 vtbar &= 0xffff0000;
3827
3828 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3829 drhd = dmar_find_matched_drhd_unit(pdev);
3830 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3831 TAINT_FIRMWARE_WORKAROUND,
3832 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3833 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3834}
3835DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3836
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003837static void __init init_no_remapping_devices(void)
3838{
3839 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003840 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003841 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003842
3843 for_each_drhd_unit(drhd) {
3844 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003845 for_each_active_dev_scope(drhd->devices,
3846 drhd->devices_cnt, i, dev)
3847 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003848 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003849 if (i == drhd->devices_cnt)
3850 drhd->ignored = 1;
3851 }
3852 }
3853
Jiang Liu7c919772014-01-06 14:18:18 +08003854 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003855 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003856 continue;
3857
Jiang Liub683b232014-02-19 14:07:32 +08003858 for_each_active_dev_scope(drhd->devices,
3859 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003860 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003861 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003862 if (i < drhd->devices_cnt)
3863 continue;
3864
David Woodhousec0771df2011-10-14 20:59:46 +01003865 /* This IOMMU has *only* gfx devices. Either bypass it or
3866 set the gfx_mapped flag, as appropriate */
3867 if (dmar_map_gfx) {
3868 intel_iommu_gfx_mapped = 1;
3869 } else {
3870 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003871 for_each_active_dev_scope(drhd->devices,
3872 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003873 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003874 }
3875 }
3876}
3877
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003878#ifdef CONFIG_SUSPEND
3879static int init_iommu_hw(void)
3880{
3881 struct dmar_drhd_unit *drhd;
3882 struct intel_iommu *iommu = NULL;
3883
3884 for_each_active_iommu(iommu, drhd)
3885 if (iommu->qi)
3886 dmar_reenable_qi(iommu);
3887
Joseph Cihulab7792602011-05-03 00:08:37 -07003888 for_each_iommu(iommu, drhd) {
3889 if (drhd->ignored) {
3890 /*
3891 * we always have to disable PMRs or DMA may fail on
3892 * this device
3893 */
3894 if (force_on)
3895 iommu_disable_protect_mem_regions(iommu);
3896 continue;
3897 }
3898
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003899 iommu_flush_write_buffer(iommu);
3900
3901 iommu_set_root_entry(iommu);
3902
3903 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003904 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003905 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3906 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003907 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003908 }
3909
3910 return 0;
3911}
3912
3913static void iommu_flush_all(void)
3914{
3915 struct dmar_drhd_unit *drhd;
3916 struct intel_iommu *iommu;
3917
3918 for_each_active_iommu(iommu, drhd) {
3919 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003920 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003921 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003922 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003923 }
3924}
3925
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003926static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003927{
3928 struct dmar_drhd_unit *drhd;
3929 struct intel_iommu *iommu = NULL;
3930 unsigned long flag;
3931
3932 for_each_active_iommu(iommu, drhd) {
3933 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3934 GFP_ATOMIC);
3935 if (!iommu->iommu_state)
3936 goto nomem;
3937 }
3938
3939 iommu_flush_all();
3940
3941 for_each_active_iommu(iommu, drhd) {
3942 iommu_disable_translation(iommu);
3943
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003944 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003945
3946 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3947 readl(iommu->reg + DMAR_FECTL_REG);
3948 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3949 readl(iommu->reg + DMAR_FEDATA_REG);
3950 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3951 readl(iommu->reg + DMAR_FEADDR_REG);
3952 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3953 readl(iommu->reg + DMAR_FEUADDR_REG);
3954
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003955 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003956 }
3957 return 0;
3958
3959nomem:
3960 for_each_active_iommu(iommu, drhd)
3961 kfree(iommu->iommu_state);
3962
3963 return -ENOMEM;
3964}
3965
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003966static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003967{
3968 struct dmar_drhd_unit *drhd;
3969 struct intel_iommu *iommu = NULL;
3970 unsigned long flag;
3971
3972 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003973 if (force_on)
3974 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3975 else
3976 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003977 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003978 }
3979
3980 for_each_active_iommu(iommu, drhd) {
3981
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003982 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003983
3984 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3985 iommu->reg + DMAR_FECTL_REG);
3986 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3987 iommu->reg + DMAR_FEDATA_REG);
3988 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3989 iommu->reg + DMAR_FEADDR_REG);
3990 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3991 iommu->reg + DMAR_FEUADDR_REG);
3992
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003993 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003994 }
3995
3996 for_each_active_iommu(iommu, drhd)
3997 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003998}
3999
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004000static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004001 .resume = iommu_resume,
4002 .suspend = iommu_suspend,
4003};
4004
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004005static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004006{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004007 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004008}
4009
4010#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004011static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004012#endif /* CONFIG_PM */
4013
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004014
Jiang Liuc2a0b532014-11-09 22:47:56 +08004015int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004016{
4017 struct acpi_dmar_reserved_memory *rmrr;
4018 struct dmar_rmrr_unit *rmrru;
4019
4020 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4021 if (!rmrru)
4022 return -ENOMEM;
4023
4024 rmrru->hdr = header;
4025 rmrr = (struct acpi_dmar_reserved_memory *)header;
4026 rmrru->base_address = rmrr->base_address;
4027 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08004028 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4029 ((void *)rmrr) + rmrr->header.length,
4030 &rmrru->devices_cnt);
4031 if (rmrru->devices_cnt && rmrru->devices == NULL) {
4032 kfree(rmrru);
4033 return -ENOMEM;
4034 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004035
Jiang Liu2e455282014-02-19 14:07:36 +08004036 list_add(&rmrru->list, &dmar_rmrr_units);
4037
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004038 return 0;
4039}
4040
Jiang Liu6b197242014-11-09 22:47:58 +08004041static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4042{
4043 struct dmar_atsr_unit *atsru;
4044 struct acpi_dmar_atsr *tmp;
4045
4046 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4047 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4048 if (atsr->segment != tmp->segment)
4049 continue;
4050 if (atsr->header.length != tmp->header.length)
4051 continue;
4052 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4053 return atsru;
4054 }
4055
4056 return NULL;
4057}
4058
4059int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004060{
4061 struct acpi_dmar_atsr *atsr;
4062 struct dmar_atsr_unit *atsru;
4063
Jiang Liu6b197242014-11-09 22:47:58 +08004064 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4065 return 0;
4066
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004067 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004068 atsru = dmar_find_atsr(atsr);
4069 if (atsru)
4070 return 0;
4071
4072 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004073 if (!atsru)
4074 return -ENOMEM;
4075
Jiang Liu6b197242014-11-09 22:47:58 +08004076 /*
4077 * If memory is allocated from slab by ACPI _DSM method, we need to
4078 * copy the memory content because the memory buffer will be freed
4079 * on return.
4080 */
4081 atsru->hdr = (void *)(atsru + 1);
4082 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004083 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004084 if (!atsru->include_all) {
4085 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4086 (void *)atsr + atsr->header.length,
4087 &atsru->devices_cnt);
4088 if (atsru->devices_cnt && atsru->devices == NULL) {
4089 kfree(atsru);
4090 return -ENOMEM;
4091 }
4092 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004093
Jiang Liu0e242612014-02-19 14:07:34 +08004094 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004095
4096 return 0;
4097}
4098
Jiang Liu9bdc5312014-01-06 14:18:27 +08004099static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4100{
4101 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4102 kfree(atsru);
4103}
4104
Jiang Liu6b197242014-11-09 22:47:58 +08004105int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4106{
4107 struct acpi_dmar_atsr *atsr;
4108 struct dmar_atsr_unit *atsru;
4109
4110 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4111 atsru = dmar_find_atsr(atsr);
4112 if (atsru) {
4113 list_del_rcu(&atsru->list);
4114 synchronize_rcu();
4115 intel_iommu_free_atsr(atsru);
4116 }
4117
4118 return 0;
4119}
4120
4121int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4122{
4123 int i;
4124 struct device *dev;
4125 struct acpi_dmar_atsr *atsr;
4126 struct dmar_atsr_unit *atsru;
4127
4128 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4129 atsru = dmar_find_atsr(atsr);
4130 if (!atsru)
4131 return 0;
4132
4133 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4134 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4135 i, dev)
4136 return -EBUSY;
4137
4138 return 0;
4139}
4140
Jiang Liuffebeb42014-11-09 22:48:02 +08004141static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4142{
4143 int sp, ret = 0;
4144 struct intel_iommu *iommu = dmaru->iommu;
4145
4146 if (g_iommus[iommu->seq_id])
4147 return 0;
4148
4149 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004150 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004151 iommu->name);
4152 return -ENXIO;
4153 }
4154 if (!ecap_sc_support(iommu->ecap) &&
4155 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004156 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004157 iommu->name);
4158 return -ENXIO;
4159 }
4160 sp = domain_update_iommu_superpage(iommu) - 1;
4161 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004162 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004163 iommu->name);
4164 return -ENXIO;
4165 }
4166
4167 /*
4168 * Disable translation if already enabled prior to OS handover.
4169 */
4170 if (iommu->gcmd & DMA_GCMD_TE)
4171 iommu_disable_translation(iommu);
4172
4173 g_iommus[iommu->seq_id] = iommu;
4174 ret = iommu_init_domains(iommu);
4175 if (ret == 0)
4176 ret = iommu_alloc_root_entry(iommu);
4177 if (ret)
4178 goto out;
4179
4180 if (dmaru->ignored) {
4181 /*
4182 * we always have to disable PMRs or DMA may fail on this device
4183 */
4184 if (force_on)
4185 iommu_disable_protect_mem_regions(iommu);
4186 return 0;
4187 }
4188
4189 intel_iommu_init_qi(iommu);
4190 iommu_flush_write_buffer(iommu);
4191 ret = dmar_set_interrupt(iommu);
4192 if (ret)
4193 goto disable_iommu;
4194
4195 iommu_set_root_entry(iommu);
4196 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4197 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4198 iommu_enable_translation(iommu);
4199
4200 if (si_domain) {
4201 ret = iommu_attach_domain(si_domain, iommu);
4202 if (ret < 0 || si_domain->id != ret)
4203 goto disable_iommu;
4204 domain_attach_iommu(si_domain, iommu);
4205 }
4206
4207 iommu_disable_protect_mem_regions(iommu);
4208 return 0;
4209
4210disable_iommu:
4211 disable_dmar_iommu(iommu);
4212out:
4213 free_dmar_iommu(iommu);
4214 return ret;
4215}
4216
Jiang Liu6b197242014-11-09 22:47:58 +08004217int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4218{
Jiang Liuffebeb42014-11-09 22:48:02 +08004219 int ret = 0;
4220 struct intel_iommu *iommu = dmaru->iommu;
4221
4222 if (!intel_iommu_enabled)
4223 return 0;
4224 if (iommu == NULL)
4225 return -EINVAL;
4226
4227 if (insert) {
4228 ret = intel_iommu_add(dmaru);
4229 } else {
4230 disable_dmar_iommu(iommu);
4231 free_dmar_iommu(iommu);
4232 }
4233
4234 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004235}
4236
Jiang Liu9bdc5312014-01-06 14:18:27 +08004237static void intel_iommu_free_dmars(void)
4238{
4239 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4240 struct dmar_atsr_unit *atsru, *atsr_n;
4241
4242 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4243 list_del(&rmrru->list);
4244 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4245 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004246 }
4247
Jiang Liu9bdc5312014-01-06 14:18:27 +08004248 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4249 list_del(&atsru->list);
4250 intel_iommu_free_atsr(atsru);
4251 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004252}
4253
4254int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4255{
Jiang Liub683b232014-02-19 14:07:32 +08004256 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004257 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004258 struct pci_dev *bridge = NULL;
4259 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004260 struct acpi_dmar_atsr *atsr;
4261 struct dmar_atsr_unit *atsru;
4262
4263 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004264 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004265 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004266 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004267 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004268 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004269 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004270 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004271 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08004272 if (!bridge)
4273 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004274
Jiang Liu0e242612014-02-19 14:07:34 +08004275 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004276 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4277 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4278 if (atsr->segment != pci_domain_nr(dev->bus))
4279 continue;
4280
Jiang Liub683b232014-02-19 14:07:32 +08004281 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004282 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004283 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004284
4285 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004286 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004287 }
Jiang Liub683b232014-02-19 14:07:32 +08004288 ret = 0;
4289out:
Jiang Liu0e242612014-02-19 14:07:34 +08004290 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004291
Jiang Liub683b232014-02-19 14:07:32 +08004292 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004293}
4294
Jiang Liu59ce0512014-02-19 14:07:35 +08004295int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4296{
4297 int ret = 0;
4298 struct dmar_rmrr_unit *rmrru;
4299 struct dmar_atsr_unit *atsru;
4300 struct acpi_dmar_atsr *atsr;
4301 struct acpi_dmar_reserved_memory *rmrr;
4302
4303 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4304 return 0;
4305
4306 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4307 rmrr = container_of(rmrru->hdr,
4308 struct acpi_dmar_reserved_memory, header);
4309 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4310 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4311 ((void *)rmrr) + rmrr->header.length,
4312 rmrr->segment, rmrru->devices,
4313 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004314 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004315 return ret;
4316 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004317 dmar_remove_dev_scope(info, rmrr->segment,
4318 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004319 }
4320 }
4321
4322 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4323 if (atsru->include_all)
4324 continue;
4325
4326 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4327 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4328 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4329 (void *)atsr + atsr->header.length,
4330 atsr->segment, atsru->devices,
4331 atsru->devices_cnt);
4332 if (ret > 0)
4333 break;
4334 else if(ret < 0)
4335 return ret;
4336 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4337 if (dmar_remove_dev_scope(info, atsr->segment,
4338 atsru->devices, atsru->devices_cnt))
4339 break;
4340 }
4341 }
4342
4343 return 0;
4344}
4345
Fenghua Yu99dcade2009-11-11 07:23:06 -08004346/*
4347 * Here we only respond to action of unbound device from driver.
4348 *
4349 * Added device is not attached to its DMAR domain here yet. That will happen
4350 * when mapping the device to iova.
4351 */
4352static int device_notifier(struct notifier_block *nb,
4353 unsigned long action, void *data)
4354{
4355 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004356 struct dmar_domain *domain;
4357
David Woodhouse3d891942014-03-06 15:59:26 +00004358 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004359 return 0;
4360
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004361 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004362 return 0;
4363
David Woodhouse1525a292014-03-06 16:19:30 +00004364 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004365 if (!domain)
4366 return 0;
4367
Jiang Liu3a5670e2014-02-19 14:07:33 +08004368 down_read(&dmar_global_lock);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004369 domain_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004370 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004371 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08004372 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07004373
Fenghua Yu99dcade2009-11-11 07:23:06 -08004374 return 0;
4375}
4376
4377static struct notifier_block device_nb = {
4378 .notifier_call = device_notifier,
4379};
4380
Jiang Liu75f05562014-02-19 14:07:37 +08004381static int intel_iommu_memory_notifier(struct notifier_block *nb,
4382 unsigned long val, void *v)
4383{
4384 struct memory_notify *mhp = v;
4385 unsigned long long start, end;
4386 unsigned long start_vpfn, last_vpfn;
4387
4388 switch (val) {
4389 case MEM_GOING_ONLINE:
4390 start = mhp->start_pfn << PAGE_SHIFT;
4391 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4392 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004393 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004394 start, end);
4395 return NOTIFY_BAD;
4396 }
4397 break;
4398
4399 case MEM_OFFLINE:
4400 case MEM_CANCEL_ONLINE:
4401 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4402 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4403 while (start_vpfn <= last_vpfn) {
4404 struct iova *iova;
4405 struct dmar_drhd_unit *drhd;
4406 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004407 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004408
4409 iova = find_iova(&si_domain->iovad, start_vpfn);
4410 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004411 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004412 start_vpfn);
4413 break;
4414 }
4415
4416 iova = split_and_remove_iova(&si_domain->iovad, iova,
4417 start_vpfn, last_vpfn);
4418 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004419 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004420 start_vpfn, last_vpfn);
4421 return NOTIFY_BAD;
4422 }
4423
David Woodhouseea8ea462014-03-05 17:09:32 +00004424 freelist = domain_unmap(si_domain, iova->pfn_lo,
4425 iova->pfn_hi);
4426
Jiang Liu75f05562014-02-19 14:07:37 +08004427 rcu_read_lock();
4428 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004429 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004430 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004431 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004432 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004433 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004434
4435 start_vpfn = iova->pfn_hi + 1;
4436 free_iova_mem(iova);
4437 }
4438 break;
4439 }
4440
4441 return NOTIFY_OK;
4442}
4443
4444static struct notifier_block intel_iommu_memory_nb = {
4445 .notifier_call = intel_iommu_memory_notifier,
4446 .priority = 0
4447};
4448
Alex Williamsona5459cf2014-06-12 16:12:31 -06004449
4450static ssize_t intel_iommu_show_version(struct device *dev,
4451 struct device_attribute *attr,
4452 char *buf)
4453{
4454 struct intel_iommu *iommu = dev_get_drvdata(dev);
4455 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4456 return sprintf(buf, "%d:%d\n",
4457 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4458}
4459static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4460
4461static ssize_t intel_iommu_show_address(struct device *dev,
4462 struct device_attribute *attr,
4463 char *buf)
4464{
4465 struct intel_iommu *iommu = dev_get_drvdata(dev);
4466 return sprintf(buf, "%llx\n", iommu->reg_phys);
4467}
4468static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4469
4470static ssize_t intel_iommu_show_cap(struct device *dev,
4471 struct device_attribute *attr,
4472 char *buf)
4473{
4474 struct intel_iommu *iommu = dev_get_drvdata(dev);
4475 return sprintf(buf, "%llx\n", iommu->cap);
4476}
4477static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4478
4479static ssize_t intel_iommu_show_ecap(struct device *dev,
4480 struct device_attribute *attr,
4481 char *buf)
4482{
4483 struct intel_iommu *iommu = dev_get_drvdata(dev);
4484 return sprintf(buf, "%llx\n", iommu->ecap);
4485}
4486static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4487
Alex Williamson2238c082015-07-14 15:24:53 -06004488static ssize_t intel_iommu_show_ndoms(struct device *dev,
4489 struct device_attribute *attr,
4490 char *buf)
4491{
4492 struct intel_iommu *iommu = dev_get_drvdata(dev);
4493 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4494}
4495static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4496
4497static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4498 struct device_attribute *attr,
4499 char *buf)
4500{
4501 struct intel_iommu *iommu = dev_get_drvdata(dev);
4502 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4503 cap_ndoms(iommu->cap)));
4504}
4505static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4506
Alex Williamsona5459cf2014-06-12 16:12:31 -06004507static struct attribute *intel_iommu_attrs[] = {
4508 &dev_attr_version.attr,
4509 &dev_attr_address.attr,
4510 &dev_attr_cap.attr,
4511 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004512 &dev_attr_domains_supported.attr,
4513 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004514 NULL,
4515};
4516
4517static struct attribute_group intel_iommu_group = {
4518 .name = "intel-iommu",
4519 .attrs = intel_iommu_attrs,
4520};
4521
4522const struct attribute_group *intel_iommu_groups[] = {
4523 &intel_iommu_group,
4524 NULL,
4525};
4526
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004527int __init intel_iommu_init(void)
4528{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004529 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004530 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004531 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004532
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004533 /* VT-d is required for a TXT/tboot launch, so enforce that */
4534 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004535
Jiang Liu3a5670e2014-02-19 14:07:33 +08004536 if (iommu_init_mempool()) {
4537 if (force_on)
4538 panic("tboot: Failed to initialize iommu memory\n");
4539 return -ENOMEM;
4540 }
4541
4542 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004543 if (dmar_table_init()) {
4544 if (force_on)
4545 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004546 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004547 }
4548
Suresh Siddhac2c72862011-08-23 17:05:19 -07004549 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004550 if (force_on)
4551 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004552 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004553 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004554
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004555 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004556 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004557
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004558 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004559 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004560
4561 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004562 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004563
Joseph Cihula51a63e62011-03-21 11:04:24 -07004564 if (dmar_init_reserved_ranges()) {
4565 if (force_on)
4566 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004567 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004568 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004569
4570 init_no_remapping_devices();
4571
Joseph Cihulab7792602011-05-03 00:08:37 -07004572 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004573 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004574 if (force_on)
4575 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004576 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004577 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004578 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004579 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004580 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004581
mark gross5e0d2a62008-03-04 15:22:08 -08004582 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004583#ifdef CONFIG_SWIOTLB
4584 swiotlb = 0;
4585#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004586 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004587
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004588 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004589
Alex Williamsona5459cf2014-06-12 16:12:31 -06004590 for_each_active_iommu(iommu, drhd)
4591 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4592 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004593 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004594
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004595 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004596 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004597 if (si_domain && !hw_pass_through)
4598 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004599
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004600 intel_iommu_enabled = 1;
4601
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004602 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004603
4604out_free_reserved_range:
4605 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004606out_free_dmar:
4607 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004608 up_write(&dmar_global_lock);
4609 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004610 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004611}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004612
Alex Williamson579305f2014-07-03 09:51:43 -06004613static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque)
4614{
4615 struct intel_iommu *iommu = opaque;
4616
4617 iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff);
4618 return 0;
4619}
4620
4621/*
4622 * NB - intel-iommu lacks any sort of reference counting for the users of
4623 * dependent devices. If multiple endpoints have intersecting dependent
4624 * devices, unbinding the driver from any one of them will possibly leave
4625 * the others unable to operate.
4626 */
Han, Weidong3199aa62009-02-26 17:31:12 +08004627static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004628 struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004629{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004630 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004631 return;
4632
Alex Williamson579305f2014-07-03 09:51:43 -06004633 pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004634}
4635
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004636static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004637 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004638{
Yijing Wangbca2b912013-10-31 17:26:04 +08004639 struct device_domain_info *info, *tmp;
Weidong Hanc7151a82008-12-08 22:51:37 +08004640 struct intel_iommu *iommu;
4641 unsigned long flags;
Quentin Lambert2f119c72015-02-06 10:59:53 +01004642 bool found = false;
David Woodhouse156baca2014-03-09 14:00:57 -07004643 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004644
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004645 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004646 if (!iommu)
4647 return;
4648
4649 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wangbca2b912013-10-31 17:26:04 +08004650 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004651 if (info->iommu == iommu && info->bus == bus &&
4652 info->devfn == devfn) {
David Woodhouse109b9b02012-05-25 17:43:02 +01004653 unlink_domain_info(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004654 spin_unlock_irqrestore(&device_domain_lock, flags);
4655
Yu Zhao93a23a72009-05-18 13:51:37 +08004656 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004657 iommu_detach_dev(iommu, info->bus, info->devfn);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004658 iommu_detach_dependent_devices(iommu, dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08004659 free_devinfo_mem(info);
4660
4661 spin_lock_irqsave(&device_domain_lock, flags);
4662
4663 if (found)
4664 break;
4665 else
4666 continue;
4667 }
4668
4669 /* if there is no other devices under the same iommu
4670 * owned by this domain, clear this iommu in iommu_bmp
4671 * update iommu count and coherency
4672 */
David Woodhouse8bbc4412014-03-09 13:52:37 -07004673 if (info->iommu == iommu)
Quentin Lambert2f119c72015-02-06 10:59:53 +01004674 found = true;
Weidong Hanc7151a82008-12-08 22:51:37 +08004675 }
4676
Roland Dreier3e7abe22011-07-20 06:22:21 -07004677 spin_unlock_irqrestore(&device_domain_lock, flags);
4678
Weidong Hanc7151a82008-12-08 22:51:37 +08004679 if (found == 0) {
Jiang Liufb170fb2014-07-11 14:19:28 +08004680 domain_detach_iommu(domain, iommu);
4681 if (!domain_type_is_vm_or_si(domain))
4682 iommu_detach_domain(domain, iommu);
Weidong Hanc7151a82008-12-08 22:51:37 +08004683 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004684}
4685
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004686static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004687{
4688 int adjust_width;
4689
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004690 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4691 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004692 domain_reserve_special_ranges(domain);
4693
4694 /* calculate AGAW */
4695 domain->gaw = guest_width;
4696 adjust_width = guestwidth_to_adjustwidth(guest_width);
4697 domain->agaw = width_to_agaw(adjust_width);
4698
Weidong Han5e98c4b2008-12-08 23:03:27 +08004699 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004700 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004701 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004702 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004703
4704 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004705 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004706 if (!domain->pgd)
4707 return -ENOMEM;
4708 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4709 return 0;
4710}
4711
Joerg Roedel00a77de2015-03-26 13:43:08 +01004712static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004713{
Joerg Roedel5d450802008-12-03 14:52:32 +01004714 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004715 struct iommu_domain *domain;
4716
4717 if (type != IOMMU_DOMAIN_UNMANAGED)
4718 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004719
Jiang Liuab8dfe22014-07-11 14:19:27 +08004720 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004721 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004722 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004723 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004724 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004725 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004726 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004727 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004728 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004729 }
Allen Kay8140a952011-10-14 12:32:17 -07004730 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004731
Joerg Roedel00a77de2015-03-26 13:43:08 +01004732 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004733 domain->geometry.aperture_start = 0;
4734 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4735 domain->geometry.force_aperture = true;
4736
Joerg Roedel00a77de2015-03-26 13:43:08 +01004737 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004738}
Kay, Allen M38717942008-09-09 18:37:29 +03004739
Joerg Roedel00a77de2015-03-26 13:43:08 +01004740static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004741{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004742 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004743}
Kay, Allen M38717942008-09-09 18:37:29 +03004744
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004745static int intel_iommu_attach_device(struct iommu_domain *domain,
4746 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004747{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004748 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004749 struct intel_iommu *iommu;
4750 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004751 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004752
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004753 if (device_is_rmrr_locked(dev)) {
4754 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4755 return -EPERM;
4756 }
4757
David Woodhouse7207d8f2014-03-09 16:31:06 -07004758 /* normally dev is not mapped */
4759 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004760 struct dmar_domain *old_domain;
4761
David Woodhouse1525a292014-03-06 16:19:30 +00004762 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004763 if (old_domain) {
Jiang Liuab8dfe22014-07-11 14:19:27 +08004764 if (domain_type_is_vm_or_si(dmar_domain))
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004765 domain_remove_one_dev_info(old_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004766 else
4767 domain_remove_dev_info(old_domain);
Joerg Roedel62c22162014-12-09 12:56:45 +01004768
4769 if (!domain_type_is_vm_or_si(old_domain) &&
4770 list_empty(&old_domain->devices))
4771 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004772 }
4773 }
4774
David Woodhouse156baca2014-03-09 14:00:57 -07004775 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004776 if (!iommu)
4777 return -ENODEV;
4778
4779 /* check if this iommu agaw is sufficient for max mapped address */
4780 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004781 if (addr_width > cap_mgaw(iommu->cap))
4782 addr_width = cap_mgaw(iommu->cap);
4783
4784 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004785 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004786 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004787 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004788 return -EFAULT;
4789 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004790 dmar_domain->gaw = addr_width;
4791
4792 /*
4793 * Knock out extra levels of page tables if necessary
4794 */
4795 while (iommu->agaw < dmar_domain->agaw) {
4796 struct dma_pte *pte;
4797
4798 pte = dmar_domain->pgd;
4799 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004800 dmar_domain->pgd = (struct dma_pte *)
4801 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004802 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004803 }
4804 dmar_domain->agaw--;
4805 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004806
Joerg Roedel28ccce02015-07-21 14:45:31 +02004807 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004808}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004809
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004810static void intel_iommu_detach_device(struct iommu_domain *domain,
4811 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004812{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004813 domain_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004814}
Kay, Allen M38717942008-09-09 18:37:29 +03004815
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004816static int intel_iommu_map(struct iommu_domain *domain,
4817 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004818 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004819{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004820 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004821 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004822 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004823 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004824
Joerg Roedeldde57a22008-12-03 15:04:09 +01004825 if (iommu_prot & IOMMU_READ)
4826 prot |= DMA_PTE_READ;
4827 if (iommu_prot & IOMMU_WRITE)
4828 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08004829 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4830 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004831
David Woodhouse163cc522009-06-28 00:51:17 +01004832 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004833 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004834 u64 end;
4835
4836 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004837 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004838 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004839 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004840 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004841 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004842 return -EFAULT;
4843 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004844 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004845 }
David Woodhousead051222009-06-28 14:22:28 +01004846 /* Round up size to next multiple of PAGE_SIZE, if it and
4847 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004848 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004849 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4850 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004851 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004852}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004853
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004854static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004855 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004856{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004857 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004858 struct page *freelist = NULL;
4859 struct intel_iommu *iommu;
4860 unsigned long start_pfn, last_pfn;
4861 unsigned int npages;
4862 int iommu_id, num, ndomains, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004863
David Woodhouse5cf0a762014-03-19 16:07:49 +00004864 /* Cope with horrid API which requires us to unmap more than the
4865 size argument if it happens to be a large-page mapping. */
4866 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4867 BUG();
4868
4869 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4870 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4871
David Woodhouseea8ea462014-03-05 17:09:32 +00004872 start_pfn = iova >> VTD_PAGE_SHIFT;
4873 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4874
4875 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4876
4877 npages = last_pfn - start_pfn + 1;
4878
4879 for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004880 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00004881
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004882 /*
4883 * find bit position of dmar_domain
4884 */
4885 ndomains = cap_ndoms(iommu->cap);
4886 for_each_set_bit(num, iommu->domain_ids, ndomains) {
4887 if (get_iommu_domain(iommu, num) == dmar_domain)
4888 iommu_flush_iotlb_psi(iommu, dmar_domain,
4889 start_pfn, npages,
4890 !freelist, 0);
4891 }
David Woodhouseea8ea462014-03-05 17:09:32 +00004892
4893 }
4894
4895 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004896
David Woodhouse163cc522009-06-28 00:51:17 +01004897 if (dmar_domain->max_addr == iova + size)
4898 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004899
David Woodhouse5cf0a762014-03-19 16:07:49 +00004900 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004901}
Kay, Allen M38717942008-09-09 18:37:29 +03004902
Joerg Roedeld14d6572008-12-03 15:06:57 +01004903static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547ac2013-03-29 01:23:58 +05304904 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004905{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004906 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004907 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004908 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004909 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004910
David Woodhouse5cf0a762014-03-19 16:07:49 +00004911 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004912 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004913 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004914
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004915 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004916}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004917
Joerg Roedel5d587b82014-09-05 10:50:45 +02004918static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004919{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004920 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004921 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004922 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004923 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004924
Joerg Roedel5d587b82014-09-05 10:50:45 +02004925 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004926}
4927
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004928static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004929{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004930 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004931 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004932 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004933
Alex Williamsona5459cf2014-06-12 16:12:31 -06004934 iommu = device_to_iommu(dev, &bus, &devfn);
4935 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004936 return -ENODEV;
4937
Alex Williamsona5459cf2014-06-12 16:12:31 -06004938 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004939
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004940 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004941
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004942 if (IS_ERR(group))
4943 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004944
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004945 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004946 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004947}
4948
4949static void intel_iommu_remove_device(struct device *dev)
4950{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004951 struct intel_iommu *iommu;
4952 u8 bus, devfn;
4953
4954 iommu = device_to_iommu(dev, &bus, &devfn);
4955 if (!iommu)
4956 return;
4957
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004958 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004959
4960 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004961}
4962
Thierry Redingb22f6432014-06-27 09:03:12 +02004963static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004964 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004965 .domain_alloc = intel_iommu_domain_alloc,
4966 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004967 .attach_dev = intel_iommu_attach_device,
4968 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004969 .map = intel_iommu_map,
4970 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004971 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004972 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004973 .add_device = intel_iommu_add_device,
4974 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004975 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004976};
David Woodhouse9af88142009-02-13 23:18:03 +00004977
Daniel Vetter94526182013-01-20 23:50:13 +01004978static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4979{
4980 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004981 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004982 dmar_map_gfx = 0;
4983}
4984
4985DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4986DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4987DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4988DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4989DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4990DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4991DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4992
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004993static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004994{
4995 /*
4996 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004997 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004998 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004999 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005000 rwbf_quirk = 1;
5001}
5002
5003DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005004DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5005DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5006DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5007DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5008DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5009DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005010
Adam Jacksoneecfd572010-08-25 21:17:34 +01005011#define GGC 0x52
5012#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5013#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5014#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5015#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5016#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5017#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5018#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5019#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5020
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005021static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005022{
5023 unsigned short ggc;
5024
Adam Jacksoneecfd572010-08-25 21:17:34 +01005025 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005026 return;
5027
Adam Jacksoneecfd572010-08-25 21:17:34 +01005028 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005029 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005030 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005031 } else if (dmar_map_gfx) {
5032 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005033 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005034 intel_iommu_strict = 1;
5035 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005036}
5037DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5038DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5039DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5040DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5041
David Woodhousee0fc7e02009-09-30 09:12:17 -07005042/* On Tylersburg chipsets, some BIOSes have been known to enable the
5043 ISOCH DMAR unit for the Azalia sound device, but not give it any
5044 TLB entries, which causes it to deadlock. Check for that. We do
5045 this in a function called from init_dmars(), instead of in a PCI
5046 quirk, because we don't want to print the obnoxious "BIOS broken"
5047 message if VT-d is actually disabled.
5048*/
5049static void __init check_tylersburg_isoch(void)
5050{
5051 struct pci_dev *pdev;
5052 uint32_t vtisochctrl;
5053
5054 /* If there's no Azalia in the system anyway, forget it. */
5055 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5056 if (!pdev)
5057 return;
5058 pci_dev_put(pdev);
5059
5060 /* System Management Registers. Might be hidden, in which case
5061 we can't do the sanity check. But that's OK, because the
5062 known-broken BIOSes _don't_ actually hide it, so far. */
5063 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5064 if (!pdev)
5065 return;
5066
5067 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5068 pci_dev_put(pdev);
5069 return;
5070 }
5071
5072 pci_dev_put(pdev);
5073
5074 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5075 if (vtisochctrl & 1)
5076 return;
5077
5078 /* Drop all bits other than the number of TLB entries */
5079 vtisochctrl &= 0x1c;
5080
5081 /* If we have the recommended number of TLB entries (16), fine. */
5082 if (vtisochctrl == 0x10)
5083 return;
5084
5085 /* Zero TLB entries? You get to ride the short bus to school. */
5086 if (!vtisochctrl) {
5087 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5088 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5089 dmi_get_system_info(DMI_BIOS_VENDOR),
5090 dmi_get_system_info(DMI_BIOS_VERSION),
5091 dmi_get_system_info(DMI_PRODUCT_VERSION));
5092 iommu_identity_mapping |= IDENTMAP_AZALIA;
5093 return;
5094 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005095
5096 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005097 vtisochctrl);
5098}