blob: 6a01735f4df3973b676c4204ba556bda65236d11 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
mark gross5e0d2a62008-03-04 15:22:08 -080036#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030037#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010038#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010040#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070041#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100042#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020043#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080044#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070045#include <linux/dma-contiguous.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020046#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070047#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070048#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070050
Joerg Roedel078e1ee2012-09-26 12:44:43 +020051#include "irq_remapping.h"
52
Fenghua Yu5b6985c2008-10-16 18:02:32 -070053#define ROOT_SIZE VTD_PAGE_SIZE
54#define CONTEXT_SIZE VTD_PAGE_SIZE
55
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070056#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000057#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070059#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060
61#define IOAPIC_RANGE_START (0xfee00000)
62#define IOAPIC_RANGE_END (0xfeefffff)
63#define IOVA_START_ADDR (0x1000)
64
65#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
66
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070067#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080068#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070069
David Woodhouse2ebe3152009-09-19 07:34:04 -070070#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
71#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
72
73/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
74 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
75#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
76 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
77#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070078
Robin Murphy1b722502015-01-12 17:51:15 +000079/* IO virtual address start page frame number */
80#define IOVA_START_PFN (1)
81
Mark McLoughlinf27be032008-11-20 15:49:43 +000082#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070083#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070084#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080085
Andrew Mortondf08cdc2010-09-22 13:05:11 -070086/* page table handling */
87#define LEVEL_STRIDE (9)
88#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
89
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020090/*
91 * This bitmap is used to advertise the page sizes our hardware support
92 * to the IOMMU core, which will then use this information to split
93 * physically contiguous memory regions it is mapping into page sizes
94 * that we support.
95 *
96 * Traditionally the IOMMU core just handed us the mappings directly,
97 * after making sure the size is an order of a 4KiB page and that the
98 * mapping has natural alignment.
99 *
100 * To retain this behavior, we currently advertise that we support
101 * all page sizes that are an order of 4KiB.
102 *
103 * If at some point we'd like to utilize the IOMMU core's new behavior,
104 * we could change this to advertise the real page sizes we support.
105 */
106#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
107
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700108static inline int agaw_to_level(int agaw)
109{
110 return agaw + 2;
111}
112
113static inline int agaw_to_width(int agaw)
114{
Jiang Liu5c645b32014-01-06 14:18:12 +0800115 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700116}
117
118static inline int width_to_agaw(int width)
119{
Jiang Liu5c645b32014-01-06 14:18:12 +0800120 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700121}
122
123static inline unsigned int level_to_offset_bits(int level)
124{
125 return (level - 1) * LEVEL_STRIDE;
126}
127
128static inline int pfn_level_offset(unsigned long pfn, int level)
129{
130 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
131}
132
133static inline unsigned long level_mask(int level)
134{
135 return -1UL << level_to_offset_bits(level);
136}
137
138static inline unsigned long level_size(int level)
139{
140 return 1UL << level_to_offset_bits(level);
141}
142
143static inline unsigned long align_to_level(unsigned long pfn, int level)
144{
145 return (pfn + level_size(level) - 1) & level_mask(level);
146}
David Woodhousefd18de52009-05-10 23:57:41 +0100147
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100148static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
149{
Jiang Liu5c645b32014-01-06 14:18:12 +0800150 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100151}
152
David Woodhousedd4e8312009-06-27 16:21:20 +0100153/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
154 are never going to work. */
155static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
156{
157 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
158}
159
160static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
161{
162 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
163}
164static inline unsigned long page_to_dma_pfn(struct page *pg)
165{
166 return mm_to_dma_pfn(page_to_pfn(pg));
167}
168static inline unsigned long virt_to_dma_pfn(void *p)
169{
170 return page_to_dma_pfn(virt_to_page(p));
171}
172
Weidong Hand9630fe2008-12-08 11:06:32 +0800173/* global iommu list, set NULL for ignored DMAR units */
174static struct intel_iommu **g_iommus;
175
David Woodhousee0fc7e02009-09-30 09:12:17 -0700176static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000177static int rwbf_quirk;
178
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000179/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700180 * set to 1 to panic kernel if can't successfully enable VT-d
181 * (used when kernel is launched w/ TXT)
182 */
183static int force_on = 0;
184
185/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000186 * 0: Present
187 * 1-11: Reserved
188 * 12-63: Context Ptr (12 - (haw-1))
189 * 64-127: Reserved
190 */
191struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000192 u64 lo;
193 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000194};
195#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000196
Joerg Roedel091d42e2015-06-12 11:56:10 +0200197/*
198 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
199 * if marked present.
200 */
201static phys_addr_t root_entry_lctp(struct root_entry *re)
202{
203 if (!(re->lo & 1))
204 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000205
Joerg Roedel091d42e2015-06-12 11:56:10 +0200206 return re->lo & VTD_PAGE_MASK;
207}
208
209/*
210 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
211 * if marked present.
212 */
213static phys_addr_t root_entry_uctp(struct root_entry *re)
214{
215 if (!(re->hi & 1))
216 return 0;
217
218 return re->hi & VTD_PAGE_MASK;
219}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000220/*
221 * low 64 bits:
222 * 0: present
223 * 1: fault processing disable
224 * 2-3: translation type
225 * 12-63: address space root
226 * high 64 bits:
227 * 0-2: address width
228 * 3-6: aval
229 * 8-23: domain id
230 */
231struct context_entry {
232 u64 lo;
233 u64 hi;
234};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000235
Joerg Roedelcf484d02015-06-12 12:21:46 +0200236static inline void context_clear_pasid_enable(struct context_entry *context)
237{
238 context->lo &= ~(1ULL << 11);
239}
240
241static inline bool context_pasid_enabled(struct context_entry *context)
242{
243 return !!(context->lo & (1ULL << 11));
244}
245
246static inline void context_set_copied(struct context_entry *context)
247{
248 context->hi |= (1ull << 3);
249}
250
251static inline bool context_copied(struct context_entry *context)
252{
253 return !!(context->hi & (1ULL << 3));
254}
255
256static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000257{
258 return (context->lo & 1);
259}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200260
261static inline bool context_present(struct context_entry *context)
262{
263 return context_pasid_enabled(context) ?
264 __context_present(context) :
265 __context_present(context) && !context_copied(context);
266}
267
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000268static inline void context_set_present(struct context_entry *context)
269{
270 context->lo |= 1;
271}
272
273static inline void context_set_fault_enable(struct context_entry *context)
274{
275 context->lo &= (((u64)-1) << 2) | 1;
276}
277
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000278static inline void context_set_translation_type(struct context_entry *context,
279 unsigned long value)
280{
281 context->lo &= (((u64)-1) << 4) | 3;
282 context->lo |= (value & 3) << 2;
283}
284
285static inline void context_set_address_root(struct context_entry *context,
286 unsigned long value)
287{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800288 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000289 context->lo |= value & VTD_PAGE_MASK;
290}
291
292static inline void context_set_address_width(struct context_entry *context,
293 unsigned long value)
294{
295 context->hi |= value & 7;
296}
297
298static inline void context_set_domain_id(struct context_entry *context,
299 unsigned long value)
300{
301 context->hi |= (value & ((1 << 16) - 1)) << 8;
302}
303
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200304static inline int context_domain_id(struct context_entry *c)
305{
306 return((c->hi >> 8) & 0xffff);
307}
308
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000309static inline void context_clear_entry(struct context_entry *context)
310{
311 context->lo = 0;
312 context->hi = 0;
313}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000314
Mark McLoughlin622ba122008-11-20 15:49:46 +0000315/*
316 * 0: readable
317 * 1: writable
318 * 2-6: reserved
319 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800320 * 8-10: available
321 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000322 * 12-63: Host physcial address
323 */
324struct dma_pte {
325 u64 val;
326};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000327
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000328static inline void dma_clear_pte(struct dma_pte *pte)
329{
330 pte->val = 0;
331}
332
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000333static inline u64 dma_pte_addr(struct dma_pte *pte)
334{
David Woodhousec85994e2009-07-01 19:21:24 +0100335#ifdef CONFIG_64BIT
336 return pte->val & VTD_PAGE_MASK;
337#else
338 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100339 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100340#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000341}
342
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000343static inline bool dma_pte_present(struct dma_pte *pte)
344{
345 return (pte->val & 3) != 0;
346}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000347
Allen Kay4399c8b2011-10-14 12:32:46 -0700348static inline bool dma_pte_superpage(struct dma_pte *pte)
349{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200350 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700351}
352
David Woodhouse75e6bf92009-07-02 11:21:16 +0100353static inline int first_pte_in_page(struct dma_pte *pte)
354{
355 return !((unsigned long)pte & ~VTD_PAGE_MASK);
356}
357
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700358/*
359 * This domain is a statically identity mapping domain.
360 * 1. This domain creats a static 1:1 mapping to all usable memory.
361 * 2. It maps to each iommu if successful.
362 * 3. Each iommu mapps to this domain if successful.
363 */
David Woodhouse19943b02009-08-04 16:19:20 +0100364static struct dmar_domain *si_domain;
365static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700366
Joerg Roedel28ccce02015-07-21 14:45:31 +0200367/*
368 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800369 * across iommus may be owned in one domain, e.g. kvm guest.
370 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800371#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800372
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700373/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800374#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700375
Joerg Roedel29a27712015-07-21 17:17:12 +0200376#define for_each_domain_iommu(idx, domain) \
377 for (idx = 0; idx < g_num_of_iommus; idx++) \
378 if (domain->iommu_refcnt[idx])
379
Mark McLoughlin99126f72008-11-20 15:49:47 +0000380struct dmar_domain {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700381 int nid; /* node id */
Joerg Roedel29a27712015-07-21 17:17:12 +0200382
383 unsigned iommu_refcnt[DMAR_UNITS_SUPPORTED];
384 /* Refcount of devices per iommu */
385
Mark McLoughlin99126f72008-11-20 15:49:47 +0000386
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +0200387 u16 iommu_did[DMAR_UNITS_SUPPORTED];
388 /* Domain ids per IOMMU. Use u16 since
389 * domain ids are 16 bit wide according
390 * to VT-d spec, section 9.3 */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000391
Joerg Roedel00a77de2015-03-26 13:43:08 +0100392 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000393 struct iova_domain iovad; /* iova's that belong to this domain */
394
395 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000396 int gaw; /* max guest address width */
397
398 /* adjusted guest address width, 0 is level 2 30-bit */
399 int agaw;
400
Weidong Han3b5410e2008-12-08 09:17:15 +0800401 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800402
403 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800404 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800405 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100406 int iommu_superpage;/* Level of superpages supported:
407 0 == 4KiB (no superpages), 1 == 2MiB,
408 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800409 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100410
411 struct iommu_domain domain; /* generic domain data structure for
412 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000413};
414
Mark McLoughlina647dacb2008-11-20 15:49:48 +0000415/* PCI domain-device relationship */
416struct device_domain_info {
417 struct list_head link; /* link to domain siblings */
418 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100419 u8 bus; /* PCI bus number */
Mark McLoughlina647dacb2008-11-20 15:49:48 +0000420 u8 devfn; /* PCI devfn number */
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -0500421 struct {
422 u8 enabled:1;
423 u8 qdep;
424 } ats; /* ATS state */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000425 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800426 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dacb2008-11-20 15:49:48 +0000427 struct dmar_domain *domain; /* pointer to domain */
428};
429
Jiang Liub94e4112014-02-19 14:07:25 +0800430struct dmar_rmrr_unit {
431 struct list_head list; /* list of rmrr units */
432 struct acpi_dmar_header *hdr; /* ACPI header */
433 u64 base_address; /* reserved base address*/
434 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000435 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800436 int devices_cnt; /* target device count */
437};
438
439struct dmar_atsr_unit {
440 struct list_head list; /* list of ATSR units */
441 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000442 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800443 int devices_cnt; /* target device count */
444 u8 include_all:1; /* include all ports */
445};
446
447static LIST_HEAD(dmar_atsr_units);
448static LIST_HEAD(dmar_rmrr_units);
449
450#define for_each_rmrr_units(rmrr) \
451 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
452
mark gross5e0d2a62008-03-04 15:22:08 -0800453static void flush_unmaps_timeout(unsigned long data);
454
Jiang Liub707cb02014-01-06 14:18:26 +0800455static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800456
mark gross80b20dd2008-04-18 13:53:58 -0700457#define HIGH_WATER_MARK 250
458struct deferred_flush_tables {
459 int next;
460 struct iova *iova[HIGH_WATER_MARK];
461 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000462 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700463};
464
465static struct deferred_flush_tables *deferred_flush;
466
mark gross5e0d2a62008-03-04 15:22:08 -0800467/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800468static int g_num_of_iommus;
469
470static DEFINE_SPINLOCK(async_umap_flush_lock);
471static LIST_HEAD(unmaps_to_do);
472
473static int timer_on;
474static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800475
Jiang Liu92d03cc2014-02-19 14:07:28 +0800476static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700477static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200478static void dmar_remove_one_dev_info(struct dmar_domain *domain,
479 struct device *dev);
Joerg Roedel127c7612015-07-23 17:44:46 +0200480static void __dmar_remove_one_dev_info(struct device_domain_info *info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200481static void domain_context_clear(struct intel_iommu *iommu,
482 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800483static int domain_detach_iommu(struct dmar_domain *domain,
484 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700485
Suresh Siddhad3f13812011-08-23 17:05:25 -0700486#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800487int dmar_disabled = 0;
488#else
489int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700490#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800491
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200492int intel_iommu_enabled = 0;
493EXPORT_SYMBOL_GPL(intel_iommu_enabled);
494
David Woodhouse2d9e6672010-06-15 10:57:57 +0100495static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700496static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800497static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100498static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100499static int intel_iommu_ecs = 1;
David Woodhouseae853dd2015-09-09 11:58:59 +0100500static int intel_iommu_pasid28;
501static int iommu_identity_mapping;
502
503#define IDENTMAP_ALL 1
504#define IDENTMAP_GFX 2
505#define IDENTMAP_AZALIA 4
David Woodhousec83b2f22015-06-12 10:15:49 +0100506
507/* We only actually use ECS when PASID support (on the new bit 40)
508 * is also advertised. Some early implementations — the ones with
509 * PASID support on bit 28 — have issues even when we *only* use
510 * extended root/context tables. */
David Woodhouseae853dd2015-09-09 11:58:59 +0100511#define pasid_enabled(iommu) (ecap_pasid(iommu->ecap) || \
512 (intel_iommu_pasid28 && ecap_broken_pasid(iommu->ecap)))
David Woodhousec83b2f22015-06-12 10:15:49 +0100513#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
David Woodhouseae853dd2015-09-09 11:58:59 +0100514 pasid_enabled(iommu))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700515
David Woodhousec0771df2011-10-14 20:59:46 +0100516int intel_iommu_gfx_mapped;
517EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
518
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700519#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
520static DEFINE_SPINLOCK(device_domain_lock);
521static LIST_HEAD(device_domain_list);
522
Thierry Redingb22f6432014-06-27 09:03:12 +0200523static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100524
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200525static bool translation_pre_enabled(struct intel_iommu *iommu)
526{
527 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
528}
529
Joerg Roedel091d42e2015-06-12 11:56:10 +0200530static void clear_translation_pre_enabled(struct intel_iommu *iommu)
531{
532 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
533}
534
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200535static void init_translation_status(struct intel_iommu *iommu)
536{
537 u32 gsts;
538
539 gsts = readl(iommu->reg + DMAR_GSTS_REG);
540 if (gsts & DMA_GSTS_TES)
541 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
542}
543
Joerg Roedel00a77de2015-03-26 13:43:08 +0100544/* Convert generic 'struct iommu_domain to private struct dmar_domain */
545static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
546{
547 return container_of(dom, struct dmar_domain, domain);
548}
549
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700550static int __init intel_iommu_setup(char *str)
551{
552 if (!str)
553 return -EINVAL;
554 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800555 if (!strncmp(str, "on", 2)) {
556 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200557 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800558 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700559 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200560 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700561 } else if (!strncmp(str, "igfx_off", 8)) {
562 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200563 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700564 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200565 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700566 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800567 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200568 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800569 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100570 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200571 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100572 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100573 } else if (!strncmp(str, "ecs_off", 7)) {
574 printk(KERN_INFO
575 "Intel-IOMMU: disable extended context table support\n");
576 intel_iommu_ecs = 0;
David Woodhouseae853dd2015-09-09 11:58:59 +0100577 } else if (!strncmp(str, "pasid28", 7)) {
578 printk(KERN_INFO
579 "Intel-IOMMU: enable pre-production PASID support\n");
580 intel_iommu_pasid28 = 1;
581 iommu_identity_mapping |= IDENTMAP_GFX;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700582 }
583
584 str += strcspn(str, ",");
585 while (*str == ',')
586 str++;
587 }
588 return 0;
589}
590__setup("intel_iommu=", intel_iommu_setup);
591
592static struct kmem_cache *iommu_domain_cache;
593static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700594
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200595static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
596{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200597 struct dmar_domain **domains;
598 int idx = did >> 8;
599
600 domains = iommu->domains[idx];
601 if (!domains)
602 return NULL;
603
604 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200605}
606
607static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
608 struct dmar_domain *domain)
609{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200610 struct dmar_domain **domains;
611 int idx = did >> 8;
612
613 if (!iommu->domains[idx]) {
614 size_t size = 256 * sizeof(struct dmar_domain *);
615 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
616 }
617
618 domains = iommu->domains[idx];
619 if (WARN_ON(!domains))
620 return;
621 else
622 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200623}
624
Suresh Siddha4c923d42009-10-02 11:01:24 -0700625static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700626{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700627 struct page *page;
628 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700629
Suresh Siddha4c923d42009-10-02 11:01:24 -0700630 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
631 if (page)
632 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700633 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700634}
635
636static inline void free_pgtable_page(void *vaddr)
637{
638 free_page((unsigned long)vaddr);
639}
640
641static inline void *alloc_domain_mem(void)
642{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900643 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700644}
645
Kay, Allen M38717942008-09-09 18:37:29 +0300646static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700647{
648 kmem_cache_free(iommu_domain_cache, vaddr);
649}
650
651static inline void * alloc_devinfo_mem(void)
652{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900653 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700654}
655
656static inline void free_devinfo_mem(void *vaddr)
657{
658 kmem_cache_free(iommu_devinfo_cache, vaddr);
659}
660
Jiang Liuab8dfe22014-07-11 14:19:27 +0800661static inline int domain_type_is_vm(struct dmar_domain *domain)
662{
663 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
664}
665
Joerg Roedel28ccce02015-07-21 14:45:31 +0200666static inline int domain_type_is_si(struct dmar_domain *domain)
667{
668 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
669}
670
Jiang Liuab8dfe22014-07-11 14:19:27 +0800671static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
672{
673 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
674 DOMAIN_FLAG_STATIC_IDENTITY);
675}
Weidong Han1b573682008-12-08 15:34:06 +0800676
Jiang Liu162d1b12014-07-11 14:19:35 +0800677static inline int domain_pfn_supported(struct dmar_domain *domain,
678 unsigned long pfn)
679{
680 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
681
682 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
683}
684
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700685static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800686{
687 unsigned long sagaw;
688 int agaw = -1;
689
690 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700691 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800692 agaw >= 0; agaw--) {
693 if (test_bit(agaw, &sagaw))
694 break;
695 }
696
697 return agaw;
698}
699
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700700/*
701 * Calculate max SAGAW for each iommu.
702 */
703int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
704{
705 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
706}
707
708/*
709 * calculate agaw for each iommu.
710 * "SAGAW" may be different across iommus, use a default agaw, and
711 * get a supported less agaw for iommus that don't support the default agaw.
712 */
713int iommu_calculate_agaw(struct intel_iommu *iommu)
714{
715 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
716}
717
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700718/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800719static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
720{
721 int iommu_id;
722
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700723 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800724 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200725 for_each_domain_iommu(iommu_id, domain)
726 break;
727
Weidong Han8c11e792008-12-08 15:29:22 +0800728 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
729 return NULL;
730
731 return g_iommus[iommu_id];
732}
733
Weidong Han8e6040972008-12-08 15:49:06 +0800734static void domain_update_iommu_coherency(struct dmar_domain *domain)
735{
David Woodhoused0501962014-03-11 17:10:29 -0700736 struct dmar_drhd_unit *drhd;
737 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100738 bool found = false;
739 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800740
David Woodhoused0501962014-03-11 17:10:29 -0700741 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800742
Joerg Roedel29a27712015-07-21 17:17:12 +0200743 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100744 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800745 if (!ecap_coherent(g_iommus[i]->ecap)) {
746 domain->iommu_coherency = 0;
747 break;
748 }
Weidong Han8e6040972008-12-08 15:49:06 +0800749 }
David Woodhoused0501962014-03-11 17:10:29 -0700750 if (found)
751 return;
752
753 /* No hardware attached; use lowest common denominator */
754 rcu_read_lock();
755 for_each_active_iommu(iommu, drhd) {
756 if (!ecap_coherent(iommu->ecap)) {
757 domain->iommu_coherency = 0;
758 break;
759 }
760 }
761 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800762}
763
Jiang Liu161f6932014-07-11 14:19:37 +0800764static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100765{
Allen Kay8140a952011-10-14 12:32:17 -0700766 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800767 struct intel_iommu *iommu;
768 int ret = 1;
769
770 rcu_read_lock();
771 for_each_active_iommu(iommu, drhd) {
772 if (iommu != skip) {
773 if (!ecap_sc_support(iommu->ecap)) {
774 ret = 0;
775 break;
776 }
777 }
778 }
779 rcu_read_unlock();
780
781 return ret;
782}
783
784static int domain_update_iommu_superpage(struct intel_iommu *skip)
785{
786 struct dmar_drhd_unit *drhd;
787 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700788 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100789
790 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800791 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100792 }
793
Allen Kay8140a952011-10-14 12:32:17 -0700794 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e2426122014-02-19 14:07:34 +0800795 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700796 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800797 if (iommu != skip) {
798 mask &= cap_super_page_val(iommu->cap);
799 if (!mask)
800 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100801 }
802 }
Jiang Liu0e2426122014-02-19 14:07:34 +0800803 rcu_read_unlock();
804
Jiang Liu161f6932014-07-11 14:19:37 +0800805 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100806}
807
Sheng Yang58c610b2009-03-18 15:33:05 +0800808/* Some capabilities may be different across iommus */
809static void domain_update_iommu_cap(struct dmar_domain *domain)
810{
811 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800812 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
813 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800814}
815
David Woodhouse03ecc322015-02-13 14:35:21 +0000816static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
817 u8 bus, u8 devfn, int alloc)
818{
819 struct root_entry *root = &iommu->root_entry[bus];
820 struct context_entry *context;
821 u64 *entry;
822
Joerg Roedel4df4eab2015-08-25 10:54:28 +0200823 entry = &root->lo;
David Woodhousec83b2f22015-06-12 10:15:49 +0100824 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000825 if (devfn >= 0x80) {
826 devfn -= 0x80;
827 entry = &root->hi;
828 }
829 devfn *= 2;
830 }
David Woodhouse03ecc322015-02-13 14:35:21 +0000831 if (*entry & 1)
832 context = phys_to_virt(*entry & VTD_PAGE_MASK);
833 else {
834 unsigned long phy_addr;
835 if (!alloc)
836 return NULL;
837
838 context = alloc_pgtable_page(iommu->node);
839 if (!context)
840 return NULL;
841
842 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
843 phy_addr = virt_to_phys((void *)context);
844 *entry = phy_addr | 1;
845 __iommu_flush_cache(iommu, entry, sizeof(*entry));
846 }
847 return &context[devfn];
848}
849
David Woodhouse4ed6a542015-05-11 14:59:20 +0100850static int iommu_dummy(struct device *dev)
851{
852 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
853}
854
David Woodhouse156baca2014-03-09 14:00:57 -0700855static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800856{
857 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800858 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700859 struct device *tmp;
860 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800861 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800862 int i;
863
David Woodhouse4ed6a542015-05-11 14:59:20 +0100864 if (iommu_dummy(dev))
865 return NULL;
866
David Woodhouse156baca2014-03-09 14:00:57 -0700867 if (dev_is_pci(dev)) {
868 pdev = to_pci_dev(dev);
869 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100870 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700871 dev = &ACPI_COMPANION(dev)->dev;
872
Jiang Liu0e2426122014-02-19 14:07:34 +0800873 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800874 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700875 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100876 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800877
Jiang Liub683b232014-02-19 14:07:32 +0800878 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700879 drhd->devices_cnt, i, tmp) {
880 if (tmp == dev) {
881 *bus = drhd->devices[i].bus;
882 *devfn = drhd->devices[i].devfn;
883 goto out;
884 }
885
886 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000887 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700888
889 ptmp = to_pci_dev(tmp);
890 if (ptmp->subordinate &&
891 ptmp->subordinate->number <= pdev->bus->number &&
892 ptmp->subordinate->busn_res.end >= pdev->bus->number)
893 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100894 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800895
David Woodhouse156baca2014-03-09 14:00:57 -0700896 if (pdev && drhd->include_all) {
897 got_pdev:
898 *bus = pdev->bus->number;
899 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800900 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700901 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800902 }
Jiang Liub683b232014-02-19 14:07:32 +0800903 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700904 out:
Jiang Liu0e2426122014-02-19 14:07:34 +0800905 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800906
Jiang Liub683b232014-02-19 14:07:32 +0800907 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800908}
909
Weidong Han5331fe62008-12-08 23:00:00 +0800910static void domain_flush_cache(struct dmar_domain *domain,
911 void *addr, int size)
912{
913 if (!domain->iommu_coherency)
914 clflush_cache_range(addr, size);
915}
916
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700917static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
918{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700919 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000920 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700921 unsigned long flags;
922
923 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000924 context = iommu_context_addr(iommu, bus, devfn, 0);
925 if (context)
926 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700927 spin_unlock_irqrestore(&iommu->lock, flags);
928 return ret;
929}
930
931static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
932{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700933 struct context_entry *context;
934 unsigned long flags;
935
936 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000937 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700938 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000939 context_clear_entry(context);
940 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700941 }
942 spin_unlock_irqrestore(&iommu->lock, flags);
943}
944
945static void free_context_table(struct intel_iommu *iommu)
946{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700947 int i;
948 unsigned long flags;
949 struct context_entry *context;
950
951 spin_lock_irqsave(&iommu->lock, flags);
952 if (!iommu->root_entry) {
953 goto out;
954 }
955 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000956 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700957 if (context)
958 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000959
David Woodhousec83b2f22015-06-12 10:15:49 +0100960 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000961 continue;
962
963 context = iommu_context_addr(iommu, i, 0x80, 0);
964 if (context)
965 free_pgtable_page(context);
966
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700967 }
968 free_pgtable_page(iommu->root_entry);
969 iommu->root_entry = NULL;
970out:
971 spin_unlock_irqrestore(&iommu->lock, flags);
972}
973
David Woodhouseb026fd22009-06-28 10:37:25 +0100974static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000975 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700976{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700977 struct dma_pte *parent, *pte = NULL;
978 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700979 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700980
981 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200982
Jiang Liu162d1b12014-07-11 14:19:35 +0800983 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200984 /* Address beyond IOMMU's addressing capabilities. */
985 return NULL;
986
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700987 parent = domain->pgd;
988
David Woodhouse5cf0a762014-03-19 16:07:49 +0000989 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700990 void *tmp_page;
991
David Woodhouseb026fd22009-06-28 10:37:25 +0100992 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700993 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000994 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100995 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000996 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700997 break;
998
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000999 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +01001000 uint64_t pteval;
1001
Suresh Siddha4c923d42009-10-02 11:01:24 -07001002 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001003
David Woodhouse206a73c2009-07-01 19:30:28 +01001004 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001005 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +01001006
David Woodhousec85994e2009-07-01 19:21:24 +01001007 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -04001008 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 +08001009 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +01001010 /* Someone else set it while we were thinking; use theirs. */
1011 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +08001012 else
David Woodhousec85994e2009-07-01 19:21:24 +01001013 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001014 }
David Woodhouse5cf0a762014-03-19 16:07:49 +00001015 if (level == 1)
1016 break;
1017
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001018 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001019 level--;
1020 }
1021
David Woodhouse5cf0a762014-03-19 16:07:49 +00001022 if (!*target_level)
1023 *target_level = level;
1024
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001025 return pte;
1026}
1027
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001028
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001029/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001030static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1031 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001032 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001033{
1034 struct dma_pte *parent, *pte = NULL;
1035 int total = agaw_to_level(domain->agaw);
1036 int offset;
1037
1038 parent = domain->pgd;
1039 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001040 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001041 pte = &parent[offset];
1042 if (level == total)
1043 return pte;
1044
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001045 if (!dma_pte_present(pte)) {
1046 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001047 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001048 }
1049
Yijing Wange16922a2014-05-20 20:37:51 +08001050 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001051 *large_page = total;
1052 return pte;
1053 }
1054
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001055 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001056 total--;
1057 }
1058 return NULL;
1059}
1060
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001061/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001062static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf52009-06-27 22:09:11 +01001063 unsigned long start_pfn,
1064 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001065{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001066 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001067 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001068
Jiang Liu162d1b12014-07-11 14:19:35 +08001069 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1070 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001071 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001072
David Woodhouse04b18e62009-06-27 19:15:01 +01001073 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001074 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001075 large_page = 1;
1076 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001077 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001078 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001079 continue;
1080 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001081 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001082 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001083 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001084 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001085 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1086
David Woodhouse310a5ab2009-06-28 18:52:20 +01001087 domain_flush_cache(domain, first_pte,
1088 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001089
1090 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001091}
1092
Alex Williamson3269ee02013-06-15 10:27:19 -06001093static void dma_pte_free_level(struct dmar_domain *domain, int level,
1094 struct dma_pte *pte, unsigned long pfn,
1095 unsigned long start_pfn, unsigned long last_pfn)
1096{
1097 pfn = max(start_pfn, pfn);
1098 pte = &pte[pfn_level_offset(pfn, level)];
1099
1100 do {
1101 unsigned long level_pfn;
1102 struct dma_pte *level_pte;
1103
1104 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1105 goto next;
1106
1107 level_pfn = pfn & level_mask(level - 1);
1108 level_pte = phys_to_virt(dma_pte_addr(pte));
1109
1110 if (level > 2)
1111 dma_pte_free_level(domain, level - 1, level_pte,
1112 level_pfn, start_pfn, last_pfn);
1113
1114 /* If range covers entire pagetable, free it */
1115 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001116 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001117 dma_clear_pte(pte);
1118 domain_flush_cache(domain, pte, sizeof(*pte));
1119 free_pgtable_page(level_pte);
1120 }
1121next:
1122 pfn += level_size(level);
1123 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1124}
1125
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001126/* free page table pages. last level pte should already be cleared */
1127static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001128 unsigned long start_pfn,
1129 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001130{
Jiang Liu162d1b12014-07-11 14:19:35 +08001131 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1132 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001133 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001134
Jiang Liud41a4ad2014-07-11 14:19:34 +08001135 dma_pte_clear_range(domain, start_pfn, last_pfn);
1136
David Woodhousef3a0a522009-06-30 03:40:07 +01001137 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001138 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1139 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001140
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001141 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001142 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001143 free_pgtable_page(domain->pgd);
1144 domain->pgd = NULL;
1145 }
1146}
1147
David Woodhouseea8ea462014-03-05 17:09:32 +00001148/* When a page at a given level is being unlinked from its parent, we don't
1149 need to *modify* it at all. All we need to do is make a list of all the
1150 pages which can be freed just as soon as we've flushed the IOTLB and we
1151 know the hardware page-walk will no longer touch them.
1152 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1153 be freed. */
1154static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1155 int level, struct dma_pte *pte,
1156 struct page *freelist)
1157{
1158 struct page *pg;
1159
1160 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1161 pg->freelist = freelist;
1162 freelist = pg;
1163
1164 if (level == 1)
1165 return freelist;
1166
Jiang Liuadeb2592014-04-09 10:20:39 +08001167 pte = page_address(pg);
1168 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001169 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1170 freelist = dma_pte_list_pagetables(domain, level - 1,
1171 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001172 pte++;
1173 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001174
1175 return freelist;
1176}
1177
1178static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1179 struct dma_pte *pte, unsigned long pfn,
1180 unsigned long start_pfn,
1181 unsigned long last_pfn,
1182 struct page *freelist)
1183{
1184 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1185
1186 pfn = max(start_pfn, pfn);
1187 pte = &pte[pfn_level_offset(pfn, level)];
1188
1189 do {
1190 unsigned long level_pfn;
1191
1192 if (!dma_pte_present(pte))
1193 goto next;
1194
1195 level_pfn = pfn & level_mask(level);
1196
1197 /* If range covers entire pagetable, free it */
1198 if (start_pfn <= level_pfn &&
1199 last_pfn >= level_pfn + level_size(level) - 1) {
1200 /* These suborbinate page tables are going away entirely. Don't
1201 bother to clear them; we're just going to *free* them. */
1202 if (level > 1 && !dma_pte_superpage(pte))
1203 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1204
1205 dma_clear_pte(pte);
1206 if (!first_pte)
1207 first_pte = pte;
1208 last_pte = pte;
1209 } else if (level > 1) {
1210 /* Recurse down into a level that isn't *entirely* obsolete */
1211 freelist = dma_pte_clear_level(domain, level - 1,
1212 phys_to_virt(dma_pte_addr(pte)),
1213 level_pfn, start_pfn, last_pfn,
1214 freelist);
1215 }
1216next:
1217 pfn += level_size(level);
1218 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1219
1220 if (first_pte)
1221 domain_flush_cache(domain, first_pte,
1222 (void *)++last_pte - (void *)first_pte);
1223
1224 return freelist;
1225}
1226
1227/* We can't just free the pages because the IOMMU may still be walking
1228 the page tables, and may have cached the intermediate levels. The
1229 pages can only be freed after the IOTLB flush has been done. */
Joerg Roedelb6904202015-08-13 11:32:18 +02001230static struct page *domain_unmap(struct dmar_domain *domain,
1231 unsigned long start_pfn,
1232 unsigned long last_pfn)
David Woodhouseea8ea462014-03-05 17:09:32 +00001233{
David Woodhouseea8ea462014-03-05 17:09:32 +00001234 struct page *freelist = NULL;
1235
Jiang Liu162d1b12014-07-11 14:19:35 +08001236 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1237 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001238 BUG_ON(start_pfn > last_pfn);
1239
1240 /* we don't need lock here; nobody else touches the iova range */
1241 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1242 domain->pgd, 0, start_pfn, last_pfn, NULL);
1243
1244 /* free pgd */
1245 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1246 struct page *pgd_page = virt_to_page(domain->pgd);
1247 pgd_page->freelist = freelist;
1248 freelist = pgd_page;
1249
1250 domain->pgd = NULL;
1251 }
1252
1253 return freelist;
1254}
1255
Joerg Roedelb6904202015-08-13 11:32:18 +02001256static void dma_free_pagelist(struct page *freelist)
David Woodhouseea8ea462014-03-05 17:09:32 +00001257{
1258 struct page *pg;
1259
1260 while ((pg = freelist)) {
1261 freelist = pg->freelist;
1262 free_pgtable_page(page_address(pg));
1263 }
1264}
1265
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001266/* iommu handling */
1267static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1268{
1269 struct root_entry *root;
1270 unsigned long flags;
1271
Suresh Siddha4c923d42009-10-02 11:01:24 -07001272 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001273 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001274 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001275 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001276 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001277 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001278
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001279 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001280
1281 spin_lock_irqsave(&iommu->lock, flags);
1282 iommu->root_entry = root;
1283 spin_unlock_irqrestore(&iommu->lock, flags);
1284
1285 return 0;
1286}
1287
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001288static void iommu_set_root_entry(struct intel_iommu *iommu)
1289{
David Woodhouse03ecc322015-02-13 14:35:21 +00001290 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001291 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292 unsigned long flag;
1293
David Woodhouse03ecc322015-02-13 14:35:21 +00001294 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001295 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001296 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001297
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001298 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001299 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001300
David Woodhousec416daa2009-05-10 20:30:58 +01001301 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001302
1303 /* Make sure hardware complete it */
1304 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001305 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001306
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001307 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001308}
1309
1310static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1311{
1312 u32 val;
1313 unsigned long flag;
1314
David Woodhouse9af88142009-02-13 23:18:03 +00001315 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001316 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001317
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001318 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001319 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001320
1321 /* Make sure hardware complete it */
1322 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001323 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001324
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001325 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001326}
1327
1328/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001329static void __iommu_flush_context(struct intel_iommu *iommu,
1330 u16 did, u16 source_id, u8 function_mask,
1331 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001332{
1333 u64 val = 0;
1334 unsigned long flag;
1335
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001336 switch (type) {
1337 case DMA_CCMD_GLOBAL_INVL:
1338 val = DMA_CCMD_GLOBAL_INVL;
1339 break;
1340 case DMA_CCMD_DOMAIN_INVL:
1341 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1342 break;
1343 case DMA_CCMD_DEVICE_INVL:
1344 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1345 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1346 break;
1347 default:
1348 BUG();
1349 }
1350 val |= DMA_CCMD_ICC;
1351
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001352 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001353 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1354
1355 /* Make sure hardware complete it */
1356 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1357 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1358
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001359 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001360}
1361
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001362/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001363static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1364 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001365{
1366 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1367 u64 val = 0, val_iva = 0;
1368 unsigned long flag;
1369
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001370 switch (type) {
1371 case DMA_TLB_GLOBAL_FLUSH:
1372 /* global flush doesn't need set IVA_REG */
1373 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1374 break;
1375 case DMA_TLB_DSI_FLUSH:
1376 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1377 break;
1378 case DMA_TLB_PSI_FLUSH:
1379 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001380 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001381 val_iva = size_order | addr;
1382 break;
1383 default:
1384 BUG();
1385 }
1386 /* Note: set drain read/write */
1387#if 0
1388 /*
1389 * This is probably to be super secure.. Looks like we can
1390 * ignore it without any impact.
1391 */
1392 if (cap_read_drain(iommu->cap))
1393 val |= DMA_TLB_READ_DRAIN;
1394#endif
1395 if (cap_write_drain(iommu->cap))
1396 val |= DMA_TLB_WRITE_DRAIN;
1397
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001398 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001399 /* Note: Only uses first TLB reg currently */
1400 if (val_iva)
1401 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1402 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1403
1404 /* Make sure hardware complete it */
1405 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1406 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1407
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001408 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001409
1410 /* check IOTLB invalidation granularity */
1411 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001412 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001413 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001414 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001415 (unsigned long long)DMA_TLB_IIRG(type),
1416 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001417}
1418
David Woodhouse64ae8922014-03-09 12:52:30 -07001419static struct device_domain_info *
1420iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1421 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001422{
Quentin Lambert2f119c72015-02-06 10:59:53 +01001423 bool found = false;
Yu Zhao93a23a72009-05-18 13:51:37 +08001424 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001425 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001426
Joerg Roedel55d94042015-07-22 16:50:40 +02001427 assert_spin_locked(&device_domain_lock);
1428
Yu Zhao93a23a72009-05-18 13:51:37 +08001429 if (!ecap_dev_iotlb_support(iommu->ecap))
1430 return NULL;
1431
1432 if (!iommu->qi)
1433 return NULL;
1434
Yu Zhao93a23a72009-05-18 13:51:37 +08001435 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001436 if (info->iommu == iommu && info->bus == bus &&
1437 info->devfn == devfn) {
Quentin Lambert2f119c72015-02-06 10:59:53 +01001438 found = true;
Yu Zhao93a23a72009-05-18 13:51:37 +08001439 break;
1440 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001441
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001442 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001443 return NULL;
1444
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001445 pdev = to_pci_dev(info->dev);
1446
1447 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001448 return NULL;
1449
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001450 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001451 return NULL;
1452
Yu Zhao93a23a72009-05-18 13:51:37 +08001453 return info;
1454}
1455
1456static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1457{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001458 struct pci_dev *pdev;
1459
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001460 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001461 return;
1462
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001463 pdev = to_pci_dev(info->dev);
1464 if (pci_enable_ats(pdev, VTD_PAGE_SHIFT))
1465 return;
1466
1467 info->ats.enabled = 1;
1468 info->ats.qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001469}
1470
1471static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1472{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001473 if (!info->ats.enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001474 return;
1475
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001476 pci_disable_ats(to_pci_dev(info->dev));
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001477 info->ats.enabled = 0;
Yu Zhao93a23a72009-05-18 13:51:37 +08001478}
1479
1480static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1481 u64 addr, unsigned mask)
1482{
1483 u16 sid, qdep;
1484 unsigned long flags;
1485 struct device_domain_info *info;
1486
1487 spin_lock_irqsave(&device_domain_lock, flags);
1488 list_for_each_entry(info, &domain->devices, link) {
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001489 if (!info->ats.enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001490 continue;
1491
1492 sid = info->bus << 8 | info->devfn;
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001493 qdep = info->ats.qdep;
Yu Zhao93a23a72009-05-18 13:51:37 +08001494 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1495 }
1496 spin_unlock_irqrestore(&device_domain_lock, flags);
1497}
1498
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001499static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1500 struct dmar_domain *domain,
1501 unsigned long pfn, unsigned int pages,
1502 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001503{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001504 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001505 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001506 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001507
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001508 BUG_ON(pages == 0);
1509
David Woodhouseea8ea462014-03-05 17:09:32 +00001510 if (ih)
1511 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001512 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001513 * Fallback to domain selective flush if no PSI support or the size is
1514 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001515 * PSI requires page size to be 2 ^ x, and the base address is naturally
1516 * aligned to the size
1517 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001518 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1519 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001520 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001521 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001522 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001523 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001524
1525 /*
Nadav Amit82653632010-04-01 13:24:40 +03001526 * In caching mode, changes of pages from non-present to present require
1527 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001528 */
Nadav Amit82653632010-04-01 13:24:40 +03001529 if (!cap_caching_mode(iommu->cap) || !map)
Joerg Roedel9452d5b2015-07-21 10:00:56 +02001530 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1531 addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001532}
1533
mark grossf8bab732008-02-08 04:18:38 -08001534static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1535{
1536 u32 pmen;
1537 unsigned long flags;
1538
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001539 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001540 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1541 pmen &= ~DMA_PMEN_EPM;
1542 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1543
1544 /* wait for the protected region status bit to clear */
1545 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1546 readl, !(pmen & DMA_PMEN_PRS), pmen);
1547
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001548 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001549}
1550
Jiang Liu2a41cce2014-07-11 14:19:33 +08001551static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001552{
1553 u32 sts;
1554 unsigned long flags;
1555
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001556 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001557 iommu->gcmd |= DMA_GCMD_TE;
1558 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001559
1560 /* Make sure hardware complete it */
1561 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001562 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001563
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001564 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001565}
1566
Jiang Liu2a41cce2014-07-11 14:19:33 +08001567static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001568{
1569 u32 sts;
1570 unsigned long flag;
1571
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001572 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001573 iommu->gcmd &= ~DMA_GCMD_TE;
1574 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1575
1576 /* Make sure hardware complete it */
1577 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001578 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001579
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001580 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001581}
1582
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001583
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001584static int iommu_init_domains(struct intel_iommu *iommu)
1585{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001586 u32 ndomains, nlongs;
1587 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001588
1589 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001590 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001591 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001592 nlongs = BITS_TO_LONGS(ndomains);
1593
Donald Dutile94a91b502009-08-20 16:51:34 -04001594 spin_lock_init(&iommu->lock);
1595
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001596 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1597 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001598 pr_err("%s: Allocating domain id array failed\n",
1599 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001600 return -ENOMEM;
1601 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001602
1603 size = ((ndomains >> 8) + 1) * sizeof(struct dmar_domain **);
1604 iommu->domains = kzalloc(size, GFP_KERNEL);
1605
1606 if (iommu->domains) {
1607 size = 256 * sizeof(struct dmar_domain *);
1608 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1609 }
1610
1611 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001612 pr_err("%s: Allocating domain array failed\n",
1613 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001614 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001615 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001616 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001617 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001618 return -ENOMEM;
1619 }
1620
Joerg Roedel8bf47812015-07-21 10:41:21 +02001621
1622
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001623 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001624 * If Caching mode is set, then invalid translations are tagged
1625 * with domain-id 0, hence we need to pre-allocate it. We also
1626 * use domain-id 0 as a marker for non-allocated domain-id, so
1627 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001628 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001629 set_bit(0, iommu->domain_ids);
1630
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001631 return 0;
1632}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001633
Jiang Liuffebeb42014-11-09 22:48:02 +08001634static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001635{
Joerg Roedel29a27712015-07-21 17:17:12 +02001636 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001637 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001638
Joerg Roedel29a27712015-07-21 17:17:12 +02001639 if (!iommu->domains || !iommu->domain_ids)
1640 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001641
Joerg Roedel55d94042015-07-22 16:50:40 +02001642 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001643 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1644 struct dmar_domain *domain;
1645
1646 if (info->iommu != iommu)
1647 continue;
1648
1649 if (!info->dev || !info->domain)
1650 continue;
1651
1652 domain = info->domain;
1653
Joerg Roedele6de0f82015-07-22 16:30:36 +02001654 dmar_remove_one_dev_info(domain, info->dev);
Joerg Roedel29a27712015-07-21 17:17:12 +02001655
1656 if (!domain_type_is_vm_or_si(domain))
1657 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001658 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001659 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001660
1661 if (iommu->gcmd & DMA_GCMD_TE)
1662 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001663}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001664
Jiang Liuffebeb42014-11-09 22:48:02 +08001665static void free_dmar_iommu(struct intel_iommu *iommu)
1666{
1667 if ((iommu->domains) && (iommu->domain_ids)) {
Joerg Roedel8bf47812015-07-21 10:41:21 +02001668 int elems = (cap_ndoms(iommu->cap) >> 8) + 1;
1669 int i;
1670
1671 for (i = 0; i < elems; i++)
1672 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001673 kfree(iommu->domains);
1674 kfree(iommu->domain_ids);
1675 iommu->domains = NULL;
1676 iommu->domain_ids = NULL;
1677 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001678
Weidong Hand9630fe2008-12-08 11:06:32 +08001679 g_iommus[iommu->seq_id] = NULL;
1680
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001681 /* free context mapping */
1682 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001683}
1684
Jiang Liuab8dfe22014-07-11 14:19:27 +08001685static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001686{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001687 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001688
1689 domain = alloc_domain_mem();
1690 if (!domain)
1691 return NULL;
1692
Jiang Liuab8dfe22014-07-11 14:19:27 +08001693 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001694 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001695 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001696 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001697
1698 return domain;
1699}
1700
Joerg Roedeld160aca2015-07-22 11:52:53 +02001701/* Must be called with iommu->lock */
1702static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001703 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001704{
Jiang Liu44bde612014-07-11 14:19:29 +08001705 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001706 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001707
Joerg Roedel55d94042015-07-22 16:50:40 +02001708 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001709 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001710
Joerg Roedel29a27712015-07-21 17:17:12 +02001711 domain->iommu_refcnt[iommu->seq_id] += 1;
1712 domain->iommu_count += 1;
1713 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001714 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001715 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1716
1717 if (num >= ndomains) {
1718 pr_err("%s: No free domain ids\n", iommu->name);
1719 domain->iommu_refcnt[iommu->seq_id] -= 1;
1720 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001721 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001722 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001723
Joerg Roedeld160aca2015-07-22 11:52:53 +02001724 set_bit(num, iommu->domain_ids);
1725 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001726
Joerg Roedeld160aca2015-07-22 11:52:53 +02001727 domain->iommu_did[iommu->seq_id] = num;
1728 domain->nid = iommu->node;
1729
Jiang Liufb170fb2014-07-11 14:19:28 +08001730 domain_update_iommu_cap(domain);
1731 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001732
Joerg Roedel55d94042015-07-22 16:50:40 +02001733 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001734}
1735
1736static int domain_detach_iommu(struct dmar_domain *domain,
1737 struct intel_iommu *iommu)
1738{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001739 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001740
Joerg Roedel55d94042015-07-22 16:50:40 +02001741 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001742 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001743
Joerg Roedel29a27712015-07-21 17:17:12 +02001744 domain->iommu_refcnt[iommu->seq_id] -= 1;
1745 count = --domain->iommu_count;
1746 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001747 num = domain->iommu_did[iommu->seq_id];
1748 clear_bit(num, iommu->domain_ids);
1749 set_iommu_domain(iommu, num, NULL);
1750
Jiang Liufb170fb2014-07-11 14:19:28 +08001751 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001752 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001753 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001754
1755 return count;
1756}
1757
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001758static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001759static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001760
Joseph Cihula51a63e62011-03-21 11:04:24 -07001761static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001762{
1763 struct pci_dev *pdev = NULL;
1764 struct iova *iova;
1765 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001766
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001767 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1768 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001769
Mark Gross8a443df2008-03-04 14:59:31 -08001770 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1771 &reserved_rbtree_key);
1772
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001773 /* IOAPIC ranges shouldn't be accessed by DMA */
1774 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1775 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001776 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001777 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001778 return -ENODEV;
1779 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001780
1781 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1782 for_each_pci_dev(pdev) {
1783 struct resource *r;
1784
1785 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1786 r = &pdev->resource[i];
1787 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1788 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001789 iova = reserve_iova(&reserved_iova_list,
1790 IOVA_PFN(r->start),
1791 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001792 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001793 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001794 return -ENODEV;
1795 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001796 }
1797 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001798 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001799}
1800
1801static void domain_reserve_special_ranges(struct dmar_domain *domain)
1802{
1803 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1804}
1805
1806static inline int guestwidth_to_adjustwidth(int gaw)
1807{
1808 int agaw;
1809 int r = (gaw - 12) % 9;
1810
1811 if (r == 0)
1812 agaw = gaw;
1813 else
1814 agaw = gaw + 9 - r;
1815 if (agaw > 64)
1816 agaw = 64;
1817 return agaw;
1818}
1819
Joerg Roedeldc534b22015-07-22 12:44:02 +02001820static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1821 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001822{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001823 int adjust_width, agaw;
1824 unsigned long sagaw;
1825
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001826 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1827 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001828 domain_reserve_special_ranges(domain);
1829
1830 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001831 if (guest_width > cap_mgaw(iommu->cap))
1832 guest_width = cap_mgaw(iommu->cap);
1833 domain->gaw = guest_width;
1834 adjust_width = guestwidth_to_adjustwidth(guest_width);
1835 agaw = width_to_agaw(adjust_width);
1836 sagaw = cap_sagaw(iommu->cap);
1837 if (!test_bit(agaw, &sagaw)) {
1838 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001839 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001840 agaw = find_next_bit(&sagaw, 5, agaw);
1841 if (agaw >= 5)
1842 return -ENODEV;
1843 }
1844 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001845
Weidong Han8e6040972008-12-08 15:49:06 +08001846 if (ecap_coherent(iommu->ecap))
1847 domain->iommu_coherency = 1;
1848 else
1849 domain->iommu_coherency = 0;
1850
Sheng Yang58c610b2009-03-18 15:33:05 +08001851 if (ecap_sc_support(iommu->ecap))
1852 domain->iommu_snooping = 1;
1853 else
1854 domain->iommu_snooping = 0;
1855
David Woodhouse214e39a2014-03-19 10:38:49 +00001856 if (intel_iommu_superpage)
1857 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1858 else
1859 domain->iommu_superpage = 0;
1860
Suresh Siddha4c923d42009-10-02 11:01:24 -07001861 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001862
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001864 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001865 if (!domain->pgd)
1866 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001867 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001868 return 0;
1869}
1870
1871static void domain_exit(struct dmar_domain *domain)
1872{
David Woodhouseea8ea462014-03-05 17:09:32 +00001873 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001874
1875 /* Domain 0 is reserved, so dont process it */
1876 if (!domain)
1877 return;
1878
Alex Williamson7b668352011-05-24 12:02:41 +01001879 /* Flush any lazy unmaps that may reference this domain */
1880 if (!intel_iommu_strict)
1881 flush_unmaps_timeout(0);
1882
Joerg Roedeld160aca2015-07-22 11:52:53 +02001883 /* Remove associated devices and clear attached or cached domains */
1884 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001885 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001886 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001887
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001888 /* destroy iovas */
1889 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001890
David Woodhouseea8ea462014-03-05 17:09:32 +00001891 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001892
David Woodhouseea8ea462014-03-05 17:09:32 +00001893 dma_free_pagelist(freelist);
1894
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001895 free_domain_mem(domain);
1896}
1897
David Woodhouse64ae8922014-03-09 12:52:30 -07001898static int domain_context_mapping_one(struct dmar_domain *domain,
1899 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001900 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001901{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001902 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001903 int translation = CONTEXT_TT_MULTI_LEVEL;
1904 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001905 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001906 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001907 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02001908 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001909
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001910 WARN_ON(did == 0);
1911
Joerg Roedel28ccce02015-07-21 14:45:31 +02001912 if (hw_pass_through && domain_type_is_si(domain))
1913 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001914
1915 pr_debug("Set context mapping for %02x:%02x.%d\n",
1916 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001917
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001918 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001919
Joerg Roedel55d94042015-07-22 16:50:40 +02001920 spin_lock_irqsave(&device_domain_lock, flags);
1921 spin_lock(&iommu->lock);
1922
1923 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00001924 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001925 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02001926 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001927
Joerg Roedel55d94042015-07-22 16:50:40 +02001928 ret = 0;
1929 if (context_present(context))
1930 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02001931
Weidong Hanea6606b2008-12-08 23:08:15 +08001932 pgd = domain->pgd;
1933
Joerg Roedelde24e552015-07-21 14:53:04 +02001934 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001935 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08001936
Joerg Roedelde24e552015-07-21 14:53:04 +02001937 /*
1938 * Skip top levels of page tables for iommu which has less agaw
1939 * than default. Unnecessary for PT mode.
1940 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001941 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02001942 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02001943 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02001944 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02001945 if (!dma_pte_present(pgd))
1946 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02001947 }
1948
David Woodhouse64ae8922014-03-09 12:52:30 -07001949 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001950 translation = info ? CONTEXT_TT_DEV_IOTLB :
1951 CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02001952
Yu Zhao93a23a72009-05-18 13:51:37 +08001953 context_set_address_root(context, virt_to_phys(pgd));
1954 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02001955 } else {
1956 /*
1957 * In pass through mode, AW must be programmed to
1958 * indicate the largest AGAW value supported by
1959 * hardware. And ASR is ignored by hardware.
1960 */
1961 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001962 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001963
1964 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001965 context_set_fault_enable(context);
1966 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001967 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001968
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001969 /*
1970 * It's a non-present to present mapping. If hardware doesn't cache
1971 * non-present entry we only need to flush the write-buffer. If the
1972 * _does_ cache non-present entries, then it does so in the special
1973 * domain #0, which we have to flush:
1974 */
1975 if (cap_caching_mode(iommu->cap)) {
1976 iommu->flush.flush_context(iommu, 0,
1977 (((u16)bus) << 8) | devfn,
1978 DMA_CCMD_MASK_NOBIT,
1979 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001980 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001981 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001982 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001983 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001984 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08001985
Joerg Roedel55d94042015-07-22 16:50:40 +02001986 ret = 0;
1987
1988out_unlock:
1989 spin_unlock(&iommu->lock);
1990 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001991
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001992 return 0;
1993}
1994
Alex Williamson579305f2014-07-03 09:51:43 -06001995struct domain_context_mapping_data {
1996 struct dmar_domain *domain;
1997 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06001998};
1999
2000static int domain_context_mapping_cb(struct pci_dev *pdev,
2001 u16 alias, void *opaque)
2002{
2003 struct domain_context_mapping_data *data = opaque;
2004
2005 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002006 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002007}
2008
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002009static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002010domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002011{
David Woodhouse64ae8922014-03-09 12:52:30 -07002012 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002013 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002014 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002015
David Woodhousee1f167f2014-03-09 15:24:46 -07002016 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002017 if (!iommu)
2018 return -ENODEV;
2019
Alex Williamson579305f2014-07-03 09:51:43 -06002020 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002021 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002022
2023 data.domain = domain;
2024 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002025
2026 return pci_for_each_dma_alias(to_pci_dev(dev),
2027 &domain_context_mapping_cb, &data);
2028}
2029
2030static int domain_context_mapped_cb(struct pci_dev *pdev,
2031 u16 alias, void *opaque)
2032{
2033 struct intel_iommu *iommu = opaque;
2034
2035 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002036}
2037
David Woodhousee1f167f2014-03-09 15:24:46 -07002038static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002039{
Weidong Han5331fe62008-12-08 23:00:00 +08002040 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002041 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002042
David Woodhousee1f167f2014-03-09 15:24:46 -07002043 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002044 if (!iommu)
2045 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002046
Alex Williamson579305f2014-07-03 09:51:43 -06002047 if (!dev_is_pci(dev))
2048 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002049
Alex Williamson579305f2014-07-03 09:51:43 -06002050 return !pci_for_each_dma_alias(to_pci_dev(dev),
2051 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002052}
2053
Fenghua Yuf5329592009-08-04 15:09:37 -07002054/* Returns a number of VTD pages, but aligned to MM page size */
2055static inline unsigned long aligned_nrpages(unsigned long host_addr,
2056 size_t size)
2057{
2058 host_addr &= ~PAGE_MASK;
2059 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2060}
2061
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002062/* Return largest possible superpage level for a given mapping */
2063static inline int hardware_largepage_caps(struct dmar_domain *domain,
2064 unsigned long iov_pfn,
2065 unsigned long phy_pfn,
2066 unsigned long pages)
2067{
2068 int support, level = 1;
2069 unsigned long pfnmerge;
2070
2071 support = domain->iommu_superpage;
2072
2073 /* To use a large page, the virtual *and* physical addresses
2074 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2075 of them will mean we have to use smaller pages. So just
2076 merge them and check both at once. */
2077 pfnmerge = iov_pfn | phy_pfn;
2078
2079 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2080 pages >>= VTD_STRIDE_SHIFT;
2081 if (!pages)
2082 break;
2083 pfnmerge >>= VTD_STRIDE_SHIFT;
2084 level++;
2085 support--;
2086 }
2087 return level;
2088}
2089
David Woodhouse9051aa02009-06-29 12:30:54 +01002090static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2091 struct scatterlist *sg, unsigned long phys_pfn,
2092 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002093{
2094 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002095 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002096 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002097 unsigned int largepage_lvl = 0;
2098 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002099
Jiang Liu162d1b12014-07-11 14:19:35 +08002100 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002101
2102 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2103 return -EINVAL;
2104
2105 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2106
Jiang Liucc4f14a2014-11-26 09:42:10 +08002107 if (!sg) {
2108 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002109 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2110 }
2111
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002112 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002113 uint64_t tmp;
2114
David Woodhousee1605492009-06-29 11:17:38 +01002115 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002116 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002117 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2118 sg->dma_length = sg->length;
Dan Williamsdb0fa0c2015-08-17 08:13:26 -06002119 pteval = (sg_phys(sg) & PAGE_MASK) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002120 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002121 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002122
David Woodhousee1605492009-06-29 11:17:38 +01002123 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002124 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2125
David Woodhouse5cf0a762014-03-19 16:07:49 +00002126 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002127 if (!pte)
2128 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002129 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002130 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002131 unsigned long nr_superpages, end_pfn;
2132
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002133 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002134 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002135
2136 nr_superpages = sg_res / lvl_pages;
2137 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2138
Jiang Liud41a4ad2014-07-11 14:19:34 +08002139 /*
2140 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002141 * removed to make room for superpage(s).
Jiang Liud41a4ad2014-07-11 14:19:34 +08002142 */
Christian Zanderba2374f2015-06-10 09:41:45 -07002143 dma_pte_free_pagetable(domain, iov_pfn, end_pfn);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002144 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002145 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002146 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002147
David Woodhousee1605492009-06-29 11:17:38 +01002148 }
2149 /* We don't need lock here, nobody else
2150 * touches the iova range
2151 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002152 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002153 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002154 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002155 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2156 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002157 if (dumps) {
2158 dumps--;
2159 debug_dma_dump_mappings(NULL);
2160 }
2161 WARN_ON(1);
2162 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002163
2164 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2165
2166 BUG_ON(nr_pages < lvl_pages);
2167 BUG_ON(sg_res < lvl_pages);
2168
2169 nr_pages -= lvl_pages;
2170 iov_pfn += lvl_pages;
2171 phys_pfn += lvl_pages;
2172 pteval += lvl_pages * VTD_PAGE_SIZE;
2173 sg_res -= lvl_pages;
2174
2175 /* If the next PTE would be the first in a new page, then we
2176 need to flush the cache on the entries we've just written.
2177 And then we'll need to recalculate 'pte', so clear it and
2178 let it get set again in the if (!pte) block above.
2179
2180 If we're done (!nr_pages) we need to flush the cache too.
2181
2182 Also if we've been setting superpages, we may need to
2183 recalculate 'pte' and switch back to smaller pages for the
2184 end of the mapping, if the trailing size is not enough to
2185 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002186 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002187 if (!nr_pages || first_pte_in_page(pte) ||
2188 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002189 domain_flush_cache(domain, first_pte,
2190 (void *)pte - (void *)first_pte);
2191 pte = NULL;
2192 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002193
2194 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002195 sg = sg_next(sg);
2196 }
2197 return 0;
2198}
2199
David Woodhouse9051aa02009-06-29 12:30:54 +01002200static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2201 struct scatterlist *sg, unsigned long nr_pages,
2202 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002203{
David Woodhouse9051aa02009-06-29 12:30:54 +01002204 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2205}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002206
David Woodhouse9051aa02009-06-29 12:30:54 +01002207static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2208 unsigned long phys_pfn, unsigned long nr_pages,
2209 int prot)
2210{
2211 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002212}
2213
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002214static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002215{
Weidong Hanc7151a82008-12-08 22:51:37 +08002216 if (!iommu)
2217 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002218
2219 clear_context_table(iommu, bus, devfn);
2220 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002221 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002222 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002223}
2224
David Woodhouse109b9b02012-05-25 17:43:02 +01002225static inline void unlink_domain_info(struct device_domain_info *info)
2226{
2227 assert_spin_locked(&device_domain_lock);
2228 list_del(&info->link);
2229 list_del(&info->global);
2230 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002231 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002232}
2233
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002234static void domain_remove_dev_info(struct dmar_domain *domain)
2235{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002236 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002237 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002238
2239 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002240 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002241 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002242 spin_unlock_irqrestore(&device_domain_lock, flags);
2243}
2244
2245/*
2246 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002247 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002248 */
David Woodhouse1525a292014-03-06 16:19:30 +00002249static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002250{
2251 struct device_domain_info *info;
2252
2253 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002254 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002255 if (info)
2256 return info->domain;
2257 return NULL;
2258}
2259
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002260static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002261dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2262{
2263 struct device_domain_info *info;
2264
2265 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002266 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002267 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002268 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002269
2270 return NULL;
2271}
2272
Joerg Roedel5db31562015-07-22 12:40:43 +02002273static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2274 int bus, int devfn,
2275 struct device *dev,
2276 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002277{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002278 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002279 struct device_domain_info *info;
2280 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002281 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002282
2283 info = alloc_devinfo_mem();
2284 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002285 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002286
Jiang Liu745f2582014-02-19 14:07:26 +08002287 info->bus = bus;
2288 info->devfn = devfn;
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05002289 info->ats.enabled = 0;
2290 info->ats.qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002291 info->dev = dev;
2292 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002293 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002294
2295 spin_lock_irqsave(&device_domain_lock, flags);
2296 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002297 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002298
2299 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002300 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002301 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002302 if (info2) {
2303 found = info2->domain;
2304 info2->dev = dev;
2305 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002306 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002307
Jiang Liu745f2582014-02-19 14:07:26 +08002308 if (found) {
2309 spin_unlock_irqrestore(&device_domain_lock, flags);
2310 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002311 /* Caller must free the original domain */
2312 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002313 }
2314
Joerg Roedeld160aca2015-07-22 11:52:53 +02002315 spin_lock(&iommu->lock);
2316 ret = domain_attach_iommu(domain, iommu);
2317 spin_unlock(&iommu->lock);
2318
2319 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002320 spin_unlock_irqrestore(&device_domain_lock, flags);
2321 return NULL;
2322 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002323
David Woodhouseb718cd32014-03-09 13:11:33 -07002324 list_add(&info->link, &domain->devices);
2325 list_add(&info->global, &device_domain_list);
2326 if (dev)
2327 dev->archdata.iommu = info;
2328 spin_unlock_irqrestore(&device_domain_lock, flags);
2329
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002330 if (dev && domain_context_mapping(domain, dev)) {
2331 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002332 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002333 return NULL;
2334 }
2335
David Woodhouseb718cd32014-03-09 13:11:33 -07002336 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002337}
2338
Alex Williamson579305f2014-07-03 09:51:43 -06002339static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2340{
2341 *(u16 *)opaque = alias;
2342 return 0;
2343}
2344
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002345/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002346static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002347{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002348 struct device_domain_info *info = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002349 struct dmar_domain *domain, *tmp;
2350 struct intel_iommu *iommu;
Joerg Roedel08a7f452015-07-23 18:09:11 +02002351 u16 req_id, dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002352 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002353 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002354
David Woodhouse146922e2014-03-09 15:44:17 -07002355 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002356 if (domain)
2357 return domain;
2358
David Woodhouse146922e2014-03-09 15:44:17 -07002359 iommu = device_to_iommu(dev, &bus, &devfn);
2360 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002361 return NULL;
2362
Joerg Roedel08a7f452015-07-23 18:09:11 +02002363 req_id = ((u16)bus << 8) | devfn;
2364
Alex Williamson579305f2014-07-03 09:51:43 -06002365 if (dev_is_pci(dev)) {
2366 struct pci_dev *pdev = to_pci_dev(dev);
2367
2368 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2369
2370 spin_lock_irqsave(&device_domain_lock, flags);
2371 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2372 PCI_BUS_NUM(dma_alias),
2373 dma_alias & 0xff);
2374 if (info) {
2375 iommu = info->iommu;
2376 domain = info->domain;
2377 }
2378 spin_unlock_irqrestore(&device_domain_lock, flags);
2379
2380 /* DMA alias already has a domain, uses it */
2381 if (info)
2382 goto found_domain;
2383 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002384
David Woodhouse146922e2014-03-09 15:44:17 -07002385 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002386 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002387 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002388 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002389 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002390 domain_exit(domain);
2391 return NULL;
2392 }
2393
2394 /* register PCI DMA alias device */
Joerg Roedel08a7f452015-07-23 18:09:11 +02002395 if (req_id != dma_alias && dev_is_pci(dev)) {
Joerg Roedel5db31562015-07-22 12:40:43 +02002396 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2397 dma_alias & 0xff, NULL, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002398
2399 if (!tmp || tmp != domain) {
2400 domain_exit(domain);
2401 domain = tmp;
2402 }
2403
David Woodhouseb718cd32014-03-09 13:11:33 -07002404 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002405 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002406 }
2407
2408found_domain:
Joerg Roedel5db31562015-07-22 12:40:43 +02002409 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002410
2411 if (!tmp || tmp != domain) {
2412 domain_exit(domain);
2413 domain = tmp;
2414 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002415
2416 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002417}
2418
David Woodhouseb2132032009-06-26 18:50:28 +01002419static int iommu_domain_identity_map(struct dmar_domain *domain,
2420 unsigned long long start,
2421 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002422{
David Woodhousec5395d52009-06-28 16:35:56 +01002423 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2424 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002425
David Woodhousec5395d52009-06-28 16:35:56 +01002426 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2427 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002428 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002429 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002430 }
2431
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002432 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002433 /*
2434 * RMRR range might have overlap with physical memory range,
2435 * clear it first
2436 */
David Woodhousec5395d52009-06-28 16:35:56 +01002437 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002438
David Woodhousec5395d52009-06-28 16:35:56 +01002439 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2440 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002441 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002442}
2443
David Woodhouse0b9d9752014-03-09 15:48:15 -07002444static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002445 unsigned long long start,
2446 unsigned long long end)
2447{
2448 struct dmar_domain *domain;
2449 int ret;
2450
David Woodhouse0b9d9752014-03-09 15:48:15 -07002451 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002452 if (!domain)
2453 return -ENOMEM;
2454
David Woodhouse19943b02009-08-04 16:19:20 +01002455 /* For _hardware_ passthrough, don't bother. But for software
2456 passthrough, we do it anyway -- it may indicate a memory
2457 range which is reserved in E820, so which didn't get set
2458 up to start with in si_domain */
2459 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002460 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2461 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002462 return 0;
2463 }
2464
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002465 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2466 dev_name(dev), start, end);
2467
David Woodhouse5595b522009-12-02 09:21:55 +00002468 if (end < start) {
2469 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2470 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2471 dmi_get_system_info(DMI_BIOS_VENDOR),
2472 dmi_get_system_info(DMI_BIOS_VERSION),
2473 dmi_get_system_info(DMI_PRODUCT_VERSION));
2474 ret = -EIO;
2475 goto error;
2476 }
2477
David Woodhouse2ff729f2009-08-26 14:25:41 +01002478 if (end >> agaw_to_width(domain->agaw)) {
2479 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2480 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2481 agaw_to_width(domain->agaw),
2482 dmi_get_system_info(DMI_BIOS_VENDOR),
2483 dmi_get_system_info(DMI_BIOS_VERSION),
2484 dmi_get_system_info(DMI_PRODUCT_VERSION));
2485 ret = -EIO;
2486 goto error;
2487 }
David Woodhouse19943b02009-08-04 16:19:20 +01002488
David Woodhouseb2132032009-06-26 18:50:28 +01002489 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002490 if (ret)
2491 goto error;
2492
David Woodhouseb2132032009-06-26 18:50:28 +01002493 return 0;
2494
2495 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002496 domain_exit(domain);
2497 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002498}
2499
2500static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002501 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002502{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002503 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002504 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002505 return iommu_prepare_identity_map(dev, rmrr->base_address,
2506 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002507}
2508
Suresh Siddhad3f13812011-08-23 17:05:25 -07002509#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002510static inline void iommu_prepare_isa(void)
2511{
2512 struct pci_dev *pdev;
2513 int ret;
2514
2515 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2516 if (!pdev)
2517 return;
2518
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002519 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002520 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002521
2522 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002523 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002524
Yijing Wang9b27e822014-05-20 20:37:52 +08002525 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002526}
2527#else
2528static inline void iommu_prepare_isa(void)
2529{
2530 return;
2531}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002532#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002533
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002534static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002535
Matt Kraai071e1372009-08-23 22:30:22 -07002536static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002537{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002538 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002539
Jiang Liuab8dfe22014-07-11 14:19:27 +08002540 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002541 if (!si_domain)
2542 return -EFAULT;
2543
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002544 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2545 domain_exit(si_domain);
2546 return -EFAULT;
2547 }
2548
Joerg Roedel0dc79712015-07-21 15:40:06 +02002549 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002550
David Woodhouse19943b02009-08-04 16:19:20 +01002551 if (hw)
2552 return 0;
2553
David Woodhousec7ab48d2009-06-26 19:10:36 +01002554 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002555 unsigned long start_pfn, end_pfn;
2556 int i;
2557
2558 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2559 ret = iommu_domain_identity_map(si_domain,
2560 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2561 if (ret)
2562 return ret;
2563 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002564 }
2565
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002566 return 0;
2567}
2568
David Woodhouse9b226622014-03-09 14:03:28 -07002569static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002570{
2571 struct device_domain_info *info;
2572
2573 if (likely(!iommu_identity_mapping))
2574 return 0;
2575
David Woodhouse9b226622014-03-09 14:03:28 -07002576 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002577 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2578 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002579
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002580 return 0;
2581}
2582
Joerg Roedel28ccce02015-07-21 14:45:31 +02002583static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002584{
David Woodhouse0ac72662014-03-09 13:19:22 -07002585 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002586 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002587 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002588
David Woodhouse5913c9b2014-03-09 16:27:31 -07002589 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002590 if (!iommu)
2591 return -ENODEV;
2592
Joerg Roedel5db31562015-07-22 12:40:43 +02002593 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002594 if (ndomain != domain)
2595 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002596
2597 return 0;
2598}
2599
David Woodhouse0b9d9752014-03-09 15:48:15 -07002600static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002601{
2602 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002603 struct device *tmp;
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002604 int i;
2605
Jiang Liu0e2426122014-02-19 14:07:34 +08002606 rcu_read_lock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002607 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002608 /*
2609 * Return TRUE if this RMRR contains the device that
2610 * is passed in.
2611 */
2612 for_each_active_dev_scope(rmrr->devices,
2613 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002614 if (tmp == dev) {
Jiang Liu0e2426122014-02-19 14:07:34 +08002615 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002616 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002617 }
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002618 }
Jiang Liu0e2426122014-02-19 14:07:34 +08002619 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002620 return false;
2621}
2622
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002623/*
2624 * There are a couple cases where we need to restrict the functionality of
2625 * devices associated with RMRRs. The first is when evaluating a device for
2626 * identity mapping because problems exist when devices are moved in and out
2627 * of domains and their respective RMRR information is lost. This means that
2628 * a device with associated RMRRs will never be in a "passthrough" domain.
2629 * The second is use of the device through the IOMMU API. This interface
2630 * expects to have full control of the IOVA space for the device. We cannot
2631 * satisfy both the requirement that RMRR access is maintained and have an
2632 * unencumbered IOVA space. We also have no ability to quiesce the device's
2633 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2634 * We therefore prevent devices associated with an RMRR from participating in
2635 * the IOMMU API, which eliminates them from device assignment.
2636 *
2637 * In both cases we assume that PCI USB devices with RMRRs have them largely
2638 * for historical reasons and that the RMRR space is not actively used post
2639 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002640 *
2641 * The same exception is made for graphics devices, with the requirement that
2642 * any use of the RMRR regions will be torn down before assigning the device
2643 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002644 */
2645static bool device_is_rmrr_locked(struct device *dev)
2646{
2647 if (!device_has_rmrr(dev))
2648 return false;
2649
2650 if (dev_is_pci(dev)) {
2651 struct pci_dev *pdev = to_pci_dev(dev);
2652
David Woodhouse18436af2015-03-25 15:05:47 +00002653 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002654 return false;
2655 }
2656
2657 return true;
2658}
2659
David Woodhouse3bdb2592014-03-09 16:03:08 -07002660static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002661{
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002662
David Woodhouse3bdb2592014-03-09 16:03:08 -07002663 if (dev_is_pci(dev)) {
2664 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002665
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002666 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002667 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002668
David Woodhouse3bdb2592014-03-09 16:03:08 -07002669 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2670 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002671
David Woodhouse3bdb2592014-03-09 16:03:08 -07002672 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2673 return 1;
2674
2675 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2676 return 0;
2677
2678 /*
2679 * We want to start off with all devices in the 1:1 domain, and
2680 * take them out later if we find they can't access all of memory.
2681 *
2682 * However, we can't do this for PCI devices behind bridges,
2683 * because all PCI devices behind the same bridge will end up
2684 * with the same source-id on their transactions.
2685 *
2686 * Practically speaking, we can't change things around for these
2687 * devices at run-time, because we can't be sure there'll be no
2688 * DMA transactions in flight for any of their siblings.
2689 *
2690 * So PCI devices (unless they're on the root bus) as well as
2691 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2692 * the 1:1 domain, just in _case_ one of their siblings turns out
2693 * not to be able to map all of memory.
2694 */
2695 if (!pci_is_pcie(pdev)) {
2696 if (!pci_is_root_bus(pdev->bus))
2697 return 0;
2698 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2699 return 0;
2700 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2701 return 0;
2702 } else {
2703 if (device_has_rmrr(dev))
2704 return 0;
2705 }
David Woodhouse6941af22009-07-04 18:24:27 +01002706
David Woodhouse3dfc8132009-07-04 19:11:08 +01002707 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002708 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002709 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002710 * take them out of the 1:1 domain later.
2711 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002712 if (!startup) {
2713 /*
2714 * If the device's dma_mask is less than the system's memory
2715 * size then this is not a candidate for identity mapping.
2716 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002717 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002718
David Woodhouse3bdb2592014-03-09 16:03:08 -07002719 if (dev->coherent_dma_mask &&
2720 dev->coherent_dma_mask < dma_mask)
2721 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002722
David Woodhouse3bdb2592014-03-09 16:03:08 -07002723 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002724 }
David Woodhouse6941af22009-07-04 18:24:27 +01002725
2726 return 1;
2727}
2728
David Woodhousecf04eee2014-03-21 16:49:04 +00002729static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2730{
2731 int ret;
2732
2733 if (!iommu_should_identity_map(dev, 1))
2734 return 0;
2735
Joerg Roedel28ccce02015-07-21 14:45:31 +02002736 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002737 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002738 pr_info("%s identity mapping for device %s\n",
2739 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002740 else if (ret == -ENODEV)
2741 /* device not associated with an iommu */
2742 ret = 0;
2743
2744 return ret;
2745}
2746
2747
Matt Kraai071e1372009-08-23 22:30:22 -07002748static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002749{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002750 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002751 struct dmar_drhd_unit *drhd;
2752 struct intel_iommu *iommu;
2753 struct device *dev;
2754 int i;
2755 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002756
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002757 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002758 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2759 if (ret)
2760 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002761 }
2762
David Woodhousecf04eee2014-03-21 16:49:04 +00002763 for_each_active_iommu(iommu, drhd)
2764 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2765 struct acpi_device_physical_node *pn;
2766 struct acpi_device *adev;
2767
2768 if (dev->bus != &acpi_bus_type)
2769 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002770
David Woodhousecf04eee2014-03-21 16:49:04 +00002771 adev= to_acpi_device(dev);
2772 mutex_lock(&adev->physical_node_lock);
2773 list_for_each_entry(pn, &adev->physical_node_list, node) {
2774 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2775 if (ret)
2776 break;
2777 }
2778 mutex_unlock(&adev->physical_node_lock);
2779 if (ret)
2780 return ret;
2781 }
2782
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002783 return 0;
2784}
2785
Jiang Liuffebeb42014-11-09 22:48:02 +08002786static void intel_iommu_init_qi(struct intel_iommu *iommu)
2787{
2788 /*
2789 * Start from the sane iommu hardware state.
2790 * If the queued invalidation is already initialized by us
2791 * (for example, while enabling interrupt-remapping) then
2792 * we got the things already rolling from a sane state.
2793 */
2794 if (!iommu->qi) {
2795 /*
2796 * Clear any previous faults.
2797 */
2798 dmar_fault(-1, iommu);
2799 /*
2800 * Disable queued invalidation if supported and already enabled
2801 * before OS handover.
2802 */
2803 dmar_disable_qi(iommu);
2804 }
2805
2806 if (dmar_enable_qi(iommu)) {
2807 /*
2808 * Queued Invalidate not enabled, use Register Based Invalidate
2809 */
2810 iommu->flush.flush_context = __iommu_flush_context;
2811 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002812 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002813 iommu->name);
2814 } else {
2815 iommu->flush.flush_context = qi_flush_context;
2816 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002817 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002818 }
2819}
2820
Joerg Roedel091d42e2015-06-12 11:56:10 +02002821static int copy_context_table(struct intel_iommu *iommu,
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002822 struct root_entry __iomem *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02002823 struct context_entry **tbl,
2824 int bus, bool ext)
2825{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002826 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002827 struct context_entry __iomem *old_ce = NULL;
2828 struct context_entry *new_ce = NULL, ce;
2829 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002830 phys_addr_t old_ce_phys;
2831
2832 tbl_idx = ext ? bus * 2 : bus;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002833 memcpy_fromio(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002834
2835 for (devfn = 0; devfn < 256; devfn++) {
2836 /* First calculate the correct index */
2837 idx = (ext ? devfn * 2 : devfn) % 256;
2838
2839 if (idx == 0) {
2840 /* First save what we may have and clean up */
2841 if (new_ce) {
2842 tbl[tbl_idx] = new_ce;
2843 __iommu_flush_cache(iommu, new_ce,
2844 VTD_PAGE_SIZE);
2845 pos = 1;
2846 }
2847
2848 if (old_ce)
2849 iounmap(old_ce);
2850
2851 ret = 0;
2852 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002853 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02002854 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002855 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02002856
2857 if (!old_ce_phys) {
2858 if (ext && devfn == 0) {
2859 /* No LCTP, try UCTP */
2860 devfn = 0x7f;
2861 continue;
2862 } else {
2863 goto out;
2864 }
2865 }
2866
2867 ret = -ENOMEM;
2868 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2869 if (!old_ce)
2870 goto out;
2871
2872 new_ce = alloc_pgtable_page(iommu->node);
2873 if (!new_ce)
2874 goto out_unmap;
2875
2876 ret = 0;
2877 }
2878
2879 /* Now copy the context entry */
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002880 memcpy_fromio(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02002881
Joerg Roedelcf484d02015-06-12 12:21:46 +02002882 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002883 continue;
2884
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002885 did = context_domain_id(&ce);
2886 if (did >= 0 && did < cap_ndoms(iommu->cap))
2887 set_bit(did, iommu->domain_ids);
2888
Joerg Roedelcf484d02015-06-12 12:21:46 +02002889 /*
2890 * We need a marker for copied context entries. This
2891 * marker needs to work for the old format as well as
2892 * for extended context entries.
2893 *
2894 * Bit 67 of the context entry is used. In the old
2895 * format this bit is available to software, in the
2896 * extended format it is the PGE bit, but PGE is ignored
2897 * by HW if PASIDs are disabled (and thus still
2898 * available).
2899 *
2900 * So disable PASIDs first and then mark the entry
2901 * copied. This means that we don't copy PASID
2902 * translations from the old kernel, but this is fine as
2903 * faults there are not fatal.
2904 */
2905 context_clear_pasid_enable(&ce);
2906 context_set_copied(&ce);
2907
Joerg Roedel091d42e2015-06-12 11:56:10 +02002908 new_ce[idx] = ce;
2909 }
2910
2911 tbl[tbl_idx + pos] = new_ce;
2912
2913 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2914
2915out_unmap:
2916 iounmap(old_ce);
2917
2918out:
2919 return ret;
2920}
2921
2922static int copy_translation_tables(struct intel_iommu *iommu)
2923{
Joerg Roedel543c8dc2015-08-13 11:56:59 +02002924 struct root_entry __iomem *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002925 struct context_entry **ctxt_tbls;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002926 phys_addr_t old_rt_phys;
2927 int ctxt_table_entries;
2928 unsigned long flags;
2929 u64 rtaddr_reg;
2930 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002931 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002932
2933 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2934 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002935 new_ext = !!ecap_ecs(iommu->ecap);
2936
2937 /*
2938 * The RTT bit can only be changed when translation is disabled,
2939 * but disabling translation means to open a window for data
2940 * corruption. So bail out and don't copy anything if we would
2941 * have to change the bit.
2942 */
2943 if (new_ext != ext)
2944 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002945
2946 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2947 if (!old_rt_phys)
2948 return -EINVAL;
2949
2950 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2951 if (!old_rt)
2952 return -ENOMEM;
2953
2954 /* This is too big for the stack - allocate it from slab */
2955 ctxt_table_entries = ext ? 512 : 256;
2956 ret = -ENOMEM;
2957 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
2958 if (!ctxt_tbls)
2959 goto out_unmap;
2960
2961 for (bus = 0; bus < 256; bus++) {
2962 ret = copy_context_table(iommu, &old_rt[bus],
2963 ctxt_tbls, bus, ext);
2964 if (ret) {
2965 pr_err("%s: Failed to copy context table for bus %d\n",
2966 iommu->name, bus);
2967 continue;
2968 }
2969 }
2970
2971 spin_lock_irqsave(&iommu->lock, flags);
2972
2973 /* Context tables are copied, now write them to the root_entry table */
2974 for (bus = 0; bus < 256; bus++) {
2975 int idx = ext ? bus * 2 : bus;
2976 u64 val;
2977
2978 if (ctxt_tbls[idx]) {
2979 val = virt_to_phys(ctxt_tbls[idx]) | 1;
2980 iommu->root_entry[bus].lo = val;
2981 }
2982
2983 if (!ext || !ctxt_tbls[idx + 1])
2984 continue;
2985
2986 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
2987 iommu->root_entry[bus].hi = val;
2988 }
2989
2990 spin_unlock_irqrestore(&iommu->lock, flags);
2991
2992 kfree(ctxt_tbls);
2993
2994 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
2995
2996 ret = 0;
2997
2998out_unmap:
2999 iounmap(old_rt);
3000
3001 return ret;
3002}
3003
Joseph Cihulab7792602011-05-03 00:08:37 -07003004static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003005{
3006 struct dmar_drhd_unit *drhd;
3007 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003008 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003009 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003010 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003011 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003012
3013 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003014 * for each drhd
3015 * allocate root
3016 * initialize and program root entry to not present
3017 * endfor
3018 */
3019 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003020 /*
3021 * lock not needed as this is only incremented in the single
3022 * threaded kernel __init code path all other access are read
3023 * only
3024 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003025 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003026 g_num_of_iommus++;
3027 continue;
3028 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003029 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003030 }
3031
Jiang Liuffebeb42014-11-09 22:48:02 +08003032 /* Preallocate enough resources for IOMMU hot-addition */
3033 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3034 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3035
Weidong Hand9630fe2008-12-08 11:06:32 +08003036 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3037 GFP_KERNEL);
3038 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003039 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003040 ret = -ENOMEM;
3041 goto error;
3042 }
3043
mark gross80b20dd2008-04-18 13:53:58 -07003044 deferred_flush = kzalloc(g_num_of_iommus *
3045 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3046 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003047 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003048 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003049 }
3050
Jiang Liu7c919772014-01-06 14:18:18 +08003051 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003052 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003053
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003054 intel_iommu_init_qi(iommu);
3055
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003056 ret = iommu_init_domains(iommu);
3057 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003058 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003059
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003060 init_translation_status(iommu);
3061
Joerg Roedel091d42e2015-06-12 11:56:10 +02003062 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3063 iommu_disable_translation(iommu);
3064 clear_translation_pre_enabled(iommu);
3065 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3066 iommu->name);
3067 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003068
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003069 /*
3070 * TBD:
3071 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003072 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003073 */
3074 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003075 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003076 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003077
Joerg Roedel091d42e2015-06-12 11:56:10 +02003078 if (translation_pre_enabled(iommu)) {
3079 pr_info("Translation already enabled - trying to copy translation structures\n");
3080
3081 ret = copy_translation_tables(iommu);
3082 if (ret) {
3083 /*
3084 * We found the IOMMU with translation
3085 * enabled - but failed to copy over the
3086 * old root-entry table. Try to proceed
3087 * by disabling translation now and
3088 * allocating a clean root-entry table.
3089 * This might cause DMAR faults, but
3090 * probably the dump will still succeed.
3091 */
3092 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3093 iommu->name);
3094 iommu_disable_translation(iommu);
3095 clear_translation_pre_enabled(iommu);
3096 } else {
3097 pr_info("Copied translation tables from previous kernel for %s\n",
3098 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003099 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003100 }
3101 }
3102
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003103 iommu_flush_write_buffer(iommu);
3104 iommu_set_root_entry(iommu);
3105 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3106 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3107
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003108 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003109 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003110 }
3111
David Woodhouse19943b02009-08-04 16:19:20 +01003112 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003113 iommu_identity_mapping |= IDENTMAP_ALL;
3114
Suresh Siddhad3f13812011-08-23 17:05:25 -07003115#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003116 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003117#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003118
Joerg Roedel86080cc2015-06-12 12:27:16 +02003119 if (iommu_identity_mapping) {
3120 ret = si_domain_init(hw_pass_through);
3121 if (ret)
3122 goto free_iommu;
3123 }
3124
David Woodhousee0fc7e02009-09-30 09:12:17 -07003125 check_tylersburg_isoch();
3126
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003127 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003128 * If we copied translations from a previous kernel in the kdump
3129 * case, we can not assign the devices to domains now, as that
3130 * would eliminate the old mappings. So skip this part and defer
3131 * the assignment to device driver initialization time.
3132 */
3133 if (copied_tables)
3134 goto domains_done;
3135
3136 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003137 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003138 * identity mappings for rmrr, gfx, and isa and may fall back to static
3139 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003140 */
David Woodhouse19943b02009-08-04 16:19:20 +01003141 if (iommu_identity_mapping) {
3142 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3143 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003144 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003145 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003146 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003147 }
David Woodhouse19943b02009-08-04 16:19:20 +01003148 /*
3149 * For each rmrr
3150 * for each dev attached to rmrr
3151 * do
3152 * locate drhd for dev, alloc domain for dev
3153 * allocate free domain
3154 * allocate page table entries for rmrr
3155 * if context not allocated for bus
3156 * allocate and init context
3157 * set present in root table for this bus
3158 * init context with domain, translation etc
3159 * endfor
3160 * endfor
3161 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003162 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003163 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003164 /* some BIOS lists non-exist devices in DMAR table. */
3165 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003166 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003167 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003168 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003169 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003170 }
3171 }
3172
3173 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003174
Joerg Roedela87f4912015-06-12 12:32:54 +02003175domains_done:
3176
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003177 /*
3178 * for each drhd
3179 * enable fault log
3180 * global invalidate context cache
3181 * global invalidate iotlb
3182 * enable translation
3183 */
Jiang Liu7c919772014-01-06 14:18:18 +08003184 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003185 if (drhd->ignored) {
3186 /*
3187 * we always have to disable PMRs or DMA may fail on
3188 * this device
3189 */
3190 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003191 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003192 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003193 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003194
3195 iommu_flush_write_buffer(iommu);
3196
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003197 ret = dmar_set_interrupt(iommu);
3198 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003199 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003200
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003201 if (!translation_pre_enabled(iommu))
3202 iommu_enable_translation(iommu);
3203
David Woodhouseb94996c2009-09-19 15:28:12 -07003204 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003205 }
3206
3207 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003208
3209free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003210 for_each_active_iommu(iommu, drhd) {
3211 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003212 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003213 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003214 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003215free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003216 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003217error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003218 return ret;
3219}
3220
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003221/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003222static struct iova *intel_alloc_iova(struct device *dev,
3223 struct dmar_domain *domain,
3224 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003225{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003226 struct iova *iova = NULL;
3227
David Woodhouse875764d2009-06-28 21:20:51 +01003228 /* Restrict dma_mask to the width that the iommu can handle */
3229 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003230 /* Ensure we reserve the whole size-aligned region */
3231 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003232
3233 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003234 /*
3235 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003236 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003237 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003238 */
David Woodhouse875764d2009-06-28 21:20:51 +01003239 iova = alloc_iova(&domain->iovad, nrpages,
3240 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3241 if (iova)
3242 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003243 }
David Woodhouse875764d2009-06-28 21:20:51 +01003244 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3245 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003246 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003247 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003248 return NULL;
3249 }
3250
3251 return iova;
3252}
3253
David Woodhoused4b709f2014-03-09 16:07:40 -07003254static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003255{
3256 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003257
David Woodhoused4b709f2014-03-09 16:07:40 -07003258 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003259 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003260 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003261 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003262 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003263 }
3264
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003265 return domain;
3266}
3267
David Woodhoused4b709f2014-03-09 16:07:40 -07003268static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003269{
3270 struct device_domain_info *info;
3271
3272 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003273 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003274 if (likely(info))
3275 return info->domain;
3276
3277 return __get_valid_domain_for_dev(dev);
3278}
3279
David Woodhouseecb509e2014-03-09 16:29:55 -07003280/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003281static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003282{
3283 int found;
3284
David Woodhouse3d891942014-03-06 15:59:26 +00003285 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003286 return 1;
3287
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003288 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003289 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003290
David Woodhouse9b226622014-03-09 14:03:28 -07003291 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003292 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003293 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003294 return 1;
3295 else {
3296 /*
3297 * 32 bit DMA is removed from si_domain and fall back
3298 * to non-identity mapping.
3299 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003300 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003301 pr_info("32bit %s uses non-identity mapping\n",
3302 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003303 return 0;
3304 }
3305 } else {
3306 /*
3307 * In case of a detached 64 bit DMA device from vm, the device
3308 * is put into si_domain for identity mapping.
3309 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003310 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003311 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003312 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003313 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003314 pr_info("64bit %s uses identity mapping\n",
3315 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003316 return 1;
3317 }
3318 }
3319 }
3320
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003321 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003322}
3323
David Woodhouse5040a912014-03-09 16:14:00 -07003324static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003325 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003326{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003327 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003328 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003329 struct iova *iova;
3330 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003331 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003332 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003333 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003334
3335 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003336
David Woodhouse5040a912014-03-09 16:14:00 -07003337 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003338 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003339
David Woodhouse5040a912014-03-09 16:14:00 -07003340 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003341 if (!domain)
3342 return 0;
3343
Weidong Han8c11e792008-12-08 15:29:22 +08003344 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003345 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003346
David Woodhouse5040a912014-03-09 16:14:00 -07003347 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003348 if (!iova)
3349 goto error;
3350
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003351 /*
3352 * Check if DMAR supports zero-length reads on write only
3353 * mappings..
3354 */
3355 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003356 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003357 prot |= DMA_PTE_READ;
3358 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3359 prot |= DMA_PTE_WRITE;
3360 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003361 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003362 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003363 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003364 * is not a big problem
3365 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003366 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003367 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003368 if (ret)
3369 goto error;
3370
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003371 /* it's a non-present to present mapping. Only flush if caching mode */
3372 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003373 iommu_flush_iotlb_psi(iommu, domain,
3374 mm_to_dma_pfn(iova->pfn_lo),
3375 size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003376 else
Weidong Han8c11e792008-12-08 15:29:22 +08003377 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003378
David Woodhouse03d6a242009-06-28 15:33:46 +01003379 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3380 start_paddr += paddr & ~PAGE_MASK;
3381 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003382
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003383error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003384 if (iova)
3385 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003386 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003387 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003388 return 0;
3389}
3390
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003391static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3392 unsigned long offset, size_t size,
3393 enum dma_data_direction dir,
3394 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003395{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003396 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003397 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003398}
3399
mark gross5e0d2a62008-03-04 15:22:08 -08003400static void flush_unmaps(void)
3401{
mark gross80b20dd2008-04-18 13:53:58 -07003402 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003403
mark gross5e0d2a62008-03-04 15:22:08 -08003404 timer_on = 0;
3405
3406 /* just flush them all */
3407 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003408 struct intel_iommu *iommu = g_iommus[i];
3409 if (!iommu)
3410 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003411
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003412 if (!deferred_flush[i].next)
3413 continue;
3414
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003415 /* In caching mode, global flushes turn emulation expensive */
3416 if (!cap_caching_mode(iommu->cap))
3417 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003418 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003419 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003420 unsigned long mask;
3421 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003422 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003423
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003424 /* On real hardware multiple invalidations are expensive */
3425 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003426 iommu_flush_iotlb_psi(iommu, domain,
Jiang Liua156ef92014-07-11 14:19:36 +08003427 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003428 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003429 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003430 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003431 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3432 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3433 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003434 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003435 if (deferred_flush[i].freelist[j])
3436 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003437 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003438 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003439 }
3440
mark gross5e0d2a62008-03-04 15:22:08 -08003441 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003442}
3443
3444static void flush_unmaps_timeout(unsigned long data)
3445{
mark gross80b20dd2008-04-18 13:53:58 -07003446 unsigned long flags;
3447
3448 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003449 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003450 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003451}
3452
David Woodhouseea8ea462014-03-05 17:09:32 +00003453static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003454{
3455 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003456 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003457 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003458
3459 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003460 if (list_size == HIGH_WATER_MARK)
3461 flush_unmaps();
3462
Weidong Han8c11e792008-12-08 15:29:22 +08003463 iommu = domain_get_iommu(dom);
3464 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003465
mark gross80b20dd2008-04-18 13:53:58 -07003466 next = deferred_flush[iommu_id].next;
3467 deferred_flush[iommu_id].domain[next] = dom;
3468 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003469 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003470 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003471
3472 if (!timer_on) {
3473 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3474 timer_on = 1;
3475 }
3476 list_size++;
3477 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3478}
3479
Jiang Liud41a4ad2014-07-11 14:19:34 +08003480static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003481{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003482 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003483 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003484 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003485 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003486 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003487
David Woodhouse73676832009-07-04 14:08:36 +01003488 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003489 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003490
David Woodhouse1525a292014-03-06 16:19:30 +00003491 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003492 BUG_ON(!domain);
3493
Weidong Han8c11e792008-12-08 15:29:22 +08003494 iommu = domain_get_iommu(domain);
3495
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003496 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003497 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3498 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003499 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003500
David Woodhoused794dc92009-06-28 00:27:49 +01003501 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3502 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003503
David Woodhoused794dc92009-06-28 00:27:49 +01003504 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003505 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003506
David Woodhouseea8ea462014-03-05 17:09:32 +00003507 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003508
mark gross5e0d2a62008-03-04 15:22:08 -08003509 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003510 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003511 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003512 /* free iova */
3513 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003514 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003515 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003516 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003517 /*
3518 * queue up the release of the unmap to save the 1/6th of the
3519 * cpu used up by the iotlb flush operation...
3520 */
mark gross5e0d2a62008-03-04 15:22:08 -08003521 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003522}
3523
Jiang Liud41a4ad2014-07-11 14:19:34 +08003524static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3525 size_t size, enum dma_data_direction dir,
3526 struct dma_attrs *attrs)
3527{
3528 intel_unmap(dev, dev_addr);
3529}
3530
David Woodhouse5040a912014-03-09 16:14:00 -07003531static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003532 dma_addr_t *dma_handle, gfp_t flags,
3533 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003534{
Akinobu Mita36746432014-06-04 16:06:51 -07003535 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003536 int order;
3537
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003538 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003539 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003540
David Woodhouse5040a912014-03-09 16:14:00 -07003541 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003542 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003543 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3544 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003545 flags |= GFP_DMA;
3546 else
3547 flags |= GFP_DMA32;
3548 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003549
Akinobu Mita36746432014-06-04 16:06:51 -07003550 if (flags & __GFP_WAIT) {
3551 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003552
Akinobu Mita36746432014-06-04 16:06:51 -07003553 page = dma_alloc_from_contiguous(dev, count, order);
3554 if (page && iommu_no_mapping(dev) &&
3555 page_to_phys(page) + size > dev->coherent_dma_mask) {
3556 dma_release_from_contiguous(dev, page, count);
3557 page = NULL;
3558 }
3559 }
3560
3561 if (!page)
3562 page = alloc_pages(flags, order);
3563 if (!page)
3564 return NULL;
3565 memset(page_address(page), 0, size);
3566
3567 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003568 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003569 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003570 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003571 return page_address(page);
3572 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3573 __free_pages(page, order);
3574
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003575 return NULL;
3576}
3577
David Woodhouse5040a912014-03-09 16:14:00 -07003578static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003579 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003580{
3581 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003582 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003583
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003584 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003585 order = get_order(size);
3586
Jiang Liud41a4ad2014-07-11 14:19:34 +08003587 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003588 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3589 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003590}
3591
David Woodhouse5040a912014-03-09 16:14:00 -07003592static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003593 int nelems, enum dma_data_direction dir,
3594 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003595{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003596 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003597}
3598
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003599static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003600 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003601{
3602 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003603 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003604
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003605 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003606 BUG_ON(!sg_page(sg));
Dan Williamsdb0fa0c2015-08-17 08:13:26 -06003607 sg->dma_address = sg_phys(sg);
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003608 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003609 }
3610 return nelems;
3611}
3612
David Woodhouse5040a912014-03-09 16:14:00 -07003613static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003614 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003615{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003616 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003617 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003618 size_t size = 0;
3619 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003620 struct iova *iova = NULL;
3621 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003622 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003623 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003624 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003625
3626 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003627 if (iommu_no_mapping(dev))
3628 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003629
David Woodhouse5040a912014-03-09 16:14:00 -07003630 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003631 if (!domain)
3632 return 0;
3633
Weidong Han8c11e792008-12-08 15:29:22 +08003634 iommu = domain_get_iommu(domain);
3635
David Woodhouseb536d242009-06-28 14:49:31 +01003636 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003637 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003638
David Woodhouse5040a912014-03-09 16:14:00 -07003639 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3640 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003641 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003642 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003643 return 0;
3644 }
3645
3646 /*
3647 * Check if DMAR supports zero-length reads on write only
3648 * mappings..
3649 */
3650 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003651 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003652 prot |= DMA_PTE_READ;
3653 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3654 prot |= DMA_PTE_WRITE;
3655
David Woodhouseb536d242009-06-28 14:49:31 +01003656 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003657
Fenghua Yuf5329592009-08-04 15:09:37 -07003658 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003659 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003660 dma_pte_free_pagetable(domain, start_vpfn,
3661 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003662 __free_iova(&domain->iovad, iova);
3663 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003664 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003665
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003666 /* it's a non-present to present mapping. Only flush if caching mode */
3667 if (cap_caching_mode(iommu->cap))
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003668 iommu_flush_iotlb_psi(iommu, domain, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003669 else
Weidong Han8c11e792008-12-08 15:29:22 +08003670 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003671
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003672 return nelems;
3673}
3674
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003675static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3676{
3677 return !dma_addr;
3678}
3679
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003680struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003681 .alloc = intel_alloc_coherent,
3682 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003683 .map_sg = intel_map_sg,
3684 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003685 .map_page = intel_map_page,
3686 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003687 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003688};
3689
3690static inline int iommu_domain_cache_init(void)
3691{
3692 int ret = 0;
3693
3694 iommu_domain_cache = kmem_cache_create("iommu_domain",
3695 sizeof(struct dmar_domain),
3696 0,
3697 SLAB_HWCACHE_ALIGN,
3698
3699 NULL);
3700 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003701 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003702 ret = -ENOMEM;
3703 }
3704
3705 return ret;
3706}
3707
3708static inline int iommu_devinfo_cache_init(void)
3709{
3710 int ret = 0;
3711
3712 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3713 sizeof(struct device_domain_info),
3714 0,
3715 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003716 NULL);
3717 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003718 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003719 ret = -ENOMEM;
3720 }
3721
3722 return ret;
3723}
3724
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003725static int __init iommu_init_mempool(void)
3726{
3727 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003728 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003729 if (ret)
3730 return ret;
3731
3732 ret = iommu_domain_cache_init();
3733 if (ret)
3734 goto domain_error;
3735
3736 ret = iommu_devinfo_cache_init();
3737 if (!ret)
3738 return ret;
3739
3740 kmem_cache_destroy(iommu_domain_cache);
3741domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003742 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003743
3744 return -ENOMEM;
3745}
3746
3747static void __init iommu_exit_mempool(void)
3748{
3749 kmem_cache_destroy(iommu_devinfo_cache);
3750 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003751 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003752}
3753
Dan Williams556ab452010-07-23 15:47:56 -07003754static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3755{
3756 struct dmar_drhd_unit *drhd;
3757 u32 vtbar;
3758 int rc;
3759
3760 /* We know that this device on this chipset has its own IOMMU.
3761 * If we find it under a different IOMMU, then the BIOS is lying
3762 * to us. Hope that the IOMMU for this device is actually
3763 * disabled, and it needs no translation...
3764 */
3765 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3766 if (rc) {
3767 /* "can't" happen */
3768 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3769 return;
3770 }
3771 vtbar &= 0xffff0000;
3772
3773 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3774 drhd = dmar_find_matched_drhd_unit(pdev);
3775 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3776 TAINT_FIRMWARE_WORKAROUND,
3777 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3778 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3779}
3780DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3781
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003782static void __init init_no_remapping_devices(void)
3783{
3784 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003785 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003786 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003787
3788 for_each_drhd_unit(drhd) {
3789 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003790 for_each_active_dev_scope(drhd->devices,
3791 drhd->devices_cnt, i, dev)
3792 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003793 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003794 if (i == drhd->devices_cnt)
3795 drhd->ignored = 1;
3796 }
3797 }
3798
Jiang Liu7c919772014-01-06 14:18:18 +08003799 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003800 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003801 continue;
3802
Jiang Liub683b232014-02-19 14:07:32 +08003803 for_each_active_dev_scope(drhd->devices,
3804 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003805 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003806 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003807 if (i < drhd->devices_cnt)
3808 continue;
3809
David Woodhousec0771df2011-10-14 20:59:46 +01003810 /* This IOMMU has *only* gfx devices. Either bypass it or
3811 set the gfx_mapped flag, as appropriate */
3812 if (dmar_map_gfx) {
3813 intel_iommu_gfx_mapped = 1;
3814 } else {
3815 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003816 for_each_active_dev_scope(drhd->devices,
3817 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003818 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003819 }
3820 }
3821}
3822
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003823#ifdef CONFIG_SUSPEND
3824static int init_iommu_hw(void)
3825{
3826 struct dmar_drhd_unit *drhd;
3827 struct intel_iommu *iommu = NULL;
3828
3829 for_each_active_iommu(iommu, drhd)
3830 if (iommu->qi)
3831 dmar_reenable_qi(iommu);
3832
Joseph Cihulab7792602011-05-03 00:08:37 -07003833 for_each_iommu(iommu, drhd) {
3834 if (drhd->ignored) {
3835 /*
3836 * we always have to disable PMRs or DMA may fail on
3837 * this device
3838 */
3839 if (force_on)
3840 iommu_disable_protect_mem_regions(iommu);
3841 continue;
3842 }
3843
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003844 iommu_flush_write_buffer(iommu);
3845
3846 iommu_set_root_entry(iommu);
3847
3848 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003849 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003850 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3851 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003852 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003853 }
3854
3855 return 0;
3856}
3857
3858static void iommu_flush_all(void)
3859{
3860 struct dmar_drhd_unit *drhd;
3861 struct intel_iommu *iommu;
3862
3863 for_each_active_iommu(iommu, drhd) {
3864 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003865 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003866 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003867 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003868 }
3869}
3870
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003871static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003872{
3873 struct dmar_drhd_unit *drhd;
3874 struct intel_iommu *iommu = NULL;
3875 unsigned long flag;
3876
3877 for_each_active_iommu(iommu, drhd) {
3878 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3879 GFP_ATOMIC);
3880 if (!iommu->iommu_state)
3881 goto nomem;
3882 }
3883
3884 iommu_flush_all();
3885
3886 for_each_active_iommu(iommu, drhd) {
3887 iommu_disable_translation(iommu);
3888
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003889 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003890
3891 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3892 readl(iommu->reg + DMAR_FECTL_REG);
3893 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3894 readl(iommu->reg + DMAR_FEDATA_REG);
3895 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3896 readl(iommu->reg + DMAR_FEADDR_REG);
3897 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3898 readl(iommu->reg + DMAR_FEUADDR_REG);
3899
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003900 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003901 }
3902 return 0;
3903
3904nomem:
3905 for_each_active_iommu(iommu, drhd)
3906 kfree(iommu->iommu_state);
3907
3908 return -ENOMEM;
3909}
3910
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003911static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003912{
3913 struct dmar_drhd_unit *drhd;
3914 struct intel_iommu *iommu = NULL;
3915 unsigned long flag;
3916
3917 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003918 if (force_on)
3919 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3920 else
3921 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003922 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003923 }
3924
3925 for_each_active_iommu(iommu, drhd) {
3926
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003927 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003928
3929 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3930 iommu->reg + DMAR_FECTL_REG);
3931 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3932 iommu->reg + DMAR_FEDATA_REG);
3933 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3934 iommu->reg + DMAR_FEADDR_REG);
3935 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3936 iommu->reg + DMAR_FEUADDR_REG);
3937
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003938 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003939 }
3940
3941 for_each_active_iommu(iommu, drhd)
3942 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003943}
3944
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003945static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003946 .resume = iommu_resume,
3947 .suspend = iommu_suspend,
3948};
3949
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003950static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003951{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003952 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003953}
3954
3955#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003956static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003957#endif /* CONFIG_PM */
3958
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003959
Jiang Liuc2a0b532014-11-09 22:47:56 +08003960int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003961{
3962 struct acpi_dmar_reserved_memory *rmrr;
3963 struct dmar_rmrr_unit *rmrru;
3964
3965 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3966 if (!rmrru)
3967 return -ENOMEM;
3968
3969 rmrru->hdr = header;
3970 rmrr = (struct acpi_dmar_reserved_memory *)header;
3971 rmrru->base_address = rmrr->base_address;
3972 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08003973 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
3974 ((void *)rmrr) + rmrr->header.length,
3975 &rmrru->devices_cnt);
3976 if (rmrru->devices_cnt && rmrru->devices == NULL) {
3977 kfree(rmrru);
3978 return -ENOMEM;
3979 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003980
Jiang Liu2e455282014-02-19 14:07:36 +08003981 list_add(&rmrru->list, &dmar_rmrr_units);
3982
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003983 return 0;
3984}
3985
Jiang Liu6b197242014-11-09 22:47:58 +08003986static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
3987{
3988 struct dmar_atsr_unit *atsru;
3989 struct acpi_dmar_atsr *tmp;
3990
3991 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
3992 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
3993 if (atsr->segment != tmp->segment)
3994 continue;
3995 if (atsr->header.length != tmp->header.length)
3996 continue;
3997 if (memcmp(atsr, tmp, atsr->header.length) == 0)
3998 return atsru;
3999 }
4000
4001 return NULL;
4002}
4003
4004int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004005{
4006 struct acpi_dmar_atsr *atsr;
4007 struct dmar_atsr_unit *atsru;
4008
Jiang Liu6b197242014-11-09 22:47:58 +08004009 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4010 return 0;
4011
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004012 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004013 atsru = dmar_find_atsr(atsr);
4014 if (atsru)
4015 return 0;
4016
4017 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004018 if (!atsru)
4019 return -ENOMEM;
4020
Jiang Liu6b197242014-11-09 22:47:58 +08004021 /*
4022 * If memory is allocated from slab by ACPI _DSM method, we need to
4023 * copy the memory content because the memory buffer will be freed
4024 * on return.
4025 */
4026 atsru->hdr = (void *)(atsru + 1);
4027 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004028 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004029 if (!atsru->include_all) {
4030 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4031 (void *)atsr + atsr->header.length,
4032 &atsru->devices_cnt);
4033 if (atsru->devices_cnt && atsru->devices == NULL) {
4034 kfree(atsru);
4035 return -ENOMEM;
4036 }
4037 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004038
Jiang Liu0e2426122014-02-19 14:07:34 +08004039 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004040
4041 return 0;
4042}
4043
Jiang Liu9bdc5312014-01-06 14:18:27 +08004044static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4045{
4046 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4047 kfree(atsru);
4048}
4049
Jiang Liu6b197242014-11-09 22:47:58 +08004050int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4051{
4052 struct acpi_dmar_atsr *atsr;
4053 struct dmar_atsr_unit *atsru;
4054
4055 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4056 atsru = dmar_find_atsr(atsr);
4057 if (atsru) {
4058 list_del_rcu(&atsru->list);
4059 synchronize_rcu();
4060 intel_iommu_free_atsr(atsru);
4061 }
4062
4063 return 0;
4064}
4065
4066int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4067{
4068 int i;
4069 struct device *dev;
4070 struct acpi_dmar_atsr *atsr;
4071 struct dmar_atsr_unit *atsru;
4072
4073 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4074 atsru = dmar_find_atsr(atsr);
4075 if (!atsru)
4076 return 0;
4077
4078 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4079 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4080 i, dev)
4081 return -EBUSY;
4082
4083 return 0;
4084}
4085
Jiang Liuffebeb42014-11-09 22:48:02 +08004086static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4087{
4088 int sp, ret = 0;
4089 struct intel_iommu *iommu = dmaru->iommu;
4090
4091 if (g_iommus[iommu->seq_id])
4092 return 0;
4093
4094 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004095 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004096 iommu->name);
4097 return -ENXIO;
4098 }
4099 if (!ecap_sc_support(iommu->ecap) &&
4100 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004101 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004102 iommu->name);
4103 return -ENXIO;
4104 }
4105 sp = domain_update_iommu_superpage(iommu) - 1;
4106 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004107 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004108 iommu->name);
4109 return -ENXIO;
4110 }
4111
4112 /*
4113 * Disable translation if already enabled prior to OS handover.
4114 */
4115 if (iommu->gcmd & DMA_GCMD_TE)
4116 iommu_disable_translation(iommu);
4117
4118 g_iommus[iommu->seq_id] = iommu;
4119 ret = iommu_init_domains(iommu);
4120 if (ret == 0)
4121 ret = iommu_alloc_root_entry(iommu);
4122 if (ret)
4123 goto out;
4124
4125 if (dmaru->ignored) {
4126 /*
4127 * we always have to disable PMRs or DMA may fail on this device
4128 */
4129 if (force_on)
4130 iommu_disable_protect_mem_regions(iommu);
4131 return 0;
4132 }
4133
4134 intel_iommu_init_qi(iommu);
4135 iommu_flush_write_buffer(iommu);
4136 ret = dmar_set_interrupt(iommu);
4137 if (ret)
4138 goto disable_iommu;
4139
4140 iommu_set_root_entry(iommu);
4141 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4142 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4143 iommu_enable_translation(iommu);
4144
Jiang Liuffebeb42014-11-09 22:48:02 +08004145 iommu_disable_protect_mem_regions(iommu);
4146 return 0;
4147
4148disable_iommu:
4149 disable_dmar_iommu(iommu);
4150out:
4151 free_dmar_iommu(iommu);
4152 return ret;
4153}
4154
Jiang Liu6b197242014-11-09 22:47:58 +08004155int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4156{
Jiang Liuffebeb42014-11-09 22:48:02 +08004157 int ret = 0;
4158 struct intel_iommu *iommu = dmaru->iommu;
4159
4160 if (!intel_iommu_enabled)
4161 return 0;
4162 if (iommu == NULL)
4163 return -EINVAL;
4164
4165 if (insert) {
4166 ret = intel_iommu_add(dmaru);
4167 } else {
4168 disable_dmar_iommu(iommu);
4169 free_dmar_iommu(iommu);
4170 }
4171
4172 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004173}
4174
Jiang Liu9bdc5312014-01-06 14:18:27 +08004175static void intel_iommu_free_dmars(void)
4176{
4177 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4178 struct dmar_atsr_unit *atsru, *atsr_n;
4179
4180 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4181 list_del(&rmrru->list);
4182 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4183 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004184 }
4185
Jiang Liu9bdc5312014-01-06 14:18:27 +08004186 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4187 list_del(&atsru->list);
4188 intel_iommu_free_atsr(atsru);
4189 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004190}
4191
4192int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4193{
Jiang Liub683b232014-02-19 14:07:32 +08004194 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004195 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004196 struct pci_dev *bridge = NULL;
4197 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004198 struct acpi_dmar_atsr *atsr;
4199 struct dmar_atsr_unit *atsru;
4200
4201 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004202 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004203 bridge = bus->self;
David Woodhoused14053b32015-10-15 09:28:06 +01004204 /* If it's an integrated device, allow ATS */
4205 if (!bridge)
4206 return 1;
4207 /* Connected via non-PCIe: no ATS */
4208 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004209 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004210 return 0;
David Woodhoused14053b32015-10-15 09:28:06 +01004211 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004212 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004213 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004214 }
4215
Jiang Liu0e2426122014-02-19 14:07:34 +08004216 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004217 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4218 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4219 if (atsr->segment != pci_domain_nr(dev->bus))
4220 continue;
4221
Jiang Liub683b232014-02-19 14:07:32 +08004222 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004223 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004224 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004225
4226 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004227 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004228 }
Jiang Liub683b232014-02-19 14:07:32 +08004229 ret = 0;
4230out:
Jiang Liu0e2426122014-02-19 14:07:34 +08004231 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004232
Jiang Liub683b232014-02-19 14:07:32 +08004233 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004234}
4235
Jiang Liu59ce0512014-02-19 14:07:35 +08004236int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4237{
4238 int ret = 0;
4239 struct dmar_rmrr_unit *rmrru;
4240 struct dmar_atsr_unit *atsru;
4241 struct acpi_dmar_atsr *atsr;
4242 struct acpi_dmar_reserved_memory *rmrr;
4243
4244 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4245 return 0;
4246
4247 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4248 rmrr = container_of(rmrru->hdr,
4249 struct acpi_dmar_reserved_memory, header);
4250 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4251 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4252 ((void *)rmrr) + rmrr->header.length,
4253 rmrr->segment, rmrru->devices,
4254 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004255 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004256 return ret;
4257 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004258 dmar_remove_dev_scope(info, rmrr->segment,
4259 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004260 }
4261 }
4262
4263 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4264 if (atsru->include_all)
4265 continue;
4266
4267 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4268 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4269 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4270 (void *)atsr + atsr->header.length,
4271 atsr->segment, atsru->devices,
4272 atsru->devices_cnt);
4273 if (ret > 0)
4274 break;
4275 else if(ret < 0)
4276 return ret;
4277 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4278 if (dmar_remove_dev_scope(info, atsr->segment,
4279 atsru->devices, atsru->devices_cnt))
4280 break;
4281 }
4282 }
4283
4284 return 0;
4285}
4286
Fenghua Yu99dcade2009-11-11 07:23:06 -08004287/*
4288 * Here we only respond to action of unbound device from driver.
4289 *
4290 * Added device is not attached to its DMAR domain here yet. That will happen
4291 * when mapping the device to iova.
4292 */
4293static int device_notifier(struct notifier_block *nb,
4294 unsigned long action, void *data)
4295{
4296 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004297 struct dmar_domain *domain;
4298
David Woodhouse3d891942014-03-06 15:59:26 +00004299 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004300 return 0;
4301
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004302 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004303 return 0;
4304
David Woodhouse1525a292014-03-06 16:19:30 +00004305 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004306 if (!domain)
4307 return 0;
4308
Joerg Roedele6de0f82015-07-22 16:30:36 +02004309 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004310 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004311 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004312
Fenghua Yu99dcade2009-11-11 07:23:06 -08004313 return 0;
4314}
4315
4316static struct notifier_block device_nb = {
4317 .notifier_call = device_notifier,
4318};
4319
Jiang Liu75f05562014-02-19 14:07:37 +08004320static int intel_iommu_memory_notifier(struct notifier_block *nb,
4321 unsigned long val, void *v)
4322{
4323 struct memory_notify *mhp = v;
4324 unsigned long long start, end;
4325 unsigned long start_vpfn, last_vpfn;
4326
4327 switch (val) {
4328 case MEM_GOING_ONLINE:
4329 start = mhp->start_pfn << PAGE_SHIFT;
4330 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4331 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004332 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004333 start, end);
4334 return NOTIFY_BAD;
4335 }
4336 break;
4337
4338 case MEM_OFFLINE:
4339 case MEM_CANCEL_ONLINE:
4340 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4341 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4342 while (start_vpfn <= last_vpfn) {
4343 struct iova *iova;
4344 struct dmar_drhd_unit *drhd;
4345 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004346 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004347
4348 iova = find_iova(&si_domain->iovad, start_vpfn);
4349 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004350 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004351 start_vpfn);
4352 break;
4353 }
4354
4355 iova = split_and_remove_iova(&si_domain->iovad, iova,
4356 start_vpfn, last_vpfn);
4357 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004358 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004359 start_vpfn, last_vpfn);
4360 return NOTIFY_BAD;
4361 }
4362
David Woodhouseea8ea462014-03-05 17:09:32 +00004363 freelist = domain_unmap(si_domain, iova->pfn_lo,
4364 iova->pfn_hi);
4365
Jiang Liu75f05562014-02-19 14:07:37 +08004366 rcu_read_lock();
4367 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004368 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004369 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004370 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004371 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004372 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004373
4374 start_vpfn = iova->pfn_hi + 1;
4375 free_iova_mem(iova);
4376 }
4377 break;
4378 }
4379
4380 return NOTIFY_OK;
4381}
4382
4383static struct notifier_block intel_iommu_memory_nb = {
4384 .notifier_call = intel_iommu_memory_notifier,
4385 .priority = 0
4386};
4387
Alex Williamsona5459cf2014-06-12 16:12:31 -06004388
4389static ssize_t intel_iommu_show_version(struct device *dev,
4390 struct device_attribute *attr,
4391 char *buf)
4392{
4393 struct intel_iommu *iommu = dev_get_drvdata(dev);
4394 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4395 return sprintf(buf, "%d:%d\n",
4396 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4397}
4398static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4399
4400static ssize_t intel_iommu_show_address(struct device *dev,
4401 struct device_attribute *attr,
4402 char *buf)
4403{
4404 struct intel_iommu *iommu = dev_get_drvdata(dev);
4405 return sprintf(buf, "%llx\n", iommu->reg_phys);
4406}
4407static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4408
4409static ssize_t intel_iommu_show_cap(struct device *dev,
4410 struct device_attribute *attr,
4411 char *buf)
4412{
4413 struct intel_iommu *iommu = dev_get_drvdata(dev);
4414 return sprintf(buf, "%llx\n", iommu->cap);
4415}
4416static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4417
4418static ssize_t intel_iommu_show_ecap(struct device *dev,
4419 struct device_attribute *attr,
4420 char *buf)
4421{
4422 struct intel_iommu *iommu = dev_get_drvdata(dev);
4423 return sprintf(buf, "%llx\n", iommu->ecap);
4424}
4425static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4426
Alex Williamson2238c082015-07-14 15:24:53 -06004427static ssize_t intel_iommu_show_ndoms(struct device *dev,
4428 struct device_attribute *attr,
4429 char *buf)
4430{
4431 struct intel_iommu *iommu = dev_get_drvdata(dev);
4432 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4433}
4434static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4435
4436static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4437 struct device_attribute *attr,
4438 char *buf)
4439{
4440 struct intel_iommu *iommu = dev_get_drvdata(dev);
4441 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4442 cap_ndoms(iommu->cap)));
4443}
4444static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4445
Alex Williamsona5459cf2014-06-12 16:12:31 -06004446static struct attribute *intel_iommu_attrs[] = {
4447 &dev_attr_version.attr,
4448 &dev_attr_address.attr,
4449 &dev_attr_cap.attr,
4450 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004451 &dev_attr_domains_supported.attr,
4452 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004453 NULL,
4454};
4455
4456static struct attribute_group intel_iommu_group = {
4457 .name = "intel-iommu",
4458 .attrs = intel_iommu_attrs,
4459};
4460
4461const struct attribute_group *intel_iommu_groups[] = {
4462 &intel_iommu_group,
4463 NULL,
4464};
4465
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004466int __init intel_iommu_init(void)
4467{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004468 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004469 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004470 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004471
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004472 /* VT-d is required for a TXT/tboot launch, so enforce that */
4473 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004474
Jiang Liu3a5670e2014-02-19 14:07:33 +08004475 if (iommu_init_mempool()) {
4476 if (force_on)
4477 panic("tboot: Failed to initialize iommu memory\n");
4478 return -ENOMEM;
4479 }
4480
4481 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004482 if (dmar_table_init()) {
4483 if (force_on)
4484 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004485 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004486 }
4487
Suresh Siddhac2c72862011-08-23 17:05:19 -07004488 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004489 if (force_on)
4490 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004491 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004492 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004493
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004494 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004495 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004496
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004497 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004498 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004499
4500 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004501 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004502
Joseph Cihula51a63e62011-03-21 11:04:24 -07004503 if (dmar_init_reserved_ranges()) {
4504 if (force_on)
4505 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004506 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004507 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004508
4509 init_no_remapping_devices();
4510
Joseph Cihulab7792602011-05-03 00:08:37 -07004511 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004512 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004513 if (force_on)
4514 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004515 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004516 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004517 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004518 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004519 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004520
mark gross5e0d2a62008-03-04 15:22:08 -08004521 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004522#ifdef CONFIG_SWIOTLB
4523 swiotlb = 0;
4524#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004525 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004526
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004527 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004528
Alex Williamsona5459cf2014-06-12 16:12:31 -06004529 for_each_active_iommu(iommu, drhd)
4530 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4531 intel_iommu_groups,
Kees Cook2439d4a2015-07-24 16:27:57 -07004532 "%s", iommu->name);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004533
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004534 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004535 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004536 if (si_domain && !hw_pass_through)
4537 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004538
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004539 intel_iommu_enabled = 1;
4540
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004541 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004542
4543out_free_reserved_range:
4544 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004545out_free_dmar:
4546 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004547 up_write(&dmar_global_lock);
4548 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004549 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004550}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004551
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004552static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004553{
4554 struct intel_iommu *iommu = opaque;
4555
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004556 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004557 return 0;
4558}
4559
4560/*
4561 * NB - intel-iommu lacks any sort of reference counting for the users of
4562 * dependent devices. If multiple endpoints have intersecting dependent
4563 * devices, unbinding the driver from any one of them will possibly leave
4564 * the others unable to operate.
4565 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004566static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004567{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004568 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004569 return;
4570
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004571 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004572}
4573
Joerg Roedel127c7612015-07-23 17:44:46 +02004574static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004575{
Weidong Hanc7151a82008-12-08 22:51:37 +08004576 struct intel_iommu *iommu;
4577 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004578
Joerg Roedel55d94042015-07-22 16:50:40 +02004579 assert_spin_locked(&device_domain_lock);
4580
Joerg Roedelb608ac32015-07-21 18:19:08 +02004581 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004582 return;
4583
Joerg Roedel127c7612015-07-23 17:44:46 +02004584 iommu = info->iommu;
4585
4586 if (info->dev) {
4587 iommu_disable_dev_iotlb(info);
4588 domain_context_clear(iommu, info->dev);
4589 }
4590
Joerg Roedelb608ac32015-07-21 18:19:08 +02004591 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004592
Joerg Roedeld160aca2015-07-22 11:52:53 +02004593 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004594 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004595 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004596
4597 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004598}
4599
Joerg Roedel55d94042015-07-22 16:50:40 +02004600static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4601 struct device *dev)
4602{
Joerg Roedel127c7612015-07-23 17:44:46 +02004603 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004604 unsigned long flags;
4605
Weidong Hanc7151a82008-12-08 22:51:37 +08004606 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004607 info = dev->archdata.iommu;
4608 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004609 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004610}
4611
4612static int md_domain_init(struct dmar_domain *domain, int guest_width)
4613{
4614 int adjust_width;
4615
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004616 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4617 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004618 domain_reserve_special_ranges(domain);
4619
4620 /* calculate AGAW */
4621 domain->gaw = guest_width;
4622 adjust_width = guestwidth_to_adjustwidth(guest_width);
4623 domain->agaw = width_to_agaw(adjust_width);
4624
Weidong Han5e98c4b2008-12-08 23:03:27 +08004625 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004626 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004627 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004628 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004629
4630 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004631 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004632 if (!domain->pgd)
4633 return -ENOMEM;
4634 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4635 return 0;
4636}
4637
Joerg Roedel00a77de2015-03-26 13:43:08 +01004638static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004639{
Joerg Roedel5d450802008-12-03 14:52:32 +01004640 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004641 struct iommu_domain *domain;
4642
4643 if (type != IOMMU_DOMAIN_UNMANAGED)
4644 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004645
Jiang Liuab8dfe22014-07-11 14:19:27 +08004646 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004647 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004648 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004649 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004650 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004651 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004652 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004653 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004654 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004655 }
Allen Kay8140a952011-10-14 12:32:17 -07004656 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004657
Joerg Roedel00a77de2015-03-26 13:43:08 +01004658 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004659 domain->geometry.aperture_start = 0;
4660 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4661 domain->geometry.force_aperture = true;
4662
Joerg Roedel00a77de2015-03-26 13:43:08 +01004663 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004664}
Kay, Allen M38717942008-09-09 18:37:29 +03004665
Joerg Roedel00a77de2015-03-26 13:43:08 +01004666static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004667{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004668 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004669}
Kay, Allen M38717942008-09-09 18:37:29 +03004670
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004671static int intel_iommu_attach_device(struct iommu_domain *domain,
4672 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004673{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004674 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004675 struct intel_iommu *iommu;
4676 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004677 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004678
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004679 if (device_is_rmrr_locked(dev)) {
4680 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4681 return -EPERM;
4682 }
4683
David Woodhouse7207d8f2014-03-09 16:31:06 -07004684 /* normally dev is not mapped */
4685 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004686 struct dmar_domain *old_domain;
4687
David Woodhouse1525a292014-03-06 16:19:30 +00004688 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004689 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02004690 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02004691 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004692 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01004693
4694 if (!domain_type_is_vm_or_si(old_domain) &&
4695 list_empty(&old_domain->devices))
4696 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004697 }
4698 }
4699
David Woodhouse156baca2014-03-09 14:00:57 -07004700 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004701 if (!iommu)
4702 return -ENODEV;
4703
4704 /* check if this iommu agaw is sufficient for max mapped address */
4705 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004706 if (addr_width > cap_mgaw(iommu->cap))
4707 addr_width = cap_mgaw(iommu->cap);
4708
4709 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004710 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004711 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004712 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004713 return -EFAULT;
4714 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004715 dmar_domain->gaw = addr_width;
4716
4717 /*
4718 * Knock out extra levels of page tables if necessary
4719 */
4720 while (iommu->agaw < dmar_domain->agaw) {
4721 struct dma_pte *pte;
4722
4723 pte = dmar_domain->pgd;
4724 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004725 dmar_domain->pgd = (struct dma_pte *)
4726 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004727 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004728 }
4729 dmar_domain->agaw--;
4730 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004731
Joerg Roedel28ccce02015-07-21 14:45:31 +02004732 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004733}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004734
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004735static void intel_iommu_detach_device(struct iommu_domain *domain,
4736 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004737{
Joerg Roedele6de0f82015-07-22 16:30:36 +02004738 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004739}
Kay, Allen M38717942008-09-09 18:37:29 +03004740
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004741static int intel_iommu_map(struct iommu_domain *domain,
4742 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004743 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004744{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004745 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004746 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004747 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004748 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004749
Joerg Roedeldde57a22008-12-03 15:04:09 +01004750 if (iommu_prot & IOMMU_READ)
4751 prot |= DMA_PTE_READ;
4752 if (iommu_prot & IOMMU_WRITE)
4753 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08004754 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4755 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004756
David Woodhouse163cc522009-06-28 00:51:17 +01004757 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004758 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004759 u64 end;
4760
4761 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004762 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004763 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004764 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004765 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004766 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004767 return -EFAULT;
4768 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004769 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004770 }
David Woodhousead051222009-06-28 14:22:28 +01004771 /* Round up size to next multiple of PAGE_SIZE, if it and
4772 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004773 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004774 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4775 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004776 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004777}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004778
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004779static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004780 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004781{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004782 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004783 struct page *freelist = NULL;
4784 struct intel_iommu *iommu;
4785 unsigned long start_pfn, last_pfn;
4786 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02004787 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004788
David Woodhouse5cf0a762014-03-19 16:07:49 +00004789 /* Cope with horrid API which requires us to unmap more than the
4790 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02004791 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00004792
4793 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4794 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4795
David Woodhouseea8ea462014-03-05 17:09:32 +00004796 start_pfn = iova >> VTD_PAGE_SHIFT;
4797 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4798
4799 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4800
4801 npages = last_pfn - start_pfn + 1;
4802
Joerg Roedel29a27712015-07-21 17:17:12 +02004803 for_each_domain_iommu(iommu_id, dmar_domain) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004804 iommu = g_iommus[iommu_id];
David Woodhouseea8ea462014-03-05 17:09:32 +00004805
Joerg Roedel42e8c182015-07-21 15:50:02 +02004806 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
4807 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00004808 }
4809
4810 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004811
David Woodhouse163cc522009-06-28 00:51:17 +01004812 if (dmar_domain->max_addr == iova + size)
4813 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004814
David Woodhouse5cf0a762014-03-19 16:07:49 +00004815 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004816}
Kay, Allen M38717942008-09-09 18:37:29 +03004817
Joerg Roedeld14d6572008-12-03 15:06:57 +01004818static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05304819 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004820{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004821 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004822 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004823 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004824 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004825
David Woodhouse5cf0a762014-03-19 16:07:49 +00004826 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004827 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004828 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004829
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004830 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004831}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004832
Joerg Roedel5d587b82014-09-05 10:50:45 +02004833static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004834{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004835 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004836 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004837 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004838 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004839
Joerg Roedel5d587b82014-09-05 10:50:45 +02004840 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004841}
4842
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004843static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004844{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004845 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004846 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004847 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004848
Alex Williamsona5459cf2014-06-12 16:12:31 -06004849 iommu = device_to_iommu(dev, &bus, &devfn);
4850 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004851 return -ENODEV;
4852
Alex Williamsona5459cf2014-06-12 16:12:31 -06004853 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004854
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004855 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004856
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004857 if (IS_ERR(group))
4858 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004859
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004860 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004861 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004862}
4863
4864static void intel_iommu_remove_device(struct device *dev)
4865{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004866 struct intel_iommu *iommu;
4867 u8 bus, devfn;
4868
4869 iommu = device_to_iommu(dev, &bus, &devfn);
4870 if (!iommu)
4871 return;
4872
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004873 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004874
4875 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004876}
4877
Thierry Redingb22f6432014-06-27 09:03:12 +02004878static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004879 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004880 .domain_alloc = intel_iommu_domain_alloc,
4881 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004882 .attach_dev = intel_iommu_attach_device,
4883 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004884 .map = intel_iommu_map,
4885 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004886 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004887 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004888 .add_device = intel_iommu_add_device,
4889 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004890 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004891};
David Woodhouse9af88142009-02-13 23:18:03 +00004892
Daniel Vetter94526182013-01-20 23:50:13 +01004893static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4894{
4895 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004896 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004897 dmar_map_gfx = 0;
4898}
4899
4900DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4901DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4902DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4903DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4904DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4905DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4906DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4907
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004908static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004909{
4910 /*
4911 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004912 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004913 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004914 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00004915 rwbf_quirk = 1;
4916}
4917
4918DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004919DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4920DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4921DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4922DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4923DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4924DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004925
Adam Jacksoneecfd572010-08-25 21:17:34 +01004926#define GGC 0x52
4927#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4928#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4929#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4930#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4931#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4932#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4933#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4934#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4935
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004936static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004937{
4938 unsigned short ggc;
4939
Adam Jacksoneecfd572010-08-25 21:17:34 +01004940 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004941 return;
4942
Adam Jacksoneecfd572010-08-25 21:17:34 +01004943 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004944 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01004945 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004946 } else if (dmar_map_gfx) {
4947 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004948 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004949 intel_iommu_strict = 1;
4950 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01004951}
4952DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
4953DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
4954DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
4955DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
4956
David Woodhousee0fc7e02009-09-30 09:12:17 -07004957/* On Tylersburg chipsets, some BIOSes have been known to enable the
4958 ISOCH DMAR unit for the Azalia sound device, but not give it any
4959 TLB entries, which causes it to deadlock. Check for that. We do
4960 this in a function called from init_dmars(), instead of in a PCI
4961 quirk, because we don't want to print the obnoxious "BIOS broken"
4962 message if VT-d is actually disabled.
4963*/
4964static void __init check_tylersburg_isoch(void)
4965{
4966 struct pci_dev *pdev;
4967 uint32_t vtisochctrl;
4968
4969 /* If there's no Azalia in the system anyway, forget it. */
4970 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
4971 if (!pdev)
4972 return;
4973 pci_dev_put(pdev);
4974
4975 /* System Management Registers. Might be hidden, in which case
4976 we can't do the sanity check. But that's OK, because the
4977 known-broken BIOSes _don't_ actually hide it, so far. */
4978 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
4979 if (!pdev)
4980 return;
4981
4982 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
4983 pci_dev_put(pdev);
4984 return;
4985 }
4986
4987 pci_dev_put(pdev);
4988
4989 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
4990 if (vtisochctrl & 1)
4991 return;
4992
4993 /* Drop all bits other than the number of TLB entries */
4994 vtisochctrl &= 0x1c;
4995
4996 /* If we have the recommended number of TLB entries (16), fine. */
4997 if (vtisochctrl == 0x10)
4998 return;
4999
5000 /* Zero TLB entries? You get to ride the short bus to school. */
5001 if (!vtisochctrl) {
5002 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5003 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5004 dmi_get_system_info(DMI_BIOS_VENDOR),
5005 dmi_get_system_info(DMI_BIOS_VERSION),
5006 dmi_get_system_info(DMI_PRODUCT_VERSION));
5007 iommu_identity_mapping |= IDENTMAP_AZALIA;
5008 return;
5009 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005010
5011 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005012 vtisochctrl);
5013}