blob: 697291aceea71103855d281d2a434a142e176808 [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
Weidong Han1ce28fe2008-12-08 16:35:39 +0800367/* domain represents a virtual machine, more than one devices
368 * across iommus may be owned in one domain, e.g. kvm guest.
369 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800370#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800371
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700372/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800373#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700374
Mark McLoughlin99126f72008-11-20 15:49:47 +0000375struct dmar_domain {
376 int id; /* domain id */
Suresh Siddha4c923d42009-10-02 11:01:24 -0700377 int nid; /* node id */
Jiang Liu78d8e702014-11-09 22:47:57 +0800378 DECLARE_BITMAP(iommu_bmp, DMAR_UNITS_SUPPORTED);
Mike Travis1b198bb2012-03-05 15:05:16 -0800379 /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000380
Joerg Roedel00a77de2015-03-26 13:43:08 +0100381 struct list_head devices; /* all devices' list */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000382 struct iova_domain iovad; /* iova's that belong to this domain */
383
384 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000385 int gaw; /* max guest address width */
386
387 /* adjusted guest address width, 0 is level 2 30-bit */
388 int agaw;
389
Weidong Han3b5410e2008-12-08 09:17:15 +0800390 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800391
392 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800393 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800394 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100395 int iommu_superpage;/* Level of superpages supported:
396 0 == 4KiB (no superpages), 1 == 2MiB,
397 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanc7151a82008-12-08 22:51:37 +0800398 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800399 u64 max_addr; /* maximum mapped address */
Joerg Roedel00a77de2015-03-26 13:43:08 +0100400
401 struct iommu_domain domain; /* generic domain data structure for
402 iommu core */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000403};
404
Mark McLoughlina647dac2008-11-20 15:49:48 +0000405/* PCI domain-device relationship */
406struct device_domain_info {
407 struct list_head link; /* link to domain siblings */
408 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100409 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000410 u8 devfn; /* PCI devfn number */
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -0500411 struct {
412 u8 enabled:1;
413 u8 qdep;
414 } ats; /* ATS state */
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000415 struct device *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800416 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000417 struct dmar_domain *domain; /* pointer to domain */
418};
419
Jiang Liub94e4112014-02-19 14:07:25 +0800420struct dmar_rmrr_unit {
421 struct list_head list; /* list of rmrr units */
422 struct acpi_dmar_header *hdr; /* ACPI header */
423 u64 base_address; /* reserved base address*/
424 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000425 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800426 int devices_cnt; /* target device count */
427};
428
429struct dmar_atsr_unit {
430 struct list_head list; /* list of ATSR units */
431 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000432 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800433 int devices_cnt; /* target device count */
434 u8 include_all:1; /* include all ports */
435};
436
437static LIST_HEAD(dmar_atsr_units);
438static LIST_HEAD(dmar_rmrr_units);
439
440#define for_each_rmrr_units(rmrr) \
441 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
442
mark gross5e0d2a62008-03-04 15:22:08 -0800443static void flush_unmaps_timeout(unsigned long data);
444
Jiang Liub707cb02014-01-06 14:18:26 +0800445static DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
mark gross5e0d2a62008-03-04 15:22:08 -0800446
mark gross80b20dd2008-04-18 13:53:58 -0700447#define HIGH_WATER_MARK 250
448struct deferred_flush_tables {
449 int next;
450 struct iova *iova[HIGH_WATER_MARK];
451 struct dmar_domain *domain[HIGH_WATER_MARK];
David Woodhouseea8ea462014-03-05 17:09:32 +0000452 struct page *freelist[HIGH_WATER_MARK];
mark gross80b20dd2008-04-18 13:53:58 -0700453};
454
455static struct deferred_flush_tables *deferred_flush;
456
mark gross5e0d2a62008-03-04 15:22:08 -0800457/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800458static int g_num_of_iommus;
459
460static DEFINE_SPINLOCK(async_umap_flush_lock);
461static LIST_HEAD(unmaps_to_do);
462
463static int timer_on;
464static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800465
Jiang Liu92d03cc2014-02-19 14:07:28 +0800466static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700467static void domain_remove_dev_info(struct dmar_domain *domain);
Jiang Liub94e4112014-02-19 14:07:25 +0800468static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -0700469 struct device *dev);
Jiang Liu92d03cc2014-02-19 14:07:28 +0800470static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +0000471 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800472static int domain_detach_iommu(struct dmar_domain *domain,
473 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700474
Suresh Siddhad3f13812011-08-23 17:05:25 -0700475#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800476int dmar_disabled = 0;
477#else
478int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700479#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800480
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200481int intel_iommu_enabled = 0;
482EXPORT_SYMBOL_GPL(intel_iommu_enabled);
483
David Woodhouse2d9e6672010-06-15 10:57:57 +0100484static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700485static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800486static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100487static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100488static int intel_iommu_ecs = 1;
489
490/* We only actually use ECS when PASID support (on the new bit 40)
491 * is also advertised. Some early implementations — the ones with
492 * PASID support on bit 28 — have issues even when we *only* use
493 * extended root/context tables. */
494#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
495 ecap_pasid(iommu->ecap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700496
David Woodhousec0771df2011-10-14 20:59:46 +0100497int intel_iommu_gfx_mapped;
498EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
499
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700500#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
501static DEFINE_SPINLOCK(device_domain_lock);
502static LIST_HEAD(device_domain_list);
503
Thierry Redingb22f6432014-06-27 09:03:12 +0200504static const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100505
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200506static bool translation_pre_enabled(struct intel_iommu *iommu)
507{
508 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
509}
510
Joerg Roedel091d42e2015-06-12 11:56:10 +0200511static void clear_translation_pre_enabled(struct intel_iommu *iommu)
512{
513 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
514}
515
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200516static void init_translation_status(struct intel_iommu *iommu)
517{
518 u32 gsts;
519
520 gsts = readl(iommu->reg + DMAR_GSTS_REG);
521 if (gsts & DMA_GSTS_TES)
522 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
523}
524
Joerg Roedel00a77de2015-03-26 13:43:08 +0100525/* Convert generic 'struct iommu_domain to private struct dmar_domain */
526static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
527{
528 return container_of(dom, struct dmar_domain, domain);
529}
530
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700531static int __init intel_iommu_setup(char *str)
532{
533 if (!str)
534 return -EINVAL;
535 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800536 if (!strncmp(str, "on", 2)) {
537 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200538 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800539 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700540 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200541 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700542 } else if (!strncmp(str, "igfx_off", 8)) {
543 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200544 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700545 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200546 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700547 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800548 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200549 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800550 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100551 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200552 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100553 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100554 } else if (!strncmp(str, "ecs_off", 7)) {
555 printk(KERN_INFO
556 "Intel-IOMMU: disable extended context table support\n");
557 intel_iommu_ecs = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700558 }
559
560 str += strcspn(str, ",");
561 while (*str == ',')
562 str++;
563 }
564 return 0;
565}
566__setup("intel_iommu=", intel_iommu_setup);
567
568static struct kmem_cache *iommu_domain_cache;
569static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700570
Suresh Siddha4c923d42009-10-02 11:01:24 -0700571static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700572{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700573 struct page *page;
574 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700575
Suresh Siddha4c923d42009-10-02 11:01:24 -0700576 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
577 if (page)
578 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700579 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700580}
581
582static inline void free_pgtable_page(void *vaddr)
583{
584 free_page((unsigned long)vaddr);
585}
586
587static inline void *alloc_domain_mem(void)
588{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900589 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700590}
591
Kay, Allen M38717942008-09-09 18:37:29 +0300592static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700593{
594 kmem_cache_free(iommu_domain_cache, vaddr);
595}
596
597static inline void * alloc_devinfo_mem(void)
598{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900599 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700600}
601
602static inline void free_devinfo_mem(void *vaddr)
603{
604 kmem_cache_free(iommu_devinfo_cache, vaddr);
605}
606
Jiang Liuab8dfe22014-07-11 14:19:27 +0800607static inline int domain_type_is_vm(struct dmar_domain *domain)
608{
609 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
610}
611
612static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
613{
614 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
615 DOMAIN_FLAG_STATIC_IDENTITY);
616}
Weidong Han1b573682008-12-08 15:34:06 +0800617
Jiang Liu162d1b12014-07-11 14:19:35 +0800618static inline int domain_pfn_supported(struct dmar_domain *domain,
619 unsigned long pfn)
620{
621 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
622
623 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
624}
625
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700626static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800627{
628 unsigned long sagaw;
629 int agaw = -1;
630
631 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700632 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800633 agaw >= 0; agaw--) {
634 if (test_bit(agaw, &sagaw))
635 break;
636 }
637
638 return agaw;
639}
640
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700641/*
642 * Calculate max SAGAW for each iommu.
643 */
644int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
645{
646 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
647}
648
649/*
650 * calculate agaw for each iommu.
651 * "SAGAW" may be different across iommus, use a default agaw, and
652 * get a supported less agaw for iommus that don't support the default agaw.
653 */
654int iommu_calculate_agaw(struct intel_iommu *iommu)
655{
656 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
657}
658
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700659/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800660static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
661{
662 int iommu_id;
663
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700664 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800665 BUG_ON(domain_type_is_vm_or_si(domain));
Mike Travis1b198bb2012-03-05 15:05:16 -0800666 iommu_id = find_first_bit(domain->iommu_bmp, g_num_of_iommus);
Weidong Han8c11e792008-12-08 15:29:22 +0800667 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
668 return NULL;
669
670 return g_iommus[iommu_id];
671}
672
Weidong Han8e6040972008-12-08 15:49:06 +0800673static void domain_update_iommu_coherency(struct dmar_domain *domain)
674{
David Woodhoused0501962014-03-11 17:10:29 -0700675 struct dmar_drhd_unit *drhd;
676 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100677 bool found = false;
678 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800679
David Woodhoused0501962014-03-11 17:10:29 -0700680 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800681
Mike Travis1b198bb2012-03-05 15:05:16 -0800682 for_each_set_bit(i, domain->iommu_bmp, g_num_of_iommus) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100683 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800684 if (!ecap_coherent(g_iommus[i]->ecap)) {
685 domain->iommu_coherency = 0;
686 break;
687 }
Weidong Han8e6040972008-12-08 15:49:06 +0800688 }
David Woodhoused0501962014-03-11 17:10:29 -0700689 if (found)
690 return;
691
692 /* No hardware attached; use lowest common denominator */
693 rcu_read_lock();
694 for_each_active_iommu(iommu, drhd) {
695 if (!ecap_coherent(iommu->ecap)) {
696 domain->iommu_coherency = 0;
697 break;
698 }
699 }
700 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800701}
702
Jiang Liu161f6932014-07-11 14:19:37 +0800703static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100704{
Allen Kay8140a952011-10-14 12:32:17 -0700705 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800706 struct intel_iommu *iommu;
707 int ret = 1;
708
709 rcu_read_lock();
710 for_each_active_iommu(iommu, drhd) {
711 if (iommu != skip) {
712 if (!ecap_sc_support(iommu->ecap)) {
713 ret = 0;
714 break;
715 }
716 }
717 }
718 rcu_read_unlock();
719
720 return ret;
721}
722
723static int domain_update_iommu_superpage(struct intel_iommu *skip)
724{
725 struct dmar_drhd_unit *drhd;
726 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700727 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100728
729 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800730 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100731 }
732
Allen Kay8140a952011-10-14 12:32:17 -0700733 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800734 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700735 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800736 if (iommu != skip) {
737 mask &= cap_super_page_val(iommu->cap);
738 if (!mask)
739 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100740 }
741 }
Jiang Liu0e242612014-02-19 14:07:34 +0800742 rcu_read_unlock();
743
Jiang Liu161f6932014-07-11 14:19:37 +0800744 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100745}
746
Sheng Yang58c610b2009-03-18 15:33:05 +0800747/* Some capabilities may be different across iommus */
748static void domain_update_iommu_cap(struct dmar_domain *domain)
749{
750 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800751 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
752 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800753}
754
David Woodhouse03ecc322015-02-13 14:35:21 +0000755static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
756 u8 bus, u8 devfn, int alloc)
757{
758 struct root_entry *root = &iommu->root_entry[bus];
759 struct context_entry *context;
760 u64 *entry;
761
David Woodhousec83b2f22015-06-12 10:15:49 +0100762 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000763 if (devfn >= 0x80) {
764 devfn -= 0x80;
765 entry = &root->hi;
766 }
767 devfn *= 2;
768 }
769 entry = &root->lo;
770 if (*entry & 1)
771 context = phys_to_virt(*entry & VTD_PAGE_MASK);
772 else {
773 unsigned long phy_addr;
774 if (!alloc)
775 return NULL;
776
777 context = alloc_pgtable_page(iommu->node);
778 if (!context)
779 return NULL;
780
781 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
782 phy_addr = virt_to_phys((void *)context);
783 *entry = phy_addr | 1;
784 __iommu_flush_cache(iommu, entry, sizeof(*entry));
785 }
786 return &context[devfn];
787}
788
David Woodhouse4ed6a542015-05-11 14:59:20 +0100789static int iommu_dummy(struct device *dev)
790{
791 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
792}
793
David Woodhouse156baca2014-03-09 14:00:57 -0700794static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800795{
796 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800797 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700798 struct device *tmp;
799 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800800 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800801 int i;
802
David Woodhouse4ed6a542015-05-11 14:59:20 +0100803 if (iommu_dummy(dev))
804 return NULL;
805
David Woodhouse156baca2014-03-09 14:00:57 -0700806 if (dev_is_pci(dev)) {
807 pdev = to_pci_dev(dev);
808 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100809 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700810 dev = &ACPI_COMPANION(dev)->dev;
811
Jiang Liu0e242612014-02-19 14:07:34 +0800812 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800813 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700814 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100815 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800816
Jiang Liub683b232014-02-19 14:07:32 +0800817 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700818 drhd->devices_cnt, i, tmp) {
819 if (tmp == dev) {
820 *bus = drhd->devices[i].bus;
821 *devfn = drhd->devices[i].devfn;
822 goto out;
823 }
824
825 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000826 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700827
828 ptmp = to_pci_dev(tmp);
829 if (ptmp->subordinate &&
830 ptmp->subordinate->number <= pdev->bus->number &&
831 ptmp->subordinate->busn_res.end >= pdev->bus->number)
832 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100833 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800834
David Woodhouse156baca2014-03-09 14:00:57 -0700835 if (pdev && drhd->include_all) {
836 got_pdev:
837 *bus = pdev->bus->number;
838 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800839 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700840 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800841 }
Jiang Liub683b232014-02-19 14:07:32 +0800842 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700843 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800844 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800845
Jiang Liub683b232014-02-19 14:07:32 +0800846 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800847}
848
Weidong Han5331fe62008-12-08 23:00:00 +0800849static void domain_flush_cache(struct dmar_domain *domain,
850 void *addr, int size)
851{
852 if (!domain->iommu_coherency)
853 clflush_cache_range(addr, size);
854}
855
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700856static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
857{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700858 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000859 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700860 unsigned long flags;
861
862 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000863 context = iommu_context_addr(iommu, bus, devfn, 0);
864 if (context)
865 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700866 spin_unlock_irqrestore(&iommu->lock, flags);
867 return ret;
868}
869
870static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
871{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700872 struct context_entry *context;
873 unsigned long flags;
874
875 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000876 context = iommu_context_addr(iommu, bus, devfn, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700877 if (context) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000878 context_clear_entry(context);
879 __iommu_flush_cache(iommu, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700880 }
881 spin_unlock_irqrestore(&iommu->lock, flags);
882}
883
884static void free_context_table(struct intel_iommu *iommu)
885{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700886 int i;
887 unsigned long flags;
888 struct context_entry *context;
889
890 spin_lock_irqsave(&iommu->lock, flags);
891 if (!iommu->root_entry) {
892 goto out;
893 }
894 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000895 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700896 if (context)
897 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000898
David Woodhousec83b2f22015-06-12 10:15:49 +0100899 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000900 continue;
901
902 context = iommu_context_addr(iommu, i, 0x80, 0);
903 if (context)
904 free_pgtable_page(context);
905
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700906 }
907 free_pgtable_page(iommu->root_entry);
908 iommu->root_entry = NULL;
909out:
910 spin_unlock_irqrestore(&iommu->lock, flags);
911}
912
David Woodhouseb026fd22009-06-28 10:37:25 +0100913static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000914 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700915{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700916 struct dma_pte *parent, *pte = NULL;
917 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700918 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700919
920 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200921
Jiang Liu162d1b12014-07-11 14:19:35 +0800922 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200923 /* Address beyond IOMMU's addressing capabilities. */
924 return NULL;
925
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700926 parent = domain->pgd;
927
David Woodhouse5cf0a762014-03-19 16:07:49 +0000928 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700929 void *tmp_page;
930
David Woodhouseb026fd22009-06-28 10:37:25 +0100931 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700932 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000933 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100934 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000935 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700936 break;
937
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000938 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100939 uint64_t pteval;
940
Suresh Siddha4c923d42009-10-02 11:01:24 -0700941 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700942
David Woodhouse206a73c2009-07-01 19:30:28 +0100943 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700944 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +0100945
David Woodhousec85994e2009-07-01 19:21:24 +0100946 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400947 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 +0800948 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100949 /* Someone else set it while we were thinking; use theirs. */
950 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800951 else
David Woodhousec85994e2009-07-01 19:21:24 +0100952 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700953 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000954 if (level == 1)
955 break;
956
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000957 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700958 level--;
959 }
960
David Woodhouse5cf0a762014-03-19 16:07:49 +0000961 if (!*target_level)
962 *target_level = level;
963
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700964 return pte;
965}
966
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100967
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100969static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
970 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100971 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972{
973 struct dma_pte *parent, *pte = NULL;
974 int total = agaw_to_level(domain->agaw);
975 int offset;
976
977 parent = domain->pgd;
978 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100979 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700980 pte = &parent[offset];
981 if (level == total)
982 return pte;
983
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100984 if (!dma_pte_present(pte)) {
985 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700986 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100987 }
988
Yijing Wange16922a2014-05-20 20:37:51 +0800989 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100990 *large_page = total;
991 return pte;
992 }
993
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000994 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995 total--;
996 }
997 return NULL;
998}
999
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001000/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001001static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf2009-06-27 22:09:11 +01001002 unsigned long start_pfn,
1003 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001004{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001005 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001006 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001007
Jiang Liu162d1b12014-07-11 14:19:35 +08001008 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1009 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001010 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001011
David Woodhouse04b18e62009-06-27 19:15:01 +01001012 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001013 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001014 large_page = 1;
1015 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001016 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001017 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001018 continue;
1019 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001020 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001021 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001022 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001023 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001024 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1025
David Woodhouse310a5ab2009-06-28 18:52:20 +01001026 domain_flush_cache(domain, first_pte,
1027 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001028
1029 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001030}
1031
Alex Williamson3269ee02013-06-15 10:27:19 -06001032static void dma_pte_free_level(struct dmar_domain *domain, int level,
1033 struct dma_pte *pte, unsigned long pfn,
1034 unsigned long start_pfn, unsigned long last_pfn)
1035{
1036 pfn = max(start_pfn, pfn);
1037 pte = &pte[pfn_level_offset(pfn, level)];
1038
1039 do {
1040 unsigned long level_pfn;
1041 struct dma_pte *level_pte;
1042
1043 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1044 goto next;
1045
1046 level_pfn = pfn & level_mask(level - 1);
1047 level_pte = phys_to_virt(dma_pte_addr(pte));
1048
1049 if (level > 2)
1050 dma_pte_free_level(domain, level - 1, level_pte,
1051 level_pfn, start_pfn, last_pfn);
1052
1053 /* If range covers entire pagetable, free it */
1054 if (!(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001055 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001056 dma_clear_pte(pte);
1057 domain_flush_cache(domain, pte, sizeof(*pte));
1058 free_pgtable_page(level_pte);
1059 }
1060next:
1061 pfn += level_size(level);
1062 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1063}
1064
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001065/* free page table pages. last level pte should already be cleared */
1066static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001067 unsigned long start_pfn,
1068 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001069{
Jiang Liu162d1b12014-07-11 14:19:35 +08001070 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1071 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001072 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001073
Jiang Liud41a4ad2014-07-11 14:19:34 +08001074 dma_pte_clear_range(domain, start_pfn, last_pfn);
1075
David Woodhousef3a0a522009-06-30 03:40:07 +01001076 /* We don't need lock here; nobody else touches the iova range */
Alex Williamson3269ee02013-06-15 10:27:19 -06001077 dma_pte_free_level(domain, agaw_to_level(domain->agaw),
1078 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001079
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001080 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001081 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001082 free_pgtable_page(domain->pgd);
1083 domain->pgd = NULL;
1084 }
1085}
1086
David Woodhouseea8ea462014-03-05 17:09:32 +00001087/* When a page at a given level is being unlinked from its parent, we don't
1088 need to *modify* it at all. All we need to do is make a list of all the
1089 pages which can be freed just as soon as we've flushed the IOTLB and we
1090 know the hardware page-walk will no longer touch them.
1091 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1092 be freed. */
1093static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1094 int level, struct dma_pte *pte,
1095 struct page *freelist)
1096{
1097 struct page *pg;
1098
1099 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1100 pg->freelist = freelist;
1101 freelist = pg;
1102
1103 if (level == 1)
1104 return freelist;
1105
Jiang Liuadeb25902014-04-09 10:20:39 +08001106 pte = page_address(pg);
1107 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001108 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1109 freelist = dma_pte_list_pagetables(domain, level - 1,
1110 pte, freelist);
Jiang Liuadeb25902014-04-09 10:20:39 +08001111 pte++;
1112 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001113
1114 return freelist;
1115}
1116
1117static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1118 struct dma_pte *pte, unsigned long pfn,
1119 unsigned long start_pfn,
1120 unsigned long last_pfn,
1121 struct page *freelist)
1122{
1123 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1124
1125 pfn = max(start_pfn, pfn);
1126 pte = &pte[pfn_level_offset(pfn, level)];
1127
1128 do {
1129 unsigned long level_pfn;
1130
1131 if (!dma_pte_present(pte))
1132 goto next;
1133
1134 level_pfn = pfn & level_mask(level);
1135
1136 /* If range covers entire pagetable, free it */
1137 if (start_pfn <= level_pfn &&
1138 last_pfn >= level_pfn + level_size(level) - 1) {
1139 /* These suborbinate page tables are going away entirely. Don't
1140 bother to clear them; we're just going to *free* them. */
1141 if (level > 1 && !dma_pte_superpage(pte))
1142 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1143
1144 dma_clear_pte(pte);
1145 if (!first_pte)
1146 first_pte = pte;
1147 last_pte = pte;
1148 } else if (level > 1) {
1149 /* Recurse down into a level that isn't *entirely* obsolete */
1150 freelist = dma_pte_clear_level(domain, level - 1,
1151 phys_to_virt(dma_pte_addr(pte)),
1152 level_pfn, start_pfn, last_pfn,
1153 freelist);
1154 }
1155next:
1156 pfn += level_size(level);
1157 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1158
1159 if (first_pte)
1160 domain_flush_cache(domain, first_pte,
1161 (void *)++last_pte - (void *)first_pte);
1162
1163 return freelist;
1164}
1165
1166/* We can't just free the pages because the IOMMU may still be walking
1167 the page tables, and may have cached the intermediate levels. The
1168 pages can only be freed after the IOTLB flush has been done. */
1169struct page *domain_unmap(struct dmar_domain *domain,
1170 unsigned long start_pfn,
1171 unsigned long last_pfn)
1172{
David Woodhouseea8ea462014-03-05 17:09:32 +00001173 struct page *freelist = NULL;
1174
Jiang Liu162d1b12014-07-11 14:19:35 +08001175 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1176 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001177 BUG_ON(start_pfn > last_pfn);
1178
1179 /* we don't need lock here; nobody else touches the iova range */
1180 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1181 domain->pgd, 0, start_pfn, last_pfn, NULL);
1182
1183 /* free pgd */
1184 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1185 struct page *pgd_page = virt_to_page(domain->pgd);
1186 pgd_page->freelist = freelist;
1187 freelist = pgd_page;
1188
1189 domain->pgd = NULL;
1190 }
1191
1192 return freelist;
1193}
1194
1195void dma_free_pagelist(struct page *freelist)
1196{
1197 struct page *pg;
1198
1199 while ((pg = freelist)) {
1200 freelist = pg->freelist;
1201 free_pgtable_page(page_address(pg));
1202 }
1203}
1204
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001205/* iommu handling */
1206static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1207{
1208 struct root_entry *root;
1209 unsigned long flags;
1210
Suresh Siddha4c923d42009-10-02 11:01:24 -07001211 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001212 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001213 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001214 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001215 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001216 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001217
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001218 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001219
1220 spin_lock_irqsave(&iommu->lock, flags);
1221 iommu->root_entry = root;
1222 spin_unlock_irqrestore(&iommu->lock, flags);
1223
1224 return 0;
1225}
1226
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001227static void iommu_set_root_entry(struct intel_iommu *iommu)
1228{
David Woodhouse03ecc322015-02-13 14:35:21 +00001229 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001230 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001231 unsigned long flag;
1232
David Woodhouse03ecc322015-02-13 14:35:21 +00001233 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001234 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001235 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001236
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001237 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001238 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001239
David Woodhousec416daa2009-05-10 20:30:58 +01001240 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241
1242 /* Make sure hardware complete it */
1243 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001244 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001245
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001246 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001247}
1248
1249static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1250{
1251 u32 val;
1252 unsigned long flag;
1253
David Woodhouse9af88142009-02-13 23:18:03 +00001254 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001255 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001256
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001257 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001258 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001259
1260 /* Make sure hardware complete it */
1261 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001262 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001263
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001264 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001265}
1266
1267/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001268static void __iommu_flush_context(struct intel_iommu *iommu,
1269 u16 did, u16 source_id, u8 function_mask,
1270 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001271{
1272 u64 val = 0;
1273 unsigned long flag;
1274
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001275 switch (type) {
1276 case DMA_CCMD_GLOBAL_INVL:
1277 val = DMA_CCMD_GLOBAL_INVL;
1278 break;
1279 case DMA_CCMD_DOMAIN_INVL:
1280 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1281 break;
1282 case DMA_CCMD_DEVICE_INVL:
1283 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1284 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1285 break;
1286 default:
1287 BUG();
1288 }
1289 val |= DMA_CCMD_ICC;
1290
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001291 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1293
1294 /* Make sure hardware complete it */
1295 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1296 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1297
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001298 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001299}
1300
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001301/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001302static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1303 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001304{
1305 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1306 u64 val = 0, val_iva = 0;
1307 unsigned long flag;
1308
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001309 switch (type) {
1310 case DMA_TLB_GLOBAL_FLUSH:
1311 /* global flush doesn't need set IVA_REG */
1312 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1313 break;
1314 case DMA_TLB_DSI_FLUSH:
1315 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1316 break;
1317 case DMA_TLB_PSI_FLUSH:
1318 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001319 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001320 val_iva = size_order | addr;
1321 break;
1322 default:
1323 BUG();
1324 }
1325 /* Note: set drain read/write */
1326#if 0
1327 /*
1328 * This is probably to be super secure.. Looks like we can
1329 * ignore it without any impact.
1330 */
1331 if (cap_read_drain(iommu->cap))
1332 val |= DMA_TLB_READ_DRAIN;
1333#endif
1334 if (cap_write_drain(iommu->cap))
1335 val |= DMA_TLB_WRITE_DRAIN;
1336
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001337 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001338 /* Note: Only uses first TLB reg currently */
1339 if (val_iva)
1340 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1341 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1342
1343 /* Make sure hardware complete it */
1344 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1345 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1346
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001347 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001348
1349 /* check IOTLB invalidation granularity */
1350 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001351 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001352 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001353 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001354 (unsigned long long)DMA_TLB_IIRG(type),
1355 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001356}
1357
David Woodhouse64ae8922014-03-09 12:52:30 -07001358static struct device_domain_info *
1359iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1360 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001361{
Quentin Lambert2f119c72015-02-06 10:59:53 +01001362 bool found = false;
Yu Zhao93a23a72009-05-18 13:51:37 +08001363 unsigned long flags;
1364 struct device_domain_info *info;
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001365 struct pci_dev *pdev;
Yu Zhao93a23a72009-05-18 13:51:37 +08001366
1367 if (!ecap_dev_iotlb_support(iommu->ecap))
1368 return NULL;
1369
1370 if (!iommu->qi)
1371 return NULL;
1372
1373 spin_lock_irqsave(&device_domain_lock, flags);
1374 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001375 if (info->iommu == iommu && info->bus == bus &&
1376 info->devfn == devfn) {
Quentin Lambert2f119c72015-02-06 10:59:53 +01001377 found = true;
Yu Zhao93a23a72009-05-18 13:51:37 +08001378 break;
1379 }
1380 spin_unlock_irqrestore(&device_domain_lock, flags);
1381
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001382 if (!found || !info->dev || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001383 return NULL;
1384
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001385 pdev = to_pci_dev(info->dev);
1386
1387 if (!pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS))
Yu Zhao93a23a72009-05-18 13:51:37 +08001388 return NULL;
1389
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001390 if (!dmar_find_matched_atsr_unit(pdev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001391 return NULL;
1392
Yu Zhao93a23a72009-05-18 13:51:37 +08001393 return info;
1394}
1395
1396static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1397{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001398 struct pci_dev *pdev;
1399
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001400 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001401 return;
1402
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001403 pdev = to_pci_dev(info->dev);
1404 if (pci_enable_ats(pdev, VTD_PAGE_SHIFT))
1405 return;
1406
1407 info->ats.enabled = 1;
1408 info->ats.qdep = pci_ats_queue_depth(pdev);
Yu Zhao93a23a72009-05-18 13:51:37 +08001409}
1410
1411static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1412{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001413 if (!info->ats.enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001414 return;
1415
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001416 pci_disable_ats(to_pci_dev(info->dev));
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001417 info->ats.enabled = 0;
Yu Zhao93a23a72009-05-18 13:51:37 +08001418}
1419
1420static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1421 u64 addr, unsigned mask)
1422{
1423 u16 sid, qdep;
1424 unsigned long flags;
1425 struct device_domain_info *info;
1426
1427 spin_lock_irqsave(&device_domain_lock, flags);
1428 list_for_each_entry(info, &domain->devices, link) {
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001429 if (!info->ats.enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001430 continue;
1431
1432 sid = info->bus << 8 | info->devfn;
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001433 qdep = info->ats.qdep;
Yu Zhao93a23a72009-05-18 13:51:37 +08001434 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1435 }
1436 spin_unlock_irqrestore(&device_domain_lock, flags);
1437}
1438
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001439static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
David Woodhouseea8ea462014-03-05 17:09:32 +00001440 unsigned long pfn, unsigned int pages, int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001441{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001442 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001443 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001444
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001445 BUG_ON(pages == 0);
1446
David Woodhouseea8ea462014-03-05 17:09:32 +00001447 if (ih)
1448 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001449 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001450 * Fallback to domain selective flush if no PSI support or the size is
1451 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001452 * PSI requires page size to be 2 ^ x, and the base address is naturally
1453 * aligned to the size
1454 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001455 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1456 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001457 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001458 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001459 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001460 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001461
1462 /*
Nadav Amit82653632010-04-01 13:24:40 +03001463 * In caching mode, changes of pages from non-present to present require
1464 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001465 */
Nadav Amit82653632010-04-01 13:24:40 +03001466 if (!cap_caching_mode(iommu->cap) || !map)
Yu Zhao93a23a72009-05-18 13:51:37 +08001467 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001468}
1469
mark grossf8bab732008-02-08 04:18:38 -08001470static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1471{
1472 u32 pmen;
1473 unsigned long flags;
1474
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001475 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001476 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1477 pmen &= ~DMA_PMEN_EPM;
1478 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1479
1480 /* wait for the protected region status bit to clear */
1481 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1482 readl, !(pmen & DMA_PMEN_PRS), pmen);
1483
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001484 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001485}
1486
Jiang Liu2a41cce2014-07-11 14:19:33 +08001487static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001488{
1489 u32 sts;
1490 unsigned long flags;
1491
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001492 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001493 iommu->gcmd |= DMA_GCMD_TE;
1494 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001495
1496 /* Make sure hardware complete it */
1497 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001498 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001499
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001500 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001501}
1502
Jiang Liu2a41cce2014-07-11 14:19:33 +08001503static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001504{
1505 u32 sts;
1506 unsigned long flag;
1507
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001508 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001509 iommu->gcmd &= ~DMA_GCMD_TE;
1510 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1511
1512 /* Make sure hardware complete it */
1513 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001514 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001515
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001516 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001517}
1518
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001519
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001520static int iommu_init_domains(struct intel_iommu *iommu)
1521{
1522 unsigned long ndomains;
1523 unsigned long nlongs;
1524
1525 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001526 pr_debug("%s: Number of Domains supported <%ld>\n",
1527 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001528 nlongs = BITS_TO_LONGS(ndomains);
1529
Donald Dutile94a91b52009-08-20 16:51:34 -04001530 spin_lock_init(&iommu->lock);
1531
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001532 /* TBD: there might be 64K domains,
1533 * consider other allocation for future chip
1534 */
1535 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1536 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001537 pr_err("%s: Allocating domain id array failed\n",
1538 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001539 return -ENOMEM;
1540 }
1541 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1542 GFP_KERNEL);
1543 if (!iommu->domains) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001544 pr_err("%s: Allocating domain array failed\n",
1545 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001546 kfree(iommu->domain_ids);
1547 iommu->domain_ids = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001548 return -ENOMEM;
1549 }
1550
1551 /*
1552 * if Caching mode is set, then invalid translations are tagged
1553 * with domainid 0. Hence we need to pre-allocate it.
1554 */
1555 if (cap_caching_mode(iommu->cap))
1556 set_bit(0, iommu->domain_ids);
1557 return 0;
1558}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001559
Jiang Liuffebeb42014-11-09 22:48:02 +08001560static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001561{
1562 struct dmar_domain *domain;
Jiang Liu2a46ddf2014-07-11 14:19:30 +08001563 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001564
Donald Dutile94a91b52009-08-20 16:51:34 -04001565 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001566 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Jiang Liua4eaa862014-02-19 14:07:30 +08001567 /*
1568 * Domain id 0 is reserved for invalid translation
1569 * if hardware supports caching mode.
1570 */
1571 if (cap_caching_mode(iommu->cap) && i == 0)
1572 continue;
1573
Donald Dutile94a91b52009-08-20 16:51:34 -04001574 domain = iommu->domains[i];
1575 clear_bit(i, iommu->domain_ids);
Jiang Liu129ad282014-07-11 14:19:31 +08001576 if (domain_detach_iommu(domain, iommu) == 0 &&
1577 !domain_type_is_vm(domain))
Jiang Liu92d03cc2014-02-19 14:07:28 +08001578 domain_exit(domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001579 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001580 }
1581
1582 if (iommu->gcmd & DMA_GCMD_TE)
1583 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001584}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001585
Jiang Liuffebeb42014-11-09 22:48:02 +08001586static void free_dmar_iommu(struct intel_iommu *iommu)
1587{
1588 if ((iommu->domains) && (iommu->domain_ids)) {
1589 kfree(iommu->domains);
1590 kfree(iommu->domain_ids);
1591 iommu->domains = NULL;
1592 iommu->domain_ids = NULL;
1593 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001594
Weidong Hand9630fe2008-12-08 11:06:32 +08001595 g_iommus[iommu->seq_id] = NULL;
1596
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001597 /* free context mapping */
1598 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001599}
1600
Jiang Liuab8dfe22014-07-11 14:19:27 +08001601static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001602{
Jiang Liu92d03cc2014-02-19 14:07:28 +08001603 /* domain id for virtual machine, it won't be set in context */
1604 static atomic_t vm_domid = ATOMIC_INIT(0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001605 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001606
1607 domain = alloc_domain_mem();
1608 if (!domain)
1609 return NULL;
1610
Jiang Liuab8dfe22014-07-11 14:19:27 +08001611 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001612 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001613 domain->flags = flags;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001614 spin_lock_init(&domain->iommu_lock);
1615 INIT_LIST_HEAD(&domain->devices);
Jiang Liuab8dfe22014-07-11 14:19:27 +08001616 if (flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Jiang Liu92d03cc2014-02-19 14:07:28 +08001617 domain->id = atomic_inc_return(&vm_domid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001618
1619 return domain;
1620}
1621
Jiang Liufb170fb2014-07-11 14:19:28 +08001622static int __iommu_attach_domain(struct dmar_domain *domain,
1623 struct intel_iommu *iommu)
1624{
1625 int num;
1626 unsigned long ndomains;
1627
1628 ndomains = cap_ndoms(iommu->cap);
1629 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1630 if (num < ndomains) {
1631 set_bit(num, iommu->domain_ids);
1632 iommu->domains[num] = domain;
1633 } else {
1634 num = -ENOSPC;
1635 }
1636
1637 return num;
1638}
1639
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001640static int iommu_attach_domain(struct dmar_domain *domain,
1641 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001642{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001643 int num;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001644 unsigned long flags;
1645
Weidong Han8c11e792008-12-08 15:29:22 +08001646 spin_lock_irqsave(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001647 num = __iommu_attach_domain(domain, iommu);
Jiang Liu44bde612014-07-11 14:19:29 +08001648 spin_unlock_irqrestore(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001649 if (num < 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001650 pr_err("%s: No free domain ids\n", iommu->name);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001651
Jiang Liufb170fb2014-07-11 14:19:28 +08001652 return num;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001653}
1654
Jiang Liu44bde612014-07-11 14:19:29 +08001655static int iommu_attach_vm_domain(struct dmar_domain *domain,
1656 struct intel_iommu *iommu)
1657{
1658 int num;
1659 unsigned long ndomains;
1660
1661 ndomains = cap_ndoms(iommu->cap);
1662 for_each_set_bit(num, iommu->domain_ids, ndomains)
1663 if (iommu->domains[num] == domain)
1664 return num;
1665
1666 return __iommu_attach_domain(domain, iommu);
1667}
1668
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001669static void iommu_detach_domain(struct dmar_domain *domain,
1670 struct intel_iommu *iommu)
1671{
1672 unsigned long flags;
1673 int num, ndomains;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001674
1675 spin_lock_irqsave(&iommu->lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08001676 if (domain_type_is_vm_or_si(domain)) {
1677 ndomains = cap_ndoms(iommu->cap);
1678 for_each_set_bit(num, iommu->domain_ids, ndomains) {
1679 if (iommu->domains[num] == domain) {
1680 clear_bit(num, iommu->domain_ids);
1681 iommu->domains[num] = NULL;
1682 break;
1683 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001684 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001685 } else {
1686 clear_bit(domain->id, iommu->domain_ids);
1687 iommu->domains[domain->id] = NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001688 }
Weidong Han8c11e792008-12-08 15:29:22 +08001689 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001690}
1691
Jiang Liufb170fb2014-07-11 14:19:28 +08001692static void domain_attach_iommu(struct dmar_domain *domain,
1693 struct intel_iommu *iommu)
1694{
1695 unsigned long flags;
1696
1697 spin_lock_irqsave(&domain->iommu_lock, flags);
1698 if (!test_and_set_bit(iommu->seq_id, domain->iommu_bmp)) {
1699 domain->iommu_count++;
1700 if (domain->iommu_count == 1)
1701 domain->nid = iommu->node;
1702 domain_update_iommu_cap(domain);
1703 }
1704 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1705}
1706
1707static int domain_detach_iommu(struct dmar_domain *domain,
1708 struct intel_iommu *iommu)
1709{
1710 unsigned long flags;
1711 int count = INT_MAX;
1712
1713 spin_lock_irqsave(&domain->iommu_lock, flags);
1714 if (test_and_clear_bit(iommu->seq_id, domain->iommu_bmp)) {
1715 count = --domain->iommu_count;
1716 domain_update_iommu_cap(domain);
1717 }
1718 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1719
1720 return count;
1721}
1722
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001723static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001724static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001725
Joseph Cihula51a63e62011-03-21 11:04:24 -07001726static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001727{
1728 struct pci_dev *pdev = NULL;
1729 struct iova *iova;
1730 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001731
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001732 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN,
1733 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001734
Mark Gross8a443df2008-03-04 14:59:31 -08001735 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1736 &reserved_rbtree_key);
1737
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001738 /* IOAPIC ranges shouldn't be accessed by DMA */
1739 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1740 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001741 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001742 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001743 return -ENODEV;
1744 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001745
1746 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1747 for_each_pci_dev(pdev) {
1748 struct resource *r;
1749
1750 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1751 r = &pdev->resource[i];
1752 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1753 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001754 iova = reserve_iova(&reserved_iova_list,
1755 IOVA_PFN(r->start),
1756 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001757 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001758 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001759 return -ENODEV;
1760 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001761 }
1762 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001763 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001764}
1765
1766static void domain_reserve_special_ranges(struct dmar_domain *domain)
1767{
1768 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1769}
1770
1771static inline int guestwidth_to_adjustwidth(int gaw)
1772{
1773 int agaw;
1774 int r = (gaw - 12) % 9;
1775
1776 if (r == 0)
1777 agaw = gaw;
1778 else
1779 agaw = gaw + 9 - r;
1780 if (agaw > 64)
1781 agaw = 64;
1782 return agaw;
1783}
1784
1785static int domain_init(struct dmar_domain *domain, int guest_width)
1786{
1787 struct intel_iommu *iommu;
1788 int adjust_width, agaw;
1789 unsigned long sagaw;
1790
Robin Murphy0fb5fe82015-01-12 17:51:16 +00001791 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
1792 DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001793 domain_reserve_special_ranges(domain);
1794
1795 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001796 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001797 if (guest_width > cap_mgaw(iommu->cap))
1798 guest_width = cap_mgaw(iommu->cap);
1799 domain->gaw = guest_width;
1800 adjust_width = guestwidth_to_adjustwidth(guest_width);
1801 agaw = width_to_agaw(adjust_width);
1802 sagaw = cap_sagaw(iommu->cap);
1803 if (!test_bit(agaw, &sagaw)) {
1804 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001805 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001806 agaw = find_next_bit(&sagaw, 5, agaw);
1807 if (agaw >= 5)
1808 return -ENODEV;
1809 }
1810 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001811
Weidong Han8e6040972008-12-08 15:49:06 +08001812 if (ecap_coherent(iommu->ecap))
1813 domain->iommu_coherency = 1;
1814 else
1815 domain->iommu_coherency = 0;
1816
Sheng Yang58c610b2009-03-18 15:33:05 +08001817 if (ecap_sc_support(iommu->ecap))
1818 domain->iommu_snooping = 1;
1819 else
1820 domain->iommu_snooping = 0;
1821
David Woodhouse214e39a2014-03-19 10:38:49 +00001822 if (intel_iommu_superpage)
1823 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1824 else
1825 domain->iommu_superpage = 0;
1826
Suresh Siddha4c923d42009-10-02 11:01:24 -07001827 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001828
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001829 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001830 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001831 if (!domain->pgd)
1832 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001833 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001834 return 0;
1835}
1836
1837static void domain_exit(struct dmar_domain *domain)
1838{
Alex Williamson46ebb7a2015-07-14 14:48:53 -06001839 struct dmar_drhd_unit *drhd;
1840 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00001841 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001842
1843 /* Domain 0 is reserved, so dont process it */
1844 if (!domain)
1845 return;
1846
Alex Williamson7b668352011-05-24 12:02:41 +01001847 /* Flush any lazy unmaps that may reference this domain */
1848 if (!intel_iommu_strict)
1849 flush_unmaps_timeout(0);
1850
Jiang Liu92d03cc2014-02-19 14:07:28 +08001851 /* remove associated devices */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001852 domain_remove_dev_info(domain);
Jiang Liu92d03cc2014-02-19 14:07:28 +08001853
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001854 /* destroy iovas */
1855 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001856
David Woodhouseea8ea462014-03-05 17:09:32 +00001857 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001858
Jiang Liu92d03cc2014-02-19 14:07:28 +08001859 /* clear attached or cached domains */
Jiang Liu0e242612014-02-19 14:07:34 +08001860 rcu_read_lock();
Alex Williamson46ebb7a2015-07-14 14:48:53 -06001861 for_each_active_iommu(iommu, drhd)
1862 if (domain_type_is_vm(domain) ||
1863 test_bit(iommu->seq_id, domain->iommu_bmp))
1864 iommu_detach_domain(domain, iommu);
Jiang Liu0e242612014-02-19 14:07:34 +08001865 rcu_read_unlock();
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001866
David Woodhouseea8ea462014-03-05 17:09:32 +00001867 dma_free_pagelist(freelist);
1868
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869 free_domain_mem(domain);
1870}
1871
David Woodhouse64ae8922014-03-09 12:52:30 -07001872static int domain_context_mapping_one(struct dmar_domain *domain,
1873 struct intel_iommu *iommu,
1874 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001875{
1876 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001877 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001878 struct dma_pte *pgd;
Weidong Hanea6606b2008-12-08 23:08:15 +08001879 int id;
1880 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001881 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001882
1883 pr_debug("Set context mapping for %02x:%02x.%d\n",
1884 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001885
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001886 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001887 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1888 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001889
David Woodhouse03ecc322015-02-13 14:35:21 +00001890 spin_lock_irqsave(&iommu->lock, flags);
1891 context = iommu_context_addr(iommu, bus, devfn, 1);
1892 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001893 if (!context)
1894 return -ENOMEM;
1895 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001896 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001897 spin_unlock_irqrestore(&iommu->lock, flags);
1898 return 0;
1899 }
1900
Joerg Roedelcf484d02015-06-12 12:21:46 +02001901 context_clear_entry(context);
1902
Weidong Hanea6606b2008-12-08 23:08:15 +08001903 id = domain->id;
1904 pgd = domain->pgd;
1905
Jiang Liuab8dfe22014-07-11 14:19:27 +08001906 if (domain_type_is_vm_or_si(domain)) {
Jiang Liu44bde612014-07-11 14:19:29 +08001907 if (domain_type_is_vm(domain)) {
1908 id = iommu_attach_vm_domain(domain, iommu);
Jiang Liufb170fb2014-07-11 14:19:28 +08001909 if (id < 0) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001910 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001911 pr_err("%s: No free domain ids\n", iommu->name);
Weidong Hanea6606b2008-12-08 23:08:15 +08001912 return -EFAULT;
1913 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001914 }
1915
1916 /* Skip top levels of page tables for
1917 * iommu which has less agaw than default.
Chris Wright1672af12009-12-02 12:06:34 -08001918 * Unnecessary for PT mode.
Weidong Hanea6606b2008-12-08 23:08:15 +08001919 */
Chris Wright1672af12009-12-02 12:06:34 -08001920 if (translation != CONTEXT_TT_PASS_THROUGH) {
1921 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1922 pgd = phys_to_virt(dma_pte_addr(pgd));
1923 if (!dma_pte_present(pgd)) {
1924 spin_unlock_irqrestore(&iommu->lock, flags);
1925 return -ENOMEM;
1926 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001927 }
1928 }
1929 }
1930
1931 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001932
Yu Zhao93a23a72009-05-18 13:51:37 +08001933 if (translation != CONTEXT_TT_PASS_THROUGH) {
David Woodhouse64ae8922014-03-09 12:52:30 -07001934 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
Yu Zhao93a23a72009-05-18 13:51:37 +08001935 translation = info ? CONTEXT_TT_DEV_IOTLB :
1936 CONTEXT_TT_MULTI_LEVEL;
1937 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001938 /*
1939 * In pass through mode, AW must be programmed to indicate the largest
1940 * AGAW value supported by hardware. And ASR is ignored by hardware.
1941 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001942 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001943 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001944 else {
1945 context_set_address_root(context, virt_to_phys(pgd));
1946 context_set_address_width(context, iommu->agaw);
1947 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001948
1949 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001950 context_set_fault_enable(context);
1951 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001952 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001953
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001954 /*
1955 * It's a non-present to present mapping. If hardware doesn't cache
1956 * non-present entry we only need to flush the write-buffer. If the
1957 * _does_ cache non-present entries, then it does so in the special
1958 * domain #0, which we have to flush:
1959 */
1960 if (cap_caching_mode(iommu->cap)) {
1961 iommu->flush.flush_context(iommu, 0,
1962 (((u16)bus) << 8) | devfn,
1963 DMA_CCMD_MASK_NOBIT,
1964 DMA_CCMD_DEVICE_INVL);
Jiang Liu18fd7792014-07-11 14:19:26 +08001965 iommu->flush.flush_iotlb(iommu, id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001966 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001967 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001968 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001969 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001970 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001971
Jiang Liufb170fb2014-07-11 14:19:28 +08001972 domain_attach_iommu(domain, iommu);
1973
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001974 return 0;
1975}
1976
Alex Williamson579305f2014-07-03 09:51:43 -06001977struct domain_context_mapping_data {
1978 struct dmar_domain *domain;
1979 struct intel_iommu *iommu;
1980 int translation;
1981};
1982
1983static int domain_context_mapping_cb(struct pci_dev *pdev,
1984 u16 alias, void *opaque)
1985{
1986 struct domain_context_mapping_data *data = opaque;
1987
1988 return domain_context_mapping_one(data->domain, data->iommu,
1989 PCI_BUS_NUM(alias), alias & 0xff,
1990 data->translation);
1991}
1992
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001993static int
David Woodhousee1f167f2014-03-09 15:24:46 -07001994domain_context_mapping(struct dmar_domain *domain, struct device *dev,
1995 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001996{
David Woodhouse64ae8922014-03-09 12:52:30 -07001997 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07001998 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06001999 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002000
David Woodhousee1f167f2014-03-09 15:24:46 -07002001 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002002 if (!iommu)
2003 return -ENODEV;
2004
Alex Williamson579305f2014-07-03 09:51:43 -06002005 if (!dev_is_pci(dev))
2006 return domain_context_mapping_one(domain, iommu, bus, devfn,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002007 translation);
Alex Williamson579305f2014-07-03 09:51:43 -06002008
2009 data.domain = domain;
2010 data.iommu = iommu;
2011 data.translation = translation;
2012
2013 return pci_for_each_dma_alias(to_pci_dev(dev),
2014 &domain_context_mapping_cb, &data);
2015}
2016
2017static int domain_context_mapped_cb(struct pci_dev *pdev,
2018 u16 alias, void *opaque)
2019{
2020 struct intel_iommu *iommu = opaque;
2021
2022 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002023}
2024
David Woodhousee1f167f2014-03-09 15:24:46 -07002025static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002026{
Weidong Han5331fe62008-12-08 23:00:00 +08002027 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002028 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002029
David Woodhousee1f167f2014-03-09 15:24:46 -07002030 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002031 if (!iommu)
2032 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002033
Alex Williamson579305f2014-07-03 09:51:43 -06002034 if (!dev_is_pci(dev))
2035 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002036
Alex Williamson579305f2014-07-03 09:51:43 -06002037 return !pci_for_each_dma_alias(to_pci_dev(dev),
2038 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002039}
2040
Fenghua Yuf5329592009-08-04 15:09:37 -07002041/* Returns a number of VTD pages, but aligned to MM page size */
2042static inline unsigned long aligned_nrpages(unsigned long host_addr,
2043 size_t size)
2044{
2045 host_addr &= ~PAGE_MASK;
2046 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2047}
2048
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002049/* Return largest possible superpage level for a given mapping */
2050static inline int hardware_largepage_caps(struct dmar_domain *domain,
2051 unsigned long iov_pfn,
2052 unsigned long phy_pfn,
2053 unsigned long pages)
2054{
2055 int support, level = 1;
2056 unsigned long pfnmerge;
2057
2058 support = domain->iommu_superpage;
2059
2060 /* To use a large page, the virtual *and* physical addresses
2061 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2062 of them will mean we have to use smaller pages. So just
2063 merge them and check both at once. */
2064 pfnmerge = iov_pfn | phy_pfn;
2065
2066 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2067 pages >>= VTD_STRIDE_SHIFT;
2068 if (!pages)
2069 break;
2070 pfnmerge >>= VTD_STRIDE_SHIFT;
2071 level++;
2072 support--;
2073 }
2074 return level;
2075}
2076
David Woodhouse9051aa02009-06-29 12:30:54 +01002077static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2078 struct scatterlist *sg, unsigned long phys_pfn,
2079 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002080{
2081 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002082 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002083 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002084 unsigned int largepage_lvl = 0;
2085 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002086
Jiang Liu162d1b12014-07-11 14:19:35 +08002087 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002088
2089 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2090 return -EINVAL;
2091
2092 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2093
Jiang Liucc4f14a2014-11-26 09:42:10 +08002094 if (!sg) {
2095 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002096 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2097 }
2098
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002099 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002100 uint64_t tmp;
2101
David Woodhousee1605492009-06-29 11:17:38 +01002102 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07002103 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01002104 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
2105 sg->dma_length = sg->length;
2106 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002107 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002108 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002109
David Woodhousee1605492009-06-29 11:17:38 +01002110 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002111 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2112
David Woodhouse5cf0a762014-03-19 16:07:49 +00002113 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002114 if (!pte)
2115 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002116 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002117 if (largepage_lvl > 1) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002118 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002119 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2120 /*
2121 * Ensure that old small page tables are
2122 * removed to make room for superpage,
2123 * if they exist.
2124 */
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002125 dma_pte_free_pagetable(domain, iov_pfn,
Jiang Liud41a4ad2014-07-11 14:19:34 +08002126 iov_pfn + lvl_pages - 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002127 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002128 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002129 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002130
David Woodhousee1605492009-06-29 11:17:38 +01002131 }
2132 /* We don't need lock here, nobody else
2133 * touches the iova range
2134 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002135 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002136 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002137 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002138 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2139 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002140 if (dumps) {
2141 dumps--;
2142 debug_dma_dump_mappings(NULL);
2143 }
2144 WARN_ON(1);
2145 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002146
2147 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2148
2149 BUG_ON(nr_pages < lvl_pages);
2150 BUG_ON(sg_res < lvl_pages);
2151
2152 nr_pages -= lvl_pages;
2153 iov_pfn += lvl_pages;
2154 phys_pfn += lvl_pages;
2155 pteval += lvl_pages * VTD_PAGE_SIZE;
2156 sg_res -= lvl_pages;
2157
2158 /* If the next PTE would be the first in a new page, then we
2159 need to flush the cache on the entries we've just written.
2160 And then we'll need to recalculate 'pte', so clear it and
2161 let it get set again in the if (!pte) block above.
2162
2163 If we're done (!nr_pages) we need to flush the cache too.
2164
2165 Also if we've been setting superpages, we may need to
2166 recalculate 'pte' and switch back to smaller pages for the
2167 end of the mapping, if the trailing size is not enough to
2168 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002169 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002170 if (!nr_pages || first_pte_in_page(pte) ||
2171 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002172 domain_flush_cache(domain, first_pte,
2173 (void *)pte - (void *)first_pte);
2174 pte = NULL;
2175 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002176
2177 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002178 sg = sg_next(sg);
2179 }
2180 return 0;
2181}
2182
David Woodhouse9051aa02009-06-29 12:30:54 +01002183static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2184 struct scatterlist *sg, unsigned long nr_pages,
2185 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002186{
David Woodhouse9051aa02009-06-29 12:30:54 +01002187 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
2188}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002189
David Woodhouse9051aa02009-06-29 12:30:54 +01002190static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2191 unsigned long phys_pfn, unsigned long nr_pages,
2192 int prot)
2193{
2194 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002195}
2196
Weidong Hanc7151a82008-12-08 22:51:37 +08002197static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002198{
Weidong Hanc7151a82008-12-08 22:51:37 +08002199 if (!iommu)
2200 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002201
2202 clear_context_table(iommu, bus, devfn);
2203 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002204 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002205 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002206}
2207
David Woodhouse109b9b02012-05-25 17:43:02 +01002208static inline void unlink_domain_info(struct device_domain_info *info)
2209{
2210 assert_spin_locked(&device_domain_lock);
2211 list_del(&info->link);
2212 list_del(&info->global);
2213 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002214 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002215}
2216
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002217static void domain_remove_dev_info(struct dmar_domain *domain)
2218{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002219 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002220 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002221
2222 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wang3a74ca02014-05-20 20:37:47 +08002223 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhouse109b9b02012-05-25 17:43:02 +01002224 unlink_domain_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002225 spin_unlock_irqrestore(&device_domain_lock, flags);
2226
Yu Zhao93a23a72009-05-18 13:51:37 +08002227 iommu_disable_dev_iotlb(info);
David Woodhouse7c7faa12014-03-09 13:33:06 -07002228 iommu_detach_dev(info->iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002229
Jiang Liuab8dfe22014-07-11 14:19:27 +08002230 if (domain_type_is_vm(domain)) {
David Woodhouse7c7faa12014-03-09 13:33:06 -07002231 iommu_detach_dependent_devices(info->iommu, info->dev);
Jiang Liufb170fb2014-07-11 14:19:28 +08002232 domain_detach_iommu(domain, info->iommu);
Jiang Liu92d03cc2014-02-19 14:07:28 +08002233 }
2234
2235 free_devinfo_mem(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002236 spin_lock_irqsave(&device_domain_lock, flags);
2237 }
2238 spin_unlock_irqrestore(&device_domain_lock, flags);
2239}
2240
2241/*
2242 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002243 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002244 */
David Woodhouse1525a292014-03-06 16:19:30 +00002245static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002246{
2247 struct device_domain_info *info;
2248
2249 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002250 info = dev->archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002251 if (info)
2252 return info->domain;
2253 return NULL;
2254}
2255
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002256static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002257dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2258{
2259 struct device_domain_info *info;
2260
2261 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002262 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002263 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002264 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002265
2266 return NULL;
2267}
2268
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002269static struct dmar_domain *dmar_insert_dev_info(struct intel_iommu *iommu,
David Woodhouse41e80dca2014-03-09 13:55:54 -07002270 int bus, int devfn,
David Woodhouseb718cd32014-03-09 13:11:33 -07002271 struct device *dev,
2272 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002273{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002274 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002275 struct device_domain_info *info;
2276 unsigned long flags;
2277
2278 info = alloc_devinfo_mem();
2279 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002280 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002281
Jiang Liu745f2582014-02-19 14:07:26 +08002282 info->bus = bus;
2283 info->devfn = devfn;
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05002284 info->ats.enabled = 0;
2285 info->ats.qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002286 info->dev = dev;
2287 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002288 info->iommu = iommu;
Jiang Liu745f2582014-02-19 14:07:26 +08002289
2290 spin_lock_irqsave(&device_domain_lock, flags);
2291 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002292 found = find_domain(dev);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002293 else {
2294 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002295 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002296 if (info2)
2297 found = info2->domain;
2298 }
Jiang Liu745f2582014-02-19 14:07:26 +08002299 if (found) {
2300 spin_unlock_irqrestore(&device_domain_lock, flags);
2301 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002302 /* Caller must free the original domain */
2303 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002304 }
2305
David Woodhouseb718cd32014-03-09 13:11:33 -07002306 list_add(&info->link, &domain->devices);
2307 list_add(&info->global, &device_domain_list);
2308 if (dev)
2309 dev->archdata.iommu = info;
2310 spin_unlock_irqrestore(&device_domain_lock, flags);
2311
2312 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002313}
2314
Alex Williamson579305f2014-07-03 09:51:43 -06002315static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2316{
2317 *(u16 *)opaque = alias;
2318 return 0;
2319}
2320
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002321/* domain is initialized */
David Woodhouse146922e2014-03-09 15:44:17 -07002322static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002323{
Alex Williamson579305f2014-07-03 09:51:43 -06002324 struct dmar_domain *domain, *tmp;
2325 struct intel_iommu *iommu;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002326 struct device_domain_info *info;
Alex Williamson579305f2014-07-03 09:51:43 -06002327 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002328 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002329 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002330
David Woodhouse146922e2014-03-09 15:44:17 -07002331 domain = find_domain(dev);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002332 if (domain)
2333 return domain;
2334
David Woodhouse146922e2014-03-09 15:44:17 -07002335 iommu = device_to_iommu(dev, &bus, &devfn);
2336 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002337 return NULL;
2338
2339 if (dev_is_pci(dev)) {
2340 struct pci_dev *pdev = to_pci_dev(dev);
2341
2342 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2343
2344 spin_lock_irqsave(&device_domain_lock, flags);
2345 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2346 PCI_BUS_NUM(dma_alias),
2347 dma_alias & 0xff);
2348 if (info) {
2349 iommu = info->iommu;
2350 domain = info->domain;
2351 }
2352 spin_unlock_irqrestore(&device_domain_lock, flags);
2353
2354 /* DMA alias already has a domain, uses it */
2355 if (info)
2356 goto found_domain;
2357 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002358
David Woodhouse146922e2014-03-09 15:44:17 -07002359 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002360 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002361 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002362 return NULL;
Jiang Liu44bde612014-07-11 14:19:29 +08002363 domain->id = iommu_attach_domain(domain, iommu);
2364 if (domain->id < 0) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07002365 free_domain_mem(domain);
Alex Williamson579305f2014-07-03 09:51:43 -06002366 return NULL;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002367 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002368 domain_attach_iommu(domain, iommu);
Alex Williamson579305f2014-07-03 09:51:43 -06002369 if (domain_init(domain, gaw)) {
2370 domain_exit(domain);
2371 return NULL;
2372 }
2373
2374 /* register PCI DMA alias device */
2375 if (dev_is_pci(dev)) {
2376 tmp = dmar_insert_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2377 dma_alias & 0xff, NULL, domain);
2378
2379 if (!tmp || tmp != domain) {
2380 domain_exit(domain);
2381 domain = tmp;
2382 }
2383
David Woodhouseb718cd32014-03-09 13:11:33 -07002384 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002385 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002386 }
2387
2388found_domain:
Alex Williamson579305f2014-07-03 09:51:43 -06002389 tmp = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
2390
2391 if (!tmp || tmp != domain) {
2392 domain_exit(domain);
2393 domain = tmp;
2394 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002395
2396 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002397}
2398
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002399static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002400#define IDENTMAP_ALL 1
2401#define IDENTMAP_GFX 2
2402#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002403
David Woodhouseb2132032009-06-26 18:50:28 +01002404static int iommu_domain_identity_map(struct dmar_domain *domain,
2405 unsigned long long start,
2406 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002407{
David Woodhousec5395d52009-06-28 16:35:56 +01002408 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2409 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002410
David Woodhousec5395d52009-06-28 16:35:56 +01002411 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2412 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002413 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002414 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002415 }
2416
David Woodhousec5395d52009-06-28 16:35:56 +01002417 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
2418 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002419 /*
2420 * RMRR range might have overlap with physical memory range,
2421 * clear it first
2422 */
David Woodhousec5395d52009-06-28 16:35:56 +01002423 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002424
David Woodhousec5395d52009-06-28 16:35:56 +01002425 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2426 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002427 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002428}
2429
David Woodhouse0b9d9752014-03-09 15:48:15 -07002430static int iommu_prepare_identity_map(struct device *dev,
David Woodhouseb2132032009-06-26 18:50:28 +01002431 unsigned long long start,
2432 unsigned long long end)
2433{
2434 struct dmar_domain *domain;
2435 int ret;
2436
David Woodhouse0b9d9752014-03-09 15:48:15 -07002437 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002438 if (!domain)
2439 return -ENOMEM;
2440
David Woodhouse19943b02009-08-04 16:19:20 +01002441 /* For _hardware_ passthrough, don't bother. But for software
2442 passthrough, we do it anyway -- it may indicate a memory
2443 range which is reserved in E820, so which didn't get set
2444 up to start with in si_domain */
2445 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002446 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2447 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002448 return 0;
2449 }
2450
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002451 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2452 dev_name(dev), start, end);
2453
David Woodhouse5595b522009-12-02 09:21:55 +00002454 if (end < start) {
2455 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2456 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2457 dmi_get_system_info(DMI_BIOS_VENDOR),
2458 dmi_get_system_info(DMI_BIOS_VERSION),
2459 dmi_get_system_info(DMI_PRODUCT_VERSION));
2460 ret = -EIO;
2461 goto error;
2462 }
2463
David Woodhouse2ff729f2009-08-26 14:25:41 +01002464 if (end >> agaw_to_width(domain->agaw)) {
2465 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2466 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2467 agaw_to_width(domain->agaw),
2468 dmi_get_system_info(DMI_BIOS_VENDOR),
2469 dmi_get_system_info(DMI_BIOS_VERSION),
2470 dmi_get_system_info(DMI_PRODUCT_VERSION));
2471 ret = -EIO;
2472 goto error;
2473 }
David Woodhouse19943b02009-08-04 16:19:20 +01002474
David Woodhouseb2132032009-06-26 18:50:28 +01002475 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002476 if (ret)
2477 goto error;
2478
2479 /* context entry init */
David Woodhouse0b9d9752014-03-09 15:48:15 -07002480 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01002481 if (ret)
2482 goto error;
2483
2484 return 0;
2485
2486 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002487 domain_exit(domain);
2488 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002489}
2490
2491static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002492 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002493{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002494 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002495 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002496 return iommu_prepare_identity_map(dev, rmrr->base_address,
2497 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002498}
2499
Suresh Siddhad3f13812011-08-23 17:05:25 -07002500#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002501static inline void iommu_prepare_isa(void)
2502{
2503 struct pci_dev *pdev;
2504 int ret;
2505
2506 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2507 if (!pdev)
2508 return;
2509
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002510 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002511 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002512
2513 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002514 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002515
Yijing Wang9b27e822014-05-20 20:37:52 +08002516 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002517}
2518#else
2519static inline void iommu_prepare_isa(void)
2520{
2521 return;
2522}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002523#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002524
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002525static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002526
Matt Kraai071e1372009-08-23 22:30:22 -07002527static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002528{
2529 struct dmar_drhd_unit *drhd;
2530 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002531 int nid, ret = 0;
Jiang Liu44bde612014-07-11 14:19:29 +08002532 bool first = true;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002533
Jiang Liuab8dfe22014-07-11 14:19:27 +08002534 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002535 if (!si_domain)
2536 return -EFAULT;
2537
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002538 for_each_active_iommu(iommu, drhd) {
2539 ret = iommu_attach_domain(si_domain, iommu);
Jiang Liufb170fb2014-07-11 14:19:28 +08002540 if (ret < 0) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002541 domain_exit(si_domain);
2542 return -EFAULT;
Jiang Liu44bde612014-07-11 14:19:29 +08002543 } else if (first) {
2544 si_domain->id = ret;
2545 first = false;
2546 } else if (si_domain->id != ret) {
2547 domain_exit(si_domain);
2548 return -EFAULT;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002549 }
Jiang Liufb170fb2014-07-11 14:19:28 +08002550 domain_attach_iommu(si_domain, iommu);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002551 }
2552
2553 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2554 domain_exit(si_domain);
2555 return -EFAULT;
2556 }
2557
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002558 pr_debug("Identity mapping domain is domain %d\n",
Jiang Liu9544c002014-01-06 14:18:13 +08002559 si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002560
David Woodhouse19943b02009-08-04 16:19:20 +01002561 if (hw)
2562 return 0;
2563
David Woodhousec7ab48d2009-06-26 19:10:36 +01002564 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002565 unsigned long start_pfn, end_pfn;
2566 int i;
2567
2568 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2569 ret = iommu_domain_identity_map(si_domain,
2570 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2571 if (ret)
2572 return ret;
2573 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002574 }
2575
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002576 return 0;
2577}
2578
David Woodhouse9b226622014-03-09 14:03:28 -07002579static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002580{
2581 struct device_domain_info *info;
2582
2583 if (likely(!iommu_identity_mapping))
2584 return 0;
2585
David Woodhouse9b226622014-03-09 14:03:28 -07002586 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002587 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2588 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002589
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002590 return 0;
2591}
2592
2593static int domain_add_dev_info(struct dmar_domain *domain,
David Woodhouse5913c9b2014-03-09 16:27:31 -07002594 struct device *dev, int translation)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002595{
David Woodhouse0ac72662014-03-09 13:19:22 -07002596 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002597 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002598 u8 bus, devfn;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002599 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002600
David Woodhouse5913c9b2014-03-09 16:27:31 -07002601 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002602 if (!iommu)
2603 return -ENODEV;
2604
David Woodhouse5913c9b2014-03-09 16:27:31 -07002605 ndomain = dmar_insert_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002606 if (ndomain != domain)
2607 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002608
David Woodhouse5913c9b2014-03-09 16:27:31 -07002609 ret = domain_context_mapping(domain, dev, translation);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002610 if (ret) {
David Woodhouse5913c9b2014-03-09 16:27:31 -07002611 domain_remove_one_dev_info(domain, dev);
David Woodhousee2ad23d2012-05-25 17:42:54 +01002612 return ret;
2613 }
2614
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002615 return 0;
2616}
2617
David Woodhouse0b9d9752014-03-09 15:48:15 -07002618static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002619{
2620 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002621 struct device *tmp;
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002622 int i;
2623
Jiang Liu0e242612014-02-19 14:07:34 +08002624 rcu_read_lock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002625 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002626 /*
2627 * Return TRUE if this RMRR contains the device that
2628 * is passed in.
2629 */
2630 for_each_active_dev_scope(rmrr->devices,
2631 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002632 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002633 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002634 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002635 }
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002636 }
Jiang Liu0e242612014-02-19 14:07:34 +08002637 rcu_read_unlock();
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002638 return false;
2639}
2640
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002641/*
2642 * There are a couple cases where we need to restrict the functionality of
2643 * devices associated with RMRRs. The first is when evaluating a device for
2644 * identity mapping because problems exist when devices are moved in and out
2645 * of domains and their respective RMRR information is lost. This means that
2646 * a device with associated RMRRs will never be in a "passthrough" domain.
2647 * The second is use of the device through the IOMMU API. This interface
2648 * expects to have full control of the IOVA space for the device. We cannot
2649 * satisfy both the requirement that RMRR access is maintained and have an
2650 * unencumbered IOVA space. We also have no ability to quiesce the device's
2651 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2652 * We therefore prevent devices associated with an RMRR from participating in
2653 * the IOMMU API, which eliminates them from device assignment.
2654 *
2655 * In both cases we assume that PCI USB devices with RMRRs have them largely
2656 * for historical reasons and that the RMRR space is not actively used post
2657 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002658 *
2659 * The same exception is made for graphics devices, with the requirement that
2660 * any use of the RMRR regions will be torn down before assigning the device
2661 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002662 */
2663static bool device_is_rmrr_locked(struct device *dev)
2664{
2665 if (!device_has_rmrr(dev))
2666 return false;
2667
2668 if (dev_is_pci(dev)) {
2669 struct pci_dev *pdev = to_pci_dev(dev);
2670
David Woodhouse18436af2015-03-25 15:05:47 +00002671 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002672 return false;
2673 }
2674
2675 return true;
2676}
2677
David Woodhouse3bdb2592014-03-09 16:03:08 -07002678static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002679{
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002680
David Woodhouse3bdb2592014-03-09 16:03:08 -07002681 if (dev_is_pci(dev)) {
2682 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f2012-11-20 19:43:17 +00002683
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002684 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002685 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002686
David Woodhouse3bdb2592014-03-09 16:03:08 -07002687 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2688 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002689
David Woodhouse3bdb2592014-03-09 16:03:08 -07002690 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2691 return 1;
2692
2693 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2694 return 0;
2695
2696 /*
2697 * We want to start off with all devices in the 1:1 domain, and
2698 * take them out later if we find they can't access all of memory.
2699 *
2700 * However, we can't do this for PCI devices behind bridges,
2701 * because all PCI devices behind the same bridge will end up
2702 * with the same source-id on their transactions.
2703 *
2704 * Practically speaking, we can't change things around for these
2705 * devices at run-time, because we can't be sure there'll be no
2706 * DMA transactions in flight for any of their siblings.
2707 *
2708 * So PCI devices (unless they're on the root bus) as well as
2709 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2710 * the 1:1 domain, just in _case_ one of their siblings turns out
2711 * not to be able to map all of memory.
2712 */
2713 if (!pci_is_pcie(pdev)) {
2714 if (!pci_is_root_bus(pdev->bus))
2715 return 0;
2716 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2717 return 0;
2718 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2719 return 0;
2720 } else {
2721 if (device_has_rmrr(dev))
2722 return 0;
2723 }
David Woodhouse6941af22009-07-04 18:24:27 +01002724
David Woodhouse3dfc8132009-07-04 19:11:08 +01002725 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002726 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002727 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002728 * take them out of the 1:1 domain later.
2729 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002730 if (!startup) {
2731 /*
2732 * If the device's dma_mask is less than the system's memory
2733 * size then this is not a candidate for identity mapping.
2734 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002735 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002736
David Woodhouse3bdb2592014-03-09 16:03:08 -07002737 if (dev->coherent_dma_mask &&
2738 dev->coherent_dma_mask < dma_mask)
2739 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002740
David Woodhouse3bdb2592014-03-09 16:03:08 -07002741 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002742 }
David Woodhouse6941af22009-07-04 18:24:27 +01002743
2744 return 1;
2745}
2746
David Woodhousecf04eee2014-03-21 16:49:04 +00002747static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2748{
2749 int ret;
2750
2751 if (!iommu_should_identity_map(dev, 1))
2752 return 0;
2753
2754 ret = domain_add_dev_info(si_domain, dev,
2755 hw ? CONTEXT_TT_PASS_THROUGH :
2756 CONTEXT_TT_MULTI_LEVEL);
2757 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002758 pr_info("%s identity mapping for device %s\n",
2759 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002760 else if (ret == -ENODEV)
2761 /* device not associated with an iommu */
2762 ret = 0;
2763
2764 return ret;
2765}
2766
2767
Matt Kraai071e1372009-08-23 22:30:22 -07002768static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002769{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002770 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002771 struct dmar_drhd_unit *drhd;
2772 struct intel_iommu *iommu;
2773 struct device *dev;
2774 int i;
2775 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002776
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002777 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002778 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2779 if (ret)
2780 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002781 }
2782
David Woodhousecf04eee2014-03-21 16:49:04 +00002783 for_each_active_iommu(iommu, drhd)
2784 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2785 struct acpi_device_physical_node *pn;
2786 struct acpi_device *adev;
2787
2788 if (dev->bus != &acpi_bus_type)
2789 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002790
David Woodhousecf04eee2014-03-21 16:49:04 +00002791 adev= to_acpi_device(dev);
2792 mutex_lock(&adev->physical_node_lock);
2793 list_for_each_entry(pn, &adev->physical_node_list, node) {
2794 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2795 if (ret)
2796 break;
2797 }
2798 mutex_unlock(&adev->physical_node_lock);
2799 if (ret)
2800 return ret;
2801 }
2802
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002803 return 0;
2804}
2805
Jiang Liuffebeb42014-11-09 22:48:02 +08002806static void intel_iommu_init_qi(struct intel_iommu *iommu)
2807{
2808 /*
2809 * Start from the sane iommu hardware state.
2810 * If the queued invalidation is already initialized by us
2811 * (for example, while enabling interrupt-remapping) then
2812 * we got the things already rolling from a sane state.
2813 */
2814 if (!iommu->qi) {
2815 /*
2816 * Clear any previous faults.
2817 */
2818 dmar_fault(-1, iommu);
2819 /*
2820 * Disable queued invalidation if supported and already enabled
2821 * before OS handover.
2822 */
2823 dmar_disable_qi(iommu);
2824 }
2825
2826 if (dmar_enable_qi(iommu)) {
2827 /*
2828 * Queued Invalidate not enabled, use Register Based Invalidate
2829 */
2830 iommu->flush.flush_context = __iommu_flush_context;
2831 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002832 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08002833 iommu->name);
2834 } else {
2835 iommu->flush.flush_context = qi_flush_context;
2836 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002837 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08002838 }
2839}
2840
Joerg Roedel091d42e2015-06-12 11:56:10 +02002841static int copy_context_table(struct intel_iommu *iommu,
2842 struct root_entry *old_re,
2843 struct context_entry **tbl,
2844 int bus, bool ext)
2845{
2846 struct context_entry *old_ce = NULL, *new_ce = NULL, ce;
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002847 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002848 phys_addr_t old_ce_phys;
2849
2850 tbl_idx = ext ? bus * 2 : bus;
2851
2852 for (devfn = 0; devfn < 256; devfn++) {
2853 /* First calculate the correct index */
2854 idx = (ext ? devfn * 2 : devfn) % 256;
2855
2856 if (idx == 0) {
2857 /* First save what we may have and clean up */
2858 if (new_ce) {
2859 tbl[tbl_idx] = new_ce;
2860 __iommu_flush_cache(iommu, new_ce,
2861 VTD_PAGE_SIZE);
2862 pos = 1;
2863 }
2864
2865 if (old_ce)
2866 iounmap(old_ce);
2867
2868 ret = 0;
2869 if (devfn < 0x80)
2870 old_ce_phys = root_entry_lctp(old_re);
2871 else
2872 old_ce_phys = root_entry_uctp(old_re);
2873
2874 if (!old_ce_phys) {
2875 if (ext && devfn == 0) {
2876 /* No LCTP, try UCTP */
2877 devfn = 0x7f;
2878 continue;
2879 } else {
2880 goto out;
2881 }
2882 }
2883
2884 ret = -ENOMEM;
2885 old_ce = ioremap_cache(old_ce_phys, PAGE_SIZE);
2886 if (!old_ce)
2887 goto out;
2888
2889 new_ce = alloc_pgtable_page(iommu->node);
2890 if (!new_ce)
2891 goto out_unmap;
2892
2893 ret = 0;
2894 }
2895
2896 /* Now copy the context entry */
2897 ce = old_ce[idx];
2898
Joerg Roedelcf484d02015-06-12 12:21:46 +02002899 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02002900 continue;
2901
Joerg Roedeldbcd8612015-06-12 12:02:09 +02002902 did = context_domain_id(&ce);
2903 if (did >= 0 && did < cap_ndoms(iommu->cap))
2904 set_bit(did, iommu->domain_ids);
2905
Joerg Roedelcf484d02015-06-12 12:21:46 +02002906 /*
2907 * We need a marker for copied context entries. This
2908 * marker needs to work for the old format as well as
2909 * for extended context entries.
2910 *
2911 * Bit 67 of the context entry is used. In the old
2912 * format this bit is available to software, in the
2913 * extended format it is the PGE bit, but PGE is ignored
2914 * by HW if PASIDs are disabled (and thus still
2915 * available).
2916 *
2917 * So disable PASIDs first and then mark the entry
2918 * copied. This means that we don't copy PASID
2919 * translations from the old kernel, but this is fine as
2920 * faults there are not fatal.
2921 */
2922 context_clear_pasid_enable(&ce);
2923 context_set_copied(&ce);
2924
Joerg Roedel091d42e2015-06-12 11:56:10 +02002925 new_ce[idx] = ce;
2926 }
2927
2928 tbl[tbl_idx + pos] = new_ce;
2929
2930 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
2931
2932out_unmap:
2933 iounmap(old_ce);
2934
2935out:
2936 return ret;
2937}
2938
2939static int copy_translation_tables(struct intel_iommu *iommu)
2940{
2941 struct context_entry **ctxt_tbls;
2942 struct root_entry *old_rt;
2943 phys_addr_t old_rt_phys;
2944 int ctxt_table_entries;
2945 unsigned long flags;
2946 u64 rtaddr_reg;
2947 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02002948 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002949
2950 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
2951 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02002952 new_ext = !!ecap_ecs(iommu->ecap);
2953
2954 /*
2955 * The RTT bit can only be changed when translation is disabled,
2956 * but disabling translation means to open a window for data
2957 * corruption. So bail out and don't copy anything if we would
2958 * have to change the bit.
2959 */
2960 if (new_ext != ext)
2961 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02002962
2963 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
2964 if (!old_rt_phys)
2965 return -EINVAL;
2966
2967 old_rt = ioremap_cache(old_rt_phys, PAGE_SIZE);
2968 if (!old_rt)
2969 return -ENOMEM;
2970
2971 /* This is too big for the stack - allocate it from slab */
2972 ctxt_table_entries = ext ? 512 : 256;
2973 ret = -ENOMEM;
2974 ctxt_tbls = kzalloc(ctxt_table_entries * sizeof(void *), GFP_KERNEL);
2975 if (!ctxt_tbls)
2976 goto out_unmap;
2977
2978 for (bus = 0; bus < 256; bus++) {
2979 ret = copy_context_table(iommu, &old_rt[bus],
2980 ctxt_tbls, bus, ext);
2981 if (ret) {
2982 pr_err("%s: Failed to copy context table for bus %d\n",
2983 iommu->name, bus);
2984 continue;
2985 }
2986 }
2987
2988 spin_lock_irqsave(&iommu->lock, flags);
2989
2990 /* Context tables are copied, now write them to the root_entry table */
2991 for (bus = 0; bus < 256; bus++) {
2992 int idx = ext ? bus * 2 : bus;
2993 u64 val;
2994
2995 if (ctxt_tbls[idx]) {
2996 val = virt_to_phys(ctxt_tbls[idx]) | 1;
2997 iommu->root_entry[bus].lo = val;
2998 }
2999
3000 if (!ext || !ctxt_tbls[idx + 1])
3001 continue;
3002
3003 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3004 iommu->root_entry[bus].hi = val;
3005 }
3006
3007 spin_unlock_irqrestore(&iommu->lock, flags);
3008
3009 kfree(ctxt_tbls);
3010
3011 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3012
3013 ret = 0;
3014
3015out_unmap:
3016 iounmap(old_rt);
3017
3018 return ret;
3019}
3020
Joseph Cihulab7792602011-05-03 00:08:37 -07003021static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003022{
3023 struct dmar_drhd_unit *drhd;
3024 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003025 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003026 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003027 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003028 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003029
3030 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003031 * for each drhd
3032 * allocate root
3033 * initialize and program root entry to not present
3034 * endfor
3035 */
3036 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003037 /*
3038 * lock not needed as this is only incremented in the single
3039 * threaded kernel __init code path all other access are read
3040 * only
3041 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003042 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003043 g_num_of_iommus++;
3044 continue;
3045 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003046 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003047 }
3048
Jiang Liuffebeb42014-11-09 22:48:02 +08003049 /* Preallocate enough resources for IOMMU hot-addition */
3050 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3051 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3052
Weidong Hand9630fe2008-12-08 11:06:32 +08003053 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3054 GFP_KERNEL);
3055 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003056 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003057 ret = -ENOMEM;
3058 goto error;
3059 }
3060
mark gross80b20dd2008-04-18 13:53:58 -07003061 deferred_flush = kzalloc(g_num_of_iommus *
3062 sizeof(struct deferred_flush_tables), GFP_KERNEL);
3063 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08003064 ret = -ENOMEM;
Jiang Liu989d51f2014-02-19 14:07:21 +08003065 goto free_g_iommus;
mark gross5e0d2a62008-03-04 15:22:08 -08003066 }
3067
Jiang Liu7c919772014-01-06 14:18:18 +08003068 for_each_active_iommu(iommu, drhd) {
Weidong Hand9630fe2008-12-08 11:06:32 +08003069 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003070
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003071 intel_iommu_init_qi(iommu);
3072
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003073 ret = iommu_init_domains(iommu);
3074 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003075 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003076
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003077 init_translation_status(iommu);
3078
Joerg Roedel091d42e2015-06-12 11:56:10 +02003079 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3080 iommu_disable_translation(iommu);
3081 clear_translation_pre_enabled(iommu);
3082 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3083 iommu->name);
3084 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003085
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003086 /*
3087 * TBD:
3088 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003089 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003090 */
3091 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003092 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003093 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003094
Joerg Roedel091d42e2015-06-12 11:56:10 +02003095 if (translation_pre_enabled(iommu)) {
3096 pr_info("Translation already enabled - trying to copy translation structures\n");
3097
3098 ret = copy_translation_tables(iommu);
3099 if (ret) {
3100 /*
3101 * We found the IOMMU with translation
3102 * enabled - but failed to copy over the
3103 * old root-entry table. Try to proceed
3104 * by disabling translation now and
3105 * allocating a clean root-entry table.
3106 * This might cause DMAR faults, but
3107 * probably the dump will still succeed.
3108 */
3109 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3110 iommu->name);
3111 iommu_disable_translation(iommu);
3112 clear_translation_pre_enabled(iommu);
3113 } else {
3114 pr_info("Copied translation tables from previous kernel for %s\n",
3115 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003116 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003117 }
3118 }
3119
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003120 iommu_flush_write_buffer(iommu);
3121 iommu_set_root_entry(iommu);
3122 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3123 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3124
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003125 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003126 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003127 }
3128
David Woodhouse19943b02009-08-04 16:19:20 +01003129 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003130 iommu_identity_mapping |= IDENTMAP_ALL;
3131
Suresh Siddhad3f13812011-08-23 17:05:25 -07003132#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003133 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003134#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003135
Joerg Roedel86080cc2015-06-12 12:27:16 +02003136 if (iommu_identity_mapping) {
3137 ret = si_domain_init(hw_pass_through);
3138 if (ret)
3139 goto free_iommu;
3140 }
3141
David Woodhousee0fc7e02009-09-30 09:12:17 -07003142 check_tylersburg_isoch();
3143
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003144 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003145 * If we copied translations from a previous kernel in the kdump
3146 * case, we can not assign the devices to domains now, as that
3147 * would eliminate the old mappings. So skip this part and defer
3148 * the assignment to device driver initialization time.
3149 */
3150 if (copied_tables)
3151 goto domains_done;
3152
3153 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003154 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003155 * identity mappings for rmrr, gfx, and isa and may fall back to static
3156 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003157 */
David Woodhouse19943b02009-08-04 16:19:20 +01003158 if (iommu_identity_mapping) {
3159 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3160 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003161 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003162 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003163 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003164 }
David Woodhouse19943b02009-08-04 16:19:20 +01003165 /*
3166 * For each rmrr
3167 * for each dev attached to rmrr
3168 * do
3169 * locate drhd for dev, alloc domain for dev
3170 * allocate free domain
3171 * allocate page table entries for rmrr
3172 * if context not allocated for bus
3173 * allocate and init context
3174 * set present in root table for this bus
3175 * init context with domain, translation etc
3176 * endfor
3177 * endfor
3178 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003179 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003180 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003181 /* some BIOS lists non-exist devices in DMAR table. */
3182 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003183 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003184 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003185 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003186 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003187 }
3188 }
3189
3190 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003191
Joerg Roedela87f4912015-06-12 12:32:54 +02003192domains_done:
3193
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003194 /*
3195 * for each drhd
3196 * enable fault log
3197 * global invalidate context cache
3198 * global invalidate iotlb
3199 * enable translation
3200 */
Jiang Liu7c919772014-01-06 14:18:18 +08003201 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003202 if (drhd->ignored) {
3203 /*
3204 * we always have to disable PMRs or DMA may fail on
3205 * this device
3206 */
3207 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003208 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003209 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003210 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003211
3212 iommu_flush_write_buffer(iommu);
3213
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003214 ret = dmar_set_interrupt(iommu);
3215 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003216 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003217
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003218 if (!translation_pre_enabled(iommu))
3219 iommu_enable_translation(iommu);
3220
David Woodhouseb94996c2009-09-19 15:28:12 -07003221 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003222 }
3223
3224 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003225
3226free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003227 for_each_active_iommu(iommu, drhd) {
3228 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003229 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003230 }
Jiang Liu9bdc5312014-01-06 14:18:27 +08003231 kfree(deferred_flush);
Jiang Liu989d51f2014-02-19 14:07:21 +08003232free_g_iommus:
Weidong Hand9630fe2008-12-08 11:06:32 +08003233 kfree(g_iommus);
Jiang Liu989d51f2014-02-19 14:07:21 +08003234error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003235 return ret;
3236}
3237
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003238/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01003239static struct iova *intel_alloc_iova(struct device *dev,
3240 struct dmar_domain *domain,
3241 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003242{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003243 struct iova *iova = NULL;
3244
David Woodhouse875764d2009-06-28 21:20:51 +01003245 /* Restrict dma_mask to the width that the iommu can handle */
3246 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
3247
3248 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003249 /*
3250 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003251 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003252 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003253 */
David Woodhouse875764d2009-06-28 21:20:51 +01003254 iova = alloc_iova(&domain->iovad, nrpages,
3255 IOVA_PFN(DMA_BIT_MASK(32)), 1);
3256 if (iova)
3257 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003258 }
David Woodhouse875764d2009-06-28 21:20:51 +01003259 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
3260 if (unlikely(!iova)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003261 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003262 nrpages, dev_name(dev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003263 return NULL;
3264 }
3265
3266 return iova;
3267}
3268
David Woodhoused4b709f2014-03-09 16:07:40 -07003269static struct dmar_domain *__get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003270{
3271 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003272 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003273
David Woodhoused4b709f2014-03-09 16:07:40 -07003274 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003275 if (!domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003276 pr_err("Allocating domain for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003277 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003278 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003279 }
3280
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003281 /* make sure context mapping is ok */
David Woodhoused4b709f2014-03-09 16:07:40 -07003282 if (unlikely(!domain_context_mapped(dev))) {
3283 ret = domain_context_mapping(domain, dev, CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003284 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003285 pr_err("Domain context map for %s failed\n",
David Woodhoused4b709f2014-03-09 16:07:40 -07003286 dev_name(dev));
Al Viro4fe05bb2007-10-29 04:51:16 +00003287 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003288 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003289 }
3290
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003291 return domain;
3292}
3293
David Woodhoused4b709f2014-03-09 16:07:40 -07003294static inline struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
David Woodhouse147202a2009-07-07 19:43:20 +01003295{
3296 struct device_domain_info *info;
3297
3298 /* No lock here, assumes no domain exit in normal case */
David Woodhoused4b709f2014-03-09 16:07:40 -07003299 info = dev->archdata.iommu;
David Woodhouse147202a2009-07-07 19:43:20 +01003300 if (likely(info))
3301 return info->domain;
3302
3303 return __get_valid_domain_for_dev(dev);
3304}
3305
David Woodhouseecb509e2014-03-09 16:29:55 -07003306/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003307static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003308{
3309 int found;
3310
David Woodhouse3d891942014-03-06 15:59:26 +00003311 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003312 return 1;
3313
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003314 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003315 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003316
David Woodhouse9b226622014-03-09 14:03:28 -07003317 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003318 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003319 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003320 return 1;
3321 else {
3322 /*
3323 * 32 bit DMA is removed from si_domain and fall back
3324 * to non-identity mapping.
3325 */
David Woodhousebf9c9ed2014-03-09 16:19:13 -07003326 domain_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003327 pr_info("32bit %s uses non-identity mapping\n",
3328 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003329 return 0;
3330 }
3331 } else {
3332 /*
3333 * In case of a detached 64 bit DMA device from vm, the device
3334 * is put into si_domain for identity mapping.
3335 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003336 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003337 int ret;
David Woodhouse5913c9b2014-03-09 16:27:31 -07003338 ret = domain_add_dev_info(si_domain, dev,
David Woodhouse5fe60f42009-08-09 10:53:41 +01003339 hw_pass_through ?
3340 CONTEXT_TT_PASS_THROUGH :
3341 CONTEXT_TT_MULTI_LEVEL);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003342 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003343 pr_info("64bit %s uses identity mapping\n",
3344 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003345 return 1;
3346 }
3347 }
3348 }
3349
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003350 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003351}
3352
David Woodhouse5040a912014-03-09 16:14:00 -07003353static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003354 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003355{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003356 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003357 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003358 struct iova *iova;
3359 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003360 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003361 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003362 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003363
3364 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003365
David Woodhouse5040a912014-03-09 16:14:00 -07003366 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003367 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003368
David Woodhouse5040a912014-03-09 16:14:00 -07003369 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003370 if (!domain)
3371 return 0;
3372
Weidong Han8c11e792008-12-08 15:29:22 +08003373 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003374 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003375
David Woodhouse5040a912014-03-09 16:14:00 -07003376 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003377 if (!iova)
3378 goto error;
3379
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003380 /*
3381 * Check if DMAR supports zero-length reads on write only
3382 * mappings..
3383 */
3384 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003385 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003386 prot |= DMA_PTE_READ;
3387 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3388 prot |= DMA_PTE_WRITE;
3389 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003390 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003391 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003392 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003393 * is not a big problem
3394 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01003395 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003396 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003397 if (ret)
3398 goto error;
3399
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003400 /* it's a non-present to present mapping. Only flush if caching mode */
3401 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003402 iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003403 else
Weidong Han8c11e792008-12-08 15:29:22 +08003404 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003405
David Woodhouse03d6a242009-06-28 15:33:46 +01003406 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
3407 start_paddr += paddr & ~PAGE_MASK;
3408 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003409
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003410error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003411 if (iova)
3412 __free_iova(&domain->iovad, iova);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003413 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003414 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003415 return 0;
3416}
3417
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003418static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3419 unsigned long offset, size_t size,
3420 enum dma_data_direction dir,
3421 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003422{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003423 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003424 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003425}
3426
mark gross5e0d2a62008-03-04 15:22:08 -08003427static void flush_unmaps(void)
3428{
mark gross80b20dd2008-04-18 13:53:58 -07003429 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08003430
mark gross5e0d2a62008-03-04 15:22:08 -08003431 timer_on = 0;
3432
3433 /* just flush them all */
3434 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08003435 struct intel_iommu *iommu = g_iommus[i];
3436 if (!iommu)
3437 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003438
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003439 if (!deferred_flush[i].next)
3440 continue;
3441
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003442 /* In caching mode, global flushes turn emulation expensive */
3443 if (!cap_caching_mode(iommu->cap))
3444 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08003445 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003446 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08003447 unsigned long mask;
3448 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003449 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08003450
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003451 /* On real hardware multiple invalidations are expensive */
3452 if (cap_caching_mode(iommu->cap))
3453 iommu_flush_iotlb_psi(iommu, domain->id,
Jiang Liua156ef92014-07-11 14:19:36 +08003454 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00003455 !deferred_flush[i].freelist[j], 0);
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003456 else {
Jiang Liua156ef92014-07-11 14:19:36 +08003457 mask = ilog2(mm_to_dma_pfn(iova_size(iova)));
Nadav Amit78d5f0f2010-04-08 23:00:41 +03003458 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
3459 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
3460 }
Yu Zhao93a23a72009-05-18 13:51:37 +08003461 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003462 if (deferred_flush[i].freelist[j])
3463 dma_free_pagelist(deferred_flush[i].freelist[j]);
mark gross80b20dd2008-04-18 13:53:58 -07003464 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08003465 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003466 }
3467
mark gross5e0d2a62008-03-04 15:22:08 -08003468 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08003469}
3470
3471static void flush_unmaps_timeout(unsigned long data)
3472{
mark gross80b20dd2008-04-18 13:53:58 -07003473 unsigned long flags;
3474
3475 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003476 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07003477 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08003478}
3479
David Woodhouseea8ea462014-03-05 17:09:32 +00003480static void add_unmap(struct dmar_domain *dom, struct iova *iova, struct page *freelist)
mark gross5e0d2a62008-03-04 15:22:08 -08003481{
3482 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07003483 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08003484 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08003485
3486 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07003487 if (list_size == HIGH_WATER_MARK)
3488 flush_unmaps();
3489
Weidong Han8c11e792008-12-08 15:29:22 +08003490 iommu = domain_get_iommu(dom);
3491 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07003492
mark gross80b20dd2008-04-18 13:53:58 -07003493 next = deferred_flush[iommu_id].next;
3494 deferred_flush[iommu_id].domain[next] = dom;
3495 deferred_flush[iommu_id].iova[next] = iova;
David Woodhouseea8ea462014-03-05 17:09:32 +00003496 deferred_flush[iommu_id].freelist[next] = freelist;
mark gross80b20dd2008-04-18 13:53:58 -07003497 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08003498
3499 if (!timer_on) {
3500 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
3501 timer_on = 1;
3502 }
3503 list_size++;
3504 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
3505}
3506
Jiang Liud41a4ad2014-07-11 14:19:34 +08003507static void intel_unmap(struct device *dev, dma_addr_t dev_addr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003508{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003509 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003510 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003511 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08003512 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003513 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003514
David Woodhouse73676832009-07-04 14:08:36 +01003515 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003516 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003517
David Woodhouse1525a292014-03-06 16:19:30 +00003518 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003519 BUG_ON(!domain);
3520
Weidong Han8c11e792008-12-08 15:29:22 +08003521 iommu = domain_get_iommu(domain);
3522
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003523 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01003524 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
3525 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003526 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003527
David Woodhoused794dc92009-06-28 00:27:49 +01003528 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
3529 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003530
David Woodhoused794dc92009-06-28 00:27:49 +01003531 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003532 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003533
David Woodhouseea8ea462014-03-05 17:09:32 +00003534 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003535
mark gross5e0d2a62008-03-04 15:22:08 -08003536 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01003537 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhouseea8ea462014-03-05 17:09:32 +00003538 last_pfn - start_pfn + 1, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003539 /* free iova */
3540 __free_iova(&domain->iovad, iova);
David Woodhouseea8ea462014-03-05 17:09:32 +00003541 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003542 } else {
David Woodhouseea8ea462014-03-05 17:09:32 +00003543 add_unmap(domain, iova, freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003544 /*
3545 * queue up the release of the unmap to save the 1/6th of the
3546 * cpu used up by the iotlb flush operation...
3547 */
mark gross5e0d2a62008-03-04 15:22:08 -08003548 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003549}
3550
Jiang Liud41a4ad2014-07-11 14:19:34 +08003551static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3552 size_t size, enum dma_data_direction dir,
3553 struct dma_attrs *attrs)
3554{
3555 intel_unmap(dev, dev_addr);
3556}
3557
David Woodhouse5040a912014-03-09 16:14:00 -07003558static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003559 dma_addr_t *dma_handle, gfp_t flags,
3560 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003561{
Akinobu Mita36746432014-06-04 16:06:51 -07003562 struct page *page = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003563 int order;
3564
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003565 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003566 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003567
David Woodhouse5040a912014-03-09 16:14:00 -07003568 if (!iommu_no_mapping(dev))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003569 flags &= ~(GFP_DMA | GFP_DMA32);
David Woodhouse5040a912014-03-09 16:14:00 -07003570 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3571 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
Alex Williamsone8bb9102009-11-04 15:59:34 -07003572 flags |= GFP_DMA;
3573 else
3574 flags |= GFP_DMA32;
3575 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003576
Akinobu Mita36746432014-06-04 16:06:51 -07003577 if (flags & __GFP_WAIT) {
3578 unsigned int count = size >> PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003579
Akinobu Mita36746432014-06-04 16:06:51 -07003580 page = dma_alloc_from_contiguous(dev, count, order);
3581 if (page && iommu_no_mapping(dev) &&
3582 page_to_phys(page) + size > dev->coherent_dma_mask) {
3583 dma_release_from_contiguous(dev, page, count);
3584 page = NULL;
3585 }
3586 }
3587
3588 if (!page)
3589 page = alloc_pages(flags, order);
3590 if (!page)
3591 return NULL;
3592 memset(page_address(page), 0, size);
3593
3594 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003595 DMA_BIDIRECTIONAL,
David Woodhouse5040a912014-03-09 16:14:00 -07003596 dev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003597 if (*dma_handle)
Akinobu Mita36746432014-06-04 16:06:51 -07003598 return page_address(page);
3599 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3600 __free_pages(page, order);
3601
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003602 return NULL;
3603}
3604
David Woodhouse5040a912014-03-09 16:14:00 -07003605static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003606 dma_addr_t dma_handle, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003607{
3608 int order;
Akinobu Mita36746432014-06-04 16:06:51 -07003609 struct page *page = virt_to_page(vaddr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003610
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003611 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003612 order = get_order(size);
3613
Jiang Liud41a4ad2014-07-11 14:19:34 +08003614 intel_unmap(dev, dma_handle);
Akinobu Mita36746432014-06-04 16:06:51 -07003615 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3616 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003617}
3618
David Woodhouse5040a912014-03-09 16:14:00 -07003619static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003620 int nelems, enum dma_data_direction dir,
3621 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003622{
Jiang Liud41a4ad2014-07-11 14:19:34 +08003623 intel_unmap(dev, sglist[0].dma_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003624}
3625
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003626static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003627 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003628{
3629 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003630 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003631
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003632 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003633 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003634 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003635 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003636 }
3637 return nelems;
3638}
3639
David Woodhouse5040a912014-03-09 16:14:00 -07003640static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003641 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003642{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003643 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003644 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003645 size_t size = 0;
3646 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003647 struct iova *iova = NULL;
3648 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003649 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003650 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003651 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003652
3653 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003654 if (iommu_no_mapping(dev))
3655 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003656
David Woodhouse5040a912014-03-09 16:14:00 -07003657 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003658 if (!domain)
3659 return 0;
3660
Weidong Han8c11e792008-12-08 15:29:22 +08003661 iommu = domain_get_iommu(domain);
3662
David Woodhouseb536d242009-06-28 14:49:31 +01003663 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003664 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003665
David Woodhouse5040a912014-03-09 16:14:00 -07003666 iova = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
3667 *dev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003668 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003669 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003670 return 0;
3671 }
3672
3673 /*
3674 * Check if DMAR supports zero-length reads on write only
3675 * mappings..
3676 */
3677 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003678 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003679 prot |= DMA_PTE_READ;
3680 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3681 prot |= DMA_PTE_WRITE;
3682
David Woodhouseb536d242009-06-28 14:49:31 +01003683 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003684
Fenghua Yuf5329592009-08-04 15:09:37 -07003685 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003686 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003687 dma_pte_free_pagetable(domain, start_vpfn,
3688 start_vpfn + size - 1);
David Woodhousee1605492009-06-29 11:17:38 +01003689 __free_iova(&domain->iovad, iova);
3690 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003691 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003692
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003693 /* it's a non-present to present mapping. Only flush if caching mode */
3694 if (cap_caching_mode(iommu->cap))
David Woodhouseea8ea462014-03-05 17:09:32 +00003695 iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 0, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003696 else
Weidong Han8c11e792008-12-08 15:29:22 +08003697 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003698
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003699 return nelems;
3700}
3701
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003702static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3703{
3704 return !dma_addr;
3705}
3706
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003707struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003708 .alloc = intel_alloc_coherent,
3709 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003710 .map_sg = intel_map_sg,
3711 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003712 .map_page = intel_map_page,
3713 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003714 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003715};
3716
3717static inline int iommu_domain_cache_init(void)
3718{
3719 int ret = 0;
3720
3721 iommu_domain_cache = kmem_cache_create("iommu_domain",
3722 sizeof(struct dmar_domain),
3723 0,
3724 SLAB_HWCACHE_ALIGN,
3725
3726 NULL);
3727 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003728 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003729 ret = -ENOMEM;
3730 }
3731
3732 return ret;
3733}
3734
3735static inline int iommu_devinfo_cache_init(void)
3736{
3737 int ret = 0;
3738
3739 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3740 sizeof(struct device_domain_info),
3741 0,
3742 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003743 NULL);
3744 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003745 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003746 ret = -ENOMEM;
3747 }
3748
3749 return ret;
3750}
3751
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003752static int __init iommu_init_mempool(void)
3753{
3754 int ret;
3755 ret = iommu_iova_cache_init();
3756 if (ret)
3757 return ret;
3758
3759 ret = iommu_domain_cache_init();
3760 if (ret)
3761 goto domain_error;
3762
3763 ret = iommu_devinfo_cache_init();
3764 if (!ret)
3765 return ret;
3766
3767 kmem_cache_destroy(iommu_domain_cache);
3768domain_error:
Robin Murphy85b45452015-01-12 17:51:14 +00003769 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003770
3771 return -ENOMEM;
3772}
3773
3774static void __init iommu_exit_mempool(void)
3775{
3776 kmem_cache_destroy(iommu_devinfo_cache);
3777 kmem_cache_destroy(iommu_domain_cache);
Robin Murphy85b45452015-01-12 17:51:14 +00003778 iommu_iova_cache_destroy();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003779}
3780
Dan Williams556ab452010-07-23 15:47:56 -07003781static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3782{
3783 struct dmar_drhd_unit *drhd;
3784 u32 vtbar;
3785 int rc;
3786
3787 /* We know that this device on this chipset has its own IOMMU.
3788 * If we find it under a different IOMMU, then the BIOS is lying
3789 * to us. Hope that the IOMMU for this device is actually
3790 * disabled, and it needs no translation...
3791 */
3792 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3793 if (rc) {
3794 /* "can't" happen */
3795 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3796 return;
3797 }
3798 vtbar &= 0xffff0000;
3799
3800 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3801 drhd = dmar_find_matched_drhd_unit(pdev);
3802 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3803 TAINT_FIRMWARE_WORKAROUND,
3804 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3805 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3806}
3807DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3808
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003809static void __init init_no_remapping_devices(void)
3810{
3811 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003812 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003813 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003814
3815 for_each_drhd_unit(drhd) {
3816 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003817 for_each_active_dev_scope(drhd->devices,
3818 drhd->devices_cnt, i, dev)
3819 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003820 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003821 if (i == drhd->devices_cnt)
3822 drhd->ignored = 1;
3823 }
3824 }
3825
Jiang Liu7c919772014-01-06 14:18:18 +08003826 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003827 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003828 continue;
3829
Jiang Liub683b232014-02-19 14:07:32 +08003830 for_each_active_dev_scope(drhd->devices,
3831 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003832 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003833 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003834 if (i < drhd->devices_cnt)
3835 continue;
3836
David Woodhousec0771df2011-10-14 20:59:46 +01003837 /* This IOMMU has *only* gfx devices. Either bypass it or
3838 set the gfx_mapped flag, as appropriate */
3839 if (dmar_map_gfx) {
3840 intel_iommu_gfx_mapped = 1;
3841 } else {
3842 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003843 for_each_active_dev_scope(drhd->devices,
3844 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003845 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003846 }
3847 }
3848}
3849
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003850#ifdef CONFIG_SUSPEND
3851static int init_iommu_hw(void)
3852{
3853 struct dmar_drhd_unit *drhd;
3854 struct intel_iommu *iommu = NULL;
3855
3856 for_each_active_iommu(iommu, drhd)
3857 if (iommu->qi)
3858 dmar_reenable_qi(iommu);
3859
Joseph Cihulab7792602011-05-03 00:08:37 -07003860 for_each_iommu(iommu, drhd) {
3861 if (drhd->ignored) {
3862 /*
3863 * we always have to disable PMRs or DMA may fail on
3864 * this device
3865 */
3866 if (force_on)
3867 iommu_disable_protect_mem_regions(iommu);
3868 continue;
3869 }
3870
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003871 iommu_flush_write_buffer(iommu);
3872
3873 iommu_set_root_entry(iommu);
3874
3875 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003876 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08003877 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3878 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07003879 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003880 }
3881
3882 return 0;
3883}
3884
3885static void iommu_flush_all(void)
3886{
3887 struct dmar_drhd_unit *drhd;
3888 struct intel_iommu *iommu;
3889
3890 for_each_active_iommu(iommu, drhd) {
3891 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003892 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003893 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003894 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003895 }
3896}
3897
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003898static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003899{
3900 struct dmar_drhd_unit *drhd;
3901 struct intel_iommu *iommu = NULL;
3902 unsigned long flag;
3903
3904 for_each_active_iommu(iommu, drhd) {
3905 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3906 GFP_ATOMIC);
3907 if (!iommu->iommu_state)
3908 goto nomem;
3909 }
3910
3911 iommu_flush_all();
3912
3913 for_each_active_iommu(iommu, drhd) {
3914 iommu_disable_translation(iommu);
3915
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003916 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003917
3918 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3919 readl(iommu->reg + DMAR_FECTL_REG);
3920 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3921 readl(iommu->reg + DMAR_FEDATA_REG);
3922 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3923 readl(iommu->reg + DMAR_FEADDR_REG);
3924 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3925 readl(iommu->reg + DMAR_FEUADDR_REG);
3926
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003927 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003928 }
3929 return 0;
3930
3931nomem:
3932 for_each_active_iommu(iommu, drhd)
3933 kfree(iommu->iommu_state);
3934
3935 return -ENOMEM;
3936}
3937
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003938static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003939{
3940 struct dmar_drhd_unit *drhd;
3941 struct intel_iommu *iommu = NULL;
3942 unsigned long flag;
3943
3944 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003945 if (force_on)
3946 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3947 else
3948 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003949 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003950 }
3951
3952 for_each_active_iommu(iommu, drhd) {
3953
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003954 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003955
3956 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3957 iommu->reg + DMAR_FECTL_REG);
3958 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3959 iommu->reg + DMAR_FEDATA_REG);
3960 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3961 iommu->reg + DMAR_FEADDR_REG);
3962 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3963 iommu->reg + DMAR_FEUADDR_REG);
3964
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02003965 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003966 }
3967
3968 for_each_active_iommu(iommu, drhd)
3969 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003970}
3971
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003972static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003973 .resume = iommu_resume,
3974 .suspend = iommu_suspend,
3975};
3976
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003977static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003978{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003979 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003980}
3981
3982#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02003983static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003984#endif /* CONFIG_PM */
3985
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003986
Jiang Liuc2a0b532014-11-09 22:47:56 +08003987int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07003988{
3989 struct acpi_dmar_reserved_memory *rmrr;
3990 struct dmar_rmrr_unit *rmrru;
3991
3992 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
3993 if (!rmrru)
3994 return -ENOMEM;
3995
3996 rmrru->hdr = header;
3997 rmrr = (struct acpi_dmar_reserved_memory *)header;
3998 rmrru->base_address = rmrr->base_address;
3999 rmrru->end_address = rmrr->end_address;
Jiang Liu2e455282014-02-19 14:07:36 +08004000 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4001 ((void *)rmrr) + rmrr->header.length,
4002 &rmrru->devices_cnt);
4003 if (rmrru->devices_cnt && rmrru->devices == NULL) {
4004 kfree(rmrru);
4005 return -ENOMEM;
4006 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004007
Jiang Liu2e455282014-02-19 14:07:36 +08004008 list_add(&rmrru->list, &dmar_rmrr_units);
4009
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004010 return 0;
4011}
4012
Jiang Liu6b197242014-11-09 22:47:58 +08004013static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4014{
4015 struct dmar_atsr_unit *atsru;
4016 struct acpi_dmar_atsr *tmp;
4017
4018 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4019 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4020 if (atsr->segment != tmp->segment)
4021 continue;
4022 if (atsr->header.length != tmp->header.length)
4023 continue;
4024 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4025 return atsru;
4026 }
4027
4028 return NULL;
4029}
4030
4031int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004032{
4033 struct acpi_dmar_atsr *atsr;
4034 struct dmar_atsr_unit *atsru;
4035
Jiang Liu6b197242014-11-09 22:47:58 +08004036 if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
4037 return 0;
4038
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004039 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004040 atsru = dmar_find_atsr(atsr);
4041 if (atsru)
4042 return 0;
4043
4044 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004045 if (!atsru)
4046 return -ENOMEM;
4047
Jiang Liu6b197242014-11-09 22:47:58 +08004048 /*
4049 * If memory is allocated from slab by ACPI _DSM method, we need to
4050 * copy the memory content because the memory buffer will be freed
4051 * on return.
4052 */
4053 atsru->hdr = (void *)(atsru + 1);
4054 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004055 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004056 if (!atsru->include_all) {
4057 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4058 (void *)atsr + atsr->header.length,
4059 &atsru->devices_cnt);
4060 if (atsru->devices_cnt && atsru->devices == NULL) {
4061 kfree(atsru);
4062 return -ENOMEM;
4063 }
4064 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004065
Jiang Liu0e242612014-02-19 14:07:34 +08004066 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004067
4068 return 0;
4069}
4070
Jiang Liu9bdc5312014-01-06 14:18:27 +08004071static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4072{
4073 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4074 kfree(atsru);
4075}
4076
Jiang Liu6b197242014-11-09 22:47:58 +08004077int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4078{
4079 struct acpi_dmar_atsr *atsr;
4080 struct dmar_atsr_unit *atsru;
4081
4082 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4083 atsru = dmar_find_atsr(atsr);
4084 if (atsru) {
4085 list_del_rcu(&atsru->list);
4086 synchronize_rcu();
4087 intel_iommu_free_atsr(atsru);
4088 }
4089
4090 return 0;
4091}
4092
4093int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4094{
4095 int i;
4096 struct device *dev;
4097 struct acpi_dmar_atsr *atsr;
4098 struct dmar_atsr_unit *atsru;
4099
4100 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4101 atsru = dmar_find_atsr(atsr);
4102 if (!atsru)
4103 return 0;
4104
4105 if (!atsru->include_all && atsru->devices && atsru->devices_cnt)
4106 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4107 i, dev)
4108 return -EBUSY;
4109
4110 return 0;
4111}
4112
Jiang Liuffebeb42014-11-09 22:48:02 +08004113static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4114{
4115 int sp, ret = 0;
4116 struct intel_iommu *iommu = dmaru->iommu;
4117
4118 if (g_iommus[iommu->seq_id])
4119 return 0;
4120
4121 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004122 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004123 iommu->name);
4124 return -ENXIO;
4125 }
4126 if (!ecap_sc_support(iommu->ecap) &&
4127 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004128 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004129 iommu->name);
4130 return -ENXIO;
4131 }
4132 sp = domain_update_iommu_superpage(iommu) - 1;
4133 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004134 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004135 iommu->name);
4136 return -ENXIO;
4137 }
4138
4139 /*
4140 * Disable translation if already enabled prior to OS handover.
4141 */
4142 if (iommu->gcmd & DMA_GCMD_TE)
4143 iommu_disable_translation(iommu);
4144
4145 g_iommus[iommu->seq_id] = iommu;
4146 ret = iommu_init_domains(iommu);
4147 if (ret == 0)
4148 ret = iommu_alloc_root_entry(iommu);
4149 if (ret)
4150 goto out;
4151
4152 if (dmaru->ignored) {
4153 /*
4154 * we always have to disable PMRs or DMA may fail on this device
4155 */
4156 if (force_on)
4157 iommu_disable_protect_mem_regions(iommu);
4158 return 0;
4159 }
4160
4161 intel_iommu_init_qi(iommu);
4162 iommu_flush_write_buffer(iommu);
4163 ret = dmar_set_interrupt(iommu);
4164 if (ret)
4165 goto disable_iommu;
4166
4167 iommu_set_root_entry(iommu);
4168 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4169 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4170 iommu_enable_translation(iommu);
4171
4172 if (si_domain) {
4173 ret = iommu_attach_domain(si_domain, iommu);
4174 if (ret < 0 || si_domain->id != ret)
4175 goto disable_iommu;
4176 domain_attach_iommu(si_domain, iommu);
4177 }
4178
4179 iommu_disable_protect_mem_regions(iommu);
4180 return 0;
4181
4182disable_iommu:
4183 disable_dmar_iommu(iommu);
4184out:
4185 free_dmar_iommu(iommu);
4186 return ret;
4187}
4188
Jiang Liu6b197242014-11-09 22:47:58 +08004189int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4190{
Jiang Liuffebeb42014-11-09 22:48:02 +08004191 int ret = 0;
4192 struct intel_iommu *iommu = dmaru->iommu;
4193
4194 if (!intel_iommu_enabled)
4195 return 0;
4196 if (iommu == NULL)
4197 return -EINVAL;
4198
4199 if (insert) {
4200 ret = intel_iommu_add(dmaru);
4201 } else {
4202 disable_dmar_iommu(iommu);
4203 free_dmar_iommu(iommu);
4204 }
4205
4206 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004207}
4208
Jiang Liu9bdc5312014-01-06 14:18:27 +08004209static void intel_iommu_free_dmars(void)
4210{
4211 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4212 struct dmar_atsr_unit *atsru, *atsr_n;
4213
4214 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4215 list_del(&rmrru->list);
4216 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
4217 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004218 }
4219
Jiang Liu9bdc5312014-01-06 14:18:27 +08004220 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4221 list_del(&atsru->list);
4222 intel_iommu_free_atsr(atsru);
4223 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004224}
4225
4226int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4227{
Jiang Liub683b232014-02-19 14:07:32 +08004228 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004229 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004230 struct pci_dev *bridge = NULL;
4231 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004232 struct acpi_dmar_atsr *atsr;
4233 struct dmar_atsr_unit *atsru;
4234
4235 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004236 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004237 bridge = bus->self;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004238 if (!bridge || !pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004239 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004240 return 0;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004241 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004242 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004243 }
Jiang Liub5f82dd2014-02-19 14:07:31 +08004244 if (!bridge)
4245 return 0;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004246
Jiang Liu0e242612014-02-19 14:07:34 +08004247 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004248 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4249 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4250 if (atsr->segment != pci_domain_nr(dev->bus))
4251 continue;
4252
Jiang Liub683b232014-02-19 14:07:32 +08004253 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004254 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004255 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004256
4257 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004258 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004259 }
Jiang Liub683b232014-02-19 14:07:32 +08004260 ret = 0;
4261out:
Jiang Liu0e242612014-02-19 14:07:34 +08004262 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004263
Jiang Liub683b232014-02-19 14:07:32 +08004264 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004265}
4266
Jiang Liu59ce0512014-02-19 14:07:35 +08004267int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4268{
4269 int ret = 0;
4270 struct dmar_rmrr_unit *rmrru;
4271 struct dmar_atsr_unit *atsru;
4272 struct acpi_dmar_atsr *atsr;
4273 struct acpi_dmar_reserved_memory *rmrr;
4274
4275 if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
4276 return 0;
4277
4278 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4279 rmrr = container_of(rmrru->hdr,
4280 struct acpi_dmar_reserved_memory, header);
4281 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4282 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4283 ((void *)rmrr) + rmrr->header.length,
4284 rmrr->segment, rmrru->devices,
4285 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004286 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004287 return ret;
4288 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004289 dmar_remove_dev_scope(info, rmrr->segment,
4290 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004291 }
4292 }
4293
4294 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4295 if (atsru->include_all)
4296 continue;
4297
4298 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4299 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4300 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4301 (void *)atsr + atsr->header.length,
4302 atsr->segment, atsru->devices,
4303 atsru->devices_cnt);
4304 if (ret > 0)
4305 break;
4306 else if(ret < 0)
4307 return ret;
4308 } else if (info->event == BUS_NOTIFY_DEL_DEVICE) {
4309 if (dmar_remove_dev_scope(info, atsr->segment,
4310 atsru->devices, atsru->devices_cnt))
4311 break;
4312 }
4313 }
4314
4315 return 0;
4316}
4317
Fenghua Yu99dcade2009-11-11 07:23:06 -08004318/*
4319 * Here we only respond to action of unbound device from driver.
4320 *
4321 * Added device is not attached to its DMAR domain here yet. That will happen
4322 * when mapping the device to iova.
4323 */
4324static int device_notifier(struct notifier_block *nb,
4325 unsigned long action, void *data)
4326{
4327 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004328 struct dmar_domain *domain;
4329
David Woodhouse3d891942014-03-06 15:59:26 +00004330 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004331 return 0;
4332
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004333 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004334 return 0;
4335
David Woodhouse1525a292014-03-06 16:19:30 +00004336 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004337 if (!domain)
4338 return 0;
4339
Jiang Liu3a5670e2014-02-19 14:07:33 +08004340 down_read(&dmar_global_lock);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004341 domain_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004342 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004343 domain_exit(domain);
Jiang Liu3a5670e2014-02-19 14:07:33 +08004344 up_read(&dmar_global_lock);
Alex Williamsona97590e2011-03-04 14:52:16 -07004345
Fenghua Yu99dcade2009-11-11 07:23:06 -08004346 return 0;
4347}
4348
4349static struct notifier_block device_nb = {
4350 .notifier_call = device_notifier,
4351};
4352
Jiang Liu75f05562014-02-19 14:07:37 +08004353static int intel_iommu_memory_notifier(struct notifier_block *nb,
4354 unsigned long val, void *v)
4355{
4356 struct memory_notify *mhp = v;
4357 unsigned long long start, end;
4358 unsigned long start_vpfn, last_vpfn;
4359
4360 switch (val) {
4361 case MEM_GOING_ONLINE:
4362 start = mhp->start_pfn << PAGE_SHIFT;
4363 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4364 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004365 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004366 start, end);
4367 return NOTIFY_BAD;
4368 }
4369 break;
4370
4371 case MEM_OFFLINE:
4372 case MEM_CANCEL_ONLINE:
4373 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4374 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4375 while (start_vpfn <= last_vpfn) {
4376 struct iova *iova;
4377 struct dmar_drhd_unit *drhd;
4378 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004379 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004380
4381 iova = find_iova(&si_domain->iovad, start_vpfn);
4382 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004383 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004384 start_vpfn);
4385 break;
4386 }
4387
4388 iova = split_and_remove_iova(&si_domain->iovad, iova,
4389 start_vpfn, last_vpfn);
4390 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004391 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004392 start_vpfn, last_vpfn);
4393 return NOTIFY_BAD;
4394 }
4395
David Woodhouseea8ea462014-03-05 17:09:32 +00004396 freelist = domain_unmap(si_domain, iova->pfn_lo,
4397 iova->pfn_hi);
4398
Jiang Liu75f05562014-02-19 14:07:37 +08004399 rcu_read_lock();
4400 for_each_active_iommu(iommu, drhd)
4401 iommu_flush_iotlb_psi(iommu, si_domain->id,
Jiang Liua156ef92014-07-11 14:19:36 +08004402 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004403 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004404 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004405 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004406
4407 start_vpfn = iova->pfn_hi + 1;
4408 free_iova_mem(iova);
4409 }
4410 break;
4411 }
4412
4413 return NOTIFY_OK;
4414}
4415
4416static struct notifier_block intel_iommu_memory_nb = {
4417 .notifier_call = intel_iommu_memory_notifier,
4418 .priority = 0
4419};
4420
Alex Williamsona5459cf2014-06-12 16:12:31 -06004421
4422static ssize_t intel_iommu_show_version(struct device *dev,
4423 struct device_attribute *attr,
4424 char *buf)
4425{
4426 struct intel_iommu *iommu = dev_get_drvdata(dev);
4427 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4428 return sprintf(buf, "%d:%d\n",
4429 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4430}
4431static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4432
4433static ssize_t intel_iommu_show_address(struct device *dev,
4434 struct device_attribute *attr,
4435 char *buf)
4436{
4437 struct intel_iommu *iommu = dev_get_drvdata(dev);
4438 return sprintf(buf, "%llx\n", iommu->reg_phys);
4439}
4440static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4441
4442static ssize_t intel_iommu_show_cap(struct device *dev,
4443 struct device_attribute *attr,
4444 char *buf)
4445{
4446 struct intel_iommu *iommu = dev_get_drvdata(dev);
4447 return sprintf(buf, "%llx\n", iommu->cap);
4448}
4449static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4450
4451static ssize_t intel_iommu_show_ecap(struct device *dev,
4452 struct device_attribute *attr,
4453 char *buf)
4454{
4455 struct intel_iommu *iommu = dev_get_drvdata(dev);
4456 return sprintf(buf, "%llx\n", iommu->ecap);
4457}
4458static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4459
4460static struct attribute *intel_iommu_attrs[] = {
4461 &dev_attr_version.attr,
4462 &dev_attr_address.attr,
4463 &dev_attr_cap.attr,
4464 &dev_attr_ecap.attr,
4465 NULL,
4466};
4467
4468static struct attribute_group intel_iommu_group = {
4469 .name = "intel-iommu",
4470 .attrs = intel_iommu_attrs,
4471};
4472
4473const struct attribute_group *intel_iommu_groups[] = {
4474 &intel_iommu_group,
4475 NULL,
4476};
4477
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004478int __init intel_iommu_init(void)
4479{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004480 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004481 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004482 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004483
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004484 /* VT-d is required for a TXT/tboot launch, so enforce that */
4485 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004486
Jiang Liu3a5670e2014-02-19 14:07:33 +08004487 if (iommu_init_mempool()) {
4488 if (force_on)
4489 panic("tboot: Failed to initialize iommu memory\n");
4490 return -ENOMEM;
4491 }
4492
4493 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004494 if (dmar_table_init()) {
4495 if (force_on)
4496 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004497 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004498 }
4499
Suresh Siddhac2c72862011-08-23 17:05:19 -07004500 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004501 if (force_on)
4502 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004503 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004504 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004505
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004506 if (no_iommu || dmar_disabled)
Jiang Liu9bdc5312014-01-06 14:18:27 +08004507 goto out_free_dmar;
Suresh Siddha2ae21012008-07-10 11:16:43 -07004508
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004509 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004510 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004511
4512 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004513 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004514
Joseph Cihula51a63e62011-03-21 11:04:24 -07004515 if (dmar_init_reserved_ranges()) {
4516 if (force_on)
4517 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004518 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004519 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004520
4521 init_no_remapping_devices();
4522
Joseph Cihulab7792602011-05-03 00:08:37 -07004523 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004524 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004525 if (force_on)
4526 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004527 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004528 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004529 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004530 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004531 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004532
mark gross5e0d2a62008-03-04 15:22:08 -08004533 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004534#ifdef CONFIG_SWIOTLB
4535 swiotlb = 0;
4536#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004537 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004538
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004539 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004540
Alex Williamsona5459cf2014-06-12 16:12:31 -06004541 for_each_active_iommu(iommu, drhd)
4542 iommu->iommu_dev = iommu_device_create(NULL, iommu,
4543 intel_iommu_groups,
4544 iommu->name);
4545
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004546 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004547 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004548 if (si_domain && !hw_pass_through)
4549 register_memory_notifier(&intel_iommu_memory_nb);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004550
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004551 intel_iommu_enabled = 1;
4552
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004553 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004554
4555out_free_reserved_range:
4556 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004557out_free_dmar:
4558 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004559 up_write(&dmar_global_lock);
4560 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004561 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004562}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004563
Alex Williamson579305f2014-07-03 09:51:43 -06004564static int iommu_detach_dev_cb(struct pci_dev *pdev, u16 alias, void *opaque)
4565{
4566 struct intel_iommu *iommu = opaque;
4567
4568 iommu_detach_dev(iommu, PCI_BUS_NUM(alias), alias & 0xff);
4569 return 0;
4570}
4571
4572/*
4573 * NB - intel-iommu lacks any sort of reference counting for the users of
4574 * dependent devices. If multiple endpoints have intersecting dependent
4575 * devices, unbinding the driver from any one of them will possibly leave
4576 * the others unable to operate.
4577 */
Han, Weidong3199aa62009-02-26 17:31:12 +08004578static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004579 struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004580{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004581 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004582 return;
4583
Alex Williamson579305f2014-07-03 09:51:43 -06004584 pci_for_each_dma_alias(to_pci_dev(dev), &iommu_detach_dev_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004585}
4586
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004587static void domain_remove_one_dev_info(struct dmar_domain *domain,
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004588 struct device *dev)
Weidong Hanc7151a82008-12-08 22:51:37 +08004589{
Yijing Wangbca2b912013-10-31 17:26:04 +08004590 struct device_domain_info *info, *tmp;
Weidong Hanc7151a82008-12-08 22:51:37 +08004591 struct intel_iommu *iommu;
4592 unsigned long flags;
Quentin Lambert2f119c72015-02-06 10:59:53 +01004593 bool found = false;
David Woodhouse156baca2014-03-09 14:00:57 -07004594 u8 bus, devfn;
Weidong Hanc7151a82008-12-08 22:51:37 +08004595
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004596 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08004597 if (!iommu)
4598 return;
4599
4600 spin_lock_irqsave(&device_domain_lock, flags);
Yijing Wangbca2b912013-10-31 17:26:04 +08004601 list_for_each_entry_safe(info, tmp, &domain->devices, link) {
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004602 if (info->iommu == iommu && info->bus == bus &&
4603 info->devfn == devfn) {
David Woodhouse109b9b02012-05-25 17:43:02 +01004604 unlink_domain_info(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004605 spin_unlock_irqrestore(&device_domain_lock, flags);
4606
Yu Zhao93a23a72009-05-18 13:51:37 +08004607 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004608 iommu_detach_dev(iommu, info->bus, info->devfn);
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004609 iommu_detach_dependent_devices(iommu, dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08004610 free_devinfo_mem(info);
4611
4612 spin_lock_irqsave(&device_domain_lock, flags);
4613
4614 if (found)
4615 break;
4616 else
4617 continue;
4618 }
4619
4620 /* if there is no other devices under the same iommu
4621 * owned by this domain, clear this iommu in iommu_bmp
4622 * update iommu count and coherency
4623 */
David Woodhouse8bbc4412014-03-09 13:52:37 -07004624 if (info->iommu == iommu)
Quentin Lambert2f119c72015-02-06 10:59:53 +01004625 found = true;
Weidong Hanc7151a82008-12-08 22:51:37 +08004626 }
4627
Roland Dreier3e7abe22011-07-20 06:22:21 -07004628 spin_unlock_irqrestore(&device_domain_lock, flags);
4629
Weidong Hanc7151a82008-12-08 22:51:37 +08004630 if (found == 0) {
Jiang Liufb170fb2014-07-11 14:19:28 +08004631 domain_detach_iommu(domain, iommu);
4632 if (!domain_type_is_vm_or_si(domain))
4633 iommu_detach_domain(domain, iommu);
Weidong Hanc7151a82008-12-08 22:51:37 +08004634 }
Weidong Hanc7151a82008-12-08 22:51:37 +08004635}
4636
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004637static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08004638{
4639 int adjust_width;
4640
Robin Murphy0fb5fe82015-01-12 17:51:16 +00004641 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN,
4642 DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004643 domain_reserve_special_ranges(domain);
4644
4645 /* calculate AGAW */
4646 domain->gaw = guest_width;
4647 adjust_width = guestwidth_to_adjustwidth(guest_width);
4648 domain->agaw = width_to_agaw(adjust_width);
4649
Weidong Han5e98c4b2008-12-08 23:03:27 +08004650 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004651 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004652 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004653 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004654
4655 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004656 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004657 if (!domain->pgd)
4658 return -ENOMEM;
4659 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4660 return 0;
4661}
4662
Joerg Roedel00a77de2015-03-26 13:43:08 +01004663static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004664{
Joerg Roedel5d450802008-12-03 14:52:32 +01004665 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004666 struct iommu_domain *domain;
4667
4668 if (type != IOMMU_DOMAIN_UNMANAGED)
4669 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004670
Jiang Liuab8dfe22014-07-11 14:19:27 +08004671 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004672 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004673 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004674 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004675 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004676 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004677 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004678 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004679 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004680 }
Allen Kay8140a952011-10-14 12:32:17 -07004681 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004682
Joerg Roedel00a77de2015-03-26 13:43:08 +01004683 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004684 domain->geometry.aperture_start = 0;
4685 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4686 domain->geometry.force_aperture = true;
4687
Joerg Roedel00a77de2015-03-26 13:43:08 +01004688 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004689}
Kay, Allen M38717942008-09-09 18:37:29 +03004690
Joerg Roedel00a77de2015-03-26 13:43:08 +01004691static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004692{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004693 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004694}
Kay, Allen M38717942008-09-09 18:37:29 +03004695
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004696static int intel_iommu_attach_device(struct iommu_domain *domain,
4697 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004698{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004699 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004700 struct intel_iommu *iommu;
4701 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004702 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004703
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004704 if (device_is_rmrr_locked(dev)) {
4705 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4706 return -EPERM;
4707 }
4708
David Woodhouse7207d8f2014-03-09 16:31:06 -07004709 /* normally dev is not mapped */
4710 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004711 struct dmar_domain *old_domain;
4712
David Woodhouse1525a292014-03-06 16:19:30 +00004713 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004714 if (old_domain) {
Jiang Liuab8dfe22014-07-11 14:19:27 +08004715 if (domain_type_is_vm_or_si(dmar_domain))
David Woodhousebf9c9ed2014-03-09 16:19:13 -07004716 domain_remove_one_dev_info(old_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004717 else
4718 domain_remove_dev_info(old_domain);
Joerg Roedel62c22162014-12-09 12:56:45 +01004719
4720 if (!domain_type_is_vm_or_si(old_domain) &&
4721 list_empty(&old_domain->devices))
4722 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004723 }
4724 }
4725
David Woodhouse156baca2014-03-09 14:00:57 -07004726 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004727 if (!iommu)
4728 return -ENODEV;
4729
4730 /* check if this iommu agaw is sufficient for max mapped address */
4731 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004732 if (addr_width > cap_mgaw(iommu->cap))
4733 addr_width = cap_mgaw(iommu->cap);
4734
4735 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004736 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004737 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004738 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004739 return -EFAULT;
4740 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004741 dmar_domain->gaw = addr_width;
4742
4743 /*
4744 * Knock out extra levels of page tables if necessary
4745 */
4746 while (iommu->agaw < dmar_domain->agaw) {
4747 struct dma_pte *pte;
4748
4749 pte = dmar_domain->pgd;
4750 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004751 dmar_domain->pgd = (struct dma_pte *)
4752 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01004753 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01004754 }
4755 dmar_domain->agaw--;
4756 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004757
David Woodhouse5913c9b2014-03-09 16:27:31 -07004758 return domain_add_dev_info(dmar_domain, dev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004759}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004760
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004761static void intel_iommu_detach_device(struct iommu_domain *domain,
4762 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004763{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004764 domain_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03004765}
Kay, Allen M38717942008-09-09 18:37:29 +03004766
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004767static int intel_iommu_map(struct iommu_domain *domain,
4768 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004769 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03004770{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004771 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004772 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004773 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004774 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004775
Joerg Roedeldde57a22008-12-03 15:04:09 +01004776 if (iommu_prot & IOMMU_READ)
4777 prot |= DMA_PTE_READ;
4778 if (iommu_prot & IOMMU_WRITE)
4779 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08004780 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
4781 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004782
David Woodhouse163cc522009-06-28 00:51:17 +01004783 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01004784 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004785 u64 end;
4786
4787 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01004788 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004789 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004790 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004791 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01004792 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004793 return -EFAULT;
4794 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01004795 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004796 }
David Woodhousead051222009-06-28 14:22:28 +01004797 /* Round up size to next multiple of PAGE_SIZE, if it and
4798 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01004799 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01004800 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
4801 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004802 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03004803}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004804
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02004805static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00004806 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004807{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004808 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00004809 struct page *freelist = NULL;
4810 struct intel_iommu *iommu;
4811 unsigned long start_pfn, last_pfn;
4812 unsigned int npages;
4813 int iommu_id, num, ndomains, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01004814
David Woodhouse5cf0a762014-03-19 16:07:49 +00004815 /* Cope with horrid API which requires us to unmap more than the
4816 size argument if it happens to be a large-page mapping. */
4817 if (!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level))
4818 BUG();
4819
4820 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
4821 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
4822
David Woodhouseea8ea462014-03-05 17:09:32 +00004823 start_pfn = iova >> VTD_PAGE_SHIFT;
4824 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
4825
4826 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
4827
4828 npages = last_pfn - start_pfn + 1;
4829
4830 for_each_set_bit(iommu_id, dmar_domain->iommu_bmp, g_num_of_iommus) {
4831 iommu = g_iommus[iommu_id];
4832
4833 /*
4834 * find bit position of dmar_domain
4835 */
4836 ndomains = cap_ndoms(iommu->cap);
4837 for_each_set_bit(num, iommu->domain_ids, ndomains) {
4838 if (iommu->domains[num] == dmar_domain)
4839 iommu_flush_iotlb_psi(iommu, num, start_pfn,
4840 npages, !freelist, 0);
4841 }
4842
4843 }
4844
4845 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004846
David Woodhouse163cc522009-06-28 00:51:17 +01004847 if (dmar_domain->max_addr == iova + size)
4848 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004849
David Woodhouse5cf0a762014-03-19 16:07:49 +00004850 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004851}
Kay, Allen M38717942008-09-09 18:37:29 +03004852
Joerg Roedeld14d6572008-12-03 15:06:57 +01004853static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05304854 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03004855{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004856 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03004857 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00004858 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004859 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03004860
David Woodhouse5cf0a762014-03-19 16:07:49 +00004861 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03004862 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004863 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03004864
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004865 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03004866}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004867
Joerg Roedel5d587b82014-09-05 10:50:45 +02004868static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004869{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004870 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004871 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04004872 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02004873 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004874
Joerg Roedel5d587b82014-09-05 10:50:45 +02004875 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08004876}
4877
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004878static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004879{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004880 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004881 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07004882 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04004883
Alex Williamsona5459cf2014-06-12 16:12:31 -06004884 iommu = device_to_iommu(dev, &bus, &devfn);
4885 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04004886 return -ENODEV;
4887
Alex Williamsona5459cf2014-06-12 16:12:31 -06004888 iommu_device_link(iommu->iommu_dev, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004889
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004890 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06004891
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004892 if (IS_ERR(group))
4893 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004894
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004895 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06004896 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004897}
4898
4899static void intel_iommu_remove_device(struct device *dev)
4900{
Alex Williamsona5459cf2014-06-12 16:12:31 -06004901 struct intel_iommu *iommu;
4902 u8 bus, devfn;
4903
4904 iommu = device_to_iommu(dev, &bus, &devfn);
4905 if (!iommu)
4906 return;
4907
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004908 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004909
4910 iommu_device_unlink(iommu->iommu_dev, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04004911}
4912
Thierry Redingb22f6432014-06-27 09:03:12 +02004913static const struct iommu_ops intel_iommu_ops = {
Joerg Roedel5d587b82014-09-05 10:50:45 +02004914 .capable = intel_iommu_capable,
Joerg Roedel00a77de2015-03-26 13:43:08 +01004915 .domain_alloc = intel_iommu_domain_alloc,
4916 .domain_free = intel_iommu_domain_free,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004917 .attach_dev = intel_iommu_attach_device,
4918 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01004919 .map = intel_iommu_map,
4920 .unmap = intel_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07004921 .map_sg = default_iommu_map_sg,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004922 .iova_to_phys = intel_iommu_iova_to_phys,
Alex Williamsonabdfdde2012-05-30 14:19:19 -06004923 .add_device = intel_iommu_add_device,
4924 .remove_device = intel_iommu_remove_device,
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +02004925 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004926};
David Woodhouse9af88142009-02-13 23:18:03 +00004927
Daniel Vetter94526182013-01-20 23:50:13 +01004928static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
4929{
4930 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004931 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01004932 dmar_map_gfx = 0;
4933}
4934
4935DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
4936DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
4937DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
4938DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
4939DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
4940DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
4941DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
4942
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004943static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00004944{
4945 /*
4946 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01004947 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00004948 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004949 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00004950 rwbf_quirk = 1;
4951}
4952
4953DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01004954DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
4955DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
4956DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
4957DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
4958DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
4959DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07004960
Adam Jacksoneecfd572010-08-25 21:17:34 +01004961#define GGC 0x52
4962#define GGC_MEMORY_SIZE_MASK (0xf << 8)
4963#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
4964#define GGC_MEMORY_SIZE_1M (0x1 << 8)
4965#define GGC_MEMORY_SIZE_2M (0x3 << 8)
4966#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
4967#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
4968#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
4969#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
4970
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08004971static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01004972{
4973 unsigned short ggc;
4974
Adam Jacksoneecfd572010-08-25 21:17:34 +01004975 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01004976 return;
4977
Adam Jacksoneecfd572010-08-25 21:17:34 +01004978 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004979 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01004980 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004981 } else if (dmar_map_gfx) {
4982 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004983 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07004984 intel_iommu_strict = 1;
4985 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01004986}
4987DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
4988DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
4989DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
4990DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
4991
David Woodhousee0fc7e02009-09-30 09:12:17 -07004992/* On Tylersburg chipsets, some BIOSes have been known to enable the
4993 ISOCH DMAR unit for the Azalia sound device, but not give it any
4994 TLB entries, which causes it to deadlock. Check for that. We do
4995 this in a function called from init_dmars(), instead of in a PCI
4996 quirk, because we don't want to print the obnoxious "BIOS broken"
4997 message if VT-d is actually disabled.
4998*/
4999static void __init check_tylersburg_isoch(void)
5000{
5001 struct pci_dev *pdev;
5002 uint32_t vtisochctrl;
5003
5004 /* If there's no Azalia in the system anyway, forget it. */
5005 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5006 if (!pdev)
5007 return;
5008 pci_dev_put(pdev);
5009
5010 /* System Management Registers. Might be hidden, in which case
5011 we can't do the sanity check. But that's OK, because the
5012 known-broken BIOSes _don't_ actually hide it, so far. */
5013 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5014 if (!pdev)
5015 return;
5016
5017 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5018 pci_dev_put(pdev);
5019 return;
5020 }
5021
5022 pci_dev_put(pdev);
5023
5024 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5025 if (vtisochctrl & 1)
5026 return;
5027
5028 /* Drop all bits other than the number of TLB entries */
5029 vtisochctrl &= 0x1c;
5030
5031 /* If we have the recommended number of TLB entries (16), fine. */
5032 if (vtisochctrl == 0x10)
5033 return;
5034
5035 /* Zero TLB entries? You get to ride the short bus to school. */
5036 if (!vtisochctrl) {
5037 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5038 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5039 dmi_get_system_info(DMI_BIOS_VENDOR),
5040 dmi_get_system_info(DMI_BIOS_VERSION),
5041 dmi_get_system_info(DMI_PRODUCT_VERSION));
5042 iommu_identity_mapping |= IDENTMAP_AZALIA;
5043 return;
5044 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005045
5046 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005047 vtisochctrl);
5048}