blob: 0ee6516de41a72d92ce146ca867ac6e30194e517 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
Omer Pelegaa473242016-04-20 11:33:02 +030036#include <linux/cpu.h>
mark gross5e0d2a62008-03-04 15:22:08 -080037#include <linux/timer.h>
Dan Williamsdfddb962015-10-09 18:16:46 -040038#include <linux/io.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010040#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030041#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010042#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070043#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100044#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020045#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080046#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070047#include <linux/dma-contiguous.h>
Christoph Hellwigfec777c2018-03-19 11:38:15 +010048#include <linux/dma-direct.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020049#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070050#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070051#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090052#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070053
Joerg Roedel078e1ee2012-09-26 12:44:43 +020054#include "irq_remapping.h"
Lu Baolu56283172018-07-14 15:46:54 +080055#include "intel-pasid.h"
Joerg Roedel078e1ee2012-09-26 12:44:43 +020056
Fenghua Yu5b6985c2008-10-16 18:02:32 -070057#define ROOT_SIZE VTD_PAGE_SIZE
58#define CONTEXT_SIZE VTD_PAGE_SIZE
59
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000061#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070062#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070063#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070064
65#define IOAPIC_RANGE_START (0xfee00000)
66#define IOAPIC_RANGE_END (0xfeefffff)
67#define IOVA_START_ADDR (0x1000)
68
Sohil Mehta5e3b4a12017-12-20 11:59:24 -080069#define DEFAULT_DOMAIN_ADDRESS_WIDTH 57
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070070
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070071#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080072#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070073
David Woodhouse2ebe3152009-09-19 07:34:04 -070074#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
75#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
76
77/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
78 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
79#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
80 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
81#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070082
Robin Murphy1b722502015-01-12 17:51:15 +000083/* IO virtual address start page frame number */
84#define IOVA_START_PFN (1)
85
Mark McLoughlinf27be032008-11-20 15:49:43 +000086#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
mark gross5e0d2a62008-03-04 15:22:08 -080087
Andrew Mortondf08cdc2010-09-22 13:05:11 -070088/* page table handling */
89#define LEVEL_STRIDE (9)
90#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
91
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020092/*
93 * This bitmap is used to advertise the page sizes our hardware support
94 * to the IOMMU core, which will then use this information to split
95 * physically contiguous memory regions it is mapping into page sizes
96 * that we support.
97 *
98 * Traditionally the IOMMU core just handed us the mappings directly,
99 * after making sure the size is an order of a 4KiB page and that the
100 * mapping has natural alignment.
101 *
102 * To retain this behavior, we currently advertise that we support
103 * all page sizes that are an order of 4KiB.
104 *
105 * If at some point we'd like to utilize the IOMMU core's new behavior,
106 * we could change this to advertise the real page sizes we support.
107 */
108#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
109
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700110static inline int agaw_to_level(int agaw)
111{
112 return agaw + 2;
113}
114
115static inline int agaw_to_width(int agaw)
116{
Jiang Liu5c645b32014-01-06 14:18:12 +0800117 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700118}
119
120static inline int width_to_agaw(int width)
121{
Jiang Liu5c645b32014-01-06 14:18:12 +0800122 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700123}
124
125static inline unsigned int level_to_offset_bits(int level)
126{
127 return (level - 1) * LEVEL_STRIDE;
128}
129
130static inline int pfn_level_offset(unsigned long pfn, int level)
131{
132 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
133}
134
135static inline unsigned long level_mask(int level)
136{
137 return -1UL << level_to_offset_bits(level);
138}
139
140static inline unsigned long level_size(int level)
141{
142 return 1UL << level_to_offset_bits(level);
143}
144
145static inline unsigned long align_to_level(unsigned long pfn, int level)
146{
147 return (pfn + level_size(level) - 1) & level_mask(level);
148}
David Woodhousefd18de52009-05-10 23:57:41 +0100149
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100150static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
151{
Jiang Liu5c645b32014-01-06 14:18:12 +0800152 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100153}
154
David Woodhousedd4e8312009-06-27 16:21:20 +0100155/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
156 are never going to work. */
157static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
158{
159 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
160}
161
162static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
163{
164 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
165}
166static inline unsigned long page_to_dma_pfn(struct page *pg)
167{
168 return mm_to_dma_pfn(page_to_pfn(pg));
169}
170static inline unsigned long virt_to_dma_pfn(void *p)
171{
172 return page_to_dma_pfn(virt_to_page(p));
173}
174
Weidong Hand9630fe2008-12-08 11:06:32 +0800175/* global iommu list, set NULL for ignored DMAR units */
176static struct intel_iommu **g_iommus;
177
David Woodhousee0fc7e02009-09-30 09:12:17 -0700178static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000179static int rwbf_quirk;
180
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000181/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700182 * set to 1 to panic kernel if can't successfully enable VT-d
183 * (used when kernel is launched w/ TXT)
184 */
185static int force_on = 0;
Shaohua Libfd20f12017-04-26 09:18:35 -0700186int intel_iommu_tboot_noforce;
Joseph Cihulab7792602011-05-03 00:08:37 -0700187
188/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000189 * 0: Present
190 * 1-11: Reserved
191 * 12-63: Context Ptr (12 - (haw-1))
192 * 64-127: Reserved
193 */
194struct root_entry {
David Woodhouse03ecc322015-02-13 14:35:21 +0000195 u64 lo;
196 u64 hi;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000197};
198#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000199
Joerg Roedel091d42e2015-06-12 11:56:10 +0200200/*
201 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
202 * if marked present.
203 */
204static phys_addr_t root_entry_lctp(struct root_entry *re)
205{
206 if (!(re->lo & 1))
207 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000208
Joerg Roedel091d42e2015-06-12 11:56:10 +0200209 return re->lo & VTD_PAGE_MASK;
210}
211
212/*
213 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
214 * if marked present.
215 */
216static phys_addr_t root_entry_uctp(struct root_entry *re)
217{
218 if (!(re->hi & 1))
219 return 0;
220
221 return re->hi & VTD_PAGE_MASK;
222}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000223/*
224 * low 64 bits:
225 * 0: present
226 * 1: fault processing disable
227 * 2-3: translation type
228 * 12-63: address space root
229 * high 64 bits:
230 * 0-2: address width
231 * 3-6: aval
232 * 8-23: domain id
233 */
234struct context_entry {
235 u64 lo;
236 u64 hi;
237};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000238
Joerg Roedelcf484d02015-06-12 12:21:46 +0200239static inline void context_clear_pasid_enable(struct context_entry *context)
240{
241 context->lo &= ~(1ULL << 11);
242}
243
244static inline bool context_pasid_enabled(struct context_entry *context)
245{
246 return !!(context->lo & (1ULL << 11));
247}
248
249static inline void context_set_copied(struct context_entry *context)
250{
251 context->hi |= (1ull << 3);
252}
253
254static inline bool context_copied(struct context_entry *context)
255{
256 return !!(context->hi & (1ULL << 3));
257}
258
259static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000260{
261 return (context->lo & 1);
262}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200263
264static inline bool context_present(struct context_entry *context)
265{
266 return context_pasid_enabled(context) ?
267 __context_present(context) :
268 __context_present(context) && !context_copied(context);
269}
270
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000271static inline void context_set_present(struct context_entry *context)
272{
273 context->lo |= 1;
274}
275
276static inline void context_set_fault_enable(struct context_entry *context)
277{
278 context->lo &= (((u64)-1) << 2) | 1;
279}
280
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000281static inline void context_set_translation_type(struct context_entry *context,
282 unsigned long value)
283{
284 context->lo &= (((u64)-1) << 4) | 3;
285 context->lo |= (value & 3) << 2;
286}
287
288static inline void context_set_address_root(struct context_entry *context,
289 unsigned long value)
290{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800291 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000292 context->lo |= value & VTD_PAGE_MASK;
293}
294
295static inline void context_set_address_width(struct context_entry *context,
296 unsigned long value)
297{
298 context->hi |= value & 7;
299}
300
301static inline void context_set_domain_id(struct context_entry *context,
302 unsigned long value)
303{
304 context->hi |= (value & ((1 << 16) - 1)) << 8;
305}
306
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200307static inline int context_domain_id(struct context_entry *c)
308{
309 return((c->hi >> 8) & 0xffff);
310}
311
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000312static inline void context_clear_entry(struct context_entry *context)
313{
314 context->lo = 0;
315 context->hi = 0;
316}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000317
Mark McLoughlin622ba122008-11-20 15:49:46 +0000318/*
319 * 0: readable
320 * 1: writable
321 * 2-6: reserved
322 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800323 * 8-10: available
324 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000325 * 12-63: Host physcial address
326 */
327struct dma_pte {
328 u64 val;
329};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000330
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000331static inline void dma_clear_pte(struct dma_pte *pte)
332{
333 pte->val = 0;
334}
335
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000336static inline u64 dma_pte_addr(struct dma_pte *pte)
337{
David Woodhousec85994e2009-07-01 19:21:24 +0100338#ifdef CONFIG_64BIT
339 return pte->val & VTD_PAGE_MASK;
340#else
341 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100342 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100343#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000344}
345
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000346static inline bool dma_pte_present(struct dma_pte *pte)
347{
348 return (pte->val & 3) != 0;
349}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000350
Allen Kay4399c8b2011-10-14 12:32:46 -0700351static inline bool dma_pte_superpage(struct dma_pte *pte)
352{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200353 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700354}
355
David Woodhouse75e6bf92009-07-02 11:21:16 +0100356static inline int first_pte_in_page(struct dma_pte *pte)
357{
358 return !((unsigned long)pte & ~VTD_PAGE_MASK);
359}
360
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700361/*
362 * This domain is a statically identity mapping domain.
363 * 1. This domain creats a static 1:1 mapping to all usable memory.
364 * 2. It maps to each iommu if successful.
365 * 3. Each iommu mapps to this domain if successful.
366 */
David Woodhouse19943b02009-08-04 16:19:20 +0100367static struct dmar_domain *si_domain;
368static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700369
Joerg Roedel28ccce02015-07-21 14:45:31 +0200370/*
371 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800372 * across iommus may be owned in one domain, e.g. kvm guest.
373 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800374#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800375
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700376/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800377#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700378
Joerg Roedel29a27712015-07-21 17:17:12 +0200379#define for_each_domain_iommu(idx, domain) \
380 for (idx = 0; idx < g_num_of_iommus; idx++) \
381 if (domain->iommu_refcnt[idx])
382
Jiang Liub94e4112014-02-19 14:07:25 +0800383struct dmar_rmrr_unit {
384 struct list_head list; /* list of rmrr units */
385 struct acpi_dmar_header *hdr; /* ACPI header */
386 u64 base_address; /* reserved base address*/
387 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000388 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800389 int devices_cnt; /* target device count */
Eric Auger0659b8d2017-01-19 20:57:53 +0000390 struct iommu_resv_region *resv; /* reserved region handle */
Jiang Liub94e4112014-02-19 14:07:25 +0800391};
392
393struct dmar_atsr_unit {
394 struct list_head list; /* list of ATSR units */
395 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000396 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800397 int devices_cnt; /* target device count */
398 u8 include_all:1; /* include all ports */
399};
400
401static LIST_HEAD(dmar_atsr_units);
402static LIST_HEAD(dmar_rmrr_units);
403
404#define for_each_rmrr_units(rmrr) \
405 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
406
mark gross5e0d2a62008-03-04 15:22:08 -0800407/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800408static int g_num_of_iommus;
409
Jiang Liu92d03cc2014-02-19 14:07:28 +0800410static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700411static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200412static void dmar_remove_one_dev_info(struct dmar_domain *domain,
413 struct device *dev);
Joerg Roedel127c7612015-07-23 17:44:46 +0200414static void __dmar_remove_one_dev_info(struct device_domain_info *info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200415static void domain_context_clear(struct intel_iommu *iommu,
416 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800417static int domain_detach_iommu(struct dmar_domain *domain,
418 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700419
Suresh Siddhad3f13812011-08-23 17:05:25 -0700420#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800421int dmar_disabled = 0;
422#else
423int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700424#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800425
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200426int intel_iommu_enabled = 0;
427EXPORT_SYMBOL_GPL(intel_iommu_enabled);
428
David Woodhouse2d9e6672010-06-15 10:57:57 +0100429static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700430static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800431static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100432static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100433static int intel_iommu_ecs = 1;
Lu Baolu2db15812018-07-08 14:23:21 +0800434static int intel_iommu_pasid28;
David Woodhouseae853dd2015-09-09 11:58:59 +0100435static int iommu_identity_mapping;
David Woodhousec83b2f22015-06-12 10:15:49 +0100436
David Woodhouseae853dd2015-09-09 11:58:59 +0100437#define IDENTMAP_ALL 1
438#define IDENTMAP_GFX 2
439#define IDENTMAP_AZALIA 4
David Woodhousec83b2f22015-06-12 10:15:49 +0100440
Lu Baolu2db15812018-07-08 14:23:21 +0800441/* Broadwell and Skylake have broken ECS support — normal so-called "second
442 * level" translation of DMA requests-without-PASID doesn't actually happen
443 * unless you also set the NESTE bit in an extended context-entry. Which of
444 * course means that SVM doesn't work because it's trying to do nested
445 * translation of the physical addresses it finds in the process page tables,
446 * through the IOVA->phys mapping found in the "second level" page tables.
447 *
448 * The VT-d specification was retroactively changed to change the definition
449 * of the capability bits and pretend that Broadwell/Skylake never happened...
450 * but unfortunately the wrong bit was changed. It's ECS which is broken, but
451 * for some reason it was the PASID capability bit which was redefined (from
452 * bit 28 on BDW/SKL to bit 40 in future).
453 *
454 * So our test for ECS needs to eschew those implementations which set the old
455 * PASID capabiity bit 28, since those are the ones on which ECS is broken.
456 * Unless we are working around the 'pasid28' limitations, that is, by putting
457 * the device into passthrough mode for normal DMA and thus masking the bug.
458 */
459#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
460 (intel_iommu_pasid28 || !ecap_broken_pasid(iommu->ecap)))
461/* PASID support is thus enabled if ECS is enabled and *either* of the old
462 * or new capability bits are set. */
463#define pasid_enabled(iommu) (ecs_enabled(iommu) && \
464 (ecap_pasid(iommu->ecap) || ecap_broken_pasid(iommu->ecap)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700465
David Woodhousec0771df2011-10-14 20:59:46 +0100466int intel_iommu_gfx_mapped;
467EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
468
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700469#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
470static DEFINE_SPINLOCK(device_domain_lock);
471static LIST_HEAD(device_domain_list);
472
Lu Baolu85319dc2018-07-14 15:46:58 +0800473/*
474 * Iterate over elements in device_domain_list and call the specified
475 * callback @fn against each element. This helper should only be used
476 * in the context where the device_domain_lock has already been holden.
477 */
478int for_each_device_domain(int (*fn)(struct device_domain_info *info,
479 void *data), void *data)
480{
481 int ret = 0;
482 struct device_domain_info *info;
483
484 assert_spin_locked(&device_domain_lock);
485 list_for_each_entry(info, &device_domain_list, global) {
486 ret = fn(info, data);
487 if (ret)
488 return ret;
489 }
490
491 return 0;
492}
493
Joerg Roedelb0119e82017-02-01 13:23:08 +0100494const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100495
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200496static bool translation_pre_enabled(struct intel_iommu *iommu)
497{
498 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
499}
500
Joerg Roedel091d42e2015-06-12 11:56:10 +0200501static void clear_translation_pre_enabled(struct intel_iommu *iommu)
502{
503 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
504}
505
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200506static void init_translation_status(struct intel_iommu *iommu)
507{
508 u32 gsts;
509
510 gsts = readl(iommu->reg + DMAR_GSTS_REG);
511 if (gsts & DMA_GSTS_TES)
512 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
513}
514
Joerg Roedel00a77de2015-03-26 13:43:08 +0100515/* Convert generic 'struct iommu_domain to private struct dmar_domain */
516static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
517{
518 return container_of(dom, struct dmar_domain, domain);
519}
520
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700521static int __init intel_iommu_setup(char *str)
522{
523 if (!str)
524 return -EINVAL;
525 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800526 if (!strncmp(str, "on", 2)) {
527 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200528 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800529 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700530 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200531 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700532 } else if (!strncmp(str, "igfx_off", 8)) {
533 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200534 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700535 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200536 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700537 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800538 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200539 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800540 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100541 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200542 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100543 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100544 } else if (!strncmp(str, "ecs_off", 7)) {
545 printk(KERN_INFO
546 "Intel-IOMMU: disable extended context table support\n");
547 intel_iommu_ecs = 0;
Lu Baolu2db15812018-07-08 14:23:21 +0800548 } else if (!strncmp(str, "pasid28", 7)) {
549 printk(KERN_INFO
550 "Intel-IOMMU: enable pre-production PASID support\n");
551 intel_iommu_pasid28 = 1;
552 iommu_identity_mapping |= IDENTMAP_GFX;
Shaohua Libfd20f12017-04-26 09:18:35 -0700553 } else if (!strncmp(str, "tboot_noforce", 13)) {
554 printk(KERN_INFO
555 "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");
556 intel_iommu_tboot_noforce = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700557 }
558
559 str += strcspn(str, ",");
560 while (*str == ',')
561 str++;
562 }
563 return 0;
564}
565__setup("intel_iommu=", intel_iommu_setup);
566
567static struct kmem_cache *iommu_domain_cache;
568static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700569
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200570static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
571{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200572 struct dmar_domain **domains;
573 int idx = did >> 8;
574
575 domains = iommu->domains[idx];
576 if (!domains)
577 return NULL;
578
579 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200580}
581
582static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
583 struct dmar_domain *domain)
584{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200585 struct dmar_domain **domains;
586 int idx = did >> 8;
587
588 if (!iommu->domains[idx]) {
589 size_t size = 256 * sizeof(struct dmar_domain *);
590 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
591 }
592
593 domains = iommu->domains[idx];
594 if (WARN_ON(!domains))
595 return;
596 else
597 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200598}
599
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800600void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700601{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700602 struct page *page;
603 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700604
Suresh Siddha4c923d42009-10-02 11:01:24 -0700605 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
606 if (page)
607 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700608 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700609}
610
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800611void free_pgtable_page(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700612{
613 free_page((unsigned long)vaddr);
614}
615
616static inline void *alloc_domain_mem(void)
617{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900618 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700619}
620
Kay, Allen M38717942008-09-09 18:37:29 +0300621static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700622{
623 kmem_cache_free(iommu_domain_cache, vaddr);
624}
625
626static inline void * alloc_devinfo_mem(void)
627{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900628 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700629}
630
631static inline void free_devinfo_mem(void *vaddr)
632{
633 kmem_cache_free(iommu_devinfo_cache, vaddr);
634}
635
Jiang Liuab8dfe22014-07-11 14:19:27 +0800636static inline int domain_type_is_vm(struct dmar_domain *domain)
637{
638 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
639}
640
Joerg Roedel28ccce02015-07-21 14:45:31 +0200641static inline int domain_type_is_si(struct dmar_domain *domain)
642{
643 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
644}
645
Jiang Liuab8dfe22014-07-11 14:19:27 +0800646static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
647{
648 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
649 DOMAIN_FLAG_STATIC_IDENTITY);
650}
Weidong Han1b573682008-12-08 15:34:06 +0800651
Jiang Liu162d1b12014-07-11 14:19:35 +0800652static inline int domain_pfn_supported(struct dmar_domain *domain,
653 unsigned long pfn)
654{
655 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
656
657 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
658}
659
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700660static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800661{
662 unsigned long sagaw;
663 int agaw = -1;
664
665 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700666 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800667 agaw >= 0; agaw--) {
668 if (test_bit(agaw, &sagaw))
669 break;
670 }
671
672 return agaw;
673}
674
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700675/*
676 * Calculate max SAGAW for each iommu.
677 */
678int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
679{
680 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
681}
682
683/*
684 * calculate agaw for each iommu.
685 * "SAGAW" may be different across iommus, use a default agaw, and
686 * get a supported less agaw for iommus that don't support the default agaw.
687 */
688int iommu_calculate_agaw(struct intel_iommu *iommu)
689{
690 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
691}
692
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700693/* This functionin only returns single iommu in a domain */
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800694struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
Weidong Han8c11e792008-12-08 15:29:22 +0800695{
696 int iommu_id;
697
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700698 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800699 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200700 for_each_domain_iommu(iommu_id, domain)
701 break;
702
Weidong Han8c11e792008-12-08 15:29:22 +0800703 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
704 return NULL;
705
706 return g_iommus[iommu_id];
707}
708
Weidong Han8e6040972008-12-08 15:49:06 +0800709static void domain_update_iommu_coherency(struct dmar_domain *domain)
710{
David Woodhoused0501962014-03-11 17:10:29 -0700711 struct dmar_drhd_unit *drhd;
712 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100713 bool found = false;
714 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800715
David Woodhoused0501962014-03-11 17:10:29 -0700716 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800717
Joerg Roedel29a27712015-07-21 17:17:12 +0200718 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100719 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800720 if (!ecap_coherent(g_iommus[i]->ecap)) {
721 domain->iommu_coherency = 0;
722 break;
723 }
Weidong Han8e6040972008-12-08 15:49:06 +0800724 }
David Woodhoused0501962014-03-11 17:10:29 -0700725 if (found)
726 return;
727
728 /* No hardware attached; use lowest common denominator */
729 rcu_read_lock();
730 for_each_active_iommu(iommu, drhd) {
731 if (!ecap_coherent(iommu->ecap)) {
732 domain->iommu_coherency = 0;
733 break;
734 }
735 }
736 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800737}
738
Jiang Liu161f6932014-07-11 14:19:37 +0800739static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100740{
Allen Kay8140a952011-10-14 12:32:17 -0700741 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800742 struct intel_iommu *iommu;
743 int ret = 1;
744
745 rcu_read_lock();
746 for_each_active_iommu(iommu, drhd) {
747 if (iommu != skip) {
748 if (!ecap_sc_support(iommu->ecap)) {
749 ret = 0;
750 break;
751 }
752 }
753 }
754 rcu_read_unlock();
755
756 return ret;
757}
758
759static int domain_update_iommu_superpage(struct intel_iommu *skip)
760{
761 struct dmar_drhd_unit *drhd;
762 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700763 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100764
765 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800766 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100767 }
768
Allen Kay8140a952011-10-14 12:32:17 -0700769 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800770 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700771 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800772 if (iommu != skip) {
773 mask &= cap_super_page_val(iommu->cap);
774 if (!mask)
775 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100776 }
777 }
Jiang Liu0e242612014-02-19 14:07:34 +0800778 rcu_read_unlock();
779
Jiang Liu161f6932014-07-11 14:19:37 +0800780 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100781}
782
Sheng Yang58c610b2009-03-18 15:33:05 +0800783/* Some capabilities may be different across iommus */
784static void domain_update_iommu_cap(struct dmar_domain *domain)
785{
786 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800787 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
788 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800789}
790
David Woodhouse03ecc322015-02-13 14:35:21 +0000791static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu,
792 u8 bus, u8 devfn, int alloc)
793{
794 struct root_entry *root = &iommu->root_entry[bus];
795 struct context_entry *context;
796 u64 *entry;
797
Joerg Roedel4df4eab2015-08-25 10:54:28 +0200798 entry = &root->lo;
David Woodhousec83b2f22015-06-12 10:15:49 +0100799 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000800 if (devfn >= 0x80) {
801 devfn -= 0x80;
802 entry = &root->hi;
803 }
804 devfn *= 2;
805 }
David Woodhouse03ecc322015-02-13 14:35:21 +0000806 if (*entry & 1)
807 context = phys_to_virt(*entry & VTD_PAGE_MASK);
808 else {
809 unsigned long phy_addr;
810 if (!alloc)
811 return NULL;
812
813 context = alloc_pgtable_page(iommu->node);
814 if (!context)
815 return NULL;
816
817 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
818 phy_addr = virt_to_phys((void *)context);
819 *entry = phy_addr | 1;
820 __iommu_flush_cache(iommu, entry, sizeof(*entry));
821 }
822 return &context[devfn];
823}
824
David Woodhouse4ed6a542015-05-11 14:59:20 +0100825static int iommu_dummy(struct device *dev)
826{
827 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
828}
829
David Woodhouse156baca2014-03-09 14:00:57 -0700830static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800831{
832 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800833 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700834 struct device *tmp;
835 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800836 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800837 int i;
838
David Woodhouse4ed6a542015-05-11 14:59:20 +0100839 if (iommu_dummy(dev))
840 return NULL;
841
David Woodhouse156baca2014-03-09 14:00:57 -0700842 if (dev_is_pci(dev)) {
Ashok Raj1c387182016-10-21 15:32:05 -0700843 struct pci_dev *pf_pdev;
844
David Woodhouse156baca2014-03-09 14:00:57 -0700845 pdev = to_pci_dev(dev);
Jon Derrick5823e332017-08-30 15:05:59 -0600846
847#ifdef CONFIG_X86
848 /* VMD child devices currently cannot be handled individually */
849 if (is_vmd(pdev->bus))
850 return NULL;
851#endif
852
Ashok Raj1c387182016-10-21 15:32:05 -0700853 /* VFs aren't listed in scope tables; we need to look up
854 * the PF instead to find the IOMMU. */
855 pf_pdev = pci_physfn(pdev);
856 dev = &pf_pdev->dev;
David Woodhouse156baca2014-03-09 14:00:57 -0700857 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100858 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700859 dev = &ACPI_COMPANION(dev)->dev;
860
Jiang Liu0e242612014-02-19 14:07:34 +0800861 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800862 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700863 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100864 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800865
Jiang Liub683b232014-02-19 14:07:32 +0800866 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700867 drhd->devices_cnt, i, tmp) {
868 if (tmp == dev) {
Ashok Raj1c387182016-10-21 15:32:05 -0700869 /* For a VF use its original BDF# not that of the PF
870 * which we used for the IOMMU lookup. Strictly speaking
871 * we could do this for all PCI devices; we only need to
872 * get the BDF# from the scope table for ACPI matches. */
Koos Vriezen5003ae12017-03-01 21:02:50 +0100873 if (pdev && pdev->is_virtfn)
Ashok Raj1c387182016-10-21 15:32:05 -0700874 goto got_pdev;
875
David Woodhouse156baca2014-03-09 14:00:57 -0700876 *bus = drhd->devices[i].bus;
877 *devfn = drhd->devices[i].devfn;
878 goto out;
879 }
880
881 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000882 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700883
884 ptmp = to_pci_dev(tmp);
885 if (ptmp->subordinate &&
886 ptmp->subordinate->number <= pdev->bus->number &&
887 ptmp->subordinate->busn_res.end >= pdev->bus->number)
888 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100889 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800890
David Woodhouse156baca2014-03-09 14:00:57 -0700891 if (pdev && drhd->include_all) {
892 got_pdev:
893 *bus = pdev->bus->number;
894 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800895 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700896 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800897 }
Jiang Liub683b232014-02-19 14:07:32 +0800898 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700899 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800900 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800901
Jiang Liub683b232014-02-19 14:07:32 +0800902 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800903}
904
Weidong Han5331fe62008-12-08 23:00:00 +0800905static void domain_flush_cache(struct dmar_domain *domain,
906 void *addr, int size)
907{
908 if (!domain->iommu_coherency)
909 clflush_cache_range(addr, size);
910}
911
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700912static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
913{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700914 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000915 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700916 unsigned long flags;
917
918 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000919 context = iommu_context_addr(iommu, bus, devfn, 0);
920 if (context)
921 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700922 spin_unlock_irqrestore(&iommu->lock, flags);
923 return ret;
924}
925
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700926static void free_context_table(struct intel_iommu *iommu)
927{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700928 int i;
929 unsigned long flags;
930 struct context_entry *context;
931
932 spin_lock_irqsave(&iommu->lock, flags);
933 if (!iommu->root_entry) {
934 goto out;
935 }
936 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000937 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700938 if (context)
939 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000940
David Woodhousec83b2f22015-06-12 10:15:49 +0100941 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000942 continue;
943
944 context = iommu_context_addr(iommu, i, 0x80, 0);
945 if (context)
946 free_pgtable_page(context);
947
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700948 }
949 free_pgtable_page(iommu->root_entry);
950 iommu->root_entry = NULL;
951out:
952 spin_unlock_irqrestore(&iommu->lock, flags);
953}
954
David Woodhouseb026fd22009-06-28 10:37:25 +0100955static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000956 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700957{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700958 struct dma_pte *parent, *pte = NULL;
959 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700960 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700961
962 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200963
Jiang Liu162d1b12014-07-11 14:19:35 +0800964 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200965 /* Address beyond IOMMU's addressing capabilities. */
966 return NULL;
967
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968 parent = domain->pgd;
969
David Woodhouse5cf0a762014-03-19 16:07:49 +0000970 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700971 void *tmp_page;
972
David Woodhouseb026fd22009-06-28 10:37:25 +0100973 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700974 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000975 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100976 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000977 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700978 break;
979
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000980 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100981 uint64_t pteval;
982
Suresh Siddha4c923d42009-10-02 11:01:24 -0700983 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700984
David Woodhouse206a73c2009-07-01 19:30:28 +0100985 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700986 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +0100987
David Woodhousec85994e2009-07-01 19:21:24 +0100988 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400989 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 +0800990 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100991 /* Someone else set it while we were thinking; use theirs. */
992 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800993 else
David Woodhousec85994e2009-07-01 19:21:24 +0100994 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000996 if (level == 1)
997 break;
998
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000999 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001000 level--;
1001 }
1002
David Woodhouse5cf0a762014-03-19 16:07:49 +00001003 if (!*target_level)
1004 *target_level = level;
1005
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001006 return pte;
1007}
1008
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001009
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001010/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +01001011static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
1012 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001013 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001014{
1015 struct dma_pte *parent, *pte = NULL;
1016 int total = agaw_to_level(domain->agaw);
1017 int offset;
1018
1019 parent = domain->pgd;
1020 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +01001021 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001022 pte = &parent[offset];
1023 if (level == total)
1024 return pte;
1025
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001026 if (!dma_pte_present(pte)) {
1027 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001028 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001029 }
1030
Yijing Wange16922a2014-05-20 20:37:51 +08001031 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001032 *large_page = total;
1033 return pte;
1034 }
1035
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001036 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001037 total--;
1038 }
1039 return NULL;
1040}
1041
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001042/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001043static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf52009-06-27 22:09:11 +01001044 unsigned long start_pfn,
1045 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001046{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001047 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001048 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001049
Jiang Liu162d1b12014-07-11 14:19:35 +08001050 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1051 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001052 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001053
David Woodhouse04b18e62009-06-27 19:15:01 +01001054 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001055 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001056 large_page = 1;
1057 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001058 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001059 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001060 continue;
1061 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001062 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001063 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001064 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001065 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001066 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1067
David Woodhouse310a5ab2009-06-28 18:52:20 +01001068 domain_flush_cache(domain, first_pte,
1069 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001070
1071 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001072}
1073
Alex Williamson3269ee02013-06-15 10:27:19 -06001074static void dma_pte_free_level(struct dmar_domain *domain, int level,
David Dillowbc24c572017-06-28 19:42:23 -07001075 int retain_level, struct dma_pte *pte,
1076 unsigned long pfn, unsigned long start_pfn,
1077 unsigned long last_pfn)
Alex Williamson3269ee02013-06-15 10:27:19 -06001078{
1079 pfn = max(start_pfn, pfn);
1080 pte = &pte[pfn_level_offset(pfn, level)];
1081
1082 do {
1083 unsigned long level_pfn;
1084 struct dma_pte *level_pte;
1085
1086 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1087 goto next;
1088
David Dillowf7116e12017-01-30 19:11:11 -08001089 level_pfn = pfn & level_mask(level);
Alex Williamson3269ee02013-06-15 10:27:19 -06001090 level_pte = phys_to_virt(dma_pte_addr(pte));
1091
David Dillowbc24c572017-06-28 19:42:23 -07001092 if (level > 2) {
1093 dma_pte_free_level(domain, level - 1, retain_level,
1094 level_pte, level_pfn, start_pfn,
1095 last_pfn);
1096 }
Alex Williamson3269ee02013-06-15 10:27:19 -06001097
David Dillowbc24c572017-06-28 19:42:23 -07001098 /*
1099 * Free the page table if we're below the level we want to
1100 * retain and the range covers the entire table.
1101 */
1102 if (level < retain_level && !(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001103 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001104 dma_clear_pte(pte);
1105 domain_flush_cache(domain, pte, sizeof(*pte));
1106 free_pgtable_page(level_pte);
1107 }
1108next:
1109 pfn += level_size(level);
1110 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1111}
1112
David Dillowbc24c572017-06-28 19:42:23 -07001113/*
1114 * clear last level (leaf) ptes and free page table pages below the
1115 * level we wish to keep intact.
1116 */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001117static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001118 unsigned long start_pfn,
David Dillowbc24c572017-06-28 19:42:23 -07001119 unsigned long last_pfn,
1120 int retain_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001121{
Jiang Liu162d1b12014-07-11 14:19:35 +08001122 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1123 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001124 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001125
Jiang Liud41a4ad2014-07-11 14:19:34 +08001126 dma_pte_clear_range(domain, start_pfn, last_pfn);
1127
David Woodhousef3a0a522009-06-30 03:40:07 +01001128 /* We don't need lock here; nobody else touches the iova range */
David Dillowbc24c572017-06-28 19:42:23 -07001129 dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level,
Alex Williamson3269ee02013-06-15 10:27:19 -06001130 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001131
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001132 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001133 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001134 free_pgtable_page(domain->pgd);
1135 domain->pgd = NULL;
1136 }
1137}
1138
David Woodhouseea8ea462014-03-05 17:09:32 +00001139/* When a page at a given level is being unlinked from its parent, we don't
1140 need to *modify* it at all. All we need to do is make a list of all the
1141 pages which can be freed just as soon as we've flushed the IOTLB and we
1142 know the hardware page-walk will no longer touch them.
1143 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1144 be freed. */
1145static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1146 int level, struct dma_pte *pte,
1147 struct page *freelist)
1148{
1149 struct page *pg;
1150
1151 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1152 pg->freelist = freelist;
1153 freelist = pg;
1154
1155 if (level == 1)
1156 return freelist;
1157
Jiang Liuadeb2592014-04-09 10:20:39 +08001158 pte = page_address(pg);
1159 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001160 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1161 freelist = dma_pte_list_pagetables(domain, level - 1,
1162 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001163 pte++;
1164 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001165
1166 return freelist;
1167}
1168
1169static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1170 struct dma_pte *pte, unsigned long pfn,
1171 unsigned long start_pfn,
1172 unsigned long last_pfn,
1173 struct page *freelist)
1174{
1175 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1176
1177 pfn = max(start_pfn, pfn);
1178 pte = &pte[pfn_level_offset(pfn, level)];
1179
1180 do {
1181 unsigned long level_pfn;
1182
1183 if (!dma_pte_present(pte))
1184 goto next;
1185
1186 level_pfn = pfn & level_mask(level);
1187
1188 /* If range covers entire pagetable, free it */
1189 if (start_pfn <= level_pfn &&
1190 last_pfn >= level_pfn + level_size(level) - 1) {
1191 /* These suborbinate page tables are going away entirely. Don't
1192 bother to clear them; we're just going to *free* them. */
1193 if (level > 1 && !dma_pte_superpage(pte))
1194 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1195
1196 dma_clear_pte(pte);
1197 if (!first_pte)
1198 first_pte = pte;
1199 last_pte = pte;
1200 } else if (level > 1) {
1201 /* Recurse down into a level that isn't *entirely* obsolete */
1202 freelist = dma_pte_clear_level(domain, level - 1,
1203 phys_to_virt(dma_pte_addr(pte)),
1204 level_pfn, start_pfn, last_pfn,
1205 freelist);
1206 }
1207next:
1208 pfn += level_size(level);
1209 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1210
1211 if (first_pte)
1212 domain_flush_cache(domain, first_pte,
1213 (void *)++last_pte - (void *)first_pte);
1214
1215 return freelist;
1216}
1217
1218/* We can't just free the pages because the IOMMU may still be walking
1219 the page tables, and may have cached the intermediate levels. The
1220 pages can only be freed after the IOTLB flush has been done. */
Joerg Roedelb6904202015-08-13 11:32:18 +02001221static struct page *domain_unmap(struct dmar_domain *domain,
1222 unsigned long start_pfn,
1223 unsigned long last_pfn)
David Woodhouseea8ea462014-03-05 17:09:32 +00001224{
David Woodhouseea8ea462014-03-05 17:09:32 +00001225 struct page *freelist = NULL;
1226
Jiang Liu162d1b12014-07-11 14:19:35 +08001227 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1228 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001229 BUG_ON(start_pfn > last_pfn);
1230
1231 /* we don't need lock here; nobody else touches the iova range */
1232 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1233 domain->pgd, 0, start_pfn, last_pfn, NULL);
1234
1235 /* free pgd */
1236 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1237 struct page *pgd_page = virt_to_page(domain->pgd);
1238 pgd_page->freelist = freelist;
1239 freelist = pgd_page;
1240
1241 domain->pgd = NULL;
1242 }
1243
1244 return freelist;
1245}
1246
Joerg Roedelb6904202015-08-13 11:32:18 +02001247static void dma_free_pagelist(struct page *freelist)
David Woodhouseea8ea462014-03-05 17:09:32 +00001248{
1249 struct page *pg;
1250
1251 while ((pg = freelist)) {
1252 freelist = pg->freelist;
1253 free_pgtable_page(page_address(pg));
1254 }
1255}
1256
Joerg Roedel13cf0172017-08-11 11:40:10 +02001257static void iova_entry_free(unsigned long data)
1258{
1259 struct page *freelist = (struct page *)data;
1260
1261 dma_free_pagelist(freelist);
1262}
1263
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001264/* iommu handling */
1265static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1266{
1267 struct root_entry *root;
1268 unsigned long flags;
1269
Suresh Siddha4c923d42009-10-02 11:01:24 -07001270 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001271 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001272 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001273 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001274 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001275 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001276
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001277 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001278
1279 spin_lock_irqsave(&iommu->lock, flags);
1280 iommu->root_entry = root;
1281 spin_unlock_irqrestore(&iommu->lock, flags);
1282
1283 return 0;
1284}
1285
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001286static void iommu_set_root_entry(struct intel_iommu *iommu)
1287{
David Woodhouse03ecc322015-02-13 14:35:21 +00001288 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001289 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001290 unsigned long flag;
1291
David Woodhouse03ecc322015-02-13 14:35:21 +00001292 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001293 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001294 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001295
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001296 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001297 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001298
David Woodhousec416daa2009-05-10 20:30:58 +01001299 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001300
1301 /* Make sure hardware complete it */
1302 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001303 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001304
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001305 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001306}
1307
1308static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1309{
1310 u32 val;
1311 unsigned long flag;
1312
David Woodhouse9af88142009-02-13 23:18:03 +00001313 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001314 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001315
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001316 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001317 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001318
1319 /* Make sure hardware complete it */
1320 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001321 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001322
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001323 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001324}
1325
1326/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001327static void __iommu_flush_context(struct intel_iommu *iommu,
1328 u16 did, u16 source_id, u8 function_mask,
1329 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001330{
1331 u64 val = 0;
1332 unsigned long flag;
1333
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001334 switch (type) {
1335 case DMA_CCMD_GLOBAL_INVL:
1336 val = DMA_CCMD_GLOBAL_INVL;
1337 break;
1338 case DMA_CCMD_DOMAIN_INVL:
1339 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1340 break;
1341 case DMA_CCMD_DEVICE_INVL:
1342 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1343 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1344 break;
1345 default:
1346 BUG();
1347 }
1348 val |= DMA_CCMD_ICC;
1349
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001350 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001351 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1352
1353 /* Make sure hardware complete it */
1354 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1355 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1356
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001357 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001358}
1359
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001360/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001361static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1362 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001363{
1364 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1365 u64 val = 0, val_iva = 0;
1366 unsigned long flag;
1367
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001368 switch (type) {
1369 case DMA_TLB_GLOBAL_FLUSH:
1370 /* global flush doesn't need set IVA_REG */
1371 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1372 break;
1373 case DMA_TLB_DSI_FLUSH:
1374 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1375 break;
1376 case DMA_TLB_PSI_FLUSH:
1377 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001378 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001379 val_iva = size_order | addr;
1380 break;
1381 default:
1382 BUG();
1383 }
1384 /* Note: set drain read/write */
1385#if 0
1386 /*
1387 * This is probably to be super secure.. Looks like we can
1388 * ignore it without any impact.
1389 */
1390 if (cap_read_drain(iommu->cap))
1391 val |= DMA_TLB_READ_DRAIN;
1392#endif
1393 if (cap_write_drain(iommu->cap))
1394 val |= DMA_TLB_WRITE_DRAIN;
1395
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001396 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001397 /* Note: Only uses first TLB reg currently */
1398 if (val_iva)
1399 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1400 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1401
1402 /* Make sure hardware complete it */
1403 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1404 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1405
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001406 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001407
1408 /* check IOTLB invalidation granularity */
1409 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001410 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001411 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001412 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001413 (unsigned long long)DMA_TLB_IIRG(type),
1414 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001415}
1416
David Woodhouse64ae8922014-03-09 12:52:30 -07001417static struct device_domain_info *
1418iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1419 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001420{
Yu Zhao93a23a72009-05-18 13:51:37 +08001421 struct device_domain_info *info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001422
Joerg Roedel55d94042015-07-22 16:50:40 +02001423 assert_spin_locked(&device_domain_lock);
1424
Yu Zhao93a23a72009-05-18 13:51:37 +08001425 if (!iommu->qi)
1426 return NULL;
1427
Yu Zhao93a23a72009-05-18 13:51:37 +08001428 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001429 if (info->iommu == iommu && info->bus == bus &&
1430 info->devfn == devfn) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001431 if (info->ats_supported && info->dev)
1432 return info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001433 break;
1434 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001435
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001436 return NULL;
Yu Zhao93a23a72009-05-18 13:51:37 +08001437}
1438
Omer Peleg0824c592016-04-20 19:03:35 +03001439static void domain_update_iotlb(struct dmar_domain *domain)
1440{
1441 struct device_domain_info *info;
1442 bool has_iotlb_device = false;
1443
1444 assert_spin_locked(&device_domain_lock);
1445
1446 list_for_each_entry(info, &domain->devices, link) {
1447 struct pci_dev *pdev;
1448
1449 if (!info->dev || !dev_is_pci(info->dev))
1450 continue;
1451
1452 pdev = to_pci_dev(info->dev);
1453 if (pdev->ats_enabled) {
1454 has_iotlb_device = true;
1455 break;
1456 }
1457 }
1458
1459 domain->has_iotlb_device = has_iotlb_device;
1460}
1461
Yu Zhao93a23a72009-05-18 13:51:37 +08001462static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1463{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001464 struct pci_dev *pdev;
1465
Omer Peleg0824c592016-04-20 19:03:35 +03001466 assert_spin_locked(&device_domain_lock);
1467
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001468 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001469 return;
1470
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001471 pdev = to_pci_dev(info->dev);
Jacob Pan1c48db42018-06-07 09:57:00 -07001472 /* For IOMMU that supports device IOTLB throttling (DIT), we assign
1473 * PFSID to the invalidation desc of a VF such that IOMMU HW can gauge
1474 * queue depth at PF level. If DIT is not set, PFSID will be treated as
1475 * reserved, which should be set to 0.
1476 */
1477 if (!ecap_dit(info->iommu->ecap))
1478 info->pfsid = 0;
1479 else {
1480 struct pci_dev *pf_pdev;
1481
1482 /* pdev will be returned if device is not a vf */
1483 pf_pdev = pci_physfn(pdev);
1484 info->pfsid = PCI_DEVID(pf_pdev->bus->number, pf_pdev->devfn);
1485 }
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001486
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001487#ifdef CONFIG_INTEL_IOMMU_SVM
1488 /* The PCIe spec, in its wisdom, declares that the behaviour of
1489 the device if you enable PASID support after ATS support is
1490 undefined. So always enable PASID support on devices which
1491 have it, even if we can't yet know if we're ever going to
1492 use it. */
1493 if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
1494 info->pasid_enabled = 1;
1495
1496 if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
1497 info->pri_enabled = 1;
1498#endif
1499 if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
1500 info->ats_enabled = 1;
Omer Peleg0824c592016-04-20 19:03:35 +03001501 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001502 info->ats_qdep = pci_ats_queue_depth(pdev);
1503 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001504}
1505
1506static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1507{
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001508 struct pci_dev *pdev;
1509
Omer Peleg0824c592016-04-20 19:03:35 +03001510 assert_spin_locked(&device_domain_lock);
1511
Jeremy McNicollda972fb2016-01-14 21:33:06 -08001512 if (!dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001513 return;
1514
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001515 pdev = to_pci_dev(info->dev);
1516
1517 if (info->ats_enabled) {
1518 pci_disable_ats(pdev);
1519 info->ats_enabled = 0;
Omer Peleg0824c592016-04-20 19:03:35 +03001520 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001521 }
1522#ifdef CONFIG_INTEL_IOMMU_SVM
1523 if (info->pri_enabled) {
1524 pci_disable_pri(pdev);
1525 info->pri_enabled = 0;
1526 }
1527 if (info->pasid_enabled) {
1528 pci_disable_pasid(pdev);
1529 info->pasid_enabled = 0;
1530 }
1531#endif
Yu Zhao93a23a72009-05-18 13:51:37 +08001532}
1533
1534static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1535 u64 addr, unsigned mask)
1536{
1537 u16 sid, qdep;
1538 unsigned long flags;
1539 struct device_domain_info *info;
1540
Omer Peleg0824c592016-04-20 19:03:35 +03001541 if (!domain->has_iotlb_device)
1542 return;
1543
Yu Zhao93a23a72009-05-18 13:51:37 +08001544 spin_lock_irqsave(&device_domain_lock, flags);
1545 list_for_each_entry(info, &domain->devices, link) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001546 if (!info->ats_enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001547 continue;
1548
1549 sid = info->bus << 8 | info->devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001550 qdep = info->ats_qdep;
Jacob Pan1c48db42018-06-07 09:57:00 -07001551 qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
1552 qdep, addr, mask);
Yu Zhao93a23a72009-05-18 13:51:37 +08001553 }
1554 spin_unlock_irqrestore(&device_domain_lock, flags);
1555}
1556
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001557static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1558 struct dmar_domain *domain,
1559 unsigned long pfn, unsigned int pages,
1560 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001561{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001562 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001563 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001564 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001565
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001566 BUG_ON(pages == 0);
1567
David Woodhouseea8ea462014-03-05 17:09:32 +00001568 if (ih)
1569 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001570 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001571 * Fallback to domain selective flush if no PSI support or the size is
1572 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001573 * PSI requires page size to be 2 ^ x, and the base address is naturally
1574 * aligned to the size
1575 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001576 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1577 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001578 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001579 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001580 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001581 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001582
1583 /*
Nadav Amit82653632010-04-01 13:24:40 +03001584 * In caching mode, changes of pages from non-present to present require
1585 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001586 */
Nadav Amit82653632010-04-01 13:24:40 +03001587 if (!cap_caching_mode(iommu->cap) || !map)
Peter Xu9d2e6502018-01-10 13:51:37 +08001588 iommu_flush_dev_iotlb(domain, addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001589}
1590
Peter Xueed91a02018-05-04 10:34:52 +08001591/* Notification for newly created mappings */
1592static inline void __mapping_notify_one(struct intel_iommu *iommu,
1593 struct dmar_domain *domain,
1594 unsigned long pfn, unsigned int pages)
1595{
1596 /* It's a non-present to present mapping. Only flush if caching mode */
1597 if (cap_caching_mode(iommu->cap))
1598 iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1);
1599 else
1600 iommu_flush_write_buffer(iommu);
1601}
1602
Joerg Roedel13cf0172017-08-11 11:40:10 +02001603static void iommu_flush_iova(struct iova_domain *iovad)
1604{
1605 struct dmar_domain *domain;
1606 int idx;
1607
1608 domain = container_of(iovad, struct dmar_domain, iovad);
1609
1610 for_each_domain_iommu(idx, domain) {
1611 struct intel_iommu *iommu = g_iommus[idx];
1612 u16 did = domain->iommu_did[iommu->seq_id];
1613
1614 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
1615
1616 if (!cap_caching_mode(iommu->cap))
1617 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1618 0, MAX_AGAW_PFN_WIDTH);
1619 }
1620}
1621
mark grossf8bab732008-02-08 04:18:38 -08001622static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1623{
1624 u32 pmen;
1625 unsigned long flags;
1626
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001627 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001628 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1629 pmen &= ~DMA_PMEN_EPM;
1630 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1631
1632 /* wait for the protected region status bit to clear */
1633 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1634 readl, !(pmen & DMA_PMEN_PRS), pmen);
1635
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001636 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001637}
1638
Jiang Liu2a41cce2014-07-11 14:19:33 +08001639static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001640{
1641 u32 sts;
1642 unsigned long flags;
1643
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001644 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001645 iommu->gcmd |= DMA_GCMD_TE;
1646 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001647
1648 /* Make sure hardware complete it */
1649 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001650 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001651
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001652 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001653}
1654
Jiang Liu2a41cce2014-07-11 14:19:33 +08001655static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001656{
1657 u32 sts;
1658 unsigned long flag;
1659
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001660 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001661 iommu->gcmd &= ~DMA_GCMD_TE;
1662 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1663
1664 /* Make sure hardware complete it */
1665 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001666 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001667
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001668 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001669}
1670
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001671
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001672static int iommu_init_domains(struct intel_iommu *iommu)
1673{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001674 u32 ndomains, nlongs;
1675 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001676
1677 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001678 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001679 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001680 nlongs = BITS_TO_LONGS(ndomains);
1681
Donald Dutile94a91b502009-08-20 16:51:34 -04001682 spin_lock_init(&iommu->lock);
1683
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001684 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1685 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001686 pr_err("%s: Allocating domain id array failed\n",
1687 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001688 return -ENOMEM;
1689 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001690
Wei Yang86f004c2016-05-21 02:41:51 +00001691 size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001692 iommu->domains = kzalloc(size, GFP_KERNEL);
1693
1694 if (iommu->domains) {
1695 size = 256 * sizeof(struct dmar_domain *);
1696 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1697 }
1698
1699 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001700 pr_err("%s: Allocating domain array failed\n",
1701 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001702 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001703 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001704 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001705 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001706 return -ENOMEM;
1707 }
1708
Joerg Roedel8bf47812015-07-21 10:41:21 +02001709
1710
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001711 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001712 * If Caching mode is set, then invalid translations are tagged
1713 * with domain-id 0, hence we need to pre-allocate it. We also
1714 * use domain-id 0 as a marker for non-allocated domain-id, so
1715 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001716 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001717 set_bit(0, iommu->domain_ids);
1718
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001719 return 0;
1720}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001721
Jiang Liuffebeb42014-11-09 22:48:02 +08001722static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001723{
Joerg Roedel29a27712015-07-21 17:17:12 +02001724 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001725 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001726
Joerg Roedel29a27712015-07-21 17:17:12 +02001727 if (!iommu->domains || !iommu->domain_ids)
1728 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001729
Joerg Roedelbea64032016-11-08 15:08:26 +01001730again:
Joerg Roedel55d94042015-07-22 16:50:40 +02001731 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001732 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1733 struct dmar_domain *domain;
1734
1735 if (info->iommu != iommu)
1736 continue;
1737
1738 if (!info->dev || !info->domain)
1739 continue;
1740
1741 domain = info->domain;
1742
Joerg Roedelbea64032016-11-08 15:08:26 +01001743 __dmar_remove_one_dev_info(info);
Joerg Roedel29a27712015-07-21 17:17:12 +02001744
Joerg Roedelbea64032016-11-08 15:08:26 +01001745 if (!domain_type_is_vm_or_si(domain)) {
1746 /*
1747 * The domain_exit() function can't be called under
1748 * device_domain_lock, as it takes this lock itself.
1749 * So release the lock here and re-run the loop
1750 * afterwards.
1751 */
1752 spin_unlock_irqrestore(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001753 domain_exit(domain);
Joerg Roedelbea64032016-11-08 15:08:26 +01001754 goto again;
1755 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001756 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001757 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001758
1759 if (iommu->gcmd & DMA_GCMD_TE)
1760 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001761}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001762
Jiang Liuffebeb42014-11-09 22:48:02 +08001763static void free_dmar_iommu(struct intel_iommu *iommu)
1764{
1765 if ((iommu->domains) && (iommu->domain_ids)) {
Wei Yang86f004c2016-05-21 02:41:51 +00001766 int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001767 int i;
1768
1769 for (i = 0; i < elems; i++)
1770 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001771 kfree(iommu->domains);
1772 kfree(iommu->domain_ids);
1773 iommu->domains = NULL;
1774 iommu->domain_ids = NULL;
1775 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001776
Weidong Hand9630fe2008-12-08 11:06:32 +08001777 g_iommus[iommu->seq_id] = NULL;
1778
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001779 /* free context mapping */
1780 free_context_table(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001781
1782#ifdef CONFIG_INTEL_IOMMU_SVM
David Woodhousea222a7f2015-10-07 23:35:18 +01001783 if (pasid_enabled(iommu)) {
1784 if (ecap_prs(iommu->ecap))
1785 intel_svm_finish_prq(iommu);
Lu Baolud9737952018-07-14 15:47:02 +08001786 intel_svm_exit(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01001787 }
David Woodhouse8a94ade2015-03-24 14:54:56 +00001788#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001789}
1790
Jiang Liuab8dfe22014-07-11 14:19:27 +08001791static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001792{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001793 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001794
1795 domain = alloc_domain_mem();
1796 if (!domain)
1797 return NULL;
1798
Jiang Liuab8dfe22014-07-11 14:19:27 +08001799 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001800 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001801 domain->flags = flags;
Omer Peleg0824c592016-04-20 19:03:35 +03001802 domain->has_iotlb_device = false;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001803 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001804
1805 return domain;
1806}
1807
Joerg Roedeld160aca2015-07-22 11:52:53 +02001808/* Must be called with iommu->lock */
1809static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001810 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001811{
Jiang Liu44bde612014-07-11 14:19:29 +08001812 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001813 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001814
Joerg Roedel55d94042015-07-22 16:50:40 +02001815 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001816 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001817
Joerg Roedel29a27712015-07-21 17:17:12 +02001818 domain->iommu_refcnt[iommu->seq_id] += 1;
1819 domain->iommu_count += 1;
1820 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001821 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001822 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1823
1824 if (num >= ndomains) {
1825 pr_err("%s: No free domain ids\n", iommu->name);
1826 domain->iommu_refcnt[iommu->seq_id] -= 1;
1827 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001828 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001829 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001830
Joerg Roedeld160aca2015-07-22 11:52:53 +02001831 set_bit(num, iommu->domain_ids);
1832 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001833
Joerg Roedeld160aca2015-07-22 11:52:53 +02001834 domain->iommu_did[iommu->seq_id] = num;
1835 domain->nid = iommu->node;
1836
Jiang Liufb170fb2014-07-11 14:19:28 +08001837 domain_update_iommu_cap(domain);
1838 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001839
Joerg Roedel55d94042015-07-22 16:50:40 +02001840 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001841}
1842
1843static int domain_detach_iommu(struct dmar_domain *domain,
1844 struct intel_iommu *iommu)
1845{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001846 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001847
Joerg Roedel55d94042015-07-22 16:50:40 +02001848 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001849 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001850
Joerg Roedel29a27712015-07-21 17:17:12 +02001851 domain->iommu_refcnt[iommu->seq_id] -= 1;
1852 count = --domain->iommu_count;
1853 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001854 num = domain->iommu_did[iommu->seq_id];
1855 clear_bit(num, iommu->domain_ids);
1856 set_iommu_domain(iommu, num, NULL);
1857
Jiang Liufb170fb2014-07-11 14:19:28 +08001858 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001859 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001860 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001861
1862 return count;
1863}
1864
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001865static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001866static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001867
Joseph Cihula51a63e62011-03-21 11:04:24 -07001868static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869{
1870 struct pci_dev *pdev = NULL;
1871 struct iova *iova;
1872 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001873
Zhen Leiaa3ac942017-09-21 16:52:45 +01001874 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001875
Mark Gross8a443df2008-03-04 14:59:31 -08001876 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1877 &reserved_rbtree_key);
1878
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001879 /* IOAPIC ranges shouldn't be accessed by DMA */
1880 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1881 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001882 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001883 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001884 return -ENODEV;
1885 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001886
1887 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1888 for_each_pci_dev(pdev) {
1889 struct resource *r;
1890
1891 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1892 r = &pdev->resource[i];
1893 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1894 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001895 iova = reserve_iova(&reserved_iova_list,
1896 IOVA_PFN(r->start),
1897 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001898 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001899 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001900 return -ENODEV;
1901 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001902 }
1903 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001904 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001905}
1906
1907static void domain_reserve_special_ranges(struct dmar_domain *domain)
1908{
1909 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1910}
1911
1912static inline int guestwidth_to_adjustwidth(int gaw)
1913{
1914 int agaw;
1915 int r = (gaw - 12) % 9;
1916
1917 if (r == 0)
1918 agaw = gaw;
1919 else
1920 agaw = gaw + 9 - r;
1921 if (agaw > 64)
1922 agaw = 64;
1923 return agaw;
1924}
1925
Joerg Roedeldc534b22015-07-22 12:44:02 +02001926static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1927 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001928{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001929 int adjust_width, agaw;
1930 unsigned long sagaw;
Joerg Roedel13cf0172017-08-11 11:40:10 +02001931 int err;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001932
Zhen Leiaa3ac942017-09-21 16:52:45 +01001933 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
Joerg Roedel13cf0172017-08-11 11:40:10 +02001934
1935 err = init_iova_flush_queue(&domain->iovad,
1936 iommu_flush_iova, iova_entry_free);
1937 if (err)
1938 return err;
1939
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001940 domain_reserve_special_ranges(domain);
1941
1942 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001943 if (guest_width > cap_mgaw(iommu->cap))
1944 guest_width = cap_mgaw(iommu->cap);
1945 domain->gaw = guest_width;
1946 adjust_width = guestwidth_to_adjustwidth(guest_width);
1947 agaw = width_to_agaw(adjust_width);
1948 sagaw = cap_sagaw(iommu->cap);
1949 if (!test_bit(agaw, &sagaw)) {
1950 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001951 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001952 agaw = find_next_bit(&sagaw, 5, agaw);
1953 if (agaw >= 5)
1954 return -ENODEV;
1955 }
1956 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001957
Weidong Han8e6040972008-12-08 15:49:06 +08001958 if (ecap_coherent(iommu->ecap))
1959 domain->iommu_coherency = 1;
1960 else
1961 domain->iommu_coherency = 0;
1962
Sheng Yang58c610b2009-03-18 15:33:05 +08001963 if (ecap_sc_support(iommu->ecap))
1964 domain->iommu_snooping = 1;
1965 else
1966 domain->iommu_snooping = 0;
1967
David Woodhouse214e39a2014-03-19 10:38:49 +00001968 if (intel_iommu_superpage)
1969 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1970 else
1971 domain->iommu_superpage = 0;
1972
Suresh Siddha4c923d42009-10-02 11:01:24 -07001973 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001974
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001975 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001976 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001977 if (!domain->pgd)
1978 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001979 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001980 return 0;
1981}
1982
1983static void domain_exit(struct dmar_domain *domain)
1984{
David Woodhouseea8ea462014-03-05 17:09:32 +00001985 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001986
1987 /* Domain 0 is reserved, so dont process it */
1988 if (!domain)
1989 return;
1990
Joerg Roedeld160aca2015-07-22 11:52:53 +02001991 /* Remove associated devices and clear attached or cached domains */
1992 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001993 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001994 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001995
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001996 /* destroy iovas */
1997 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001998
David Woodhouseea8ea462014-03-05 17:09:32 +00001999 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002000
David Woodhouseea8ea462014-03-05 17:09:32 +00002001 dma_free_pagelist(freelist);
2002
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002003 free_domain_mem(domain);
2004}
2005
David Woodhouse64ae8922014-03-09 12:52:30 -07002006static int domain_context_mapping_one(struct dmar_domain *domain,
2007 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002008 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002009{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002010 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02002011 int translation = CONTEXT_TT_MULTI_LEVEL;
2012 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002013 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002014 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08002015 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02002016 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02002017
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002018 WARN_ON(did == 0);
2019
Joerg Roedel28ccce02015-07-21 14:45:31 +02002020 if (hw_pass_through && domain_type_is_si(domain))
2021 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002022
2023 pr_debug("Set context mapping for %02x:%02x.%d\n",
2024 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002025
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002026 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08002027
Joerg Roedel55d94042015-07-22 16:50:40 +02002028 spin_lock_irqsave(&device_domain_lock, flags);
2029 spin_lock(&iommu->lock);
2030
2031 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00002032 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002033 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02002034 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002035
Joerg Roedel55d94042015-07-22 16:50:40 +02002036 ret = 0;
2037 if (context_present(context))
2038 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02002039
Xunlei Pangaec0e862016-12-05 20:09:07 +08002040 /*
2041 * For kdump cases, old valid entries may be cached due to the
2042 * in-flight DMA and copied pgtable, but there is no unmapping
2043 * behaviour for them, thus we need an explicit cache flush for
2044 * the newly-mapped device. For kdump, at this point, the device
2045 * is supposed to finish reset at its driver probe stage, so no
2046 * in-flight DMA will exist, and we don't need to worry anymore
2047 * hereafter.
2048 */
2049 if (context_copied(context)) {
2050 u16 did_old = context_domain_id(context);
2051
Christos Gkekasb117e032017-10-08 23:33:31 +01002052 if (did_old < cap_ndoms(iommu->cap)) {
Xunlei Pangaec0e862016-12-05 20:09:07 +08002053 iommu->flush.flush_context(iommu, did_old,
2054 (((u16)bus) << 8) | devfn,
2055 DMA_CCMD_MASK_NOBIT,
2056 DMA_CCMD_DEVICE_INVL);
KarimAllah Ahmedf73a7ee2017-05-05 11:39:59 -07002057 iommu->flush.flush_iotlb(iommu, did_old, 0, 0,
2058 DMA_TLB_DSI_FLUSH);
2059 }
Xunlei Pangaec0e862016-12-05 20:09:07 +08002060 }
2061
Weidong Hanea6606b2008-12-08 23:08:15 +08002062 pgd = domain->pgd;
2063
Joerg Roedelde24e552015-07-21 14:53:04 +02002064 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002065 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08002066
Joerg Roedelde24e552015-07-21 14:53:04 +02002067 /*
2068 * Skip top levels of page tables for iommu which has less agaw
2069 * than default. Unnecessary for PT mode.
2070 */
Yu Zhao93a23a72009-05-18 13:51:37 +08002071 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02002072 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02002073 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02002074 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02002075 if (!dma_pte_present(pgd))
2076 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02002077 }
2078
David Woodhouse64ae8922014-03-09 12:52:30 -07002079 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002080 if (info && info->ats_supported)
2081 translation = CONTEXT_TT_DEV_IOTLB;
2082 else
2083 translation = CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02002084
Yu Zhao93a23a72009-05-18 13:51:37 +08002085 context_set_address_root(context, virt_to_phys(pgd));
2086 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02002087 } else {
2088 /*
2089 * In pass through mode, AW must be programmed to
2090 * indicate the largest AGAW value supported by
2091 * hardware. And ASR is ignored by hardware.
2092 */
2093 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08002094 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002095
2096 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00002097 context_set_fault_enable(context);
2098 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08002099 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002100
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002101 /*
2102 * It's a non-present to present mapping. If hardware doesn't cache
2103 * non-present entry we only need to flush the write-buffer. If the
2104 * _does_ cache non-present entries, then it does so in the special
2105 * domain #0, which we have to flush:
2106 */
2107 if (cap_caching_mode(iommu->cap)) {
2108 iommu->flush.flush_context(iommu, 0,
2109 (((u16)bus) << 8) | devfn,
2110 DMA_CCMD_MASK_NOBIT,
2111 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002112 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002113 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002114 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002115 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002116 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08002117
Joerg Roedel55d94042015-07-22 16:50:40 +02002118 ret = 0;
2119
2120out_unlock:
2121 spin_unlock(&iommu->lock);
2122 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08002123
Wei Yang5c365d12016-07-13 13:53:21 +00002124 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002125}
2126
Alex Williamson579305f2014-07-03 09:51:43 -06002127struct domain_context_mapping_data {
2128 struct dmar_domain *domain;
2129 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002130};
2131
2132static int domain_context_mapping_cb(struct pci_dev *pdev,
2133 u16 alias, void *opaque)
2134{
2135 struct domain_context_mapping_data *data = opaque;
2136
2137 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002138 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002139}
2140
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002141static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002142domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002143{
David Woodhouse64ae8922014-03-09 12:52:30 -07002144 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002145 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002146 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002147
David Woodhousee1f167f2014-03-09 15:24:46 -07002148 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002149 if (!iommu)
2150 return -ENODEV;
2151
Alex Williamson579305f2014-07-03 09:51:43 -06002152 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002153 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002154
2155 data.domain = domain;
2156 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002157
2158 return pci_for_each_dma_alias(to_pci_dev(dev),
2159 &domain_context_mapping_cb, &data);
2160}
2161
2162static int domain_context_mapped_cb(struct pci_dev *pdev,
2163 u16 alias, void *opaque)
2164{
2165 struct intel_iommu *iommu = opaque;
2166
2167 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002168}
2169
David Woodhousee1f167f2014-03-09 15:24:46 -07002170static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002171{
Weidong Han5331fe62008-12-08 23:00:00 +08002172 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002173 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002174
David Woodhousee1f167f2014-03-09 15:24:46 -07002175 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002176 if (!iommu)
2177 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002178
Alex Williamson579305f2014-07-03 09:51:43 -06002179 if (!dev_is_pci(dev))
2180 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002181
Alex Williamson579305f2014-07-03 09:51:43 -06002182 return !pci_for_each_dma_alias(to_pci_dev(dev),
2183 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002184}
2185
Fenghua Yuf5329592009-08-04 15:09:37 -07002186/* Returns a number of VTD pages, but aligned to MM page size */
2187static inline unsigned long aligned_nrpages(unsigned long host_addr,
2188 size_t size)
2189{
2190 host_addr &= ~PAGE_MASK;
2191 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2192}
2193
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002194/* Return largest possible superpage level for a given mapping */
2195static inline int hardware_largepage_caps(struct dmar_domain *domain,
2196 unsigned long iov_pfn,
2197 unsigned long phy_pfn,
2198 unsigned long pages)
2199{
2200 int support, level = 1;
2201 unsigned long pfnmerge;
2202
2203 support = domain->iommu_superpage;
2204
2205 /* To use a large page, the virtual *and* physical addresses
2206 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2207 of them will mean we have to use smaller pages. So just
2208 merge them and check both at once. */
2209 pfnmerge = iov_pfn | phy_pfn;
2210
2211 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2212 pages >>= VTD_STRIDE_SHIFT;
2213 if (!pages)
2214 break;
2215 pfnmerge >>= VTD_STRIDE_SHIFT;
2216 level++;
2217 support--;
2218 }
2219 return level;
2220}
2221
David Woodhouse9051aa02009-06-29 12:30:54 +01002222static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2223 struct scatterlist *sg, unsigned long phys_pfn,
2224 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002225{
2226 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002227 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002228 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002229 unsigned int largepage_lvl = 0;
2230 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002231
Jiang Liu162d1b12014-07-11 14:19:35 +08002232 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002233
2234 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2235 return -EINVAL;
2236
2237 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2238
Jiang Liucc4f14a2014-11-26 09:42:10 +08002239 if (!sg) {
2240 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002241 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2242 }
2243
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002244 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002245 uint64_t tmp;
2246
David Woodhousee1605492009-06-29 11:17:38 +01002247 if (!sg_res) {
Robin Murphy29a90b72017-09-28 15:14:01 +01002248 unsigned int pgoff = sg->offset & ~PAGE_MASK;
2249
Fenghua Yuf5329592009-08-04 15:09:37 -07002250 sg_res = aligned_nrpages(sg->offset, sg->length);
Robin Murphy29a90b72017-09-28 15:14:01 +01002251 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff;
David Woodhousee1605492009-06-29 11:17:38 +01002252 sg->dma_length = sg->length;
Robin Murphy29a90b72017-09-28 15:14:01 +01002253 pteval = (sg_phys(sg) - pgoff) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002254 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002255 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002256
David Woodhousee1605492009-06-29 11:17:38 +01002257 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002258 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2259
David Woodhouse5cf0a762014-03-19 16:07:49 +00002260 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002261 if (!pte)
2262 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002263 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002264 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002265 unsigned long nr_superpages, end_pfn;
2266
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002267 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002268 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002269
2270 nr_superpages = sg_res / lvl_pages;
2271 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2272
Jiang Liud41a4ad2014-07-11 14:19:34 +08002273 /*
2274 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002275 * removed to make room for superpage(s).
David Dillowbc24c572017-06-28 19:42:23 -07002276 * We're adding new large pages, so make sure
2277 * we don't remove their parent tables.
Jiang Liud41a4ad2014-07-11 14:19:34 +08002278 */
David Dillowbc24c572017-06-28 19:42:23 -07002279 dma_pte_free_pagetable(domain, iov_pfn, end_pfn,
2280 largepage_lvl + 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002281 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002282 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002283 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002284
David Woodhousee1605492009-06-29 11:17:38 +01002285 }
2286 /* We don't need lock here, nobody else
2287 * touches the iova range
2288 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002289 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002290 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002291 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002292 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2293 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002294 if (dumps) {
2295 dumps--;
2296 debug_dma_dump_mappings(NULL);
2297 }
2298 WARN_ON(1);
2299 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002300
2301 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2302
2303 BUG_ON(nr_pages < lvl_pages);
2304 BUG_ON(sg_res < lvl_pages);
2305
2306 nr_pages -= lvl_pages;
2307 iov_pfn += lvl_pages;
2308 phys_pfn += lvl_pages;
2309 pteval += lvl_pages * VTD_PAGE_SIZE;
2310 sg_res -= lvl_pages;
2311
2312 /* If the next PTE would be the first in a new page, then we
2313 need to flush the cache on the entries we've just written.
2314 And then we'll need to recalculate 'pte', so clear it and
2315 let it get set again in the if (!pte) block above.
2316
2317 If we're done (!nr_pages) we need to flush the cache too.
2318
2319 Also if we've been setting superpages, we may need to
2320 recalculate 'pte' and switch back to smaller pages for the
2321 end of the mapping, if the trailing size is not enough to
2322 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002323 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002324 if (!nr_pages || first_pte_in_page(pte) ||
2325 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002326 domain_flush_cache(domain, first_pte,
2327 (void *)pte - (void *)first_pte);
2328 pte = NULL;
2329 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002330
2331 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002332 sg = sg_next(sg);
2333 }
2334 return 0;
2335}
2336
Peter Xu87684fd2018-05-04 10:34:53 +08002337static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2338 struct scatterlist *sg, unsigned long phys_pfn,
2339 unsigned long nr_pages, int prot)
2340{
2341 int ret;
2342 struct intel_iommu *iommu;
2343
2344 /* Do the real mapping first */
2345 ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot);
2346 if (ret)
2347 return ret;
2348
2349 /* Notify about the new mapping */
2350 if (domain_type_is_vm(domain)) {
2351 /* VM typed domains can have more than one IOMMUs */
2352 int iommu_id;
2353 for_each_domain_iommu(iommu_id, domain) {
2354 iommu = g_iommus[iommu_id];
2355 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2356 }
2357 } else {
2358 /* General domains only have one IOMMU */
2359 iommu = domain_get_iommu(domain);
2360 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2361 }
2362
2363 return 0;
2364}
2365
David Woodhouse9051aa02009-06-29 12:30:54 +01002366static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2367 struct scatterlist *sg, unsigned long nr_pages,
2368 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002369{
Peter Xu87684fd2018-05-04 10:34:53 +08002370 return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
David Woodhouse9051aa02009-06-29 12:30:54 +01002371}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002372
David Woodhouse9051aa02009-06-29 12:30:54 +01002373static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2374 unsigned long phys_pfn, unsigned long nr_pages,
2375 int prot)
2376{
Peter Xu87684fd2018-05-04 10:34:53 +08002377 return domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002378}
2379
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002380static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002381{
Filippo Sironi50822192017-08-31 10:58:11 +02002382 unsigned long flags;
2383 struct context_entry *context;
2384 u16 did_old;
2385
Weidong Hanc7151a82008-12-08 22:51:37 +08002386 if (!iommu)
2387 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002388
Filippo Sironi50822192017-08-31 10:58:11 +02002389 spin_lock_irqsave(&iommu->lock, flags);
2390 context = iommu_context_addr(iommu, bus, devfn, 0);
2391 if (!context) {
2392 spin_unlock_irqrestore(&iommu->lock, flags);
2393 return;
2394 }
2395 did_old = context_domain_id(context);
2396 context_clear_entry(context);
2397 __iommu_flush_cache(iommu, context, sizeof(*context));
2398 spin_unlock_irqrestore(&iommu->lock, flags);
2399 iommu->flush.flush_context(iommu,
2400 did_old,
2401 (((u16)bus) << 8) | devfn,
2402 DMA_CCMD_MASK_NOBIT,
2403 DMA_CCMD_DEVICE_INVL);
2404 iommu->flush.flush_iotlb(iommu,
2405 did_old,
2406 0,
2407 0,
2408 DMA_TLB_DSI_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002409}
2410
David Woodhouse109b9b02012-05-25 17:43:02 +01002411static inline void unlink_domain_info(struct device_domain_info *info)
2412{
2413 assert_spin_locked(&device_domain_lock);
2414 list_del(&info->link);
2415 list_del(&info->global);
2416 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002417 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002418}
2419
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002420static void domain_remove_dev_info(struct dmar_domain *domain)
2421{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002422 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002423 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002424
2425 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002426 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002427 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002428 spin_unlock_irqrestore(&device_domain_lock, flags);
2429}
2430
2431/*
2432 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002433 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002434 */
David Woodhouse1525a292014-03-06 16:19:30 +00002435static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002436{
2437 struct device_domain_info *info;
2438
2439 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002440 info = dev->archdata.iommu;
Peter Xub316d022017-05-22 18:28:51 +08002441 if (likely(info))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002442 return info->domain;
2443 return NULL;
2444}
2445
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002446static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002447dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2448{
2449 struct device_domain_info *info;
2450
2451 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002452 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002453 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002454 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002455
2456 return NULL;
2457}
2458
Joerg Roedel5db31562015-07-22 12:40:43 +02002459static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2460 int bus, int devfn,
2461 struct device *dev,
2462 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002463{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002464 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002465 struct device_domain_info *info;
2466 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002467 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002468
2469 info = alloc_devinfo_mem();
2470 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002471 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002472
Jiang Liu745f2582014-02-19 14:07:26 +08002473 info->bus = bus;
2474 info->devfn = devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002475 info->ats_supported = info->pasid_supported = info->pri_supported = 0;
2476 info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
2477 info->ats_qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002478 info->dev = dev;
2479 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002480 info->iommu = iommu;
Lu Baolucc580e42018-07-14 15:46:59 +08002481 info->pasid_table = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002482
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002483 if (dev && dev_is_pci(dev)) {
2484 struct pci_dev *pdev = to_pci_dev(info->dev);
2485
Gil Kupfercef74402018-05-10 17:56:02 -05002486 if (!pci_ats_disabled() &&
2487 ecap_dev_iotlb_support(iommu->ecap) &&
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002488 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
2489 dmar_find_matched_atsr_unit(pdev))
2490 info->ats_supported = 1;
2491
2492 if (ecs_enabled(iommu)) {
2493 if (pasid_enabled(iommu)) {
2494 int features = pci_pasid_features(pdev);
2495 if (features >= 0)
2496 info->pasid_supported = features | 1;
2497 }
2498
2499 if (info->ats_supported && ecap_prs(iommu->ecap) &&
2500 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
2501 info->pri_supported = 1;
2502 }
2503 }
2504
Jiang Liu745f2582014-02-19 14:07:26 +08002505 spin_lock_irqsave(&device_domain_lock, flags);
2506 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002507 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002508
2509 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002510 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002511 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002512 if (info2) {
2513 found = info2->domain;
2514 info2->dev = dev;
2515 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002516 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002517
Jiang Liu745f2582014-02-19 14:07:26 +08002518 if (found) {
2519 spin_unlock_irqrestore(&device_domain_lock, flags);
2520 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002521 /* Caller must free the original domain */
2522 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002523 }
2524
Joerg Roedeld160aca2015-07-22 11:52:53 +02002525 spin_lock(&iommu->lock);
2526 ret = domain_attach_iommu(domain, iommu);
2527 spin_unlock(&iommu->lock);
2528
2529 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002530 spin_unlock_irqrestore(&device_domain_lock, flags);
Sudip Mukherjee499f3aa2015-09-18 16:27:07 +05302531 free_devinfo_mem(info);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002532 return NULL;
2533 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002534
David Woodhouseb718cd32014-03-09 13:11:33 -07002535 list_add(&info->link, &domain->devices);
2536 list_add(&info->global, &device_domain_list);
2537 if (dev)
2538 dev->archdata.iommu = info;
Lu Baolua7fc93f2018-07-14 15:47:00 +08002539
2540 if (dev && dev_is_pci(dev) && info->pasid_supported) {
2541 ret = intel_pasid_alloc_table(dev);
2542 if (ret) {
2543 __dmar_remove_one_dev_info(info);
2544 spin_unlock_irqrestore(&device_domain_lock, flags);
2545 return NULL;
2546 }
2547 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002548 spin_unlock_irqrestore(&device_domain_lock, flags);
2549
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002550 if (dev && domain_context_mapping(domain, dev)) {
2551 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002552 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002553 return NULL;
2554 }
2555
David Woodhouseb718cd32014-03-09 13:11:33 -07002556 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002557}
2558
Alex Williamson579305f2014-07-03 09:51:43 -06002559static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2560{
2561 *(u16 *)opaque = alias;
2562 return 0;
2563}
2564
Joerg Roedel76208352016-08-25 14:25:12 +02002565static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002566{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002567 struct device_domain_info *info = NULL;
Joerg Roedel76208352016-08-25 14:25:12 +02002568 struct dmar_domain *domain = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002569 struct intel_iommu *iommu;
Lu Baolufcc35c62018-05-04 13:08:17 +08002570 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002571 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002572 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002573
David Woodhouse146922e2014-03-09 15:44:17 -07002574 iommu = device_to_iommu(dev, &bus, &devfn);
2575 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002576 return NULL;
2577
2578 if (dev_is_pci(dev)) {
2579 struct pci_dev *pdev = to_pci_dev(dev);
2580
2581 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2582
2583 spin_lock_irqsave(&device_domain_lock, flags);
2584 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2585 PCI_BUS_NUM(dma_alias),
2586 dma_alias & 0xff);
2587 if (info) {
2588 iommu = info->iommu;
2589 domain = info->domain;
2590 }
2591 spin_unlock_irqrestore(&device_domain_lock, flags);
2592
Joerg Roedel76208352016-08-25 14:25:12 +02002593 /* DMA alias already has a domain, use it */
Alex Williamson579305f2014-07-03 09:51:43 -06002594 if (info)
Joerg Roedel76208352016-08-25 14:25:12 +02002595 goto out;
Alex Williamson579305f2014-07-03 09:51:43 -06002596 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002597
David Woodhouse146922e2014-03-09 15:44:17 -07002598 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002599 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002600 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002601 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002602 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002603 domain_exit(domain);
2604 return NULL;
2605 }
2606
Joerg Roedel76208352016-08-25 14:25:12 +02002607out:
Alex Williamson579305f2014-07-03 09:51:43 -06002608
Joerg Roedel76208352016-08-25 14:25:12 +02002609 return domain;
2610}
2611
2612static struct dmar_domain *set_domain_for_dev(struct device *dev,
2613 struct dmar_domain *domain)
2614{
2615 struct intel_iommu *iommu;
2616 struct dmar_domain *tmp;
2617 u16 req_id, dma_alias;
2618 u8 bus, devfn;
2619
2620 iommu = device_to_iommu(dev, &bus, &devfn);
2621 if (!iommu)
2622 return NULL;
2623
2624 req_id = ((u16)bus << 8) | devfn;
2625
2626 if (dev_is_pci(dev)) {
2627 struct pci_dev *pdev = to_pci_dev(dev);
2628
2629 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2630
2631 /* register PCI DMA alias device */
2632 if (req_id != dma_alias) {
2633 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2634 dma_alias & 0xff, NULL, domain);
2635
2636 if (!tmp || tmp != domain)
2637 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002638 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002639 }
2640
Joerg Roedel5db31562015-07-22 12:40:43 +02002641 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Joerg Roedel76208352016-08-25 14:25:12 +02002642 if (!tmp || tmp != domain)
2643 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002644
Joerg Roedel76208352016-08-25 14:25:12 +02002645 return domain;
2646}
2647
2648static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
2649{
2650 struct dmar_domain *domain, *tmp;
2651
2652 domain = find_domain(dev);
2653 if (domain)
2654 goto out;
2655
2656 domain = find_or_alloc_domain(dev, gaw);
2657 if (!domain)
2658 goto out;
2659
2660 tmp = set_domain_for_dev(dev, domain);
2661 if (!tmp || domain != tmp) {
Alex Williamson579305f2014-07-03 09:51:43 -06002662 domain_exit(domain);
2663 domain = tmp;
2664 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002665
Joerg Roedel76208352016-08-25 14:25:12 +02002666out:
2667
David Woodhouseb718cd32014-03-09 13:11:33 -07002668 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002669}
2670
David Woodhouseb2132032009-06-26 18:50:28 +01002671static int iommu_domain_identity_map(struct dmar_domain *domain,
2672 unsigned long long start,
2673 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002674{
David Woodhousec5395d52009-06-28 16:35:56 +01002675 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2676 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002677
David Woodhousec5395d52009-06-28 16:35:56 +01002678 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2679 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002680 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002681 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002682 }
2683
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002684 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002685 /*
2686 * RMRR range might have overlap with physical memory range,
2687 * clear it first
2688 */
David Woodhousec5395d52009-06-28 16:35:56 +01002689 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002690
Peter Xu87684fd2018-05-04 10:34:53 +08002691 return __domain_mapping(domain, first_vpfn, NULL,
2692 first_vpfn, last_vpfn - first_vpfn + 1,
2693 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002694}
2695
Joerg Roedeld66ce542015-09-23 19:00:10 +02002696static int domain_prepare_identity_map(struct device *dev,
2697 struct dmar_domain *domain,
2698 unsigned long long start,
2699 unsigned long long end)
David Woodhouseb2132032009-06-26 18:50:28 +01002700{
David Woodhouse19943b02009-08-04 16:19:20 +01002701 /* For _hardware_ passthrough, don't bother. But for software
2702 passthrough, we do it anyway -- it may indicate a memory
2703 range which is reserved in E820, so which didn't get set
2704 up to start with in si_domain */
2705 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002706 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2707 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002708 return 0;
2709 }
2710
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002711 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2712 dev_name(dev), start, end);
2713
David Woodhouse5595b522009-12-02 09:21:55 +00002714 if (end < start) {
2715 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2716 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2717 dmi_get_system_info(DMI_BIOS_VENDOR),
2718 dmi_get_system_info(DMI_BIOS_VERSION),
2719 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002720 return -EIO;
David Woodhouse5595b522009-12-02 09:21:55 +00002721 }
2722
David Woodhouse2ff729f2009-08-26 14:25:41 +01002723 if (end >> agaw_to_width(domain->agaw)) {
2724 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2725 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2726 agaw_to_width(domain->agaw),
2727 dmi_get_system_info(DMI_BIOS_VENDOR),
2728 dmi_get_system_info(DMI_BIOS_VERSION),
2729 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002730 return -EIO;
David Woodhouse2ff729f2009-08-26 14:25:41 +01002731 }
David Woodhouse19943b02009-08-04 16:19:20 +01002732
Joerg Roedeld66ce542015-09-23 19:00:10 +02002733 return iommu_domain_identity_map(domain, start, end);
2734}
2735
2736static int iommu_prepare_identity_map(struct device *dev,
2737 unsigned long long start,
2738 unsigned long long end)
2739{
2740 struct dmar_domain *domain;
2741 int ret;
2742
2743 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
2744 if (!domain)
2745 return -ENOMEM;
2746
2747 ret = domain_prepare_identity_map(dev, domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002748 if (ret)
Joerg Roedeld66ce542015-09-23 19:00:10 +02002749 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002750
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002751 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002752}
2753
2754static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002755 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002756{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002757 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002758 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002759 return iommu_prepare_identity_map(dev, rmrr->base_address,
2760 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002761}
2762
Suresh Siddhad3f13812011-08-23 17:05:25 -07002763#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002764static inline void iommu_prepare_isa(void)
2765{
2766 struct pci_dev *pdev;
2767 int ret;
2768
2769 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2770 if (!pdev)
2771 return;
2772
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002773 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002774 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002775
2776 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002777 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002778
Yijing Wang9b27e822014-05-20 20:37:52 +08002779 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002780}
2781#else
2782static inline void iommu_prepare_isa(void)
2783{
2784 return;
2785}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002786#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002787
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002788static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002789
Matt Kraai071e1372009-08-23 22:30:22 -07002790static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002791{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002792 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002793
Jiang Liuab8dfe22014-07-11 14:19:27 +08002794 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002795 if (!si_domain)
2796 return -EFAULT;
2797
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002798 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2799 domain_exit(si_domain);
2800 return -EFAULT;
2801 }
2802
Joerg Roedel0dc79712015-07-21 15:40:06 +02002803 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002804
David Woodhouse19943b02009-08-04 16:19:20 +01002805 if (hw)
2806 return 0;
2807
David Woodhousec7ab48d2009-06-26 19:10:36 +01002808 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002809 unsigned long start_pfn, end_pfn;
2810 int i;
2811
2812 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2813 ret = iommu_domain_identity_map(si_domain,
2814 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2815 if (ret)
2816 return ret;
2817 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002818 }
2819
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002820 return 0;
2821}
2822
David Woodhouse9b226622014-03-09 14:03:28 -07002823static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002824{
2825 struct device_domain_info *info;
2826
2827 if (likely(!iommu_identity_mapping))
2828 return 0;
2829
David Woodhouse9b226622014-03-09 14:03:28 -07002830 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002831 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2832 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002833
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002834 return 0;
2835}
2836
Joerg Roedel28ccce02015-07-21 14:45:31 +02002837static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002838{
David Woodhouse0ac72662014-03-09 13:19:22 -07002839 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002840 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002841 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002842
David Woodhouse5913c9b2014-03-09 16:27:31 -07002843 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002844 if (!iommu)
2845 return -ENODEV;
2846
Joerg Roedel5db31562015-07-22 12:40:43 +02002847 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002848 if (ndomain != domain)
2849 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002850
2851 return 0;
2852}
2853
David Woodhouse0b9d9752014-03-09 15:48:15 -07002854static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002855{
2856 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002857 struct device *tmp;
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002858 int i;
2859
Jiang Liu0e242612014-02-19 14:07:34 +08002860 rcu_read_lock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002861 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002862 /*
2863 * Return TRUE if this RMRR contains the device that
2864 * is passed in.
2865 */
2866 for_each_active_dev_scope(rmrr->devices,
2867 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002868 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002869 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002870 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002871 }
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002872 }
Jiang Liu0e242612014-02-19 14:07:34 +08002873 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002874 return false;
2875}
2876
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002877/*
2878 * There are a couple cases where we need to restrict the functionality of
2879 * devices associated with RMRRs. The first is when evaluating a device for
2880 * identity mapping because problems exist when devices are moved in and out
2881 * of domains and their respective RMRR information is lost. This means that
2882 * a device with associated RMRRs will never be in a "passthrough" domain.
2883 * The second is use of the device through the IOMMU API. This interface
2884 * expects to have full control of the IOVA space for the device. We cannot
2885 * satisfy both the requirement that RMRR access is maintained and have an
2886 * unencumbered IOVA space. We also have no ability to quiesce the device's
2887 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2888 * We therefore prevent devices associated with an RMRR from participating in
2889 * the IOMMU API, which eliminates them from device assignment.
2890 *
2891 * In both cases we assume that PCI USB devices with RMRRs have them largely
2892 * for historical reasons and that the RMRR space is not actively used post
2893 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002894 *
2895 * The same exception is made for graphics devices, with the requirement that
2896 * any use of the RMRR regions will be torn down before assigning the device
2897 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002898 */
2899static bool device_is_rmrr_locked(struct device *dev)
2900{
2901 if (!device_has_rmrr(dev))
2902 return false;
2903
2904 if (dev_is_pci(dev)) {
2905 struct pci_dev *pdev = to_pci_dev(dev);
2906
David Woodhouse18436af2015-03-25 15:05:47 +00002907 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002908 return false;
2909 }
2910
2911 return true;
2912}
2913
David Woodhouse3bdb2592014-03-09 16:03:08 -07002914static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002915{
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002916
David Woodhouse3bdb2592014-03-09 16:03:08 -07002917 if (dev_is_pci(dev)) {
2918 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002919
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002920 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002921 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002922
David Woodhouse3bdb2592014-03-09 16:03:08 -07002923 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2924 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002925
David Woodhouse3bdb2592014-03-09 16:03:08 -07002926 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2927 return 1;
2928
2929 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2930 return 0;
2931
2932 /*
2933 * We want to start off with all devices in the 1:1 domain, and
2934 * take them out later if we find they can't access all of memory.
2935 *
2936 * However, we can't do this for PCI devices behind bridges,
2937 * because all PCI devices behind the same bridge will end up
2938 * with the same source-id on their transactions.
2939 *
2940 * Practically speaking, we can't change things around for these
2941 * devices at run-time, because we can't be sure there'll be no
2942 * DMA transactions in flight for any of their siblings.
2943 *
2944 * So PCI devices (unless they're on the root bus) as well as
2945 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2946 * the 1:1 domain, just in _case_ one of their siblings turns out
2947 * not to be able to map all of memory.
2948 */
2949 if (!pci_is_pcie(pdev)) {
2950 if (!pci_is_root_bus(pdev->bus))
2951 return 0;
2952 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2953 return 0;
2954 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2955 return 0;
2956 } else {
2957 if (device_has_rmrr(dev))
2958 return 0;
2959 }
David Woodhouse6941af22009-07-04 18:24:27 +01002960
David Woodhouse3dfc8132009-07-04 19:11:08 +01002961 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002962 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002963 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002964 * take them out of the 1:1 domain later.
2965 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002966 if (!startup) {
2967 /*
2968 * If the device's dma_mask is less than the system's memory
2969 * size then this is not a candidate for identity mapping.
2970 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002971 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002972
David Woodhouse3bdb2592014-03-09 16:03:08 -07002973 if (dev->coherent_dma_mask &&
2974 dev->coherent_dma_mask < dma_mask)
2975 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002976
David Woodhouse3bdb2592014-03-09 16:03:08 -07002977 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002978 }
David Woodhouse6941af22009-07-04 18:24:27 +01002979
2980 return 1;
2981}
2982
David Woodhousecf04eee2014-03-21 16:49:04 +00002983static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2984{
2985 int ret;
2986
2987 if (!iommu_should_identity_map(dev, 1))
2988 return 0;
2989
Joerg Roedel28ccce02015-07-21 14:45:31 +02002990 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002991 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002992 pr_info("%s identity mapping for device %s\n",
2993 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002994 else if (ret == -ENODEV)
2995 /* device not associated with an iommu */
2996 ret = 0;
2997
2998 return ret;
2999}
3000
3001
Matt Kraai071e1372009-08-23 22:30:22 -07003002static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003003{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003004 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00003005 struct dmar_drhd_unit *drhd;
3006 struct intel_iommu *iommu;
3007 struct device *dev;
3008 int i;
3009 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003010
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003011 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00003012 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
3013 if (ret)
3014 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003015 }
3016
David Woodhousecf04eee2014-03-21 16:49:04 +00003017 for_each_active_iommu(iommu, drhd)
3018 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
3019 struct acpi_device_physical_node *pn;
3020 struct acpi_device *adev;
3021
3022 if (dev->bus != &acpi_bus_type)
3023 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02003024
David Woodhousecf04eee2014-03-21 16:49:04 +00003025 adev= to_acpi_device(dev);
3026 mutex_lock(&adev->physical_node_lock);
3027 list_for_each_entry(pn, &adev->physical_node_list, node) {
3028 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
3029 if (ret)
3030 break;
3031 }
3032 mutex_unlock(&adev->physical_node_lock);
3033 if (ret)
3034 return ret;
3035 }
3036
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003037 return 0;
3038}
3039
Jiang Liuffebeb42014-11-09 22:48:02 +08003040static void intel_iommu_init_qi(struct intel_iommu *iommu)
3041{
3042 /*
3043 * Start from the sane iommu hardware state.
3044 * If the queued invalidation is already initialized by us
3045 * (for example, while enabling interrupt-remapping) then
3046 * we got the things already rolling from a sane state.
3047 */
3048 if (!iommu->qi) {
3049 /*
3050 * Clear any previous faults.
3051 */
3052 dmar_fault(-1, iommu);
3053 /*
3054 * Disable queued invalidation if supported and already enabled
3055 * before OS handover.
3056 */
3057 dmar_disable_qi(iommu);
3058 }
3059
3060 if (dmar_enable_qi(iommu)) {
3061 /*
3062 * Queued Invalidate not enabled, use Register Based Invalidate
3063 */
3064 iommu->flush.flush_context = __iommu_flush_context;
3065 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003066 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003067 iommu->name);
3068 } else {
3069 iommu->flush.flush_context = qi_flush_context;
3070 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003071 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08003072 }
3073}
3074
Joerg Roedel091d42e2015-06-12 11:56:10 +02003075static int copy_context_table(struct intel_iommu *iommu,
Dan Williamsdfddb962015-10-09 18:16:46 -04003076 struct root_entry *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02003077 struct context_entry **tbl,
3078 int bus, bool ext)
3079{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003080 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003081 struct context_entry *new_ce = NULL, ce;
Dan Williamsdfddb962015-10-09 18:16:46 -04003082 struct context_entry *old_ce = NULL;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003083 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003084 phys_addr_t old_ce_phys;
3085
3086 tbl_idx = ext ? bus * 2 : bus;
Dan Williamsdfddb962015-10-09 18:16:46 -04003087 memcpy(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003088
3089 for (devfn = 0; devfn < 256; devfn++) {
3090 /* First calculate the correct index */
3091 idx = (ext ? devfn * 2 : devfn) % 256;
3092
3093 if (idx == 0) {
3094 /* First save what we may have and clean up */
3095 if (new_ce) {
3096 tbl[tbl_idx] = new_ce;
3097 __iommu_flush_cache(iommu, new_ce,
3098 VTD_PAGE_SIZE);
3099 pos = 1;
3100 }
3101
3102 if (old_ce)
3103 iounmap(old_ce);
3104
3105 ret = 0;
3106 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003107 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003108 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003109 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003110
3111 if (!old_ce_phys) {
3112 if (ext && devfn == 0) {
3113 /* No LCTP, try UCTP */
3114 devfn = 0x7f;
3115 continue;
3116 } else {
3117 goto out;
3118 }
3119 }
3120
3121 ret = -ENOMEM;
Dan Williamsdfddb962015-10-09 18:16:46 -04003122 old_ce = memremap(old_ce_phys, PAGE_SIZE,
3123 MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003124 if (!old_ce)
3125 goto out;
3126
3127 new_ce = alloc_pgtable_page(iommu->node);
3128 if (!new_ce)
3129 goto out_unmap;
3130
3131 ret = 0;
3132 }
3133
3134 /* Now copy the context entry */
Dan Williamsdfddb962015-10-09 18:16:46 -04003135 memcpy(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003136
Joerg Roedelcf484d02015-06-12 12:21:46 +02003137 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02003138 continue;
3139
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003140 did = context_domain_id(&ce);
3141 if (did >= 0 && did < cap_ndoms(iommu->cap))
3142 set_bit(did, iommu->domain_ids);
3143
Joerg Roedelcf484d02015-06-12 12:21:46 +02003144 /*
3145 * We need a marker for copied context entries. This
3146 * marker needs to work for the old format as well as
3147 * for extended context entries.
3148 *
3149 * Bit 67 of the context entry is used. In the old
3150 * format this bit is available to software, in the
3151 * extended format it is the PGE bit, but PGE is ignored
3152 * by HW if PASIDs are disabled (and thus still
3153 * available).
3154 *
3155 * So disable PASIDs first and then mark the entry
3156 * copied. This means that we don't copy PASID
3157 * translations from the old kernel, but this is fine as
3158 * faults there are not fatal.
3159 */
3160 context_clear_pasid_enable(&ce);
3161 context_set_copied(&ce);
3162
Joerg Roedel091d42e2015-06-12 11:56:10 +02003163 new_ce[idx] = ce;
3164 }
3165
3166 tbl[tbl_idx + pos] = new_ce;
3167
3168 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
3169
3170out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003171 memunmap(old_ce);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003172
3173out:
3174 return ret;
3175}
3176
3177static int copy_translation_tables(struct intel_iommu *iommu)
3178{
3179 struct context_entry **ctxt_tbls;
Dan Williamsdfddb962015-10-09 18:16:46 -04003180 struct root_entry *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003181 phys_addr_t old_rt_phys;
3182 int ctxt_table_entries;
3183 unsigned long flags;
3184 u64 rtaddr_reg;
3185 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02003186 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003187
3188 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
3189 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02003190 new_ext = !!ecap_ecs(iommu->ecap);
3191
3192 /*
3193 * The RTT bit can only be changed when translation is disabled,
3194 * but disabling translation means to open a window for data
3195 * corruption. So bail out and don't copy anything if we would
3196 * have to change the bit.
3197 */
3198 if (new_ext != ext)
3199 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003200
3201 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
3202 if (!old_rt_phys)
3203 return -EINVAL;
3204
Dan Williamsdfddb962015-10-09 18:16:46 -04003205 old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003206 if (!old_rt)
3207 return -ENOMEM;
3208
3209 /* This is too big for the stack - allocate it from slab */
3210 ctxt_table_entries = ext ? 512 : 256;
3211 ret = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07003212 ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003213 if (!ctxt_tbls)
3214 goto out_unmap;
3215
3216 for (bus = 0; bus < 256; bus++) {
3217 ret = copy_context_table(iommu, &old_rt[bus],
3218 ctxt_tbls, bus, ext);
3219 if (ret) {
3220 pr_err("%s: Failed to copy context table for bus %d\n",
3221 iommu->name, bus);
3222 continue;
3223 }
3224 }
3225
3226 spin_lock_irqsave(&iommu->lock, flags);
3227
3228 /* Context tables are copied, now write them to the root_entry table */
3229 for (bus = 0; bus < 256; bus++) {
3230 int idx = ext ? bus * 2 : bus;
3231 u64 val;
3232
3233 if (ctxt_tbls[idx]) {
3234 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3235 iommu->root_entry[bus].lo = val;
3236 }
3237
3238 if (!ext || !ctxt_tbls[idx + 1])
3239 continue;
3240
3241 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3242 iommu->root_entry[bus].hi = val;
3243 }
3244
3245 spin_unlock_irqrestore(&iommu->lock, flags);
3246
3247 kfree(ctxt_tbls);
3248
3249 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3250
3251 ret = 0;
3252
3253out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003254 memunmap(old_rt);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003255
3256 return ret;
3257}
3258
Joseph Cihulab7792602011-05-03 00:08:37 -07003259static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003260{
3261 struct dmar_drhd_unit *drhd;
3262 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003263 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003264 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003265 struct intel_iommu *iommu;
Joerg Roedel13cf0172017-08-11 11:40:10 +02003266 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003267
3268 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003269 * for each drhd
3270 * allocate root
3271 * initialize and program root entry to not present
3272 * endfor
3273 */
3274 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003275 /*
3276 * lock not needed as this is only incremented in the single
3277 * threaded kernel __init code path all other access are read
3278 * only
3279 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003280 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003281 g_num_of_iommus++;
3282 continue;
3283 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003284 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003285 }
3286
Jiang Liuffebeb42014-11-09 22:48:02 +08003287 /* Preallocate enough resources for IOMMU hot-addition */
3288 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3289 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3290
Weidong Hand9630fe2008-12-08 11:06:32 +08003291 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3292 GFP_KERNEL);
3293 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003294 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003295 ret = -ENOMEM;
3296 goto error;
3297 }
3298
Jiang Liu7c919772014-01-06 14:18:18 +08003299 for_each_active_iommu(iommu, drhd) {
Lu Baolu56283172018-07-14 15:46:54 +08003300 /*
3301 * Find the max pasid size of all IOMMU's in the system.
3302 * We need to ensure the system pasid table is no bigger
3303 * than the smallest supported.
3304 */
3305 if (pasid_enabled(iommu)) {
3306 u32 temp = 2 << ecap_pss(iommu->ecap);
3307
3308 intel_pasid_max_id = min_t(u32, temp,
3309 intel_pasid_max_id);
3310 }
3311
Weidong Hand9630fe2008-12-08 11:06:32 +08003312 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003313
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003314 intel_iommu_init_qi(iommu);
3315
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003316 ret = iommu_init_domains(iommu);
3317 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003318 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003319
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003320 init_translation_status(iommu);
3321
Joerg Roedel091d42e2015-06-12 11:56:10 +02003322 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3323 iommu_disable_translation(iommu);
3324 clear_translation_pre_enabled(iommu);
3325 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3326 iommu->name);
3327 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003328
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003329 /*
3330 * TBD:
3331 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003332 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003333 */
3334 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003335 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003336 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003337
Joerg Roedel091d42e2015-06-12 11:56:10 +02003338 if (translation_pre_enabled(iommu)) {
3339 pr_info("Translation already enabled - trying to copy translation structures\n");
3340
3341 ret = copy_translation_tables(iommu);
3342 if (ret) {
3343 /*
3344 * We found the IOMMU with translation
3345 * enabled - but failed to copy over the
3346 * old root-entry table. Try to proceed
3347 * by disabling translation now and
3348 * allocating a clean root-entry table.
3349 * This might cause DMAR faults, but
3350 * probably the dump will still succeed.
3351 */
3352 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3353 iommu->name);
3354 iommu_disable_translation(iommu);
3355 clear_translation_pre_enabled(iommu);
3356 } else {
3357 pr_info("Copied translation tables from previous kernel for %s\n",
3358 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003359 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003360 }
3361 }
3362
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003363 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003364 hw_pass_through = 0;
David Woodhouse8a94ade2015-03-24 14:54:56 +00003365#ifdef CONFIG_INTEL_IOMMU_SVM
3366 if (pasid_enabled(iommu))
Lu Baolud9737952018-07-14 15:47:02 +08003367 intel_svm_init(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00003368#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003369 }
3370
Joerg Roedela4c34ff2016-06-17 11:29:48 +02003371 /*
3372 * Now that qi is enabled on all iommus, set the root entry and flush
3373 * caches. This is required on some Intel X58 chipsets, otherwise the
3374 * flush_context function will loop forever and the boot hangs.
3375 */
3376 for_each_active_iommu(iommu, drhd) {
3377 iommu_flush_write_buffer(iommu);
3378 iommu_set_root_entry(iommu);
3379 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3380 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3381 }
3382
David Woodhouse19943b02009-08-04 16:19:20 +01003383 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003384 iommu_identity_mapping |= IDENTMAP_ALL;
3385
Suresh Siddhad3f13812011-08-23 17:05:25 -07003386#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003387 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003388#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003389
Ashok Raj21e722c2017-01-30 09:39:53 -08003390 check_tylersburg_isoch();
3391
Joerg Roedel86080cc2015-06-12 12:27:16 +02003392 if (iommu_identity_mapping) {
3393 ret = si_domain_init(hw_pass_through);
3394 if (ret)
3395 goto free_iommu;
3396 }
3397
David Woodhousee0fc7e02009-09-30 09:12:17 -07003398
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003399 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003400 * If we copied translations from a previous kernel in the kdump
3401 * case, we can not assign the devices to domains now, as that
3402 * would eliminate the old mappings. So skip this part and defer
3403 * the assignment to device driver initialization time.
3404 */
3405 if (copied_tables)
3406 goto domains_done;
3407
3408 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003409 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003410 * identity mappings for rmrr, gfx, and isa and may fall back to static
3411 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003412 */
David Woodhouse19943b02009-08-04 16:19:20 +01003413 if (iommu_identity_mapping) {
3414 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3415 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003416 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003417 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003418 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003419 }
David Woodhouse19943b02009-08-04 16:19:20 +01003420 /*
3421 * For each rmrr
3422 * for each dev attached to rmrr
3423 * do
3424 * locate drhd for dev, alloc domain for dev
3425 * allocate free domain
3426 * allocate page table entries for rmrr
3427 * if context not allocated for bus
3428 * allocate and init context
3429 * set present in root table for this bus
3430 * init context with domain, translation etc
3431 * endfor
3432 * endfor
3433 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003434 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003435 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003436 /* some BIOS lists non-exist devices in DMAR table. */
3437 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003438 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003439 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003440 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003441 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003442 }
3443 }
3444
3445 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003446
Joerg Roedela87f4912015-06-12 12:32:54 +02003447domains_done:
3448
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003449 /*
3450 * for each drhd
3451 * enable fault log
3452 * global invalidate context cache
3453 * global invalidate iotlb
3454 * enable translation
3455 */
Jiang Liu7c919772014-01-06 14:18:18 +08003456 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003457 if (drhd->ignored) {
3458 /*
3459 * we always have to disable PMRs or DMA may fail on
3460 * this device
3461 */
3462 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003463 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003464 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003465 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003466
3467 iommu_flush_write_buffer(iommu);
3468
David Woodhousea222a7f2015-10-07 23:35:18 +01003469#ifdef CONFIG_INTEL_IOMMU_SVM
3470 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
3471 ret = intel_svm_enable_prq(iommu);
3472 if (ret)
3473 goto free_iommu;
3474 }
3475#endif
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003476 ret = dmar_set_interrupt(iommu);
3477 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003478 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003479
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003480 if (!translation_pre_enabled(iommu))
3481 iommu_enable_translation(iommu);
3482
David Woodhouseb94996c2009-09-19 15:28:12 -07003483 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003484 }
3485
3486 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003487
3488free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003489 for_each_active_iommu(iommu, drhd) {
3490 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003491 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003492 }
Joerg Roedel13cf0172017-08-11 11:40:10 +02003493
Weidong Hand9630fe2008-12-08 11:06:32 +08003494 kfree(g_iommus);
Joerg Roedel13cf0172017-08-11 11:40:10 +02003495
Jiang Liu989d51f2014-02-19 14:07:21 +08003496error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003497 return ret;
3498}
3499
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003500/* This takes a number of _MM_ pages, not VTD pages */
Omer Peleg2aac6302016-04-20 11:33:57 +03003501static unsigned long intel_alloc_iova(struct device *dev,
David Woodhouse875764d2009-06-28 21:20:51 +01003502 struct dmar_domain *domain,
3503 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003504{
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003505 unsigned long iova_pfn = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003506
David Woodhouse875764d2009-06-28 21:20:51 +01003507 /* Restrict dma_mask to the width that the iommu can handle */
3508 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003509 /* Ensure we reserve the whole size-aligned region */
3510 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003511
3512 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003513 /*
3514 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003515 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003516 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003517 */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003518 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02003519 IOVA_PFN(DMA_BIT_MASK(32)), false);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003520 if (iova_pfn)
3521 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003522 }
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02003523 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
3524 IOVA_PFN(dma_mask), true);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003525 if (unlikely(!iova_pfn)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003526 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003527 nrpages, dev_name(dev));
Omer Peleg2aac6302016-04-20 11:33:57 +03003528 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003529 }
3530
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003531 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003532}
3533
Lu Baolu9ddbfb42018-07-14 15:46:57 +08003534struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003535{
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003536 struct dmar_domain *domain, *tmp;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003537 struct dmar_rmrr_unit *rmrr;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003538 struct device *i_dev;
3539 int i, ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003540
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003541 domain = find_domain(dev);
3542 if (domain)
3543 goto out;
3544
3545 domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
3546 if (!domain)
3547 goto out;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003548
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003549 /* We have a new domain - setup possible RMRRs for the device */
3550 rcu_read_lock();
3551 for_each_rmrr_units(rmrr) {
3552 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
3553 i, i_dev) {
3554 if (i_dev != dev)
3555 continue;
3556
3557 ret = domain_prepare_identity_map(dev, domain,
3558 rmrr->base_address,
3559 rmrr->end_address);
3560 if (ret)
3561 dev_err(dev, "Mapping reserved region failed\n");
3562 }
3563 }
3564 rcu_read_unlock();
3565
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003566 tmp = set_domain_for_dev(dev, domain);
3567 if (!tmp || domain != tmp) {
3568 domain_exit(domain);
3569 domain = tmp;
3570 }
3571
3572out:
3573
3574 if (!domain)
3575 pr_err("Allocating domain for %s failed\n", dev_name(dev));
3576
3577
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003578 return domain;
3579}
3580
David Woodhouseecb509e2014-03-09 16:29:55 -07003581/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003582static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003583{
3584 int found;
3585
David Woodhouse3d891942014-03-06 15:59:26 +00003586 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003587 return 1;
3588
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003589 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003590 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003591
David Woodhouse9b226622014-03-09 14:03:28 -07003592 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003593 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003594 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003595 return 1;
3596 else {
3597 /*
3598 * 32 bit DMA is removed from si_domain and fall back
3599 * to non-identity mapping.
3600 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003601 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003602 pr_info("32bit %s uses non-identity mapping\n",
3603 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003604 return 0;
3605 }
3606 } else {
3607 /*
3608 * In case of a detached 64 bit DMA device from vm, the device
3609 * is put into si_domain for identity mapping.
3610 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003611 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003612 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003613 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003614 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003615 pr_info("64bit %s uses identity mapping\n",
3616 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003617 return 1;
3618 }
3619 }
3620 }
3621
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003622 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003623}
3624
David Woodhouse5040a912014-03-09 16:14:00 -07003625static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003626 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003627{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003628 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003629 phys_addr_t start_paddr;
Omer Peleg2aac6302016-04-20 11:33:57 +03003630 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003631 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003632 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003633 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003634 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003635
3636 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003637
David Woodhouse5040a912014-03-09 16:14:00 -07003638 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003639 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003640
David Woodhouse5040a912014-03-09 16:14:00 -07003641 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003642 if (!domain)
3643 return 0;
3644
Weidong Han8c11e792008-12-08 15:29:22 +08003645 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003646 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003647
Omer Peleg2aac6302016-04-20 11:33:57 +03003648 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
3649 if (!iova_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003650 goto error;
3651
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003652 /*
3653 * Check if DMAR supports zero-length reads on write only
3654 * mappings..
3655 */
3656 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003657 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003658 prot |= DMA_PTE_READ;
3659 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3660 prot |= DMA_PTE_WRITE;
3661 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003662 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003663 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003664 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003665 * is not a big problem
3666 */
Omer Peleg2aac6302016-04-20 11:33:57 +03003667 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003668 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003669 if (ret)
3670 goto error;
3671
Omer Peleg2aac6302016-04-20 11:33:57 +03003672 start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
David Woodhouse03d6a242009-06-28 15:33:46 +01003673 start_paddr += paddr & ~PAGE_MASK;
3674 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003675
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003676error:
Omer Peleg2aac6302016-04-20 11:33:57 +03003677 if (iova_pfn)
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003678 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003679 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003680 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003681 return 0;
3682}
3683
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003684static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3685 unsigned long offset, size_t size,
3686 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003687 unsigned long attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003688{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003689 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003690 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003691}
3692
Omer Peleg769530e2016-04-20 11:33:25 +03003693static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003694{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003695 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003696 unsigned long start_pfn, last_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +03003697 unsigned long nrpages;
Omer Peleg2aac6302016-04-20 11:33:57 +03003698 unsigned long iova_pfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003699 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003700 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003701
David Woodhouse73676832009-07-04 14:08:36 +01003702 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003703 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003704
David Woodhouse1525a292014-03-06 16:19:30 +00003705 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003706 BUG_ON(!domain);
3707
Weidong Han8c11e792008-12-08 15:29:22 +08003708 iommu = domain_get_iommu(domain);
3709
Omer Peleg2aac6302016-04-20 11:33:57 +03003710 iova_pfn = IOVA_PFN(dev_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003711
Omer Peleg769530e2016-04-20 11:33:25 +03003712 nrpages = aligned_nrpages(dev_addr, size);
Omer Peleg2aac6302016-04-20 11:33:57 +03003713 start_pfn = mm_to_dma_pfn(iova_pfn);
Omer Peleg769530e2016-04-20 11:33:25 +03003714 last_pfn = start_pfn + nrpages - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003715
David Woodhoused794dc92009-06-28 00:27:49 +01003716 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003717 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003718
David Woodhouseea8ea462014-03-05 17:09:32 +00003719 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003720
mark gross5e0d2a62008-03-04 15:22:08 -08003721 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003722 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
Omer Peleg769530e2016-04-20 11:33:25 +03003723 nrpages, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003724 /* free iova */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003725 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
David Woodhouseea8ea462014-03-05 17:09:32 +00003726 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003727 } else {
Joerg Roedel13cf0172017-08-11 11:40:10 +02003728 queue_iova(&domain->iovad, iova_pfn, nrpages,
3729 (unsigned long)freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003730 /*
3731 * queue up the release of the unmap to save the 1/6th of the
3732 * cpu used up by the iotlb flush operation...
3733 */
mark gross5e0d2a62008-03-04 15:22:08 -08003734 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003735}
3736
Jiang Liud41a4ad2014-07-11 14:19:34 +08003737static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3738 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003739 unsigned long attrs)
Jiang Liud41a4ad2014-07-11 14:19:34 +08003740{
Omer Peleg769530e2016-04-20 11:33:25 +03003741 intel_unmap(dev, dev_addr, size);
Jiang Liud41a4ad2014-07-11 14:19:34 +08003742}
3743
David Woodhouse5040a912014-03-09 16:14:00 -07003744static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003745 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003746 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003747{
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003748 struct page *page = NULL;
3749 int order;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003750
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003751 size = PAGE_ALIGN(size);
3752 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003753
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003754 if (!iommu_no_mapping(dev))
3755 flags &= ~(GFP_DMA | GFP_DMA32);
3756 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3757 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
3758 flags |= GFP_DMA;
3759 else
3760 flags |= GFP_DMA32;
3761 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003762
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003763 if (gfpflags_allow_blocking(flags)) {
3764 unsigned int count = size >> PAGE_SHIFT;
3765
Marek Szyprowskid834c5a2018-08-17 15:49:00 -07003766 page = dma_alloc_from_contiguous(dev, count, order,
3767 flags & __GFP_NOWARN);
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003768 if (page && iommu_no_mapping(dev) &&
3769 page_to_phys(page) + size > dev->coherent_dma_mask) {
3770 dma_release_from_contiguous(dev, page, count);
3771 page = NULL;
3772 }
3773 }
3774
3775 if (!page)
3776 page = alloc_pages(flags, order);
3777 if (!page)
3778 return NULL;
3779 memset(page_address(page), 0, size);
3780
3781 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
3782 DMA_BIDIRECTIONAL,
3783 dev->coherent_dma_mask);
3784 if (*dma_handle)
3785 return page_address(page);
3786 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3787 __free_pages(page, order);
3788
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003789 return NULL;
3790}
3791
David Woodhouse5040a912014-03-09 16:14:00 -07003792static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003793 dma_addr_t dma_handle, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003794{
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003795 int order;
3796 struct page *page = virt_to_page(vaddr);
3797
3798 size = PAGE_ALIGN(size);
3799 order = get_order(size);
3800
3801 intel_unmap(dev, dma_handle, size);
3802 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3803 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003804}
3805
David Woodhouse5040a912014-03-09 16:14:00 -07003806static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003807 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003808 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003809{
Omer Peleg769530e2016-04-20 11:33:25 +03003810 dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK;
3811 unsigned long nrpages = 0;
3812 struct scatterlist *sg;
3813 int i;
3814
3815 for_each_sg(sglist, sg, nelems, i) {
3816 nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg));
3817 }
3818
3819 intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003820}
3821
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003822static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003823 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003824{
3825 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003826 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003827
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003828 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003829 BUG_ON(!sg_page(sg));
Robin Murphy29a90b72017-09-28 15:14:01 +01003830 sg->dma_address = sg_phys(sg);
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003831 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003832 }
3833 return nelems;
3834}
3835
David Woodhouse5040a912014-03-09 16:14:00 -07003836static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003837 enum dma_data_direction dir, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003838{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003839 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003840 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003841 size_t size = 0;
3842 int prot = 0;
Omer Peleg2aac6302016-04-20 11:33:57 +03003843 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003844 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003845 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003846 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003847 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003848
3849 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003850 if (iommu_no_mapping(dev))
3851 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003852
David Woodhouse5040a912014-03-09 16:14:00 -07003853 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003854 if (!domain)
3855 return 0;
3856
Weidong Han8c11e792008-12-08 15:29:22 +08003857 iommu = domain_get_iommu(domain);
3858
David Woodhouseb536d242009-06-28 14:49:31 +01003859 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003860 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003861
Omer Peleg2aac6302016-04-20 11:33:57 +03003862 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
David Woodhouse5040a912014-03-09 16:14:00 -07003863 *dev->dma_mask);
Omer Peleg2aac6302016-04-20 11:33:57 +03003864 if (!iova_pfn) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003865 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003866 return 0;
3867 }
3868
3869 /*
3870 * Check if DMAR supports zero-length reads on write only
3871 * mappings..
3872 */
3873 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003874 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003875 prot |= DMA_PTE_READ;
3876 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3877 prot |= DMA_PTE_WRITE;
3878
Omer Peleg2aac6302016-04-20 11:33:57 +03003879 start_vpfn = mm_to_dma_pfn(iova_pfn);
David Woodhousee1605492009-06-29 11:17:38 +01003880
Fenghua Yuf5329592009-08-04 15:09:37 -07003881 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003882 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003883 dma_pte_free_pagetable(domain, start_vpfn,
David Dillowbc24c572017-06-28 19:42:23 -07003884 start_vpfn + size - 1,
3885 agaw_to_level(domain->agaw) + 1);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003886 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
David Woodhousee1605492009-06-29 11:17:38 +01003887 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003888 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003889
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003890 return nelems;
3891}
3892
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003893static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3894{
3895 return !dma_addr;
3896}
3897
Christoph Hellwig02b4da52018-09-17 19:10:31 +02003898static const struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003899 .alloc = intel_alloc_coherent,
3900 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003901 .map_sg = intel_map_sg,
3902 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003903 .map_page = intel_map_page,
3904 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003905 .mapping_error = intel_mapping_error,
Christoph Hellwigfec777c2018-03-19 11:38:15 +01003906 .dma_supported = dma_direct_supported,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003907};
3908
3909static inline int iommu_domain_cache_init(void)
3910{
3911 int ret = 0;
3912
3913 iommu_domain_cache = kmem_cache_create("iommu_domain",
3914 sizeof(struct dmar_domain),
3915 0,
3916 SLAB_HWCACHE_ALIGN,
3917
3918 NULL);
3919 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003920 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003921 ret = -ENOMEM;
3922 }
3923
3924 return ret;
3925}
3926
3927static inline int iommu_devinfo_cache_init(void)
3928{
3929 int ret = 0;
3930
3931 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3932 sizeof(struct device_domain_info),
3933 0,
3934 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003935 NULL);
3936 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003937 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003938 ret = -ENOMEM;
3939 }
3940
3941 return ret;
3942}
3943
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003944static int __init iommu_init_mempool(void)
3945{
3946 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003947 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003948 if (ret)
3949 return ret;
3950
3951 ret = iommu_domain_cache_init();
3952 if (ret)
3953 goto domain_error;
3954
3955 ret = iommu_devinfo_cache_init();
3956 if (!ret)
3957 return ret;
3958
3959 kmem_cache_destroy(iommu_domain_cache);
3960domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003961 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003962
3963 return -ENOMEM;
3964}
3965
3966static void __init iommu_exit_mempool(void)
3967{
3968 kmem_cache_destroy(iommu_devinfo_cache);
3969 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003970 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003971}
3972
Dan Williams556ab452010-07-23 15:47:56 -07003973static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3974{
3975 struct dmar_drhd_unit *drhd;
3976 u32 vtbar;
3977 int rc;
3978
3979 /* We know that this device on this chipset has its own IOMMU.
3980 * If we find it under a different IOMMU, then the BIOS is lying
3981 * to us. Hope that the IOMMU for this device is actually
3982 * disabled, and it needs no translation...
3983 */
3984 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3985 if (rc) {
3986 /* "can't" happen */
3987 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3988 return;
3989 }
3990 vtbar &= 0xffff0000;
3991
3992 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3993 drhd = dmar_find_matched_drhd_unit(pdev);
3994 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3995 TAINT_FIRMWARE_WORKAROUND,
3996 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3997 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3998}
3999DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
4000
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004001static void __init init_no_remapping_devices(void)
4002{
4003 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00004004 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08004005 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004006
4007 for_each_drhd_unit(drhd) {
4008 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08004009 for_each_active_dev_scope(drhd->devices,
4010 drhd->devices_cnt, i, dev)
4011 break;
David Woodhouse832bd852014-03-07 15:08:36 +00004012 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004013 if (i == drhd->devices_cnt)
4014 drhd->ignored = 1;
4015 }
4016 }
4017
Jiang Liu7c919772014-01-06 14:18:18 +08004018 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08004019 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004020 continue;
4021
Jiang Liub683b232014-02-19 14:07:32 +08004022 for_each_active_dev_scope(drhd->devices,
4023 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00004024 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004025 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004026 if (i < drhd->devices_cnt)
4027 continue;
4028
David Woodhousec0771df2011-10-14 20:59:46 +01004029 /* This IOMMU has *only* gfx devices. Either bypass it or
4030 set the gfx_mapped flag, as appropriate */
4031 if (dmar_map_gfx) {
4032 intel_iommu_gfx_mapped = 1;
4033 } else {
4034 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08004035 for_each_active_dev_scope(drhd->devices,
4036 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00004037 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004038 }
4039 }
4040}
4041
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004042#ifdef CONFIG_SUSPEND
4043static int init_iommu_hw(void)
4044{
4045 struct dmar_drhd_unit *drhd;
4046 struct intel_iommu *iommu = NULL;
4047
4048 for_each_active_iommu(iommu, drhd)
4049 if (iommu->qi)
4050 dmar_reenable_qi(iommu);
4051
Joseph Cihulab7792602011-05-03 00:08:37 -07004052 for_each_iommu(iommu, drhd) {
4053 if (drhd->ignored) {
4054 /*
4055 * we always have to disable PMRs or DMA may fail on
4056 * this device
4057 */
4058 if (force_on)
4059 iommu_disable_protect_mem_regions(iommu);
4060 continue;
4061 }
4062
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004063 iommu_flush_write_buffer(iommu);
4064
4065 iommu_set_root_entry(iommu);
4066
4067 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004068 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08004069 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4070 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07004071 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004072 }
4073
4074 return 0;
4075}
4076
4077static void iommu_flush_all(void)
4078{
4079 struct dmar_drhd_unit *drhd;
4080 struct intel_iommu *iommu;
4081
4082 for_each_active_iommu(iommu, drhd) {
4083 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004084 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004085 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004086 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004087 }
4088}
4089
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004090static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004091{
4092 struct dmar_drhd_unit *drhd;
4093 struct intel_iommu *iommu = NULL;
4094 unsigned long flag;
4095
4096 for_each_active_iommu(iommu, drhd) {
Kees Cook6396bb22018-06-12 14:03:40 -07004097 iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32),
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004098 GFP_ATOMIC);
4099 if (!iommu->iommu_state)
4100 goto nomem;
4101 }
4102
4103 iommu_flush_all();
4104
4105 for_each_active_iommu(iommu, drhd) {
4106 iommu_disable_translation(iommu);
4107
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004108 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004109
4110 iommu->iommu_state[SR_DMAR_FECTL_REG] =
4111 readl(iommu->reg + DMAR_FECTL_REG);
4112 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
4113 readl(iommu->reg + DMAR_FEDATA_REG);
4114 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
4115 readl(iommu->reg + DMAR_FEADDR_REG);
4116 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
4117 readl(iommu->reg + DMAR_FEUADDR_REG);
4118
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004119 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004120 }
4121 return 0;
4122
4123nomem:
4124 for_each_active_iommu(iommu, drhd)
4125 kfree(iommu->iommu_state);
4126
4127 return -ENOMEM;
4128}
4129
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004130static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004131{
4132 struct dmar_drhd_unit *drhd;
4133 struct intel_iommu *iommu = NULL;
4134 unsigned long flag;
4135
4136 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07004137 if (force_on)
4138 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
4139 else
4140 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004141 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004142 }
4143
4144 for_each_active_iommu(iommu, drhd) {
4145
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004146 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004147
4148 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
4149 iommu->reg + DMAR_FECTL_REG);
4150 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
4151 iommu->reg + DMAR_FEDATA_REG);
4152 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
4153 iommu->reg + DMAR_FEADDR_REG);
4154 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
4155 iommu->reg + DMAR_FEUADDR_REG);
4156
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004157 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004158 }
4159
4160 for_each_active_iommu(iommu, drhd)
4161 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004162}
4163
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004164static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004165 .resume = iommu_resume,
4166 .suspend = iommu_suspend,
4167};
4168
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004169static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004170{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004171 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004172}
4173
4174#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004175static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004176#endif /* CONFIG_PM */
4177
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004178
Jiang Liuc2a0b532014-11-09 22:47:56 +08004179int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004180{
4181 struct acpi_dmar_reserved_memory *rmrr;
Eric Auger0659b8d2017-01-19 20:57:53 +00004182 int prot = DMA_PTE_READ|DMA_PTE_WRITE;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004183 struct dmar_rmrr_unit *rmrru;
Eric Auger0659b8d2017-01-19 20:57:53 +00004184 size_t length;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004185
4186 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4187 if (!rmrru)
Eric Auger0659b8d2017-01-19 20:57:53 +00004188 goto out;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004189
4190 rmrru->hdr = header;
4191 rmrr = (struct acpi_dmar_reserved_memory *)header;
4192 rmrru->base_address = rmrr->base_address;
4193 rmrru->end_address = rmrr->end_address;
Eric Auger0659b8d2017-01-19 20:57:53 +00004194
4195 length = rmrr->end_address - rmrr->base_address + 1;
4196 rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length, prot,
4197 IOMMU_RESV_DIRECT);
4198 if (!rmrru->resv)
4199 goto free_rmrru;
4200
Jiang Liu2e455282014-02-19 14:07:36 +08004201 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4202 ((void *)rmrr) + rmrr->header.length,
4203 &rmrru->devices_cnt);
Eric Auger0659b8d2017-01-19 20:57:53 +00004204 if (rmrru->devices_cnt && rmrru->devices == NULL)
4205 goto free_all;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004206
Jiang Liu2e455282014-02-19 14:07:36 +08004207 list_add(&rmrru->list, &dmar_rmrr_units);
4208
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004209 return 0;
Eric Auger0659b8d2017-01-19 20:57:53 +00004210free_all:
4211 kfree(rmrru->resv);
4212free_rmrru:
4213 kfree(rmrru);
4214out:
4215 return -ENOMEM;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004216}
4217
Jiang Liu6b197242014-11-09 22:47:58 +08004218static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4219{
4220 struct dmar_atsr_unit *atsru;
4221 struct acpi_dmar_atsr *tmp;
4222
4223 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4224 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4225 if (atsr->segment != tmp->segment)
4226 continue;
4227 if (atsr->header.length != tmp->header.length)
4228 continue;
4229 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4230 return atsru;
4231 }
4232
4233 return NULL;
4234}
4235
4236int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004237{
4238 struct acpi_dmar_atsr *atsr;
4239 struct dmar_atsr_unit *atsru;
4240
Thomas Gleixnerb608fe32017-05-16 20:42:41 +02004241 if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
Jiang Liu6b197242014-11-09 22:47:58 +08004242 return 0;
4243
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004244 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004245 atsru = dmar_find_atsr(atsr);
4246 if (atsru)
4247 return 0;
4248
4249 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004250 if (!atsru)
4251 return -ENOMEM;
4252
Jiang Liu6b197242014-11-09 22:47:58 +08004253 /*
4254 * If memory is allocated from slab by ACPI _DSM method, we need to
4255 * copy the memory content because the memory buffer will be freed
4256 * on return.
4257 */
4258 atsru->hdr = (void *)(atsru + 1);
4259 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004260 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004261 if (!atsru->include_all) {
4262 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4263 (void *)atsr + atsr->header.length,
4264 &atsru->devices_cnt);
4265 if (atsru->devices_cnt && atsru->devices == NULL) {
4266 kfree(atsru);
4267 return -ENOMEM;
4268 }
4269 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004270
Jiang Liu0e242612014-02-19 14:07:34 +08004271 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004272
4273 return 0;
4274}
4275
Jiang Liu9bdc5312014-01-06 14:18:27 +08004276static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4277{
4278 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4279 kfree(atsru);
4280}
4281
Jiang Liu6b197242014-11-09 22:47:58 +08004282int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4283{
4284 struct acpi_dmar_atsr *atsr;
4285 struct dmar_atsr_unit *atsru;
4286
4287 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4288 atsru = dmar_find_atsr(atsr);
4289 if (atsru) {
4290 list_del_rcu(&atsru->list);
4291 synchronize_rcu();
4292 intel_iommu_free_atsr(atsru);
4293 }
4294
4295 return 0;
4296}
4297
4298int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4299{
4300 int i;
4301 struct device *dev;
4302 struct acpi_dmar_atsr *atsr;
4303 struct dmar_atsr_unit *atsru;
4304
4305 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4306 atsru = dmar_find_atsr(atsr);
4307 if (!atsru)
4308 return 0;
4309
Linus Torvalds194dc872016-07-27 20:03:31 -07004310 if (!atsru->include_all && atsru->devices && atsru->devices_cnt) {
Jiang Liu6b197242014-11-09 22:47:58 +08004311 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4312 i, dev)
4313 return -EBUSY;
Linus Torvalds194dc872016-07-27 20:03:31 -07004314 }
Jiang Liu6b197242014-11-09 22:47:58 +08004315
4316 return 0;
4317}
4318
Jiang Liuffebeb42014-11-09 22:48:02 +08004319static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4320{
4321 int sp, ret = 0;
4322 struct intel_iommu *iommu = dmaru->iommu;
4323
4324 if (g_iommus[iommu->seq_id])
4325 return 0;
4326
4327 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004328 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004329 iommu->name);
4330 return -ENXIO;
4331 }
4332 if (!ecap_sc_support(iommu->ecap) &&
4333 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004334 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004335 iommu->name);
4336 return -ENXIO;
4337 }
4338 sp = domain_update_iommu_superpage(iommu) - 1;
4339 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004340 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004341 iommu->name);
4342 return -ENXIO;
4343 }
4344
4345 /*
4346 * Disable translation if already enabled prior to OS handover.
4347 */
4348 if (iommu->gcmd & DMA_GCMD_TE)
4349 iommu_disable_translation(iommu);
4350
4351 g_iommus[iommu->seq_id] = iommu;
4352 ret = iommu_init_domains(iommu);
4353 if (ret == 0)
4354 ret = iommu_alloc_root_entry(iommu);
4355 if (ret)
4356 goto out;
4357
David Woodhouse8a94ade2015-03-24 14:54:56 +00004358#ifdef CONFIG_INTEL_IOMMU_SVM
4359 if (pasid_enabled(iommu))
Lu Baolud9737952018-07-14 15:47:02 +08004360 intel_svm_init(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00004361#endif
4362
Jiang Liuffebeb42014-11-09 22:48:02 +08004363 if (dmaru->ignored) {
4364 /*
4365 * we always have to disable PMRs or DMA may fail on this device
4366 */
4367 if (force_on)
4368 iommu_disable_protect_mem_regions(iommu);
4369 return 0;
4370 }
4371
4372 intel_iommu_init_qi(iommu);
4373 iommu_flush_write_buffer(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01004374
4375#ifdef CONFIG_INTEL_IOMMU_SVM
4376 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
4377 ret = intel_svm_enable_prq(iommu);
4378 if (ret)
4379 goto disable_iommu;
4380 }
4381#endif
Jiang Liuffebeb42014-11-09 22:48:02 +08004382 ret = dmar_set_interrupt(iommu);
4383 if (ret)
4384 goto disable_iommu;
4385
4386 iommu_set_root_entry(iommu);
4387 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4388 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4389 iommu_enable_translation(iommu);
4390
Jiang Liuffebeb42014-11-09 22:48:02 +08004391 iommu_disable_protect_mem_regions(iommu);
4392 return 0;
4393
4394disable_iommu:
4395 disable_dmar_iommu(iommu);
4396out:
4397 free_dmar_iommu(iommu);
4398 return ret;
4399}
4400
Jiang Liu6b197242014-11-09 22:47:58 +08004401int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4402{
Jiang Liuffebeb42014-11-09 22:48:02 +08004403 int ret = 0;
4404 struct intel_iommu *iommu = dmaru->iommu;
4405
4406 if (!intel_iommu_enabled)
4407 return 0;
4408 if (iommu == NULL)
4409 return -EINVAL;
4410
4411 if (insert) {
4412 ret = intel_iommu_add(dmaru);
4413 } else {
4414 disable_dmar_iommu(iommu);
4415 free_dmar_iommu(iommu);
4416 }
4417
4418 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004419}
4420
Jiang Liu9bdc5312014-01-06 14:18:27 +08004421static void intel_iommu_free_dmars(void)
4422{
4423 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4424 struct dmar_atsr_unit *atsru, *atsr_n;
4425
4426 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4427 list_del(&rmrru->list);
4428 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
Eric Auger0659b8d2017-01-19 20:57:53 +00004429 kfree(rmrru->resv);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004430 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004431 }
4432
Jiang Liu9bdc5312014-01-06 14:18:27 +08004433 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4434 list_del(&atsru->list);
4435 intel_iommu_free_atsr(atsru);
4436 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004437}
4438
4439int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4440{
Jiang Liub683b232014-02-19 14:07:32 +08004441 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004442 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004443 struct pci_dev *bridge = NULL;
4444 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004445 struct acpi_dmar_atsr *atsr;
4446 struct dmar_atsr_unit *atsru;
4447
4448 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004449 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004450 bridge = bus->self;
David Woodhoused14053b32015-10-15 09:28:06 +01004451 /* If it's an integrated device, allow ATS */
4452 if (!bridge)
4453 return 1;
4454 /* Connected via non-PCIe: no ATS */
4455 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004456 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004457 return 0;
David Woodhoused14053b32015-10-15 09:28:06 +01004458 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004459 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004460 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004461 }
4462
Jiang Liu0e242612014-02-19 14:07:34 +08004463 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004464 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4465 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4466 if (atsr->segment != pci_domain_nr(dev->bus))
4467 continue;
4468
Jiang Liub683b232014-02-19 14:07:32 +08004469 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004470 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004471 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004472
4473 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004474 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004475 }
Jiang Liub683b232014-02-19 14:07:32 +08004476 ret = 0;
4477out:
Jiang Liu0e242612014-02-19 14:07:34 +08004478 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004479
Jiang Liub683b232014-02-19 14:07:32 +08004480 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004481}
4482
Jiang Liu59ce0512014-02-19 14:07:35 +08004483int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4484{
4485 int ret = 0;
4486 struct dmar_rmrr_unit *rmrru;
4487 struct dmar_atsr_unit *atsru;
4488 struct acpi_dmar_atsr *atsr;
4489 struct acpi_dmar_reserved_memory *rmrr;
4490
Thomas Gleixnerb608fe32017-05-16 20:42:41 +02004491 if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
Jiang Liu59ce0512014-02-19 14:07:35 +08004492 return 0;
4493
4494 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4495 rmrr = container_of(rmrru->hdr,
4496 struct acpi_dmar_reserved_memory, header);
4497 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4498 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4499 ((void *)rmrr) + rmrr->header.length,
4500 rmrr->segment, rmrru->devices,
4501 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004502 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004503 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004504 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004505 dmar_remove_dev_scope(info, rmrr->segment,
4506 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004507 }
4508 }
4509
4510 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4511 if (atsru->include_all)
4512 continue;
4513
4514 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4515 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4516 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4517 (void *)atsr + atsr->header.length,
4518 atsr->segment, atsru->devices,
4519 atsru->devices_cnt);
4520 if (ret > 0)
4521 break;
4522 else if(ret < 0)
4523 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004524 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu59ce0512014-02-19 14:07:35 +08004525 if (dmar_remove_dev_scope(info, atsr->segment,
4526 atsru->devices, atsru->devices_cnt))
4527 break;
4528 }
4529 }
4530
4531 return 0;
4532}
4533
Fenghua Yu99dcade2009-11-11 07:23:06 -08004534/*
4535 * Here we only respond to action of unbound device from driver.
4536 *
4537 * Added device is not attached to its DMAR domain here yet. That will happen
4538 * when mapping the device to iova.
4539 */
4540static int device_notifier(struct notifier_block *nb,
4541 unsigned long action, void *data)
4542{
4543 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004544 struct dmar_domain *domain;
4545
David Woodhouse3d891942014-03-06 15:59:26 +00004546 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004547 return 0;
4548
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004549 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004550 return 0;
4551
David Woodhouse1525a292014-03-06 16:19:30 +00004552 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004553 if (!domain)
4554 return 0;
4555
Joerg Roedele6de0f82015-07-22 16:30:36 +02004556 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004557 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004558 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004559
Fenghua Yu99dcade2009-11-11 07:23:06 -08004560 return 0;
4561}
4562
4563static struct notifier_block device_nb = {
4564 .notifier_call = device_notifier,
4565};
4566
Jiang Liu75f05562014-02-19 14:07:37 +08004567static int intel_iommu_memory_notifier(struct notifier_block *nb,
4568 unsigned long val, void *v)
4569{
4570 struct memory_notify *mhp = v;
4571 unsigned long long start, end;
4572 unsigned long start_vpfn, last_vpfn;
4573
4574 switch (val) {
4575 case MEM_GOING_ONLINE:
4576 start = mhp->start_pfn << PAGE_SHIFT;
4577 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4578 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004579 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004580 start, end);
4581 return NOTIFY_BAD;
4582 }
4583 break;
4584
4585 case MEM_OFFLINE:
4586 case MEM_CANCEL_ONLINE:
4587 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4588 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4589 while (start_vpfn <= last_vpfn) {
4590 struct iova *iova;
4591 struct dmar_drhd_unit *drhd;
4592 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004593 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004594
4595 iova = find_iova(&si_domain->iovad, start_vpfn);
4596 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004597 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004598 start_vpfn);
4599 break;
4600 }
4601
4602 iova = split_and_remove_iova(&si_domain->iovad, iova,
4603 start_vpfn, last_vpfn);
4604 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004605 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004606 start_vpfn, last_vpfn);
4607 return NOTIFY_BAD;
4608 }
4609
David Woodhouseea8ea462014-03-05 17:09:32 +00004610 freelist = domain_unmap(si_domain, iova->pfn_lo,
4611 iova->pfn_hi);
4612
Jiang Liu75f05562014-02-19 14:07:37 +08004613 rcu_read_lock();
4614 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004615 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004616 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004617 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004618 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004619 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004620
4621 start_vpfn = iova->pfn_hi + 1;
4622 free_iova_mem(iova);
4623 }
4624 break;
4625 }
4626
4627 return NOTIFY_OK;
4628}
4629
4630static struct notifier_block intel_iommu_memory_nb = {
4631 .notifier_call = intel_iommu_memory_notifier,
4632 .priority = 0
4633};
4634
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004635static void free_all_cpu_cached_iovas(unsigned int cpu)
4636{
4637 int i;
4638
4639 for (i = 0; i < g_num_of_iommus; i++) {
4640 struct intel_iommu *iommu = g_iommus[i];
4641 struct dmar_domain *domain;
Aaron Campbell0caa7612016-07-02 21:23:24 -03004642 int did;
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004643
4644 if (!iommu)
4645 continue;
4646
Jan Niehusmann3bd4f912016-06-06 14:20:11 +02004647 for (did = 0; did < cap_ndoms(iommu->cap); did++) {
Aaron Campbell0caa7612016-07-02 21:23:24 -03004648 domain = get_iommu_domain(iommu, (u16)did);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004649
4650 if (!domain)
4651 continue;
4652 free_cpu_cached_iovas(cpu, &domain->iovad);
4653 }
4654 }
4655}
4656
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004657static int intel_iommu_cpu_dead(unsigned int cpu)
Omer Pelegaa473242016-04-20 11:33:02 +03004658{
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004659 free_all_cpu_cached_iovas(cpu);
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004660 return 0;
Omer Pelegaa473242016-04-20 11:33:02 +03004661}
4662
Joerg Roedel161b28a2017-03-28 17:04:52 +02004663static void intel_disable_iommus(void)
4664{
4665 struct intel_iommu *iommu = NULL;
4666 struct dmar_drhd_unit *drhd;
4667
4668 for_each_iommu(iommu, drhd)
4669 iommu_disable_translation(iommu);
4670}
4671
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004672static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev)
4673{
Joerg Roedel2926a2aa2017-08-14 17:19:26 +02004674 struct iommu_device *iommu_dev = dev_to_iommu_device(dev);
4675
4676 return container_of(iommu_dev, struct intel_iommu, iommu);
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004677}
4678
Alex Williamsona5459cf2014-06-12 16:12:31 -06004679static ssize_t intel_iommu_show_version(struct device *dev,
4680 struct device_attribute *attr,
4681 char *buf)
4682{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004683 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004684 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4685 return sprintf(buf, "%d:%d\n",
4686 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4687}
4688static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4689
4690static ssize_t intel_iommu_show_address(struct device *dev,
4691 struct device_attribute *attr,
4692 char *buf)
4693{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004694 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004695 return sprintf(buf, "%llx\n", iommu->reg_phys);
4696}
4697static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4698
4699static ssize_t intel_iommu_show_cap(struct device *dev,
4700 struct device_attribute *attr,
4701 char *buf)
4702{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004703 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004704 return sprintf(buf, "%llx\n", iommu->cap);
4705}
4706static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4707
4708static ssize_t intel_iommu_show_ecap(struct device *dev,
4709 struct device_attribute *attr,
4710 char *buf)
4711{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004712 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004713 return sprintf(buf, "%llx\n", iommu->ecap);
4714}
4715static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4716
Alex Williamson2238c082015-07-14 15:24:53 -06004717static ssize_t intel_iommu_show_ndoms(struct device *dev,
4718 struct device_attribute *attr,
4719 char *buf)
4720{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004721 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamson2238c082015-07-14 15:24:53 -06004722 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4723}
4724static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4725
4726static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4727 struct device_attribute *attr,
4728 char *buf)
4729{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004730 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamson2238c082015-07-14 15:24:53 -06004731 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4732 cap_ndoms(iommu->cap)));
4733}
4734static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4735
Alex Williamsona5459cf2014-06-12 16:12:31 -06004736static struct attribute *intel_iommu_attrs[] = {
4737 &dev_attr_version.attr,
4738 &dev_attr_address.attr,
4739 &dev_attr_cap.attr,
4740 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004741 &dev_attr_domains_supported.attr,
4742 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004743 NULL,
4744};
4745
4746static struct attribute_group intel_iommu_group = {
4747 .name = "intel-iommu",
4748 .attrs = intel_iommu_attrs,
4749};
4750
4751const struct attribute_group *intel_iommu_groups[] = {
4752 &intel_iommu_group,
4753 NULL,
4754};
4755
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004756int __init intel_iommu_init(void)
4757{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004758 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004759 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004760 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004761
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004762 /* VT-d is required for a TXT/tboot launch, so enforce that */
4763 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004764
Jiang Liu3a5670e2014-02-19 14:07:33 +08004765 if (iommu_init_mempool()) {
4766 if (force_on)
4767 panic("tboot: Failed to initialize iommu memory\n");
4768 return -ENOMEM;
4769 }
4770
4771 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004772 if (dmar_table_init()) {
4773 if (force_on)
4774 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004775 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004776 }
4777
Suresh Siddhac2c72862011-08-23 17:05:19 -07004778 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004779 if (force_on)
4780 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004781 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004782 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004783
Joerg Roedelec154bf2017-10-06 15:00:53 +02004784 up_write(&dmar_global_lock);
4785
4786 /*
4787 * The bus notifier takes the dmar_global_lock, so lockdep will
4788 * complain later when we register it under the lock.
4789 */
4790 dmar_register_bus_notifier();
4791
4792 down_write(&dmar_global_lock);
4793
Joerg Roedel161b28a2017-03-28 17:04:52 +02004794 if (no_iommu || dmar_disabled) {
4795 /*
Shaohua Libfd20f12017-04-26 09:18:35 -07004796 * We exit the function here to ensure IOMMU's remapping and
4797 * mempool aren't setup, which means that the IOMMU's PMRs
4798 * won't be disabled via the call to init_dmars(). So disable
4799 * it explicitly here. The PMRs were setup by tboot prior to
4800 * calling SENTER, but the kernel is expected to reset/tear
4801 * down the PMRs.
4802 */
4803 if (intel_iommu_tboot_noforce) {
4804 for_each_iommu(iommu, drhd)
4805 iommu_disable_protect_mem_regions(iommu);
4806 }
4807
4808 /*
Joerg Roedel161b28a2017-03-28 17:04:52 +02004809 * Make sure the IOMMUs are switched off, even when we
4810 * boot into a kexec kernel and the previous kernel left
4811 * them enabled
4812 */
4813 intel_disable_iommus();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004814 goto out_free_dmar;
Joerg Roedel161b28a2017-03-28 17:04:52 +02004815 }
Suresh Siddha2ae21012008-07-10 11:16:43 -07004816
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004817 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004818 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004819
4820 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004821 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004822
Joseph Cihula51a63e62011-03-21 11:04:24 -07004823 if (dmar_init_reserved_ranges()) {
4824 if (force_on)
4825 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004826 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004827 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004828
4829 init_no_remapping_devices();
4830
Joseph Cihulab7792602011-05-03 00:08:37 -07004831 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004832 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004833 if (force_on)
4834 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004835 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004836 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004837 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004838 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004839 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004840
Christoph Hellwig4fac8072017-12-24 13:57:08 +01004841#if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB)
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004842 swiotlb = 0;
4843#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004844 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004845
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004846 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004847
Joerg Roedel39ab9552017-02-01 16:56:46 +01004848 for_each_active_iommu(iommu, drhd) {
4849 iommu_device_sysfs_add(&iommu->iommu, NULL,
4850 intel_iommu_groups,
4851 "%s", iommu->name);
4852 iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
4853 iommu_device_register(&iommu->iommu);
4854 }
Alex Williamsona5459cf2014-06-12 16:12:31 -06004855
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004856 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004857 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004858 if (si_domain && !hw_pass_through)
4859 register_memory_notifier(&intel_iommu_memory_nb);
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004860 cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL,
4861 intel_iommu_cpu_dead);
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004862 intel_iommu_enabled = 1;
4863
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004864 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004865
4866out_free_reserved_range:
4867 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004868out_free_dmar:
4869 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004870 up_write(&dmar_global_lock);
4871 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004872 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004873}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004874
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004875static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004876{
4877 struct intel_iommu *iommu = opaque;
4878
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004879 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004880 return 0;
4881}
4882
4883/*
4884 * NB - intel-iommu lacks any sort of reference counting for the users of
4885 * dependent devices. If multiple endpoints have intersecting dependent
4886 * devices, unbinding the driver from any one of them will possibly leave
4887 * the others unable to operate.
4888 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004889static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004890{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004891 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004892 return;
4893
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004894 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004895}
4896
Joerg Roedel127c7612015-07-23 17:44:46 +02004897static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004898{
Weidong Hanc7151a82008-12-08 22:51:37 +08004899 struct intel_iommu *iommu;
4900 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004901
Joerg Roedel55d94042015-07-22 16:50:40 +02004902 assert_spin_locked(&device_domain_lock);
4903
Joerg Roedelb608ac32015-07-21 18:19:08 +02004904 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004905 return;
4906
Joerg Roedel127c7612015-07-23 17:44:46 +02004907 iommu = info->iommu;
4908
4909 if (info->dev) {
4910 iommu_disable_dev_iotlb(info);
4911 domain_context_clear(iommu, info->dev);
Lu Baolua7fc93f2018-07-14 15:47:00 +08004912 intel_pasid_free_table(info->dev);
Joerg Roedel127c7612015-07-23 17:44:46 +02004913 }
4914
Joerg Roedelb608ac32015-07-21 18:19:08 +02004915 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004916
Joerg Roedeld160aca2015-07-22 11:52:53 +02004917 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004918 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004919 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004920
4921 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004922}
4923
Joerg Roedel55d94042015-07-22 16:50:40 +02004924static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4925 struct device *dev)
4926{
Joerg Roedel127c7612015-07-23 17:44:46 +02004927 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004928 unsigned long flags;
4929
Weidong Hanc7151a82008-12-08 22:51:37 +08004930 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004931 info = dev->archdata.iommu;
4932 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004933 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004934}
4935
4936static int md_domain_init(struct dmar_domain *domain, int guest_width)
4937{
4938 int adjust_width;
4939
Zhen Leiaa3ac942017-09-21 16:52:45 +01004940 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004941 domain_reserve_special_ranges(domain);
4942
4943 /* calculate AGAW */
4944 domain->gaw = guest_width;
4945 adjust_width = guestwidth_to_adjustwidth(guest_width);
4946 domain->agaw = width_to_agaw(adjust_width);
4947
Weidong Han5e98c4b2008-12-08 23:03:27 +08004948 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004949 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004950 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004951 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004952
4953 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004954 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004955 if (!domain->pgd)
4956 return -ENOMEM;
4957 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4958 return 0;
4959}
4960
Joerg Roedel00a77de2015-03-26 13:43:08 +01004961static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004962{
Joerg Roedel5d450802008-12-03 14:52:32 +01004963 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004964 struct iommu_domain *domain;
4965
4966 if (type != IOMMU_DOMAIN_UNMANAGED)
4967 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004968
Jiang Liuab8dfe22014-07-11 14:19:27 +08004969 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004970 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004971 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004972 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004973 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004974 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004975 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004976 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004977 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004978 }
Allen Kay8140a952011-10-14 12:32:17 -07004979 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004980
Joerg Roedel00a77de2015-03-26 13:43:08 +01004981 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004982 domain->geometry.aperture_start = 0;
4983 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4984 domain->geometry.force_aperture = true;
4985
Joerg Roedel00a77de2015-03-26 13:43:08 +01004986 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004987}
Kay, Allen M38717942008-09-09 18:37:29 +03004988
Joerg Roedel00a77de2015-03-26 13:43:08 +01004989static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004990{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004991 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004992}
Kay, Allen M38717942008-09-09 18:37:29 +03004993
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004994static int intel_iommu_attach_device(struct iommu_domain *domain,
4995 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004996{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004997 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004998 struct intel_iommu *iommu;
4999 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07005000 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03005001
Alex Williamsonc875d2c2014-07-03 09:57:02 -06005002 if (device_is_rmrr_locked(dev)) {
5003 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
5004 return -EPERM;
5005 }
5006
David Woodhouse7207d8f2014-03-09 16:31:06 -07005007 /* normally dev is not mapped */
5008 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005009 struct dmar_domain *old_domain;
5010
David Woodhouse1525a292014-03-06 16:19:30 +00005011 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005012 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02005013 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02005014 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02005015 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01005016
5017 if (!domain_type_is_vm_or_si(old_domain) &&
5018 list_empty(&old_domain->devices))
5019 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005020 }
5021 }
5022
David Woodhouse156baca2014-03-09 14:00:57 -07005023 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005024 if (!iommu)
5025 return -ENODEV;
5026
5027 /* check if this iommu agaw is sufficient for max mapped address */
5028 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01005029 if (addr_width > cap_mgaw(iommu->cap))
5030 addr_width = cap_mgaw(iommu->cap);
5031
5032 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005033 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005034 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01005035 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005036 return -EFAULT;
5037 }
Tom Lyona99c47a2010-05-17 08:20:45 +01005038 dmar_domain->gaw = addr_width;
5039
5040 /*
5041 * Knock out extra levels of page tables if necessary
5042 */
5043 while (iommu->agaw < dmar_domain->agaw) {
5044 struct dma_pte *pte;
5045
5046 pte = dmar_domain->pgd;
5047 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08005048 dmar_domain->pgd = (struct dma_pte *)
5049 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01005050 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01005051 }
5052 dmar_domain->agaw--;
5053 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005054
Joerg Roedel28ccce02015-07-21 14:45:31 +02005055 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005056}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005057
Joerg Roedel4c5478c2008-12-03 14:58:24 +01005058static void intel_iommu_detach_device(struct iommu_domain *domain,
5059 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03005060{
Joerg Roedele6de0f82015-07-22 16:30:36 +02005061 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03005062}
Kay, Allen M38717942008-09-09 18:37:29 +03005063
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005064static int intel_iommu_map(struct iommu_domain *domain,
5065 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005066 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03005067{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005068 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005069 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005070 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005071 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005072
Joerg Roedeldde57a22008-12-03 15:04:09 +01005073 if (iommu_prot & IOMMU_READ)
5074 prot |= DMA_PTE_READ;
5075 if (iommu_prot & IOMMU_WRITE)
5076 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08005077 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
5078 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005079
David Woodhouse163cc522009-06-28 00:51:17 +01005080 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005081 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005082 u64 end;
5083
5084 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01005085 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005086 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005087 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005088 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01005089 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005090 return -EFAULT;
5091 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01005092 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005093 }
David Woodhousead051222009-06-28 14:22:28 +01005094 /* Round up size to next multiple of PAGE_SIZE, if it and
5095 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01005096 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01005097 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
5098 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005099 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03005100}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005101
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005102static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00005103 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005104{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005105 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00005106 struct page *freelist = NULL;
David Woodhouseea8ea462014-03-05 17:09:32 +00005107 unsigned long start_pfn, last_pfn;
5108 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02005109 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01005110
David Woodhouse5cf0a762014-03-19 16:07:49 +00005111 /* Cope with horrid API which requires us to unmap more than the
5112 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02005113 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00005114
5115 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
5116 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
5117
David Woodhouseea8ea462014-03-05 17:09:32 +00005118 start_pfn = iova >> VTD_PAGE_SHIFT;
5119 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
5120
5121 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
5122
5123 npages = last_pfn - start_pfn + 1;
5124
Shaokun Zhangf746a022018-03-22 18:18:06 +08005125 for_each_domain_iommu(iommu_id, dmar_domain)
Joerg Roedel42e8c182015-07-21 15:50:02 +02005126 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
5127 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00005128
5129 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005130
David Woodhouse163cc522009-06-28 00:51:17 +01005131 if (dmar_domain->max_addr == iova + size)
5132 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005133
David Woodhouse5cf0a762014-03-19 16:07:49 +00005134 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005135}
Kay, Allen M38717942008-09-09 18:37:29 +03005136
Joerg Roedeld14d6572008-12-03 15:06:57 +01005137static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05305138 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03005139{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005140 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03005141 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00005142 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005143 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03005144
David Woodhouse5cf0a762014-03-19 16:07:49 +00005145 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03005146 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005147 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03005148
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005149 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03005150}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005151
Joerg Roedel5d587b82014-09-05 10:50:45 +02005152static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005153{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005154 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005155 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04005156 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005157 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005158
Joerg Roedel5d587b82014-09-05 10:50:45 +02005159 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005160}
5161
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005162static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005163{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005164 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005165 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07005166 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04005167
Alex Williamsona5459cf2014-06-12 16:12:31 -06005168 iommu = device_to_iommu(dev, &bus, &devfn);
5169 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005170 return -ENODEV;
5171
Joerg Roedele3d10af2017-02-01 17:23:22 +01005172 iommu_device_link(&iommu->iommu, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005173
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005174 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06005175
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005176 if (IS_ERR(group))
5177 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005178
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005179 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005180 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005181}
5182
5183static void intel_iommu_remove_device(struct device *dev)
5184{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005185 struct intel_iommu *iommu;
5186 u8 bus, devfn;
5187
5188 iommu = device_to_iommu(dev, &bus, &devfn);
5189 if (!iommu)
5190 return;
5191
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005192 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06005193
Joerg Roedele3d10af2017-02-01 17:23:22 +01005194 iommu_device_unlink(&iommu->iommu, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005195}
5196
Eric Auger0659b8d2017-01-19 20:57:53 +00005197static void intel_iommu_get_resv_regions(struct device *device,
5198 struct list_head *head)
5199{
5200 struct iommu_resv_region *reg;
5201 struct dmar_rmrr_unit *rmrr;
5202 struct device *i_dev;
5203 int i;
5204
5205 rcu_read_lock();
5206 for_each_rmrr_units(rmrr) {
5207 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
5208 i, i_dev) {
5209 if (i_dev != device)
5210 continue;
5211
5212 list_add_tail(&rmrr->resv->list, head);
5213 }
5214 }
5215 rcu_read_unlock();
5216
5217 reg = iommu_alloc_resv_region(IOAPIC_RANGE_START,
5218 IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00005219 0, IOMMU_RESV_MSI);
Eric Auger0659b8d2017-01-19 20:57:53 +00005220 if (!reg)
5221 return;
5222 list_add_tail(&reg->list, head);
5223}
5224
5225static void intel_iommu_put_resv_regions(struct device *dev,
5226 struct list_head *head)
5227{
5228 struct iommu_resv_region *entry, *next;
5229
5230 list_for_each_entry_safe(entry, next, head, list) {
5231 if (entry->type == IOMMU_RESV_RESERVED)
5232 kfree(entry);
5233 }
Kay, Allen M38717942008-09-09 18:37:29 +03005234}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005235
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005236#ifdef CONFIG_INTEL_IOMMU_SVM
Jacob Pan65ca7f52016-12-06 10:14:23 -08005237#define MAX_NR_PASID_BITS (20)
Lu Baolu4774cc52018-07-14 15:47:01 +08005238static inline unsigned long intel_iommu_get_pts(struct device *dev)
Jacob Pan65ca7f52016-12-06 10:14:23 -08005239{
Lu Baolu4774cc52018-07-14 15:47:01 +08005240 int pts, max_pasid;
5241
5242 max_pasid = intel_pasid_get_dev_max_id(dev);
5243 pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS);
5244 if (pts < 5)
Jacob Pan65ca7f52016-12-06 10:14:23 -08005245 return 0;
5246
Lu Baolu4774cc52018-07-14 15:47:01 +08005247 return pts - 5;
Jacob Pan65ca7f52016-12-06 10:14:23 -08005248}
5249
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005250int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
5251{
5252 struct device_domain_info *info;
5253 struct context_entry *context;
5254 struct dmar_domain *domain;
5255 unsigned long flags;
5256 u64 ctx_lo;
5257 int ret;
5258
5259 domain = get_valid_domain_for_dev(sdev->dev);
5260 if (!domain)
5261 return -EINVAL;
5262
5263 spin_lock_irqsave(&device_domain_lock, flags);
5264 spin_lock(&iommu->lock);
5265
5266 ret = -EINVAL;
5267 info = sdev->dev->archdata.iommu;
5268 if (!info || !info->pasid_supported)
5269 goto out;
5270
5271 context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
5272 if (WARN_ON(!context))
5273 goto out;
5274
5275 ctx_lo = context[0].lo;
5276
5277 sdev->did = domain->iommu_did[iommu->seq_id];
5278 sdev->sid = PCI_DEVID(info->bus, info->devfn);
5279
5280 if (!(ctx_lo & CONTEXT_PASIDE)) {
Ashok Raj11b93eb2017-08-08 13:29:28 -07005281 if (iommu->pasid_state_table)
5282 context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
Lu Baolu4774cc52018-07-14 15:47:01 +08005283 context[1].lo = (u64)virt_to_phys(info->pasid_table->table) |
5284 intel_iommu_get_pts(sdev->dev);
Jacob Pan65ca7f52016-12-06 10:14:23 -08005285
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005286 wmb();
5287 /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
5288 * extended to permit requests-with-PASID if the PASIDE bit
5289 * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
5290 * however, the PASIDE bit is ignored and requests-with-PASID
5291 * are unconditionally blocked. Which makes less sense.
5292 * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
5293 * "guest mode" translation types depending on whether ATS
5294 * is available or not. Annoyingly, we can't use the new
5295 * modes *unless* PASIDE is set. */
5296 if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
5297 ctx_lo &= ~CONTEXT_TT_MASK;
5298 if (info->ats_supported)
5299 ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
5300 else
5301 ctx_lo |= CONTEXT_TT_PT_PASID << 2;
5302 }
5303 ctx_lo |= CONTEXT_PASIDE;
David Woodhouse907fea32015-10-13 14:11:13 +01005304 if (iommu->pasid_state_table)
5305 ctx_lo |= CONTEXT_DINVE;
David Woodhousea222a7f2015-10-07 23:35:18 +01005306 if (info->pri_supported)
5307 ctx_lo |= CONTEXT_PRS;
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005308 context[0].lo = ctx_lo;
5309 wmb();
5310 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
5311 DMA_CCMD_MASK_NOBIT,
5312 DMA_CCMD_DEVICE_INVL);
5313 }
5314
5315 /* Enable PASID support in the device, if it wasn't already */
5316 if (!info->pasid_enabled)
5317 iommu_enable_dev_iotlb(info);
5318
5319 if (info->ats_enabled) {
5320 sdev->dev_iotlb = 1;
5321 sdev->qdep = info->ats_qdep;
5322 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
5323 sdev->qdep = 0;
5324 }
5325 ret = 0;
5326
5327 out:
5328 spin_unlock(&iommu->lock);
5329 spin_unlock_irqrestore(&device_domain_lock, flags);
5330
5331 return ret;
5332}
5333
5334struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5335{
5336 struct intel_iommu *iommu;
5337 u8 bus, devfn;
5338
5339 if (iommu_dummy(dev)) {
5340 dev_warn(dev,
5341 "No IOMMU translation for device; cannot enable SVM\n");
5342 return NULL;
5343 }
5344
5345 iommu = device_to_iommu(dev, &bus, &devfn);
5346 if ((!iommu)) {
Sudeep Duttb9997e32015-10-18 20:54:37 -07005347 dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005348 return NULL;
5349 }
5350
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005351 return iommu;
5352}
5353#endif /* CONFIG_INTEL_IOMMU_SVM */
5354
Joerg Roedelb0119e82017-02-01 13:23:08 +01005355const struct iommu_ops intel_iommu_ops = {
Eric Auger0659b8d2017-01-19 20:57:53 +00005356 .capable = intel_iommu_capable,
5357 .domain_alloc = intel_iommu_domain_alloc,
5358 .domain_free = intel_iommu_domain_free,
5359 .attach_dev = intel_iommu_attach_device,
5360 .detach_dev = intel_iommu_detach_device,
5361 .map = intel_iommu_map,
5362 .unmap = intel_iommu_unmap,
Eric Auger0659b8d2017-01-19 20:57:53 +00005363 .iova_to_phys = intel_iommu_iova_to_phys,
5364 .add_device = intel_iommu_add_device,
5365 .remove_device = intel_iommu_remove_device,
5366 .get_resv_regions = intel_iommu_get_resv_regions,
5367 .put_resv_regions = intel_iommu_put_resv_regions,
5368 .device_group = pci_device_group,
5369 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005370};
David Woodhouse9af88142009-02-13 23:18:03 +00005371
Daniel Vetter94526182013-01-20 23:50:13 +01005372static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
5373{
5374 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005375 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01005376 dmar_map_gfx = 0;
5377}
5378
5379DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
5380DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
5381DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
5382DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
5383DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
5384DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
5385DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
5386
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005387static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00005388{
5389 /*
5390 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01005391 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00005392 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005393 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005394 rwbf_quirk = 1;
5395}
5396
5397DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005398DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5399DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5400DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5401DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5402DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5403DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005404
Adam Jacksoneecfd572010-08-25 21:17:34 +01005405#define GGC 0x52
5406#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5407#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5408#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5409#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5410#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5411#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5412#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5413#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5414
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005415static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005416{
5417 unsigned short ggc;
5418
Adam Jacksoneecfd572010-08-25 21:17:34 +01005419 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005420 return;
5421
Adam Jacksoneecfd572010-08-25 21:17:34 +01005422 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005423 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005424 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005425 } else if (dmar_map_gfx) {
5426 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005427 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005428 intel_iommu_strict = 1;
5429 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005430}
5431DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5432DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5433DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5434DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5435
David Woodhousee0fc7e02009-09-30 09:12:17 -07005436/* On Tylersburg chipsets, some BIOSes have been known to enable the
5437 ISOCH DMAR unit for the Azalia sound device, but not give it any
5438 TLB entries, which causes it to deadlock. Check for that. We do
5439 this in a function called from init_dmars(), instead of in a PCI
5440 quirk, because we don't want to print the obnoxious "BIOS broken"
5441 message if VT-d is actually disabled.
5442*/
5443static void __init check_tylersburg_isoch(void)
5444{
5445 struct pci_dev *pdev;
5446 uint32_t vtisochctrl;
5447
5448 /* If there's no Azalia in the system anyway, forget it. */
5449 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5450 if (!pdev)
5451 return;
5452 pci_dev_put(pdev);
5453
5454 /* System Management Registers. Might be hidden, in which case
5455 we can't do the sanity check. But that's OK, because the
5456 known-broken BIOSes _don't_ actually hide it, so far. */
5457 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5458 if (!pdev)
5459 return;
5460
5461 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5462 pci_dev_put(pdev);
5463 return;
5464 }
5465
5466 pci_dev_put(pdev);
5467
5468 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5469 if (vtisochctrl & 1)
5470 return;
5471
5472 /* Drop all bits other than the number of TLB entries */
5473 vtisochctrl &= 0x1c;
5474
5475 /* If we have the recommended number of TLB entries (16), fine. */
5476 if (vtisochctrl == 0x10)
5477 return;
5478
5479 /* Zero TLB entries? You get to ride the short bus to school. */
5480 if (!vtisochctrl) {
5481 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5482 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5483 dmi_get_system_info(DMI_BIOS_VENDOR),
5484 dmi_get_system_info(DMI_BIOS_VERSION),
5485 dmi_get_system_info(DMI_PRODUCT_VERSION));
5486 iommu_identity_mapping |= IDENTMAP_AZALIA;
5487 return;
5488 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005489
5490 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005491 vtisochctrl);
5492}