blob: 30e8584137f537dbd66cd006c7cf91e080fed8fc [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;
Lu Baolu89a60792018-10-23 15:45:01 +0800187static int no_platform_optin;
Joseph Cihulab7792602011-05-03 00:08:37 -0700188
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000189#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000190
Joerg Roedel091d42e2015-06-12 11:56:10 +0200191/*
192 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
193 * if marked present.
194 */
195static phys_addr_t root_entry_lctp(struct root_entry *re)
196{
197 if (!(re->lo & 1))
198 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000199
Joerg Roedel091d42e2015-06-12 11:56:10 +0200200 return re->lo & VTD_PAGE_MASK;
201}
202
203/*
204 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
205 * if marked present.
206 */
207static phys_addr_t root_entry_uctp(struct root_entry *re)
208{
209 if (!(re->hi & 1))
210 return 0;
211
212 return re->hi & VTD_PAGE_MASK;
213}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000214
Joerg Roedelcf484d02015-06-12 12:21:46 +0200215static inline void context_clear_pasid_enable(struct context_entry *context)
216{
217 context->lo &= ~(1ULL << 11);
218}
219
220static inline bool context_pasid_enabled(struct context_entry *context)
221{
222 return !!(context->lo & (1ULL << 11));
223}
224
225static inline void context_set_copied(struct context_entry *context)
226{
227 context->hi |= (1ull << 3);
228}
229
230static inline bool context_copied(struct context_entry *context)
231{
232 return !!(context->hi & (1ULL << 3));
233}
234
235static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000236{
237 return (context->lo & 1);
238}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200239
Sohil Mehta26b86092018-09-11 17:11:36 -0700240bool context_present(struct context_entry *context)
Joerg Roedelcf484d02015-06-12 12:21:46 +0200241{
242 return context_pasid_enabled(context) ?
243 __context_present(context) :
244 __context_present(context) && !context_copied(context);
245}
246
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000247static inline void context_set_present(struct context_entry *context)
248{
249 context->lo |= 1;
250}
251
252static inline void context_set_fault_enable(struct context_entry *context)
253{
254 context->lo &= (((u64)-1) << 2) | 1;
255}
256
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000257static inline void context_set_translation_type(struct context_entry *context,
258 unsigned long value)
259{
260 context->lo &= (((u64)-1) << 4) | 3;
261 context->lo |= (value & 3) << 2;
262}
263
264static inline void context_set_address_root(struct context_entry *context,
265 unsigned long value)
266{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800267 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000268 context->lo |= value & VTD_PAGE_MASK;
269}
270
271static inline void context_set_address_width(struct context_entry *context,
272 unsigned long value)
273{
274 context->hi |= value & 7;
275}
276
277static inline void context_set_domain_id(struct context_entry *context,
278 unsigned long value)
279{
280 context->hi |= (value & ((1 << 16) - 1)) << 8;
281}
282
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200283static inline int context_domain_id(struct context_entry *c)
284{
285 return((c->hi >> 8) & 0xffff);
286}
287
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000288static inline void context_clear_entry(struct context_entry *context)
289{
290 context->lo = 0;
291 context->hi = 0;
292}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000293
Mark McLoughlin622ba122008-11-20 15:49:46 +0000294/*
295 * 0: readable
296 * 1: writable
297 * 2-6: reserved
298 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800299 * 8-10: available
300 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000301 * 12-63: Host physcial address
302 */
303struct dma_pte {
304 u64 val;
305};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000306
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000307static inline void dma_clear_pte(struct dma_pte *pte)
308{
309 pte->val = 0;
310}
311
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000312static inline u64 dma_pte_addr(struct dma_pte *pte)
313{
David Woodhousec85994e2009-07-01 19:21:24 +0100314#ifdef CONFIG_64BIT
315 return pte->val & VTD_PAGE_MASK;
316#else
317 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100318 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100319#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000320}
321
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000322static inline bool dma_pte_present(struct dma_pte *pte)
323{
324 return (pte->val & 3) != 0;
325}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000326
Allen Kay4399c8b2011-10-14 12:32:46 -0700327static inline bool dma_pte_superpage(struct dma_pte *pte)
328{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200329 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700330}
331
David Woodhouse75e6bf92009-07-02 11:21:16 +0100332static inline int first_pte_in_page(struct dma_pte *pte)
333{
334 return !((unsigned long)pte & ~VTD_PAGE_MASK);
335}
336
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700337/*
338 * This domain is a statically identity mapping domain.
339 * 1. This domain creats a static 1:1 mapping to all usable memory.
340 * 2. It maps to each iommu if successful.
341 * 3. Each iommu mapps to this domain if successful.
342 */
David Woodhouse19943b02009-08-04 16:19:20 +0100343static struct dmar_domain *si_domain;
344static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700345
Joerg Roedel28ccce02015-07-21 14:45:31 +0200346/*
347 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800348 * across iommus may be owned in one domain, e.g. kvm guest.
349 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800350#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800351
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700352/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800353#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700354
Joerg Roedel29a27712015-07-21 17:17:12 +0200355#define for_each_domain_iommu(idx, domain) \
356 for (idx = 0; idx < g_num_of_iommus; idx++) \
357 if (domain->iommu_refcnt[idx])
358
Jiang Liub94e4112014-02-19 14:07:25 +0800359struct dmar_rmrr_unit {
360 struct list_head list; /* list of rmrr units */
361 struct acpi_dmar_header *hdr; /* ACPI header */
362 u64 base_address; /* reserved base address*/
363 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000364 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800365 int devices_cnt; /* target device count */
Eric Auger0659b8d2017-01-19 20:57:53 +0000366 struct iommu_resv_region *resv; /* reserved region handle */
Jiang Liub94e4112014-02-19 14:07:25 +0800367};
368
369struct dmar_atsr_unit {
370 struct list_head list; /* list of ATSR units */
371 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000372 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800373 int devices_cnt; /* target device count */
374 u8 include_all:1; /* include all ports */
375};
376
377static LIST_HEAD(dmar_atsr_units);
378static LIST_HEAD(dmar_rmrr_units);
379
380#define for_each_rmrr_units(rmrr) \
381 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
382
mark gross5e0d2a62008-03-04 15:22:08 -0800383/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800384static int g_num_of_iommus;
385
Jiang Liu92d03cc2014-02-19 14:07:28 +0800386static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700387static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200388static void dmar_remove_one_dev_info(struct dmar_domain *domain,
389 struct device *dev);
Joerg Roedel127c7612015-07-23 17:44:46 +0200390static void __dmar_remove_one_dev_info(struct device_domain_info *info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200391static void domain_context_clear(struct intel_iommu *iommu,
392 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800393static int domain_detach_iommu(struct dmar_domain *domain,
394 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700395
Suresh Siddhad3f13812011-08-23 17:05:25 -0700396#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800397int dmar_disabled = 0;
398#else
399int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700400#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800401
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200402int intel_iommu_enabled = 0;
403EXPORT_SYMBOL_GPL(intel_iommu_enabled);
404
David Woodhouse2d9e6672010-06-15 10:57:57 +0100405static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700406static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800407static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100408static int intel_iommu_superpage = 1;
David Woodhousec83b2f22015-06-12 10:15:49 +0100409static int intel_iommu_ecs = 1;
Lu Baolu2db15812018-07-08 14:23:21 +0800410static int intel_iommu_pasid28;
David Woodhouseae853dd2015-09-09 11:58:59 +0100411static int iommu_identity_mapping;
David Woodhousec83b2f22015-06-12 10:15:49 +0100412
David Woodhouseae853dd2015-09-09 11:58:59 +0100413#define IDENTMAP_ALL 1
414#define IDENTMAP_GFX 2
415#define IDENTMAP_AZALIA 4
David Woodhousec83b2f22015-06-12 10:15:49 +0100416
Lu Baolu2db15812018-07-08 14:23:21 +0800417/* Broadwell and Skylake have broken ECS support — normal so-called "second
418 * level" translation of DMA requests-without-PASID doesn't actually happen
419 * unless you also set the NESTE bit in an extended context-entry. Which of
420 * course means that SVM doesn't work because it's trying to do nested
421 * translation of the physical addresses it finds in the process page tables,
422 * through the IOVA->phys mapping found in the "second level" page tables.
423 *
424 * The VT-d specification was retroactively changed to change the definition
425 * of the capability bits and pretend that Broadwell/Skylake never happened...
426 * but unfortunately the wrong bit was changed. It's ECS which is broken, but
427 * for some reason it was the PASID capability bit which was redefined (from
428 * bit 28 on BDW/SKL to bit 40 in future).
429 *
430 * So our test for ECS needs to eschew those implementations which set the old
431 * PASID capabiity bit 28, since those are the ones on which ECS is broken.
432 * Unless we are working around the 'pasid28' limitations, that is, by putting
433 * the device into passthrough mode for normal DMA and thus masking the bug.
434 */
435#define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap) && \
436 (intel_iommu_pasid28 || !ecap_broken_pasid(iommu->ecap)))
437/* PASID support is thus enabled if ECS is enabled and *either* of the old
438 * or new capability bits are set. */
439#define pasid_enabled(iommu) (ecs_enabled(iommu) && \
440 (ecap_pasid(iommu->ecap) || ecap_broken_pasid(iommu->ecap)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700441
David Woodhousec0771df2011-10-14 20:59:46 +0100442int intel_iommu_gfx_mapped;
443EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
444
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700445#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
446static DEFINE_SPINLOCK(device_domain_lock);
447static LIST_HEAD(device_domain_list);
448
Lu Baolu85319dc2018-07-14 15:46:58 +0800449/*
450 * Iterate over elements in device_domain_list and call the specified
451 * callback @fn against each element. This helper should only be used
452 * in the context where the device_domain_lock has already been holden.
453 */
454int for_each_device_domain(int (*fn)(struct device_domain_info *info,
455 void *data), void *data)
456{
457 int ret = 0;
458 struct device_domain_info *info;
459
460 assert_spin_locked(&device_domain_lock);
461 list_for_each_entry(info, &device_domain_list, global) {
462 ret = fn(info, data);
463 if (ret)
464 return ret;
465 }
466
467 return 0;
468}
469
Joerg Roedelb0119e82017-02-01 13:23:08 +0100470const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100471
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200472static bool translation_pre_enabled(struct intel_iommu *iommu)
473{
474 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
475}
476
Joerg Roedel091d42e2015-06-12 11:56:10 +0200477static void clear_translation_pre_enabled(struct intel_iommu *iommu)
478{
479 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
480}
481
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200482static void init_translation_status(struct intel_iommu *iommu)
483{
484 u32 gsts;
485
486 gsts = readl(iommu->reg + DMAR_GSTS_REG);
487 if (gsts & DMA_GSTS_TES)
488 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
489}
490
Joerg Roedel00a77de2015-03-26 13:43:08 +0100491/* Convert generic 'struct iommu_domain to private struct dmar_domain */
492static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
493{
494 return container_of(dom, struct dmar_domain, domain);
495}
496
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700497static int __init intel_iommu_setup(char *str)
498{
499 if (!str)
500 return -EINVAL;
501 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800502 if (!strncmp(str, "on", 2)) {
503 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200504 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800505 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700506 dmar_disabled = 1;
Lu Baolu89a60792018-10-23 15:45:01 +0800507 no_platform_optin = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200508 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700509 } else if (!strncmp(str, "igfx_off", 8)) {
510 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200511 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700512 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200513 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700514 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800515 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200516 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800517 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100518 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200519 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100520 intel_iommu_superpage = 0;
David Woodhousec83b2f22015-06-12 10:15:49 +0100521 } else if (!strncmp(str, "ecs_off", 7)) {
522 printk(KERN_INFO
523 "Intel-IOMMU: disable extended context table support\n");
524 intel_iommu_ecs = 0;
Lu Baolu2db15812018-07-08 14:23:21 +0800525 } else if (!strncmp(str, "pasid28", 7)) {
526 printk(KERN_INFO
527 "Intel-IOMMU: enable pre-production PASID support\n");
528 intel_iommu_pasid28 = 1;
529 iommu_identity_mapping |= IDENTMAP_GFX;
Shaohua Libfd20f12017-04-26 09:18:35 -0700530 } else if (!strncmp(str, "tboot_noforce", 13)) {
531 printk(KERN_INFO
532 "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");
533 intel_iommu_tboot_noforce = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700534 }
535
536 str += strcspn(str, ",");
537 while (*str == ',')
538 str++;
539 }
540 return 0;
541}
542__setup("intel_iommu=", intel_iommu_setup);
543
544static struct kmem_cache *iommu_domain_cache;
545static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700546
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200547static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
548{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200549 struct dmar_domain **domains;
550 int idx = did >> 8;
551
552 domains = iommu->domains[idx];
553 if (!domains)
554 return NULL;
555
556 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200557}
558
559static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
560 struct dmar_domain *domain)
561{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200562 struct dmar_domain **domains;
563 int idx = did >> 8;
564
565 if (!iommu->domains[idx]) {
566 size_t size = 256 * sizeof(struct dmar_domain *);
567 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
568 }
569
570 domains = iommu->domains[idx];
571 if (WARN_ON(!domains))
572 return;
573 else
574 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200575}
576
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800577void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700578{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700579 struct page *page;
580 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700581
Suresh Siddha4c923d42009-10-02 11:01:24 -0700582 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
583 if (page)
584 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700585 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700586}
587
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800588void free_pgtable_page(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700589{
590 free_page((unsigned long)vaddr);
591}
592
593static inline void *alloc_domain_mem(void)
594{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900595 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700596}
597
Kay, Allen M38717942008-09-09 18:37:29 +0300598static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700599{
600 kmem_cache_free(iommu_domain_cache, vaddr);
601}
602
603static inline void * alloc_devinfo_mem(void)
604{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900605 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700606}
607
608static inline void free_devinfo_mem(void *vaddr)
609{
610 kmem_cache_free(iommu_devinfo_cache, vaddr);
611}
612
Jiang Liuab8dfe22014-07-11 14:19:27 +0800613static inline int domain_type_is_vm(struct dmar_domain *domain)
614{
615 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
616}
617
Joerg Roedel28ccce02015-07-21 14:45:31 +0200618static inline int domain_type_is_si(struct dmar_domain *domain)
619{
620 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
621}
622
Jiang Liuab8dfe22014-07-11 14:19:27 +0800623static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
624{
625 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
626 DOMAIN_FLAG_STATIC_IDENTITY);
627}
Weidong Han1b573682008-12-08 15:34:06 +0800628
Jiang Liu162d1b12014-07-11 14:19:35 +0800629static inline int domain_pfn_supported(struct dmar_domain *domain,
630 unsigned long pfn)
631{
632 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
633
634 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
635}
636
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700637static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800638{
639 unsigned long sagaw;
640 int agaw = -1;
641
642 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700643 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800644 agaw >= 0; agaw--) {
645 if (test_bit(agaw, &sagaw))
646 break;
647 }
648
649 return agaw;
650}
651
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700652/*
653 * Calculate max SAGAW for each iommu.
654 */
655int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
656{
657 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
658}
659
660/*
661 * calculate agaw for each iommu.
662 * "SAGAW" may be different across iommus, use a default agaw, and
663 * get a supported less agaw for iommus that don't support the default agaw.
664 */
665int iommu_calculate_agaw(struct intel_iommu *iommu)
666{
667 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
668}
669
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700670/* This functionin only returns single iommu in a domain */
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800671struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
Weidong Han8c11e792008-12-08 15:29:22 +0800672{
673 int iommu_id;
674
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700675 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800676 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200677 for_each_domain_iommu(iommu_id, domain)
678 break;
679
Weidong Han8c11e792008-12-08 15:29:22 +0800680 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
681 return NULL;
682
683 return g_iommus[iommu_id];
684}
685
Weidong Han8e6040972008-12-08 15:49:06 +0800686static void domain_update_iommu_coherency(struct dmar_domain *domain)
687{
David Woodhoused0501962014-03-11 17:10:29 -0700688 struct dmar_drhd_unit *drhd;
689 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100690 bool found = false;
691 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800692
David Woodhoused0501962014-03-11 17:10:29 -0700693 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800694
Joerg Roedel29a27712015-07-21 17:17:12 +0200695 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100696 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800697 if (!ecap_coherent(g_iommus[i]->ecap)) {
698 domain->iommu_coherency = 0;
699 break;
700 }
Weidong Han8e6040972008-12-08 15:49:06 +0800701 }
David Woodhoused0501962014-03-11 17:10:29 -0700702 if (found)
703 return;
704
705 /* No hardware attached; use lowest common denominator */
706 rcu_read_lock();
707 for_each_active_iommu(iommu, drhd) {
708 if (!ecap_coherent(iommu->ecap)) {
709 domain->iommu_coherency = 0;
710 break;
711 }
712 }
713 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800714}
715
Jiang Liu161f6932014-07-11 14:19:37 +0800716static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100717{
Allen Kay8140a952011-10-14 12:32:17 -0700718 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800719 struct intel_iommu *iommu;
720 int ret = 1;
721
722 rcu_read_lock();
723 for_each_active_iommu(iommu, drhd) {
724 if (iommu != skip) {
725 if (!ecap_sc_support(iommu->ecap)) {
726 ret = 0;
727 break;
728 }
729 }
730 }
731 rcu_read_unlock();
732
733 return ret;
734}
735
736static int domain_update_iommu_superpage(struct intel_iommu *skip)
737{
738 struct dmar_drhd_unit *drhd;
739 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700740 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100741
742 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800743 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100744 }
745
Allen Kay8140a952011-10-14 12:32:17 -0700746 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e242612014-02-19 14:07:34 +0800747 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700748 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800749 if (iommu != skip) {
750 mask &= cap_super_page_val(iommu->cap);
751 if (!mask)
752 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100753 }
754 }
Jiang Liu0e242612014-02-19 14:07:34 +0800755 rcu_read_unlock();
756
Jiang Liu161f6932014-07-11 14:19:37 +0800757 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100758}
759
Sheng Yang58c610b2009-03-18 15:33:05 +0800760/* Some capabilities may be different across iommus */
761static void domain_update_iommu_cap(struct dmar_domain *domain)
762{
763 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800764 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
765 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800766}
767
Sohil Mehta26b86092018-09-11 17:11:36 -0700768struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
769 u8 devfn, int alloc)
David Woodhouse03ecc322015-02-13 14:35:21 +0000770{
771 struct root_entry *root = &iommu->root_entry[bus];
772 struct context_entry *context;
773 u64 *entry;
774
Joerg Roedel4df4eab2015-08-25 10:54:28 +0200775 entry = &root->lo;
David Woodhousec83b2f22015-06-12 10:15:49 +0100776 if (ecs_enabled(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000777 if (devfn >= 0x80) {
778 devfn -= 0x80;
779 entry = &root->hi;
780 }
781 devfn *= 2;
782 }
David Woodhouse03ecc322015-02-13 14:35:21 +0000783 if (*entry & 1)
784 context = phys_to_virt(*entry & VTD_PAGE_MASK);
785 else {
786 unsigned long phy_addr;
787 if (!alloc)
788 return NULL;
789
790 context = alloc_pgtable_page(iommu->node);
791 if (!context)
792 return NULL;
793
794 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
795 phy_addr = virt_to_phys((void *)context);
796 *entry = phy_addr | 1;
797 __iommu_flush_cache(iommu, entry, sizeof(*entry));
798 }
799 return &context[devfn];
800}
801
David Woodhouse4ed6a542015-05-11 14:59:20 +0100802static int iommu_dummy(struct device *dev)
803{
804 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
805}
806
David Woodhouse156baca2014-03-09 14:00:57 -0700807static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800808{
809 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800810 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700811 struct device *tmp;
812 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800813 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800814 int i;
815
David Woodhouse4ed6a542015-05-11 14:59:20 +0100816 if (iommu_dummy(dev))
817 return NULL;
818
David Woodhouse156baca2014-03-09 14:00:57 -0700819 if (dev_is_pci(dev)) {
Ashok Raj1c387182016-10-21 15:32:05 -0700820 struct pci_dev *pf_pdev;
821
David Woodhouse156baca2014-03-09 14:00:57 -0700822 pdev = to_pci_dev(dev);
Jon Derrick5823e332017-08-30 15:05:59 -0600823
824#ifdef CONFIG_X86
825 /* VMD child devices currently cannot be handled individually */
826 if (is_vmd(pdev->bus))
827 return NULL;
828#endif
829
Ashok Raj1c387182016-10-21 15:32:05 -0700830 /* VFs aren't listed in scope tables; we need to look up
831 * the PF instead to find the IOMMU. */
832 pf_pdev = pci_physfn(pdev);
833 dev = &pf_pdev->dev;
David Woodhouse156baca2014-03-09 14:00:57 -0700834 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100835 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700836 dev = &ACPI_COMPANION(dev)->dev;
837
Jiang Liu0e242612014-02-19 14:07:34 +0800838 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800839 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700840 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100841 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800842
Jiang Liub683b232014-02-19 14:07:32 +0800843 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700844 drhd->devices_cnt, i, tmp) {
845 if (tmp == dev) {
Ashok Raj1c387182016-10-21 15:32:05 -0700846 /* For a VF use its original BDF# not that of the PF
847 * which we used for the IOMMU lookup. Strictly speaking
848 * we could do this for all PCI devices; we only need to
849 * get the BDF# from the scope table for ACPI matches. */
Koos Vriezen5003ae12017-03-01 21:02:50 +0100850 if (pdev && pdev->is_virtfn)
Ashok Raj1c387182016-10-21 15:32:05 -0700851 goto got_pdev;
852
David Woodhouse156baca2014-03-09 14:00:57 -0700853 *bus = drhd->devices[i].bus;
854 *devfn = drhd->devices[i].devfn;
855 goto out;
856 }
857
858 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000859 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700860
861 ptmp = to_pci_dev(tmp);
862 if (ptmp->subordinate &&
863 ptmp->subordinate->number <= pdev->bus->number &&
864 ptmp->subordinate->busn_res.end >= pdev->bus->number)
865 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100866 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800867
David Woodhouse156baca2014-03-09 14:00:57 -0700868 if (pdev && drhd->include_all) {
869 got_pdev:
870 *bus = pdev->bus->number;
871 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800872 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700873 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800874 }
Jiang Liub683b232014-02-19 14:07:32 +0800875 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700876 out:
Jiang Liu0e242612014-02-19 14:07:34 +0800877 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800878
Jiang Liub683b232014-02-19 14:07:32 +0800879 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800880}
881
Weidong Han5331fe62008-12-08 23:00:00 +0800882static void domain_flush_cache(struct dmar_domain *domain,
883 void *addr, int size)
884{
885 if (!domain->iommu_coherency)
886 clflush_cache_range(addr, size);
887}
888
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700889static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
890{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700891 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000892 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700893 unsigned long flags;
894
895 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000896 context = iommu_context_addr(iommu, bus, devfn, 0);
897 if (context)
898 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700899 spin_unlock_irqrestore(&iommu->lock, flags);
900 return ret;
901}
902
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700903static void free_context_table(struct intel_iommu *iommu)
904{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700905 int i;
906 unsigned long flags;
907 struct context_entry *context;
908
909 spin_lock_irqsave(&iommu->lock, flags);
910 if (!iommu->root_entry) {
911 goto out;
912 }
913 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000914 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700915 if (context)
916 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000917
David Woodhousec83b2f22015-06-12 10:15:49 +0100918 if (!ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000919 continue;
920
921 context = iommu_context_addr(iommu, i, 0x80, 0);
922 if (context)
923 free_pgtable_page(context);
924
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700925 }
926 free_pgtable_page(iommu->root_entry);
927 iommu->root_entry = NULL;
928out:
929 spin_unlock_irqrestore(&iommu->lock, flags);
930}
931
David Woodhouseb026fd22009-06-28 10:37:25 +0100932static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000933 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700934{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700935 struct dma_pte *parent, *pte = NULL;
936 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700937 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700938
939 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200940
Jiang Liu162d1b12014-07-11 14:19:35 +0800941 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200942 /* Address beyond IOMMU's addressing capabilities. */
943 return NULL;
944
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700945 parent = domain->pgd;
946
David Woodhouse5cf0a762014-03-19 16:07:49 +0000947 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700948 void *tmp_page;
949
David Woodhouseb026fd22009-06-28 10:37:25 +0100950 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700951 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000952 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100953 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000954 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700955 break;
956
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000957 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100958 uint64_t pteval;
959
Suresh Siddha4c923d42009-10-02 11:01:24 -0700960 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700961
David Woodhouse206a73c2009-07-01 19:30:28 +0100962 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700963 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +0100964
David Woodhousec85994e2009-07-01 19:21:24 +0100965 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400966 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 +0800967 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100968 /* Someone else set it while we were thinking; use theirs. */
969 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800970 else
David Woodhousec85994e2009-07-01 19:21:24 +0100971 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000973 if (level == 1)
974 break;
975
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000976 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700977 level--;
978 }
979
David Woodhouse5cf0a762014-03-19 16:07:49 +0000980 if (!*target_level)
981 *target_level = level;
982
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700983 return pte;
984}
985
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100986
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700987/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100988static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
989 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100990 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700991{
992 struct dma_pte *parent, *pte = NULL;
993 int total = agaw_to_level(domain->agaw);
994 int offset;
995
996 parent = domain->pgd;
997 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100998 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700999 pte = &parent[offset];
1000 if (level == total)
1001 return pte;
1002
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001003 if (!dma_pte_present(pte)) {
1004 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001005 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001006 }
1007
Yijing Wange16922a2014-05-20 20:37:51 +08001008 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001009 *large_page = total;
1010 return pte;
1011 }
1012
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001013 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001014 total--;
1015 }
1016 return NULL;
1017}
1018
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001019/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +00001020static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf52009-06-27 22:09:11 +01001021 unsigned long start_pfn,
1022 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001023{
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001024 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +01001025 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001026
Jiang Liu162d1b12014-07-11 14:19:35 +08001027 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1028 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001029 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001030
David Woodhouse04b18e62009-06-27 19:15:01 +01001031 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001032 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001033 large_page = 1;
1034 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001035 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001036 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001037 continue;
1038 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001039 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001040 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001041 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001042 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001043 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1044
David Woodhouse310a5ab2009-06-28 18:52:20 +01001045 domain_flush_cache(domain, first_pte,
1046 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001047
1048 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001049}
1050
Alex Williamson3269ee02013-06-15 10:27:19 -06001051static void dma_pte_free_level(struct dmar_domain *domain, int level,
David Dillowbc24c572017-06-28 19:42:23 -07001052 int retain_level, struct dma_pte *pte,
1053 unsigned long pfn, unsigned long start_pfn,
1054 unsigned long last_pfn)
Alex Williamson3269ee02013-06-15 10:27:19 -06001055{
1056 pfn = max(start_pfn, pfn);
1057 pte = &pte[pfn_level_offset(pfn, level)];
1058
1059 do {
1060 unsigned long level_pfn;
1061 struct dma_pte *level_pte;
1062
1063 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1064 goto next;
1065
David Dillowf7116e12017-01-30 19:11:11 -08001066 level_pfn = pfn & level_mask(level);
Alex Williamson3269ee02013-06-15 10:27:19 -06001067 level_pte = phys_to_virt(dma_pte_addr(pte));
1068
David Dillowbc24c572017-06-28 19:42:23 -07001069 if (level > 2) {
1070 dma_pte_free_level(domain, level - 1, retain_level,
1071 level_pte, level_pfn, start_pfn,
1072 last_pfn);
1073 }
Alex Williamson3269ee02013-06-15 10:27:19 -06001074
David Dillowbc24c572017-06-28 19:42:23 -07001075 /*
1076 * Free the page table if we're below the level we want to
1077 * retain and the range covers the entire table.
1078 */
1079 if (level < retain_level && !(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001080 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001081 dma_clear_pte(pte);
1082 domain_flush_cache(domain, pte, sizeof(*pte));
1083 free_pgtable_page(level_pte);
1084 }
1085next:
1086 pfn += level_size(level);
1087 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1088}
1089
David Dillowbc24c572017-06-28 19:42:23 -07001090/*
1091 * clear last level (leaf) ptes and free page table pages below the
1092 * level we wish to keep intact.
1093 */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001094static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001095 unsigned long start_pfn,
David Dillowbc24c572017-06-28 19:42:23 -07001096 unsigned long last_pfn,
1097 int retain_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001098{
Jiang Liu162d1b12014-07-11 14:19:35 +08001099 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1100 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001101 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001102
Jiang Liud41a4ad2014-07-11 14:19:34 +08001103 dma_pte_clear_range(domain, start_pfn, last_pfn);
1104
David Woodhousef3a0a522009-06-30 03:40:07 +01001105 /* We don't need lock here; nobody else touches the iova range */
David Dillowbc24c572017-06-28 19:42:23 -07001106 dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level,
Alex Williamson3269ee02013-06-15 10:27:19 -06001107 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001108
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001109 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001110 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001111 free_pgtable_page(domain->pgd);
1112 domain->pgd = NULL;
1113 }
1114}
1115
David Woodhouseea8ea462014-03-05 17:09:32 +00001116/* When a page at a given level is being unlinked from its parent, we don't
1117 need to *modify* it at all. All we need to do is make a list of all the
1118 pages which can be freed just as soon as we've flushed the IOTLB and we
1119 know the hardware page-walk will no longer touch them.
1120 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1121 be freed. */
1122static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1123 int level, struct dma_pte *pte,
1124 struct page *freelist)
1125{
1126 struct page *pg;
1127
1128 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1129 pg->freelist = freelist;
1130 freelist = pg;
1131
1132 if (level == 1)
1133 return freelist;
1134
Jiang Liuadeb2592014-04-09 10:20:39 +08001135 pte = page_address(pg);
1136 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001137 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1138 freelist = dma_pte_list_pagetables(domain, level - 1,
1139 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001140 pte++;
1141 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001142
1143 return freelist;
1144}
1145
1146static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1147 struct dma_pte *pte, unsigned long pfn,
1148 unsigned long start_pfn,
1149 unsigned long last_pfn,
1150 struct page *freelist)
1151{
1152 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1153
1154 pfn = max(start_pfn, pfn);
1155 pte = &pte[pfn_level_offset(pfn, level)];
1156
1157 do {
1158 unsigned long level_pfn;
1159
1160 if (!dma_pte_present(pte))
1161 goto next;
1162
1163 level_pfn = pfn & level_mask(level);
1164
1165 /* If range covers entire pagetable, free it */
1166 if (start_pfn <= level_pfn &&
1167 last_pfn >= level_pfn + level_size(level) - 1) {
1168 /* These suborbinate page tables are going away entirely. Don't
1169 bother to clear them; we're just going to *free* them. */
1170 if (level > 1 && !dma_pte_superpage(pte))
1171 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1172
1173 dma_clear_pte(pte);
1174 if (!first_pte)
1175 first_pte = pte;
1176 last_pte = pte;
1177 } else if (level > 1) {
1178 /* Recurse down into a level that isn't *entirely* obsolete */
1179 freelist = dma_pte_clear_level(domain, level - 1,
1180 phys_to_virt(dma_pte_addr(pte)),
1181 level_pfn, start_pfn, last_pfn,
1182 freelist);
1183 }
1184next:
1185 pfn += level_size(level);
1186 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1187
1188 if (first_pte)
1189 domain_flush_cache(domain, first_pte,
1190 (void *)++last_pte - (void *)first_pte);
1191
1192 return freelist;
1193}
1194
1195/* We can't just free the pages because the IOMMU may still be walking
1196 the page tables, and may have cached the intermediate levels. The
1197 pages can only be freed after the IOTLB flush has been done. */
Joerg Roedelb6904202015-08-13 11:32:18 +02001198static struct page *domain_unmap(struct dmar_domain *domain,
1199 unsigned long start_pfn,
1200 unsigned long last_pfn)
David Woodhouseea8ea462014-03-05 17:09:32 +00001201{
David Woodhouseea8ea462014-03-05 17:09:32 +00001202 struct page *freelist = NULL;
1203
Jiang Liu162d1b12014-07-11 14:19:35 +08001204 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1205 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001206 BUG_ON(start_pfn > last_pfn);
1207
1208 /* we don't need lock here; nobody else touches the iova range */
1209 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1210 domain->pgd, 0, start_pfn, last_pfn, NULL);
1211
1212 /* free pgd */
1213 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1214 struct page *pgd_page = virt_to_page(domain->pgd);
1215 pgd_page->freelist = freelist;
1216 freelist = pgd_page;
1217
1218 domain->pgd = NULL;
1219 }
1220
1221 return freelist;
1222}
1223
Joerg Roedelb6904202015-08-13 11:32:18 +02001224static void dma_free_pagelist(struct page *freelist)
David Woodhouseea8ea462014-03-05 17:09:32 +00001225{
1226 struct page *pg;
1227
1228 while ((pg = freelist)) {
1229 freelist = pg->freelist;
1230 free_pgtable_page(page_address(pg));
1231 }
1232}
1233
Joerg Roedel13cf0172017-08-11 11:40:10 +02001234static void iova_entry_free(unsigned long data)
1235{
1236 struct page *freelist = (struct page *)data;
1237
1238 dma_free_pagelist(freelist);
1239}
1240
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241/* iommu handling */
1242static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1243{
1244 struct root_entry *root;
1245 unsigned long flags;
1246
Suresh Siddha4c923d42009-10-02 11:01:24 -07001247 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001248 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001249 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001250 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001251 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001252 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001253
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001254 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001255
1256 spin_lock_irqsave(&iommu->lock, flags);
1257 iommu->root_entry = root;
1258 spin_unlock_irqrestore(&iommu->lock, flags);
1259
1260 return 0;
1261}
1262
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001263static void iommu_set_root_entry(struct intel_iommu *iommu)
1264{
David Woodhouse03ecc322015-02-13 14:35:21 +00001265 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001266 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001267 unsigned long flag;
1268
David Woodhouse03ecc322015-02-13 14:35:21 +00001269 addr = virt_to_phys(iommu->root_entry);
David Woodhousec83b2f22015-06-12 10:15:49 +01001270 if (ecs_enabled(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +00001271 addr |= DMA_RTADDR_RTT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001272
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001273 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001274 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001275
David Woodhousec416daa2009-05-10 20:30:58 +01001276 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001277
1278 /* Make sure hardware complete it */
1279 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001280 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001281
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001282 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001283}
1284
1285static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1286{
1287 u32 val;
1288 unsigned long flag;
1289
David Woodhouse9af88142009-02-13 23:18:03 +00001290 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001291 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001293 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001294 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001295
1296 /* Make sure hardware complete it */
1297 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001298 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001299
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001300 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001301}
1302
1303/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001304static void __iommu_flush_context(struct intel_iommu *iommu,
1305 u16 did, u16 source_id, u8 function_mask,
1306 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001307{
1308 u64 val = 0;
1309 unsigned long flag;
1310
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001311 switch (type) {
1312 case DMA_CCMD_GLOBAL_INVL:
1313 val = DMA_CCMD_GLOBAL_INVL;
1314 break;
1315 case DMA_CCMD_DOMAIN_INVL:
1316 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1317 break;
1318 case DMA_CCMD_DEVICE_INVL:
1319 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1320 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1321 break;
1322 default:
1323 BUG();
1324 }
1325 val |= DMA_CCMD_ICC;
1326
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001327 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001328 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1329
1330 /* Make sure hardware complete it */
1331 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1332 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1333
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001334 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001335}
1336
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001337/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001338static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1339 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001340{
1341 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1342 u64 val = 0, val_iva = 0;
1343 unsigned long flag;
1344
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001345 switch (type) {
1346 case DMA_TLB_GLOBAL_FLUSH:
1347 /* global flush doesn't need set IVA_REG */
1348 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1349 break;
1350 case DMA_TLB_DSI_FLUSH:
1351 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1352 break;
1353 case DMA_TLB_PSI_FLUSH:
1354 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001355 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001356 val_iva = size_order | addr;
1357 break;
1358 default:
1359 BUG();
1360 }
1361 /* Note: set drain read/write */
1362#if 0
1363 /*
1364 * This is probably to be super secure.. Looks like we can
1365 * ignore it without any impact.
1366 */
1367 if (cap_read_drain(iommu->cap))
1368 val |= DMA_TLB_READ_DRAIN;
1369#endif
1370 if (cap_write_drain(iommu->cap))
1371 val |= DMA_TLB_WRITE_DRAIN;
1372
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001373 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001374 /* Note: Only uses first TLB reg currently */
1375 if (val_iva)
1376 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1377 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1378
1379 /* Make sure hardware complete it */
1380 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1381 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1382
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001383 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001384
1385 /* check IOTLB invalidation granularity */
1386 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001387 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001388 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001389 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001390 (unsigned long long)DMA_TLB_IIRG(type),
1391 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001392}
1393
David Woodhouse64ae8922014-03-09 12:52:30 -07001394static struct device_domain_info *
1395iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1396 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001397{
Yu Zhao93a23a72009-05-18 13:51:37 +08001398 struct device_domain_info *info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001399
Joerg Roedel55d94042015-07-22 16:50:40 +02001400 assert_spin_locked(&device_domain_lock);
1401
Yu Zhao93a23a72009-05-18 13:51:37 +08001402 if (!iommu->qi)
1403 return NULL;
1404
Yu Zhao93a23a72009-05-18 13:51:37 +08001405 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001406 if (info->iommu == iommu && info->bus == bus &&
1407 info->devfn == devfn) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001408 if (info->ats_supported && info->dev)
1409 return info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001410 break;
1411 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001412
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001413 return NULL;
Yu Zhao93a23a72009-05-18 13:51:37 +08001414}
1415
Omer Peleg0824c592016-04-20 19:03:35 +03001416static void domain_update_iotlb(struct dmar_domain *domain)
1417{
1418 struct device_domain_info *info;
1419 bool has_iotlb_device = false;
1420
1421 assert_spin_locked(&device_domain_lock);
1422
1423 list_for_each_entry(info, &domain->devices, link) {
1424 struct pci_dev *pdev;
1425
1426 if (!info->dev || !dev_is_pci(info->dev))
1427 continue;
1428
1429 pdev = to_pci_dev(info->dev);
1430 if (pdev->ats_enabled) {
1431 has_iotlb_device = true;
1432 break;
1433 }
1434 }
1435
1436 domain->has_iotlb_device = has_iotlb_device;
1437}
1438
Yu Zhao93a23a72009-05-18 13:51:37 +08001439static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1440{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001441 struct pci_dev *pdev;
1442
Omer Peleg0824c592016-04-20 19:03:35 +03001443 assert_spin_locked(&device_domain_lock);
1444
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001445 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001446 return;
1447
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001448 pdev = to_pci_dev(info->dev);
Jacob Pan1c48db42018-06-07 09:57:00 -07001449 /* For IOMMU that supports device IOTLB throttling (DIT), we assign
1450 * PFSID to the invalidation desc of a VF such that IOMMU HW can gauge
1451 * queue depth at PF level. If DIT is not set, PFSID will be treated as
1452 * reserved, which should be set to 0.
1453 */
1454 if (!ecap_dit(info->iommu->ecap))
1455 info->pfsid = 0;
1456 else {
1457 struct pci_dev *pf_pdev;
1458
1459 /* pdev will be returned if device is not a vf */
1460 pf_pdev = pci_physfn(pdev);
1461 info->pfsid = PCI_DEVID(pf_pdev->bus->number, pf_pdev->devfn);
1462 }
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001463
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001464#ifdef CONFIG_INTEL_IOMMU_SVM
1465 /* The PCIe spec, in its wisdom, declares that the behaviour of
1466 the device if you enable PASID support after ATS support is
1467 undefined. So always enable PASID support on devices which
1468 have it, even if we can't yet know if we're ever going to
1469 use it. */
1470 if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
1471 info->pasid_enabled = 1;
1472
1473 if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
1474 info->pri_enabled = 1;
1475#endif
1476 if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
1477 info->ats_enabled = 1;
Omer Peleg0824c592016-04-20 19:03:35 +03001478 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001479 info->ats_qdep = pci_ats_queue_depth(pdev);
1480 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001481}
1482
1483static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1484{
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001485 struct pci_dev *pdev;
1486
Omer Peleg0824c592016-04-20 19:03:35 +03001487 assert_spin_locked(&device_domain_lock);
1488
Jeremy McNicollda972fb2016-01-14 21:33:06 -08001489 if (!dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001490 return;
1491
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001492 pdev = to_pci_dev(info->dev);
1493
1494 if (info->ats_enabled) {
1495 pci_disable_ats(pdev);
1496 info->ats_enabled = 0;
Omer Peleg0824c592016-04-20 19:03:35 +03001497 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001498 }
1499#ifdef CONFIG_INTEL_IOMMU_SVM
1500 if (info->pri_enabled) {
1501 pci_disable_pri(pdev);
1502 info->pri_enabled = 0;
1503 }
1504 if (info->pasid_enabled) {
1505 pci_disable_pasid(pdev);
1506 info->pasid_enabled = 0;
1507 }
1508#endif
Yu Zhao93a23a72009-05-18 13:51:37 +08001509}
1510
1511static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1512 u64 addr, unsigned mask)
1513{
1514 u16 sid, qdep;
1515 unsigned long flags;
1516 struct device_domain_info *info;
1517
Omer Peleg0824c592016-04-20 19:03:35 +03001518 if (!domain->has_iotlb_device)
1519 return;
1520
Yu Zhao93a23a72009-05-18 13:51:37 +08001521 spin_lock_irqsave(&device_domain_lock, flags);
1522 list_for_each_entry(info, &domain->devices, link) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001523 if (!info->ats_enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001524 continue;
1525
1526 sid = info->bus << 8 | info->devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001527 qdep = info->ats_qdep;
Jacob Pan1c48db42018-06-07 09:57:00 -07001528 qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
1529 qdep, addr, mask);
Yu Zhao93a23a72009-05-18 13:51:37 +08001530 }
1531 spin_unlock_irqrestore(&device_domain_lock, flags);
1532}
1533
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001534static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1535 struct dmar_domain *domain,
1536 unsigned long pfn, unsigned int pages,
1537 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001538{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001539 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001540 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001541 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001542
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001543 BUG_ON(pages == 0);
1544
David Woodhouseea8ea462014-03-05 17:09:32 +00001545 if (ih)
1546 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001547 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001548 * Fallback to domain selective flush if no PSI support or the size is
1549 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001550 * PSI requires page size to be 2 ^ x, and the base address is naturally
1551 * aligned to the size
1552 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001553 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1554 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001555 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001556 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001557 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001558 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001559
1560 /*
Nadav Amit82653632010-04-01 13:24:40 +03001561 * In caching mode, changes of pages from non-present to present require
1562 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001563 */
Nadav Amit82653632010-04-01 13:24:40 +03001564 if (!cap_caching_mode(iommu->cap) || !map)
Peter Xu9d2e6502018-01-10 13:51:37 +08001565 iommu_flush_dev_iotlb(domain, addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001566}
1567
Peter Xueed91a02018-05-04 10:34:52 +08001568/* Notification for newly created mappings */
1569static inline void __mapping_notify_one(struct intel_iommu *iommu,
1570 struct dmar_domain *domain,
1571 unsigned long pfn, unsigned int pages)
1572{
1573 /* It's a non-present to present mapping. Only flush if caching mode */
1574 if (cap_caching_mode(iommu->cap))
1575 iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1);
1576 else
1577 iommu_flush_write_buffer(iommu);
1578}
1579
Joerg Roedel13cf0172017-08-11 11:40:10 +02001580static void iommu_flush_iova(struct iova_domain *iovad)
1581{
1582 struct dmar_domain *domain;
1583 int idx;
1584
1585 domain = container_of(iovad, struct dmar_domain, iovad);
1586
1587 for_each_domain_iommu(idx, domain) {
1588 struct intel_iommu *iommu = g_iommus[idx];
1589 u16 did = domain->iommu_did[iommu->seq_id];
1590
1591 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
1592
1593 if (!cap_caching_mode(iommu->cap))
1594 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1595 0, MAX_AGAW_PFN_WIDTH);
1596 }
1597}
1598
mark grossf8bab732008-02-08 04:18:38 -08001599static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1600{
1601 u32 pmen;
1602 unsigned long flags;
1603
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001604 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001605 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1606 pmen &= ~DMA_PMEN_EPM;
1607 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1608
1609 /* wait for the protected region status bit to clear */
1610 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1611 readl, !(pmen & DMA_PMEN_PRS), pmen);
1612
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001613 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001614}
1615
Jiang Liu2a41cce2014-07-11 14:19:33 +08001616static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001617{
1618 u32 sts;
1619 unsigned long flags;
1620
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001621 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001622 iommu->gcmd |= DMA_GCMD_TE;
1623 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001624
1625 /* Make sure hardware complete it */
1626 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001627 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001628
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001629 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001630}
1631
Jiang Liu2a41cce2014-07-11 14:19:33 +08001632static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001633{
1634 u32 sts;
1635 unsigned long flag;
1636
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001637 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001638 iommu->gcmd &= ~DMA_GCMD_TE;
1639 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1640
1641 /* Make sure hardware complete it */
1642 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001643 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001644
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001645 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001646}
1647
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001648
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001649static int iommu_init_domains(struct intel_iommu *iommu)
1650{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001651 u32 ndomains, nlongs;
1652 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001653
1654 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001655 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001656 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001657 nlongs = BITS_TO_LONGS(ndomains);
1658
Donald Dutile94a91b502009-08-20 16:51:34 -04001659 spin_lock_init(&iommu->lock);
1660
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001661 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1662 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001663 pr_err("%s: Allocating domain id array failed\n",
1664 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001665 return -ENOMEM;
1666 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001667
Wei Yang86f004c2016-05-21 02:41:51 +00001668 size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001669 iommu->domains = kzalloc(size, GFP_KERNEL);
1670
1671 if (iommu->domains) {
1672 size = 256 * sizeof(struct dmar_domain *);
1673 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1674 }
1675
1676 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001677 pr_err("%s: Allocating domain array failed\n",
1678 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001679 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001680 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001681 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001682 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001683 return -ENOMEM;
1684 }
1685
Joerg Roedel8bf47812015-07-21 10:41:21 +02001686
1687
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001688 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001689 * If Caching mode is set, then invalid translations are tagged
1690 * with domain-id 0, hence we need to pre-allocate it. We also
1691 * use domain-id 0 as a marker for non-allocated domain-id, so
1692 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001693 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001694 set_bit(0, iommu->domain_ids);
1695
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001696 return 0;
1697}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001698
Jiang Liuffebeb42014-11-09 22:48:02 +08001699static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001700{
Joerg Roedel29a27712015-07-21 17:17:12 +02001701 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001702 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001703
Joerg Roedel29a27712015-07-21 17:17:12 +02001704 if (!iommu->domains || !iommu->domain_ids)
1705 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001706
Joerg Roedelbea64032016-11-08 15:08:26 +01001707again:
Joerg Roedel55d94042015-07-22 16:50:40 +02001708 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001709 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1710 struct dmar_domain *domain;
1711
1712 if (info->iommu != iommu)
1713 continue;
1714
1715 if (!info->dev || !info->domain)
1716 continue;
1717
1718 domain = info->domain;
1719
Joerg Roedelbea64032016-11-08 15:08:26 +01001720 __dmar_remove_one_dev_info(info);
Joerg Roedel29a27712015-07-21 17:17:12 +02001721
Joerg Roedelbea64032016-11-08 15:08:26 +01001722 if (!domain_type_is_vm_or_si(domain)) {
1723 /*
1724 * The domain_exit() function can't be called under
1725 * device_domain_lock, as it takes this lock itself.
1726 * So release the lock here and re-run the loop
1727 * afterwards.
1728 */
1729 spin_unlock_irqrestore(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001730 domain_exit(domain);
Joerg Roedelbea64032016-11-08 15:08:26 +01001731 goto again;
1732 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001733 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001734 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001735
1736 if (iommu->gcmd & DMA_GCMD_TE)
1737 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001738}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001739
Jiang Liuffebeb42014-11-09 22:48:02 +08001740static void free_dmar_iommu(struct intel_iommu *iommu)
1741{
1742 if ((iommu->domains) && (iommu->domain_ids)) {
Wei Yang86f004c2016-05-21 02:41:51 +00001743 int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001744 int i;
1745
1746 for (i = 0; i < elems; i++)
1747 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001748 kfree(iommu->domains);
1749 kfree(iommu->domain_ids);
1750 iommu->domains = NULL;
1751 iommu->domain_ids = NULL;
1752 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001753
Weidong Hand9630fe2008-12-08 11:06:32 +08001754 g_iommus[iommu->seq_id] = NULL;
1755
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001756 /* free context mapping */
1757 free_context_table(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001758
1759#ifdef CONFIG_INTEL_IOMMU_SVM
David Woodhousea222a7f2015-10-07 23:35:18 +01001760 if (pasid_enabled(iommu)) {
1761 if (ecap_prs(iommu->ecap))
1762 intel_svm_finish_prq(iommu);
Lu Baolud9737952018-07-14 15:47:02 +08001763 intel_svm_exit(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01001764 }
David Woodhouse8a94ade2015-03-24 14:54:56 +00001765#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001766}
1767
Jiang Liuab8dfe22014-07-11 14:19:27 +08001768static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001769{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001770 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001771
1772 domain = alloc_domain_mem();
1773 if (!domain)
1774 return NULL;
1775
Jiang Liuab8dfe22014-07-11 14:19:27 +08001776 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001777 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001778 domain->flags = flags;
Omer Peleg0824c592016-04-20 19:03:35 +03001779 domain->has_iotlb_device = false;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001780 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001781
1782 return domain;
1783}
1784
Joerg Roedeld160aca2015-07-22 11:52:53 +02001785/* Must be called with iommu->lock */
1786static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001787 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001788{
Jiang Liu44bde612014-07-11 14:19:29 +08001789 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001790 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001791
Joerg Roedel55d94042015-07-22 16:50:40 +02001792 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001793 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001794
Joerg Roedel29a27712015-07-21 17:17:12 +02001795 domain->iommu_refcnt[iommu->seq_id] += 1;
1796 domain->iommu_count += 1;
1797 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001798 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001799 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1800
1801 if (num >= ndomains) {
1802 pr_err("%s: No free domain ids\n", iommu->name);
1803 domain->iommu_refcnt[iommu->seq_id] -= 1;
1804 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001805 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001806 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001807
Joerg Roedeld160aca2015-07-22 11:52:53 +02001808 set_bit(num, iommu->domain_ids);
1809 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001810
Joerg Roedeld160aca2015-07-22 11:52:53 +02001811 domain->iommu_did[iommu->seq_id] = num;
1812 domain->nid = iommu->node;
1813
Jiang Liufb170fb2014-07-11 14:19:28 +08001814 domain_update_iommu_cap(domain);
1815 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001816
Joerg Roedel55d94042015-07-22 16:50:40 +02001817 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001818}
1819
1820static int domain_detach_iommu(struct dmar_domain *domain,
1821 struct intel_iommu *iommu)
1822{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001823 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001824
Joerg Roedel55d94042015-07-22 16:50:40 +02001825 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001826 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001827
Joerg Roedel29a27712015-07-21 17:17:12 +02001828 domain->iommu_refcnt[iommu->seq_id] -= 1;
1829 count = --domain->iommu_count;
1830 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001831 num = domain->iommu_did[iommu->seq_id];
1832 clear_bit(num, iommu->domain_ids);
1833 set_iommu_domain(iommu, num, NULL);
1834
Jiang Liufb170fb2014-07-11 14:19:28 +08001835 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001836 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001837 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001838
1839 return count;
1840}
1841
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001842static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001843static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001844
Joseph Cihula51a63e62011-03-21 11:04:24 -07001845static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001846{
1847 struct pci_dev *pdev = NULL;
1848 struct iova *iova;
1849 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001850
Zhen Leiaa3ac942017-09-21 16:52:45 +01001851 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001852
Mark Gross8a443df2008-03-04 14:59:31 -08001853 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1854 &reserved_rbtree_key);
1855
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001856 /* IOAPIC ranges shouldn't be accessed by DMA */
1857 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1858 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001859 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001860 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001861 return -ENODEV;
1862 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863
1864 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1865 for_each_pci_dev(pdev) {
1866 struct resource *r;
1867
1868 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1869 r = &pdev->resource[i];
1870 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1871 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001872 iova = reserve_iova(&reserved_iova_list,
1873 IOVA_PFN(r->start),
1874 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001875 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001876 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001877 return -ENODEV;
1878 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001879 }
1880 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001881 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001882}
1883
1884static void domain_reserve_special_ranges(struct dmar_domain *domain)
1885{
1886 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1887}
1888
1889static inline int guestwidth_to_adjustwidth(int gaw)
1890{
1891 int agaw;
1892 int r = (gaw - 12) % 9;
1893
1894 if (r == 0)
1895 agaw = gaw;
1896 else
1897 agaw = gaw + 9 - r;
1898 if (agaw > 64)
1899 agaw = 64;
1900 return agaw;
1901}
1902
Joerg Roedeldc534b22015-07-22 12:44:02 +02001903static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1904 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001905{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001906 int adjust_width, agaw;
1907 unsigned long sagaw;
Joerg Roedel13cf0172017-08-11 11:40:10 +02001908 int err;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001909
Zhen Leiaa3ac942017-09-21 16:52:45 +01001910 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
Joerg Roedel13cf0172017-08-11 11:40:10 +02001911
1912 err = init_iova_flush_queue(&domain->iovad,
1913 iommu_flush_iova, iova_entry_free);
1914 if (err)
1915 return err;
1916
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001917 domain_reserve_special_ranges(domain);
1918
1919 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001920 if (guest_width > cap_mgaw(iommu->cap))
1921 guest_width = cap_mgaw(iommu->cap);
1922 domain->gaw = guest_width;
1923 adjust_width = guestwidth_to_adjustwidth(guest_width);
1924 agaw = width_to_agaw(adjust_width);
1925 sagaw = cap_sagaw(iommu->cap);
1926 if (!test_bit(agaw, &sagaw)) {
1927 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001928 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001929 agaw = find_next_bit(&sagaw, 5, agaw);
1930 if (agaw >= 5)
1931 return -ENODEV;
1932 }
1933 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001934
Weidong Han8e6040972008-12-08 15:49:06 +08001935 if (ecap_coherent(iommu->ecap))
1936 domain->iommu_coherency = 1;
1937 else
1938 domain->iommu_coherency = 0;
1939
Sheng Yang58c610b2009-03-18 15:33:05 +08001940 if (ecap_sc_support(iommu->ecap))
1941 domain->iommu_snooping = 1;
1942 else
1943 domain->iommu_snooping = 0;
1944
David Woodhouse214e39a2014-03-19 10:38:49 +00001945 if (intel_iommu_superpage)
1946 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1947 else
1948 domain->iommu_superpage = 0;
1949
Suresh Siddha4c923d42009-10-02 11:01:24 -07001950 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001951
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001952 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001953 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001954 if (!domain->pgd)
1955 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001956 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001957 return 0;
1958}
1959
1960static void domain_exit(struct dmar_domain *domain)
1961{
David Woodhouseea8ea462014-03-05 17:09:32 +00001962 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001963
1964 /* Domain 0 is reserved, so dont process it */
1965 if (!domain)
1966 return;
1967
Joerg Roedeld160aca2015-07-22 11:52:53 +02001968 /* Remove associated devices and clear attached or cached domains */
1969 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001970 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001971 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001972
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001973 /* destroy iovas */
1974 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001975
David Woodhouseea8ea462014-03-05 17:09:32 +00001976 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001977
David Woodhouseea8ea462014-03-05 17:09:32 +00001978 dma_free_pagelist(freelist);
1979
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001980 free_domain_mem(domain);
1981}
1982
David Woodhouse64ae8922014-03-09 12:52:30 -07001983static int domain_context_mapping_one(struct dmar_domain *domain,
1984 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001985 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001986{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001987 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001988 int translation = CONTEXT_TT_MULTI_LEVEL;
1989 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001990 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001991 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001992 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02001993 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001994
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001995 WARN_ON(did == 0);
1996
Joerg Roedel28ccce02015-07-21 14:45:31 +02001997 if (hw_pass_through && domain_type_is_si(domain))
1998 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001999
2000 pr_debug("Set context mapping for %02x:%02x.%d\n",
2001 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002002
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002003 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08002004
Joerg Roedel55d94042015-07-22 16:50:40 +02002005 spin_lock_irqsave(&device_domain_lock, flags);
2006 spin_lock(&iommu->lock);
2007
2008 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00002009 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002010 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02002011 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002012
Joerg Roedel55d94042015-07-22 16:50:40 +02002013 ret = 0;
2014 if (context_present(context))
2015 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02002016
Xunlei Pangaec0e862016-12-05 20:09:07 +08002017 /*
2018 * For kdump cases, old valid entries may be cached due to the
2019 * in-flight DMA and copied pgtable, but there is no unmapping
2020 * behaviour for them, thus we need an explicit cache flush for
2021 * the newly-mapped device. For kdump, at this point, the device
2022 * is supposed to finish reset at its driver probe stage, so no
2023 * in-flight DMA will exist, and we don't need to worry anymore
2024 * hereafter.
2025 */
2026 if (context_copied(context)) {
2027 u16 did_old = context_domain_id(context);
2028
Christos Gkekasb117e032017-10-08 23:33:31 +01002029 if (did_old < cap_ndoms(iommu->cap)) {
Xunlei Pangaec0e862016-12-05 20:09:07 +08002030 iommu->flush.flush_context(iommu, did_old,
2031 (((u16)bus) << 8) | devfn,
2032 DMA_CCMD_MASK_NOBIT,
2033 DMA_CCMD_DEVICE_INVL);
KarimAllah Ahmedf73a7ee2017-05-05 11:39:59 -07002034 iommu->flush.flush_iotlb(iommu, did_old, 0, 0,
2035 DMA_TLB_DSI_FLUSH);
2036 }
Xunlei Pangaec0e862016-12-05 20:09:07 +08002037 }
2038
Weidong Hanea6606b2008-12-08 23:08:15 +08002039 pgd = domain->pgd;
2040
Joerg Roedelde24e552015-07-21 14:53:04 +02002041 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002042 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08002043
Joerg Roedelde24e552015-07-21 14:53:04 +02002044 /*
2045 * Skip top levels of page tables for iommu which has less agaw
2046 * than default. Unnecessary for PT mode.
2047 */
Yu Zhao93a23a72009-05-18 13:51:37 +08002048 if (translation != CONTEXT_TT_PASS_THROUGH) {
Joerg Roedelde24e552015-07-21 14:53:04 +02002049 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02002050 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02002051 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02002052 if (!dma_pte_present(pgd))
2053 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02002054 }
2055
David Woodhouse64ae8922014-03-09 12:52:30 -07002056 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002057 if (info && info->ats_supported)
2058 translation = CONTEXT_TT_DEV_IOTLB;
2059 else
2060 translation = CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02002061
Yu Zhao93a23a72009-05-18 13:51:37 +08002062 context_set_address_root(context, virt_to_phys(pgd));
2063 context_set_address_width(context, iommu->agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02002064 } else {
2065 /*
2066 * In pass through mode, AW must be programmed to
2067 * indicate the largest AGAW value supported by
2068 * hardware. And ASR is ignored by hardware.
2069 */
2070 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08002071 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002072
2073 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00002074 context_set_fault_enable(context);
2075 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08002076 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002077
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002078 /*
2079 * It's a non-present to present mapping. If hardware doesn't cache
2080 * non-present entry we only need to flush the write-buffer. If the
2081 * _does_ cache non-present entries, then it does so in the special
2082 * domain #0, which we have to flush:
2083 */
2084 if (cap_caching_mode(iommu->cap)) {
2085 iommu->flush.flush_context(iommu, 0,
2086 (((u16)bus) << 8) | devfn,
2087 DMA_CCMD_MASK_NOBIT,
2088 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002089 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002090 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002091 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002092 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002093 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08002094
Joerg Roedel55d94042015-07-22 16:50:40 +02002095 ret = 0;
2096
2097out_unlock:
2098 spin_unlock(&iommu->lock);
2099 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08002100
Wei Yang5c365d12016-07-13 13:53:21 +00002101 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002102}
2103
Alex Williamson579305f2014-07-03 09:51:43 -06002104struct domain_context_mapping_data {
2105 struct dmar_domain *domain;
2106 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002107};
2108
2109static int domain_context_mapping_cb(struct pci_dev *pdev,
2110 u16 alias, void *opaque)
2111{
2112 struct domain_context_mapping_data *data = opaque;
2113
2114 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002115 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002116}
2117
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002118static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002119domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002120{
David Woodhouse64ae8922014-03-09 12:52:30 -07002121 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002122 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002123 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002124
David Woodhousee1f167f2014-03-09 15:24:46 -07002125 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002126 if (!iommu)
2127 return -ENODEV;
2128
Alex Williamson579305f2014-07-03 09:51:43 -06002129 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002130 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002131
2132 data.domain = domain;
2133 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002134
2135 return pci_for_each_dma_alias(to_pci_dev(dev),
2136 &domain_context_mapping_cb, &data);
2137}
2138
2139static int domain_context_mapped_cb(struct pci_dev *pdev,
2140 u16 alias, void *opaque)
2141{
2142 struct intel_iommu *iommu = opaque;
2143
2144 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002145}
2146
David Woodhousee1f167f2014-03-09 15:24:46 -07002147static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002148{
Weidong Han5331fe62008-12-08 23:00:00 +08002149 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002150 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002151
David Woodhousee1f167f2014-03-09 15:24:46 -07002152 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002153 if (!iommu)
2154 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002155
Alex Williamson579305f2014-07-03 09:51:43 -06002156 if (!dev_is_pci(dev))
2157 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002158
Alex Williamson579305f2014-07-03 09:51:43 -06002159 return !pci_for_each_dma_alias(to_pci_dev(dev),
2160 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002161}
2162
Fenghua Yuf5329592009-08-04 15:09:37 -07002163/* Returns a number of VTD pages, but aligned to MM page size */
2164static inline unsigned long aligned_nrpages(unsigned long host_addr,
2165 size_t size)
2166{
2167 host_addr &= ~PAGE_MASK;
2168 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2169}
2170
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002171/* Return largest possible superpage level for a given mapping */
2172static inline int hardware_largepage_caps(struct dmar_domain *domain,
2173 unsigned long iov_pfn,
2174 unsigned long phy_pfn,
2175 unsigned long pages)
2176{
2177 int support, level = 1;
2178 unsigned long pfnmerge;
2179
2180 support = domain->iommu_superpage;
2181
2182 /* To use a large page, the virtual *and* physical addresses
2183 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2184 of them will mean we have to use smaller pages. So just
2185 merge them and check both at once. */
2186 pfnmerge = iov_pfn | phy_pfn;
2187
2188 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2189 pages >>= VTD_STRIDE_SHIFT;
2190 if (!pages)
2191 break;
2192 pfnmerge >>= VTD_STRIDE_SHIFT;
2193 level++;
2194 support--;
2195 }
2196 return level;
2197}
2198
David Woodhouse9051aa02009-06-29 12:30:54 +01002199static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2200 struct scatterlist *sg, unsigned long phys_pfn,
2201 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002202{
2203 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002204 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002205 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002206 unsigned int largepage_lvl = 0;
2207 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002208
Jiang Liu162d1b12014-07-11 14:19:35 +08002209 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002210
2211 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2212 return -EINVAL;
2213
2214 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2215
Jiang Liucc4f14a2014-11-26 09:42:10 +08002216 if (!sg) {
2217 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002218 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2219 }
2220
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002221 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002222 uint64_t tmp;
2223
David Woodhousee1605492009-06-29 11:17:38 +01002224 if (!sg_res) {
Robin Murphy29a90b72017-09-28 15:14:01 +01002225 unsigned int pgoff = sg->offset & ~PAGE_MASK;
2226
Fenghua Yuf5329592009-08-04 15:09:37 -07002227 sg_res = aligned_nrpages(sg->offset, sg->length);
Robin Murphy29a90b72017-09-28 15:14:01 +01002228 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff;
David Woodhousee1605492009-06-29 11:17:38 +01002229 sg->dma_length = sg->length;
Robin Murphy29a90b72017-09-28 15:14:01 +01002230 pteval = (sg_phys(sg) - pgoff) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002231 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002232 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002233
David Woodhousee1605492009-06-29 11:17:38 +01002234 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002235 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2236
David Woodhouse5cf0a762014-03-19 16:07:49 +00002237 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002238 if (!pte)
2239 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002240 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002241 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002242 unsigned long nr_superpages, end_pfn;
2243
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002244 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002245 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002246
2247 nr_superpages = sg_res / lvl_pages;
2248 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2249
Jiang Liud41a4ad2014-07-11 14:19:34 +08002250 /*
2251 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002252 * removed to make room for superpage(s).
David Dillowbc24c572017-06-28 19:42:23 -07002253 * We're adding new large pages, so make sure
2254 * we don't remove their parent tables.
Jiang Liud41a4ad2014-07-11 14:19:34 +08002255 */
David Dillowbc24c572017-06-28 19:42:23 -07002256 dma_pte_free_pagetable(domain, iov_pfn, end_pfn,
2257 largepage_lvl + 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002258 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002259 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002260 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002261
David Woodhousee1605492009-06-29 11:17:38 +01002262 }
2263 /* We don't need lock here, nobody else
2264 * touches the iova range
2265 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002266 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002267 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002268 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002269 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2270 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002271 if (dumps) {
2272 dumps--;
2273 debug_dma_dump_mappings(NULL);
2274 }
2275 WARN_ON(1);
2276 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002277
2278 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2279
2280 BUG_ON(nr_pages < lvl_pages);
2281 BUG_ON(sg_res < lvl_pages);
2282
2283 nr_pages -= lvl_pages;
2284 iov_pfn += lvl_pages;
2285 phys_pfn += lvl_pages;
2286 pteval += lvl_pages * VTD_PAGE_SIZE;
2287 sg_res -= lvl_pages;
2288
2289 /* If the next PTE would be the first in a new page, then we
2290 need to flush the cache on the entries we've just written.
2291 And then we'll need to recalculate 'pte', so clear it and
2292 let it get set again in the if (!pte) block above.
2293
2294 If we're done (!nr_pages) we need to flush the cache too.
2295
2296 Also if we've been setting superpages, we may need to
2297 recalculate 'pte' and switch back to smaller pages for the
2298 end of the mapping, if the trailing size is not enough to
2299 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002300 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002301 if (!nr_pages || first_pte_in_page(pte) ||
2302 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002303 domain_flush_cache(domain, first_pte,
2304 (void *)pte - (void *)first_pte);
2305 pte = NULL;
2306 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002307
2308 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002309 sg = sg_next(sg);
2310 }
2311 return 0;
2312}
2313
Peter Xu87684fd2018-05-04 10:34:53 +08002314static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2315 struct scatterlist *sg, unsigned long phys_pfn,
2316 unsigned long nr_pages, int prot)
2317{
2318 int ret;
2319 struct intel_iommu *iommu;
2320
2321 /* Do the real mapping first */
2322 ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot);
2323 if (ret)
2324 return ret;
2325
2326 /* Notify about the new mapping */
2327 if (domain_type_is_vm(domain)) {
2328 /* VM typed domains can have more than one IOMMUs */
2329 int iommu_id;
2330 for_each_domain_iommu(iommu_id, domain) {
2331 iommu = g_iommus[iommu_id];
2332 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2333 }
2334 } else {
2335 /* General domains only have one IOMMU */
2336 iommu = domain_get_iommu(domain);
2337 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2338 }
2339
2340 return 0;
2341}
2342
David Woodhouse9051aa02009-06-29 12:30:54 +01002343static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2344 struct scatterlist *sg, unsigned long nr_pages,
2345 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002346{
Peter Xu87684fd2018-05-04 10:34:53 +08002347 return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
David Woodhouse9051aa02009-06-29 12:30:54 +01002348}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002349
David Woodhouse9051aa02009-06-29 12:30:54 +01002350static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2351 unsigned long phys_pfn, unsigned long nr_pages,
2352 int prot)
2353{
Peter Xu87684fd2018-05-04 10:34:53 +08002354 return domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002355}
2356
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002357static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002358{
Filippo Sironi50822192017-08-31 10:58:11 +02002359 unsigned long flags;
2360 struct context_entry *context;
2361 u16 did_old;
2362
Weidong Hanc7151a82008-12-08 22:51:37 +08002363 if (!iommu)
2364 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002365
Filippo Sironi50822192017-08-31 10:58:11 +02002366 spin_lock_irqsave(&iommu->lock, flags);
2367 context = iommu_context_addr(iommu, bus, devfn, 0);
2368 if (!context) {
2369 spin_unlock_irqrestore(&iommu->lock, flags);
2370 return;
2371 }
2372 did_old = context_domain_id(context);
2373 context_clear_entry(context);
2374 __iommu_flush_cache(iommu, context, sizeof(*context));
2375 spin_unlock_irqrestore(&iommu->lock, flags);
2376 iommu->flush.flush_context(iommu,
2377 did_old,
2378 (((u16)bus) << 8) | devfn,
2379 DMA_CCMD_MASK_NOBIT,
2380 DMA_CCMD_DEVICE_INVL);
2381 iommu->flush.flush_iotlb(iommu,
2382 did_old,
2383 0,
2384 0,
2385 DMA_TLB_DSI_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002386}
2387
David Woodhouse109b9b02012-05-25 17:43:02 +01002388static inline void unlink_domain_info(struct device_domain_info *info)
2389{
2390 assert_spin_locked(&device_domain_lock);
2391 list_del(&info->link);
2392 list_del(&info->global);
2393 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002394 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002395}
2396
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002397static void domain_remove_dev_info(struct dmar_domain *domain)
2398{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002399 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002400 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002401
2402 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002403 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002404 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002405 spin_unlock_irqrestore(&device_domain_lock, flags);
2406}
2407
2408/*
2409 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002410 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002411 */
David Woodhouse1525a292014-03-06 16:19:30 +00002412static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002413{
2414 struct device_domain_info *info;
2415
2416 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002417 info = dev->archdata.iommu;
Peter Xub316d022017-05-22 18:28:51 +08002418 if (likely(info))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002419 return info->domain;
2420 return NULL;
2421}
2422
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002423static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002424dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2425{
2426 struct device_domain_info *info;
2427
2428 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002429 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002430 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002431 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002432
2433 return NULL;
2434}
2435
Joerg Roedel5db31562015-07-22 12:40:43 +02002436static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2437 int bus, int devfn,
2438 struct device *dev,
2439 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002440{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002441 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002442 struct device_domain_info *info;
2443 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002444 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002445
2446 info = alloc_devinfo_mem();
2447 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002448 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002449
Jiang Liu745f2582014-02-19 14:07:26 +08002450 info->bus = bus;
2451 info->devfn = devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002452 info->ats_supported = info->pasid_supported = info->pri_supported = 0;
2453 info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
2454 info->ats_qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002455 info->dev = dev;
2456 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002457 info->iommu = iommu;
Lu Baolucc580e42018-07-14 15:46:59 +08002458 info->pasid_table = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002459
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002460 if (dev && dev_is_pci(dev)) {
2461 struct pci_dev *pdev = to_pci_dev(info->dev);
2462
Gil Kupfercef74402018-05-10 17:56:02 -05002463 if (!pci_ats_disabled() &&
2464 ecap_dev_iotlb_support(iommu->ecap) &&
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002465 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
2466 dmar_find_matched_atsr_unit(pdev))
2467 info->ats_supported = 1;
2468
2469 if (ecs_enabled(iommu)) {
2470 if (pasid_enabled(iommu)) {
2471 int features = pci_pasid_features(pdev);
2472 if (features >= 0)
2473 info->pasid_supported = features | 1;
2474 }
2475
2476 if (info->ats_supported && ecap_prs(iommu->ecap) &&
2477 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
2478 info->pri_supported = 1;
2479 }
2480 }
2481
Jiang Liu745f2582014-02-19 14:07:26 +08002482 spin_lock_irqsave(&device_domain_lock, flags);
2483 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002484 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002485
2486 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002487 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002488 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002489 if (info2) {
2490 found = info2->domain;
2491 info2->dev = dev;
2492 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002493 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002494
Jiang Liu745f2582014-02-19 14:07:26 +08002495 if (found) {
2496 spin_unlock_irqrestore(&device_domain_lock, flags);
2497 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002498 /* Caller must free the original domain */
2499 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002500 }
2501
Joerg Roedeld160aca2015-07-22 11:52:53 +02002502 spin_lock(&iommu->lock);
2503 ret = domain_attach_iommu(domain, iommu);
2504 spin_unlock(&iommu->lock);
2505
2506 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002507 spin_unlock_irqrestore(&device_domain_lock, flags);
Sudip Mukherjee499f3aa2015-09-18 16:27:07 +05302508 free_devinfo_mem(info);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002509 return NULL;
2510 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002511
David Woodhouseb718cd32014-03-09 13:11:33 -07002512 list_add(&info->link, &domain->devices);
2513 list_add(&info->global, &device_domain_list);
2514 if (dev)
2515 dev->archdata.iommu = info;
Lu Baolua7fc93f2018-07-14 15:47:00 +08002516
2517 if (dev && dev_is_pci(dev) && info->pasid_supported) {
2518 ret = intel_pasid_alloc_table(dev);
2519 if (ret) {
Lu Baolube9e6592018-09-08 09:42:53 +08002520 pr_warn("No pasid table for %s, pasid disabled\n",
2521 dev_name(dev));
2522 info->pasid_supported = 0;
Lu Baolua7fc93f2018-07-14 15:47:00 +08002523 }
2524 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002525 spin_unlock_irqrestore(&device_domain_lock, flags);
2526
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002527 if (dev && domain_context_mapping(domain, dev)) {
2528 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002529 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002530 return NULL;
2531 }
2532
David Woodhouseb718cd32014-03-09 13:11:33 -07002533 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002534}
2535
Alex Williamson579305f2014-07-03 09:51:43 -06002536static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2537{
2538 *(u16 *)opaque = alias;
2539 return 0;
2540}
2541
Joerg Roedel76208352016-08-25 14:25:12 +02002542static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002543{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002544 struct device_domain_info *info = NULL;
Joerg Roedel76208352016-08-25 14:25:12 +02002545 struct dmar_domain *domain = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002546 struct intel_iommu *iommu;
Lu Baolufcc35c62018-05-04 13:08:17 +08002547 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002548 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002549 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002550
David Woodhouse146922e2014-03-09 15:44:17 -07002551 iommu = device_to_iommu(dev, &bus, &devfn);
2552 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002553 return NULL;
2554
2555 if (dev_is_pci(dev)) {
2556 struct pci_dev *pdev = to_pci_dev(dev);
2557
2558 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2559
2560 spin_lock_irqsave(&device_domain_lock, flags);
2561 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2562 PCI_BUS_NUM(dma_alias),
2563 dma_alias & 0xff);
2564 if (info) {
2565 iommu = info->iommu;
2566 domain = info->domain;
2567 }
2568 spin_unlock_irqrestore(&device_domain_lock, flags);
2569
Joerg Roedel76208352016-08-25 14:25:12 +02002570 /* DMA alias already has a domain, use it */
Alex Williamson579305f2014-07-03 09:51:43 -06002571 if (info)
Joerg Roedel76208352016-08-25 14:25:12 +02002572 goto out;
Alex Williamson579305f2014-07-03 09:51:43 -06002573 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002574
David Woodhouse146922e2014-03-09 15:44:17 -07002575 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002576 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002577 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002578 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002579 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002580 domain_exit(domain);
2581 return NULL;
2582 }
2583
Joerg Roedel76208352016-08-25 14:25:12 +02002584out:
Alex Williamson579305f2014-07-03 09:51:43 -06002585
Joerg Roedel76208352016-08-25 14:25:12 +02002586 return domain;
2587}
2588
2589static struct dmar_domain *set_domain_for_dev(struct device *dev,
2590 struct dmar_domain *domain)
2591{
2592 struct intel_iommu *iommu;
2593 struct dmar_domain *tmp;
2594 u16 req_id, dma_alias;
2595 u8 bus, devfn;
2596
2597 iommu = device_to_iommu(dev, &bus, &devfn);
2598 if (!iommu)
2599 return NULL;
2600
2601 req_id = ((u16)bus << 8) | devfn;
2602
2603 if (dev_is_pci(dev)) {
2604 struct pci_dev *pdev = to_pci_dev(dev);
2605
2606 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2607
2608 /* register PCI DMA alias device */
2609 if (req_id != dma_alias) {
2610 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2611 dma_alias & 0xff, NULL, domain);
2612
2613 if (!tmp || tmp != domain)
2614 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002615 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002616 }
2617
Joerg Roedel5db31562015-07-22 12:40:43 +02002618 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Joerg Roedel76208352016-08-25 14:25:12 +02002619 if (!tmp || tmp != domain)
2620 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002621
Joerg Roedel76208352016-08-25 14:25:12 +02002622 return domain;
2623}
2624
2625static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
2626{
2627 struct dmar_domain *domain, *tmp;
2628
2629 domain = find_domain(dev);
2630 if (domain)
2631 goto out;
2632
2633 domain = find_or_alloc_domain(dev, gaw);
2634 if (!domain)
2635 goto out;
2636
2637 tmp = set_domain_for_dev(dev, domain);
2638 if (!tmp || domain != tmp) {
Alex Williamson579305f2014-07-03 09:51:43 -06002639 domain_exit(domain);
2640 domain = tmp;
2641 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002642
Joerg Roedel76208352016-08-25 14:25:12 +02002643out:
2644
David Woodhouseb718cd32014-03-09 13:11:33 -07002645 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002646}
2647
David Woodhouseb2132032009-06-26 18:50:28 +01002648static int iommu_domain_identity_map(struct dmar_domain *domain,
2649 unsigned long long start,
2650 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002651{
David Woodhousec5395d52009-06-28 16:35:56 +01002652 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2653 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002654
David Woodhousec5395d52009-06-28 16:35:56 +01002655 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2656 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002657 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002658 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002659 }
2660
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002661 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002662 /*
2663 * RMRR range might have overlap with physical memory range,
2664 * clear it first
2665 */
David Woodhousec5395d52009-06-28 16:35:56 +01002666 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002667
Peter Xu87684fd2018-05-04 10:34:53 +08002668 return __domain_mapping(domain, first_vpfn, NULL,
2669 first_vpfn, last_vpfn - first_vpfn + 1,
2670 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002671}
2672
Joerg Roedeld66ce542015-09-23 19:00:10 +02002673static int domain_prepare_identity_map(struct device *dev,
2674 struct dmar_domain *domain,
2675 unsigned long long start,
2676 unsigned long long end)
David Woodhouseb2132032009-06-26 18:50:28 +01002677{
David Woodhouse19943b02009-08-04 16:19:20 +01002678 /* For _hardware_ passthrough, don't bother. But for software
2679 passthrough, we do it anyway -- it may indicate a memory
2680 range which is reserved in E820, so which didn't get set
2681 up to start with in si_domain */
2682 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002683 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2684 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002685 return 0;
2686 }
2687
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002688 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2689 dev_name(dev), start, end);
2690
David Woodhouse5595b522009-12-02 09:21:55 +00002691 if (end < start) {
2692 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2693 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2694 dmi_get_system_info(DMI_BIOS_VENDOR),
2695 dmi_get_system_info(DMI_BIOS_VERSION),
2696 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002697 return -EIO;
David Woodhouse5595b522009-12-02 09:21:55 +00002698 }
2699
David Woodhouse2ff729f2009-08-26 14:25:41 +01002700 if (end >> agaw_to_width(domain->agaw)) {
2701 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2702 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2703 agaw_to_width(domain->agaw),
2704 dmi_get_system_info(DMI_BIOS_VENDOR),
2705 dmi_get_system_info(DMI_BIOS_VERSION),
2706 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002707 return -EIO;
David Woodhouse2ff729f2009-08-26 14:25:41 +01002708 }
David Woodhouse19943b02009-08-04 16:19:20 +01002709
Joerg Roedeld66ce542015-09-23 19:00:10 +02002710 return iommu_domain_identity_map(domain, start, end);
2711}
2712
2713static int iommu_prepare_identity_map(struct device *dev,
2714 unsigned long long start,
2715 unsigned long long end)
2716{
2717 struct dmar_domain *domain;
2718 int ret;
2719
2720 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
2721 if (!domain)
2722 return -ENOMEM;
2723
2724 ret = domain_prepare_identity_map(dev, domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002725 if (ret)
Joerg Roedeld66ce542015-09-23 19:00:10 +02002726 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002727
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002728 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002729}
2730
2731static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002732 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002733{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002734 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002735 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002736 return iommu_prepare_identity_map(dev, rmrr->base_address,
2737 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002738}
2739
Suresh Siddhad3f13812011-08-23 17:05:25 -07002740#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002741static inline void iommu_prepare_isa(void)
2742{
2743 struct pci_dev *pdev;
2744 int ret;
2745
2746 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2747 if (!pdev)
2748 return;
2749
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002750 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002751 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002752
2753 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002754 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002755
Yijing Wang9b27e822014-05-20 20:37:52 +08002756 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002757}
2758#else
2759static inline void iommu_prepare_isa(void)
2760{
2761 return;
2762}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002763#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002764
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002765static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002766
Matt Kraai071e1372009-08-23 22:30:22 -07002767static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002768{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002769 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002770
Jiang Liuab8dfe22014-07-11 14:19:27 +08002771 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002772 if (!si_domain)
2773 return -EFAULT;
2774
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002775 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2776 domain_exit(si_domain);
2777 return -EFAULT;
2778 }
2779
Joerg Roedel0dc79712015-07-21 15:40:06 +02002780 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002781
David Woodhouse19943b02009-08-04 16:19:20 +01002782 if (hw)
2783 return 0;
2784
David Woodhousec7ab48d2009-06-26 19:10:36 +01002785 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002786 unsigned long start_pfn, end_pfn;
2787 int i;
2788
2789 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2790 ret = iommu_domain_identity_map(si_domain,
2791 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2792 if (ret)
2793 return ret;
2794 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002795 }
2796
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002797 return 0;
2798}
2799
David Woodhouse9b226622014-03-09 14:03:28 -07002800static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002801{
2802 struct device_domain_info *info;
2803
2804 if (likely(!iommu_identity_mapping))
2805 return 0;
2806
David Woodhouse9b226622014-03-09 14:03:28 -07002807 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002808 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2809 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002810
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002811 return 0;
2812}
2813
Joerg Roedel28ccce02015-07-21 14:45:31 +02002814static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002815{
David Woodhouse0ac72662014-03-09 13:19:22 -07002816 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002817 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002818 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002819
David Woodhouse5913c9b2014-03-09 16:27:31 -07002820 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002821 if (!iommu)
2822 return -ENODEV;
2823
Joerg Roedel5db31562015-07-22 12:40:43 +02002824 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002825 if (ndomain != domain)
2826 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002827
2828 return 0;
2829}
2830
David Woodhouse0b9d9752014-03-09 15:48:15 -07002831static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002832{
2833 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002834 struct device *tmp;
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002835 int i;
2836
Jiang Liu0e242612014-02-19 14:07:34 +08002837 rcu_read_lock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002838 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002839 /*
2840 * Return TRUE if this RMRR contains the device that
2841 * is passed in.
2842 */
2843 for_each_active_dev_scope(rmrr->devices,
2844 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002845 if (tmp == dev) {
Jiang Liu0e242612014-02-19 14:07:34 +08002846 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002847 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002848 }
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002849 }
Jiang Liu0e242612014-02-19 14:07:34 +08002850 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002851 return false;
2852}
2853
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002854/*
2855 * There are a couple cases where we need to restrict the functionality of
2856 * devices associated with RMRRs. The first is when evaluating a device for
2857 * identity mapping because problems exist when devices are moved in and out
2858 * of domains and their respective RMRR information is lost. This means that
2859 * a device with associated RMRRs will never be in a "passthrough" domain.
2860 * The second is use of the device through the IOMMU API. This interface
2861 * expects to have full control of the IOVA space for the device. We cannot
2862 * satisfy both the requirement that RMRR access is maintained and have an
2863 * unencumbered IOVA space. We also have no ability to quiesce the device's
2864 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2865 * We therefore prevent devices associated with an RMRR from participating in
2866 * the IOMMU API, which eliminates them from device assignment.
2867 *
2868 * In both cases we assume that PCI USB devices with RMRRs have them largely
2869 * for historical reasons and that the RMRR space is not actively used post
2870 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002871 *
2872 * The same exception is made for graphics devices, with the requirement that
2873 * any use of the RMRR regions will be torn down before assigning the device
2874 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002875 */
2876static bool device_is_rmrr_locked(struct device *dev)
2877{
2878 if (!device_has_rmrr(dev))
2879 return false;
2880
2881 if (dev_is_pci(dev)) {
2882 struct pci_dev *pdev = to_pci_dev(dev);
2883
David Woodhouse18436af2015-03-25 15:05:47 +00002884 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002885 return false;
2886 }
2887
2888 return true;
2889}
2890
David Woodhouse3bdb2592014-03-09 16:03:08 -07002891static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002892{
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002893
David Woodhouse3bdb2592014-03-09 16:03:08 -07002894 if (dev_is_pci(dev)) {
2895 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002896
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002897 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002898 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002899
Lu Baolu89a60792018-10-23 15:45:01 +08002900 /*
2901 * Prevent any device marked as untrusted from getting
2902 * placed into the statically identity mapping domain.
2903 */
2904 if (pdev->untrusted)
2905 return 0;
2906
David Woodhouse3bdb2592014-03-09 16:03:08 -07002907 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2908 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002909
David Woodhouse3bdb2592014-03-09 16:03:08 -07002910 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2911 return 1;
2912
2913 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2914 return 0;
2915
2916 /*
2917 * We want to start off with all devices in the 1:1 domain, and
2918 * take them out later if we find they can't access all of memory.
2919 *
2920 * However, we can't do this for PCI devices behind bridges,
2921 * because all PCI devices behind the same bridge will end up
2922 * with the same source-id on their transactions.
2923 *
2924 * Practically speaking, we can't change things around for these
2925 * devices at run-time, because we can't be sure there'll be no
2926 * DMA transactions in flight for any of their siblings.
2927 *
2928 * So PCI devices (unless they're on the root bus) as well as
2929 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2930 * the 1:1 domain, just in _case_ one of their siblings turns out
2931 * not to be able to map all of memory.
2932 */
2933 if (!pci_is_pcie(pdev)) {
2934 if (!pci_is_root_bus(pdev->bus))
2935 return 0;
2936 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2937 return 0;
2938 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2939 return 0;
2940 } else {
2941 if (device_has_rmrr(dev))
2942 return 0;
2943 }
David Woodhouse6941af22009-07-04 18:24:27 +01002944
David Woodhouse3dfc8132009-07-04 19:11:08 +01002945 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002946 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002947 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002948 * take them out of the 1:1 domain later.
2949 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002950 if (!startup) {
2951 /*
2952 * If the device's dma_mask is less than the system's memory
2953 * size then this is not a candidate for identity mapping.
2954 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002955 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002956
David Woodhouse3bdb2592014-03-09 16:03:08 -07002957 if (dev->coherent_dma_mask &&
2958 dev->coherent_dma_mask < dma_mask)
2959 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002960
David Woodhouse3bdb2592014-03-09 16:03:08 -07002961 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002962 }
David Woodhouse6941af22009-07-04 18:24:27 +01002963
2964 return 1;
2965}
2966
David Woodhousecf04eee2014-03-21 16:49:04 +00002967static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2968{
2969 int ret;
2970
2971 if (!iommu_should_identity_map(dev, 1))
2972 return 0;
2973
Joerg Roedel28ccce02015-07-21 14:45:31 +02002974 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002975 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002976 pr_info("%s identity mapping for device %s\n",
2977 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002978 else if (ret == -ENODEV)
2979 /* device not associated with an iommu */
2980 ret = 0;
2981
2982 return ret;
2983}
2984
2985
Matt Kraai071e1372009-08-23 22:30:22 -07002986static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002987{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002988 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002989 struct dmar_drhd_unit *drhd;
2990 struct intel_iommu *iommu;
2991 struct device *dev;
2992 int i;
2993 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002994
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002995 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002996 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2997 if (ret)
2998 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002999 }
3000
David Woodhousecf04eee2014-03-21 16:49:04 +00003001 for_each_active_iommu(iommu, drhd)
3002 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
3003 struct acpi_device_physical_node *pn;
3004 struct acpi_device *adev;
3005
3006 if (dev->bus != &acpi_bus_type)
3007 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02003008
David Woodhousecf04eee2014-03-21 16:49:04 +00003009 adev= to_acpi_device(dev);
3010 mutex_lock(&adev->physical_node_lock);
3011 list_for_each_entry(pn, &adev->physical_node_list, node) {
3012 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
3013 if (ret)
3014 break;
3015 }
3016 mutex_unlock(&adev->physical_node_lock);
3017 if (ret)
3018 return ret;
3019 }
3020
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003021 return 0;
3022}
3023
Jiang Liuffebeb42014-11-09 22:48:02 +08003024static void intel_iommu_init_qi(struct intel_iommu *iommu)
3025{
3026 /*
3027 * Start from the sane iommu hardware state.
3028 * If the queued invalidation is already initialized by us
3029 * (for example, while enabling interrupt-remapping) then
3030 * we got the things already rolling from a sane state.
3031 */
3032 if (!iommu->qi) {
3033 /*
3034 * Clear any previous faults.
3035 */
3036 dmar_fault(-1, iommu);
3037 /*
3038 * Disable queued invalidation if supported and already enabled
3039 * before OS handover.
3040 */
3041 dmar_disable_qi(iommu);
3042 }
3043
3044 if (dmar_enable_qi(iommu)) {
3045 /*
3046 * Queued Invalidate not enabled, use Register Based Invalidate
3047 */
3048 iommu->flush.flush_context = __iommu_flush_context;
3049 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003050 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003051 iommu->name);
3052 } else {
3053 iommu->flush.flush_context = qi_flush_context;
3054 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003055 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08003056 }
3057}
3058
Joerg Roedel091d42e2015-06-12 11:56:10 +02003059static int copy_context_table(struct intel_iommu *iommu,
Dan Williamsdfddb962015-10-09 18:16:46 -04003060 struct root_entry *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02003061 struct context_entry **tbl,
3062 int bus, bool ext)
3063{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003064 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003065 struct context_entry *new_ce = NULL, ce;
Dan Williamsdfddb962015-10-09 18:16:46 -04003066 struct context_entry *old_ce = NULL;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003067 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003068 phys_addr_t old_ce_phys;
3069
3070 tbl_idx = ext ? bus * 2 : bus;
Dan Williamsdfddb962015-10-09 18:16:46 -04003071 memcpy(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003072
3073 for (devfn = 0; devfn < 256; devfn++) {
3074 /* First calculate the correct index */
3075 idx = (ext ? devfn * 2 : devfn) % 256;
3076
3077 if (idx == 0) {
3078 /* First save what we may have and clean up */
3079 if (new_ce) {
3080 tbl[tbl_idx] = new_ce;
3081 __iommu_flush_cache(iommu, new_ce,
3082 VTD_PAGE_SIZE);
3083 pos = 1;
3084 }
3085
3086 if (old_ce)
Pan Bian829383e2018-11-21 17:53:47 +08003087 memunmap(old_ce);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003088
3089 ret = 0;
3090 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003091 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003092 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003093 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003094
3095 if (!old_ce_phys) {
3096 if (ext && devfn == 0) {
3097 /* No LCTP, try UCTP */
3098 devfn = 0x7f;
3099 continue;
3100 } else {
3101 goto out;
3102 }
3103 }
3104
3105 ret = -ENOMEM;
Dan Williamsdfddb962015-10-09 18:16:46 -04003106 old_ce = memremap(old_ce_phys, PAGE_SIZE,
3107 MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003108 if (!old_ce)
3109 goto out;
3110
3111 new_ce = alloc_pgtable_page(iommu->node);
3112 if (!new_ce)
3113 goto out_unmap;
3114
3115 ret = 0;
3116 }
3117
3118 /* Now copy the context entry */
Dan Williamsdfddb962015-10-09 18:16:46 -04003119 memcpy(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003120
Joerg Roedelcf484d02015-06-12 12:21:46 +02003121 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02003122 continue;
3123
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003124 did = context_domain_id(&ce);
3125 if (did >= 0 && did < cap_ndoms(iommu->cap))
3126 set_bit(did, iommu->domain_ids);
3127
Joerg Roedelcf484d02015-06-12 12:21:46 +02003128 /*
3129 * We need a marker for copied context entries. This
3130 * marker needs to work for the old format as well as
3131 * for extended context entries.
3132 *
3133 * Bit 67 of the context entry is used. In the old
3134 * format this bit is available to software, in the
3135 * extended format it is the PGE bit, but PGE is ignored
3136 * by HW if PASIDs are disabled (and thus still
3137 * available).
3138 *
3139 * So disable PASIDs first and then mark the entry
3140 * copied. This means that we don't copy PASID
3141 * translations from the old kernel, but this is fine as
3142 * faults there are not fatal.
3143 */
3144 context_clear_pasid_enable(&ce);
3145 context_set_copied(&ce);
3146
Joerg Roedel091d42e2015-06-12 11:56:10 +02003147 new_ce[idx] = ce;
3148 }
3149
3150 tbl[tbl_idx + pos] = new_ce;
3151
3152 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
3153
3154out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003155 memunmap(old_ce);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003156
3157out:
3158 return ret;
3159}
3160
3161static int copy_translation_tables(struct intel_iommu *iommu)
3162{
3163 struct context_entry **ctxt_tbls;
Dan Williamsdfddb962015-10-09 18:16:46 -04003164 struct root_entry *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003165 phys_addr_t old_rt_phys;
3166 int ctxt_table_entries;
3167 unsigned long flags;
3168 u64 rtaddr_reg;
3169 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02003170 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003171
3172 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
3173 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02003174 new_ext = !!ecap_ecs(iommu->ecap);
3175
3176 /*
3177 * The RTT bit can only be changed when translation is disabled,
3178 * but disabling translation means to open a window for data
3179 * corruption. So bail out and don't copy anything if we would
3180 * have to change the bit.
3181 */
3182 if (new_ext != ext)
3183 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003184
3185 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
3186 if (!old_rt_phys)
3187 return -EINVAL;
3188
Dan Williamsdfddb962015-10-09 18:16:46 -04003189 old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003190 if (!old_rt)
3191 return -ENOMEM;
3192
3193 /* This is too big for the stack - allocate it from slab */
3194 ctxt_table_entries = ext ? 512 : 256;
3195 ret = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07003196 ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003197 if (!ctxt_tbls)
3198 goto out_unmap;
3199
3200 for (bus = 0; bus < 256; bus++) {
3201 ret = copy_context_table(iommu, &old_rt[bus],
3202 ctxt_tbls, bus, ext);
3203 if (ret) {
3204 pr_err("%s: Failed to copy context table for bus %d\n",
3205 iommu->name, bus);
3206 continue;
3207 }
3208 }
3209
3210 spin_lock_irqsave(&iommu->lock, flags);
3211
3212 /* Context tables are copied, now write them to the root_entry table */
3213 for (bus = 0; bus < 256; bus++) {
3214 int idx = ext ? bus * 2 : bus;
3215 u64 val;
3216
3217 if (ctxt_tbls[idx]) {
3218 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3219 iommu->root_entry[bus].lo = val;
3220 }
3221
3222 if (!ext || !ctxt_tbls[idx + 1])
3223 continue;
3224
3225 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3226 iommu->root_entry[bus].hi = val;
3227 }
3228
3229 spin_unlock_irqrestore(&iommu->lock, flags);
3230
3231 kfree(ctxt_tbls);
3232
3233 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3234
3235 ret = 0;
3236
3237out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003238 memunmap(old_rt);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003239
3240 return ret;
3241}
3242
Joseph Cihulab7792602011-05-03 00:08:37 -07003243static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003244{
3245 struct dmar_drhd_unit *drhd;
3246 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003247 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003248 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003249 struct intel_iommu *iommu;
Joerg Roedel13cf0172017-08-11 11:40:10 +02003250 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003251
3252 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003253 * for each drhd
3254 * allocate root
3255 * initialize and program root entry to not present
3256 * endfor
3257 */
3258 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003259 /*
3260 * lock not needed as this is only incremented in the single
3261 * threaded kernel __init code path all other access are read
3262 * only
3263 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003264 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003265 g_num_of_iommus++;
3266 continue;
3267 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003268 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003269 }
3270
Jiang Liuffebeb42014-11-09 22:48:02 +08003271 /* Preallocate enough resources for IOMMU hot-addition */
3272 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3273 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3274
Weidong Hand9630fe2008-12-08 11:06:32 +08003275 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3276 GFP_KERNEL);
3277 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003278 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003279 ret = -ENOMEM;
3280 goto error;
3281 }
3282
Jiang Liu7c919772014-01-06 14:18:18 +08003283 for_each_active_iommu(iommu, drhd) {
Lu Baolu56283172018-07-14 15:46:54 +08003284 /*
3285 * Find the max pasid size of all IOMMU's in the system.
3286 * We need to ensure the system pasid table is no bigger
3287 * than the smallest supported.
3288 */
3289 if (pasid_enabled(iommu)) {
3290 u32 temp = 2 << ecap_pss(iommu->ecap);
3291
3292 intel_pasid_max_id = min_t(u32, temp,
3293 intel_pasid_max_id);
3294 }
3295
Weidong Hand9630fe2008-12-08 11:06:32 +08003296 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003297
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003298 intel_iommu_init_qi(iommu);
3299
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003300 ret = iommu_init_domains(iommu);
3301 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003302 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003303
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003304 init_translation_status(iommu);
3305
Joerg Roedel091d42e2015-06-12 11:56:10 +02003306 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3307 iommu_disable_translation(iommu);
3308 clear_translation_pre_enabled(iommu);
3309 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3310 iommu->name);
3311 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003312
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003313 /*
3314 * TBD:
3315 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003316 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003317 */
3318 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003319 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003320 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003321
Joerg Roedel091d42e2015-06-12 11:56:10 +02003322 if (translation_pre_enabled(iommu)) {
3323 pr_info("Translation already enabled - trying to copy translation structures\n");
3324
3325 ret = copy_translation_tables(iommu);
3326 if (ret) {
3327 /*
3328 * We found the IOMMU with translation
3329 * enabled - but failed to copy over the
3330 * old root-entry table. Try to proceed
3331 * by disabling translation now and
3332 * allocating a clean root-entry table.
3333 * This might cause DMAR faults, but
3334 * probably the dump will still succeed.
3335 */
3336 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3337 iommu->name);
3338 iommu_disable_translation(iommu);
3339 clear_translation_pre_enabled(iommu);
3340 } else {
3341 pr_info("Copied translation tables from previous kernel for %s\n",
3342 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003343 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003344 }
3345 }
3346
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003347 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003348 hw_pass_through = 0;
David Woodhouse8a94ade2015-03-24 14:54:56 +00003349#ifdef CONFIG_INTEL_IOMMU_SVM
3350 if (pasid_enabled(iommu))
Lu Baolud9737952018-07-14 15:47:02 +08003351 intel_svm_init(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00003352#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003353 }
3354
Joerg Roedela4c34ff2016-06-17 11:29:48 +02003355 /*
3356 * Now that qi is enabled on all iommus, set the root entry and flush
3357 * caches. This is required on some Intel X58 chipsets, otherwise the
3358 * flush_context function will loop forever and the boot hangs.
3359 */
3360 for_each_active_iommu(iommu, drhd) {
3361 iommu_flush_write_buffer(iommu);
3362 iommu_set_root_entry(iommu);
3363 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3364 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3365 }
3366
David Woodhouse19943b02009-08-04 16:19:20 +01003367 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003368 iommu_identity_mapping |= IDENTMAP_ALL;
3369
Suresh Siddhad3f13812011-08-23 17:05:25 -07003370#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003371 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003372#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003373
Ashok Raj21e722c2017-01-30 09:39:53 -08003374 check_tylersburg_isoch();
3375
Joerg Roedel86080cc2015-06-12 12:27:16 +02003376 if (iommu_identity_mapping) {
3377 ret = si_domain_init(hw_pass_through);
3378 if (ret)
3379 goto free_iommu;
3380 }
3381
David Woodhousee0fc7e02009-09-30 09:12:17 -07003382
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003383 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003384 * If we copied translations from a previous kernel in the kdump
3385 * case, we can not assign the devices to domains now, as that
3386 * would eliminate the old mappings. So skip this part and defer
3387 * the assignment to device driver initialization time.
3388 */
3389 if (copied_tables)
3390 goto domains_done;
3391
3392 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003393 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003394 * identity mappings for rmrr, gfx, and isa and may fall back to static
3395 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003396 */
David Woodhouse19943b02009-08-04 16:19:20 +01003397 if (iommu_identity_mapping) {
3398 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3399 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003400 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003401 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003402 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003403 }
David Woodhouse19943b02009-08-04 16:19:20 +01003404 /*
3405 * For each rmrr
3406 * for each dev attached to rmrr
3407 * do
3408 * locate drhd for dev, alloc domain for dev
3409 * allocate free domain
3410 * allocate page table entries for rmrr
3411 * if context not allocated for bus
3412 * allocate and init context
3413 * set present in root table for this bus
3414 * init context with domain, translation etc
3415 * endfor
3416 * endfor
3417 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003418 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003419 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003420 /* some BIOS lists non-exist devices in DMAR table. */
3421 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003422 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003423 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003424 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003425 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003426 }
3427 }
3428
3429 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003430
Joerg Roedela87f4912015-06-12 12:32:54 +02003431domains_done:
3432
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003433 /*
3434 * for each drhd
3435 * enable fault log
3436 * global invalidate context cache
3437 * global invalidate iotlb
3438 * enable translation
3439 */
Jiang Liu7c919772014-01-06 14:18:18 +08003440 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003441 if (drhd->ignored) {
3442 /*
3443 * we always have to disable PMRs or DMA may fail on
3444 * this device
3445 */
3446 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003447 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003448 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003449 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003450
3451 iommu_flush_write_buffer(iommu);
3452
David Woodhousea222a7f2015-10-07 23:35:18 +01003453#ifdef CONFIG_INTEL_IOMMU_SVM
3454 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
3455 ret = intel_svm_enable_prq(iommu);
3456 if (ret)
3457 goto free_iommu;
3458 }
3459#endif
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003460 ret = dmar_set_interrupt(iommu);
3461 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003462 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003463
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003464 if (!translation_pre_enabled(iommu))
3465 iommu_enable_translation(iommu);
3466
David Woodhouseb94996c2009-09-19 15:28:12 -07003467 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003468 }
3469
3470 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003471
3472free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003473 for_each_active_iommu(iommu, drhd) {
3474 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003475 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003476 }
Joerg Roedel13cf0172017-08-11 11:40:10 +02003477
Weidong Hand9630fe2008-12-08 11:06:32 +08003478 kfree(g_iommus);
Joerg Roedel13cf0172017-08-11 11:40:10 +02003479
Jiang Liu989d51f2014-02-19 14:07:21 +08003480error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003481 return ret;
3482}
3483
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003484/* This takes a number of _MM_ pages, not VTD pages */
Omer Peleg2aac6302016-04-20 11:33:57 +03003485static unsigned long intel_alloc_iova(struct device *dev,
David Woodhouse875764d2009-06-28 21:20:51 +01003486 struct dmar_domain *domain,
3487 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003488{
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003489 unsigned long iova_pfn = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003490
David Woodhouse875764d2009-06-28 21:20:51 +01003491 /* Restrict dma_mask to the width that the iommu can handle */
3492 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003493 /* Ensure we reserve the whole size-aligned region */
3494 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003495
3496 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003497 /*
3498 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003499 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003500 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003501 */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003502 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02003503 IOVA_PFN(DMA_BIT_MASK(32)), false);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003504 if (iova_pfn)
3505 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003506 }
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02003507 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
3508 IOVA_PFN(dma_mask), true);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003509 if (unlikely(!iova_pfn)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003510 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003511 nrpages, dev_name(dev));
Omer Peleg2aac6302016-04-20 11:33:57 +03003512 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003513 }
3514
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003515 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003516}
3517
Lu Baolu9ddbfb42018-07-14 15:46:57 +08003518struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003519{
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003520 struct dmar_domain *domain, *tmp;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003521 struct dmar_rmrr_unit *rmrr;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003522 struct device *i_dev;
3523 int i, ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003524
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003525 domain = find_domain(dev);
3526 if (domain)
3527 goto out;
3528
3529 domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
3530 if (!domain)
3531 goto out;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003532
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003533 /* We have a new domain - setup possible RMRRs for the device */
3534 rcu_read_lock();
3535 for_each_rmrr_units(rmrr) {
3536 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
3537 i, i_dev) {
3538 if (i_dev != dev)
3539 continue;
3540
3541 ret = domain_prepare_identity_map(dev, domain,
3542 rmrr->base_address,
3543 rmrr->end_address);
3544 if (ret)
3545 dev_err(dev, "Mapping reserved region failed\n");
3546 }
3547 }
3548 rcu_read_unlock();
3549
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003550 tmp = set_domain_for_dev(dev, domain);
3551 if (!tmp || domain != tmp) {
3552 domain_exit(domain);
3553 domain = tmp;
3554 }
3555
3556out:
3557
3558 if (!domain)
3559 pr_err("Allocating domain for %s failed\n", dev_name(dev));
3560
3561
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003562 return domain;
3563}
3564
David Woodhouseecb509e2014-03-09 16:29:55 -07003565/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003566static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003567{
3568 int found;
3569
David Woodhouse3d891942014-03-06 15:59:26 +00003570 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003571 return 1;
3572
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003573 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003574 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003575
David Woodhouse9b226622014-03-09 14:03:28 -07003576 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003577 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003578 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003579 return 1;
3580 else {
3581 /*
3582 * 32 bit DMA is removed from si_domain and fall back
3583 * to non-identity mapping.
3584 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003585 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003586 pr_info("32bit %s uses non-identity mapping\n",
3587 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003588 return 0;
3589 }
3590 } else {
3591 /*
3592 * In case of a detached 64 bit DMA device from vm, the device
3593 * is put into si_domain for identity mapping.
3594 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003595 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003596 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003597 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003598 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003599 pr_info("64bit %s uses identity mapping\n",
3600 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003601 return 1;
3602 }
3603 }
3604 }
3605
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003606 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003607}
3608
David Woodhouse5040a912014-03-09 16:14:00 -07003609static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003610 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003611{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003612 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003613 phys_addr_t start_paddr;
Omer Peleg2aac6302016-04-20 11:33:57 +03003614 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003615 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003616 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003617 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003618 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003619
3620 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003621
David Woodhouse5040a912014-03-09 16:14:00 -07003622 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003623 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003624
David Woodhouse5040a912014-03-09 16:14:00 -07003625 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003626 if (!domain)
3627 return 0;
3628
Weidong Han8c11e792008-12-08 15:29:22 +08003629 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003630 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003631
Omer Peleg2aac6302016-04-20 11:33:57 +03003632 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
3633 if (!iova_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003634 goto error;
3635
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003636 /*
3637 * Check if DMAR supports zero-length reads on write only
3638 * mappings..
3639 */
3640 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003641 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003642 prot |= DMA_PTE_READ;
3643 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3644 prot |= DMA_PTE_WRITE;
3645 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003646 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003647 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003648 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003649 * is not a big problem
3650 */
Omer Peleg2aac6302016-04-20 11:33:57 +03003651 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003652 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003653 if (ret)
3654 goto error;
3655
Omer Peleg2aac6302016-04-20 11:33:57 +03003656 start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
David Woodhouse03d6a242009-06-28 15:33:46 +01003657 start_paddr += paddr & ~PAGE_MASK;
3658 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003659
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003660error:
Omer Peleg2aac6302016-04-20 11:33:57 +03003661 if (iova_pfn)
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003662 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003663 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003664 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003665 return 0;
3666}
3667
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003668static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3669 unsigned long offset, size_t size,
3670 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003671 unsigned long attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003672{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003673 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003674 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003675}
3676
Omer Peleg769530e2016-04-20 11:33:25 +03003677static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003678{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003679 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003680 unsigned long start_pfn, last_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +03003681 unsigned long nrpages;
Omer Peleg2aac6302016-04-20 11:33:57 +03003682 unsigned long iova_pfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003683 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003684 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003685
David Woodhouse73676832009-07-04 14:08:36 +01003686 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003687 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003688
David Woodhouse1525a292014-03-06 16:19:30 +00003689 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003690 BUG_ON(!domain);
3691
Weidong Han8c11e792008-12-08 15:29:22 +08003692 iommu = domain_get_iommu(domain);
3693
Omer Peleg2aac6302016-04-20 11:33:57 +03003694 iova_pfn = IOVA_PFN(dev_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003695
Omer Peleg769530e2016-04-20 11:33:25 +03003696 nrpages = aligned_nrpages(dev_addr, size);
Omer Peleg2aac6302016-04-20 11:33:57 +03003697 start_pfn = mm_to_dma_pfn(iova_pfn);
Omer Peleg769530e2016-04-20 11:33:25 +03003698 last_pfn = start_pfn + nrpages - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003699
David Woodhoused794dc92009-06-28 00:27:49 +01003700 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003701 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003702
David Woodhouseea8ea462014-03-05 17:09:32 +00003703 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003704
mark gross5e0d2a62008-03-04 15:22:08 -08003705 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003706 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
Omer Peleg769530e2016-04-20 11:33:25 +03003707 nrpages, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003708 /* free iova */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003709 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
David Woodhouseea8ea462014-03-05 17:09:32 +00003710 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003711 } else {
Joerg Roedel13cf0172017-08-11 11:40:10 +02003712 queue_iova(&domain->iovad, iova_pfn, nrpages,
3713 (unsigned long)freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003714 /*
3715 * queue up the release of the unmap to save the 1/6th of the
3716 * cpu used up by the iotlb flush operation...
3717 */
mark gross5e0d2a62008-03-04 15:22:08 -08003718 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003719}
3720
Jiang Liud41a4ad2014-07-11 14:19:34 +08003721static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3722 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003723 unsigned long attrs)
Jiang Liud41a4ad2014-07-11 14:19:34 +08003724{
Omer Peleg769530e2016-04-20 11:33:25 +03003725 intel_unmap(dev, dev_addr, size);
Jiang Liud41a4ad2014-07-11 14:19:34 +08003726}
3727
David Woodhouse5040a912014-03-09 16:14:00 -07003728static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003729 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003730 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003731{
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003732 struct page *page = NULL;
3733 int order;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003734
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003735 size = PAGE_ALIGN(size);
3736 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003737
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003738 if (!iommu_no_mapping(dev))
3739 flags &= ~(GFP_DMA | GFP_DMA32);
3740 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3741 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
3742 flags |= GFP_DMA;
3743 else
3744 flags |= GFP_DMA32;
3745 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003746
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003747 if (gfpflags_allow_blocking(flags)) {
3748 unsigned int count = size >> PAGE_SHIFT;
3749
Marek Szyprowskid834c5a2018-08-17 15:49:00 -07003750 page = dma_alloc_from_contiguous(dev, count, order,
3751 flags & __GFP_NOWARN);
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003752 if (page && iommu_no_mapping(dev) &&
3753 page_to_phys(page) + size > dev->coherent_dma_mask) {
3754 dma_release_from_contiguous(dev, page, count);
3755 page = NULL;
3756 }
3757 }
3758
3759 if (!page)
3760 page = alloc_pages(flags, order);
3761 if (!page)
3762 return NULL;
3763 memset(page_address(page), 0, size);
3764
3765 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
3766 DMA_BIDIRECTIONAL,
3767 dev->coherent_dma_mask);
3768 if (*dma_handle)
3769 return page_address(page);
3770 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3771 __free_pages(page, order);
3772
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003773 return NULL;
3774}
3775
David Woodhouse5040a912014-03-09 16:14:00 -07003776static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003777 dma_addr_t dma_handle, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003778{
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003779 int order;
3780 struct page *page = virt_to_page(vaddr);
3781
3782 size = PAGE_ALIGN(size);
3783 order = get_order(size);
3784
3785 intel_unmap(dev, dma_handle, size);
3786 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3787 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003788}
3789
David Woodhouse5040a912014-03-09 16:14:00 -07003790static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003791 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003792 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003793{
Omer Peleg769530e2016-04-20 11:33:25 +03003794 dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK;
3795 unsigned long nrpages = 0;
3796 struct scatterlist *sg;
3797 int i;
3798
3799 for_each_sg(sglist, sg, nelems, i) {
3800 nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg));
3801 }
3802
3803 intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003804}
3805
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003806static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003807 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003808{
3809 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003810 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003811
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003812 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003813 BUG_ON(!sg_page(sg));
Robin Murphy29a90b72017-09-28 15:14:01 +01003814 sg->dma_address = sg_phys(sg);
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003815 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003816 }
3817 return nelems;
3818}
3819
David Woodhouse5040a912014-03-09 16:14:00 -07003820static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003821 enum dma_data_direction dir, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003822{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003823 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003824 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003825 size_t size = 0;
3826 int prot = 0;
Omer Peleg2aac6302016-04-20 11:33:57 +03003827 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003828 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003829 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003830 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003831 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003832
3833 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003834 if (iommu_no_mapping(dev))
3835 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003836
David Woodhouse5040a912014-03-09 16:14:00 -07003837 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003838 if (!domain)
3839 return 0;
3840
Weidong Han8c11e792008-12-08 15:29:22 +08003841 iommu = domain_get_iommu(domain);
3842
David Woodhouseb536d242009-06-28 14:49:31 +01003843 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003844 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003845
Omer Peleg2aac6302016-04-20 11:33:57 +03003846 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
David Woodhouse5040a912014-03-09 16:14:00 -07003847 *dev->dma_mask);
Omer Peleg2aac6302016-04-20 11:33:57 +03003848 if (!iova_pfn) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003849 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003850 return 0;
3851 }
3852
3853 /*
3854 * Check if DMAR supports zero-length reads on write only
3855 * mappings..
3856 */
3857 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003858 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003859 prot |= DMA_PTE_READ;
3860 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3861 prot |= DMA_PTE_WRITE;
3862
Omer Peleg2aac6302016-04-20 11:33:57 +03003863 start_vpfn = mm_to_dma_pfn(iova_pfn);
David Woodhousee1605492009-06-29 11:17:38 +01003864
Fenghua Yuf5329592009-08-04 15:09:37 -07003865 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003866 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003867 dma_pte_free_pagetable(domain, start_vpfn,
David Dillowbc24c572017-06-28 19:42:23 -07003868 start_vpfn + size - 1,
3869 agaw_to_level(domain->agaw) + 1);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003870 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
David Woodhousee1605492009-06-29 11:17:38 +01003871 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003872 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003873
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003874 return nelems;
3875}
3876
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003877static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3878{
3879 return !dma_addr;
3880}
3881
Christoph Hellwig02b4da52018-09-17 19:10:31 +02003882static const struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003883 .alloc = intel_alloc_coherent,
3884 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003885 .map_sg = intel_map_sg,
3886 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003887 .map_page = intel_map_page,
3888 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003889 .mapping_error = intel_mapping_error,
Christoph Hellwigfec777c2018-03-19 11:38:15 +01003890 .dma_supported = dma_direct_supported,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003891};
3892
3893static inline int iommu_domain_cache_init(void)
3894{
3895 int ret = 0;
3896
3897 iommu_domain_cache = kmem_cache_create("iommu_domain",
3898 sizeof(struct dmar_domain),
3899 0,
3900 SLAB_HWCACHE_ALIGN,
3901
3902 NULL);
3903 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003904 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003905 ret = -ENOMEM;
3906 }
3907
3908 return ret;
3909}
3910
3911static inline int iommu_devinfo_cache_init(void)
3912{
3913 int ret = 0;
3914
3915 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3916 sizeof(struct device_domain_info),
3917 0,
3918 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003919 NULL);
3920 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003921 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003922 ret = -ENOMEM;
3923 }
3924
3925 return ret;
3926}
3927
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003928static int __init iommu_init_mempool(void)
3929{
3930 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003931 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003932 if (ret)
3933 return ret;
3934
3935 ret = iommu_domain_cache_init();
3936 if (ret)
3937 goto domain_error;
3938
3939 ret = iommu_devinfo_cache_init();
3940 if (!ret)
3941 return ret;
3942
3943 kmem_cache_destroy(iommu_domain_cache);
3944domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003945 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003946
3947 return -ENOMEM;
3948}
3949
3950static void __init iommu_exit_mempool(void)
3951{
3952 kmem_cache_destroy(iommu_devinfo_cache);
3953 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003954 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003955}
3956
Dan Williams556ab452010-07-23 15:47:56 -07003957static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3958{
3959 struct dmar_drhd_unit *drhd;
3960 u32 vtbar;
3961 int rc;
3962
3963 /* We know that this device on this chipset has its own IOMMU.
3964 * If we find it under a different IOMMU, then the BIOS is lying
3965 * to us. Hope that the IOMMU for this device is actually
3966 * disabled, and it needs no translation...
3967 */
3968 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3969 if (rc) {
3970 /* "can't" happen */
3971 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3972 return;
3973 }
3974 vtbar &= 0xffff0000;
3975
3976 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3977 drhd = dmar_find_matched_drhd_unit(pdev);
3978 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3979 TAINT_FIRMWARE_WORKAROUND,
3980 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3981 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3982}
3983DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3984
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003985static void __init init_no_remapping_devices(void)
3986{
3987 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003988 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003989 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003990
3991 for_each_drhd_unit(drhd) {
3992 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003993 for_each_active_dev_scope(drhd->devices,
3994 drhd->devices_cnt, i, dev)
3995 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003996 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003997 if (i == drhd->devices_cnt)
3998 drhd->ignored = 1;
3999 }
4000 }
4001
Jiang Liu7c919772014-01-06 14:18:18 +08004002 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08004003 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004004 continue;
4005
Jiang Liub683b232014-02-19 14:07:32 +08004006 for_each_active_dev_scope(drhd->devices,
4007 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00004008 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004009 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004010 if (i < drhd->devices_cnt)
4011 continue;
4012
David Woodhousec0771df2011-10-14 20:59:46 +01004013 /* This IOMMU has *only* gfx devices. Either bypass it or
4014 set the gfx_mapped flag, as appropriate */
4015 if (dmar_map_gfx) {
4016 intel_iommu_gfx_mapped = 1;
4017 } else {
4018 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08004019 for_each_active_dev_scope(drhd->devices,
4020 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00004021 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004022 }
4023 }
4024}
4025
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004026#ifdef CONFIG_SUSPEND
4027static int init_iommu_hw(void)
4028{
4029 struct dmar_drhd_unit *drhd;
4030 struct intel_iommu *iommu = NULL;
4031
4032 for_each_active_iommu(iommu, drhd)
4033 if (iommu->qi)
4034 dmar_reenable_qi(iommu);
4035
Joseph Cihulab7792602011-05-03 00:08:37 -07004036 for_each_iommu(iommu, drhd) {
4037 if (drhd->ignored) {
4038 /*
4039 * we always have to disable PMRs or DMA may fail on
4040 * this device
4041 */
4042 if (force_on)
4043 iommu_disable_protect_mem_regions(iommu);
4044 continue;
4045 }
4046
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004047 iommu_flush_write_buffer(iommu);
4048
4049 iommu_set_root_entry(iommu);
4050
4051 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004052 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08004053 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4054 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07004055 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004056 }
4057
4058 return 0;
4059}
4060
4061static void iommu_flush_all(void)
4062{
4063 struct dmar_drhd_unit *drhd;
4064 struct intel_iommu *iommu;
4065
4066 for_each_active_iommu(iommu, drhd) {
4067 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004068 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004069 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004070 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004071 }
4072}
4073
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004074static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004075{
4076 struct dmar_drhd_unit *drhd;
4077 struct intel_iommu *iommu = NULL;
4078 unsigned long flag;
4079
4080 for_each_active_iommu(iommu, drhd) {
Kees Cook6396bb22018-06-12 14:03:40 -07004081 iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32),
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004082 GFP_ATOMIC);
4083 if (!iommu->iommu_state)
4084 goto nomem;
4085 }
4086
4087 iommu_flush_all();
4088
4089 for_each_active_iommu(iommu, drhd) {
4090 iommu_disable_translation(iommu);
4091
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004092 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004093
4094 iommu->iommu_state[SR_DMAR_FECTL_REG] =
4095 readl(iommu->reg + DMAR_FECTL_REG);
4096 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
4097 readl(iommu->reg + DMAR_FEDATA_REG);
4098 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
4099 readl(iommu->reg + DMAR_FEADDR_REG);
4100 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
4101 readl(iommu->reg + DMAR_FEUADDR_REG);
4102
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004103 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004104 }
4105 return 0;
4106
4107nomem:
4108 for_each_active_iommu(iommu, drhd)
4109 kfree(iommu->iommu_state);
4110
4111 return -ENOMEM;
4112}
4113
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004114static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004115{
4116 struct dmar_drhd_unit *drhd;
4117 struct intel_iommu *iommu = NULL;
4118 unsigned long flag;
4119
4120 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07004121 if (force_on)
4122 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
4123 else
4124 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004125 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004126 }
4127
4128 for_each_active_iommu(iommu, drhd) {
4129
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004130 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004131
4132 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
4133 iommu->reg + DMAR_FECTL_REG);
4134 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
4135 iommu->reg + DMAR_FEDATA_REG);
4136 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
4137 iommu->reg + DMAR_FEADDR_REG);
4138 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
4139 iommu->reg + DMAR_FEUADDR_REG);
4140
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004141 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004142 }
4143
4144 for_each_active_iommu(iommu, drhd)
4145 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004146}
4147
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004148static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004149 .resume = iommu_resume,
4150 .suspend = iommu_suspend,
4151};
4152
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004153static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004154{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004155 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004156}
4157
4158#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004159static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004160#endif /* CONFIG_PM */
4161
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004162
Jiang Liuc2a0b532014-11-09 22:47:56 +08004163int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004164{
4165 struct acpi_dmar_reserved_memory *rmrr;
Eric Auger0659b8d2017-01-19 20:57:53 +00004166 int prot = DMA_PTE_READ|DMA_PTE_WRITE;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004167 struct dmar_rmrr_unit *rmrru;
Eric Auger0659b8d2017-01-19 20:57:53 +00004168 size_t length;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004169
4170 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4171 if (!rmrru)
Eric Auger0659b8d2017-01-19 20:57:53 +00004172 goto out;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004173
4174 rmrru->hdr = header;
4175 rmrr = (struct acpi_dmar_reserved_memory *)header;
4176 rmrru->base_address = rmrr->base_address;
4177 rmrru->end_address = rmrr->end_address;
Eric Auger0659b8d2017-01-19 20:57:53 +00004178
4179 length = rmrr->end_address - rmrr->base_address + 1;
4180 rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length, prot,
4181 IOMMU_RESV_DIRECT);
4182 if (!rmrru->resv)
4183 goto free_rmrru;
4184
Jiang Liu2e455282014-02-19 14:07:36 +08004185 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4186 ((void *)rmrr) + rmrr->header.length,
4187 &rmrru->devices_cnt);
Eric Auger0659b8d2017-01-19 20:57:53 +00004188 if (rmrru->devices_cnt && rmrru->devices == NULL)
4189 goto free_all;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004190
Jiang Liu2e455282014-02-19 14:07:36 +08004191 list_add(&rmrru->list, &dmar_rmrr_units);
4192
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004193 return 0;
Eric Auger0659b8d2017-01-19 20:57:53 +00004194free_all:
4195 kfree(rmrru->resv);
4196free_rmrru:
4197 kfree(rmrru);
4198out:
4199 return -ENOMEM;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004200}
4201
Jiang Liu6b197242014-11-09 22:47:58 +08004202static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4203{
4204 struct dmar_atsr_unit *atsru;
4205 struct acpi_dmar_atsr *tmp;
4206
4207 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4208 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4209 if (atsr->segment != tmp->segment)
4210 continue;
4211 if (atsr->header.length != tmp->header.length)
4212 continue;
4213 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4214 return atsru;
4215 }
4216
4217 return NULL;
4218}
4219
4220int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004221{
4222 struct acpi_dmar_atsr *atsr;
4223 struct dmar_atsr_unit *atsru;
4224
Thomas Gleixnerb608fe32017-05-16 20:42:41 +02004225 if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
Jiang Liu6b197242014-11-09 22:47:58 +08004226 return 0;
4227
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004228 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004229 atsru = dmar_find_atsr(atsr);
4230 if (atsru)
4231 return 0;
4232
4233 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004234 if (!atsru)
4235 return -ENOMEM;
4236
Jiang Liu6b197242014-11-09 22:47:58 +08004237 /*
4238 * If memory is allocated from slab by ACPI _DSM method, we need to
4239 * copy the memory content because the memory buffer will be freed
4240 * on return.
4241 */
4242 atsru->hdr = (void *)(atsru + 1);
4243 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004244 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004245 if (!atsru->include_all) {
4246 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4247 (void *)atsr + atsr->header.length,
4248 &atsru->devices_cnt);
4249 if (atsru->devices_cnt && atsru->devices == NULL) {
4250 kfree(atsru);
4251 return -ENOMEM;
4252 }
4253 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004254
Jiang Liu0e242612014-02-19 14:07:34 +08004255 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004256
4257 return 0;
4258}
4259
Jiang Liu9bdc5312014-01-06 14:18:27 +08004260static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4261{
4262 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4263 kfree(atsru);
4264}
4265
Jiang Liu6b197242014-11-09 22:47:58 +08004266int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4267{
4268 struct acpi_dmar_atsr *atsr;
4269 struct dmar_atsr_unit *atsru;
4270
4271 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4272 atsru = dmar_find_atsr(atsr);
4273 if (atsru) {
4274 list_del_rcu(&atsru->list);
4275 synchronize_rcu();
4276 intel_iommu_free_atsr(atsru);
4277 }
4278
4279 return 0;
4280}
4281
4282int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4283{
4284 int i;
4285 struct device *dev;
4286 struct acpi_dmar_atsr *atsr;
4287 struct dmar_atsr_unit *atsru;
4288
4289 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4290 atsru = dmar_find_atsr(atsr);
4291 if (!atsru)
4292 return 0;
4293
Linus Torvalds194dc872016-07-27 20:03:31 -07004294 if (!atsru->include_all && atsru->devices && atsru->devices_cnt) {
Jiang Liu6b197242014-11-09 22:47:58 +08004295 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4296 i, dev)
4297 return -EBUSY;
Linus Torvalds194dc872016-07-27 20:03:31 -07004298 }
Jiang Liu6b197242014-11-09 22:47:58 +08004299
4300 return 0;
4301}
4302
Jiang Liuffebeb42014-11-09 22:48:02 +08004303static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4304{
4305 int sp, ret = 0;
4306 struct intel_iommu *iommu = dmaru->iommu;
4307
4308 if (g_iommus[iommu->seq_id])
4309 return 0;
4310
4311 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004312 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004313 iommu->name);
4314 return -ENXIO;
4315 }
4316 if (!ecap_sc_support(iommu->ecap) &&
4317 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004318 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004319 iommu->name);
4320 return -ENXIO;
4321 }
4322 sp = domain_update_iommu_superpage(iommu) - 1;
4323 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004324 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004325 iommu->name);
4326 return -ENXIO;
4327 }
4328
4329 /*
4330 * Disable translation if already enabled prior to OS handover.
4331 */
4332 if (iommu->gcmd & DMA_GCMD_TE)
4333 iommu_disable_translation(iommu);
4334
4335 g_iommus[iommu->seq_id] = iommu;
4336 ret = iommu_init_domains(iommu);
4337 if (ret == 0)
4338 ret = iommu_alloc_root_entry(iommu);
4339 if (ret)
4340 goto out;
4341
David Woodhouse8a94ade2015-03-24 14:54:56 +00004342#ifdef CONFIG_INTEL_IOMMU_SVM
4343 if (pasid_enabled(iommu))
Lu Baolud9737952018-07-14 15:47:02 +08004344 intel_svm_init(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00004345#endif
4346
Jiang Liuffebeb42014-11-09 22:48:02 +08004347 if (dmaru->ignored) {
4348 /*
4349 * we always have to disable PMRs or DMA may fail on this device
4350 */
4351 if (force_on)
4352 iommu_disable_protect_mem_regions(iommu);
4353 return 0;
4354 }
4355
4356 intel_iommu_init_qi(iommu);
4357 iommu_flush_write_buffer(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01004358
4359#ifdef CONFIG_INTEL_IOMMU_SVM
4360 if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) {
4361 ret = intel_svm_enable_prq(iommu);
4362 if (ret)
4363 goto disable_iommu;
4364 }
4365#endif
Jiang Liuffebeb42014-11-09 22:48:02 +08004366 ret = dmar_set_interrupt(iommu);
4367 if (ret)
4368 goto disable_iommu;
4369
4370 iommu_set_root_entry(iommu);
4371 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4372 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4373 iommu_enable_translation(iommu);
4374
Jiang Liuffebeb42014-11-09 22:48:02 +08004375 iommu_disable_protect_mem_regions(iommu);
4376 return 0;
4377
4378disable_iommu:
4379 disable_dmar_iommu(iommu);
4380out:
4381 free_dmar_iommu(iommu);
4382 return ret;
4383}
4384
Jiang Liu6b197242014-11-09 22:47:58 +08004385int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4386{
Jiang Liuffebeb42014-11-09 22:48:02 +08004387 int ret = 0;
4388 struct intel_iommu *iommu = dmaru->iommu;
4389
4390 if (!intel_iommu_enabled)
4391 return 0;
4392 if (iommu == NULL)
4393 return -EINVAL;
4394
4395 if (insert) {
4396 ret = intel_iommu_add(dmaru);
4397 } else {
4398 disable_dmar_iommu(iommu);
4399 free_dmar_iommu(iommu);
4400 }
4401
4402 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004403}
4404
Jiang Liu9bdc5312014-01-06 14:18:27 +08004405static void intel_iommu_free_dmars(void)
4406{
4407 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4408 struct dmar_atsr_unit *atsru, *atsr_n;
4409
4410 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4411 list_del(&rmrru->list);
4412 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
Eric Auger0659b8d2017-01-19 20:57:53 +00004413 kfree(rmrru->resv);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004414 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004415 }
4416
Jiang Liu9bdc5312014-01-06 14:18:27 +08004417 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4418 list_del(&atsru->list);
4419 intel_iommu_free_atsr(atsru);
4420 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004421}
4422
4423int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4424{
Jiang Liub683b232014-02-19 14:07:32 +08004425 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004426 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004427 struct pci_dev *bridge = NULL;
4428 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004429 struct acpi_dmar_atsr *atsr;
4430 struct dmar_atsr_unit *atsru;
4431
4432 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004433 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004434 bridge = bus->self;
David Woodhoused14053b32015-10-15 09:28:06 +01004435 /* If it's an integrated device, allow ATS */
4436 if (!bridge)
4437 return 1;
4438 /* Connected via non-PCIe: no ATS */
4439 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004440 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004441 return 0;
David Woodhoused14053b32015-10-15 09:28:06 +01004442 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004443 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004444 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004445 }
4446
Jiang Liu0e242612014-02-19 14:07:34 +08004447 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004448 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4449 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4450 if (atsr->segment != pci_domain_nr(dev->bus))
4451 continue;
4452
Jiang Liub683b232014-02-19 14:07:32 +08004453 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004454 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004455 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004456
4457 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004458 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004459 }
Jiang Liub683b232014-02-19 14:07:32 +08004460 ret = 0;
4461out:
Jiang Liu0e242612014-02-19 14:07:34 +08004462 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004463
Jiang Liub683b232014-02-19 14:07:32 +08004464 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004465}
4466
Jiang Liu59ce0512014-02-19 14:07:35 +08004467int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4468{
4469 int ret = 0;
4470 struct dmar_rmrr_unit *rmrru;
4471 struct dmar_atsr_unit *atsru;
4472 struct acpi_dmar_atsr *atsr;
4473 struct acpi_dmar_reserved_memory *rmrr;
4474
Thomas Gleixnerb608fe32017-05-16 20:42:41 +02004475 if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
Jiang Liu59ce0512014-02-19 14:07:35 +08004476 return 0;
4477
4478 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4479 rmrr = container_of(rmrru->hdr,
4480 struct acpi_dmar_reserved_memory, header);
4481 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4482 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4483 ((void *)rmrr) + rmrr->header.length,
4484 rmrr->segment, rmrru->devices,
4485 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004486 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004487 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004488 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004489 dmar_remove_dev_scope(info, rmrr->segment,
4490 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004491 }
4492 }
4493
4494 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4495 if (atsru->include_all)
4496 continue;
4497
4498 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4499 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4500 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4501 (void *)atsr + atsr->header.length,
4502 atsr->segment, atsru->devices,
4503 atsru->devices_cnt);
4504 if (ret > 0)
4505 break;
4506 else if(ret < 0)
4507 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004508 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu59ce0512014-02-19 14:07:35 +08004509 if (dmar_remove_dev_scope(info, atsr->segment,
4510 atsru->devices, atsru->devices_cnt))
4511 break;
4512 }
4513 }
4514
4515 return 0;
4516}
4517
Fenghua Yu99dcade2009-11-11 07:23:06 -08004518/*
4519 * Here we only respond to action of unbound device from driver.
4520 *
4521 * Added device is not attached to its DMAR domain here yet. That will happen
4522 * when mapping the device to iova.
4523 */
4524static int device_notifier(struct notifier_block *nb,
4525 unsigned long action, void *data)
4526{
4527 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004528 struct dmar_domain *domain;
4529
David Woodhouse3d891942014-03-06 15:59:26 +00004530 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004531 return 0;
4532
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004533 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004534 return 0;
4535
David Woodhouse1525a292014-03-06 16:19:30 +00004536 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004537 if (!domain)
4538 return 0;
4539
Joerg Roedele6de0f82015-07-22 16:30:36 +02004540 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004541 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004542 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004543
Fenghua Yu99dcade2009-11-11 07:23:06 -08004544 return 0;
4545}
4546
4547static struct notifier_block device_nb = {
4548 .notifier_call = device_notifier,
4549};
4550
Jiang Liu75f05562014-02-19 14:07:37 +08004551static int intel_iommu_memory_notifier(struct notifier_block *nb,
4552 unsigned long val, void *v)
4553{
4554 struct memory_notify *mhp = v;
4555 unsigned long long start, end;
4556 unsigned long start_vpfn, last_vpfn;
4557
4558 switch (val) {
4559 case MEM_GOING_ONLINE:
4560 start = mhp->start_pfn << PAGE_SHIFT;
4561 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4562 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004563 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004564 start, end);
4565 return NOTIFY_BAD;
4566 }
4567 break;
4568
4569 case MEM_OFFLINE:
4570 case MEM_CANCEL_ONLINE:
4571 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4572 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4573 while (start_vpfn <= last_vpfn) {
4574 struct iova *iova;
4575 struct dmar_drhd_unit *drhd;
4576 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004577 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004578
4579 iova = find_iova(&si_domain->iovad, start_vpfn);
4580 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004581 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004582 start_vpfn);
4583 break;
4584 }
4585
4586 iova = split_and_remove_iova(&si_domain->iovad, iova,
4587 start_vpfn, last_vpfn);
4588 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004589 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004590 start_vpfn, last_vpfn);
4591 return NOTIFY_BAD;
4592 }
4593
David Woodhouseea8ea462014-03-05 17:09:32 +00004594 freelist = domain_unmap(si_domain, iova->pfn_lo,
4595 iova->pfn_hi);
4596
Jiang Liu75f05562014-02-19 14:07:37 +08004597 rcu_read_lock();
4598 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004599 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004600 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004601 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004602 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004603 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004604
4605 start_vpfn = iova->pfn_hi + 1;
4606 free_iova_mem(iova);
4607 }
4608 break;
4609 }
4610
4611 return NOTIFY_OK;
4612}
4613
4614static struct notifier_block intel_iommu_memory_nb = {
4615 .notifier_call = intel_iommu_memory_notifier,
4616 .priority = 0
4617};
4618
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004619static void free_all_cpu_cached_iovas(unsigned int cpu)
4620{
4621 int i;
4622
4623 for (i = 0; i < g_num_of_iommus; i++) {
4624 struct intel_iommu *iommu = g_iommus[i];
4625 struct dmar_domain *domain;
Aaron Campbell0caa7612016-07-02 21:23:24 -03004626 int did;
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004627
4628 if (!iommu)
4629 continue;
4630
Jan Niehusmann3bd4f912016-06-06 14:20:11 +02004631 for (did = 0; did < cap_ndoms(iommu->cap); did++) {
Aaron Campbell0caa7612016-07-02 21:23:24 -03004632 domain = get_iommu_domain(iommu, (u16)did);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004633
4634 if (!domain)
4635 continue;
4636 free_cpu_cached_iovas(cpu, &domain->iovad);
4637 }
4638 }
4639}
4640
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004641static int intel_iommu_cpu_dead(unsigned int cpu)
Omer Pelegaa473242016-04-20 11:33:02 +03004642{
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004643 free_all_cpu_cached_iovas(cpu);
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004644 return 0;
Omer Pelegaa473242016-04-20 11:33:02 +03004645}
4646
Joerg Roedel161b28a2017-03-28 17:04:52 +02004647static void intel_disable_iommus(void)
4648{
4649 struct intel_iommu *iommu = NULL;
4650 struct dmar_drhd_unit *drhd;
4651
4652 for_each_iommu(iommu, drhd)
4653 iommu_disable_translation(iommu);
4654}
4655
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004656static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev)
4657{
Joerg Roedel2926a2aa2017-08-14 17:19:26 +02004658 struct iommu_device *iommu_dev = dev_to_iommu_device(dev);
4659
4660 return container_of(iommu_dev, struct intel_iommu, iommu);
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004661}
4662
Alex Williamsona5459cf2014-06-12 16:12:31 -06004663static ssize_t intel_iommu_show_version(struct device *dev,
4664 struct device_attribute *attr,
4665 char *buf)
4666{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004667 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004668 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4669 return sprintf(buf, "%d:%d\n",
4670 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4671}
4672static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4673
4674static ssize_t intel_iommu_show_address(struct device *dev,
4675 struct device_attribute *attr,
4676 char *buf)
4677{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004678 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004679 return sprintf(buf, "%llx\n", iommu->reg_phys);
4680}
4681static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4682
4683static ssize_t intel_iommu_show_cap(struct device *dev,
4684 struct device_attribute *attr,
4685 char *buf)
4686{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004687 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004688 return sprintf(buf, "%llx\n", iommu->cap);
4689}
4690static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4691
4692static ssize_t intel_iommu_show_ecap(struct device *dev,
4693 struct device_attribute *attr,
4694 char *buf)
4695{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004696 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004697 return sprintf(buf, "%llx\n", iommu->ecap);
4698}
4699static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4700
Alex Williamson2238c082015-07-14 15:24:53 -06004701static ssize_t intel_iommu_show_ndoms(struct device *dev,
4702 struct device_attribute *attr,
4703 char *buf)
4704{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004705 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamson2238c082015-07-14 15:24:53 -06004706 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4707}
4708static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4709
4710static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4711 struct device_attribute *attr,
4712 char *buf)
4713{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004714 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamson2238c082015-07-14 15:24:53 -06004715 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4716 cap_ndoms(iommu->cap)));
4717}
4718static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4719
Alex Williamsona5459cf2014-06-12 16:12:31 -06004720static struct attribute *intel_iommu_attrs[] = {
4721 &dev_attr_version.attr,
4722 &dev_attr_address.attr,
4723 &dev_attr_cap.attr,
4724 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004725 &dev_attr_domains_supported.attr,
4726 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004727 NULL,
4728};
4729
4730static struct attribute_group intel_iommu_group = {
4731 .name = "intel-iommu",
4732 .attrs = intel_iommu_attrs,
4733};
4734
4735const struct attribute_group *intel_iommu_groups[] = {
4736 &intel_iommu_group,
4737 NULL,
4738};
4739
Lu Baolu89a60792018-10-23 15:45:01 +08004740static int __init platform_optin_force_iommu(void)
4741{
4742 struct pci_dev *pdev = NULL;
4743 bool has_untrusted_dev = false;
4744
4745 if (!dmar_platform_optin() || no_platform_optin)
4746 return 0;
4747
4748 for_each_pci_dev(pdev) {
4749 if (pdev->untrusted) {
4750 has_untrusted_dev = true;
4751 break;
4752 }
4753 }
4754
4755 if (!has_untrusted_dev)
4756 return 0;
4757
4758 if (no_iommu || dmar_disabled)
4759 pr_info("Intel-IOMMU force enabled due to platform opt in\n");
4760
4761 /*
4762 * If Intel-IOMMU is disabled by default, we will apply identity
4763 * map for all devices except those marked as being untrusted.
4764 */
4765 if (dmar_disabled)
4766 iommu_identity_mapping |= IDENTMAP_ALL;
4767
4768 dmar_disabled = 0;
4769#if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB)
4770 swiotlb = 0;
4771#endif
4772 no_iommu = 0;
4773
4774 return 1;
4775}
4776
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004777int __init intel_iommu_init(void)
4778{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004779 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004780 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004781 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004782
Lu Baolu89a60792018-10-23 15:45:01 +08004783 /*
4784 * Intel IOMMU is required for a TXT/tboot launch or platform
4785 * opt in, so enforce that.
4786 */
4787 force_on = tboot_force_iommu() || platform_optin_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004788
Jiang Liu3a5670e2014-02-19 14:07:33 +08004789 if (iommu_init_mempool()) {
4790 if (force_on)
4791 panic("tboot: Failed to initialize iommu memory\n");
4792 return -ENOMEM;
4793 }
4794
4795 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004796 if (dmar_table_init()) {
4797 if (force_on)
4798 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004799 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004800 }
4801
Suresh Siddhac2c72862011-08-23 17:05:19 -07004802 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004803 if (force_on)
4804 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004805 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004806 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004807
Joerg Roedelec154bf2017-10-06 15:00:53 +02004808 up_write(&dmar_global_lock);
4809
4810 /*
4811 * The bus notifier takes the dmar_global_lock, so lockdep will
4812 * complain later when we register it under the lock.
4813 */
4814 dmar_register_bus_notifier();
4815
4816 down_write(&dmar_global_lock);
4817
Joerg Roedel161b28a2017-03-28 17:04:52 +02004818 if (no_iommu || dmar_disabled) {
4819 /*
Shaohua Libfd20f12017-04-26 09:18:35 -07004820 * We exit the function here to ensure IOMMU's remapping and
4821 * mempool aren't setup, which means that the IOMMU's PMRs
4822 * won't be disabled via the call to init_dmars(). So disable
4823 * it explicitly here. The PMRs were setup by tboot prior to
4824 * calling SENTER, but the kernel is expected to reset/tear
4825 * down the PMRs.
4826 */
4827 if (intel_iommu_tboot_noforce) {
4828 for_each_iommu(iommu, drhd)
4829 iommu_disable_protect_mem_regions(iommu);
4830 }
4831
4832 /*
Joerg Roedel161b28a2017-03-28 17:04:52 +02004833 * Make sure the IOMMUs are switched off, even when we
4834 * boot into a kexec kernel and the previous kernel left
4835 * them enabled
4836 */
4837 intel_disable_iommus();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004838 goto out_free_dmar;
Joerg Roedel161b28a2017-03-28 17:04:52 +02004839 }
Suresh Siddha2ae21012008-07-10 11:16:43 -07004840
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004841 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004842 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004843
4844 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004845 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004846
Joseph Cihula51a63e62011-03-21 11:04:24 -07004847 if (dmar_init_reserved_ranges()) {
4848 if (force_on)
4849 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004850 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004851 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004852
4853 init_no_remapping_devices();
4854
Joseph Cihulab7792602011-05-03 00:08:37 -07004855 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004856 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004857 if (force_on)
4858 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004859 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004860 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004861 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004862 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004863 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004864
Christoph Hellwig4fac8072017-12-24 13:57:08 +01004865#if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB)
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004866 swiotlb = 0;
4867#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004868 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004869
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004870 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004871
Joerg Roedel39ab9552017-02-01 16:56:46 +01004872 for_each_active_iommu(iommu, drhd) {
4873 iommu_device_sysfs_add(&iommu->iommu, NULL,
4874 intel_iommu_groups,
4875 "%s", iommu->name);
4876 iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
4877 iommu_device_register(&iommu->iommu);
4878 }
Alex Williamsona5459cf2014-06-12 16:12:31 -06004879
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004880 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004881 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004882 if (si_domain && !hw_pass_through)
4883 register_memory_notifier(&intel_iommu_memory_nb);
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004884 cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL,
4885 intel_iommu_cpu_dead);
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004886 intel_iommu_enabled = 1;
Sohil Mehtaee2636b2018-09-11 17:11:38 -07004887 intel_iommu_debugfs_init();
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004888
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004889 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004890
4891out_free_reserved_range:
4892 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004893out_free_dmar:
4894 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004895 up_write(&dmar_global_lock);
4896 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004897 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004898}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004899
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004900static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004901{
4902 struct intel_iommu *iommu = opaque;
4903
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004904 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004905 return 0;
4906}
4907
4908/*
4909 * NB - intel-iommu lacks any sort of reference counting for the users of
4910 * dependent devices. If multiple endpoints have intersecting dependent
4911 * devices, unbinding the driver from any one of them will possibly leave
4912 * the others unable to operate.
4913 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004914static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004915{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004916 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004917 return;
4918
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004919 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004920}
4921
Joerg Roedel127c7612015-07-23 17:44:46 +02004922static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004923{
Weidong Hanc7151a82008-12-08 22:51:37 +08004924 struct intel_iommu *iommu;
4925 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004926
Joerg Roedel55d94042015-07-22 16:50:40 +02004927 assert_spin_locked(&device_domain_lock);
4928
Joerg Roedelb608ac32015-07-21 18:19:08 +02004929 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004930 return;
4931
Joerg Roedel127c7612015-07-23 17:44:46 +02004932 iommu = info->iommu;
4933
4934 if (info->dev) {
4935 iommu_disable_dev_iotlb(info);
4936 domain_context_clear(iommu, info->dev);
Lu Baolua7fc93f2018-07-14 15:47:00 +08004937 intel_pasid_free_table(info->dev);
Joerg Roedel127c7612015-07-23 17:44:46 +02004938 }
4939
Joerg Roedelb608ac32015-07-21 18:19:08 +02004940 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004941
Joerg Roedeld160aca2015-07-22 11:52:53 +02004942 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004943 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004944 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004945
4946 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004947}
4948
Joerg Roedel55d94042015-07-22 16:50:40 +02004949static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4950 struct device *dev)
4951{
Joerg Roedel127c7612015-07-23 17:44:46 +02004952 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004953 unsigned long flags;
4954
Weidong Hanc7151a82008-12-08 22:51:37 +08004955 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004956 info = dev->archdata.iommu;
4957 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004958 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004959}
4960
4961static int md_domain_init(struct dmar_domain *domain, int guest_width)
4962{
4963 int adjust_width;
4964
Zhen Leiaa3ac942017-09-21 16:52:45 +01004965 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004966 domain_reserve_special_ranges(domain);
4967
4968 /* calculate AGAW */
4969 domain->gaw = guest_width;
4970 adjust_width = guestwidth_to_adjustwidth(guest_width);
4971 domain->agaw = width_to_agaw(adjust_width);
4972
Weidong Han5e98c4b2008-12-08 23:03:27 +08004973 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004974 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004975 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004976 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004977
4978 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004979 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004980 if (!domain->pgd)
4981 return -ENOMEM;
4982 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4983 return 0;
4984}
4985
Joerg Roedel00a77de2015-03-26 13:43:08 +01004986static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004987{
Joerg Roedel5d450802008-12-03 14:52:32 +01004988 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004989 struct iommu_domain *domain;
4990
4991 if (type != IOMMU_DOMAIN_UNMANAGED)
4992 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004993
Jiang Liuab8dfe22014-07-11 14:19:27 +08004994 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004995 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004996 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004997 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004998 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004999 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005000 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08005001 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01005002 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03005003 }
Allen Kay8140a952011-10-14 12:32:17 -07005004 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005005
Joerg Roedel00a77de2015-03-26 13:43:08 +01005006 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01005007 domain->geometry.aperture_start = 0;
5008 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
5009 domain->geometry.force_aperture = true;
5010
Joerg Roedel00a77de2015-03-26 13:43:08 +01005011 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03005012}
Kay, Allen M38717942008-09-09 18:37:29 +03005013
Joerg Roedel00a77de2015-03-26 13:43:08 +01005014static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03005015{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005016 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03005017}
Kay, Allen M38717942008-09-09 18:37:29 +03005018
Joerg Roedel4c5478c2008-12-03 14:58:24 +01005019static int intel_iommu_attach_device(struct iommu_domain *domain,
5020 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03005021{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005022 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005023 struct intel_iommu *iommu;
5024 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07005025 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03005026
Alex Williamsonc875d2c2014-07-03 09:57:02 -06005027 if (device_is_rmrr_locked(dev)) {
5028 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
5029 return -EPERM;
5030 }
5031
David Woodhouse7207d8f2014-03-09 16:31:06 -07005032 /* normally dev is not mapped */
5033 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005034 struct dmar_domain *old_domain;
5035
David Woodhouse1525a292014-03-06 16:19:30 +00005036 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005037 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02005038 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02005039 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02005040 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01005041
5042 if (!domain_type_is_vm_or_si(old_domain) &&
5043 list_empty(&old_domain->devices))
5044 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005045 }
5046 }
5047
David Woodhouse156baca2014-03-09 14:00:57 -07005048 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005049 if (!iommu)
5050 return -ENODEV;
5051
5052 /* check if this iommu agaw is sufficient for max mapped address */
5053 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01005054 if (addr_width > cap_mgaw(iommu->cap))
5055 addr_width = cap_mgaw(iommu->cap);
5056
5057 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005058 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005059 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01005060 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005061 return -EFAULT;
5062 }
Tom Lyona99c47a2010-05-17 08:20:45 +01005063 dmar_domain->gaw = addr_width;
5064
5065 /*
5066 * Knock out extra levels of page tables if necessary
5067 */
5068 while (iommu->agaw < dmar_domain->agaw) {
5069 struct dma_pte *pte;
5070
5071 pte = dmar_domain->pgd;
5072 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08005073 dmar_domain->pgd = (struct dma_pte *)
5074 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01005075 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01005076 }
5077 dmar_domain->agaw--;
5078 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005079
Joerg Roedel28ccce02015-07-21 14:45:31 +02005080 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005081}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005082
Joerg Roedel4c5478c2008-12-03 14:58:24 +01005083static void intel_iommu_detach_device(struct iommu_domain *domain,
5084 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03005085{
Joerg Roedele6de0f82015-07-22 16:30:36 +02005086 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03005087}
Kay, Allen M38717942008-09-09 18:37:29 +03005088
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005089static int intel_iommu_map(struct iommu_domain *domain,
5090 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005091 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03005092{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005093 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005094 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005095 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005096 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005097
Joerg Roedeldde57a22008-12-03 15:04:09 +01005098 if (iommu_prot & IOMMU_READ)
5099 prot |= DMA_PTE_READ;
5100 if (iommu_prot & IOMMU_WRITE)
5101 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08005102 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
5103 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005104
David Woodhouse163cc522009-06-28 00:51:17 +01005105 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005106 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005107 u64 end;
5108
5109 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01005110 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005111 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005112 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005113 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01005114 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005115 return -EFAULT;
5116 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01005117 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005118 }
David Woodhousead051222009-06-28 14:22:28 +01005119 /* Round up size to next multiple of PAGE_SIZE, if it and
5120 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01005121 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01005122 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
5123 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005124 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03005125}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005126
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005127static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00005128 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005129{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005130 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00005131 struct page *freelist = NULL;
David Woodhouseea8ea462014-03-05 17:09:32 +00005132 unsigned long start_pfn, last_pfn;
5133 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02005134 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01005135
David Woodhouse5cf0a762014-03-19 16:07:49 +00005136 /* Cope with horrid API which requires us to unmap more than the
5137 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02005138 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00005139
5140 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
5141 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
5142
David Woodhouseea8ea462014-03-05 17:09:32 +00005143 start_pfn = iova >> VTD_PAGE_SHIFT;
5144 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
5145
5146 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
5147
5148 npages = last_pfn - start_pfn + 1;
5149
Shaokun Zhangf746a022018-03-22 18:18:06 +08005150 for_each_domain_iommu(iommu_id, dmar_domain)
Joerg Roedel42e8c182015-07-21 15:50:02 +02005151 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
5152 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00005153
5154 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005155
David Woodhouse163cc522009-06-28 00:51:17 +01005156 if (dmar_domain->max_addr == iova + size)
5157 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005158
David Woodhouse5cf0a762014-03-19 16:07:49 +00005159 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005160}
Kay, Allen M38717942008-09-09 18:37:29 +03005161
Joerg Roedeld14d6572008-12-03 15:06:57 +01005162static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05305163 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03005164{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005165 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03005166 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00005167 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005168 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03005169
David Woodhouse5cf0a762014-03-19 16:07:49 +00005170 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03005171 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005172 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03005173
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005174 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03005175}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005176
Joerg Roedel5d587b82014-09-05 10:50:45 +02005177static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005178{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005179 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005180 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04005181 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005182 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005183
Joerg Roedel5d587b82014-09-05 10:50:45 +02005184 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005185}
5186
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005187static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005188{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005189 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005190 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07005191 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04005192
Alex Williamsona5459cf2014-06-12 16:12:31 -06005193 iommu = device_to_iommu(dev, &bus, &devfn);
5194 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005195 return -ENODEV;
5196
Joerg Roedele3d10af2017-02-01 17:23:22 +01005197 iommu_device_link(&iommu->iommu, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005198
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005199 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06005200
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005201 if (IS_ERR(group))
5202 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005203
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005204 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005205 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005206}
5207
5208static void intel_iommu_remove_device(struct device *dev)
5209{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005210 struct intel_iommu *iommu;
5211 u8 bus, devfn;
5212
5213 iommu = device_to_iommu(dev, &bus, &devfn);
5214 if (!iommu)
5215 return;
5216
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005217 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06005218
Joerg Roedele3d10af2017-02-01 17:23:22 +01005219 iommu_device_unlink(&iommu->iommu, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005220}
5221
Eric Auger0659b8d2017-01-19 20:57:53 +00005222static void intel_iommu_get_resv_regions(struct device *device,
5223 struct list_head *head)
5224{
5225 struct iommu_resv_region *reg;
5226 struct dmar_rmrr_unit *rmrr;
5227 struct device *i_dev;
5228 int i;
5229
5230 rcu_read_lock();
5231 for_each_rmrr_units(rmrr) {
5232 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
5233 i, i_dev) {
5234 if (i_dev != device)
5235 continue;
5236
5237 list_add_tail(&rmrr->resv->list, head);
5238 }
5239 }
5240 rcu_read_unlock();
5241
5242 reg = iommu_alloc_resv_region(IOAPIC_RANGE_START,
5243 IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00005244 0, IOMMU_RESV_MSI);
Eric Auger0659b8d2017-01-19 20:57:53 +00005245 if (!reg)
5246 return;
5247 list_add_tail(&reg->list, head);
5248}
5249
5250static void intel_iommu_put_resv_regions(struct device *dev,
5251 struct list_head *head)
5252{
5253 struct iommu_resv_region *entry, *next;
5254
5255 list_for_each_entry_safe(entry, next, head, list) {
5256 if (entry->type == IOMMU_RESV_RESERVED)
5257 kfree(entry);
5258 }
Kay, Allen M38717942008-09-09 18:37:29 +03005259}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005260
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005261#ifdef CONFIG_INTEL_IOMMU_SVM
Jacob Pan65ca7f52016-12-06 10:14:23 -08005262#define MAX_NR_PASID_BITS (20)
Lu Baolu4774cc52018-07-14 15:47:01 +08005263static inline unsigned long intel_iommu_get_pts(struct device *dev)
Jacob Pan65ca7f52016-12-06 10:14:23 -08005264{
Lu Baolu4774cc52018-07-14 15:47:01 +08005265 int pts, max_pasid;
5266
5267 max_pasid = intel_pasid_get_dev_max_id(dev);
5268 pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS);
5269 if (pts < 5)
Jacob Pan65ca7f52016-12-06 10:14:23 -08005270 return 0;
5271
Lu Baolu4774cc52018-07-14 15:47:01 +08005272 return pts - 5;
Jacob Pan65ca7f52016-12-06 10:14:23 -08005273}
5274
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005275int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
5276{
5277 struct device_domain_info *info;
5278 struct context_entry *context;
5279 struct dmar_domain *domain;
5280 unsigned long flags;
5281 u64 ctx_lo;
5282 int ret;
5283
5284 domain = get_valid_domain_for_dev(sdev->dev);
5285 if (!domain)
5286 return -EINVAL;
5287
5288 spin_lock_irqsave(&device_domain_lock, flags);
5289 spin_lock(&iommu->lock);
5290
5291 ret = -EINVAL;
5292 info = sdev->dev->archdata.iommu;
5293 if (!info || !info->pasid_supported)
5294 goto out;
5295
5296 context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
5297 if (WARN_ON(!context))
5298 goto out;
5299
5300 ctx_lo = context[0].lo;
5301
5302 sdev->did = domain->iommu_did[iommu->seq_id];
5303 sdev->sid = PCI_DEVID(info->bus, info->devfn);
5304
5305 if (!(ctx_lo & CONTEXT_PASIDE)) {
Ashok Raj11b93eb2017-08-08 13:29:28 -07005306 if (iommu->pasid_state_table)
5307 context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
Lu Baolu4774cc52018-07-14 15:47:01 +08005308 context[1].lo = (u64)virt_to_phys(info->pasid_table->table) |
5309 intel_iommu_get_pts(sdev->dev);
Jacob Pan65ca7f52016-12-06 10:14:23 -08005310
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005311 wmb();
5312 /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
5313 * extended to permit requests-with-PASID if the PASIDE bit
5314 * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
5315 * however, the PASIDE bit is ignored and requests-with-PASID
5316 * are unconditionally blocked. Which makes less sense.
5317 * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
5318 * "guest mode" translation types depending on whether ATS
5319 * is available or not. Annoyingly, we can't use the new
5320 * modes *unless* PASIDE is set. */
5321 if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
5322 ctx_lo &= ~CONTEXT_TT_MASK;
5323 if (info->ats_supported)
5324 ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
5325 else
5326 ctx_lo |= CONTEXT_TT_PT_PASID << 2;
5327 }
5328 ctx_lo |= CONTEXT_PASIDE;
David Woodhouse907fea32015-10-13 14:11:13 +01005329 if (iommu->pasid_state_table)
5330 ctx_lo |= CONTEXT_DINVE;
David Woodhousea222a7f2015-10-07 23:35:18 +01005331 if (info->pri_supported)
5332 ctx_lo |= CONTEXT_PRS;
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005333 context[0].lo = ctx_lo;
5334 wmb();
5335 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
5336 DMA_CCMD_MASK_NOBIT,
5337 DMA_CCMD_DEVICE_INVL);
5338 }
5339
5340 /* Enable PASID support in the device, if it wasn't already */
5341 if (!info->pasid_enabled)
5342 iommu_enable_dev_iotlb(info);
5343
5344 if (info->ats_enabled) {
5345 sdev->dev_iotlb = 1;
5346 sdev->qdep = info->ats_qdep;
5347 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
5348 sdev->qdep = 0;
5349 }
5350 ret = 0;
5351
5352 out:
5353 spin_unlock(&iommu->lock);
5354 spin_unlock_irqrestore(&device_domain_lock, flags);
5355
5356 return ret;
5357}
5358
5359struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5360{
5361 struct intel_iommu *iommu;
5362 u8 bus, devfn;
5363
5364 if (iommu_dummy(dev)) {
5365 dev_warn(dev,
5366 "No IOMMU translation for device; cannot enable SVM\n");
5367 return NULL;
5368 }
5369
5370 iommu = device_to_iommu(dev, &bus, &devfn);
5371 if ((!iommu)) {
Sudeep Duttb9997e32015-10-18 20:54:37 -07005372 dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005373 return NULL;
5374 }
5375
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005376 return iommu;
5377}
5378#endif /* CONFIG_INTEL_IOMMU_SVM */
5379
Joerg Roedelb0119e82017-02-01 13:23:08 +01005380const struct iommu_ops intel_iommu_ops = {
Eric Auger0659b8d2017-01-19 20:57:53 +00005381 .capable = intel_iommu_capable,
5382 .domain_alloc = intel_iommu_domain_alloc,
5383 .domain_free = intel_iommu_domain_free,
5384 .attach_dev = intel_iommu_attach_device,
5385 .detach_dev = intel_iommu_detach_device,
5386 .map = intel_iommu_map,
5387 .unmap = intel_iommu_unmap,
Eric Auger0659b8d2017-01-19 20:57:53 +00005388 .iova_to_phys = intel_iommu_iova_to_phys,
5389 .add_device = intel_iommu_add_device,
5390 .remove_device = intel_iommu_remove_device,
5391 .get_resv_regions = intel_iommu_get_resv_regions,
5392 .put_resv_regions = intel_iommu_put_resv_regions,
5393 .device_group = pci_device_group,
5394 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005395};
David Woodhouse9af88142009-02-13 23:18:03 +00005396
Daniel Vetter94526182013-01-20 23:50:13 +01005397static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
5398{
5399 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005400 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01005401 dmar_map_gfx = 0;
5402}
5403
5404DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
5405DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
5406DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
5407DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
5408DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
5409DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
5410DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
5411
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005412static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00005413{
5414 /*
5415 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01005416 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00005417 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005418 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005419 rwbf_quirk = 1;
5420}
5421
5422DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005423DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5424DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5425DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5426DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5427DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5428DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005429
Adam Jacksoneecfd572010-08-25 21:17:34 +01005430#define GGC 0x52
5431#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5432#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5433#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5434#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5435#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5436#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5437#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5438#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5439
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005440static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005441{
5442 unsigned short ggc;
5443
Adam Jacksoneecfd572010-08-25 21:17:34 +01005444 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005445 return;
5446
Adam Jacksoneecfd572010-08-25 21:17:34 +01005447 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005448 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005449 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005450 } else if (dmar_map_gfx) {
5451 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005452 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005453 intel_iommu_strict = 1;
5454 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005455}
5456DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5457DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5458DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5459DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5460
David Woodhousee0fc7e02009-09-30 09:12:17 -07005461/* On Tylersburg chipsets, some BIOSes have been known to enable the
5462 ISOCH DMAR unit for the Azalia sound device, but not give it any
5463 TLB entries, which causes it to deadlock. Check for that. We do
5464 this in a function called from init_dmars(), instead of in a PCI
5465 quirk, because we don't want to print the obnoxious "BIOS broken"
5466 message if VT-d is actually disabled.
5467*/
5468static void __init check_tylersburg_isoch(void)
5469{
5470 struct pci_dev *pdev;
5471 uint32_t vtisochctrl;
5472
5473 /* If there's no Azalia in the system anyway, forget it. */
5474 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5475 if (!pdev)
5476 return;
5477 pci_dev_put(pdev);
5478
5479 /* System Management Registers. Might be hidden, in which case
5480 we can't do the sanity check. But that's OK, because the
5481 known-broken BIOSes _don't_ actually hide it, so far. */
5482 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5483 if (!pdev)
5484 return;
5485
5486 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5487 pci_dev_put(pdev);
5488 return;
5489 }
5490
5491 pci_dev_put(pdev);
5492
5493 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5494 if (vtisochctrl & 1)
5495 return;
5496
5497 /* Drop all bits other than the number of TLB entries */
5498 vtisochctrl &= 0x1c;
5499
5500 /* If we have the recommended number of TLB entries (16), fine. */
5501 if (vtisochctrl == 0x10)
5502 return;
5503
5504 /* Zero TLB entries? You get to ride the short bus to school. */
5505 if (!vtisochctrl) {
5506 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5507 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5508 dmi_get_system_info(DMI_BIOS_VENDOR),
5509 dmi_get_system_info(DMI_BIOS_VERSION),
5510 dmi_get_system_info(DMI_PRODUCT_VERSION));
5511 iommu_identity_mapping |= IDENTMAP_AZALIA;
5512 return;
5513 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005514
5515 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005516 vtisochctrl);
5517}