blob: 5552a1aaf5ea5e217dbefd95dcf7459c8a58b696 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
David Woodhouseea8ea462014-03-05 17:09:32 +00002 * Copyright © 2006-2014 Intel Corporation.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
David Woodhouseea8ea462014-03-05 17:09:32 +000013 * Authors: David Woodhouse <dwmw2@infradead.org>,
14 * Ashok Raj <ashok.raj@intel.com>,
15 * Shaohua Li <shaohua.li@intel.com>,
16 * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
17 * Fenghua Yu <fenghua.yu@intel.com>
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020018 * Joerg Roedel <jroedel@suse.de>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070019 */
20
Joerg Roedel9f10e5b2015-06-12 09:57:06 +020021#define pr_fmt(fmt) "DMAR: " fmt
22
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070023#include <linux/init.h>
24#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080025#include <linux/debugfs.h>
Paul Gortmaker54485c32011-10-29 10:26:25 -040026#include <linux/export.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
Jiang Liu75f05562014-02-19 14:07:37 +080035#include <linux/memory.h>
Omer Pelegaa473242016-04-20 11:33:02 +030036#include <linux/cpu.h>
mark gross5e0d2a62008-03-04 15:22:08 -080037#include <linux/timer.h>
Dan Williamsdfddb962015-10-09 18:16:46 -040038#include <linux/io.h>
Kay, Allen M38717942008-09-09 18:37:29 +030039#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010040#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030041#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010042#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070043#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100044#include <linux/dmi.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020045#include <linux/pci-ats.h>
Tejun Heo0ee332c2011-12-08 10:22:09 -080046#include <linux/memblock.h>
Akinobu Mita36746432014-06-04 16:06:51 -070047#include <linux/dma-contiguous.h>
Christoph Hellwigfec777c2018-03-19 11:38:15 +010048#include <linux/dma-direct.h>
Joerg Roedel091d42e2015-06-12 11:56:10 +020049#include <linux/crash_dump.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070050#include <asm/irq_remapping.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070051#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090052#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070053
Joerg Roedel078e1ee2012-09-26 12:44:43 +020054#include "irq_remapping.h"
Lu Baolu56283172018-07-14 15:46:54 +080055#include "intel-pasid.h"
Joerg Roedel078e1ee2012-09-26 12:44:43 +020056
Fenghua Yu5b6985c2008-10-16 18:02:32 -070057#define ROOT_SIZE VTD_PAGE_SIZE
58#define CONTEXT_SIZE VTD_PAGE_SIZE
59
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
David Woodhouse18436af2015-03-25 15:05:47 +000061#define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070062#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070063#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070064
65#define IOAPIC_RANGE_START (0xfee00000)
66#define IOAPIC_RANGE_END (0xfeefffff)
67#define IOVA_START_ADDR (0x1000)
68
Sohil Mehta5e3b4a12017-12-20 11:59:24 -080069#define DEFAULT_DOMAIN_ADDRESS_WIDTH 57
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070070
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070071#define MAX_AGAW_WIDTH 64
Jiang Liu5c645b32014-01-06 14:18:12 +080072#define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT)
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070073
David Woodhouse2ebe3152009-09-19 07:34:04 -070074#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
75#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
76
77/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
78 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
79#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
80 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
81#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070082
Robin Murphy1b722502015-01-12 17:51:15 +000083/* IO virtual address start page frame number */
84#define IOVA_START_PFN (1)
85
Mark McLoughlinf27be032008-11-20 15:49:43 +000086#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
mark gross5e0d2a62008-03-04 15:22:08 -080087
Andrew Mortondf08cdc2010-09-22 13:05:11 -070088/* page table handling */
89#define LEVEL_STRIDE (9)
90#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
91
Ohad Ben-Cohen6d1c56a2011-11-10 11:32:30 +020092/*
93 * This bitmap is used to advertise the page sizes our hardware support
94 * to the IOMMU core, which will then use this information to split
95 * physically contiguous memory regions it is mapping into page sizes
96 * that we support.
97 *
98 * Traditionally the IOMMU core just handed us the mappings directly,
99 * after making sure the size is an order of a 4KiB page and that the
100 * mapping has natural alignment.
101 *
102 * To retain this behavior, we currently advertise that we support
103 * all page sizes that are an order of 4KiB.
104 *
105 * If at some point we'd like to utilize the IOMMU core's new behavior,
106 * we could change this to advertise the real page sizes we support.
107 */
108#define INTEL_IOMMU_PGSIZES (~0xFFFUL)
109
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700110static inline int agaw_to_level(int agaw)
111{
112 return agaw + 2;
113}
114
115static inline int agaw_to_width(int agaw)
116{
Jiang Liu5c645b32014-01-06 14:18:12 +0800117 return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700118}
119
120static inline int width_to_agaw(int width)
121{
Jiang Liu5c645b32014-01-06 14:18:12 +0800122 return DIV_ROUND_UP(width - 30, LEVEL_STRIDE);
Andrew Mortondf08cdc2010-09-22 13:05:11 -0700123}
124
125static inline unsigned int level_to_offset_bits(int level)
126{
127 return (level - 1) * LEVEL_STRIDE;
128}
129
130static inline int pfn_level_offset(unsigned long pfn, int level)
131{
132 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
133}
134
135static inline unsigned long level_mask(int level)
136{
137 return -1UL << level_to_offset_bits(level);
138}
139
140static inline unsigned long level_size(int level)
141{
142 return 1UL << level_to_offset_bits(level);
143}
144
145static inline unsigned long align_to_level(unsigned long pfn, int level)
146{
147 return (pfn + level_size(level) - 1) & level_mask(level);
148}
David Woodhousefd18de52009-05-10 23:57:41 +0100149
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100150static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
151{
Jiang Liu5c645b32014-01-06 14:18:12 +0800152 return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100153}
154
David Woodhousedd4e8312009-06-27 16:21:20 +0100155/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
156 are never going to work. */
157static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
158{
159 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
160}
161
162static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
163{
164 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
165}
166static inline unsigned long page_to_dma_pfn(struct page *pg)
167{
168 return mm_to_dma_pfn(page_to_pfn(pg));
169}
170static inline unsigned long virt_to_dma_pfn(void *p)
171{
172 return page_to_dma_pfn(virt_to_page(p));
173}
174
Weidong Hand9630fe2008-12-08 11:06:32 +0800175/* global iommu list, set NULL for ignored DMAR units */
176static struct intel_iommu **g_iommus;
177
David Woodhousee0fc7e02009-09-30 09:12:17 -0700178static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000179static int rwbf_quirk;
180
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000181/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700182 * set to 1 to panic kernel if can't successfully enable VT-d
183 * (used when kernel is launched w/ TXT)
184 */
185static int force_on = 0;
Shaohua Libfd20f12017-04-26 09:18:35 -0700186int intel_iommu_tboot_noforce;
Joseph Cihulab7792602011-05-03 00:08:37 -0700187
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000188#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000189
Joerg Roedel091d42e2015-06-12 11:56:10 +0200190/*
191 * Take a root_entry and return the Lower Context Table Pointer (LCTP)
192 * if marked present.
193 */
194static phys_addr_t root_entry_lctp(struct root_entry *re)
195{
196 if (!(re->lo & 1))
197 return 0;
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000198
Joerg Roedel091d42e2015-06-12 11:56:10 +0200199 return re->lo & VTD_PAGE_MASK;
200}
201
202/*
203 * Take a root_entry and return the Upper Context Table Pointer (UCTP)
204 * if marked present.
205 */
206static phys_addr_t root_entry_uctp(struct root_entry *re)
207{
208 if (!(re->hi & 1))
209 return 0;
210
211 return re->hi & VTD_PAGE_MASK;
212}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000213
Joerg Roedelcf484d02015-06-12 12:21:46 +0200214static inline void context_clear_pasid_enable(struct context_entry *context)
215{
216 context->lo &= ~(1ULL << 11);
217}
218
219static inline bool context_pasid_enabled(struct context_entry *context)
220{
221 return !!(context->lo & (1ULL << 11));
222}
223
224static inline void context_set_copied(struct context_entry *context)
225{
226 context->hi |= (1ull << 3);
227}
228
229static inline bool context_copied(struct context_entry *context)
230{
231 return !!(context->hi & (1ULL << 3));
232}
233
234static inline bool __context_present(struct context_entry *context)
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000235{
236 return (context->lo & 1);
237}
Joerg Roedelcf484d02015-06-12 12:21:46 +0200238
Sohil Mehta26b86092018-09-11 17:11:36 -0700239bool context_present(struct context_entry *context)
Joerg Roedelcf484d02015-06-12 12:21:46 +0200240{
241 return context_pasid_enabled(context) ?
242 __context_present(context) :
243 __context_present(context) && !context_copied(context);
244}
245
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000246static inline void context_set_present(struct context_entry *context)
247{
248 context->lo |= 1;
249}
250
251static inline void context_set_fault_enable(struct context_entry *context)
252{
253 context->lo &= (((u64)-1) << 2) | 1;
254}
255
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000256static inline void context_set_translation_type(struct context_entry *context,
257 unsigned long value)
258{
259 context->lo &= (((u64)-1) << 4) | 3;
260 context->lo |= (value & 3) << 2;
261}
262
263static inline void context_set_address_root(struct context_entry *context,
264 unsigned long value)
265{
Li, Zhen-Hua1a2262f2014-11-05 15:30:19 +0800266 context->lo &= ~VTD_PAGE_MASK;
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000267 context->lo |= value & VTD_PAGE_MASK;
268}
269
270static inline void context_set_address_width(struct context_entry *context,
271 unsigned long value)
272{
273 context->hi |= value & 7;
274}
275
276static inline void context_set_domain_id(struct context_entry *context,
277 unsigned long value)
278{
279 context->hi |= (value & ((1 << 16) - 1)) << 8;
280}
281
Joerg Roedeldbcd8612015-06-12 12:02:09 +0200282static inline int context_domain_id(struct context_entry *c)
283{
284 return((c->hi >> 8) & 0xffff);
285}
286
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000287static inline void context_clear_entry(struct context_entry *context)
288{
289 context->lo = 0;
290 context->hi = 0;
291}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000292
Mark McLoughlin622ba122008-11-20 15:49:46 +0000293/*
294 * 0: readable
295 * 1: writable
296 * 2-6: reserved
297 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800298 * 8-10: available
299 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000300 * 12-63: Host physcial address
301 */
302struct dma_pte {
303 u64 val;
304};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000305
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000306static inline void dma_clear_pte(struct dma_pte *pte)
307{
308 pte->val = 0;
309}
310
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000311static inline u64 dma_pte_addr(struct dma_pte *pte)
312{
David Woodhousec85994e2009-07-01 19:21:24 +0100313#ifdef CONFIG_64BIT
314 return pte->val & VTD_PAGE_MASK;
315#else
316 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100317 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100318#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000319}
320
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000321static inline bool dma_pte_present(struct dma_pte *pte)
322{
323 return (pte->val & 3) != 0;
324}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000325
Allen Kay4399c8b2011-10-14 12:32:46 -0700326static inline bool dma_pte_superpage(struct dma_pte *pte)
327{
Joerg Roedelc3c75eb2014-07-04 11:19:10 +0200328 return (pte->val & DMA_PTE_LARGE_PAGE);
Allen Kay4399c8b2011-10-14 12:32:46 -0700329}
330
David Woodhouse75e6bf92009-07-02 11:21:16 +0100331static inline int first_pte_in_page(struct dma_pte *pte)
332{
333 return !((unsigned long)pte & ~VTD_PAGE_MASK);
334}
335
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700336/*
337 * This domain is a statically identity mapping domain.
338 * 1. This domain creats a static 1:1 mapping to all usable memory.
339 * 2. It maps to each iommu if successful.
340 * 3. Each iommu mapps to this domain if successful.
341 */
David Woodhouse19943b02009-08-04 16:19:20 +0100342static struct dmar_domain *si_domain;
343static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700344
Joerg Roedel28ccce02015-07-21 14:45:31 +0200345/*
346 * Domain represents a virtual machine, more than one devices
Weidong Han1ce28fe2008-12-08 16:35:39 +0800347 * across iommus may be owned in one domain, e.g. kvm guest.
348 */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800349#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0)
Weidong Han1ce28fe2008-12-08 16:35:39 +0800350
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700351/* si_domain contains mulitple devices */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800352#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700353
Joerg Roedel29a27712015-07-21 17:17:12 +0200354#define for_each_domain_iommu(idx, domain) \
355 for (idx = 0; idx < g_num_of_iommus; idx++) \
356 if (domain->iommu_refcnt[idx])
357
Jiang Liub94e4112014-02-19 14:07:25 +0800358struct dmar_rmrr_unit {
359 struct list_head list; /* list of rmrr units */
360 struct acpi_dmar_header *hdr; /* ACPI header */
361 u64 base_address; /* reserved base address*/
362 u64 end_address; /* reserved end address */
David Woodhouse832bd852014-03-07 15:08:36 +0000363 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800364 int devices_cnt; /* target device count */
Eric Auger0659b8d2017-01-19 20:57:53 +0000365 struct iommu_resv_region *resv; /* reserved region handle */
Jiang Liub94e4112014-02-19 14:07:25 +0800366};
367
368struct dmar_atsr_unit {
369 struct list_head list; /* list of ATSR units */
370 struct acpi_dmar_header *hdr; /* ACPI header */
David Woodhouse832bd852014-03-07 15:08:36 +0000371 struct dmar_dev_scope *devices; /* target devices */
Jiang Liub94e4112014-02-19 14:07:25 +0800372 int devices_cnt; /* target device count */
373 u8 include_all:1; /* include all ports */
374};
375
376static LIST_HEAD(dmar_atsr_units);
377static LIST_HEAD(dmar_rmrr_units);
378
379#define for_each_rmrr_units(rmrr) \
380 list_for_each_entry(rmrr, &dmar_rmrr_units, list)
381
mark gross5e0d2a62008-03-04 15:22:08 -0800382/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800383static int g_num_of_iommus;
384
Jiang Liu92d03cc2014-02-19 14:07:28 +0800385static void domain_exit(struct dmar_domain *domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700386static void domain_remove_dev_info(struct dmar_domain *domain);
Joerg Roedele6de0f82015-07-22 16:30:36 +0200387static void dmar_remove_one_dev_info(struct dmar_domain *domain,
388 struct device *dev);
Joerg Roedel127c7612015-07-23 17:44:46 +0200389static void __dmar_remove_one_dev_info(struct device_domain_info *info);
Joerg Roedel2452d9d2015-07-23 16:20:14 +0200390static void domain_context_clear(struct intel_iommu *iommu,
391 struct device *dev);
Jiang Liu2a46ddf2014-07-11 14:19:30 +0800392static int domain_detach_iommu(struct dmar_domain *domain,
393 struct intel_iommu *iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700394
Suresh Siddhad3f13812011-08-23 17:05:25 -0700395#ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800396int dmar_disabled = 0;
397#else
398int dmar_disabled = 1;
Suresh Siddhad3f13812011-08-23 17:05:25 -0700399#endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800400
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -0200401int intel_iommu_enabled = 0;
402EXPORT_SYMBOL_GPL(intel_iommu_enabled);
403
David Woodhouse2d9e6672010-06-15 10:57:57 +0100404static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700405static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800406static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100407static int intel_iommu_superpage = 1;
Lu Baolu765b6a92018-12-10 09:58:55 +0800408static int intel_iommu_sm = 1;
David Woodhouseae853dd2015-09-09 11:58:59 +0100409static int iommu_identity_mapping;
David Woodhousec83b2f22015-06-12 10:15:49 +0100410
David Woodhouseae853dd2015-09-09 11:58:59 +0100411#define IDENTMAP_ALL 1
412#define IDENTMAP_GFX 2
413#define IDENTMAP_AZALIA 4
David Woodhousec83b2f22015-06-12 10:15:49 +0100414
Lu Baolu765b6a92018-12-10 09:58:55 +0800415#define sm_supported(iommu) (intel_iommu_sm && ecap_smts((iommu)->ecap))
416#define pasid_supported(iommu) (sm_supported(iommu) && \
417 ecap_pasid((iommu)->ecap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700418
David Woodhousec0771df2011-10-14 20:59:46 +0100419int intel_iommu_gfx_mapped;
420EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
421
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700422#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
423static DEFINE_SPINLOCK(device_domain_lock);
424static LIST_HEAD(device_domain_list);
425
Lu Baolu85319dc2018-07-14 15:46:58 +0800426/*
427 * Iterate over elements in device_domain_list and call the specified
Lu Baolu0bbeb012018-12-10 09:58:56 +0800428 * callback @fn against each element.
Lu Baolu85319dc2018-07-14 15:46:58 +0800429 */
430int for_each_device_domain(int (*fn)(struct device_domain_info *info,
431 void *data), void *data)
432{
433 int ret = 0;
Lu Baolu0bbeb012018-12-10 09:58:56 +0800434 unsigned long flags;
Lu Baolu85319dc2018-07-14 15:46:58 +0800435 struct device_domain_info *info;
436
Lu Baolu0bbeb012018-12-10 09:58:56 +0800437 spin_lock_irqsave(&device_domain_lock, flags);
Lu Baolu85319dc2018-07-14 15:46:58 +0800438 list_for_each_entry(info, &device_domain_list, global) {
439 ret = fn(info, data);
Lu Baolu0bbeb012018-12-10 09:58:56 +0800440 if (ret) {
441 spin_unlock_irqrestore(&device_domain_lock, flags);
Lu Baolu85319dc2018-07-14 15:46:58 +0800442 return ret;
Lu Baolu0bbeb012018-12-10 09:58:56 +0800443 }
Lu Baolu85319dc2018-07-14 15:46:58 +0800444 }
Lu Baolu0bbeb012018-12-10 09:58:56 +0800445 spin_unlock_irqrestore(&device_domain_lock, flags);
Lu Baolu85319dc2018-07-14 15:46:58 +0800446
447 return 0;
448}
449
Joerg Roedelb0119e82017-02-01 13:23:08 +0100450const struct iommu_ops intel_iommu_ops;
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100451
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200452static bool translation_pre_enabled(struct intel_iommu *iommu)
453{
454 return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED);
455}
456
Joerg Roedel091d42e2015-06-12 11:56:10 +0200457static void clear_translation_pre_enabled(struct intel_iommu *iommu)
458{
459 iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED;
460}
461
Joerg Roedel4158c2e2015-06-12 10:14:02 +0200462static void init_translation_status(struct intel_iommu *iommu)
463{
464 u32 gsts;
465
466 gsts = readl(iommu->reg + DMAR_GSTS_REG);
467 if (gsts & DMA_GSTS_TES)
468 iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED;
469}
470
Joerg Roedel00a77de2015-03-26 13:43:08 +0100471/* Convert generic 'struct iommu_domain to private struct dmar_domain */
472static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom)
473{
474 return container_of(dom, struct dmar_domain, domain);
475}
476
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700477static int __init intel_iommu_setup(char *str)
478{
479 if (!str)
480 return -EINVAL;
481 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800482 if (!strncmp(str, "on", 2)) {
483 dmar_disabled = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200484 pr_info("IOMMU enabled\n");
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800485 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700486 dmar_disabled = 1;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200487 pr_info("IOMMU disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700488 } else if (!strncmp(str, "igfx_off", 8)) {
489 dmar_map_gfx = 0;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200490 pr_info("Disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700491 } else if (!strncmp(str, "forcedac", 8)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200492 pr_info("Forcing DAC for PCI devices\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700493 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800494 } else if (!strncmp(str, "strict", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200495 pr_info("Disable batched IOTLB flush\n");
mark gross5e0d2a62008-03-04 15:22:08 -0800496 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100497 } else if (!strncmp(str, "sp_off", 6)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200498 pr_info("Disable supported super page\n");
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100499 intel_iommu_superpage = 0;
Lu Baolu765b6a92018-12-10 09:58:55 +0800500 } else if (!strncmp(str, "sm_off", 6)) {
501 pr_info("Intel-IOMMU: disable scalable mode support\n");
502 intel_iommu_sm = 0;
Shaohua Libfd20f12017-04-26 09:18:35 -0700503 } else if (!strncmp(str, "tboot_noforce", 13)) {
504 printk(KERN_INFO
505 "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n");
506 intel_iommu_tboot_noforce = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700507 }
508
509 str += strcspn(str, ",");
510 while (*str == ',')
511 str++;
512 }
513 return 0;
514}
515__setup("intel_iommu=", intel_iommu_setup);
516
517static struct kmem_cache *iommu_domain_cache;
518static struct kmem_cache *iommu_devinfo_cache;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700519
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200520static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did)
521{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200522 struct dmar_domain **domains;
523 int idx = did >> 8;
524
525 domains = iommu->domains[idx];
526 if (!domains)
527 return NULL;
528
529 return domains[did & 0xff];
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200530}
531
532static void set_iommu_domain(struct intel_iommu *iommu, u16 did,
533 struct dmar_domain *domain)
534{
Joerg Roedel8bf47812015-07-21 10:41:21 +0200535 struct dmar_domain **domains;
536 int idx = did >> 8;
537
538 if (!iommu->domains[idx]) {
539 size_t size = 256 * sizeof(struct dmar_domain *);
540 iommu->domains[idx] = kzalloc(size, GFP_ATOMIC);
541 }
542
543 domains = iommu->domains[idx];
544 if (WARN_ON(!domains))
545 return;
546 else
547 domains[did & 0xff] = domain;
Joerg Roedel9452d5b2015-07-21 10:00:56 +0200548}
549
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800550void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700551{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700552 struct page *page;
553 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700554
Suresh Siddha4c923d42009-10-02 11:01:24 -0700555 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
556 if (page)
557 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700558 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700559}
560
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800561void free_pgtable_page(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700562{
563 free_page((unsigned long)vaddr);
564}
565
566static inline void *alloc_domain_mem(void)
567{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900568 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700569}
570
Kay, Allen M38717942008-09-09 18:37:29 +0300571static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700572{
573 kmem_cache_free(iommu_domain_cache, vaddr);
574}
575
576static inline void * alloc_devinfo_mem(void)
577{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900578 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700579}
580
581static inline void free_devinfo_mem(void *vaddr)
582{
583 kmem_cache_free(iommu_devinfo_cache, vaddr);
584}
585
Jiang Liuab8dfe22014-07-11 14:19:27 +0800586static inline int domain_type_is_vm(struct dmar_domain *domain)
587{
588 return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE;
589}
590
Joerg Roedel28ccce02015-07-21 14:45:31 +0200591static inline int domain_type_is_si(struct dmar_domain *domain)
592{
593 return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY;
594}
595
Jiang Liuab8dfe22014-07-11 14:19:27 +0800596static inline int domain_type_is_vm_or_si(struct dmar_domain *domain)
597{
598 return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE |
599 DOMAIN_FLAG_STATIC_IDENTITY);
600}
Weidong Han1b573682008-12-08 15:34:06 +0800601
Jiang Liu162d1b12014-07-11 14:19:35 +0800602static inline int domain_pfn_supported(struct dmar_domain *domain,
603 unsigned long pfn)
604{
605 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
606
607 return !(addr_width < BITS_PER_LONG && pfn >> addr_width);
608}
609
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700610static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800611{
612 unsigned long sagaw;
613 int agaw = -1;
614
615 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700616 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800617 agaw >= 0; agaw--) {
618 if (test_bit(agaw, &sagaw))
619 break;
620 }
621
622 return agaw;
623}
624
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700625/*
626 * Calculate max SAGAW for each iommu.
627 */
628int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
629{
630 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
631}
632
633/*
634 * calculate agaw for each iommu.
635 * "SAGAW" may be different across iommus, use a default agaw, and
636 * get a supported less agaw for iommus that don't support the default agaw.
637 */
638int iommu_calculate_agaw(struct intel_iommu *iommu)
639{
640 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
641}
642
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700643/* This functionin only returns single iommu in a domain */
Lu Baolu9ddbfb42018-07-14 15:46:57 +0800644struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
Weidong Han8c11e792008-12-08 15:29:22 +0800645{
646 int iommu_id;
647
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700648 /* si_domain and vm domain should not get here. */
Jiang Liuab8dfe22014-07-11 14:19:27 +0800649 BUG_ON(domain_type_is_vm_or_si(domain));
Joerg Roedel29a27712015-07-21 17:17:12 +0200650 for_each_domain_iommu(iommu_id, domain)
651 break;
652
Weidong Han8c11e792008-12-08 15:29:22 +0800653 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
654 return NULL;
655
656 return g_iommus[iommu_id];
657}
658
Weidong Han8e6040972008-12-08 15:49:06 +0800659static void domain_update_iommu_coherency(struct dmar_domain *domain)
660{
David Woodhoused0501962014-03-11 17:10:29 -0700661 struct dmar_drhd_unit *drhd;
662 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100663 bool found = false;
664 int i;
Weidong Han8e6040972008-12-08 15:49:06 +0800665
David Woodhoused0501962014-03-11 17:10:29 -0700666 domain->iommu_coherency = 1;
Weidong Han8e6040972008-12-08 15:49:06 +0800667
Joerg Roedel29a27712015-07-21 17:17:12 +0200668 for_each_domain_iommu(i, domain) {
Quentin Lambert2f119c72015-02-06 10:59:53 +0100669 found = true;
Weidong Han8e6040972008-12-08 15:49:06 +0800670 if (!ecap_coherent(g_iommus[i]->ecap)) {
671 domain->iommu_coherency = 0;
672 break;
673 }
Weidong Han8e6040972008-12-08 15:49:06 +0800674 }
David Woodhoused0501962014-03-11 17:10:29 -0700675 if (found)
676 return;
677
678 /* No hardware attached; use lowest common denominator */
679 rcu_read_lock();
680 for_each_active_iommu(iommu, drhd) {
681 if (!ecap_coherent(iommu->ecap)) {
682 domain->iommu_coherency = 0;
683 break;
684 }
685 }
686 rcu_read_unlock();
Weidong Han8e6040972008-12-08 15:49:06 +0800687}
688
Jiang Liu161f6932014-07-11 14:19:37 +0800689static int domain_update_iommu_snooping(struct intel_iommu *skip)
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100690{
Allen Kay8140a952011-10-14 12:32:17 -0700691 struct dmar_drhd_unit *drhd;
Jiang Liu161f6932014-07-11 14:19:37 +0800692 struct intel_iommu *iommu;
693 int ret = 1;
694
695 rcu_read_lock();
696 for_each_active_iommu(iommu, drhd) {
697 if (iommu != skip) {
698 if (!ecap_sc_support(iommu->ecap)) {
699 ret = 0;
700 break;
701 }
702 }
703 }
704 rcu_read_unlock();
705
706 return ret;
707}
708
709static int domain_update_iommu_superpage(struct intel_iommu *skip)
710{
711 struct dmar_drhd_unit *drhd;
712 struct intel_iommu *iommu;
Allen Kay8140a952011-10-14 12:32:17 -0700713 int mask = 0xf;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100714
715 if (!intel_iommu_superpage) {
Jiang Liu161f6932014-07-11 14:19:37 +0800716 return 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100717 }
718
Allen Kay8140a952011-10-14 12:32:17 -0700719 /* set iommu_superpage to the smallest common denominator */
Jiang Liu0e2426122014-02-19 14:07:34 +0800720 rcu_read_lock();
Allen Kay8140a952011-10-14 12:32:17 -0700721 for_each_active_iommu(iommu, drhd) {
Jiang Liu161f6932014-07-11 14:19:37 +0800722 if (iommu != skip) {
723 mask &= cap_super_page_val(iommu->cap);
724 if (!mask)
725 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100726 }
727 }
Jiang Liu0e2426122014-02-19 14:07:34 +0800728 rcu_read_unlock();
729
Jiang Liu161f6932014-07-11 14:19:37 +0800730 return fls(mask);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100731}
732
Sheng Yang58c610b2009-03-18 15:33:05 +0800733/* Some capabilities may be different across iommus */
734static void domain_update_iommu_cap(struct dmar_domain *domain)
735{
736 domain_update_iommu_coherency(domain);
Jiang Liu161f6932014-07-11 14:19:37 +0800737 domain->iommu_snooping = domain_update_iommu_snooping(NULL);
738 domain->iommu_superpage = domain_update_iommu_superpage(NULL);
Sheng Yang58c610b2009-03-18 15:33:05 +0800739}
740
Sohil Mehta26b86092018-09-11 17:11:36 -0700741struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
742 u8 devfn, int alloc)
David Woodhouse03ecc322015-02-13 14:35:21 +0000743{
744 struct root_entry *root = &iommu->root_entry[bus];
745 struct context_entry *context;
746 u64 *entry;
747
Joerg Roedel4df4eab2015-08-25 10:54:28 +0200748 entry = &root->lo;
Lu Baolu765b6a92018-12-10 09:58:55 +0800749 if (sm_supported(iommu)) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000750 if (devfn >= 0x80) {
751 devfn -= 0x80;
752 entry = &root->hi;
753 }
754 devfn *= 2;
755 }
David Woodhouse03ecc322015-02-13 14:35:21 +0000756 if (*entry & 1)
757 context = phys_to_virt(*entry & VTD_PAGE_MASK);
758 else {
759 unsigned long phy_addr;
760 if (!alloc)
761 return NULL;
762
763 context = alloc_pgtable_page(iommu->node);
764 if (!context)
765 return NULL;
766
767 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
768 phy_addr = virt_to_phys((void *)context);
769 *entry = phy_addr | 1;
770 __iommu_flush_cache(iommu, entry, sizeof(*entry));
771 }
772 return &context[devfn];
773}
774
David Woodhouse4ed6a542015-05-11 14:59:20 +0100775static int iommu_dummy(struct device *dev)
776{
777 return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
778}
779
David Woodhouse156baca2014-03-09 14:00:57 -0700780static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800781{
782 struct dmar_drhd_unit *drhd = NULL;
Jiang Liub683b232014-02-19 14:07:32 +0800783 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -0700784 struct device *tmp;
785 struct pci_dev *ptmp, *pdev = NULL;
Yijing Wangaa4d0662014-05-26 20:14:06 +0800786 u16 segment = 0;
Weidong Hanc7151a82008-12-08 22:51:37 +0800787 int i;
788
David Woodhouse4ed6a542015-05-11 14:59:20 +0100789 if (iommu_dummy(dev))
790 return NULL;
791
David Woodhouse156baca2014-03-09 14:00:57 -0700792 if (dev_is_pci(dev)) {
Ashok Raj1c387182016-10-21 15:32:05 -0700793 struct pci_dev *pf_pdev;
794
David Woodhouse156baca2014-03-09 14:00:57 -0700795 pdev = to_pci_dev(dev);
Jon Derrick5823e332017-08-30 15:05:59 -0600796
797#ifdef CONFIG_X86
798 /* VMD child devices currently cannot be handled individually */
799 if (is_vmd(pdev->bus))
800 return NULL;
801#endif
802
Ashok Raj1c387182016-10-21 15:32:05 -0700803 /* VFs aren't listed in scope tables; we need to look up
804 * the PF instead to find the IOMMU. */
805 pf_pdev = pci_physfn(pdev);
806 dev = &pf_pdev->dev;
David Woodhouse156baca2014-03-09 14:00:57 -0700807 segment = pci_domain_nr(pdev->bus);
Rafael J. Wysockica5b74d2015-03-16 23:49:08 +0100808 } else if (has_acpi_companion(dev))
David Woodhouse156baca2014-03-09 14:00:57 -0700809 dev = &ACPI_COMPANION(dev)->dev;
810
Jiang Liu0e2426122014-02-19 14:07:34 +0800811 rcu_read_lock();
Jiang Liub683b232014-02-19 14:07:32 +0800812 for_each_active_iommu(iommu, drhd) {
David Woodhouse156baca2014-03-09 14:00:57 -0700813 if (pdev && segment != drhd->segment)
David Woodhouse276dbf992009-04-04 01:45:37 +0100814 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800815
Jiang Liub683b232014-02-19 14:07:32 +0800816 for_each_active_dev_scope(drhd->devices,
David Woodhouse156baca2014-03-09 14:00:57 -0700817 drhd->devices_cnt, i, tmp) {
818 if (tmp == dev) {
Ashok Raj1c387182016-10-21 15:32:05 -0700819 /* For a VF use its original BDF# not that of the PF
820 * which we used for the IOMMU lookup. Strictly speaking
821 * we could do this for all PCI devices; we only need to
822 * get the BDF# from the scope table for ACPI matches. */
Koos Vriezen5003ae12017-03-01 21:02:50 +0100823 if (pdev && pdev->is_virtfn)
Ashok Raj1c387182016-10-21 15:32:05 -0700824 goto got_pdev;
825
David Woodhouse156baca2014-03-09 14:00:57 -0700826 *bus = drhd->devices[i].bus;
827 *devfn = drhd->devices[i].devfn;
828 goto out;
829 }
830
831 if (!pdev || !dev_is_pci(tmp))
David Woodhouse832bd852014-03-07 15:08:36 +0000832 continue;
David Woodhouse156baca2014-03-09 14:00:57 -0700833
834 ptmp = to_pci_dev(tmp);
835 if (ptmp->subordinate &&
836 ptmp->subordinate->number <= pdev->bus->number &&
837 ptmp->subordinate->busn_res.end >= pdev->bus->number)
838 goto got_pdev;
David Woodhouse924b6232009-04-04 00:39:25 +0100839 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800840
David Woodhouse156baca2014-03-09 14:00:57 -0700841 if (pdev && drhd->include_all) {
842 got_pdev:
843 *bus = pdev->bus->number;
844 *devfn = pdev->devfn;
Jiang Liub683b232014-02-19 14:07:32 +0800845 goto out;
David Woodhouse156baca2014-03-09 14:00:57 -0700846 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800847 }
Jiang Liub683b232014-02-19 14:07:32 +0800848 iommu = NULL;
David Woodhouse156baca2014-03-09 14:00:57 -0700849 out:
Jiang Liu0e2426122014-02-19 14:07:34 +0800850 rcu_read_unlock();
Weidong Hanc7151a82008-12-08 22:51:37 +0800851
Jiang Liub683b232014-02-19 14:07:32 +0800852 return iommu;
Weidong Hanc7151a82008-12-08 22:51:37 +0800853}
854
Weidong Han5331fe62008-12-08 23:00:00 +0800855static void domain_flush_cache(struct dmar_domain *domain,
856 void *addr, int size)
857{
858 if (!domain->iommu_coherency)
859 clflush_cache_range(addr, size);
860}
861
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700862static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
863{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700864 struct context_entry *context;
David Woodhouse03ecc322015-02-13 14:35:21 +0000865 int ret = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700866 unsigned long flags;
867
868 spin_lock_irqsave(&iommu->lock, flags);
David Woodhouse03ecc322015-02-13 14:35:21 +0000869 context = iommu_context_addr(iommu, bus, devfn, 0);
870 if (context)
871 ret = context_present(context);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700872 spin_unlock_irqrestore(&iommu->lock, flags);
873 return ret;
874}
875
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700876static void free_context_table(struct intel_iommu *iommu)
877{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700878 int i;
879 unsigned long flags;
880 struct context_entry *context;
881
882 spin_lock_irqsave(&iommu->lock, flags);
883 if (!iommu->root_entry) {
884 goto out;
885 }
886 for (i = 0; i < ROOT_ENTRY_NR; i++) {
David Woodhouse03ecc322015-02-13 14:35:21 +0000887 context = iommu_context_addr(iommu, i, 0, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700888 if (context)
889 free_pgtable_page(context);
David Woodhouse03ecc322015-02-13 14:35:21 +0000890
Lu Baolu765b6a92018-12-10 09:58:55 +0800891 if (!sm_supported(iommu))
David Woodhouse03ecc322015-02-13 14:35:21 +0000892 continue;
893
894 context = iommu_context_addr(iommu, i, 0x80, 0);
895 if (context)
896 free_pgtable_page(context);
897
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700898 }
899 free_pgtable_page(iommu->root_entry);
900 iommu->root_entry = NULL;
901out:
902 spin_unlock_irqrestore(&iommu->lock, flags);
903}
904
David Woodhouseb026fd22009-06-28 10:37:25 +0100905static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
David Woodhouse5cf0a762014-03-19 16:07:49 +0000906 unsigned long pfn, int *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700907{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700908 struct dma_pte *parent, *pte = NULL;
909 int level = agaw_to_level(domain->agaw);
Allen Kay4399c8b2011-10-14 12:32:46 -0700910 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700911
912 BUG_ON(!domain->pgd);
Julian Stecklinaf9423602013-10-09 10:03:52 +0200913
Jiang Liu162d1b12014-07-11 14:19:35 +0800914 if (!domain_pfn_supported(domain, pfn))
Julian Stecklinaf9423602013-10-09 10:03:52 +0200915 /* Address beyond IOMMU's addressing capabilities. */
916 return NULL;
917
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700918 parent = domain->pgd;
919
David Woodhouse5cf0a762014-03-19 16:07:49 +0000920 while (1) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700921 void *tmp_page;
922
David Woodhouseb026fd22009-06-28 10:37:25 +0100923 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700924 pte = &parent[offset];
David Woodhouse5cf0a762014-03-19 16:07:49 +0000925 if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte)))
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100926 break;
David Woodhouse5cf0a762014-03-19 16:07:49 +0000927 if (level == *target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700928 break;
929
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000930 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100931 uint64_t pteval;
932
Suresh Siddha4c923d42009-10-02 11:01:24 -0700933 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700934
David Woodhouse206a73c2009-07-01 19:30:28 +0100935 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700936 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +0100937
David Woodhousec85994e2009-07-01 19:21:24 +0100938 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400939 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 +0800940 if (cmpxchg64(&pte->val, 0ULL, pteval))
David Woodhousec85994e2009-07-01 19:21:24 +0100941 /* Someone else set it while we were thinking; use theirs. */
942 free_pgtable_page(tmp_page);
Yijing Wangeffad4b2014-05-26 20:13:47 +0800943 else
David Woodhousec85994e2009-07-01 19:21:24 +0100944 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700945 }
David Woodhouse5cf0a762014-03-19 16:07:49 +0000946 if (level == 1)
947 break;
948
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000949 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700950 level--;
951 }
952
David Woodhouse5cf0a762014-03-19 16:07:49 +0000953 if (!*target_level)
954 *target_level = level;
955
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700956 return pte;
957}
958
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100959
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700960/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100961static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
962 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100963 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700964{
965 struct dma_pte *parent, *pte = NULL;
966 int total = agaw_to_level(domain->agaw);
967 int offset;
968
969 parent = domain->pgd;
970 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100971 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972 pte = &parent[offset];
973 if (level == total)
974 return pte;
975
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100976 if (!dma_pte_present(pte)) {
977 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700978 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100979 }
980
Yijing Wange16922a2014-05-20 20:37:51 +0800981 if (dma_pte_superpage(pte)) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100982 *large_page = total;
983 return pte;
984 }
985
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000986 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700987 total--;
988 }
989 return NULL;
990}
991
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700992/* clear last level pte, a tlb flush should be followed */
David Woodhouse5cf0a762014-03-19 16:07:49 +0000993static void dma_pte_clear_range(struct dmar_domain *domain,
David Woodhouse595badf52009-06-27 22:09:11 +0100994 unsigned long start_pfn,
995 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700996{
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100997 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +0100998 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700999
Jiang Liu162d1b12014-07-11 14:19:35 +08001000 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1001 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001002 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +01001003
David Woodhouse04b18e62009-06-27 19:15:01 +01001004 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -07001005 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001006 large_page = 1;
1007 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001008 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001009 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001010 continue;
1011 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001012 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +01001013 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001014 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +01001015 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001016 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
1017
David Woodhouse310a5ab2009-06-28 18:52:20 +01001018 domain_flush_cache(domain, first_pte,
1019 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -07001020
1021 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001022}
1023
Alex Williamson3269ee02013-06-15 10:27:19 -06001024static void dma_pte_free_level(struct dmar_domain *domain, int level,
David Dillowbc24c572017-06-28 19:42:23 -07001025 int retain_level, struct dma_pte *pte,
1026 unsigned long pfn, unsigned long start_pfn,
1027 unsigned long last_pfn)
Alex Williamson3269ee02013-06-15 10:27:19 -06001028{
1029 pfn = max(start_pfn, pfn);
1030 pte = &pte[pfn_level_offset(pfn, level)];
1031
1032 do {
1033 unsigned long level_pfn;
1034 struct dma_pte *level_pte;
1035
1036 if (!dma_pte_present(pte) || dma_pte_superpage(pte))
1037 goto next;
1038
David Dillowf7116e12017-01-30 19:11:11 -08001039 level_pfn = pfn & level_mask(level);
Alex Williamson3269ee02013-06-15 10:27:19 -06001040 level_pte = phys_to_virt(dma_pte_addr(pte));
1041
David Dillowbc24c572017-06-28 19:42:23 -07001042 if (level > 2) {
1043 dma_pte_free_level(domain, level - 1, retain_level,
1044 level_pte, level_pfn, start_pfn,
1045 last_pfn);
1046 }
Alex Williamson3269ee02013-06-15 10:27:19 -06001047
David Dillowbc24c572017-06-28 19:42:23 -07001048 /*
1049 * Free the page table if we're below the level we want to
1050 * retain and the range covers the entire table.
1051 */
1052 if (level < retain_level && !(start_pfn > level_pfn ||
Alex Williamson08336fd2014-01-21 15:48:18 -08001053 last_pfn < level_pfn + level_size(level) - 1)) {
Alex Williamson3269ee02013-06-15 10:27:19 -06001054 dma_clear_pte(pte);
1055 domain_flush_cache(domain, pte, sizeof(*pte));
1056 free_pgtable_page(level_pte);
1057 }
1058next:
1059 pfn += level_size(level);
1060 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1061}
1062
David Dillowbc24c572017-06-28 19:42:23 -07001063/*
1064 * clear last level (leaf) ptes and free page table pages below the
1065 * level we wish to keep intact.
1066 */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001067static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +01001068 unsigned long start_pfn,
David Dillowbc24c572017-06-28 19:42:23 -07001069 unsigned long last_pfn,
1070 int retain_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001071{
Jiang Liu162d1b12014-07-11 14:19:35 +08001072 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1073 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouse59c36282009-09-19 07:36:28 -07001074 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001075
Jiang Liud41a4ad2014-07-11 14:19:34 +08001076 dma_pte_clear_range(domain, start_pfn, last_pfn);
1077
David Woodhousef3a0a522009-06-30 03:40:07 +01001078 /* We don't need lock here; nobody else touches the iova range */
David Dillowbc24c572017-06-28 19:42:23 -07001079 dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level,
Alex Williamson3269ee02013-06-15 10:27:19 -06001080 domain->pgd, 0, start_pfn, last_pfn);
David Woodhouse6660c632009-06-27 22:41:00 +01001081
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001082 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +01001083 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001084 free_pgtable_page(domain->pgd);
1085 domain->pgd = NULL;
1086 }
1087}
1088
David Woodhouseea8ea462014-03-05 17:09:32 +00001089/* When a page at a given level is being unlinked from its parent, we don't
1090 need to *modify* it at all. All we need to do is make a list of all the
1091 pages which can be freed just as soon as we've flushed the IOTLB and we
1092 know the hardware page-walk will no longer touch them.
1093 The 'pte' argument is the *parent* PTE, pointing to the page that is to
1094 be freed. */
1095static struct page *dma_pte_list_pagetables(struct dmar_domain *domain,
1096 int level, struct dma_pte *pte,
1097 struct page *freelist)
1098{
1099 struct page *pg;
1100
1101 pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT);
1102 pg->freelist = freelist;
1103 freelist = pg;
1104
1105 if (level == 1)
1106 return freelist;
1107
Jiang Liuadeb2592014-04-09 10:20:39 +08001108 pte = page_address(pg);
1109 do {
David Woodhouseea8ea462014-03-05 17:09:32 +00001110 if (dma_pte_present(pte) && !dma_pte_superpage(pte))
1111 freelist = dma_pte_list_pagetables(domain, level - 1,
1112 pte, freelist);
Jiang Liuadeb2592014-04-09 10:20:39 +08001113 pte++;
1114 } while (!first_pte_in_page(pte));
David Woodhouseea8ea462014-03-05 17:09:32 +00001115
1116 return freelist;
1117}
1118
1119static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
1120 struct dma_pte *pte, unsigned long pfn,
1121 unsigned long start_pfn,
1122 unsigned long last_pfn,
1123 struct page *freelist)
1124{
1125 struct dma_pte *first_pte = NULL, *last_pte = NULL;
1126
1127 pfn = max(start_pfn, pfn);
1128 pte = &pte[pfn_level_offset(pfn, level)];
1129
1130 do {
1131 unsigned long level_pfn;
1132
1133 if (!dma_pte_present(pte))
1134 goto next;
1135
1136 level_pfn = pfn & level_mask(level);
1137
1138 /* If range covers entire pagetable, free it */
1139 if (start_pfn <= level_pfn &&
1140 last_pfn >= level_pfn + level_size(level) - 1) {
1141 /* These suborbinate page tables are going away entirely. Don't
1142 bother to clear them; we're just going to *free* them. */
1143 if (level > 1 && !dma_pte_superpage(pte))
1144 freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist);
1145
1146 dma_clear_pte(pte);
1147 if (!first_pte)
1148 first_pte = pte;
1149 last_pte = pte;
1150 } else if (level > 1) {
1151 /* Recurse down into a level that isn't *entirely* obsolete */
1152 freelist = dma_pte_clear_level(domain, level - 1,
1153 phys_to_virt(dma_pte_addr(pte)),
1154 level_pfn, start_pfn, last_pfn,
1155 freelist);
1156 }
1157next:
1158 pfn += level_size(level);
1159 } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
1160
1161 if (first_pte)
1162 domain_flush_cache(domain, first_pte,
1163 (void *)++last_pte - (void *)first_pte);
1164
1165 return freelist;
1166}
1167
1168/* We can't just free the pages because the IOMMU may still be walking
1169 the page tables, and may have cached the intermediate levels. The
1170 pages can only be freed after the IOTLB flush has been done. */
Joerg Roedelb6904202015-08-13 11:32:18 +02001171static struct page *domain_unmap(struct dmar_domain *domain,
1172 unsigned long start_pfn,
1173 unsigned long last_pfn)
David Woodhouseea8ea462014-03-05 17:09:32 +00001174{
David Woodhouseea8ea462014-03-05 17:09:32 +00001175 struct page *freelist = NULL;
1176
Jiang Liu162d1b12014-07-11 14:19:35 +08001177 BUG_ON(!domain_pfn_supported(domain, start_pfn));
1178 BUG_ON(!domain_pfn_supported(domain, last_pfn));
David Woodhouseea8ea462014-03-05 17:09:32 +00001179 BUG_ON(start_pfn > last_pfn);
1180
1181 /* we don't need lock here; nobody else touches the iova range */
1182 freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
1183 domain->pgd, 0, start_pfn, last_pfn, NULL);
1184
1185 /* free pgd */
1186 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
1187 struct page *pgd_page = virt_to_page(domain->pgd);
1188 pgd_page->freelist = freelist;
1189 freelist = pgd_page;
1190
1191 domain->pgd = NULL;
1192 }
1193
1194 return freelist;
1195}
1196
Joerg Roedelb6904202015-08-13 11:32:18 +02001197static void dma_free_pagelist(struct page *freelist)
David Woodhouseea8ea462014-03-05 17:09:32 +00001198{
1199 struct page *pg;
1200
1201 while ((pg = freelist)) {
1202 freelist = pg->freelist;
1203 free_pgtable_page(page_address(pg));
1204 }
1205}
1206
Joerg Roedel13cf0172017-08-11 11:40:10 +02001207static void iova_entry_free(unsigned long data)
1208{
1209 struct page *freelist = (struct page *)data;
1210
1211 dma_free_pagelist(freelist);
1212}
1213
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001214/* iommu handling */
1215static int iommu_alloc_root_entry(struct intel_iommu *iommu)
1216{
1217 struct root_entry *root;
1218 unsigned long flags;
1219
Suresh Siddha4c923d42009-10-02 11:01:24 -07001220 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Jiang Liuffebeb42014-11-09 22:48:02 +08001221 if (!root) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001222 pr_err("Allocating root entry for %s failed\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08001223 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001224 return -ENOMEM;
Jiang Liuffebeb42014-11-09 22:48:02 +08001225 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001226
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001227 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001228
1229 spin_lock_irqsave(&iommu->lock, flags);
1230 iommu->root_entry = root;
1231 spin_unlock_irqrestore(&iommu->lock, flags);
1232
1233 return 0;
1234}
1235
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001236static void iommu_set_root_entry(struct intel_iommu *iommu)
1237{
David Woodhouse03ecc322015-02-13 14:35:21 +00001238 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +01001239 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001240 unsigned long flag;
1241
David Woodhouse03ecc322015-02-13 14:35:21 +00001242 addr = virt_to_phys(iommu->root_entry);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001243
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001244 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse03ecc322015-02-13 14:35:21 +00001245 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001246
David Woodhousec416daa2009-05-10 20:30:58 +01001247 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001248
1249 /* Make sure hardware complete it */
1250 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001251 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001252
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001253 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001254}
1255
1256static void iommu_flush_write_buffer(struct intel_iommu *iommu)
1257{
1258 u32 val;
1259 unsigned long flag;
1260
David Woodhouse9af88142009-02-13 23:18:03 +00001261 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001262 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001263
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001264 raw_spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +01001265 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001266
1267 /* Make sure hardware complete it */
1268 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001269 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001270
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001271 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001272}
1273
1274/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001275static void __iommu_flush_context(struct intel_iommu *iommu,
1276 u16 did, u16 source_id, u8 function_mask,
1277 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001278{
1279 u64 val = 0;
1280 unsigned long flag;
1281
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001282 switch (type) {
1283 case DMA_CCMD_GLOBAL_INVL:
1284 val = DMA_CCMD_GLOBAL_INVL;
1285 break;
1286 case DMA_CCMD_DOMAIN_INVL:
1287 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
1288 break;
1289 case DMA_CCMD_DEVICE_INVL:
1290 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
1291 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
1292 break;
1293 default:
1294 BUG();
1295 }
1296 val |= DMA_CCMD_ICC;
1297
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001298 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001299 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
1300
1301 /* Make sure hardware complete it */
1302 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
1303 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
1304
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001305 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001306}
1307
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001308/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001309static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
1310 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001311{
1312 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1313 u64 val = 0, val_iva = 0;
1314 unsigned long flag;
1315
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001316 switch (type) {
1317 case DMA_TLB_GLOBAL_FLUSH:
1318 /* global flush doesn't need set IVA_REG */
1319 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1320 break;
1321 case DMA_TLB_DSI_FLUSH:
1322 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1323 break;
1324 case DMA_TLB_PSI_FLUSH:
1325 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
David Woodhouseea8ea462014-03-05 17:09:32 +00001326 /* IH bit is passed in as part of address */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001327 val_iva = size_order | addr;
1328 break;
1329 default:
1330 BUG();
1331 }
1332 /* Note: set drain read/write */
1333#if 0
1334 /*
1335 * This is probably to be super secure.. Looks like we can
1336 * ignore it without any impact.
1337 */
1338 if (cap_read_drain(iommu->cap))
1339 val |= DMA_TLB_READ_DRAIN;
1340#endif
1341 if (cap_write_drain(iommu->cap))
1342 val |= DMA_TLB_WRITE_DRAIN;
1343
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001344 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001345 /* Note: Only uses first TLB reg currently */
1346 if (val_iva)
1347 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1348 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1349
1350 /* Make sure hardware complete it */
1351 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1352 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1353
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001354 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001355
1356 /* check IOTLB invalidation granularity */
1357 if (DMA_TLB_IAIG(val) == 0)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001358 pr_err("Flush IOTLB failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001359 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001360 pr_debug("TLB flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001361 (unsigned long long)DMA_TLB_IIRG(type),
1362 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001363}
1364
David Woodhouse64ae8922014-03-09 12:52:30 -07001365static struct device_domain_info *
1366iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
1367 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001368{
Yu Zhao93a23a72009-05-18 13:51:37 +08001369 struct device_domain_info *info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001370
Joerg Roedel55d94042015-07-22 16:50:40 +02001371 assert_spin_locked(&device_domain_lock);
1372
Yu Zhao93a23a72009-05-18 13:51:37 +08001373 if (!iommu->qi)
1374 return NULL;
1375
Yu Zhao93a23a72009-05-18 13:51:37 +08001376 list_for_each_entry(info, &domain->devices, link)
Jiang Liuc3b497c2014-07-11 14:19:25 +08001377 if (info->iommu == iommu && info->bus == bus &&
1378 info->devfn == devfn) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001379 if (info->ats_supported && info->dev)
1380 return info;
Yu Zhao93a23a72009-05-18 13:51:37 +08001381 break;
1382 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001383
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001384 return NULL;
Yu Zhao93a23a72009-05-18 13:51:37 +08001385}
1386
Omer Peleg0824c592016-04-20 19:03:35 +03001387static void domain_update_iotlb(struct dmar_domain *domain)
1388{
1389 struct device_domain_info *info;
1390 bool has_iotlb_device = false;
1391
1392 assert_spin_locked(&device_domain_lock);
1393
1394 list_for_each_entry(info, &domain->devices, link) {
1395 struct pci_dev *pdev;
1396
1397 if (!info->dev || !dev_is_pci(info->dev))
1398 continue;
1399
1400 pdev = to_pci_dev(info->dev);
1401 if (pdev->ats_enabled) {
1402 has_iotlb_device = true;
1403 break;
1404 }
1405 }
1406
1407 domain->has_iotlb_device = has_iotlb_device;
1408}
1409
Yu Zhao93a23a72009-05-18 13:51:37 +08001410static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1411{
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001412 struct pci_dev *pdev;
1413
Omer Peleg0824c592016-04-20 19:03:35 +03001414 assert_spin_locked(&device_domain_lock);
1415
David Woodhouse0bcb3e22014-03-06 17:12:03 +00001416 if (!info || !dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001417 return;
1418
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001419 pdev = to_pci_dev(info->dev);
Jacob Pan1c48db42018-06-07 09:57:00 -07001420 /* For IOMMU that supports device IOTLB throttling (DIT), we assign
1421 * PFSID to the invalidation desc of a VF such that IOMMU HW can gauge
1422 * queue depth at PF level. If DIT is not set, PFSID will be treated as
1423 * reserved, which should be set to 0.
1424 */
1425 if (!ecap_dit(info->iommu->ecap))
1426 info->pfsid = 0;
1427 else {
1428 struct pci_dev *pf_pdev;
1429
1430 /* pdev will be returned if device is not a vf */
1431 pf_pdev = pci_physfn(pdev);
1432 info->pfsid = PCI_DEVID(pf_pdev->bus->number, pf_pdev->devfn);
1433 }
Bjorn Helgaasfb0cc3a2015-07-20 09:10:36 -05001434
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001435#ifdef CONFIG_INTEL_IOMMU_SVM
1436 /* The PCIe spec, in its wisdom, declares that the behaviour of
1437 the device if you enable PASID support after ATS support is
1438 undefined. So always enable PASID support on devices which
1439 have it, even if we can't yet know if we're ever going to
1440 use it. */
1441 if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1))
1442 info->pasid_enabled = 1;
1443
1444 if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
1445 info->pri_enabled = 1;
1446#endif
1447 if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
1448 info->ats_enabled = 1;
Omer Peleg0824c592016-04-20 19:03:35 +03001449 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001450 info->ats_qdep = pci_ats_queue_depth(pdev);
1451 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001452}
1453
1454static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1455{
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001456 struct pci_dev *pdev;
1457
Omer Peleg0824c592016-04-20 19:03:35 +03001458 assert_spin_locked(&device_domain_lock);
1459
Jeremy McNicollda972fb2016-01-14 21:33:06 -08001460 if (!dev_is_pci(info->dev))
Yu Zhao93a23a72009-05-18 13:51:37 +08001461 return;
1462
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001463 pdev = to_pci_dev(info->dev);
1464
1465 if (info->ats_enabled) {
1466 pci_disable_ats(pdev);
1467 info->ats_enabled = 0;
Omer Peleg0824c592016-04-20 19:03:35 +03001468 domain_update_iotlb(info->domain);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001469 }
1470#ifdef CONFIG_INTEL_IOMMU_SVM
1471 if (info->pri_enabled) {
1472 pci_disable_pri(pdev);
1473 info->pri_enabled = 0;
1474 }
1475 if (info->pasid_enabled) {
1476 pci_disable_pasid(pdev);
1477 info->pasid_enabled = 0;
1478 }
1479#endif
Yu Zhao93a23a72009-05-18 13:51:37 +08001480}
1481
1482static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1483 u64 addr, unsigned mask)
1484{
1485 u16 sid, qdep;
1486 unsigned long flags;
1487 struct device_domain_info *info;
1488
Omer Peleg0824c592016-04-20 19:03:35 +03001489 if (!domain->has_iotlb_device)
1490 return;
1491
Yu Zhao93a23a72009-05-18 13:51:37 +08001492 spin_lock_irqsave(&device_domain_lock, flags);
1493 list_for_each_entry(info, &domain->devices, link) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001494 if (!info->ats_enabled)
Yu Zhao93a23a72009-05-18 13:51:37 +08001495 continue;
1496
1497 sid = info->bus << 8 | info->devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01001498 qdep = info->ats_qdep;
Jacob Pan1c48db42018-06-07 09:57:00 -07001499 qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
1500 qdep, addr, mask);
Yu Zhao93a23a72009-05-18 13:51:37 +08001501 }
1502 spin_unlock_irqrestore(&device_domain_lock, flags);
1503}
1504
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001505static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
1506 struct dmar_domain *domain,
1507 unsigned long pfn, unsigned int pages,
1508 int ih, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001509{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001510 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001511 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02001512 u16 did = domain->iommu_did[iommu->seq_id];
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001513
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001514 BUG_ON(pages == 0);
1515
David Woodhouseea8ea462014-03-05 17:09:32 +00001516 if (ih)
1517 ih = 1 << 6;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001518 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001519 * Fallback to domain selective flush if no PSI support or the size is
1520 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001521 * PSI requires page size to be 2 ^ x, and the base address is naturally
1522 * aligned to the size
1523 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001524 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1525 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001526 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001527 else
David Woodhouseea8ea462014-03-05 17:09:32 +00001528 iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001529 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001530
1531 /*
Nadav Amit82653632010-04-01 13:24:40 +03001532 * In caching mode, changes of pages from non-present to present require
1533 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001534 */
Nadav Amit82653632010-04-01 13:24:40 +03001535 if (!cap_caching_mode(iommu->cap) || !map)
Peter Xu9d2e6502018-01-10 13:51:37 +08001536 iommu_flush_dev_iotlb(domain, addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001537}
1538
Peter Xueed91a02018-05-04 10:34:52 +08001539/* Notification for newly created mappings */
1540static inline void __mapping_notify_one(struct intel_iommu *iommu,
1541 struct dmar_domain *domain,
1542 unsigned long pfn, unsigned int pages)
1543{
1544 /* It's a non-present to present mapping. Only flush if caching mode */
1545 if (cap_caching_mode(iommu->cap))
1546 iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1);
1547 else
1548 iommu_flush_write_buffer(iommu);
1549}
1550
Joerg Roedel13cf0172017-08-11 11:40:10 +02001551static void iommu_flush_iova(struct iova_domain *iovad)
1552{
1553 struct dmar_domain *domain;
1554 int idx;
1555
1556 domain = container_of(iovad, struct dmar_domain, iovad);
1557
1558 for_each_domain_iommu(idx, domain) {
1559 struct intel_iommu *iommu = g_iommus[idx];
1560 u16 did = domain->iommu_did[iommu->seq_id];
1561
1562 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
1563
1564 if (!cap_caching_mode(iommu->cap))
1565 iommu_flush_dev_iotlb(get_iommu_domain(iommu, did),
1566 0, MAX_AGAW_PFN_WIDTH);
1567 }
1568}
1569
mark grossf8bab732008-02-08 04:18:38 -08001570static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1571{
1572 u32 pmen;
1573 unsigned long flags;
1574
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001575 raw_spin_lock_irqsave(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001576 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1577 pmen &= ~DMA_PMEN_EPM;
1578 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1579
1580 /* wait for the protected region status bit to clear */
1581 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1582 readl, !(pmen & DMA_PMEN_PRS), pmen);
1583
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001584 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
mark grossf8bab732008-02-08 04:18:38 -08001585}
1586
Jiang Liu2a41cce2014-07-11 14:19:33 +08001587static void iommu_enable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001588{
1589 u32 sts;
1590 unsigned long flags;
1591
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001592 raw_spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001593 iommu->gcmd |= DMA_GCMD_TE;
1594 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001595
1596 /* Make sure hardware complete it */
1597 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001598 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001599
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001600 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001601}
1602
Jiang Liu2a41cce2014-07-11 14:19:33 +08001603static void iommu_disable_translation(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001604{
1605 u32 sts;
1606 unsigned long flag;
1607
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001608 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001609 iommu->gcmd &= ~DMA_GCMD_TE;
1610 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1611
1612 /* Make sure hardware complete it */
1613 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001614 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001615
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02001616 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001617}
1618
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001619
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001620static int iommu_init_domains(struct intel_iommu *iommu)
1621{
Joerg Roedel8bf47812015-07-21 10:41:21 +02001622 u32 ndomains, nlongs;
1623 size_t size;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001624
1625 ndomains = cap_ndoms(iommu->cap);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001626 pr_debug("%s: Number of Domains supported <%d>\n",
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001627 iommu->name, ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001628 nlongs = BITS_TO_LONGS(ndomains);
1629
Donald Dutile94a91b502009-08-20 16:51:34 -04001630 spin_lock_init(&iommu->lock);
1631
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001632 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1633 if (!iommu->domain_ids) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001634 pr_err("%s: Allocating domain id array failed\n",
1635 iommu->name);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001636 return -ENOMEM;
1637 }
Joerg Roedel8bf47812015-07-21 10:41:21 +02001638
Wei Yang86f004c2016-05-21 02:41:51 +00001639 size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001640 iommu->domains = kzalloc(size, GFP_KERNEL);
1641
1642 if (iommu->domains) {
1643 size = 256 * sizeof(struct dmar_domain *);
1644 iommu->domains[0] = kzalloc(size, GFP_KERNEL);
1645 }
1646
1647 if (!iommu->domains || !iommu->domains[0]) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001648 pr_err("%s: Allocating domain array failed\n",
1649 iommu->name);
Jiang Liu852bdb02014-01-06 14:18:11 +08001650 kfree(iommu->domain_ids);
Joerg Roedel8bf47812015-07-21 10:41:21 +02001651 kfree(iommu->domains);
Jiang Liu852bdb02014-01-06 14:18:11 +08001652 iommu->domain_ids = NULL;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001653 iommu->domains = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001654 return -ENOMEM;
1655 }
1656
Joerg Roedel8bf47812015-07-21 10:41:21 +02001657
1658
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001659 /*
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001660 * If Caching mode is set, then invalid translations are tagged
1661 * with domain-id 0, hence we need to pre-allocate it. We also
1662 * use domain-id 0 as a marker for non-allocated domain-id, so
1663 * make sure it is not used for a real domain.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001664 */
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001665 set_bit(0, iommu->domain_ids);
1666
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001667 return 0;
1668}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001669
Jiang Liuffebeb42014-11-09 22:48:02 +08001670static void disable_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001671{
Joerg Roedel29a27712015-07-21 17:17:12 +02001672 struct device_domain_info *info, *tmp;
Joerg Roedel55d94042015-07-22 16:50:40 +02001673 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001674
Joerg Roedel29a27712015-07-21 17:17:12 +02001675 if (!iommu->domains || !iommu->domain_ids)
1676 return;
Jiang Liua4eaa862014-02-19 14:07:30 +08001677
Joerg Roedelbea64032016-11-08 15:08:26 +01001678again:
Joerg Roedel55d94042015-07-22 16:50:40 +02001679 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001680 list_for_each_entry_safe(info, tmp, &device_domain_list, global) {
1681 struct dmar_domain *domain;
1682
1683 if (info->iommu != iommu)
1684 continue;
1685
1686 if (!info->dev || !info->domain)
1687 continue;
1688
1689 domain = info->domain;
1690
Joerg Roedelbea64032016-11-08 15:08:26 +01001691 __dmar_remove_one_dev_info(info);
Joerg Roedel29a27712015-07-21 17:17:12 +02001692
Joerg Roedelbea64032016-11-08 15:08:26 +01001693 if (!domain_type_is_vm_or_si(domain)) {
1694 /*
1695 * The domain_exit() function can't be called under
1696 * device_domain_lock, as it takes this lock itself.
1697 * So release the lock here and re-run the loop
1698 * afterwards.
1699 */
1700 spin_unlock_irqrestore(&device_domain_lock, flags);
Joerg Roedel29a27712015-07-21 17:17:12 +02001701 domain_exit(domain);
Joerg Roedelbea64032016-11-08 15:08:26 +01001702 goto again;
1703 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001704 }
Joerg Roedel55d94042015-07-22 16:50:40 +02001705 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001706
1707 if (iommu->gcmd & DMA_GCMD_TE)
1708 iommu_disable_translation(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08001709}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710
Jiang Liuffebeb42014-11-09 22:48:02 +08001711static void free_dmar_iommu(struct intel_iommu *iommu)
1712{
1713 if ((iommu->domains) && (iommu->domain_ids)) {
Wei Yang86f004c2016-05-21 02:41:51 +00001714 int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8;
Joerg Roedel8bf47812015-07-21 10:41:21 +02001715 int i;
1716
1717 for (i = 0; i < elems; i++)
1718 kfree(iommu->domains[i]);
Jiang Liuffebeb42014-11-09 22:48:02 +08001719 kfree(iommu->domains);
1720 kfree(iommu->domain_ids);
1721 iommu->domains = NULL;
1722 iommu->domain_ids = NULL;
1723 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001724
Weidong Hand9630fe2008-12-08 11:06:32 +08001725 g_iommus[iommu->seq_id] = NULL;
1726
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001727 /* free context mapping */
1728 free_context_table(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00001729
1730#ifdef CONFIG_INTEL_IOMMU_SVM
Lu Baolu765b6a92018-12-10 09:58:55 +08001731 if (pasid_supported(iommu)) {
David Woodhousea222a7f2015-10-07 23:35:18 +01001732 if (ecap_prs(iommu->ecap))
1733 intel_svm_finish_prq(iommu);
Lu Baolud9737952018-07-14 15:47:02 +08001734 intel_svm_exit(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01001735 }
David Woodhouse8a94ade2015-03-24 14:54:56 +00001736#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001737}
1738
Jiang Liuab8dfe22014-07-11 14:19:27 +08001739static struct dmar_domain *alloc_domain(int flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001740{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001741 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001742
1743 domain = alloc_domain_mem();
1744 if (!domain)
1745 return NULL;
1746
Jiang Liuab8dfe22014-07-11 14:19:27 +08001747 memset(domain, 0, sizeof(*domain));
Suresh Siddha4c923d42009-10-02 11:01:24 -07001748 domain->nid = -1;
Jiang Liuab8dfe22014-07-11 14:19:27 +08001749 domain->flags = flags;
Omer Peleg0824c592016-04-20 19:03:35 +03001750 domain->has_iotlb_device = false;
Jiang Liu92d03cc2014-02-19 14:07:28 +08001751 INIT_LIST_HEAD(&domain->devices);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001752
1753 return domain;
1754}
1755
Joerg Roedeld160aca2015-07-22 11:52:53 +02001756/* Must be called with iommu->lock */
1757static int domain_attach_iommu(struct dmar_domain *domain,
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001758 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001759{
Jiang Liu44bde612014-07-11 14:19:29 +08001760 unsigned long ndomains;
Joerg Roedel55d94042015-07-22 16:50:40 +02001761 int num;
Jiang Liu44bde612014-07-11 14:19:29 +08001762
Joerg Roedel55d94042015-07-22 16:50:40 +02001763 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001764 assert_spin_locked(&iommu->lock);
Jiang Liu44bde612014-07-11 14:19:29 +08001765
Joerg Roedel29a27712015-07-21 17:17:12 +02001766 domain->iommu_refcnt[iommu->seq_id] += 1;
1767 domain->iommu_count += 1;
1768 if (domain->iommu_refcnt[iommu->seq_id] == 1) {
Jiang Liufb170fb2014-07-11 14:19:28 +08001769 ndomains = cap_ndoms(iommu->cap);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001770 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1771
1772 if (num >= ndomains) {
1773 pr_err("%s: No free domain ids\n", iommu->name);
1774 domain->iommu_refcnt[iommu->seq_id] -= 1;
1775 domain->iommu_count -= 1;
Joerg Roedel55d94042015-07-22 16:50:40 +02001776 return -ENOSPC;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001777 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001778
Joerg Roedeld160aca2015-07-22 11:52:53 +02001779 set_bit(num, iommu->domain_ids);
1780 set_iommu_domain(iommu, num, domain);
Jiang Liufb170fb2014-07-11 14:19:28 +08001781
Joerg Roedeld160aca2015-07-22 11:52:53 +02001782 domain->iommu_did[iommu->seq_id] = num;
1783 domain->nid = iommu->node;
1784
Jiang Liufb170fb2014-07-11 14:19:28 +08001785 domain_update_iommu_cap(domain);
1786 }
Joerg Roedeld160aca2015-07-22 11:52:53 +02001787
Joerg Roedel55d94042015-07-22 16:50:40 +02001788 return 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001789}
1790
1791static int domain_detach_iommu(struct dmar_domain *domain,
1792 struct intel_iommu *iommu)
1793{
Joerg Roedeld160aca2015-07-22 11:52:53 +02001794 int num, count = INT_MAX;
Jiang Liufb170fb2014-07-11 14:19:28 +08001795
Joerg Roedel55d94042015-07-22 16:50:40 +02001796 assert_spin_locked(&device_domain_lock);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001797 assert_spin_locked(&iommu->lock);
Jiang Liufb170fb2014-07-11 14:19:28 +08001798
Joerg Roedel29a27712015-07-21 17:17:12 +02001799 domain->iommu_refcnt[iommu->seq_id] -= 1;
1800 count = --domain->iommu_count;
1801 if (domain->iommu_refcnt[iommu->seq_id] == 0) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02001802 num = domain->iommu_did[iommu->seq_id];
1803 clear_bit(num, iommu->domain_ids);
1804 set_iommu_domain(iommu, num, NULL);
1805
Jiang Liufb170fb2014-07-11 14:19:28 +08001806 domain_update_iommu_cap(domain);
Joerg Roedelc0e8a6c2015-07-21 09:39:46 +02001807 domain->iommu_did[iommu->seq_id] = 0;
Jiang Liufb170fb2014-07-11 14:19:28 +08001808 }
Jiang Liufb170fb2014-07-11 14:19:28 +08001809
1810 return count;
1811}
1812
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001813static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001814static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001815
Joseph Cihula51a63e62011-03-21 11:04:24 -07001816static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001817{
1818 struct pci_dev *pdev = NULL;
1819 struct iova *iova;
1820 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001821
Zhen Leiaa3ac942017-09-21 16:52:45 +01001822 init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001823
Mark Gross8a443df2008-03-04 14:59:31 -08001824 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1825 &reserved_rbtree_key);
1826
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001827 /* IOAPIC ranges shouldn't be accessed by DMA */
1828 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1829 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001830 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001831 pr_err("Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001832 return -ENODEV;
1833 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001834
1835 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1836 for_each_pci_dev(pdev) {
1837 struct resource *r;
1838
1839 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1840 r = &pdev->resource[i];
1841 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1842 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001843 iova = reserve_iova(&reserved_iova_list,
1844 IOVA_PFN(r->start),
1845 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001846 if (!iova) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001847 pr_err("Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001848 return -ENODEV;
1849 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001850 }
1851 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001852 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001853}
1854
1855static void domain_reserve_special_ranges(struct dmar_domain *domain)
1856{
1857 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1858}
1859
1860static inline int guestwidth_to_adjustwidth(int gaw)
1861{
1862 int agaw;
1863 int r = (gaw - 12) % 9;
1864
1865 if (r == 0)
1866 agaw = gaw;
1867 else
1868 agaw = gaw + 9 - r;
1869 if (agaw > 64)
1870 agaw = 64;
1871 return agaw;
1872}
1873
Joerg Roedeldc534b22015-07-22 12:44:02 +02001874static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu,
1875 int guest_width)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001876{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001877 int adjust_width, agaw;
1878 unsigned long sagaw;
Joerg Roedel13cf0172017-08-11 11:40:10 +02001879 int err;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001880
Zhen Leiaa3ac942017-09-21 16:52:45 +01001881 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
Joerg Roedel13cf0172017-08-11 11:40:10 +02001882
1883 err = init_iova_flush_queue(&domain->iovad,
1884 iommu_flush_iova, iova_entry_free);
1885 if (err)
1886 return err;
1887
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001888 domain_reserve_special_ranges(domain);
1889
1890 /* calculate AGAW */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001891 if (guest_width > cap_mgaw(iommu->cap))
1892 guest_width = cap_mgaw(iommu->cap);
1893 domain->gaw = guest_width;
1894 adjust_width = guestwidth_to_adjustwidth(guest_width);
1895 agaw = width_to_agaw(adjust_width);
1896 sagaw = cap_sagaw(iommu->cap);
1897 if (!test_bit(agaw, &sagaw)) {
1898 /* hardware doesn't support it, choose a bigger one */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001899 pr_debug("Hardware doesn't support agaw %d\n", agaw);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001900 agaw = find_next_bit(&sagaw, 5, agaw);
1901 if (agaw >= 5)
1902 return -ENODEV;
1903 }
1904 domain->agaw = agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001905
Weidong Han8e6040972008-12-08 15:49:06 +08001906 if (ecap_coherent(iommu->ecap))
1907 domain->iommu_coherency = 1;
1908 else
1909 domain->iommu_coherency = 0;
1910
Sheng Yang58c610b2009-03-18 15:33:05 +08001911 if (ecap_sc_support(iommu->ecap))
1912 domain->iommu_snooping = 1;
1913 else
1914 domain->iommu_snooping = 0;
1915
David Woodhouse214e39a2014-03-19 10:38:49 +00001916 if (intel_iommu_superpage)
1917 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
1918 else
1919 domain->iommu_superpage = 0;
1920
Suresh Siddha4c923d42009-10-02 11:01:24 -07001921 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001922
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001923 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001924 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001925 if (!domain->pgd)
1926 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001927 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001928 return 0;
1929}
1930
1931static void domain_exit(struct dmar_domain *domain)
1932{
David Woodhouseea8ea462014-03-05 17:09:32 +00001933 struct page *freelist = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001934
1935 /* Domain 0 is reserved, so dont process it */
1936 if (!domain)
1937 return;
1938
Joerg Roedeld160aca2015-07-22 11:52:53 +02001939 /* Remove associated devices and clear attached or cached domains */
1940 rcu_read_lock();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001941 domain_remove_dev_info(domain);
Joerg Roedeld160aca2015-07-22 11:52:53 +02001942 rcu_read_unlock();
Jiang Liu92d03cc2014-02-19 14:07:28 +08001943
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001944 /* destroy iovas */
1945 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001946
David Woodhouseea8ea462014-03-05 17:09:32 +00001947 freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001948
David Woodhouseea8ea462014-03-05 17:09:32 +00001949 dma_free_pagelist(freelist);
1950
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001951 free_domain_mem(domain);
1952}
1953
David Woodhouse64ae8922014-03-09 12:52:30 -07001954static int domain_context_mapping_one(struct dmar_domain *domain,
1955 struct intel_iommu *iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02001956 u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001957{
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001958 u16 did = domain->iommu_did[iommu->seq_id];
Joerg Roedel28ccce02015-07-21 14:45:31 +02001959 int translation = CONTEXT_TT_MULTI_LEVEL;
1960 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001961 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001962 unsigned long flags;
Weidong Hanea6606b2008-12-08 23:08:15 +08001963 struct dma_pte *pgd;
Joerg Roedel55d94042015-07-22 16:50:40 +02001964 int ret, agaw;
Joerg Roedel28ccce02015-07-21 14:45:31 +02001965
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02001966 WARN_ON(did == 0);
1967
Joerg Roedel28ccce02015-07-21 14:45:31 +02001968 if (hw_pass_through && domain_type_is_si(domain))
1969 translation = CONTEXT_TT_PASS_THROUGH;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001970
1971 pr_debug("Set context mapping for %02x:%02x.%d\n",
1972 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001973
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001974 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001975
Joerg Roedel55d94042015-07-22 16:50:40 +02001976 spin_lock_irqsave(&device_domain_lock, flags);
1977 spin_lock(&iommu->lock);
1978
1979 ret = -ENOMEM;
David Woodhouse03ecc322015-02-13 14:35:21 +00001980 context = iommu_context_addr(iommu, bus, devfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001981 if (!context)
Joerg Roedel55d94042015-07-22 16:50:40 +02001982 goto out_unlock;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001983
Joerg Roedel55d94042015-07-22 16:50:40 +02001984 ret = 0;
1985 if (context_present(context))
1986 goto out_unlock;
Joerg Roedelcf484d02015-06-12 12:21:46 +02001987
Xunlei Pangaec0e862016-12-05 20:09:07 +08001988 /*
1989 * For kdump cases, old valid entries may be cached due to the
1990 * in-flight DMA and copied pgtable, but there is no unmapping
1991 * behaviour for them, thus we need an explicit cache flush for
1992 * the newly-mapped device. For kdump, at this point, the device
1993 * is supposed to finish reset at its driver probe stage, so no
1994 * in-flight DMA will exist, and we don't need to worry anymore
1995 * hereafter.
1996 */
1997 if (context_copied(context)) {
1998 u16 did_old = context_domain_id(context);
1999
Christos Gkekasb117e032017-10-08 23:33:31 +01002000 if (did_old < cap_ndoms(iommu->cap)) {
Xunlei Pangaec0e862016-12-05 20:09:07 +08002001 iommu->flush.flush_context(iommu, did_old,
2002 (((u16)bus) << 8) | devfn,
2003 DMA_CCMD_MASK_NOBIT,
2004 DMA_CCMD_DEVICE_INVL);
KarimAllah Ahmedf73a7ee2017-05-05 11:39:59 -07002005 iommu->flush.flush_iotlb(iommu, did_old, 0, 0,
2006 DMA_TLB_DSI_FLUSH);
2007 }
Xunlei Pangaec0e862016-12-05 20:09:07 +08002008 }
2009
Weidong Hanea6606b2008-12-08 23:08:15 +08002010 pgd = domain->pgd;
2011
Joerg Roedelde24e552015-07-21 14:53:04 +02002012 context_clear_entry(context);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002013 context_set_domain_id(context, did);
Weidong Hanea6606b2008-12-08 23:08:15 +08002014
Joerg Roedelde24e552015-07-21 14:53:04 +02002015 /*
2016 * Skip top levels of page tables for iommu which has less agaw
2017 * than default. Unnecessary for PT mode.
2018 */
Yu Zhao93a23a72009-05-18 13:51:37 +08002019 if (translation != CONTEXT_TT_PASS_THROUGH) {
Sohil Mehta3569dd02018-11-21 15:29:33 -08002020 for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) {
Joerg Roedel55d94042015-07-22 16:50:40 +02002021 ret = -ENOMEM;
Joerg Roedelde24e552015-07-21 14:53:04 +02002022 pgd = phys_to_virt(dma_pte_addr(pgd));
Joerg Roedel55d94042015-07-22 16:50:40 +02002023 if (!dma_pte_present(pgd))
2024 goto out_unlock;
Joerg Roedelde24e552015-07-21 14:53:04 +02002025 }
2026
David Woodhouse64ae8922014-03-09 12:52:30 -07002027 info = iommu_support_dev_iotlb(domain, iommu, bus, devfn);
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002028 if (info && info->ats_supported)
2029 translation = CONTEXT_TT_DEV_IOTLB;
2030 else
2031 translation = CONTEXT_TT_MULTI_LEVEL;
Joerg Roedelde24e552015-07-21 14:53:04 +02002032
Yu Zhao93a23a72009-05-18 13:51:37 +08002033 context_set_address_root(context, virt_to_phys(pgd));
Sohil Mehta3569dd02018-11-21 15:29:33 -08002034 context_set_address_width(context, agaw);
Joerg Roedelde24e552015-07-21 14:53:04 +02002035 } else {
2036 /*
2037 * In pass through mode, AW must be programmed to
2038 * indicate the largest AGAW value supported by
2039 * hardware. And ASR is ignored by hardware.
2040 */
2041 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08002042 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002043
2044 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00002045 context_set_fault_enable(context);
2046 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08002047 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002048
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002049 /*
2050 * It's a non-present to present mapping. If hardware doesn't cache
2051 * non-present entry we only need to flush the write-buffer. If the
2052 * _does_ cache non-present entries, then it does so in the special
2053 * domain #0, which we have to flush:
2054 */
2055 if (cap_caching_mode(iommu->cap)) {
2056 iommu->flush.flush_context(iommu, 0,
2057 (((u16)bus) << 8) | devfn,
2058 DMA_CCMD_MASK_NOBIT,
2059 DMA_CCMD_DEVICE_INVL);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002060 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002061 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002062 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002063 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002064 iommu_enable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08002065
Joerg Roedel55d94042015-07-22 16:50:40 +02002066 ret = 0;
2067
2068out_unlock:
2069 spin_unlock(&iommu->lock);
2070 spin_unlock_irqrestore(&device_domain_lock, flags);
Jiang Liufb170fb2014-07-11 14:19:28 +08002071
Wei Yang5c365d12016-07-13 13:53:21 +00002072 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002073}
2074
Alex Williamson579305f2014-07-03 09:51:43 -06002075struct domain_context_mapping_data {
2076 struct dmar_domain *domain;
2077 struct intel_iommu *iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002078};
2079
2080static int domain_context_mapping_cb(struct pci_dev *pdev,
2081 u16 alias, void *opaque)
2082{
2083 struct domain_context_mapping_data *data = opaque;
2084
2085 return domain_context_mapping_one(data->domain, data->iommu,
Joerg Roedel28ccce02015-07-21 14:45:31 +02002086 PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06002087}
2088
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002089static int
Joerg Roedel28ccce02015-07-21 14:45:31 +02002090domain_context_mapping(struct dmar_domain *domain, struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002091{
David Woodhouse64ae8922014-03-09 12:52:30 -07002092 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002093 u8 bus, devfn;
Alex Williamson579305f2014-07-03 09:51:43 -06002094 struct domain_context_mapping_data data;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002095
David Woodhousee1f167f2014-03-09 15:24:46 -07002096 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse64ae8922014-03-09 12:52:30 -07002097 if (!iommu)
2098 return -ENODEV;
2099
Alex Williamson579305f2014-07-03 09:51:43 -06002100 if (!dev_is_pci(dev))
Joerg Roedel28ccce02015-07-21 14:45:31 +02002101 return domain_context_mapping_one(domain, iommu, bus, devfn);
Alex Williamson579305f2014-07-03 09:51:43 -06002102
2103 data.domain = domain;
2104 data.iommu = iommu;
Alex Williamson579305f2014-07-03 09:51:43 -06002105
2106 return pci_for_each_dma_alias(to_pci_dev(dev),
2107 &domain_context_mapping_cb, &data);
2108}
2109
2110static int domain_context_mapped_cb(struct pci_dev *pdev,
2111 u16 alias, void *opaque)
2112{
2113 struct intel_iommu *iommu = opaque;
2114
2115 return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002116}
2117
David Woodhousee1f167f2014-03-09 15:24:46 -07002118static int domain_context_mapped(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002119{
Weidong Han5331fe62008-12-08 23:00:00 +08002120 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002121 u8 bus, devfn;
Weidong Han5331fe62008-12-08 23:00:00 +08002122
David Woodhousee1f167f2014-03-09 15:24:46 -07002123 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08002124 if (!iommu)
2125 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002126
Alex Williamson579305f2014-07-03 09:51:43 -06002127 if (!dev_is_pci(dev))
2128 return device_context_mapped(iommu, bus, devfn);
David Woodhousee1f167f2014-03-09 15:24:46 -07002129
Alex Williamson579305f2014-07-03 09:51:43 -06002130 return !pci_for_each_dma_alias(to_pci_dev(dev),
2131 domain_context_mapped_cb, iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002132}
2133
Fenghua Yuf5329592009-08-04 15:09:37 -07002134/* Returns a number of VTD pages, but aligned to MM page size */
2135static inline unsigned long aligned_nrpages(unsigned long host_addr,
2136 size_t size)
2137{
2138 host_addr &= ~PAGE_MASK;
2139 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
2140}
2141
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002142/* Return largest possible superpage level for a given mapping */
2143static inline int hardware_largepage_caps(struct dmar_domain *domain,
2144 unsigned long iov_pfn,
2145 unsigned long phy_pfn,
2146 unsigned long pages)
2147{
2148 int support, level = 1;
2149 unsigned long pfnmerge;
2150
2151 support = domain->iommu_superpage;
2152
2153 /* To use a large page, the virtual *and* physical addresses
2154 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
2155 of them will mean we have to use smaller pages. So just
2156 merge them and check both at once. */
2157 pfnmerge = iov_pfn | phy_pfn;
2158
2159 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
2160 pages >>= VTD_STRIDE_SHIFT;
2161 if (!pages)
2162 break;
2163 pfnmerge >>= VTD_STRIDE_SHIFT;
2164 level++;
2165 support--;
2166 }
2167 return level;
2168}
2169
David Woodhouse9051aa02009-06-29 12:30:54 +01002170static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2171 struct scatterlist *sg, unsigned long phys_pfn,
2172 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01002173{
2174 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01002175 phys_addr_t uninitialized_var(pteval);
Jiang Liucc4f14a2014-11-26 09:42:10 +08002176 unsigned long sg_res = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002177 unsigned int largepage_lvl = 0;
2178 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01002179
Jiang Liu162d1b12014-07-11 14:19:35 +08002180 BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
David Woodhousee1605492009-06-29 11:17:38 +01002181
2182 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
2183 return -EINVAL;
2184
2185 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
2186
Jiang Liucc4f14a2014-11-26 09:42:10 +08002187 if (!sg) {
2188 sg_res = nr_pages;
David Woodhouse9051aa02009-06-29 12:30:54 +01002189 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
2190 }
2191
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002192 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01002193 uint64_t tmp;
2194
David Woodhousee1605492009-06-29 11:17:38 +01002195 if (!sg_res) {
Robin Murphy29a90b72017-09-28 15:14:01 +01002196 unsigned int pgoff = sg->offset & ~PAGE_MASK;
2197
Fenghua Yuf5329592009-08-04 15:09:37 -07002198 sg_res = aligned_nrpages(sg->offset, sg->length);
Robin Murphy29a90b72017-09-28 15:14:01 +01002199 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff;
David Woodhousee1605492009-06-29 11:17:38 +01002200 sg->dma_length = sg->length;
Robin Murphy29a90b72017-09-28 15:14:01 +01002201 pteval = (sg_phys(sg) - pgoff) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002202 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01002203 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002204
David Woodhousee1605492009-06-29 11:17:38 +01002205 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002206 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
2207
David Woodhouse5cf0a762014-03-19 16:07:49 +00002208 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01002209 if (!pte)
2210 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002211 /* It is large page*/
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002212 if (largepage_lvl > 1) {
Christian Zanderba2374f2015-06-10 09:41:45 -07002213 unsigned long nr_superpages, end_pfn;
2214
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002215 pteval |= DMA_PTE_LARGE_PAGE;
Jiang Liud41a4ad2014-07-11 14:19:34 +08002216 lvl_pages = lvl_to_nr_pages(largepage_lvl);
Christian Zanderba2374f2015-06-10 09:41:45 -07002217
2218 nr_superpages = sg_res / lvl_pages;
2219 end_pfn = iov_pfn + nr_superpages * lvl_pages - 1;
2220
Jiang Liud41a4ad2014-07-11 14:19:34 +08002221 /*
2222 * Ensure that old small page tables are
Christian Zanderba2374f2015-06-10 09:41:45 -07002223 * removed to make room for superpage(s).
David Dillowbc24c572017-06-28 19:42:23 -07002224 * We're adding new large pages, so make sure
2225 * we don't remove their parent tables.
Jiang Liud41a4ad2014-07-11 14:19:34 +08002226 */
David Dillowbc24c572017-06-28 19:42:23 -07002227 dma_pte_free_pagetable(domain, iov_pfn, end_pfn,
2228 largepage_lvl + 1);
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002229 } else {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002230 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
Woodhouse, David6491d4d2012-12-19 13:25:35 +00002231 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002232
David Woodhousee1605492009-06-29 11:17:38 +01002233 }
2234 /* We don't need lock here, nobody else
2235 * touches the iova range
2236 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01002237 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01002238 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01002239 static int dumps = 5;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002240 pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
2241 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01002242 if (dumps) {
2243 dumps--;
2244 debug_dma_dump_mappings(NULL);
2245 }
2246 WARN_ON(1);
2247 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002248
2249 lvl_pages = lvl_to_nr_pages(largepage_lvl);
2250
2251 BUG_ON(nr_pages < lvl_pages);
2252 BUG_ON(sg_res < lvl_pages);
2253
2254 nr_pages -= lvl_pages;
2255 iov_pfn += lvl_pages;
2256 phys_pfn += lvl_pages;
2257 pteval += lvl_pages * VTD_PAGE_SIZE;
2258 sg_res -= lvl_pages;
2259
2260 /* If the next PTE would be the first in a new page, then we
2261 need to flush the cache on the entries we've just written.
2262 And then we'll need to recalculate 'pte', so clear it and
2263 let it get set again in the if (!pte) block above.
2264
2265 If we're done (!nr_pages) we need to flush the cache too.
2266
2267 Also if we've been setting superpages, we may need to
2268 recalculate 'pte' and switch back to smaller pages for the
2269 end of the mapping, if the trailing size is not enough to
2270 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01002271 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002272 if (!nr_pages || first_pte_in_page(pte) ||
2273 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01002274 domain_flush_cache(domain, first_pte,
2275 (void *)pte - (void *)first_pte);
2276 pte = NULL;
2277 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01002278
2279 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01002280 sg = sg_next(sg);
2281 }
2282 return 0;
2283}
2284
Peter Xu87684fd2018-05-04 10:34:53 +08002285static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2286 struct scatterlist *sg, unsigned long phys_pfn,
2287 unsigned long nr_pages, int prot)
2288{
2289 int ret;
2290 struct intel_iommu *iommu;
2291
2292 /* Do the real mapping first */
2293 ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot);
2294 if (ret)
2295 return ret;
2296
2297 /* Notify about the new mapping */
2298 if (domain_type_is_vm(domain)) {
2299 /* VM typed domains can have more than one IOMMUs */
2300 int iommu_id;
2301 for_each_domain_iommu(iommu_id, domain) {
2302 iommu = g_iommus[iommu_id];
2303 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2304 }
2305 } else {
2306 /* General domains only have one IOMMU */
2307 iommu = domain_get_iommu(domain);
2308 __mapping_notify_one(iommu, domain, iov_pfn, nr_pages);
2309 }
2310
2311 return 0;
2312}
2313
David Woodhouse9051aa02009-06-29 12:30:54 +01002314static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2315 struct scatterlist *sg, unsigned long nr_pages,
2316 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002317{
Peter Xu87684fd2018-05-04 10:34:53 +08002318 return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
David Woodhouse9051aa02009-06-29 12:30:54 +01002319}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002320
David Woodhouse9051aa02009-06-29 12:30:54 +01002321static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
2322 unsigned long phys_pfn, unsigned long nr_pages,
2323 int prot)
2324{
Peter Xu87684fd2018-05-04 10:34:53 +08002325 return domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002326}
2327
Joerg Roedel2452d9d2015-07-23 16:20:14 +02002328static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002329{
Filippo Sironi50822192017-08-31 10:58:11 +02002330 unsigned long flags;
2331 struct context_entry *context;
2332 u16 did_old;
2333
Weidong Hanc7151a82008-12-08 22:51:37 +08002334 if (!iommu)
2335 return;
Weidong Han8c11e792008-12-08 15:29:22 +08002336
Filippo Sironi50822192017-08-31 10:58:11 +02002337 spin_lock_irqsave(&iommu->lock, flags);
2338 context = iommu_context_addr(iommu, bus, devfn, 0);
2339 if (!context) {
2340 spin_unlock_irqrestore(&iommu->lock, flags);
2341 return;
2342 }
2343 did_old = context_domain_id(context);
2344 context_clear_entry(context);
2345 __iommu_flush_cache(iommu, context, sizeof(*context));
2346 spin_unlock_irqrestore(&iommu->lock, flags);
2347 iommu->flush.flush_context(iommu,
2348 did_old,
2349 (((u16)bus) << 8) | devfn,
2350 DMA_CCMD_MASK_NOBIT,
2351 DMA_CCMD_DEVICE_INVL);
2352 iommu->flush.flush_iotlb(iommu,
2353 did_old,
2354 0,
2355 0,
2356 DMA_TLB_DSI_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002357}
2358
David Woodhouse109b9b02012-05-25 17:43:02 +01002359static inline void unlink_domain_info(struct device_domain_info *info)
2360{
2361 assert_spin_locked(&device_domain_lock);
2362 list_del(&info->link);
2363 list_del(&info->global);
2364 if (info->dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002365 info->dev->archdata.iommu = NULL;
David Woodhouse109b9b02012-05-25 17:43:02 +01002366}
2367
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002368static void domain_remove_dev_info(struct dmar_domain *domain)
2369{
Yijing Wang3a74ca02014-05-20 20:37:47 +08002370 struct device_domain_info *info, *tmp;
Jiang Liufb170fb2014-07-11 14:19:28 +08002371 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002372
2373 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel76f45fe2015-07-21 18:25:11 +02002374 list_for_each_entry_safe(info, tmp, &domain->devices, link)
Joerg Roedel127c7612015-07-23 17:44:46 +02002375 __dmar_remove_one_dev_info(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002376 spin_unlock_irqrestore(&device_domain_lock, flags);
2377}
2378
2379/*
2380 * find_domain
David Woodhouse1525a292014-03-06 16:19:30 +00002381 * Note: we use struct device->archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002382 */
David Woodhouse1525a292014-03-06 16:19:30 +00002383static struct dmar_domain *find_domain(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002384{
2385 struct device_domain_info *info;
2386
2387 /* No lock here, assumes no domain exit in normal case */
David Woodhouse1525a292014-03-06 16:19:30 +00002388 info = dev->archdata.iommu;
Peter Xub316d022017-05-22 18:28:51 +08002389 if (likely(info))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002390 return info->domain;
2391 return NULL;
2392}
2393
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002394static inline struct device_domain_info *
Jiang Liu745f2582014-02-19 14:07:26 +08002395dmar_search_domain_by_dev_info(int segment, int bus, int devfn)
2396{
2397 struct device_domain_info *info;
2398
2399 list_for_each_entry(info, &device_domain_list, global)
David Woodhouse41e80dca2014-03-09 13:55:54 -07002400 if (info->iommu->segment == segment && info->bus == bus &&
Jiang Liu745f2582014-02-19 14:07:26 +08002401 info->devfn == devfn)
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002402 return info;
Jiang Liu745f2582014-02-19 14:07:26 +08002403
2404 return NULL;
2405}
2406
Joerg Roedel5db31562015-07-22 12:40:43 +02002407static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
2408 int bus, int devfn,
2409 struct device *dev,
2410 struct dmar_domain *domain)
Jiang Liu745f2582014-02-19 14:07:26 +08002411{
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002412 struct dmar_domain *found = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002413 struct device_domain_info *info;
2414 unsigned long flags;
Joerg Roedeld160aca2015-07-22 11:52:53 +02002415 int ret;
Jiang Liu745f2582014-02-19 14:07:26 +08002416
2417 info = alloc_devinfo_mem();
2418 if (!info)
David Woodhouseb718cd32014-03-09 13:11:33 -07002419 return NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002420
Jiang Liu745f2582014-02-19 14:07:26 +08002421 info->bus = bus;
2422 info->devfn = devfn;
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002423 info->ats_supported = info->pasid_supported = info->pri_supported = 0;
2424 info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0;
2425 info->ats_qdep = 0;
Jiang Liu745f2582014-02-19 14:07:26 +08002426 info->dev = dev;
2427 info->domain = domain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002428 info->iommu = iommu;
Lu Baolucc580e42018-07-14 15:46:59 +08002429 info->pasid_table = NULL;
Jiang Liu745f2582014-02-19 14:07:26 +08002430
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002431 if (dev && dev_is_pci(dev)) {
2432 struct pci_dev *pdev = to_pci_dev(info->dev);
2433
Gil Kupfercef74402018-05-10 17:56:02 -05002434 if (!pci_ats_disabled() &&
2435 ecap_dev_iotlb_support(iommu->ecap) &&
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002436 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
2437 dmar_find_matched_atsr_unit(pdev))
2438 info->ats_supported = 1;
2439
Lu Baolu765b6a92018-12-10 09:58:55 +08002440 if (sm_supported(iommu)) {
2441 if (pasid_supported(iommu)) {
David Woodhouseb16d0cb2015-10-12 14:17:37 +01002442 int features = pci_pasid_features(pdev);
2443 if (features >= 0)
2444 info->pasid_supported = features | 1;
2445 }
2446
2447 if (info->ats_supported && ecap_prs(iommu->ecap) &&
2448 pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI))
2449 info->pri_supported = 1;
2450 }
2451 }
2452
Jiang Liu745f2582014-02-19 14:07:26 +08002453 spin_lock_irqsave(&device_domain_lock, flags);
2454 if (dev)
David Woodhouse0bcb3e22014-03-06 17:12:03 +00002455 found = find_domain(dev);
Joerg Roedelf303e502015-07-23 18:37:13 +02002456
2457 if (!found) {
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002458 struct device_domain_info *info2;
David Woodhouse41e80dca2014-03-09 13:55:54 -07002459 info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn);
Joerg Roedelf303e502015-07-23 18:37:13 +02002460 if (info2) {
2461 found = info2->domain;
2462 info2->dev = dev;
2463 }
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002464 }
Joerg Roedelf303e502015-07-23 18:37:13 +02002465
Jiang Liu745f2582014-02-19 14:07:26 +08002466 if (found) {
2467 spin_unlock_irqrestore(&device_domain_lock, flags);
2468 free_devinfo_mem(info);
David Woodhouseb718cd32014-03-09 13:11:33 -07002469 /* Caller must free the original domain */
2470 return found;
Jiang Liu745f2582014-02-19 14:07:26 +08002471 }
2472
Joerg Roedeld160aca2015-07-22 11:52:53 +02002473 spin_lock(&iommu->lock);
2474 ret = domain_attach_iommu(domain, iommu);
2475 spin_unlock(&iommu->lock);
2476
2477 if (ret) {
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002478 spin_unlock_irqrestore(&device_domain_lock, flags);
Sudip Mukherjee499f3aa2015-09-18 16:27:07 +05302479 free_devinfo_mem(info);
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002480 return NULL;
2481 }
Joerg Roedelc6c2ceb2015-07-22 13:11:53 +02002482
David Woodhouseb718cd32014-03-09 13:11:33 -07002483 list_add(&info->link, &domain->devices);
2484 list_add(&info->global, &device_domain_list);
2485 if (dev)
2486 dev->archdata.iommu = info;
Lu Baolu0bbeb012018-12-10 09:58:56 +08002487 spin_unlock_irqrestore(&device_domain_lock, flags);
Lu Baolua7fc93f2018-07-14 15:47:00 +08002488
Lu Baolu0bbeb012018-12-10 09:58:56 +08002489 /* PASID table is mandatory for a PCI device in scalable mode. */
2490 if (dev && dev_is_pci(dev) && sm_supported(iommu)) {
Lu Baolua7fc93f2018-07-14 15:47:00 +08002491 ret = intel_pasid_alloc_table(dev);
2492 if (ret) {
Lu Baolu0bbeb012018-12-10 09:58:56 +08002493 pr_err("PASID table allocation for %s failed\n",
2494 dev_name(dev));
2495 dmar_remove_one_dev_info(domain, dev);
2496 return NULL;
Lu Baolua7fc93f2018-07-14 15:47:00 +08002497 }
2498 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002499
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002500 if (dev && domain_context_mapping(domain, dev)) {
2501 pr_err("Domain context map for %s failed\n", dev_name(dev));
Joerg Roedele6de0f82015-07-22 16:30:36 +02002502 dmar_remove_one_dev_info(domain, dev);
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002503 return NULL;
2504 }
2505
David Woodhouseb718cd32014-03-09 13:11:33 -07002506 return domain;
Jiang Liu745f2582014-02-19 14:07:26 +08002507}
2508
Alex Williamson579305f2014-07-03 09:51:43 -06002509static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque)
2510{
2511 *(u16 *)opaque = alias;
2512 return 0;
2513}
2514
Joerg Roedel76208352016-08-25 14:25:12 +02002515static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002516{
Joerg Roedelcc4e2572015-07-22 10:04:36 +02002517 struct device_domain_info *info = NULL;
Joerg Roedel76208352016-08-25 14:25:12 +02002518 struct dmar_domain *domain = NULL;
Alex Williamson579305f2014-07-03 09:51:43 -06002519 struct intel_iommu *iommu;
Lu Baolufcc35c62018-05-04 13:08:17 +08002520 u16 dma_alias;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002521 unsigned long flags;
Yijing Wangaa4d0662014-05-26 20:14:06 +08002522 u8 bus, devfn;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002523
David Woodhouse146922e2014-03-09 15:44:17 -07002524 iommu = device_to_iommu(dev, &bus, &devfn);
2525 if (!iommu)
Alex Williamson579305f2014-07-03 09:51:43 -06002526 return NULL;
2527
2528 if (dev_is_pci(dev)) {
2529 struct pci_dev *pdev = to_pci_dev(dev);
2530
2531 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2532
2533 spin_lock_irqsave(&device_domain_lock, flags);
2534 info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus),
2535 PCI_BUS_NUM(dma_alias),
2536 dma_alias & 0xff);
2537 if (info) {
2538 iommu = info->iommu;
2539 domain = info->domain;
2540 }
2541 spin_unlock_irqrestore(&device_domain_lock, flags);
2542
Joerg Roedel76208352016-08-25 14:25:12 +02002543 /* DMA alias already has a domain, use it */
Alex Williamson579305f2014-07-03 09:51:43 -06002544 if (info)
Joerg Roedel76208352016-08-25 14:25:12 +02002545 goto out;
Alex Williamson579305f2014-07-03 09:51:43 -06002546 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002547
David Woodhouse146922e2014-03-09 15:44:17 -07002548 /* Allocate and initialize new domain for the device */
Jiang Liuab8dfe22014-07-11 14:19:27 +08002549 domain = alloc_domain(0);
Jiang Liu745f2582014-02-19 14:07:26 +08002550 if (!domain)
Alex Williamson579305f2014-07-03 09:51:43 -06002551 return NULL;
Joerg Roedeldc534b22015-07-22 12:44:02 +02002552 if (domain_init(domain, iommu, gaw)) {
Alex Williamson579305f2014-07-03 09:51:43 -06002553 domain_exit(domain);
2554 return NULL;
2555 }
2556
Joerg Roedel76208352016-08-25 14:25:12 +02002557out:
Alex Williamson579305f2014-07-03 09:51:43 -06002558
Joerg Roedel76208352016-08-25 14:25:12 +02002559 return domain;
2560}
2561
2562static struct dmar_domain *set_domain_for_dev(struct device *dev,
2563 struct dmar_domain *domain)
2564{
2565 struct intel_iommu *iommu;
2566 struct dmar_domain *tmp;
2567 u16 req_id, dma_alias;
2568 u8 bus, devfn;
2569
2570 iommu = device_to_iommu(dev, &bus, &devfn);
2571 if (!iommu)
2572 return NULL;
2573
2574 req_id = ((u16)bus << 8) | devfn;
2575
2576 if (dev_is_pci(dev)) {
2577 struct pci_dev *pdev = to_pci_dev(dev);
2578
2579 pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias);
2580
2581 /* register PCI DMA alias device */
2582 if (req_id != dma_alias) {
2583 tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias),
2584 dma_alias & 0xff, NULL, domain);
2585
2586 if (!tmp || tmp != domain)
2587 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002588 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002589 }
2590
Joerg Roedel5db31562015-07-22 12:40:43 +02002591 tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
Joerg Roedel76208352016-08-25 14:25:12 +02002592 if (!tmp || tmp != domain)
2593 return tmp;
Alex Williamson579305f2014-07-03 09:51:43 -06002594
Joerg Roedel76208352016-08-25 14:25:12 +02002595 return domain;
2596}
2597
2598static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw)
2599{
2600 struct dmar_domain *domain, *tmp;
2601
2602 domain = find_domain(dev);
2603 if (domain)
2604 goto out;
2605
2606 domain = find_or_alloc_domain(dev, gaw);
2607 if (!domain)
2608 goto out;
2609
2610 tmp = set_domain_for_dev(dev, domain);
2611 if (!tmp || domain != tmp) {
Alex Williamson579305f2014-07-03 09:51:43 -06002612 domain_exit(domain);
2613 domain = tmp;
2614 }
David Woodhouseb718cd32014-03-09 13:11:33 -07002615
Joerg Roedel76208352016-08-25 14:25:12 +02002616out:
2617
David Woodhouseb718cd32014-03-09 13:11:33 -07002618 return domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002619}
2620
David Woodhouseb2132032009-06-26 18:50:28 +01002621static int iommu_domain_identity_map(struct dmar_domain *domain,
2622 unsigned long long start,
2623 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002624{
David Woodhousec5395d52009-06-28 16:35:56 +01002625 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2626 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002627
David Woodhousec5395d52009-06-28 16:35:56 +01002628 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2629 dma_to_mm_pfn(last_vpfn))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002630 pr_err("Reserving iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002631 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002632 }
2633
Joerg Roedelaf1089c2015-07-21 15:45:19 +02002634 pr_debug("Mapping reserved region %llx-%llx\n", start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002635 /*
2636 * RMRR range might have overlap with physical memory range,
2637 * clear it first
2638 */
David Woodhousec5395d52009-06-28 16:35:56 +01002639 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002640
Peter Xu87684fd2018-05-04 10:34:53 +08002641 return __domain_mapping(domain, first_vpfn, NULL,
2642 first_vpfn, last_vpfn - first_vpfn + 1,
2643 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002644}
2645
Joerg Roedeld66ce542015-09-23 19:00:10 +02002646static int domain_prepare_identity_map(struct device *dev,
2647 struct dmar_domain *domain,
2648 unsigned long long start,
2649 unsigned long long end)
David Woodhouseb2132032009-06-26 18:50:28 +01002650{
David Woodhouse19943b02009-08-04 16:19:20 +01002651 /* For _hardware_ passthrough, don't bother. But for software
2652 passthrough, we do it anyway -- it may indicate a memory
2653 range which is reserved in E820, so which didn't get set
2654 up to start with in si_domain */
2655 if (domain == si_domain && hw_pass_through) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002656 pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2657 dev_name(dev), start, end);
David Woodhouse19943b02009-08-04 16:19:20 +01002658 return 0;
2659 }
2660
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002661 pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2662 dev_name(dev), start, end);
2663
David Woodhouse5595b522009-12-02 09:21:55 +00002664 if (end < start) {
2665 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2666 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2667 dmi_get_system_info(DMI_BIOS_VENDOR),
2668 dmi_get_system_info(DMI_BIOS_VERSION),
2669 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002670 return -EIO;
David Woodhouse5595b522009-12-02 09:21:55 +00002671 }
2672
David Woodhouse2ff729f2009-08-26 14:25:41 +01002673 if (end >> agaw_to_width(domain->agaw)) {
2674 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2675 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2676 agaw_to_width(domain->agaw),
2677 dmi_get_system_info(DMI_BIOS_VENDOR),
2678 dmi_get_system_info(DMI_BIOS_VERSION),
2679 dmi_get_system_info(DMI_PRODUCT_VERSION));
Joerg Roedeld66ce542015-09-23 19:00:10 +02002680 return -EIO;
David Woodhouse2ff729f2009-08-26 14:25:41 +01002681 }
David Woodhouse19943b02009-08-04 16:19:20 +01002682
Joerg Roedeld66ce542015-09-23 19:00:10 +02002683 return iommu_domain_identity_map(domain, start, end);
2684}
2685
2686static int iommu_prepare_identity_map(struct device *dev,
2687 unsigned long long start,
2688 unsigned long long end)
2689{
2690 struct dmar_domain *domain;
2691 int ret;
2692
2693 domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
2694 if (!domain)
2695 return -ENOMEM;
2696
2697 ret = domain_prepare_identity_map(dev, domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002698 if (ret)
Joerg Roedeld66ce542015-09-23 19:00:10 +02002699 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002700
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002701 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002702}
2703
2704static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
David Woodhouse0b9d9752014-03-09 15:48:15 -07002705 struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002706{
David Woodhouse0b9d9752014-03-09 15:48:15 -07002707 if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002708 return 0;
David Woodhouse0b9d9752014-03-09 15:48:15 -07002709 return iommu_prepare_identity_map(dev, rmrr->base_address,
2710 rmrr->end_address);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002711}
2712
Suresh Siddhad3f13812011-08-23 17:05:25 -07002713#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002714static inline void iommu_prepare_isa(void)
2715{
2716 struct pci_dev *pdev;
2717 int ret;
2718
2719 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2720 if (!pdev)
2721 return;
2722
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002723 pr_info("Prepare 0-16MiB unity mapping for LPC\n");
David Woodhouse0b9d9752014-03-09 15:48:15 -07002724 ret = iommu_prepare_identity_map(&pdev->dev, 0, 16*1024*1024 - 1);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002725
2726 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002727 pr_err("Failed to create 0-16MiB identity map - floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002728
Yijing Wang9b27e822014-05-20 20:37:52 +08002729 pci_dev_put(pdev);
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002730}
2731#else
2732static inline void iommu_prepare_isa(void)
2733{
2734 return;
2735}
Suresh Siddhad3f13812011-08-23 17:05:25 -07002736#endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002737
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002738static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002739
Matt Kraai071e1372009-08-23 22:30:22 -07002740static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002741{
David Woodhousec7ab48d2009-06-26 19:10:36 +01002742 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002743
Jiang Liuab8dfe22014-07-11 14:19:27 +08002744 si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002745 if (!si_domain)
2746 return -EFAULT;
2747
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002748 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2749 domain_exit(si_domain);
2750 return -EFAULT;
2751 }
2752
Joerg Roedel0dc79712015-07-21 15:40:06 +02002753 pr_debug("Identity mapping domain allocated\n");
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002754
David Woodhouse19943b02009-08-04 16:19:20 +01002755 if (hw)
2756 return 0;
2757
David Woodhousec7ab48d2009-06-26 19:10:36 +01002758 for_each_online_node(nid) {
Tejun Heod4bbf7e2011-11-28 09:46:22 -08002759 unsigned long start_pfn, end_pfn;
2760 int i;
2761
2762 for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
2763 ret = iommu_domain_identity_map(si_domain,
2764 PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
2765 if (ret)
2766 return ret;
2767 }
David Woodhousec7ab48d2009-06-26 19:10:36 +01002768 }
2769
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002770 return 0;
2771}
2772
David Woodhouse9b226622014-03-09 14:03:28 -07002773static int identity_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002774{
2775 struct device_domain_info *info;
2776
2777 if (likely(!iommu_identity_mapping))
2778 return 0;
2779
David Woodhouse9b226622014-03-09 14:03:28 -07002780 info = dev->archdata.iommu;
Mike Traviscb452a42011-05-28 13:15:03 -05002781 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2782 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002783
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002784 return 0;
2785}
2786
Joerg Roedel28ccce02015-07-21 14:45:31 +02002787static int domain_add_dev_info(struct dmar_domain *domain, struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002788{
David Woodhouse0ac72662014-03-09 13:19:22 -07002789 struct dmar_domain *ndomain;
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002790 struct intel_iommu *iommu;
David Woodhouse156baca2014-03-09 14:00:57 -07002791 u8 bus, devfn;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002792
David Woodhouse5913c9b2014-03-09 16:27:31 -07002793 iommu = device_to_iommu(dev, &bus, &devfn);
David Woodhouse5a8f40e2014-03-09 13:31:18 -07002794 if (!iommu)
2795 return -ENODEV;
2796
Joerg Roedel5db31562015-07-22 12:40:43 +02002797 ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain);
David Woodhouse0ac72662014-03-09 13:19:22 -07002798 if (ndomain != domain)
2799 return -EBUSY;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002800
2801 return 0;
2802}
2803
David Woodhouse0b9d9752014-03-09 15:48:15 -07002804static bool device_has_rmrr(struct device *dev)
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002805{
2806 struct dmar_rmrr_unit *rmrr;
David Woodhouse832bd852014-03-07 15:08:36 +00002807 struct device *tmp;
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002808 int i;
2809
Jiang Liu0e2426122014-02-19 14:07:34 +08002810 rcu_read_lock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002811 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08002812 /*
2813 * Return TRUE if this RMRR contains the device that
2814 * is passed in.
2815 */
2816 for_each_active_dev_scope(rmrr->devices,
2817 rmrr->devices_cnt, i, tmp)
David Woodhouse0b9d9752014-03-09 15:48:15 -07002818 if (tmp == dev) {
Jiang Liu0e2426122014-02-19 14:07:34 +08002819 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002820 return true;
Jiang Liub683b232014-02-19 14:07:32 +08002821 }
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002822 }
Jiang Liu0e2426122014-02-19 14:07:34 +08002823 rcu_read_unlock();
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002824 return false;
2825}
2826
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002827/*
2828 * There are a couple cases where we need to restrict the functionality of
2829 * devices associated with RMRRs. The first is when evaluating a device for
2830 * identity mapping because problems exist when devices are moved in and out
2831 * of domains and their respective RMRR information is lost. This means that
2832 * a device with associated RMRRs will never be in a "passthrough" domain.
2833 * The second is use of the device through the IOMMU API. This interface
2834 * expects to have full control of the IOVA space for the device. We cannot
2835 * satisfy both the requirement that RMRR access is maintained and have an
2836 * unencumbered IOVA space. We also have no ability to quiesce the device's
2837 * use of the RMRR space or even inform the IOMMU API user of the restriction.
2838 * We therefore prevent devices associated with an RMRR from participating in
2839 * the IOMMU API, which eliminates them from device assignment.
2840 *
2841 * In both cases we assume that PCI USB devices with RMRRs have them largely
2842 * for historical reasons and that the RMRR space is not actively used post
2843 * boot. This exclusion may change if vendors begin to abuse it.
David Woodhouse18436af2015-03-25 15:05:47 +00002844 *
2845 * The same exception is made for graphics devices, with the requirement that
2846 * any use of the RMRR regions will be torn down before assigning the device
2847 * to a guest.
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002848 */
2849static bool device_is_rmrr_locked(struct device *dev)
2850{
2851 if (!device_has_rmrr(dev))
2852 return false;
2853
2854 if (dev_is_pci(dev)) {
2855 struct pci_dev *pdev = to_pci_dev(dev);
2856
David Woodhouse18436af2015-03-25 15:05:47 +00002857 if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev))
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002858 return false;
2859 }
2860
2861 return true;
2862}
2863
David Woodhouse3bdb2592014-03-09 16:03:08 -07002864static int iommu_should_identity_map(struct device *dev, int startup)
David Woodhouse6941af22009-07-04 18:24:27 +01002865{
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002866
David Woodhouse3bdb2592014-03-09 16:03:08 -07002867 if (dev_is_pci(dev)) {
2868 struct pci_dev *pdev = to_pci_dev(dev);
Tom Mingarelliea2447f72012-11-20 19:43:17 +00002869
Alex Williamsonc875d2c2014-07-03 09:57:02 -06002870 if (device_is_rmrr_locked(dev))
David Woodhouse3bdb2592014-03-09 16:03:08 -07002871 return 0;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002872
David Woodhouse3bdb2592014-03-09 16:03:08 -07002873 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2874 return 1;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002875
David Woodhouse3bdb2592014-03-09 16:03:08 -07002876 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2877 return 1;
2878
2879 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2880 return 0;
2881
2882 /*
2883 * We want to start off with all devices in the 1:1 domain, and
2884 * take them out later if we find they can't access all of memory.
2885 *
2886 * However, we can't do this for PCI devices behind bridges,
2887 * because all PCI devices behind the same bridge will end up
2888 * with the same source-id on their transactions.
2889 *
2890 * Practically speaking, we can't change things around for these
2891 * devices at run-time, because we can't be sure there'll be no
2892 * DMA transactions in flight for any of their siblings.
2893 *
2894 * So PCI devices (unless they're on the root bus) as well as
2895 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2896 * the 1:1 domain, just in _case_ one of their siblings turns out
2897 * not to be able to map all of memory.
2898 */
2899 if (!pci_is_pcie(pdev)) {
2900 if (!pci_is_root_bus(pdev->bus))
2901 return 0;
2902 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2903 return 0;
2904 } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE)
2905 return 0;
2906 } else {
2907 if (device_has_rmrr(dev))
2908 return 0;
2909 }
David Woodhouse6941af22009-07-04 18:24:27 +01002910
David Woodhouse3dfc8132009-07-04 19:11:08 +01002911 /*
David Woodhouse3dfc8132009-07-04 19:11:08 +01002912 * At boot time, we don't yet know if devices will be 64-bit capable.
David Woodhouse3bdb2592014-03-09 16:03:08 -07002913 * Assume that they will — if they turn out not to be, then we can
David Woodhouse3dfc8132009-07-04 19:11:08 +01002914 * take them out of the 1:1 domain later.
2915 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002916 if (!startup) {
2917 /*
2918 * If the device's dma_mask is less than the system's memory
2919 * size then this is not a candidate for identity mapping.
2920 */
David Woodhouse3bdb2592014-03-09 16:03:08 -07002921 u64 dma_mask = *dev->dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002922
David Woodhouse3bdb2592014-03-09 16:03:08 -07002923 if (dev->coherent_dma_mask &&
2924 dev->coherent_dma_mask < dma_mask)
2925 dma_mask = dev->coherent_dma_mask;
Chris Wright8fcc5372011-05-28 13:15:02 -05002926
David Woodhouse3bdb2592014-03-09 16:03:08 -07002927 return dma_mask >= dma_get_required_mask(dev);
Chris Wright8fcc5372011-05-28 13:15:02 -05002928 }
David Woodhouse6941af22009-07-04 18:24:27 +01002929
2930 return 1;
2931}
2932
David Woodhousecf04eee2014-03-21 16:49:04 +00002933static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw)
2934{
2935 int ret;
2936
2937 if (!iommu_should_identity_map(dev, 1))
2938 return 0;
2939
Joerg Roedel28ccce02015-07-21 14:45:31 +02002940 ret = domain_add_dev_info(si_domain, dev);
David Woodhousecf04eee2014-03-21 16:49:04 +00002941 if (!ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02002942 pr_info("%s identity mapping for device %s\n",
2943 hw ? "Hardware" : "Software", dev_name(dev));
David Woodhousecf04eee2014-03-21 16:49:04 +00002944 else if (ret == -ENODEV)
2945 /* device not associated with an iommu */
2946 ret = 0;
2947
2948 return ret;
2949}
2950
2951
Matt Kraai071e1372009-08-23 22:30:22 -07002952static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002953{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002954 struct pci_dev *pdev = NULL;
David Woodhousecf04eee2014-03-21 16:49:04 +00002955 struct dmar_drhd_unit *drhd;
2956 struct intel_iommu *iommu;
2957 struct device *dev;
2958 int i;
2959 int ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002960
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002961 for_each_pci_dev(pdev) {
David Woodhousecf04eee2014-03-21 16:49:04 +00002962 ret = dev_prepare_static_identity_mapping(&pdev->dev, hw);
2963 if (ret)
2964 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002965 }
2966
David Woodhousecf04eee2014-03-21 16:49:04 +00002967 for_each_active_iommu(iommu, drhd)
2968 for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) {
2969 struct acpi_device_physical_node *pn;
2970 struct acpi_device *adev;
2971
2972 if (dev->bus != &acpi_bus_type)
2973 continue;
Joerg Roedel86080cc2015-06-12 12:27:16 +02002974
David Woodhousecf04eee2014-03-21 16:49:04 +00002975 adev= to_acpi_device(dev);
2976 mutex_lock(&adev->physical_node_lock);
2977 list_for_each_entry(pn, &adev->physical_node_list, node) {
2978 ret = dev_prepare_static_identity_mapping(pn->dev, hw);
2979 if (ret)
2980 break;
2981 }
2982 mutex_unlock(&adev->physical_node_lock);
2983 if (ret)
2984 return ret;
2985 }
2986
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002987 return 0;
2988}
2989
Jiang Liuffebeb42014-11-09 22:48:02 +08002990static void intel_iommu_init_qi(struct intel_iommu *iommu)
2991{
2992 /*
2993 * Start from the sane iommu hardware state.
2994 * If the queued invalidation is already initialized by us
2995 * (for example, while enabling interrupt-remapping) then
2996 * we got the things already rolling from a sane state.
2997 */
2998 if (!iommu->qi) {
2999 /*
3000 * Clear any previous faults.
3001 */
3002 dmar_fault(-1, iommu);
3003 /*
3004 * Disable queued invalidation if supported and already enabled
3005 * before OS handover.
3006 */
3007 dmar_disable_qi(iommu);
3008 }
3009
3010 if (dmar_enable_qi(iommu)) {
3011 /*
3012 * Queued Invalidate not enabled, use Register Based Invalidate
3013 */
3014 iommu->flush.flush_context = __iommu_flush_context;
3015 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003016 pr_info("%s: Using Register based invalidation\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08003017 iommu->name);
3018 } else {
3019 iommu->flush.flush_context = qi_flush_context;
3020 iommu->flush.flush_iotlb = qi_flush_iotlb;
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003021 pr_info("%s: Using Queued invalidation\n", iommu->name);
Jiang Liuffebeb42014-11-09 22:48:02 +08003022 }
3023}
3024
Joerg Roedel091d42e2015-06-12 11:56:10 +02003025static int copy_context_table(struct intel_iommu *iommu,
Dan Williamsdfddb962015-10-09 18:16:46 -04003026 struct root_entry *old_re,
Joerg Roedel091d42e2015-06-12 11:56:10 +02003027 struct context_entry **tbl,
3028 int bus, bool ext)
3029{
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003030 int tbl_idx, pos = 0, idx, devfn, ret = 0, did;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003031 struct context_entry *new_ce = NULL, ce;
Dan Williamsdfddb962015-10-09 18:16:46 -04003032 struct context_entry *old_ce = NULL;
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003033 struct root_entry re;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003034 phys_addr_t old_ce_phys;
3035
3036 tbl_idx = ext ? bus * 2 : bus;
Dan Williamsdfddb962015-10-09 18:16:46 -04003037 memcpy(&re, old_re, sizeof(re));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003038
3039 for (devfn = 0; devfn < 256; devfn++) {
3040 /* First calculate the correct index */
3041 idx = (ext ? devfn * 2 : devfn) % 256;
3042
3043 if (idx == 0) {
3044 /* First save what we may have and clean up */
3045 if (new_ce) {
3046 tbl[tbl_idx] = new_ce;
3047 __iommu_flush_cache(iommu, new_ce,
3048 VTD_PAGE_SIZE);
3049 pos = 1;
3050 }
3051
3052 if (old_ce)
3053 iounmap(old_ce);
3054
3055 ret = 0;
3056 if (devfn < 0x80)
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003057 old_ce_phys = root_entry_lctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003058 else
Joerg Roedel543c8dc2015-08-13 11:56:59 +02003059 old_ce_phys = root_entry_uctp(&re);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003060
3061 if (!old_ce_phys) {
3062 if (ext && devfn == 0) {
3063 /* No LCTP, try UCTP */
3064 devfn = 0x7f;
3065 continue;
3066 } else {
3067 goto out;
3068 }
3069 }
3070
3071 ret = -ENOMEM;
Dan Williamsdfddb962015-10-09 18:16:46 -04003072 old_ce = memremap(old_ce_phys, PAGE_SIZE,
3073 MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003074 if (!old_ce)
3075 goto out;
3076
3077 new_ce = alloc_pgtable_page(iommu->node);
3078 if (!new_ce)
3079 goto out_unmap;
3080
3081 ret = 0;
3082 }
3083
3084 /* Now copy the context entry */
Dan Williamsdfddb962015-10-09 18:16:46 -04003085 memcpy(&ce, old_ce + idx, sizeof(ce));
Joerg Roedel091d42e2015-06-12 11:56:10 +02003086
Joerg Roedelcf484d02015-06-12 12:21:46 +02003087 if (!__context_present(&ce))
Joerg Roedel091d42e2015-06-12 11:56:10 +02003088 continue;
3089
Joerg Roedeldbcd8612015-06-12 12:02:09 +02003090 did = context_domain_id(&ce);
3091 if (did >= 0 && did < cap_ndoms(iommu->cap))
3092 set_bit(did, iommu->domain_ids);
3093
Joerg Roedelcf484d02015-06-12 12:21:46 +02003094 /*
3095 * We need a marker for copied context entries. This
3096 * marker needs to work for the old format as well as
3097 * for extended context entries.
3098 *
3099 * Bit 67 of the context entry is used. In the old
3100 * format this bit is available to software, in the
3101 * extended format it is the PGE bit, but PGE is ignored
3102 * by HW if PASIDs are disabled (and thus still
3103 * available).
3104 *
3105 * So disable PASIDs first and then mark the entry
3106 * copied. This means that we don't copy PASID
3107 * translations from the old kernel, but this is fine as
3108 * faults there are not fatal.
3109 */
3110 context_clear_pasid_enable(&ce);
3111 context_set_copied(&ce);
3112
Joerg Roedel091d42e2015-06-12 11:56:10 +02003113 new_ce[idx] = ce;
3114 }
3115
3116 tbl[tbl_idx + pos] = new_ce;
3117
3118 __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE);
3119
3120out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003121 memunmap(old_ce);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003122
3123out:
3124 return ret;
3125}
3126
3127static int copy_translation_tables(struct intel_iommu *iommu)
3128{
3129 struct context_entry **ctxt_tbls;
Dan Williamsdfddb962015-10-09 18:16:46 -04003130 struct root_entry *old_rt;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003131 phys_addr_t old_rt_phys;
3132 int ctxt_table_entries;
3133 unsigned long flags;
3134 u64 rtaddr_reg;
3135 int bus, ret;
Joerg Roedelc3361f22015-06-12 12:39:25 +02003136 bool new_ext, ext;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003137
3138 rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
3139 ext = !!(rtaddr_reg & DMA_RTADDR_RTT);
Joerg Roedelc3361f22015-06-12 12:39:25 +02003140 new_ext = !!ecap_ecs(iommu->ecap);
3141
3142 /*
3143 * The RTT bit can only be changed when translation is disabled,
3144 * but disabling translation means to open a window for data
3145 * corruption. So bail out and don't copy anything if we would
3146 * have to change the bit.
3147 */
3148 if (new_ext != ext)
3149 return -EINVAL;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003150
3151 old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
3152 if (!old_rt_phys)
3153 return -EINVAL;
3154
Dan Williamsdfddb962015-10-09 18:16:46 -04003155 old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003156 if (!old_rt)
3157 return -ENOMEM;
3158
3159 /* This is too big for the stack - allocate it from slab */
3160 ctxt_table_entries = ext ? 512 : 256;
3161 ret = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07003162 ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003163 if (!ctxt_tbls)
3164 goto out_unmap;
3165
3166 for (bus = 0; bus < 256; bus++) {
3167 ret = copy_context_table(iommu, &old_rt[bus],
3168 ctxt_tbls, bus, ext);
3169 if (ret) {
3170 pr_err("%s: Failed to copy context table for bus %d\n",
3171 iommu->name, bus);
3172 continue;
3173 }
3174 }
3175
3176 spin_lock_irqsave(&iommu->lock, flags);
3177
3178 /* Context tables are copied, now write them to the root_entry table */
3179 for (bus = 0; bus < 256; bus++) {
3180 int idx = ext ? bus * 2 : bus;
3181 u64 val;
3182
3183 if (ctxt_tbls[idx]) {
3184 val = virt_to_phys(ctxt_tbls[idx]) | 1;
3185 iommu->root_entry[bus].lo = val;
3186 }
3187
3188 if (!ext || !ctxt_tbls[idx + 1])
3189 continue;
3190
3191 val = virt_to_phys(ctxt_tbls[idx + 1]) | 1;
3192 iommu->root_entry[bus].hi = val;
3193 }
3194
3195 spin_unlock_irqrestore(&iommu->lock, flags);
3196
3197 kfree(ctxt_tbls);
3198
3199 __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE);
3200
3201 ret = 0;
3202
3203out_unmap:
Dan Williamsdfddb962015-10-09 18:16:46 -04003204 memunmap(old_rt);
Joerg Roedel091d42e2015-06-12 11:56:10 +02003205
3206 return ret;
3207}
3208
Joseph Cihulab7792602011-05-03 00:08:37 -07003209static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003210{
3211 struct dmar_drhd_unit *drhd;
3212 struct dmar_rmrr_unit *rmrr;
Joerg Roedela87f4912015-06-12 12:32:54 +02003213 bool copied_tables = false;
David Woodhouse832bd852014-03-07 15:08:36 +00003214 struct device *dev;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003215 struct intel_iommu *iommu;
Joerg Roedel13cf0172017-08-11 11:40:10 +02003216 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003217
3218 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003219 * for each drhd
3220 * allocate root
3221 * initialize and program root entry to not present
3222 * endfor
3223 */
3224 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08003225 /*
3226 * lock not needed as this is only incremented in the single
3227 * threaded kernel __init code path all other access are read
3228 * only
3229 */
Jiang Liu78d8e702014-11-09 22:47:57 +08003230 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) {
Mike Travis1b198bb2012-03-05 15:05:16 -08003231 g_num_of_iommus++;
3232 continue;
3233 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003234 pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED);
mark gross5e0d2a62008-03-04 15:22:08 -08003235 }
3236
Jiang Liuffebeb42014-11-09 22:48:02 +08003237 /* Preallocate enough resources for IOMMU hot-addition */
3238 if (g_num_of_iommus < DMAR_UNITS_SUPPORTED)
3239 g_num_of_iommus = DMAR_UNITS_SUPPORTED;
3240
Weidong Hand9630fe2008-12-08 11:06:32 +08003241 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
3242 GFP_KERNEL);
3243 if (!g_iommus) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003244 pr_err("Allocating global iommu array failed\n");
Weidong Hand9630fe2008-12-08 11:06:32 +08003245 ret = -ENOMEM;
3246 goto error;
3247 }
3248
Jiang Liu7c919772014-01-06 14:18:18 +08003249 for_each_active_iommu(iommu, drhd) {
Lu Baolu56283172018-07-14 15:46:54 +08003250 /*
3251 * Find the max pasid size of all IOMMU's in the system.
3252 * We need to ensure the system pasid table is no bigger
3253 * than the smallest supported.
3254 */
Lu Baolu765b6a92018-12-10 09:58:55 +08003255 if (pasid_supported(iommu)) {
Lu Baolu56283172018-07-14 15:46:54 +08003256 u32 temp = 2 << ecap_pss(iommu->ecap);
3257
3258 intel_pasid_max_id = min_t(u32, temp,
3259 intel_pasid_max_id);
3260 }
3261
Weidong Hand9630fe2008-12-08 11:06:32 +08003262 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003263
Joerg Roedelb63d80d2015-06-12 09:14:34 +02003264 intel_iommu_init_qi(iommu);
3265
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003266 ret = iommu_init_domains(iommu);
3267 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003268 goto free_iommu;
Suresh Siddhae61d98d2008-07-10 11:16:35 -07003269
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003270 init_translation_status(iommu);
3271
Joerg Roedel091d42e2015-06-12 11:56:10 +02003272 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
3273 iommu_disable_translation(iommu);
3274 clear_translation_pre_enabled(iommu);
3275 pr_warn("Translation was enabled for %s but we are not in kdump mode\n",
3276 iommu->name);
3277 }
Joerg Roedel4158c2e2015-06-12 10:14:02 +02003278
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003279 /*
3280 * TBD:
3281 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003282 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003283 */
3284 ret = iommu_alloc_root_entry(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003285 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003286 goto free_iommu;
Joerg Roedel5f0a7f72015-06-12 09:18:53 +02003287
Joerg Roedel091d42e2015-06-12 11:56:10 +02003288 if (translation_pre_enabled(iommu)) {
3289 pr_info("Translation already enabled - trying to copy translation structures\n");
3290
3291 ret = copy_translation_tables(iommu);
3292 if (ret) {
3293 /*
3294 * We found the IOMMU with translation
3295 * enabled - but failed to copy over the
3296 * old root-entry table. Try to proceed
3297 * by disabling translation now and
3298 * allocating a clean root-entry table.
3299 * This might cause DMAR faults, but
3300 * probably the dump will still succeed.
3301 */
3302 pr_err("Failed to copy translation tables from previous kernel for %s\n",
3303 iommu->name);
3304 iommu_disable_translation(iommu);
3305 clear_translation_pre_enabled(iommu);
3306 } else {
3307 pr_info("Copied translation tables from previous kernel for %s\n",
3308 iommu->name);
Joerg Roedela87f4912015-06-12 12:32:54 +02003309 copied_tables = true;
Joerg Roedel091d42e2015-06-12 11:56:10 +02003310 }
3311 }
3312
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003313 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01003314 hw_pass_through = 0;
David Woodhouse8a94ade2015-03-24 14:54:56 +00003315#ifdef CONFIG_INTEL_IOMMU_SVM
Lu Baolu765b6a92018-12-10 09:58:55 +08003316 if (pasid_supported(iommu))
Lu Baolud9737952018-07-14 15:47:02 +08003317 intel_svm_init(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00003318#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003319 }
3320
Joerg Roedela4c34ff2016-06-17 11:29:48 +02003321 /*
3322 * Now that qi is enabled on all iommus, set the root entry and flush
3323 * caches. This is required on some Intel X58 chipsets, otherwise the
3324 * flush_context function will loop forever and the boot hangs.
3325 */
3326 for_each_active_iommu(iommu, drhd) {
3327 iommu_flush_write_buffer(iommu);
3328 iommu_set_root_entry(iommu);
3329 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
3330 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
3331 }
3332
David Woodhouse19943b02009-08-04 16:19:20 +01003333 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07003334 iommu_identity_mapping |= IDENTMAP_ALL;
3335
Suresh Siddhad3f13812011-08-23 17:05:25 -07003336#ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07003337 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01003338#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07003339
Ashok Raj21e722c2017-01-30 09:39:53 -08003340 check_tylersburg_isoch();
3341
Joerg Roedel86080cc2015-06-12 12:27:16 +02003342 if (iommu_identity_mapping) {
3343 ret = si_domain_init(hw_pass_through);
3344 if (ret)
3345 goto free_iommu;
3346 }
3347
David Woodhousee0fc7e02009-09-30 09:12:17 -07003348
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003349 /*
Joerg Roedela87f4912015-06-12 12:32:54 +02003350 * If we copied translations from a previous kernel in the kdump
3351 * case, we can not assign the devices to domains now, as that
3352 * would eliminate the old mappings. So skip this part and defer
3353 * the assignment to device driver initialization time.
3354 */
3355 if (copied_tables)
3356 goto domains_done;
3357
3358 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003359 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003360 * identity mappings for rmrr, gfx, and isa and may fall back to static
3361 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003362 */
David Woodhouse19943b02009-08-04 16:19:20 +01003363 if (iommu_identity_mapping) {
3364 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
3365 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003366 pr_crit("Failed to setup IOMMU pass-through\n");
Jiang Liu989d51f2014-02-19 14:07:21 +08003367 goto free_iommu;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003368 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003369 }
David Woodhouse19943b02009-08-04 16:19:20 +01003370 /*
3371 * For each rmrr
3372 * for each dev attached to rmrr
3373 * do
3374 * locate drhd for dev, alloc domain for dev
3375 * allocate free domain
3376 * allocate page table entries for rmrr
3377 * if context not allocated for bus
3378 * allocate and init context
3379 * set present in root table for this bus
3380 * init context with domain, translation etc
3381 * endfor
3382 * endfor
3383 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003384 pr_info("Setting RMRR:\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003385 for_each_rmrr_units(rmrr) {
Jiang Liub683b232014-02-19 14:07:32 +08003386 /* some BIOS lists non-exist devices in DMAR table. */
3387 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
David Woodhouse832bd852014-03-07 15:08:36 +00003388 i, dev) {
David Woodhouse0b9d9752014-03-09 15:48:15 -07003389 ret = iommu_prepare_rmrr_dev(rmrr, dev);
David Woodhouse19943b02009-08-04 16:19:20 +01003390 if (ret)
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003391 pr_err("Mapping reserved region failed\n");
David Woodhouse19943b02009-08-04 16:19:20 +01003392 }
3393 }
3394
3395 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07003396
Joerg Roedela87f4912015-06-12 12:32:54 +02003397domains_done:
3398
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003399 /*
3400 * for each drhd
3401 * enable fault log
3402 * global invalidate context cache
3403 * global invalidate iotlb
3404 * enable translation
3405 */
Jiang Liu7c919772014-01-06 14:18:18 +08003406 for_each_iommu(iommu, drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07003407 if (drhd->ignored) {
3408 /*
3409 * we always have to disable PMRs or DMA may fail on
3410 * this device
3411 */
3412 if (force_on)
Jiang Liu7c919772014-01-06 14:18:18 +08003413 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003414 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07003415 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003416
3417 iommu_flush_write_buffer(iommu);
3418
David Woodhousea222a7f2015-10-07 23:35:18 +01003419#ifdef CONFIG_INTEL_IOMMU_SVM
Lu Baolu765b6a92018-12-10 09:58:55 +08003420 if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) {
David Woodhousea222a7f2015-10-07 23:35:18 +01003421 ret = intel_svm_enable_prq(iommu);
3422 if (ret)
3423 goto free_iommu;
3424 }
3425#endif
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003426 ret = dmar_set_interrupt(iommu);
3427 if (ret)
Jiang Liu989d51f2014-02-19 14:07:21 +08003428 goto free_iommu;
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07003429
Joerg Roedel8939ddf2015-06-12 14:40:01 +02003430 if (!translation_pre_enabled(iommu))
3431 iommu_enable_translation(iommu);
3432
David Woodhouseb94996c2009-09-19 15:28:12 -07003433 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003434 }
3435
3436 return 0;
Jiang Liu989d51f2014-02-19 14:07:21 +08003437
3438free_iommu:
Jiang Liuffebeb42014-11-09 22:48:02 +08003439 for_each_active_iommu(iommu, drhd) {
3440 disable_dmar_iommu(iommu);
Jiang Liua868e6b2014-01-06 14:18:20 +08003441 free_dmar_iommu(iommu);
Jiang Liuffebeb42014-11-09 22:48:02 +08003442 }
Joerg Roedel13cf0172017-08-11 11:40:10 +02003443
Weidong Hand9630fe2008-12-08 11:06:32 +08003444 kfree(g_iommus);
Joerg Roedel13cf0172017-08-11 11:40:10 +02003445
Jiang Liu989d51f2014-02-19 14:07:21 +08003446error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003447 return ret;
3448}
3449
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003450/* This takes a number of _MM_ pages, not VTD pages */
Omer Peleg2aac6302016-04-20 11:33:57 +03003451static unsigned long intel_alloc_iova(struct device *dev,
David Woodhouse875764d2009-06-28 21:20:51 +01003452 struct dmar_domain *domain,
3453 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003454{
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003455 unsigned long iova_pfn = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003456
David Woodhouse875764d2009-06-28 21:20:51 +01003457 /* Restrict dma_mask to the width that the iommu can handle */
3458 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
Robin Murphy8f6429c2015-07-16 19:40:12 +01003459 /* Ensure we reserve the whole size-aligned region */
3460 nrpages = __roundup_pow_of_two(nrpages);
David Woodhouse875764d2009-06-28 21:20:51 +01003461
3462 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003463 /*
3464 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07003465 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08003466 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003467 */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003468 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02003469 IOVA_PFN(DMA_BIT_MASK(32)), false);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003470 if (iova_pfn)
3471 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003472 }
Tomasz Nowicki538d5b32017-09-20 10:52:02 +02003473 iova_pfn = alloc_iova_fast(&domain->iovad, nrpages,
3474 IOVA_PFN(dma_mask), true);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003475 if (unlikely(!iova_pfn)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003476 pr_err("Allocating %ld-page iova for %s failed",
David Woodhouse207e3592014-03-09 16:12:32 -07003477 nrpages, dev_name(dev));
Omer Peleg2aac6302016-04-20 11:33:57 +03003478 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003479 }
3480
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003481 return iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003482}
3483
Lu Baolu9ddbfb42018-07-14 15:46:57 +08003484struct dmar_domain *get_valid_domain_for_dev(struct device *dev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003485{
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003486 struct dmar_domain *domain, *tmp;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003487 struct dmar_rmrr_unit *rmrr;
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003488 struct device *i_dev;
3489 int i, ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003490
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003491 domain = find_domain(dev);
3492 if (domain)
3493 goto out;
3494
3495 domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
3496 if (!domain)
3497 goto out;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003498
Joerg Roedelb1ce5b72015-09-23 19:16:01 +02003499 /* We have a new domain - setup possible RMRRs for the device */
3500 rcu_read_lock();
3501 for_each_rmrr_units(rmrr) {
3502 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
3503 i, i_dev) {
3504 if (i_dev != dev)
3505 continue;
3506
3507 ret = domain_prepare_identity_map(dev, domain,
3508 rmrr->base_address,
3509 rmrr->end_address);
3510 if (ret)
3511 dev_err(dev, "Mapping reserved region failed\n");
3512 }
3513 }
3514 rcu_read_unlock();
3515
Joerg Roedel1c5ebba2016-08-25 13:52:51 +02003516 tmp = set_domain_for_dev(dev, domain);
3517 if (!tmp || domain != tmp) {
3518 domain_exit(domain);
3519 domain = tmp;
3520 }
3521
3522out:
3523
3524 if (!domain)
3525 pr_err("Allocating domain for %s failed\n", dev_name(dev));
3526
3527
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003528 return domain;
3529}
3530
David Woodhouseecb509e2014-03-09 16:29:55 -07003531/* Check if the dev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01003532static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003533{
3534 int found;
3535
David Woodhouse3d891942014-03-06 15:59:26 +00003536 if (iommu_dummy(dev))
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003537 return 1;
3538
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003539 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003540 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003541
David Woodhouse9b226622014-03-09 14:03:28 -07003542 found = identity_mapping(dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003543 if (found) {
David Woodhouseecb509e2014-03-09 16:29:55 -07003544 if (iommu_should_identity_map(dev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003545 return 1;
3546 else {
3547 /*
3548 * 32 bit DMA is removed from si_domain and fall back
3549 * to non-identity mapping.
3550 */
Joerg Roedele6de0f82015-07-22 16:30:36 +02003551 dmar_remove_one_dev_info(si_domain, dev);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003552 pr_info("32bit %s uses non-identity mapping\n",
3553 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003554 return 0;
3555 }
3556 } else {
3557 /*
3558 * In case of a detached 64 bit DMA device from vm, the device
3559 * is put into si_domain for identity mapping.
3560 */
David Woodhouseecb509e2014-03-09 16:29:55 -07003561 if (iommu_should_identity_map(dev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003562 int ret;
Joerg Roedel28ccce02015-07-21 14:45:31 +02003563 ret = domain_add_dev_info(si_domain, dev);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003564 if (!ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003565 pr_info("64bit %s uses identity mapping\n",
3566 dev_name(dev));
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003567 return 1;
3568 }
3569 }
3570 }
3571
David Woodhouse1e4c64c2009-07-04 10:40:38 +01003572 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003573}
3574
David Woodhouse5040a912014-03-09 16:14:00 -07003575static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003576 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003577{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003578 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07003579 phys_addr_t start_paddr;
Omer Peleg2aac6302016-04-20 11:33:57 +03003580 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003581 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003582 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08003583 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07003584 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003585
3586 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003587
David Woodhouse5040a912014-03-09 16:14:00 -07003588 if (iommu_no_mapping(dev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003589 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003590
David Woodhouse5040a912014-03-09 16:14:00 -07003591 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003592 if (!domain)
3593 return 0;
3594
Weidong Han8c11e792008-12-08 15:29:22 +08003595 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01003596 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003597
Omer Peleg2aac6302016-04-20 11:33:57 +03003598 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask);
3599 if (!iova_pfn)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003600 goto error;
3601
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003602 /*
3603 * Check if DMAR supports zero-length reads on write only
3604 * mappings..
3605 */
3606 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003607 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003608 prot |= DMA_PTE_READ;
3609 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3610 prot |= DMA_PTE_WRITE;
3611 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003612 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003613 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02003614 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003615 * is not a big problem
3616 */
Omer Peleg2aac6302016-04-20 11:33:57 +03003617 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn),
Fenghua Yu33041ec2009-08-04 15:10:59 -07003618 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003619 if (ret)
3620 goto error;
3621
Omer Peleg2aac6302016-04-20 11:33:57 +03003622 start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT;
David Woodhouse03d6a242009-06-28 15:33:46 +01003623 start_paddr += paddr & ~PAGE_MASK;
3624 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003625
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003626error:
Omer Peleg2aac6302016-04-20 11:33:57 +03003627 if (iova_pfn)
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003628 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003629 pr_err("Device %s request: %zx@%llx dir %d --- failed\n",
David Woodhouse5040a912014-03-09 16:14:00 -07003630 dev_name(dev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003631 return 0;
3632}
3633
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003634static dma_addr_t intel_map_page(struct device *dev, struct page *page,
3635 unsigned long offset, size_t size,
3636 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003637 unsigned long attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003638{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003639 return __intel_map_single(dev, page_to_phys(page) + offset, size,
David Woodhouse46333e32014-03-10 20:01:21 -07003640 dir, *dev->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09003641}
3642
Omer Peleg769530e2016-04-20 11:33:25 +03003643static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003644{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003645 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01003646 unsigned long start_pfn, last_pfn;
Omer Peleg769530e2016-04-20 11:33:25 +03003647 unsigned long nrpages;
Omer Peleg2aac6302016-04-20 11:33:57 +03003648 unsigned long iova_pfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003649 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00003650 struct page *freelist;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003651
David Woodhouse73676832009-07-04 14:08:36 +01003652 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003653 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003654
David Woodhouse1525a292014-03-06 16:19:30 +00003655 domain = find_domain(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003656 BUG_ON(!domain);
3657
Weidong Han8c11e792008-12-08 15:29:22 +08003658 iommu = domain_get_iommu(domain);
3659
Omer Peleg2aac6302016-04-20 11:33:57 +03003660 iova_pfn = IOVA_PFN(dev_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003661
Omer Peleg769530e2016-04-20 11:33:25 +03003662 nrpages = aligned_nrpages(dev_addr, size);
Omer Peleg2aac6302016-04-20 11:33:57 +03003663 start_pfn = mm_to_dma_pfn(iova_pfn);
Omer Peleg769530e2016-04-20 11:33:25 +03003664 last_pfn = start_pfn + nrpages - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003665
David Woodhoused794dc92009-06-28 00:27:49 +01003666 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
David Woodhouse207e3592014-03-09 16:12:32 -07003667 dev_name(dev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003668
David Woodhouseea8ea462014-03-05 17:09:32 +00003669 freelist = domain_unmap(domain, start_pfn, last_pfn);
David Woodhoused794dc92009-06-28 00:27:49 +01003670
mark gross5e0d2a62008-03-04 15:22:08 -08003671 if (intel_iommu_strict) {
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02003672 iommu_flush_iotlb_psi(iommu, domain, start_pfn,
Omer Peleg769530e2016-04-20 11:33:25 +03003673 nrpages, !freelist, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08003674 /* free iova */
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003675 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages));
David Woodhouseea8ea462014-03-05 17:09:32 +00003676 dma_free_pagelist(freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003677 } else {
Joerg Roedel13cf0172017-08-11 11:40:10 +02003678 queue_iova(&domain->iovad, iova_pfn, nrpages,
3679 (unsigned long)freelist);
mark gross5e0d2a62008-03-04 15:22:08 -08003680 /*
3681 * queue up the release of the unmap to save the 1/6th of the
3682 * cpu used up by the iotlb flush operation...
3683 */
mark gross5e0d2a62008-03-04 15:22:08 -08003684 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003685}
3686
Jiang Liud41a4ad2014-07-11 14:19:34 +08003687static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
3688 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003689 unsigned long attrs)
Jiang Liud41a4ad2014-07-11 14:19:34 +08003690{
Omer Peleg769530e2016-04-20 11:33:25 +03003691 intel_unmap(dev, dev_addr, size);
Jiang Liud41a4ad2014-07-11 14:19:34 +08003692}
3693
David Woodhouse5040a912014-03-09 16:14:00 -07003694static void *intel_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003695 dma_addr_t *dma_handle, gfp_t flags,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003696 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003697{
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003698 struct page *page = NULL;
3699 int order;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003700
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003701 size = PAGE_ALIGN(size);
3702 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07003703
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003704 if (!iommu_no_mapping(dev))
3705 flags &= ~(GFP_DMA | GFP_DMA32);
3706 else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) {
3707 if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
3708 flags |= GFP_DMA;
3709 else
3710 flags |= GFP_DMA32;
3711 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003712
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003713 if (gfpflags_allow_blocking(flags)) {
3714 unsigned int count = size >> PAGE_SHIFT;
3715
Marek Szyprowskid834c5a2018-08-17 15:49:00 -07003716 page = dma_alloc_from_contiguous(dev, count, order,
3717 flags & __GFP_NOWARN);
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003718 if (page && iommu_no_mapping(dev) &&
3719 page_to_phys(page) + size > dev->coherent_dma_mask) {
3720 dma_release_from_contiguous(dev, page, count);
3721 page = NULL;
3722 }
3723 }
3724
3725 if (!page)
3726 page = alloc_pages(flags, order);
3727 if (!page)
3728 return NULL;
3729 memset(page_address(page), 0, size);
3730
3731 *dma_handle = __intel_map_single(dev, page_to_phys(page), size,
3732 DMA_BIDIRECTIONAL,
3733 dev->coherent_dma_mask);
3734 if (*dma_handle)
3735 return page_address(page);
3736 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3737 __free_pages(page, order);
3738
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003739 return NULL;
3740}
3741
David Woodhouse5040a912014-03-09 16:14:00 -07003742static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003743 dma_addr_t dma_handle, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003744{
Christoph Hellwig7ec916f2018-07-05 13:29:55 -06003745 int order;
3746 struct page *page = virt_to_page(vaddr);
3747
3748 size = PAGE_ALIGN(size);
3749 order = get_order(size);
3750
3751 intel_unmap(dev, dma_handle, size);
3752 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
3753 __free_pages(page, order);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003754}
3755
David Woodhouse5040a912014-03-09 16:14:00 -07003756static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003757 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003758 unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003759{
Omer Peleg769530e2016-04-20 11:33:25 +03003760 dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK;
3761 unsigned long nrpages = 0;
3762 struct scatterlist *sg;
3763 int i;
3764
3765 for_each_sg(sglist, sg, nelems, i) {
3766 nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg));
3767 }
3768
3769 intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003770}
3771
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003772static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003773 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003774{
3775 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003776 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003777
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003778 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003779 BUG_ON(!sg_page(sg));
Robin Murphy29a90b72017-09-28 15:14:01 +01003780 sg->dma_address = sg_phys(sg);
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003781 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003782 }
3783 return nelems;
3784}
3785
David Woodhouse5040a912014-03-09 16:14:00 -07003786static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07003787 enum dma_data_direction dir, unsigned long attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003788{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003789 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003790 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003791 size_t size = 0;
3792 int prot = 0;
Omer Peleg2aac6302016-04-20 11:33:57 +03003793 unsigned long iova_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003794 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003795 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003796 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003797 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003798
3799 BUG_ON(dir == DMA_NONE);
David Woodhouse5040a912014-03-09 16:14:00 -07003800 if (iommu_no_mapping(dev))
3801 return intel_nontranslate_map_sg(dev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003802
David Woodhouse5040a912014-03-09 16:14:00 -07003803 domain = get_valid_domain_for_dev(dev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003804 if (!domain)
3805 return 0;
3806
Weidong Han8c11e792008-12-08 15:29:22 +08003807 iommu = domain_get_iommu(domain);
3808
David Woodhouseb536d242009-06-28 14:49:31 +01003809 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003810 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003811
Omer Peleg2aac6302016-04-20 11:33:57 +03003812 iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size),
David Woodhouse5040a912014-03-09 16:14:00 -07003813 *dev->dma_mask);
Omer Peleg2aac6302016-04-20 11:33:57 +03003814 if (!iova_pfn) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003815 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003816 return 0;
3817 }
3818
3819 /*
3820 * Check if DMAR supports zero-length reads on write only
3821 * mappings..
3822 */
3823 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003824 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003825 prot |= DMA_PTE_READ;
3826 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3827 prot |= DMA_PTE_WRITE;
3828
Omer Peleg2aac6302016-04-20 11:33:57 +03003829 start_vpfn = mm_to_dma_pfn(iova_pfn);
David Woodhousee1605492009-06-29 11:17:38 +01003830
Fenghua Yuf5329592009-08-04 15:09:37 -07003831 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003832 if (unlikely(ret)) {
David Woodhousee1605492009-06-29 11:17:38 +01003833 dma_pte_free_pagetable(domain, start_vpfn,
David Dillowbc24c572017-06-28 19:42:23 -07003834 start_vpfn + size - 1,
3835 agaw_to_level(domain->agaw) + 1);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03003836 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size));
David Woodhousee1605492009-06-29 11:17:38 +01003837 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003838 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003839
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003840 return nelems;
3841}
3842
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003843static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3844{
3845 return !dma_addr;
3846}
3847
Christoph Hellwig02b4da52018-09-17 19:10:31 +02003848static const struct dma_map_ops intel_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02003849 .alloc = intel_alloc_coherent,
3850 .free = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003851 .map_sg = intel_map_sg,
3852 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003853 .map_page = intel_map_page,
3854 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003855 .mapping_error = intel_mapping_error,
Christoph Hellwigfec777c2018-03-19 11:38:15 +01003856 .dma_supported = dma_direct_supported,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003857};
3858
3859static inline int iommu_domain_cache_init(void)
3860{
3861 int ret = 0;
3862
3863 iommu_domain_cache = kmem_cache_create("iommu_domain",
3864 sizeof(struct dmar_domain),
3865 0,
3866 SLAB_HWCACHE_ALIGN,
3867
3868 NULL);
3869 if (!iommu_domain_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003870 pr_err("Couldn't create iommu_domain cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003871 ret = -ENOMEM;
3872 }
3873
3874 return ret;
3875}
3876
3877static inline int iommu_devinfo_cache_init(void)
3878{
3879 int ret = 0;
3880
3881 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3882 sizeof(struct device_domain_info),
3883 0,
3884 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003885 NULL);
3886 if (!iommu_devinfo_cache) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02003887 pr_err("Couldn't create devinfo cache\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003888 ret = -ENOMEM;
3889 }
3890
3891 return ret;
3892}
3893
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003894static int __init iommu_init_mempool(void)
3895{
3896 int ret;
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003897 ret = iova_cache_get();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003898 if (ret)
3899 return ret;
3900
3901 ret = iommu_domain_cache_init();
3902 if (ret)
3903 goto domain_error;
3904
3905 ret = iommu_devinfo_cache_init();
3906 if (!ret)
3907 return ret;
3908
3909 kmem_cache_destroy(iommu_domain_cache);
3910domain_error:
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003911 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003912
3913 return -ENOMEM;
3914}
3915
3916static void __init iommu_exit_mempool(void)
3917{
3918 kmem_cache_destroy(iommu_devinfo_cache);
3919 kmem_cache_destroy(iommu_domain_cache);
Sakari Ailusae1ff3d2015-07-13 14:31:28 +03003920 iova_cache_put();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003921}
3922
Dan Williams556ab452010-07-23 15:47:56 -07003923static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3924{
3925 struct dmar_drhd_unit *drhd;
3926 u32 vtbar;
3927 int rc;
3928
3929 /* We know that this device on this chipset has its own IOMMU.
3930 * If we find it under a different IOMMU, then the BIOS is lying
3931 * to us. Hope that the IOMMU for this device is actually
3932 * disabled, and it needs no translation...
3933 */
3934 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3935 if (rc) {
3936 /* "can't" happen */
3937 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3938 return;
3939 }
3940 vtbar &= 0xffff0000;
3941
3942 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3943 drhd = dmar_find_matched_drhd_unit(pdev);
3944 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3945 TAINT_FIRMWARE_WORKAROUND,
3946 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3947 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3948}
3949DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3950
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003951static void __init init_no_remapping_devices(void)
3952{
3953 struct dmar_drhd_unit *drhd;
David Woodhouse832bd852014-03-07 15:08:36 +00003954 struct device *dev;
Jiang Liub683b232014-02-19 14:07:32 +08003955 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003956
3957 for_each_drhd_unit(drhd) {
3958 if (!drhd->include_all) {
Jiang Liub683b232014-02-19 14:07:32 +08003959 for_each_active_dev_scope(drhd->devices,
3960 drhd->devices_cnt, i, dev)
3961 break;
David Woodhouse832bd852014-03-07 15:08:36 +00003962 /* ignore DMAR unit if no devices exist */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003963 if (i == drhd->devices_cnt)
3964 drhd->ignored = 1;
3965 }
3966 }
3967
Jiang Liu7c919772014-01-06 14:18:18 +08003968 for_each_active_drhd_unit(drhd) {
Jiang Liu7c919772014-01-06 14:18:18 +08003969 if (drhd->include_all)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003970 continue;
3971
Jiang Liub683b232014-02-19 14:07:32 +08003972 for_each_active_dev_scope(drhd->devices,
3973 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003974 if (!dev_is_pci(dev) || !IS_GFX_DEVICE(to_pci_dev(dev)))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003975 break;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003976 if (i < drhd->devices_cnt)
3977 continue;
3978
David Woodhousec0771df2011-10-14 20:59:46 +01003979 /* This IOMMU has *only* gfx devices. Either bypass it or
3980 set the gfx_mapped flag, as appropriate */
3981 if (dmar_map_gfx) {
3982 intel_iommu_gfx_mapped = 1;
3983 } else {
3984 drhd->ignored = 1;
Jiang Liub683b232014-02-19 14:07:32 +08003985 for_each_active_dev_scope(drhd->devices,
3986 drhd->devices_cnt, i, dev)
David Woodhouse832bd852014-03-07 15:08:36 +00003987 dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003988 }
3989 }
3990}
3991
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003992#ifdef CONFIG_SUSPEND
3993static int init_iommu_hw(void)
3994{
3995 struct dmar_drhd_unit *drhd;
3996 struct intel_iommu *iommu = NULL;
3997
3998 for_each_active_iommu(iommu, drhd)
3999 if (iommu->qi)
4000 dmar_reenable_qi(iommu);
4001
Joseph Cihulab7792602011-05-03 00:08:37 -07004002 for_each_iommu(iommu, drhd) {
4003 if (drhd->ignored) {
4004 /*
4005 * we always have to disable PMRs or DMA may fail on
4006 * this device
4007 */
4008 if (force_on)
4009 iommu_disable_protect_mem_regions(iommu);
4010 continue;
4011 }
4012
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004013 iommu_flush_write_buffer(iommu);
4014
4015 iommu_set_root_entry(iommu);
4016
4017 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004018 DMA_CCMD_GLOBAL_INVL);
Jiang Liu2a41cce2014-07-11 14:19:33 +08004019 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4020 iommu_enable_translation(iommu);
David Woodhouseb94996c2009-09-19 15:28:12 -07004021 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004022 }
4023
4024 return 0;
4025}
4026
4027static void iommu_flush_all(void)
4028{
4029 struct dmar_drhd_unit *drhd;
4030 struct intel_iommu *iommu;
4031
4032 for_each_active_iommu(iommu, drhd) {
4033 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004034 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004035 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01004036 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004037 }
4038}
4039
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004040static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004041{
4042 struct dmar_drhd_unit *drhd;
4043 struct intel_iommu *iommu = NULL;
4044 unsigned long flag;
4045
4046 for_each_active_iommu(iommu, drhd) {
Kees Cook6396bb22018-06-12 14:03:40 -07004047 iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32),
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004048 GFP_ATOMIC);
4049 if (!iommu->iommu_state)
4050 goto nomem;
4051 }
4052
4053 iommu_flush_all();
4054
4055 for_each_active_iommu(iommu, drhd) {
4056 iommu_disable_translation(iommu);
4057
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004058 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004059
4060 iommu->iommu_state[SR_DMAR_FECTL_REG] =
4061 readl(iommu->reg + DMAR_FECTL_REG);
4062 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
4063 readl(iommu->reg + DMAR_FEDATA_REG);
4064 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
4065 readl(iommu->reg + DMAR_FEADDR_REG);
4066 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
4067 readl(iommu->reg + DMAR_FEUADDR_REG);
4068
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004069 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004070 }
4071 return 0;
4072
4073nomem:
4074 for_each_active_iommu(iommu, drhd)
4075 kfree(iommu->iommu_state);
4076
4077 return -ENOMEM;
4078}
4079
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004080static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004081{
4082 struct dmar_drhd_unit *drhd;
4083 struct intel_iommu *iommu = NULL;
4084 unsigned long flag;
4085
4086 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07004087 if (force_on)
4088 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
4089 else
4090 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004091 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004092 }
4093
4094 for_each_active_iommu(iommu, drhd) {
4095
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004096 raw_spin_lock_irqsave(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004097
4098 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
4099 iommu->reg + DMAR_FECTL_REG);
4100 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
4101 iommu->reg + DMAR_FEDATA_REG);
4102 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
4103 iommu->reg + DMAR_FEADDR_REG);
4104 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
4105 iommu->reg + DMAR_FEUADDR_REG);
4106
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +02004107 raw_spin_unlock_irqrestore(&iommu->register_lock, flag);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004108 }
4109
4110 for_each_active_iommu(iommu, drhd)
4111 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004112}
4113
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004114static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004115 .resume = iommu_resume,
4116 .suspend = iommu_suspend,
4117};
4118
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004119static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004120{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004121 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004122}
4123
4124#else
Rafael J. Wysocki99592ba2011-06-07 21:32:31 +02004125static inline void init_iommu_pm_ops(void) {}
Fenghua Yuf59c7b62009-03-27 14:22:42 -07004126#endif /* CONFIG_PM */
4127
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004128
Jiang Liuc2a0b532014-11-09 22:47:56 +08004129int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004130{
4131 struct acpi_dmar_reserved_memory *rmrr;
Eric Auger0659b8d2017-01-19 20:57:53 +00004132 int prot = DMA_PTE_READ|DMA_PTE_WRITE;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004133 struct dmar_rmrr_unit *rmrru;
Eric Auger0659b8d2017-01-19 20:57:53 +00004134 size_t length;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004135
4136 rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL);
4137 if (!rmrru)
Eric Auger0659b8d2017-01-19 20:57:53 +00004138 goto out;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004139
4140 rmrru->hdr = header;
4141 rmrr = (struct acpi_dmar_reserved_memory *)header;
4142 rmrru->base_address = rmrr->base_address;
4143 rmrru->end_address = rmrr->end_address;
Eric Auger0659b8d2017-01-19 20:57:53 +00004144
4145 length = rmrr->end_address - rmrr->base_address + 1;
4146 rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length, prot,
4147 IOMMU_RESV_DIRECT);
4148 if (!rmrru->resv)
4149 goto free_rmrru;
4150
Jiang Liu2e455282014-02-19 14:07:36 +08004151 rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1),
4152 ((void *)rmrr) + rmrr->header.length,
4153 &rmrru->devices_cnt);
Eric Auger0659b8d2017-01-19 20:57:53 +00004154 if (rmrru->devices_cnt && rmrru->devices == NULL)
4155 goto free_all;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004156
Jiang Liu2e455282014-02-19 14:07:36 +08004157 list_add(&rmrru->list, &dmar_rmrr_units);
4158
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004159 return 0;
Eric Auger0659b8d2017-01-19 20:57:53 +00004160free_all:
4161 kfree(rmrru->resv);
4162free_rmrru:
4163 kfree(rmrru);
4164out:
4165 return -ENOMEM;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004166}
4167
Jiang Liu6b197242014-11-09 22:47:58 +08004168static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr)
4169{
4170 struct dmar_atsr_unit *atsru;
4171 struct acpi_dmar_atsr *tmp;
4172
4173 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4174 tmp = (struct acpi_dmar_atsr *)atsru->hdr;
4175 if (atsr->segment != tmp->segment)
4176 continue;
4177 if (atsr->header.length != tmp->header.length)
4178 continue;
4179 if (memcmp(atsr, tmp, atsr->header.length) == 0)
4180 return atsru;
4181 }
4182
4183 return NULL;
4184}
4185
4186int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004187{
4188 struct acpi_dmar_atsr *atsr;
4189 struct dmar_atsr_unit *atsru;
4190
Thomas Gleixnerb608fe32017-05-16 20:42:41 +02004191 if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
Jiang Liu6b197242014-11-09 22:47:58 +08004192 return 0;
4193
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004194 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
Jiang Liu6b197242014-11-09 22:47:58 +08004195 atsru = dmar_find_atsr(atsr);
4196 if (atsru)
4197 return 0;
4198
4199 atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004200 if (!atsru)
4201 return -ENOMEM;
4202
Jiang Liu6b197242014-11-09 22:47:58 +08004203 /*
4204 * If memory is allocated from slab by ACPI _DSM method, we need to
4205 * copy the memory content because the memory buffer will be freed
4206 * on return.
4207 */
4208 atsru->hdr = (void *)(atsru + 1);
4209 memcpy(atsru->hdr, hdr, hdr->length);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004210 atsru->include_all = atsr->flags & 0x1;
Jiang Liu2e455282014-02-19 14:07:36 +08004211 if (!atsru->include_all) {
4212 atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1),
4213 (void *)atsr + atsr->header.length,
4214 &atsru->devices_cnt);
4215 if (atsru->devices_cnt && atsru->devices == NULL) {
4216 kfree(atsru);
4217 return -ENOMEM;
4218 }
4219 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004220
Jiang Liu0e2426122014-02-19 14:07:34 +08004221 list_add_rcu(&atsru->list, &dmar_atsr_units);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004222
4223 return 0;
4224}
4225
Jiang Liu9bdc5312014-01-06 14:18:27 +08004226static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru)
4227{
4228 dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt);
4229 kfree(atsru);
4230}
4231
Jiang Liu6b197242014-11-09 22:47:58 +08004232int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4233{
4234 struct acpi_dmar_atsr *atsr;
4235 struct dmar_atsr_unit *atsru;
4236
4237 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4238 atsru = dmar_find_atsr(atsr);
4239 if (atsru) {
4240 list_del_rcu(&atsru->list);
4241 synchronize_rcu();
4242 intel_iommu_free_atsr(atsru);
4243 }
4244
4245 return 0;
4246}
4247
4248int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg)
4249{
4250 int i;
4251 struct device *dev;
4252 struct acpi_dmar_atsr *atsr;
4253 struct dmar_atsr_unit *atsru;
4254
4255 atsr = container_of(hdr, struct acpi_dmar_atsr, header);
4256 atsru = dmar_find_atsr(atsr);
4257 if (!atsru)
4258 return 0;
4259
Linus Torvalds194dc872016-07-27 20:03:31 -07004260 if (!atsru->include_all && atsru->devices && atsru->devices_cnt) {
Jiang Liu6b197242014-11-09 22:47:58 +08004261 for_each_active_dev_scope(atsru->devices, atsru->devices_cnt,
4262 i, dev)
4263 return -EBUSY;
Linus Torvalds194dc872016-07-27 20:03:31 -07004264 }
Jiang Liu6b197242014-11-09 22:47:58 +08004265
4266 return 0;
4267}
4268
Jiang Liuffebeb42014-11-09 22:48:02 +08004269static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
4270{
4271 int sp, ret = 0;
4272 struct intel_iommu *iommu = dmaru->iommu;
4273
4274 if (g_iommus[iommu->seq_id])
4275 return 0;
4276
4277 if (hw_pass_through && !ecap_pass_through(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004278 pr_warn("%s: Doesn't support hardware pass through.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004279 iommu->name);
4280 return -ENXIO;
4281 }
4282 if (!ecap_sc_support(iommu->ecap) &&
4283 domain_update_iommu_snooping(iommu)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004284 pr_warn("%s: Doesn't support snooping.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004285 iommu->name);
4286 return -ENXIO;
4287 }
4288 sp = domain_update_iommu_superpage(iommu) - 1;
4289 if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004290 pr_warn("%s: Doesn't support large page.\n",
Jiang Liuffebeb42014-11-09 22:48:02 +08004291 iommu->name);
4292 return -ENXIO;
4293 }
4294
4295 /*
4296 * Disable translation if already enabled prior to OS handover.
4297 */
4298 if (iommu->gcmd & DMA_GCMD_TE)
4299 iommu_disable_translation(iommu);
4300
4301 g_iommus[iommu->seq_id] = iommu;
4302 ret = iommu_init_domains(iommu);
4303 if (ret == 0)
4304 ret = iommu_alloc_root_entry(iommu);
4305 if (ret)
4306 goto out;
4307
David Woodhouse8a94ade2015-03-24 14:54:56 +00004308#ifdef CONFIG_INTEL_IOMMU_SVM
Lu Baolu765b6a92018-12-10 09:58:55 +08004309 if (pasid_supported(iommu))
Lu Baolud9737952018-07-14 15:47:02 +08004310 intel_svm_init(iommu);
David Woodhouse8a94ade2015-03-24 14:54:56 +00004311#endif
4312
Jiang Liuffebeb42014-11-09 22:48:02 +08004313 if (dmaru->ignored) {
4314 /*
4315 * we always have to disable PMRs or DMA may fail on this device
4316 */
4317 if (force_on)
4318 iommu_disable_protect_mem_regions(iommu);
4319 return 0;
4320 }
4321
4322 intel_iommu_init_qi(iommu);
4323 iommu_flush_write_buffer(iommu);
David Woodhousea222a7f2015-10-07 23:35:18 +01004324
4325#ifdef CONFIG_INTEL_IOMMU_SVM
Lu Baolu765b6a92018-12-10 09:58:55 +08004326 if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) {
David Woodhousea222a7f2015-10-07 23:35:18 +01004327 ret = intel_svm_enable_prq(iommu);
4328 if (ret)
4329 goto disable_iommu;
4330 }
4331#endif
Jiang Liuffebeb42014-11-09 22:48:02 +08004332 ret = dmar_set_interrupt(iommu);
4333 if (ret)
4334 goto disable_iommu;
4335
4336 iommu_set_root_entry(iommu);
4337 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
4338 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
4339 iommu_enable_translation(iommu);
4340
Jiang Liuffebeb42014-11-09 22:48:02 +08004341 iommu_disable_protect_mem_regions(iommu);
4342 return 0;
4343
4344disable_iommu:
4345 disable_dmar_iommu(iommu);
4346out:
4347 free_dmar_iommu(iommu);
4348 return ret;
4349}
4350
Jiang Liu6b197242014-11-09 22:47:58 +08004351int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
4352{
Jiang Liuffebeb42014-11-09 22:48:02 +08004353 int ret = 0;
4354 struct intel_iommu *iommu = dmaru->iommu;
4355
4356 if (!intel_iommu_enabled)
4357 return 0;
4358 if (iommu == NULL)
4359 return -EINVAL;
4360
4361 if (insert) {
4362 ret = intel_iommu_add(dmaru);
4363 } else {
4364 disable_dmar_iommu(iommu);
4365 free_dmar_iommu(iommu);
4366 }
4367
4368 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08004369}
4370
Jiang Liu9bdc5312014-01-06 14:18:27 +08004371static void intel_iommu_free_dmars(void)
4372{
4373 struct dmar_rmrr_unit *rmrru, *rmrr_n;
4374 struct dmar_atsr_unit *atsru, *atsr_n;
4375
4376 list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) {
4377 list_del(&rmrru->list);
4378 dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt);
Eric Auger0659b8d2017-01-19 20:57:53 +00004379 kfree(rmrru->resv);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004380 kfree(rmrru);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004381 }
4382
Jiang Liu9bdc5312014-01-06 14:18:27 +08004383 list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) {
4384 list_del(&atsru->list);
4385 intel_iommu_free_atsr(atsru);
4386 }
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004387}
4388
4389int dmar_find_matched_atsr_unit(struct pci_dev *dev)
4390{
Jiang Liub683b232014-02-19 14:07:32 +08004391 int i, ret = 1;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004392 struct pci_bus *bus;
David Woodhouse832bd852014-03-07 15:08:36 +00004393 struct pci_dev *bridge = NULL;
4394 struct device *tmp;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004395 struct acpi_dmar_atsr *atsr;
4396 struct dmar_atsr_unit *atsru;
4397
4398 dev = pci_physfn(dev);
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004399 for (bus = dev->bus; bus; bus = bus->parent) {
Jiang Liub5f82dd2014-02-19 14:07:31 +08004400 bridge = bus->self;
David Woodhoused14053b32015-10-15 09:28:06 +01004401 /* If it's an integrated device, allow ATS */
4402 if (!bridge)
4403 return 1;
4404 /* Connected via non-PCIe: no ATS */
4405 if (!pci_is_pcie(bridge) ||
Yijing Wang62f87c02012-07-24 17:20:03 +08004406 pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004407 return 0;
David Woodhoused14053b32015-10-15 09:28:06 +01004408 /* If we found the root port, look it up in the ATSR */
Jiang Liub5f82dd2014-02-19 14:07:31 +08004409 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT)
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004410 break;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004411 }
4412
Jiang Liu0e2426122014-02-19 14:07:34 +08004413 rcu_read_lock();
Jiang Liub5f82dd2014-02-19 14:07:31 +08004414 list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) {
4415 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4416 if (atsr->segment != pci_domain_nr(dev->bus))
4417 continue;
4418
Jiang Liub683b232014-02-19 14:07:32 +08004419 for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp)
David Woodhouse832bd852014-03-07 15:08:36 +00004420 if (tmp == &bridge->dev)
Jiang Liub683b232014-02-19 14:07:32 +08004421 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004422
4423 if (atsru->include_all)
Jiang Liub683b232014-02-19 14:07:32 +08004424 goto out;
Jiang Liub5f82dd2014-02-19 14:07:31 +08004425 }
Jiang Liub683b232014-02-19 14:07:32 +08004426 ret = 0;
4427out:
Jiang Liu0e2426122014-02-19 14:07:34 +08004428 rcu_read_unlock();
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004429
Jiang Liub683b232014-02-19 14:07:32 +08004430 return ret;
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004431}
4432
Jiang Liu59ce0512014-02-19 14:07:35 +08004433int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info)
4434{
4435 int ret = 0;
4436 struct dmar_rmrr_unit *rmrru;
4437 struct dmar_atsr_unit *atsru;
4438 struct acpi_dmar_atsr *atsr;
4439 struct acpi_dmar_reserved_memory *rmrr;
4440
Thomas Gleixnerb608fe32017-05-16 20:42:41 +02004441 if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
Jiang Liu59ce0512014-02-19 14:07:35 +08004442 return 0;
4443
4444 list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
4445 rmrr = container_of(rmrru->hdr,
4446 struct acpi_dmar_reserved_memory, header);
4447 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4448 ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1),
4449 ((void *)rmrr) + rmrr->header.length,
4450 rmrr->segment, rmrru->devices,
4451 rmrru->devices_cnt);
Jiang Liu27e24952014-06-20 15:08:06 +08004452 if(ret < 0)
Jiang Liu59ce0512014-02-19 14:07:35 +08004453 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004454 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu27e24952014-06-20 15:08:06 +08004455 dmar_remove_dev_scope(info, rmrr->segment,
4456 rmrru->devices, rmrru->devices_cnt);
Jiang Liu59ce0512014-02-19 14:07:35 +08004457 }
4458 }
4459
4460 list_for_each_entry(atsru, &dmar_atsr_units, list) {
4461 if (atsru->include_all)
4462 continue;
4463
4464 atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header);
4465 if (info->event == BUS_NOTIFY_ADD_DEVICE) {
4466 ret = dmar_insert_dev_scope(info, (void *)(atsr + 1),
4467 (void *)atsr + atsr->header.length,
4468 atsr->segment, atsru->devices,
4469 atsru->devices_cnt);
4470 if (ret > 0)
4471 break;
4472 else if(ret < 0)
4473 return ret;
Joerg Roedele6a8c9b2016-02-29 23:49:47 +01004474 } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) {
Jiang Liu59ce0512014-02-19 14:07:35 +08004475 if (dmar_remove_dev_scope(info, atsr->segment,
4476 atsru->devices, atsru->devices_cnt))
4477 break;
4478 }
4479 }
4480
4481 return 0;
4482}
4483
Fenghua Yu99dcade2009-11-11 07:23:06 -08004484/*
4485 * Here we only respond to action of unbound device from driver.
4486 *
4487 * Added device is not attached to its DMAR domain here yet. That will happen
4488 * when mapping the device to iova.
4489 */
4490static int device_notifier(struct notifier_block *nb,
4491 unsigned long action, void *data)
4492{
4493 struct device *dev = data;
Fenghua Yu99dcade2009-11-11 07:23:06 -08004494 struct dmar_domain *domain;
4495
David Woodhouse3d891942014-03-06 15:59:26 +00004496 if (iommu_dummy(dev))
David Woodhouse44cd6132009-12-02 10:18:30 +00004497 return 0;
4498
Joerg Roedel1196c2f2014-09-30 13:02:03 +02004499 if (action != BUS_NOTIFY_REMOVED_DEVICE)
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004500 return 0;
4501
David Woodhouse1525a292014-03-06 16:19:30 +00004502 domain = find_domain(dev);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004503 if (!domain)
4504 return 0;
4505
Joerg Roedele6de0f82015-07-22 16:30:36 +02004506 dmar_remove_one_dev_info(domain, dev);
Jiang Liuab8dfe22014-07-11 14:19:27 +08004507 if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices))
Jiang Liu7e7dfab2014-02-19 14:07:23 +08004508 domain_exit(domain);
Alex Williamsona97590e2011-03-04 14:52:16 -07004509
Fenghua Yu99dcade2009-11-11 07:23:06 -08004510 return 0;
4511}
4512
4513static struct notifier_block device_nb = {
4514 .notifier_call = device_notifier,
4515};
4516
Jiang Liu75f05562014-02-19 14:07:37 +08004517static int intel_iommu_memory_notifier(struct notifier_block *nb,
4518 unsigned long val, void *v)
4519{
4520 struct memory_notify *mhp = v;
4521 unsigned long long start, end;
4522 unsigned long start_vpfn, last_vpfn;
4523
4524 switch (val) {
4525 case MEM_GOING_ONLINE:
4526 start = mhp->start_pfn << PAGE_SHIFT;
4527 end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1;
4528 if (iommu_domain_identity_map(si_domain, start, end)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004529 pr_warn("Failed to build identity map for [%llx-%llx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004530 start, end);
4531 return NOTIFY_BAD;
4532 }
4533 break;
4534
4535 case MEM_OFFLINE:
4536 case MEM_CANCEL_ONLINE:
4537 start_vpfn = mm_to_dma_pfn(mhp->start_pfn);
4538 last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1);
4539 while (start_vpfn <= last_vpfn) {
4540 struct iova *iova;
4541 struct dmar_drhd_unit *drhd;
4542 struct intel_iommu *iommu;
David Woodhouseea8ea462014-03-05 17:09:32 +00004543 struct page *freelist;
Jiang Liu75f05562014-02-19 14:07:37 +08004544
4545 iova = find_iova(&si_domain->iovad, start_vpfn);
4546 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004547 pr_debug("Failed get IOVA for PFN %lx\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004548 start_vpfn);
4549 break;
4550 }
4551
4552 iova = split_and_remove_iova(&si_domain->iovad, iova,
4553 start_vpfn, last_vpfn);
4554 if (iova == NULL) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004555 pr_warn("Failed to split IOVA PFN [%lx-%lx]\n",
Jiang Liu75f05562014-02-19 14:07:37 +08004556 start_vpfn, last_vpfn);
4557 return NOTIFY_BAD;
4558 }
4559
David Woodhouseea8ea462014-03-05 17:09:32 +00004560 freelist = domain_unmap(si_domain, iova->pfn_lo,
4561 iova->pfn_hi);
4562
Jiang Liu75f05562014-02-19 14:07:37 +08004563 rcu_read_lock();
4564 for_each_active_iommu(iommu, drhd)
Joerg Roedela1ddcbe2015-07-21 15:20:32 +02004565 iommu_flush_iotlb_psi(iommu, si_domain,
Jiang Liua156ef92014-07-11 14:19:36 +08004566 iova->pfn_lo, iova_size(iova),
David Woodhouseea8ea462014-03-05 17:09:32 +00004567 !freelist, 0);
Jiang Liu75f05562014-02-19 14:07:37 +08004568 rcu_read_unlock();
David Woodhouseea8ea462014-03-05 17:09:32 +00004569 dma_free_pagelist(freelist);
Jiang Liu75f05562014-02-19 14:07:37 +08004570
4571 start_vpfn = iova->pfn_hi + 1;
4572 free_iova_mem(iova);
4573 }
4574 break;
4575 }
4576
4577 return NOTIFY_OK;
4578}
4579
4580static struct notifier_block intel_iommu_memory_nb = {
4581 .notifier_call = intel_iommu_memory_notifier,
4582 .priority = 0
4583};
4584
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004585static void free_all_cpu_cached_iovas(unsigned int cpu)
4586{
4587 int i;
4588
4589 for (i = 0; i < g_num_of_iommus; i++) {
4590 struct intel_iommu *iommu = g_iommus[i];
4591 struct dmar_domain *domain;
Aaron Campbell0caa7612016-07-02 21:23:24 -03004592 int did;
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004593
4594 if (!iommu)
4595 continue;
4596
Jan Niehusmann3bd4f912016-06-06 14:20:11 +02004597 for (did = 0; did < cap_ndoms(iommu->cap); did++) {
Aaron Campbell0caa7612016-07-02 21:23:24 -03004598 domain = get_iommu_domain(iommu, (u16)did);
Omer Peleg22e2f9f2016-04-20 11:34:11 +03004599
4600 if (!domain)
4601 continue;
4602 free_cpu_cached_iovas(cpu, &domain->iovad);
4603 }
4604 }
4605}
4606
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004607static int intel_iommu_cpu_dead(unsigned int cpu)
Omer Pelegaa473242016-04-20 11:33:02 +03004608{
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004609 free_all_cpu_cached_iovas(cpu);
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004610 return 0;
Omer Pelegaa473242016-04-20 11:33:02 +03004611}
4612
Joerg Roedel161b28a2017-03-28 17:04:52 +02004613static void intel_disable_iommus(void)
4614{
4615 struct intel_iommu *iommu = NULL;
4616 struct dmar_drhd_unit *drhd;
4617
4618 for_each_iommu(iommu, drhd)
4619 iommu_disable_translation(iommu);
4620}
4621
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004622static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev)
4623{
Joerg Roedel2926a2aa2017-08-14 17:19:26 +02004624 struct iommu_device *iommu_dev = dev_to_iommu_device(dev);
4625
4626 return container_of(iommu_dev, struct intel_iommu, iommu);
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004627}
4628
Alex Williamsona5459cf2014-06-12 16:12:31 -06004629static ssize_t intel_iommu_show_version(struct device *dev,
4630 struct device_attribute *attr,
4631 char *buf)
4632{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004633 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004634 u32 ver = readl(iommu->reg + DMAR_VER_REG);
4635 return sprintf(buf, "%d:%d\n",
4636 DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver));
4637}
4638static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL);
4639
4640static ssize_t intel_iommu_show_address(struct device *dev,
4641 struct device_attribute *attr,
4642 char *buf)
4643{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004644 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004645 return sprintf(buf, "%llx\n", iommu->reg_phys);
4646}
4647static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL);
4648
4649static ssize_t intel_iommu_show_cap(struct device *dev,
4650 struct device_attribute *attr,
4651 char *buf)
4652{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004653 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004654 return sprintf(buf, "%llx\n", iommu->cap);
4655}
4656static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL);
4657
4658static ssize_t intel_iommu_show_ecap(struct device *dev,
4659 struct device_attribute *attr,
4660 char *buf)
4661{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004662 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06004663 return sprintf(buf, "%llx\n", iommu->ecap);
4664}
4665static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL);
4666
Alex Williamson2238c082015-07-14 15:24:53 -06004667static ssize_t intel_iommu_show_ndoms(struct device *dev,
4668 struct device_attribute *attr,
4669 char *buf)
4670{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004671 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamson2238c082015-07-14 15:24:53 -06004672 return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap));
4673}
4674static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL);
4675
4676static ssize_t intel_iommu_show_ndoms_used(struct device *dev,
4677 struct device_attribute *attr,
4678 char *buf)
4679{
Joerg Roedela7fdb6e2017-02-28 13:57:18 +01004680 struct intel_iommu *iommu = dev_to_intel_iommu(dev);
Alex Williamson2238c082015-07-14 15:24:53 -06004681 return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids,
4682 cap_ndoms(iommu->cap)));
4683}
4684static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL);
4685
Alex Williamsona5459cf2014-06-12 16:12:31 -06004686static struct attribute *intel_iommu_attrs[] = {
4687 &dev_attr_version.attr,
4688 &dev_attr_address.attr,
4689 &dev_attr_cap.attr,
4690 &dev_attr_ecap.attr,
Alex Williamson2238c082015-07-14 15:24:53 -06004691 &dev_attr_domains_supported.attr,
4692 &dev_attr_domains_used.attr,
Alex Williamsona5459cf2014-06-12 16:12:31 -06004693 NULL,
4694};
4695
4696static struct attribute_group intel_iommu_group = {
4697 .name = "intel-iommu",
4698 .attrs = intel_iommu_attrs,
4699};
4700
4701const struct attribute_group *intel_iommu_groups[] = {
4702 &intel_iommu_group,
4703 NULL,
4704};
4705
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004706int __init intel_iommu_init(void)
4707{
Jiang Liu9bdc5312014-01-06 14:18:27 +08004708 int ret = -ENODEV;
Takao Indoh3a93c842013-04-23 17:35:03 +09004709 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +08004710 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004711
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004712 /* VT-d is required for a TXT/tboot launch, so enforce that */
4713 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004714
Jiang Liu3a5670e2014-02-19 14:07:33 +08004715 if (iommu_init_mempool()) {
4716 if (force_on)
4717 panic("tboot: Failed to initialize iommu memory\n");
4718 return -ENOMEM;
4719 }
4720
4721 down_write(&dmar_global_lock);
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004722 if (dmar_table_init()) {
4723 if (force_on)
4724 panic("tboot: Failed to initialize DMAR table\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004725 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004726 }
4727
Suresh Siddhac2c72862011-08-23 17:05:19 -07004728 if (dmar_dev_scope_init() < 0) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004729 if (force_on)
4730 panic("tboot: Failed to initialize DMAR device scope\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004731 goto out_free_dmar;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004732 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07004733
Joerg Roedelec154bf2017-10-06 15:00:53 +02004734 up_write(&dmar_global_lock);
4735
4736 /*
4737 * The bus notifier takes the dmar_global_lock, so lockdep will
4738 * complain later when we register it under the lock.
4739 */
4740 dmar_register_bus_notifier();
4741
4742 down_write(&dmar_global_lock);
4743
Joerg Roedel161b28a2017-03-28 17:04:52 +02004744 if (no_iommu || dmar_disabled) {
4745 /*
Shaohua Libfd20f12017-04-26 09:18:35 -07004746 * We exit the function here to ensure IOMMU's remapping and
4747 * mempool aren't setup, which means that the IOMMU's PMRs
4748 * won't be disabled via the call to init_dmars(). So disable
4749 * it explicitly here. The PMRs were setup by tboot prior to
4750 * calling SENTER, but the kernel is expected to reset/tear
4751 * down the PMRs.
4752 */
4753 if (intel_iommu_tboot_noforce) {
4754 for_each_iommu(iommu, drhd)
4755 iommu_disable_protect_mem_regions(iommu);
4756 }
4757
4758 /*
Joerg Roedel161b28a2017-03-28 17:04:52 +02004759 * Make sure the IOMMUs are switched off, even when we
4760 * boot into a kexec kernel and the previous kernel left
4761 * them enabled
4762 */
4763 intel_disable_iommus();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004764 goto out_free_dmar;
Joerg Roedel161b28a2017-03-28 17:04:52 +02004765 }
Suresh Siddha2ae21012008-07-10 11:16:43 -07004766
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004767 if (list_empty(&dmar_rmrr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004768 pr_info("No RMRR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004769
4770 if (list_empty(&dmar_atsr_units))
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004771 pr_info("No ATSR found\n");
Suresh Siddha318fe7d2011-08-23 17:05:20 -07004772
Joseph Cihula51a63e62011-03-21 11:04:24 -07004773 if (dmar_init_reserved_ranges()) {
4774 if (force_on)
4775 panic("tboot: Failed to reserve iommu ranges\n");
Jiang Liu3a5670e2014-02-19 14:07:33 +08004776 goto out_free_reserved_range;
Joseph Cihula51a63e62011-03-21 11:04:24 -07004777 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004778
4779 init_no_remapping_devices();
4780
Joseph Cihulab7792602011-05-03 00:08:37 -07004781 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004782 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07004783 if (force_on)
4784 panic("tboot: Failed to initialize DMARs\n");
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004785 pr_err("Initialization failed\n");
Jiang Liu9bdc5312014-01-06 14:18:27 +08004786 goto out_free_reserved_range;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004787 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08004788 up_write(&dmar_global_lock);
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004789 pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004790
Christoph Hellwig4fac8072017-12-24 13:57:08 +01004791#if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB)
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09004792 swiotlb = 0;
4793#endif
David Woodhouse19943b02009-08-04 16:19:20 +01004794 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07004795
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01004796 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01004797
Joerg Roedel39ab9552017-02-01 16:56:46 +01004798 for_each_active_iommu(iommu, drhd) {
4799 iommu_device_sysfs_add(&iommu->iommu, NULL,
4800 intel_iommu_groups,
4801 "%s", iommu->name);
4802 iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
4803 iommu_device_register(&iommu->iommu);
4804 }
Alex Williamsona5459cf2014-06-12 16:12:31 -06004805
Joerg Roedel4236d97d2011-09-06 17:56:07 +02004806 bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
Fenghua Yu99dcade2009-11-11 07:23:06 -08004807 bus_register_notifier(&pci_bus_type, &device_nb);
Jiang Liu75f05562014-02-19 14:07:37 +08004808 if (si_domain && !hw_pass_through)
4809 register_memory_notifier(&intel_iommu_memory_nb);
Anna-Maria Gleixner21647612016-11-27 00:13:41 +01004810 cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL,
4811 intel_iommu_cpu_dead);
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004812 intel_iommu_enabled = 1;
Sohil Mehtaee2636b2018-09-11 17:11:38 -07004813 intel_iommu_debugfs_init();
Eugeni Dodonov8bc1f852011-11-23 16:42:14 -02004814
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004815 return 0;
Jiang Liu9bdc5312014-01-06 14:18:27 +08004816
4817out_free_reserved_range:
4818 put_iova_domain(&reserved_iova_list);
Jiang Liu9bdc5312014-01-06 14:18:27 +08004819out_free_dmar:
4820 intel_iommu_free_dmars();
Jiang Liu3a5670e2014-02-19 14:07:33 +08004821 up_write(&dmar_global_lock);
4822 iommu_exit_mempool();
Jiang Liu9bdc5312014-01-06 14:18:27 +08004823 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07004824}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07004825
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004826static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *opaque)
Alex Williamson579305f2014-07-03 09:51:43 -06004827{
4828 struct intel_iommu *iommu = opaque;
4829
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004830 domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff);
Alex Williamson579305f2014-07-03 09:51:43 -06004831 return 0;
4832}
4833
4834/*
4835 * NB - intel-iommu lacks any sort of reference counting for the users of
4836 * dependent devices. If multiple endpoints have intersecting dependent
4837 * devices, unbinding the driver from any one of them will possibly leave
4838 * the others unable to operate.
4839 */
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004840static void domain_context_clear(struct intel_iommu *iommu, struct device *dev)
Han, Weidong3199aa62009-02-26 17:31:12 +08004841{
David Woodhouse0bcb3e22014-03-06 17:12:03 +00004842 if (!iommu || !dev || !dev_is_pci(dev))
Han, Weidong3199aa62009-02-26 17:31:12 +08004843 return;
4844
Joerg Roedel2452d9d2015-07-23 16:20:14 +02004845 pci_for_each_dma_alias(to_pci_dev(dev), &domain_context_clear_one_cb, iommu);
Han, Weidong3199aa62009-02-26 17:31:12 +08004846}
4847
Joerg Roedel127c7612015-07-23 17:44:46 +02004848static void __dmar_remove_one_dev_info(struct device_domain_info *info)
Weidong Hanc7151a82008-12-08 22:51:37 +08004849{
Weidong Hanc7151a82008-12-08 22:51:37 +08004850 struct intel_iommu *iommu;
4851 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08004852
Joerg Roedel55d94042015-07-22 16:50:40 +02004853 assert_spin_locked(&device_domain_lock);
4854
Joerg Roedelb608ac32015-07-21 18:19:08 +02004855 if (WARN_ON(!info))
Weidong Hanc7151a82008-12-08 22:51:37 +08004856 return;
4857
Joerg Roedel127c7612015-07-23 17:44:46 +02004858 iommu = info->iommu;
4859
4860 if (info->dev) {
4861 iommu_disable_dev_iotlb(info);
4862 domain_context_clear(iommu, info->dev);
Lu Baolua7fc93f2018-07-14 15:47:00 +08004863 intel_pasid_free_table(info->dev);
Joerg Roedel127c7612015-07-23 17:44:46 +02004864 }
4865
Joerg Roedelb608ac32015-07-21 18:19:08 +02004866 unlink_domain_info(info);
Roland Dreier3e7abe22011-07-20 06:22:21 -07004867
Joerg Roedeld160aca2015-07-22 11:52:53 +02004868 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004869 domain_detach_iommu(info->domain, iommu);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004870 spin_unlock_irqrestore(&iommu->lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004871
4872 free_devinfo_mem(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08004873}
4874
Joerg Roedel55d94042015-07-22 16:50:40 +02004875static void dmar_remove_one_dev_info(struct dmar_domain *domain,
4876 struct device *dev)
4877{
Joerg Roedel127c7612015-07-23 17:44:46 +02004878 struct device_domain_info *info;
Joerg Roedel55d94042015-07-22 16:50:40 +02004879 unsigned long flags;
4880
Weidong Hanc7151a82008-12-08 22:51:37 +08004881 spin_lock_irqsave(&device_domain_lock, flags);
Joerg Roedel127c7612015-07-23 17:44:46 +02004882 info = dev->archdata.iommu;
4883 __dmar_remove_one_dev_info(info);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004884 spin_unlock_irqrestore(&device_domain_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004885}
4886
4887static int md_domain_init(struct dmar_domain *domain, int guest_width)
4888{
4889 int adjust_width;
4890
Zhen Leiaa3ac942017-09-21 16:52:45 +01004891 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004892 domain_reserve_special_ranges(domain);
4893
4894 /* calculate AGAW */
4895 domain->gaw = guest_width;
4896 adjust_width = guestwidth_to_adjustwidth(guest_width);
4897 domain->agaw = width_to_agaw(adjust_width);
4898
Weidong Han5e98c4b2008-12-08 23:03:27 +08004899 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08004900 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01004901 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004902 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08004903
4904 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07004905 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08004906 if (!domain->pgd)
4907 return -ENOMEM;
4908 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
4909 return 0;
4910}
4911
Joerg Roedel00a77de2015-03-26 13:43:08 +01004912static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
Kay, Allen M38717942008-09-09 18:37:29 +03004913{
Joerg Roedel5d450802008-12-03 14:52:32 +01004914 struct dmar_domain *dmar_domain;
Joerg Roedel00a77de2015-03-26 13:43:08 +01004915 struct iommu_domain *domain;
4916
4917 if (type != IOMMU_DOMAIN_UNMANAGED)
4918 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004919
Jiang Liuab8dfe22014-07-11 14:19:27 +08004920 dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE);
Joerg Roedel5d450802008-12-03 14:52:32 +01004921 if (!dmar_domain) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004922 pr_err("Can't allocate dmar_domain\n");
Joerg Roedel00a77de2015-03-26 13:43:08 +01004923 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004924 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07004925 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004926 pr_err("Domain initialization failed\n");
Jiang Liu92d03cc2014-02-19 14:07:28 +08004927 domain_exit(dmar_domain);
Joerg Roedel00a77de2015-03-26 13:43:08 +01004928 return NULL;
Kay, Allen M38717942008-09-09 18:37:29 +03004929 }
Allen Kay8140a952011-10-14 12:32:17 -07004930 domain_update_iommu_cap(dmar_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004931
Joerg Roedel00a77de2015-03-26 13:43:08 +01004932 domain = &dmar_domain->domain;
Joerg Roedel8a0e7152012-01-26 19:40:54 +01004933 domain->geometry.aperture_start = 0;
4934 domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw);
4935 domain->geometry.force_aperture = true;
4936
Joerg Roedel00a77de2015-03-26 13:43:08 +01004937 return domain;
Kay, Allen M38717942008-09-09 18:37:29 +03004938}
Kay, Allen M38717942008-09-09 18:37:29 +03004939
Joerg Roedel00a77de2015-03-26 13:43:08 +01004940static void intel_iommu_domain_free(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03004941{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004942 domain_exit(to_dmar_domain(domain));
Kay, Allen M38717942008-09-09 18:37:29 +03004943}
Kay, Allen M38717942008-09-09 18:37:29 +03004944
Joerg Roedel4c5478c2008-12-03 14:58:24 +01004945static int intel_iommu_attach_device(struct iommu_domain *domain,
4946 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03004947{
Joerg Roedel00a77de2015-03-26 13:43:08 +01004948 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004949 struct intel_iommu *iommu;
4950 int addr_width;
David Woodhouse156baca2014-03-09 14:00:57 -07004951 u8 bus, devfn;
Kay, Allen M38717942008-09-09 18:37:29 +03004952
Alex Williamsonc875d2c2014-07-03 09:57:02 -06004953 if (device_is_rmrr_locked(dev)) {
4954 dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n");
4955 return -EPERM;
4956 }
4957
David Woodhouse7207d8f2014-03-09 16:31:06 -07004958 /* normally dev is not mapped */
4959 if (unlikely(domain_context_mapped(dev))) {
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004960 struct dmar_domain *old_domain;
4961
David Woodhouse1525a292014-03-06 16:19:30 +00004962 old_domain = find_domain(dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004963 if (old_domain) {
Joerg Roedeld160aca2015-07-22 11:52:53 +02004964 rcu_read_lock();
Joerg Roedelde7e8882015-07-22 11:58:07 +02004965 dmar_remove_one_dev_info(old_domain, dev);
Joerg Roedeld160aca2015-07-22 11:52:53 +02004966 rcu_read_unlock();
Joerg Roedel62c22162014-12-09 12:56:45 +01004967
4968 if (!domain_type_is_vm_or_si(old_domain) &&
4969 list_empty(&old_domain->devices))
4970 domain_exit(old_domain);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08004971 }
4972 }
4973
David Woodhouse156baca2014-03-09 14:00:57 -07004974 iommu = device_to_iommu(dev, &bus, &devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004975 if (!iommu)
4976 return -ENODEV;
4977
4978 /* check if this iommu agaw is sufficient for max mapped address */
4979 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01004980 if (addr_width > cap_mgaw(iommu->cap))
4981 addr_width = cap_mgaw(iommu->cap);
4982
4983 if (dmar_domain->max_addr > (1LL << addr_width)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02004984 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004985 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01004986 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08004987 return -EFAULT;
4988 }
Tom Lyona99c47a2010-05-17 08:20:45 +01004989 dmar_domain->gaw = addr_width;
4990
4991 /*
4992 * Knock out extra levels of page tables if necessary
4993 */
4994 while (iommu->agaw < dmar_domain->agaw) {
4995 struct dma_pte *pte;
4996
4997 pte = dmar_domain->pgd;
4998 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08004999 dmar_domain->pgd = (struct dma_pte *)
5000 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01005001 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01005002 }
5003 dmar_domain->agaw--;
5004 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005005
Joerg Roedel28ccce02015-07-21 14:45:31 +02005006 return domain_add_dev_info(dmar_domain, dev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005007}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005008
Joerg Roedel4c5478c2008-12-03 14:58:24 +01005009static void intel_iommu_detach_device(struct iommu_domain *domain,
5010 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03005011{
Joerg Roedele6de0f82015-07-22 16:30:36 +02005012 dmar_remove_one_dev_info(to_dmar_domain(domain), dev);
Kay, Allen M38717942008-09-09 18:37:29 +03005013}
Kay, Allen M38717942008-09-09 18:37:29 +03005014
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005015static int intel_iommu_map(struct iommu_domain *domain,
5016 unsigned long iova, phys_addr_t hpa,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005017 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03005018{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005019 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005020 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005021 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005022 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005023
Joerg Roedeldde57a22008-12-03 15:04:09 +01005024 if (iommu_prot & IOMMU_READ)
5025 prot |= DMA_PTE_READ;
5026 if (iommu_prot & IOMMU_WRITE)
5027 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08005028 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
5029 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005030
David Woodhouse163cc522009-06-28 00:51:17 +01005031 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01005032 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005033 u64 end;
5034
5035 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01005036 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005037 if (end < max_addr) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005038 pr_err("%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005039 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01005040 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005041 return -EFAULT;
5042 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01005043 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005044 }
David Woodhousead051222009-06-28 14:22:28 +01005045 /* Round up size to next multiple of PAGE_SIZE, if it and
5046 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01005047 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01005048 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
5049 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005050 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03005051}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005052
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02005053static size_t intel_iommu_unmap(struct iommu_domain *domain,
David Woodhouseea8ea462014-03-05 17:09:32 +00005054 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005055{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005056 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
David Woodhouseea8ea462014-03-05 17:09:32 +00005057 struct page *freelist = NULL;
David Woodhouseea8ea462014-03-05 17:09:32 +00005058 unsigned long start_pfn, last_pfn;
5059 unsigned int npages;
Joerg Roedel42e8c182015-07-21 15:50:02 +02005060 int iommu_id, level = 0;
Sheng Yang4b99d352009-07-08 11:52:52 +01005061
David Woodhouse5cf0a762014-03-19 16:07:49 +00005062 /* Cope with horrid API which requires us to unmap more than the
5063 size argument if it happens to be a large-page mapping. */
Joerg Roedeldc02e462015-08-13 11:15:13 +02005064 BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level));
David Woodhouse5cf0a762014-03-19 16:07:49 +00005065
5066 if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
5067 size = VTD_PAGE_SIZE << level_to_offset_bits(level);
5068
David Woodhouseea8ea462014-03-05 17:09:32 +00005069 start_pfn = iova >> VTD_PAGE_SHIFT;
5070 last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT;
5071
5072 freelist = domain_unmap(dmar_domain, start_pfn, last_pfn);
5073
5074 npages = last_pfn - start_pfn + 1;
5075
Shaokun Zhangf746a022018-03-22 18:18:06 +08005076 for_each_domain_iommu(iommu_id, dmar_domain)
Joerg Roedel42e8c182015-07-21 15:50:02 +02005077 iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain,
5078 start_pfn, npages, !freelist, 0);
David Woodhouseea8ea462014-03-05 17:09:32 +00005079
5080 dma_free_pagelist(freelist);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08005081
David Woodhouse163cc522009-06-28 00:51:17 +01005082 if (dmar_domain->max_addr == iova + size)
5083 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01005084
David Woodhouse5cf0a762014-03-19 16:07:49 +00005085 return size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005086}
Kay, Allen M38717942008-09-09 18:37:29 +03005087
Joerg Roedeld14d6572008-12-03 15:06:57 +01005088static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
Varun Sethibb5547a2013-03-29 01:23:58 +05305089 dma_addr_t iova)
Kay, Allen M38717942008-09-09 18:37:29 +03005090{
Joerg Roedel00a77de2015-03-26 13:43:08 +01005091 struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Kay, Allen M38717942008-09-09 18:37:29 +03005092 struct dma_pte *pte;
David Woodhouse5cf0a762014-03-19 16:07:49 +00005093 int level = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005094 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03005095
David Woodhouse5cf0a762014-03-19 16:07:49 +00005096 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level);
Kay, Allen M38717942008-09-09 18:37:29 +03005097 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005098 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03005099
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08005100 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03005101}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005102
Joerg Roedel5d587b82014-09-05 10:50:45 +02005103static bool intel_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005104{
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005105 if (cap == IOMMU_CAP_CACHE_COHERENCY)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005106 return domain_update_iommu_snooping(NULL) == 1;
Tom Lyon323f99c2010-07-02 16:56:14 -04005107 if (cap == IOMMU_CAP_INTR_REMAP)
Joerg Roedel5d587b82014-09-05 10:50:45 +02005108 return irq_remapping_enabled == 1;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005109
Joerg Roedel5d587b82014-09-05 10:50:45 +02005110 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08005111}
5112
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005113static int intel_iommu_add_device(struct device *dev)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005114{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005115 struct intel_iommu *iommu;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005116 struct iommu_group *group;
David Woodhouse156baca2014-03-09 14:00:57 -07005117 u8 bus, devfn;
Alex Williamson70ae6f02011-10-21 15:56:11 -04005118
Alex Williamsona5459cf2014-06-12 16:12:31 -06005119 iommu = device_to_iommu(dev, &bus, &devfn);
5120 if (!iommu)
Alex Williamson70ae6f02011-10-21 15:56:11 -04005121 return -ENODEV;
5122
Joerg Roedele3d10af2017-02-01 17:23:22 +01005123 iommu_device_link(&iommu->iommu, dev);
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005124
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005125 group = iommu_group_get_for_dev(dev);
Alex Williamson783f1572012-05-30 14:19:43 -06005126
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005127 if (IS_ERR(group))
5128 return PTR_ERR(group);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005129
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005130 iommu_group_put(group);
Alex Williamsone17f9ff2014-07-03 09:51:37 -06005131 return 0;
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005132}
5133
5134static void intel_iommu_remove_device(struct device *dev)
5135{
Alex Williamsona5459cf2014-06-12 16:12:31 -06005136 struct intel_iommu *iommu;
5137 u8 bus, devfn;
5138
5139 iommu = device_to_iommu(dev, &bus, &devfn);
5140 if (!iommu)
5141 return;
5142
Alex Williamsonabdfdde2012-05-30 14:19:19 -06005143 iommu_group_remove_device(dev);
Alex Williamsona5459cf2014-06-12 16:12:31 -06005144
Joerg Roedele3d10af2017-02-01 17:23:22 +01005145 iommu_device_unlink(&iommu->iommu, dev);
Alex Williamson70ae6f02011-10-21 15:56:11 -04005146}
5147
Eric Auger0659b8d2017-01-19 20:57:53 +00005148static void intel_iommu_get_resv_regions(struct device *device,
5149 struct list_head *head)
5150{
5151 struct iommu_resv_region *reg;
5152 struct dmar_rmrr_unit *rmrr;
5153 struct device *i_dev;
5154 int i;
5155
5156 rcu_read_lock();
5157 for_each_rmrr_units(rmrr) {
5158 for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt,
5159 i, i_dev) {
5160 if (i_dev != device)
5161 continue;
5162
5163 list_add_tail(&rmrr->resv->list, head);
5164 }
5165 }
5166 rcu_read_unlock();
5167
5168 reg = iommu_alloc_resv_region(IOAPIC_RANGE_START,
5169 IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00005170 0, IOMMU_RESV_MSI);
Eric Auger0659b8d2017-01-19 20:57:53 +00005171 if (!reg)
5172 return;
5173 list_add_tail(&reg->list, head);
5174}
5175
5176static void intel_iommu_put_resv_regions(struct device *dev,
5177 struct list_head *head)
5178{
5179 struct iommu_resv_region *entry, *next;
5180
5181 list_for_each_entry_safe(entry, next, head, list) {
5182 if (entry->type == IOMMU_RESV_RESERVED)
5183 kfree(entry);
5184 }
Kay, Allen M38717942008-09-09 18:37:29 +03005185}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005186
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005187#ifdef CONFIG_INTEL_IOMMU_SVM
Jacob Pan65ca7f52016-12-06 10:14:23 -08005188#define MAX_NR_PASID_BITS (20)
Lu Baolu4774cc52018-07-14 15:47:01 +08005189static inline unsigned long intel_iommu_get_pts(struct device *dev)
Jacob Pan65ca7f52016-12-06 10:14:23 -08005190{
Lu Baolu4774cc52018-07-14 15:47:01 +08005191 int pts, max_pasid;
5192
5193 max_pasid = intel_pasid_get_dev_max_id(dev);
5194 pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS);
5195 if (pts < 5)
Jacob Pan65ca7f52016-12-06 10:14:23 -08005196 return 0;
5197
Lu Baolu4774cc52018-07-14 15:47:01 +08005198 return pts - 5;
Jacob Pan65ca7f52016-12-06 10:14:23 -08005199}
5200
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005201int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev)
5202{
5203 struct device_domain_info *info;
5204 struct context_entry *context;
5205 struct dmar_domain *domain;
5206 unsigned long flags;
5207 u64 ctx_lo;
5208 int ret;
5209
5210 domain = get_valid_domain_for_dev(sdev->dev);
5211 if (!domain)
5212 return -EINVAL;
5213
5214 spin_lock_irqsave(&device_domain_lock, flags);
5215 spin_lock(&iommu->lock);
5216
5217 ret = -EINVAL;
5218 info = sdev->dev->archdata.iommu;
5219 if (!info || !info->pasid_supported)
5220 goto out;
5221
5222 context = iommu_context_addr(iommu, info->bus, info->devfn, 0);
5223 if (WARN_ON(!context))
5224 goto out;
5225
5226 ctx_lo = context[0].lo;
5227
5228 sdev->did = domain->iommu_did[iommu->seq_id];
5229 sdev->sid = PCI_DEVID(info->bus, info->devfn);
5230
5231 if (!(ctx_lo & CONTEXT_PASIDE)) {
Ashok Raj11b93eb2017-08-08 13:29:28 -07005232 if (iommu->pasid_state_table)
5233 context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table);
Lu Baolu4774cc52018-07-14 15:47:01 +08005234 context[1].lo = (u64)virt_to_phys(info->pasid_table->table) |
5235 intel_iommu_get_pts(sdev->dev);
Jacob Pan65ca7f52016-12-06 10:14:23 -08005236
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005237 wmb();
5238 /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both
5239 * extended to permit requests-with-PASID if the PASIDE bit
5240 * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH,
5241 * however, the PASIDE bit is ignored and requests-with-PASID
5242 * are unconditionally blocked. Which makes less sense.
5243 * So convert from CONTEXT_TT_PASS_THROUGH to one of the new
5244 * "guest mode" translation types depending on whether ATS
5245 * is available or not. Annoyingly, we can't use the new
5246 * modes *unless* PASIDE is set. */
5247 if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) {
5248 ctx_lo &= ~CONTEXT_TT_MASK;
5249 if (info->ats_supported)
5250 ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2;
5251 else
5252 ctx_lo |= CONTEXT_TT_PT_PASID << 2;
5253 }
5254 ctx_lo |= CONTEXT_PASIDE;
David Woodhouse907fea32015-10-13 14:11:13 +01005255 if (iommu->pasid_state_table)
5256 ctx_lo |= CONTEXT_DINVE;
David Woodhousea222a7f2015-10-07 23:35:18 +01005257 if (info->pri_supported)
5258 ctx_lo |= CONTEXT_PRS;
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005259 context[0].lo = ctx_lo;
5260 wmb();
5261 iommu->flush.flush_context(iommu, sdev->did, sdev->sid,
5262 DMA_CCMD_MASK_NOBIT,
5263 DMA_CCMD_DEVICE_INVL);
5264 }
5265
5266 /* Enable PASID support in the device, if it wasn't already */
5267 if (!info->pasid_enabled)
5268 iommu_enable_dev_iotlb(info);
5269
5270 if (info->ats_enabled) {
5271 sdev->dev_iotlb = 1;
5272 sdev->qdep = info->ats_qdep;
5273 if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
5274 sdev->qdep = 0;
5275 }
5276 ret = 0;
5277
5278 out:
5279 spin_unlock(&iommu->lock);
5280 spin_unlock_irqrestore(&device_domain_lock, flags);
5281
5282 return ret;
5283}
5284
5285struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5286{
5287 struct intel_iommu *iommu;
5288 u8 bus, devfn;
5289
5290 if (iommu_dummy(dev)) {
5291 dev_warn(dev,
5292 "No IOMMU translation for device; cannot enable SVM\n");
5293 return NULL;
5294 }
5295
5296 iommu = device_to_iommu(dev, &bus, &devfn);
5297 if ((!iommu)) {
Sudeep Duttb9997e32015-10-18 20:54:37 -07005298 dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005299 return NULL;
5300 }
5301
David Woodhouse2f26e0a2015-09-09 11:40:47 +01005302 return iommu;
5303}
5304#endif /* CONFIG_INTEL_IOMMU_SVM */
5305
Joerg Roedelb0119e82017-02-01 13:23:08 +01005306const struct iommu_ops intel_iommu_ops = {
Eric Auger0659b8d2017-01-19 20:57:53 +00005307 .capable = intel_iommu_capable,
5308 .domain_alloc = intel_iommu_domain_alloc,
5309 .domain_free = intel_iommu_domain_free,
5310 .attach_dev = intel_iommu_attach_device,
5311 .detach_dev = intel_iommu_detach_device,
5312 .map = intel_iommu_map,
5313 .unmap = intel_iommu_unmap,
Eric Auger0659b8d2017-01-19 20:57:53 +00005314 .iova_to_phys = intel_iommu_iova_to_phys,
5315 .add_device = intel_iommu_add_device,
5316 .remove_device = intel_iommu_remove_device,
5317 .get_resv_regions = intel_iommu_get_resv_regions,
5318 .put_resv_regions = intel_iommu_put_resv_regions,
5319 .device_group = pci_device_group,
5320 .pgsize_bitmap = INTEL_IOMMU_PGSIZES,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01005321};
David Woodhouse9af88142009-02-13 23:18:03 +00005322
Daniel Vetter94526182013-01-20 23:50:13 +01005323static void quirk_iommu_g4x_gfx(struct pci_dev *dev)
5324{
5325 /* G4x/GM45 integrated gfx dmar support is totally busted. */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005326 pr_info("Disabling IOMMU for graphics on this chipset\n");
Daniel Vetter94526182013-01-20 23:50:13 +01005327 dmar_map_gfx = 0;
5328}
5329
5330DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx);
5331DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx);
5332DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx);
5333DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx);
5334DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx);
5335DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx);
5336DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx);
5337
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005338static void quirk_iommu_rwbf(struct pci_dev *dev)
David Woodhouse9af88142009-02-13 23:18:03 +00005339{
5340 /*
5341 * Mobile 4 Series Chipset neglects to set RWBF capability,
Daniel Vetter210561f2013-01-21 19:48:59 +01005342 * but needs it. Same seems to hold for the desktop versions.
David Woodhouse9af88142009-02-13 23:18:03 +00005343 */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005344 pr_info("Forcing write-buffer flush capability\n");
David Woodhouse9af88142009-02-13 23:18:03 +00005345 rwbf_quirk = 1;
5346}
5347
5348DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
Daniel Vetter210561f2013-01-21 19:48:59 +01005349DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf);
5350DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf);
5351DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf);
5352DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf);
5353DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf);
5354DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07005355
Adam Jacksoneecfd572010-08-25 21:17:34 +01005356#define GGC 0x52
5357#define GGC_MEMORY_SIZE_MASK (0xf << 8)
5358#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
5359#define GGC_MEMORY_SIZE_1M (0x1 << 8)
5360#define GGC_MEMORY_SIZE_2M (0x3 << 8)
5361#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
5362#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
5363#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
5364#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
5365
Greg Kroah-Hartmand34d6512012-12-21 15:05:21 -08005366static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
David Woodhouse9eecabc2010-09-21 22:28:23 +01005367{
5368 unsigned short ggc;
5369
Adam Jacksoneecfd572010-08-25 21:17:34 +01005370 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01005371 return;
5372
Adam Jacksoneecfd572010-08-25 21:17:34 +01005373 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005374 pr_info("BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
David Woodhouse9eecabc2010-09-21 22:28:23 +01005375 dmar_map_gfx = 0;
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005376 } else if (dmar_map_gfx) {
5377 /* we have to ensure the gfx device is idle before we flush */
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005378 pr_info("Disabling batched IOTLB flush on Ironlake\n");
David Woodhouse6fbcfb32011-09-25 19:11:14 -07005379 intel_iommu_strict = 1;
5380 }
David Woodhouse9eecabc2010-09-21 22:28:23 +01005381}
5382DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
5383DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
5384DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
5385DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
5386
David Woodhousee0fc7e02009-09-30 09:12:17 -07005387/* On Tylersburg chipsets, some BIOSes have been known to enable the
5388 ISOCH DMAR unit for the Azalia sound device, but not give it any
5389 TLB entries, which causes it to deadlock. Check for that. We do
5390 this in a function called from init_dmars(), instead of in a PCI
5391 quirk, because we don't want to print the obnoxious "BIOS broken"
5392 message if VT-d is actually disabled.
5393*/
5394static void __init check_tylersburg_isoch(void)
5395{
5396 struct pci_dev *pdev;
5397 uint32_t vtisochctrl;
5398
5399 /* If there's no Azalia in the system anyway, forget it. */
5400 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
5401 if (!pdev)
5402 return;
5403 pci_dev_put(pdev);
5404
5405 /* System Management Registers. Might be hidden, in which case
5406 we can't do the sanity check. But that's OK, because the
5407 known-broken BIOSes _don't_ actually hide it, so far. */
5408 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
5409 if (!pdev)
5410 return;
5411
5412 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
5413 pci_dev_put(pdev);
5414 return;
5415 }
5416
5417 pci_dev_put(pdev);
5418
5419 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
5420 if (vtisochctrl & 1)
5421 return;
5422
5423 /* Drop all bits other than the number of TLB entries */
5424 vtisochctrl &= 0x1c;
5425
5426 /* If we have the recommended number of TLB entries (16), fine. */
5427 if (vtisochctrl == 0x10)
5428 return;
5429
5430 /* Zero TLB entries? You get to ride the short bus to school. */
5431 if (!vtisochctrl) {
5432 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
5433 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
5434 dmi_get_system_info(DMI_BIOS_VENDOR),
5435 dmi_get_system_info(DMI_BIOS_VERSION),
5436 dmi_get_system_info(DMI_PRODUCT_VERSION));
5437 iommu_identity_mapping |= IDENTMAP_AZALIA;
5438 return;
5439 }
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02005440
5441 pr_warn("Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
David Woodhousee0fc7e02009-09-30 09:12:17 -07005442 vtisochctrl);
5443}