blob: c1233d0288a035d2ef69a662f7eb04f13c5012c0 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -040022#include <linux/acpi.h>
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -040023#include <linux/amba/bus.h>
Wan Zongshun0076cd32016-05-10 09:21:01 -040024#include <linux/platform_device.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020025#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080026#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010028#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020029#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090030#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020031#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010032#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020033#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020034#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010035#include <linux/notifier.h>
36#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020037#include <linux/irq.h>
38#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020039#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080040#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010041#include <linux/percpu.h>
Joerg Roedel307d5852016-07-05 11:54:04 +020042#include <linux/iova.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020043#include <asm/irq_remapping.h>
44#include <asm/io_apic.h>
45#include <asm/apic.h>
46#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020047#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020048#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010050#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020051#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020052
53#include "amd_iommu_proto.h"
54#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020055#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020056
57#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
58
Joerg Roedel815b33f2011-04-06 17:26:49 +020059#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020060
Joerg Roedel307d5852016-07-05 11:54:04 +020061/* IO virtual address start page frame number */
62#define IOVA_START_PFN (1)
63#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
64#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
65
Joerg Roedel81cd07b2016-07-07 18:01:10 +020066/* Reserved IOVA ranges */
67#define MSI_RANGE_START (0xfee00000)
68#define MSI_RANGE_END (0xfeefffff)
69#define HT_RANGE_START (0xfd00000000ULL)
70#define HT_RANGE_END (0xffffffffffULL)
71
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020072/*
73 * This bitmap is used to advertise the page sizes our hardware support
74 * to the IOMMU core, which will then use this information to split
75 * physically contiguous memory regions it is mapping into page sizes
76 * that we support.
77 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010078 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020079 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010080#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020081
Joerg Roedelb6c02712008-06-26 21:27:53 +020082static DEFINE_RWLOCK(amd_iommu_devtable_lock);
83
Joerg Roedel8fa5f802011-06-09 12:24:45 +020084/* List of all available dev_data structures */
85static LIST_HEAD(dev_data_list);
86static DEFINE_SPINLOCK(dev_data_list_lock);
87
Joerg Roedel6efed632012-06-14 15:52:58 +020088LIST_HEAD(ioapic_map);
89LIST_HEAD(hpet_map);
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040090LIST_HEAD(acpihid_map);
Joerg Roedel6efed632012-06-14 15:52:58 +020091
Joerg Roedelc5b5da92016-07-06 11:55:37 +020092#define FLUSH_QUEUE_SIZE 256
93
94struct flush_queue_entry {
95 unsigned long iova_pfn;
96 unsigned long pages;
97 struct dma_ops_domain *dma_dom;
98};
99
100struct flush_queue {
101 spinlock_t lock;
102 unsigned next;
103 struct flush_queue_entry *entries;
104};
105
Wei Yongjuna5604f22016-07-28 02:09:53 +0000106static DEFINE_PER_CPU(struct flush_queue, flush_queue);
Joerg Roedelc5b5da92016-07-06 11:55:37 +0200107
Joerg Roedelbb279472016-07-06 13:56:36 +0200108static atomic_t queue_timer_on;
109static struct timer_list queue_timer;
110
Joerg Roedel0feae532009-08-26 15:26:30 +0200111/*
112 * Domain for untranslated devices - only allocated
113 * if iommu=pt passed on kernel cmd line.
114 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200115static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +0100116
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100117static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +0100118int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100119
Joerg Roedelac1534a2012-06-21 14:52:40 +0200120static struct dma_map_ops amd_iommu_dma_ops;
121
Joerg Roedel431b2a22008-07-11 17:14:22 +0200122/*
Joerg Roedel50917e22014-08-05 16:38:38 +0200123 * This struct contains device specific data for the IOMMU
124 */
125struct iommu_dev_data {
126 struct list_head list; /* For domain->dev_list */
127 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +0200128 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +0200129 u16 devid; /* PCI Device ID */
Joerg Roedele3156042016-04-08 15:12:24 +0200130 u16 alias; /* Alias Device ID */
Joerg Roedel50917e22014-08-05 16:38:38 +0200131 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +0200132 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +0200133 struct {
134 bool enabled;
135 int qdep;
136 } ats; /* ATS state */
137 bool pri_tlp; /* PASID TLB required for
138 PPR completions */
139 u32 errata; /* Bitmap for errata to apply */
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500140 bool use_vapic; /* Enable device to use vapic mode */
Joerg Roedel50917e22014-08-05 16:38:38 +0200141};
142
143/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200144 * general struct to manage commands send to an IOMMU
145 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200146struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200147 u32 data[4];
148};
149
Joerg Roedel05152a02012-06-15 16:53:51 +0200150struct kmem_cache *amd_iommu_irq_cache;
151
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200152static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200153static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100154static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700155
Joerg Roedel007b74b2015-12-21 12:53:54 +0100156/*
Joerg Roedel007b74b2015-12-21 12:53:54 +0100157 * Data container for a dma_ops specific protection domain
158 */
159struct dma_ops_domain {
160 /* generic protection domain information */
161 struct protection_domain domain;
162
Joerg Roedel307d5852016-07-05 11:54:04 +0200163 /* IOVA RB-Tree */
164 struct iova_domain iovad;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100165};
166
Joerg Roedel81cd07b2016-07-07 18:01:10 +0200167static struct iova_domain reserved_iova_ranges;
168static struct lock_class_key reserved_rbtree_key;
169
Joerg Roedel15898bb2009-11-24 15:39:42 +0100170/****************************************************************************
171 *
172 * Helper functions
173 *
174 ****************************************************************************/
175
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400176static inline int match_hid_uid(struct device *dev,
177 struct acpihid_map_entry *entry)
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100178{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400179 const char *hid, *uid;
180
181 hid = acpi_device_hid(ACPI_COMPANION(dev));
182 uid = acpi_device_uid(ACPI_COMPANION(dev));
183
184 if (!hid || !(*hid))
185 return -ENODEV;
186
187 if (!uid || !(*uid))
188 return strcmp(hid, entry->hid);
189
190 if (!(*entry->uid))
191 return strcmp(hid, entry->hid);
192
193 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100194}
195
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400196static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedele3156042016-04-08 15:12:24 +0200197{
198 struct pci_dev *pdev = to_pci_dev(dev);
199
200 return PCI_DEVID(pdev->bus->number, pdev->devfn);
201}
202
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400203static inline int get_acpihid_device_id(struct device *dev,
204 struct acpihid_map_entry **entry)
205{
206 struct acpihid_map_entry *p;
207
208 list_for_each_entry(p, &acpihid_map, list) {
209 if (!match_hid_uid(dev, p)) {
210 if (entry)
211 *entry = p;
212 return p->devid;
213 }
214 }
215 return -EINVAL;
216}
217
218static inline int get_device_id(struct device *dev)
219{
220 int devid;
221
222 if (dev_is_pci(dev))
223 devid = get_pci_device_id(dev);
224 else
225 devid = get_acpihid_device_id(dev, NULL);
226
227 return devid;
228}
229
Joerg Roedel15898bb2009-11-24 15:39:42 +0100230static struct protection_domain *to_pdomain(struct iommu_domain *dom)
231{
232 return container_of(dom, struct protection_domain, domain);
233}
234
Joerg Roedelb3311b02016-07-08 13:31:31 +0200235static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
236{
237 BUG_ON(domain->flags != PD_DMA_OPS_MASK);
238 return container_of(domain, struct dma_ops_domain, domain);
239}
240
Joerg Roedelf62dda62011-06-09 12:55:35 +0200241static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200242{
243 struct iommu_dev_data *dev_data;
244 unsigned long flags;
245
246 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
247 if (!dev_data)
248 return NULL;
249
Joerg Roedelf62dda62011-06-09 12:55:35 +0200250 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200251
252 spin_lock_irqsave(&dev_data_list_lock, flags);
253 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
254 spin_unlock_irqrestore(&dev_data_list_lock, flags);
255
256 return dev_data;
257}
258
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200259static struct iommu_dev_data *search_dev_data(u16 devid)
260{
261 struct iommu_dev_data *dev_data;
262 unsigned long flags;
263
264 spin_lock_irqsave(&dev_data_list_lock, flags);
265 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
266 if (dev_data->devid == devid)
267 goto out_unlock;
268 }
269
270 dev_data = NULL;
271
272out_unlock:
273 spin_unlock_irqrestore(&dev_data_list_lock, flags);
274
275 return dev_data;
276}
277
Joerg Roedele3156042016-04-08 15:12:24 +0200278static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
279{
280 *(u16 *)data = alias;
281 return 0;
282}
283
284static u16 get_alias(struct device *dev)
285{
286 struct pci_dev *pdev = to_pci_dev(dev);
287 u16 devid, ivrs_alias, pci_alias;
288
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200289 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200290 devid = get_device_id(dev);
Arindam Nath792a9642018-09-18 15:40:58 +0530291
292 /* For ACPI HID devices, we simply return the devid as such */
293 if (!dev_is_pci(dev))
294 return devid;
295
Joerg Roedele3156042016-04-08 15:12:24 +0200296 ivrs_alias = amd_iommu_alias_table[devid];
Arindam Nath792a9642018-09-18 15:40:58 +0530297
Joerg Roedele3156042016-04-08 15:12:24 +0200298 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
299
300 if (ivrs_alias == pci_alias)
301 return ivrs_alias;
302
303 /*
304 * DMA alias showdown
305 *
306 * The IVRS is fairly reliable in telling us about aliases, but it
307 * can't know about every screwy device. If we don't have an IVRS
308 * reported alias, use the PCI reported alias. In that case we may
309 * still need to initialize the rlookup and dev_table entries if the
310 * alias is to a non-existent device.
311 */
312 if (ivrs_alias == devid) {
313 if (!amd_iommu_rlookup_table[pci_alias]) {
314 amd_iommu_rlookup_table[pci_alias] =
315 amd_iommu_rlookup_table[devid];
316 memcpy(amd_iommu_dev_table[pci_alias].data,
317 amd_iommu_dev_table[devid].data,
318 sizeof(amd_iommu_dev_table[pci_alias].data));
319 }
320
321 return pci_alias;
322 }
323
324 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
325 "for device %s[%04x:%04x], kernel reported alias "
326 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
327 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
328 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
329 PCI_FUNC(pci_alias));
330
331 /*
332 * If we don't have a PCI DMA alias and the IVRS alias is on the same
333 * bus, then the IVRS table may know about a quirk that we don't.
334 */
335 if (pci_alias == devid &&
336 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700337 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Joerg Roedele3156042016-04-08 15:12:24 +0200338 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
339 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
340 dev_name(dev));
341 }
342
343 return ivrs_alias;
344}
345
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200346static struct iommu_dev_data *find_dev_data(u16 devid)
347{
348 struct iommu_dev_data *dev_data;
349
350 dev_data = search_dev_data(devid);
351
352 if (dev_data == NULL)
353 dev_data = alloc_dev_data(devid);
354
355 return dev_data;
356}
357
Joerg Roedel657cbb62009-11-23 15:26:46 +0100358static struct iommu_dev_data *get_dev_data(struct device *dev)
359{
360 return dev->archdata.iommu;
361}
362
Wan Zongshunb097d112016-04-01 09:06:04 -0400363/*
364* Find or create an IOMMU group for a acpihid device.
365*/
366static struct iommu_group *acpihid_device_group(struct device *dev)
367{
368 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300369 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400370
371 devid = get_acpihid_device_id(dev, &entry);
372 if (devid < 0)
373 return ERR_PTR(devid);
374
375 list_for_each_entry(p, &acpihid_map, list) {
376 if ((devid == p->devid) && p->group)
377 entry->group = p->group;
378 }
379
380 if (!entry->group)
381 entry->group = generic_device_group(dev);
382
383 return entry->group;
384}
385
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100386static bool pci_iommuv2_capable(struct pci_dev *pdev)
387{
388 static const int caps[] = {
389 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100390 PCI_EXT_CAP_ID_PRI,
391 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100392 };
393 int i, pos;
394
395 for (i = 0; i < 3; ++i) {
396 pos = pci_find_ext_capability(pdev, caps[i]);
397 if (pos == 0)
398 return false;
399 }
400
401 return true;
402}
403
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100404static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
405{
406 struct iommu_dev_data *dev_data;
407
408 dev_data = get_dev_data(&pdev->dev);
409
410 return dev_data->errata & (1 << erratum) ? true : false;
411}
412
Joerg Roedel71c70982009-11-24 16:43:06 +0100413/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100414 * This function checks if the driver got a valid device from the caller to
415 * avoid dereferencing invalid pointers.
416 */
417static bool check_device(struct device *dev)
418{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400419 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100420
421 if (!dev || !dev->dma_mask)
422 return false;
423
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100424 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200425 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400426 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100427
428 /* Out of our scope? */
429 if (devid > amd_iommu_last_bdf)
430 return false;
431
432 if (amd_iommu_rlookup_table[devid] == NULL)
433 return false;
434
435 return true;
436}
437
Alex Williamson25b11ce2014-09-19 10:03:13 -0600438static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600439{
Alex Williamson2851db22012-10-08 22:49:41 -0600440 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600441
Alex Williamson65d53522014-07-03 09:51:30 -0600442 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200443 if (IS_ERR(group))
444 return;
445
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200446 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600447}
448
449static int iommu_init_device(struct device *dev)
450{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600451 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400452 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600453
454 if (dev->archdata.iommu)
455 return 0;
456
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400457 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200458 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400459 return devid;
460
461 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600462 if (!dev_data)
463 return -ENOMEM;
464
Joerg Roedele3156042016-04-08 15:12:24 +0200465 dev_data->alias = get_alias(dev);
466
Yu Zhaod4ccfce2018-12-06 14:39:15 -0700467 /*
468 * By default we use passthrough mode for IOMMUv2 capable device.
469 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
470 * invalid address), we ignore the capability for the device so
471 * it'll be forced to go into translation mode.
472 */
473 if ((iommu_pass_through || !amd_iommu_force_isolation) &&
474 dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100475 struct amd_iommu *iommu;
476
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400477 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100478 dev_data->iommu_v2 = iommu->is_iommu_v2;
479 }
480
Joerg Roedel657cbb62009-11-23 15:26:46 +0100481 dev->archdata.iommu = dev_data;
482
Alex Williamson066f2e92014-06-12 16:12:37 -0600483 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
484 dev);
485
Joerg Roedel657cbb62009-11-23 15:26:46 +0100486 return 0;
487}
488
Joerg Roedel26018872011-06-06 16:50:14 +0200489static void iommu_ignore_device(struct device *dev)
490{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400491 u16 alias;
492 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200493
494 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200495 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400496 return;
497
Joerg Roedele3156042016-04-08 15:12:24 +0200498 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200499
500 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
501 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
502
503 amd_iommu_rlookup_table[devid] = NULL;
504 amd_iommu_rlookup_table[alias] = NULL;
505}
506
Joerg Roedel657cbb62009-11-23 15:26:46 +0100507static void iommu_uninit_device(struct device *dev)
508{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400509 int devid;
510 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600511
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400512 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200513 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400514 return;
515
516 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600517 if (!dev_data)
518 return;
519
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100520 if (dev_data->domain)
521 detach_device(dev);
522
Alex Williamson066f2e92014-06-12 16:12:37 -0600523 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
524 dev);
525
Alex Williamson9dcd6132012-05-30 14:19:07 -0600526 iommu_group_remove_device(dev);
527
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200528 /* Remove dma-ops */
529 dev->archdata.dma_ops = NULL;
530
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200531 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600532 * We keep dev_data around for unplugged devices and reuse it when the
533 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200534 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100535}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100536
Joerg Roedel431b2a22008-07-11 17:14:22 +0200537/****************************************************************************
538 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200539 * Interrupt handling functions
540 *
541 ****************************************************************************/
542
Joerg Roedele3e59872009-09-03 14:02:10 +0200543static void dump_dte_entry(u16 devid)
544{
545 int i;
546
Joerg Roedelee6c2862011-11-09 12:06:03 +0100547 for (i = 0; i < 4; ++i)
548 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200549 amd_iommu_dev_table[devid].data[i]);
550}
551
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200552static void dump_command(unsigned long phys_addr)
553{
554 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
555 int i;
556
557 for (i = 0; i < 4; ++i)
558 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
559}
560
Joerg Roedela345b232009-09-03 15:01:43 +0200561static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200562{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200563 int type, devid, domid, flags;
564 volatile u32 *event = __evt;
565 int count = 0;
566 u64 address;
567
568retry:
569 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
570 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
571 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
572 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
573 address = (u64)(((u64)event[3]) << 32) | event[2];
574
575 if (type == 0) {
576 /* Did we hit the erratum? */
577 if (++count == LOOP_TIMEOUT) {
578 pr_err("AMD-Vi: No event written to event log\n");
579 return;
580 }
581 udelay(1);
582 goto retry;
583 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200584
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200585 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200586
587 switch (type) {
588 case EVENT_TYPE_ILL_DEV:
589 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
590 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700591 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200592 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200593 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200594 break;
595 case EVENT_TYPE_IO_FAULT:
596 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
597 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700598 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200599 domid, address, flags);
600 break;
601 case EVENT_TYPE_DEV_TAB_ERR:
602 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
603 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700604 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200605 address, flags);
606 break;
607 case EVENT_TYPE_PAGE_TAB_ERR:
608 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
609 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700610 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611 domid, address, flags);
612 break;
613 case EVENT_TYPE_ILL_CMD:
614 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200615 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200616 break;
617 case EVENT_TYPE_CMD_HARD_ERR:
618 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
619 "flags=0x%04x]\n", address, flags);
620 break;
621 case EVENT_TYPE_IOTLB_INV_TO:
622 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
623 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700624 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200625 address);
626 break;
627 case EVENT_TYPE_INV_DEV_REQ:
628 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
629 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700630 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200631 address, flags);
632 break;
633 default:
634 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
635 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200636
637 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200638}
639
640static void iommu_poll_events(struct amd_iommu *iommu)
641{
642 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200643
644 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
645 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
646
647 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200648 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200649 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200650 }
651
652 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200653}
654
Joerg Roedeleee53532012-06-01 15:20:23 +0200655static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100656{
657 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100658
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100659 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
660 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
661 return;
662 }
663
664 fault.address = raw[1];
665 fault.pasid = PPR_PASID(raw[0]);
666 fault.device_id = PPR_DEVID(raw[0]);
667 fault.tag = PPR_TAG(raw[0]);
668 fault.flags = PPR_FLAGS(raw[0]);
669
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100670 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
671}
672
673static void iommu_poll_ppr_log(struct amd_iommu *iommu)
674{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100675 u32 head, tail;
676
677 if (iommu->ppr_log == NULL)
678 return;
679
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100680 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
681 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
682
683 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200684 volatile u64 *raw;
685 u64 entry[2];
686 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100687
Joerg Roedeleee53532012-06-01 15:20:23 +0200688 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100689
Joerg Roedeleee53532012-06-01 15:20:23 +0200690 /*
691 * Hardware bug: Interrupt may arrive before the entry is
692 * written to memory. If this happens we need to wait for the
693 * entry to arrive.
694 */
695 for (i = 0; i < LOOP_TIMEOUT; ++i) {
696 if (PPR_REQ_TYPE(raw[0]) != 0)
697 break;
698 udelay(1);
699 }
700
701 /* Avoid memcpy function-call overhead */
702 entry[0] = raw[0];
703 entry[1] = raw[1];
704
705 /*
706 * To detect the hardware bug we need to clear the entry
707 * back to zero.
708 */
709 raw[0] = raw[1] = 0UL;
710
711 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100712 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
713 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200714
Joerg Roedeleee53532012-06-01 15:20:23 +0200715 /* Handle PPR entry */
716 iommu_handle_ppr_entry(iommu, entry);
717
Joerg Roedeleee53532012-06-01 15:20:23 +0200718 /* Refresh ring-buffer information */
719 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100720 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
721 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100722}
723
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500724#ifdef CONFIG_IRQ_REMAP
725static int (*iommu_ga_log_notifier)(u32);
726
727int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
728{
729 iommu_ga_log_notifier = notifier;
730
731 return 0;
732}
733EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
734
735static void iommu_poll_ga_log(struct amd_iommu *iommu)
736{
737 u32 head, tail, cnt = 0;
738
739 if (iommu->ga_log == NULL)
740 return;
741
742 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
743 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
744
745 while (head != tail) {
746 volatile u64 *raw;
747 u64 log_entry;
748
749 raw = (u64 *)(iommu->ga_log + head);
750 cnt++;
751
752 /* Avoid memcpy function-call overhead */
753 log_entry = *raw;
754
755 /* Update head pointer of hardware ring-buffer */
756 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
757 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
758
759 /* Handle GA entry */
760 switch (GA_REQ_TYPE(log_entry)) {
761 case GA_GUEST_NR:
762 if (!iommu_ga_log_notifier)
763 break;
764
765 pr_debug("AMD-Vi: %s: devid=%#x, ga_tag=%#x\n",
766 __func__, GA_DEVID(log_entry),
767 GA_TAG(log_entry));
768
769 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
770 pr_err("AMD-Vi: GA log notifier failed.\n");
771 break;
772 default:
773 break;
774 }
775 }
776}
777#endif /* CONFIG_IRQ_REMAP */
778
779#define AMD_IOMMU_INT_MASK \
780 (MMIO_STATUS_EVT_INT_MASK | \
781 MMIO_STATUS_PPR_INT_MASK | \
782 MMIO_STATUS_GALOG_INT_MASK)
783
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200784irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200785{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500786 struct amd_iommu *iommu = (struct amd_iommu *) data;
787 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200788
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500789 while (status & AMD_IOMMU_INT_MASK) {
790 /* Enable EVT and PPR and GA interrupts again */
791 writel(AMD_IOMMU_INT_MASK,
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500792 iommu->mmio_base + MMIO_STATUS_OFFSET);
793
794 if (status & MMIO_STATUS_EVT_INT_MASK) {
795 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
796 iommu_poll_events(iommu);
797 }
798
799 if (status & MMIO_STATUS_PPR_INT_MASK) {
800 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
801 iommu_poll_ppr_log(iommu);
802 }
803
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500804#ifdef CONFIG_IRQ_REMAP
805 if (status & MMIO_STATUS_GALOG_INT_MASK) {
806 pr_devel("AMD-Vi: Processing IOMMU GA Log\n");
807 iommu_poll_ga_log(iommu);
808 }
809#endif
810
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500811 /*
812 * Hardware bug: ERBT1312
813 * When re-enabling interrupt (by writing 1
814 * to clear the bit), the hardware might also try to set
815 * the interrupt bit in the event status register.
816 * In this scenario, the bit will be set, and disable
817 * subsequent interrupts.
818 *
819 * Workaround: The IOMMU driver should read back the
820 * status register and check if the interrupt bits are cleared.
821 * If not, driver will need to go through the interrupt handler
822 * again and re-clear the bits
823 */
824 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100825 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200826 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200827}
828
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200829irqreturn_t amd_iommu_int_handler(int irq, void *data)
830{
831 return IRQ_WAKE_THREAD;
832}
833
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200834/****************************************************************************
835 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200836 * IOMMU command queuing functions
837 *
838 ****************************************************************************/
839
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200840static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200841{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200842 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200843
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200844 while (*sem == 0 && i < LOOP_TIMEOUT) {
845 udelay(1);
846 i += 1;
847 }
848
849 if (i == LOOP_TIMEOUT) {
850 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
851 return -EIO;
852 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200853
854 return 0;
855}
856
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200857static void copy_cmd_to_buffer(struct amd_iommu *iommu,
858 struct iommu_cmd *cmd,
859 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200860{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200861 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200862
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200863 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200864 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200865
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200866 /* Copy command to buffer */
867 memcpy(target, cmd, sizeof(*cmd));
868
869 /* Tell the IOMMU about it */
870 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
871}
872
Joerg Roedel815b33f2011-04-06 17:26:49 +0200873static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200874{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200875 WARN_ON(address & 0x7ULL);
876
Joerg Roedelded46732011-04-06 10:53:48 +0200877 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200878 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
879 cmd->data[1] = upper_32_bits(__pa(address));
880 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200881 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
882}
883
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200884static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
885{
886 memset(cmd, 0, sizeof(*cmd));
887 cmd->data[0] = devid;
888 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
889}
890
Joerg Roedel11b64022011-04-06 11:49:28 +0200891static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
892 size_t size, u16 domid, int pde)
893{
894 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100895 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200896
897 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100898 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200899
900 if (pages > 1) {
901 /*
902 * If we have to flush more than one page, flush all
903 * TLB entries for this domain
904 */
905 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100906 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200907 }
908
909 address &= PAGE_MASK;
910
911 memset(cmd, 0, sizeof(*cmd));
912 cmd->data[1] |= domid;
913 cmd->data[2] = lower_32_bits(address);
914 cmd->data[3] = upper_32_bits(address);
915 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
916 if (s) /* size bit - we flush more than one 4kb page */
917 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200918 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200919 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
920}
921
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200922static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
923 u64 address, size_t size)
924{
925 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100926 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200927
928 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100929 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200930
931 if (pages > 1) {
932 /*
933 * If we have to flush more than one page, flush all
934 * TLB entries for this domain
935 */
936 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100937 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200938 }
939
940 address &= PAGE_MASK;
941
942 memset(cmd, 0, sizeof(*cmd));
943 cmd->data[0] = devid;
944 cmd->data[0] |= (qdep & 0xff) << 24;
945 cmd->data[1] = devid;
946 cmd->data[2] = lower_32_bits(address);
947 cmd->data[3] = upper_32_bits(address);
948 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
949 if (s)
950 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
951}
952
Joerg Roedel22e266c2011-11-21 15:59:08 +0100953static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
954 u64 address, bool size)
955{
956 memset(cmd, 0, sizeof(*cmd));
957
958 address &= ~(0xfffULL);
959
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600960 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100961 cmd->data[1] = domid;
962 cmd->data[2] = lower_32_bits(address);
963 cmd->data[3] = upper_32_bits(address);
964 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
965 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
966 if (size)
967 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
968 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
969}
970
971static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
972 int qdep, u64 address, bool size)
973{
974 memset(cmd, 0, sizeof(*cmd));
975
976 address &= ~(0xfffULL);
977
978 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600979 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100980 cmd->data[0] |= (qdep & 0xff) << 24;
981 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600982 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100983 cmd->data[2] = lower_32_bits(address);
984 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
985 cmd->data[3] = upper_32_bits(address);
986 if (size)
987 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
988 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
989}
990
Joerg Roedelc99afa22011-11-21 18:19:25 +0100991static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
992 int status, int tag, bool gn)
993{
994 memset(cmd, 0, sizeof(*cmd));
995
996 cmd->data[0] = devid;
997 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600998 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100999 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
1000 }
1001 cmd->data[3] = tag & 0x1ff;
1002 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
1003
1004 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
1005}
1006
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001007static void build_inv_all(struct iommu_cmd *cmd)
1008{
1009 memset(cmd, 0, sizeof(*cmd));
1010 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001011}
1012
Joerg Roedel7ef27982012-06-21 16:46:04 +02001013static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1014{
1015 memset(cmd, 0, sizeof(*cmd));
1016 cmd->data[0] = devid;
1017 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1018}
1019
Joerg Roedel431b2a22008-07-11 17:14:22 +02001020/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001021 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001022 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001023 */
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001024static int __iommu_queue_command_sync(struct amd_iommu *iommu,
1025 struct iommu_cmd *cmd,
1026 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001027{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001028 u32 left, tail, head, next_tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001029
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001030again:
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001031
1032 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1033 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +02001034 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1035 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001036
Huang Rui21488352016-12-12 07:28:26 -05001037 if (left <= 0x20) {
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001038 struct iommu_cmd sync_cmd;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001039 int ret;
1040
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001041 iommu->cmd_sem = 0;
1042
1043 build_completion_wait(&sync_cmd, (u64)&iommu->cmd_sem);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001044 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1045
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001046 if ((ret = wait_on_sem(&iommu->cmd_sem)) != 0)
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001047 return ret;
1048
1049 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001050 }
1051
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001052 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001053
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001054 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001055 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001056
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001057 return 0;
1058}
1059
1060static int iommu_queue_command_sync(struct amd_iommu *iommu,
1061 struct iommu_cmd *cmd,
1062 bool sync)
1063{
1064 unsigned long flags;
1065 int ret;
1066
1067 spin_lock_irqsave(&iommu->lock, flags);
1068 ret = __iommu_queue_command_sync(iommu, cmd, sync);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001069 spin_unlock_irqrestore(&iommu->lock, flags);
1070
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001071 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001072}
1073
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001074static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1075{
1076 return iommu_queue_command_sync(iommu, cmd, true);
1077}
1078
Joerg Roedel8d201962008-12-02 20:34:41 +01001079/*
1080 * This function queues a completion wait command into the command
1081 * buffer of an IOMMU
1082 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001083static int iommu_completion_wait(struct amd_iommu *iommu)
1084{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001085 struct iommu_cmd cmd;
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001086 unsigned long flags;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001087 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001088
1089 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001090 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001091
Joerg Roedel8d201962008-12-02 20:34:41 +01001092
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001093 build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
1094
1095 spin_lock_irqsave(&iommu->lock, flags);
1096
1097 iommu->cmd_sem = 0;
1098
1099 ret = __iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001100 if (ret)
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001101 goto out_unlock;
Joerg Roedel8d201962008-12-02 20:34:41 +01001102
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001103 ret = wait_on_sem(&iommu->cmd_sem);
1104
1105out_unlock:
1106 spin_unlock_irqrestore(&iommu->lock, flags);
1107
1108 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001109}
1110
Joerg Roedeld8c13082011-04-06 18:51:26 +02001111static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001112{
1113 struct iommu_cmd cmd;
1114
Joerg Roedeld8c13082011-04-06 18:51:26 +02001115 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001116
Joerg Roedeld8c13082011-04-06 18:51:26 +02001117 return iommu_queue_command(iommu, &cmd);
1118}
1119
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001120static void iommu_flush_dte_all(struct amd_iommu *iommu)
1121{
1122 u32 devid;
1123
1124 for (devid = 0; devid <= 0xffff; ++devid)
1125 iommu_flush_dte(iommu, devid);
1126
1127 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001128}
1129
1130/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001131 * This function uses heavy locking and may disable irqs for some time. But
1132 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001133 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001134static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001135{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001136 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001137
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001138 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1139 struct iommu_cmd cmd;
1140 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1141 dom_id, 1);
1142 iommu_queue_command(iommu, &cmd);
1143 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001144
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001145 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001146}
1147
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001148static void iommu_flush_all(struct amd_iommu *iommu)
1149{
1150 struct iommu_cmd cmd;
1151
1152 build_inv_all(&cmd);
1153
1154 iommu_queue_command(iommu, &cmd);
1155 iommu_completion_wait(iommu);
1156}
1157
Joerg Roedel7ef27982012-06-21 16:46:04 +02001158static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1159{
1160 struct iommu_cmd cmd;
1161
1162 build_inv_irt(&cmd, devid);
1163
1164 iommu_queue_command(iommu, &cmd);
1165}
1166
1167static void iommu_flush_irt_all(struct amd_iommu *iommu)
1168{
1169 u32 devid;
1170
1171 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1172 iommu_flush_irt(iommu, devid);
1173
1174 iommu_completion_wait(iommu);
1175}
1176
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001177void iommu_flush_all_caches(struct amd_iommu *iommu)
1178{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001179 if (iommu_feature(iommu, FEATURE_IA)) {
1180 iommu_flush_all(iommu);
1181 } else {
1182 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001183 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001184 iommu_flush_tlb_all(iommu);
1185 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001186}
1187
Joerg Roedel431b2a22008-07-11 17:14:22 +02001188/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001189 * Command send function for flushing on-device TLB
1190 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001191static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1192 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001193{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001194 struct amd_iommu *iommu;
1195 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001196 int qdep;
1197
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001198 qdep = dev_data->ats.qdep;
1199 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001200
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001201 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001202
1203 return iommu_queue_command(iommu, &cmd);
1204}
1205
1206/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001207 * Command send function for invalidating a device table entry
1208 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001209static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001210{
1211 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001212 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001213 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001214
Joerg Roedel6c542042011-06-09 17:07:31 +02001215 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001216 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001217
Joerg Roedelf62dda62011-06-09 12:55:35 +02001218 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001219 if (!ret && alias != dev_data->devid)
1220 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001221 if (ret)
1222 return ret;
1223
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001224 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001225 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001226
1227 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001228}
1229
Joerg Roedel431b2a22008-07-11 17:14:22 +02001230/*
1231 * TLB invalidation function which is called from the mapping functions.
1232 * It invalidates a single PTE if the range to flush is within a single
1233 * page. Otherwise it flushes the whole TLB of the IOMMU.
1234 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001235static void __domain_flush_pages(struct protection_domain *domain,
1236 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001237{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001238 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001239 struct iommu_cmd cmd;
1240 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001241
Joerg Roedel11b64022011-04-06 11:49:28 +02001242 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001243
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001244 for (i = 0; i < amd_iommus_present; ++i) {
1245 if (!domain->dev_iommu[i])
1246 continue;
1247
1248 /*
1249 * Devices of this domain are behind this IOMMU
1250 * We need a TLB flush
1251 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001252 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001253 }
1254
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001255 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001256
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001257 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001258 continue;
1259
Joerg Roedel6c542042011-06-09 17:07:31 +02001260 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001261 }
1262
Joerg Roedel11b64022011-04-06 11:49:28 +02001263 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001264}
1265
Joerg Roedel17b124b2011-04-06 18:01:35 +02001266static void domain_flush_pages(struct protection_domain *domain,
1267 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001268{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001269 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001270}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001271
Joerg Roedel1c655772008-09-04 18:40:05 +02001272/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001273static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001274{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001275 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001276}
1277
Chris Wright42a49f92009-06-15 15:42:00 +02001278/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001279static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001280{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001281 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1282}
1283
1284static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001285{
1286 int i;
1287
1288 for (i = 0; i < amd_iommus_present; ++i) {
Joerg Roedelf1eae7c2016-07-06 12:50:35 +02001289 if (domain && !domain->dev_iommu[i])
Joerg Roedelb6c02712008-06-26 21:27:53 +02001290 continue;
1291
1292 /*
1293 * Devices of this domain are behind this IOMMU
1294 * We need to wait for completion of all commands.
1295 */
1296 iommu_completion_wait(amd_iommus[i]);
1297 }
1298}
1299
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001300
Joerg Roedel43f49602008-12-02 21:01:12 +01001301/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001302 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001303 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001304static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001305{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001306 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001307
1308 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001309 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001310}
1311
Joerg Roedel431b2a22008-07-11 17:14:22 +02001312/****************************************************************************
1313 *
1314 * The functions below are used the create the page table mappings for
1315 * unity mapped regions.
1316 *
1317 ****************************************************************************/
1318
1319/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001320 * This function is used to add another level to an IO page table. Adding
1321 * another level increases the size of the address space by 9 bits to a size up
1322 * to 64 bits.
1323 */
1324static bool increase_address_space(struct protection_domain *domain,
1325 gfp_t gfp)
1326{
1327 u64 *pte;
1328
1329 if (domain->mode == PAGE_MODE_6_LEVEL)
1330 /* address space already 64 bit large */
1331 return false;
1332
1333 pte = (void *)get_zeroed_page(gfp);
1334 if (!pte)
1335 return false;
1336
1337 *pte = PM_LEVEL_PDE(domain->mode,
1338 virt_to_phys(domain->pt_root));
1339 domain->pt_root = pte;
1340 domain->mode += 1;
1341 domain->updated = true;
1342
1343 return true;
1344}
1345
1346static u64 *alloc_pte(struct protection_domain *domain,
1347 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001348 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001349 u64 **pte_page,
1350 gfp_t gfp)
1351{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001352 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001353 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001354
1355 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001356
1357 while (address > PM_LEVEL_SIZE(domain->mode))
1358 increase_address_space(domain, gfp);
1359
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001360 level = domain->mode - 1;
1361 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1362 address = PAGE_SIZE_ALIGN(address, page_size);
1363 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001364
1365 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001366 u64 __pte, __npte;
1367
1368 __pte = *pte;
1369
1370 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001371 page = (u64 *)get_zeroed_page(gfp);
1372 if (!page)
1373 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001374
1375 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1376
Baoquan He134414f2016-09-15 16:50:50 +08001377 /* pte could have been changed somewhere. */
1378 if (cmpxchg64(pte, __pte, __npte) != __pte) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001379 free_page((unsigned long)page);
1380 continue;
1381 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001382 }
1383
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001384 /* No level skipping support yet */
1385 if (PM_PTE_LEVEL(*pte) != level)
1386 return NULL;
1387
Joerg Roedel308973d2009-11-24 17:43:32 +01001388 level -= 1;
1389
1390 pte = IOMMU_PTE_PAGE(*pte);
1391
1392 if (pte_page && level == end_lvl)
1393 *pte_page = pte;
1394
1395 pte = &pte[PM_LEVEL_INDEX(level, address)];
1396 }
1397
1398 return pte;
1399}
1400
1401/*
1402 * This function checks if there is a PTE for a given dma address. If
1403 * there is one, it returns the pointer to it.
1404 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001405static u64 *fetch_pte(struct protection_domain *domain,
1406 unsigned long address,
1407 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001408{
1409 int level;
1410 u64 *pte;
1411
Joerg Roedel24cd7722010-01-19 17:27:39 +01001412 if (address > PM_LEVEL_SIZE(domain->mode))
1413 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001414
Joerg Roedel3039ca12015-04-01 14:58:48 +02001415 level = domain->mode - 1;
1416 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1417 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001418
1419 while (level > 0) {
1420
1421 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001422 if (!IOMMU_PTE_PRESENT(*pte))
1423 return NULL;
1424
Joerg Roedel24cd7722010-01-19 17:27:39 +01001425 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001426 if (PM_PTE_LEVEL(*pte) == 7 ||
1427 PM_PTE_LEVEL(*pte) == 0)
1428 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001429
1430 /* No level skipping support yet */
1431 if (PM_PTE_LEVEL(*pte) != level)
1432 return NULL;
1433
Joerg Roedel308973d2009-11-24 17:43:32 +01001434 level -= 1;
1435
Joerg Roedel24cd7722010-01-19 17:27:39 +01001436 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001437 pte = IOMMU_PTE_PAGE(*pte);
1438 pte = &pte[PM_LEVEL_INDEX(level, address)];
1439 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1440 }
1441
1442 if (PM_PTE_LEVEL(*pte) == 0x07) {
1443 unsigned long pte_mask;
1444
1445 /*
1446 * If we have a series of large PTEs, make
1447 * sure to return a pointer to the first one.
1448 */
1449 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1450 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1451 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001452 }
1453
1454 return pte;
1455}
1456
1457/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001458 * Generic mapping functions. It maps a physical address into a DMA
1459 * address space. It allocates the page table pages if necessary.
1460 * In the future it can be extended to a generic mapping function
1461 * supporting all features of AMD IOMMU page tables like level skipping
1462 * and full 64 bit address spaces.
1463 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001464static int iommu_map_page(struct protection_domain *dom,
1465 unsigned long bus_addr,
1466 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001467 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001468 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001469 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001470{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001471 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001472 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001473
Joerg Roedeld4b03662015-04-01 14:58:52 +02001474 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1475 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1476
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001477 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001478 return -EINVAL;
1479
Joerg Roedeld4b03662015-04-01 14:58:52 +02001480 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001481 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001482
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001483 if (!pte)
1484 return -ENOMEM;
1485
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001486 for (i = 0; i < count; ++i)
1487 if (IOMMU_PTE_PRESENT(pte[i]))
1488 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001489
Joerg Roedeld4b03662015-04-01 14:58:52 +02001490 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001491 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1492 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1493 } else
1494 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1495
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001496 if (prot & IOMMU_PROT_IR)
1497 __pte |= IOMMU_PTE_IR;
1498 if (prot & IOMMU_PROT_IW)
1499 __pte |= IOMMU_PTE_IW;
1500
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001501 for (i = 0; i < count; ++i)
1502 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001503
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001504 update_domain(dom);
1505
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001506 return 0;
1507}
1508
Joerg Roedel24cd7722010-01-19 17:27:39 +01001509static unsigned long iommu_unmap_page(struct protection_domain *dom,
1510 unsigned long bus_addr,
1511 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001512{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001513 unsigned long long unmapped;
1514 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001515 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001516
Joerg Roedel24cd7722010-01-19 17:27:39 +01001517 BUG_ON(!is_power_of_2(page_size));
1518
1519 unmapped = 0;
1520
1521 while (unmapped < page_size) {
1522
Joerg Roedel71b390e2015-04-01 14:58:49 +02001523 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001524
Joerg Roedel71b390e2015-04-01 14:58:49 +02001525 if (pte) {
1526 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001527
Joerg Roedel71b390e2015-04-01 14:58:49 +02001528 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001529 for (i = 0; i < count; i++)
1530 pte[i] = 0ULL;
1531 }
1532
1533 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1534 unmapped += unmap_size;
1535 }
1536
Alex Williamson60d0ca32013-06-21 14:33:19 -06001537 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001538
1539 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001540}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001541
Joerg Roedel431b2a22008-07-11 17:14:22 +02001542/****************************************************************************
1543 *
1544 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001545 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001546 *
1547 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001548
Joerg Roedel9cabe892009-05-18 16:38:55 +02001549
Joerg Roedel256e4622016-07-05 14:23:01 +02001550static unsigned long dma_ops_alloc_iova(struct device *dev,
1551 struct dma_ops_domain *dma_dom,
1552 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001553{
Joerg Roedel256e4622016-07-05 14:23:01 +02001554 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001555
Joerg Roedel256e4622016-07-05 14:23:01 +02001556 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001557
Joerg Roedel256e4622016-07-05 14:23:01 +02001558 if (dma_mask > DMA_BIT_MASK(32))
1559 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1560 IOVA_PFN(DMA_BIT_MASK(32)));
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001561
Joerg Roedel256e4622016-07-05 14:23:01 +02001562 if (!pfn)
1563 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001564
Joerg Roedel256e4622016-07-05 14:23:01 +02001565 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001566}
1567
Joerg Roedel256e4622016-07-05 14:23:01 +02001568static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1569 unsigned long address,
1570 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001571{
Joerg Roedel256e4622016-07-05 14:23:01 +02001572 pages = __roundup_pow_of_two(pages);
1573 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001574
Joerg Roedel256e4622016-07-05 14:23:01 +02001575 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001576}
1577
Joerg Roedel431b2a22008-07-11 17:14:22 +02001578/****************************************************************************
1579 *
1580 * The next functions belong to the domain allocation. A domain is
1581 * allocated for every IOMMU as the default domain. If device isolation
1582 * is enabled, every device get its own domain. The most important thing
1583 * about domains is the page table mapping the DMA address space they
1584 * contain.
1585 *
1586 ****************************************************************************/
1587
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001588/*
1589 * This function adds a protection domain to the global protection domain list
1590 */
1591static void add_domain_to_list(struct protection_domain *domain)
1592{
1593 unsigned long flags;
1594
1595 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1596 list_add(&domain->list, &amd_iommu_pd_list);
1597 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1598}
1599
1600/*
1601 * This function removes a protection domain to the global
1602 * protection domain list
1603 */
1604static void del_domain_from_list(struct protection_domain *domain)
1605{
1606 unsigned long flags;
1607
1608 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1609 list_del(&domain->list);
1610 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1611}
1612
Joerg Roedelec487d12008-06-26 21:27:58 +02001613static u16 domain_id_alloc(void)
1614{
1615 unsigned long flags;
1616 int id;
1617
1618 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1619 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1620 BUG_ON(id == 0);
1621 if (id > 0 && id < MAX_DOMAIN_ID)
1622 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1623 else
1624 id = 0;
1625 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1626
1627 return id;
1628}
1629
Joerg Roedela2acfb72008-12-02 18:28:53 +01001630static void domain_id_free(int id)
1631{
1632 unsigned long flags;
1633
1634 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1635 if (id > 0 && id < MAX_DOMAIN_ID)
1636 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1637 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1638}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001639
Joerg Roedel5c34c402013-06-20 20:22:58 +02001640#define DEFINE_FREE_PT_FN(LVL, FN) \
1641static void free_pt_##LVL (unsigned long __pt) \
1642{ \
1643 unsigned long p; \
1644 u64 *pt; \
1645 int i; \
1646 \
1647 pt = (u64 *)__pt; \
1648 \
1649 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001650 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001651 if (!IOMMU_PTE_PRESENT(pt[i])) \
1652 continue; \
1653 \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001654 /* Large PTE? */ \
1655 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1656 PM_PTE_LEVEL(pt[i]) == 7) \
1657 continue; \
1658 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001659 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1660 FN(p); \
1661 } \
1662 free_page((unsigned long)pt); \
1663}
1664
1665DEFINE_FREE_PT_FN(l2, free_page)
1666DEFINE_FREE_PT_FN(l3, free_pt_l2)
1667DEFINE_FREE_PT_FN(l4, free_pt_l3)
1668DEFINE_FREE_PT_FN(l5, free_pt_l4)
1669DEFINE_FREE_PT_FN(l6, free_pt_l5)
1670
Joerg Roedel86db2e52008-12-02 18:20:21 +01001671static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001672{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001673 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001674
Joerg Roedel5c34c402013-06-20 20:22:58 +02001675 switch (domain->mode) {
1676 case PAGE_MODE_NONE:
1677 break;
1678 case PAGE_MODE_1_LEVEL:
1679 free_page(root);
1680 break;
1681 case PAGE_MODE_2_LEVEL:
1682 free_pt_l2(root);
1683 break;
1684 case PAGE_MODE_3_LEVEL:
1685 free_pt_l3(root);
1686 break;
1687 case PAGE_MODE_4_LEVEL:
1688 free_pt_l4(root);
1689 break;
1690 case PAGE_MODE_5_LEVEL:
1691 free_pt_l5(root);
1692 break;
1693 case PAGE_MODE_6_LEVEL:
1694 free_pt_l6(root);
1695 break;
1696 default:
1697 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001698 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001699}
1700
Joerg Roedelb16137b2011-11-21 16:50:23 +01001701static void free_gcr3_tbl_level1(u64 *tbl)
1702{
1703 u64 *ptr;
1704 int i;
1705
1706 for (i = 0; i < 512; ++i) {
1707 if (!(tbl[i] & GCR3_VALID))
1708 continue;
1709
1710 ptr = __va(tbl[i] & PAGE_MASK);
1711
1712 free_page((unsigned long)ptr);
1713 }
1714}
1715
1716static void free_gcr3_tbl_level2(u64 *tbl)
1717{
1718 u64 *ptr;
1719 int i;
1720
1721 for (i = 0; i < 512; ++i) {
1722 if (!(tbl[i] & GCR3_VALID))
1723 continue;
1724
1725 ptr = __va(tbl[i] & PAGE_MASK);
1726
1727 free_gcr3_tbl_level1(ptr);
1728 }
1729}
1730
Joerg Roedel52815b72011-11-17 17:24:28 +01001731static void free_gcr3_table(struct protection_domain *domain)
1732{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001733 if (domain->glx == 2)
1734 free_gcr3_tbl_level2(domain->gcr3_tbl);
1735 else if (domain->glx == 1)
1736 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001737 else
1738 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001739
Joerg Roedel52815b72011-11-17 17:24:28 +01001740 free_page((unsigned long)domain->gcr3_tbl);
1741}
1742
Joerg Roedel431b2a22008-07-11 17:14:22 +02001743/*
1744 * Free a domain, only used if something went wrong in the
1745 * allocation path and we need to free an already allocated page table
1746 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001747static void dma_ops_domain_free(struct dma_ops_domain *dom)
1748{
1749 if (!dom)
1750 return;
1751
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001752 del_domain_from_list(&dom->domain);
1753
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001754 put_iova_domain(&dom->iovad);
1755
Joerg Roedel86db2e52008-12-02 18:20:21 +01001756 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001757
Baoquan Hec3db9012016-09-15 16:50:52 +08001758 if (dom->domain.id)
1759 domain_id_free(dom->domain.id);
1760
Joerg Roedelec487d12008-06-26 21:27:58 +02001761 kfree(dom);
1762}
1763
Joerg Roedel431b2a22008-07-11 17:14:22 +02001764/*
1765 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001766 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001767 * structures required for the dma_ops interface
1768 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001769static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001770{
1771 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001772
1773 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1774 if (!dma_dom)
1775 return NULL;
1776
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001777 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001778 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001779
Joerg Roedelffec2192016-07-26 15:31:23 +02001780 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001781 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001782 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001783 if (!dma_dom->domain.pt_root)
1784 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001785
Joerg Roedel307d5852016-07-05 11:54:04 +02001786 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1787 IOVA_START_PFN, DMA_32BIT_PFN);
1788
Joerg Roedel81cd07b2016-07-07 18:01:10 +02001789 /* Initialize reserved ranges */
1790 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1791
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001792 add_domain_to_list(&dma_dom->domain);
1793
Joerg Roedelec487d12008-06-26 21:27:58 +02001794 return dma_dom;
1795
1796free_dma_dom:
1797 dma_ops_domain_free(dma_dom);
1798
1799 return NULL;
1800}
1801
Joerg Roedel431b2a22008-07-11 17:14:22 +02001802/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001803 * little helper function to check whether a given protection domain is a
1804 * dma_ops domain
1805 */
1806static bool dma_ops_domain(struct protection_domain *domain)
1807{
1808 return domain->flags & PD_DMA_OPS_MASK;
1809}
1810
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001811static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001812{
Joerg Roedel132bd682011-11-17 14:18:46 +01001813 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001814 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001815
Joerg Roedel132bd682011-11-17 14:18:46 +01001816 if (domain->mode != PAGE_MODE_NONE)
1817 pte_root = virt_to_phys(domain->pt_root);
1818
Joerg Roedel38ddf412008-09-11 10:38:32 +02001819 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1820 << DEV_ENTRY_MODE_SHIFT;
1821 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001822
Joerg Roedelee6c2862011-11-09 12:06:03 +01001823 flags = amd_iommu_dev_table[devid].data[1];
1824
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001825 if (ats)
1826 flags |= DTE_FLAG_IOTLB;
1827
Joerg Roedel52815b72011-11-17 17:24:28 +01001828 if (domain->flags & PD_IOMMUV2_MASK) {
1829 u64 gcr3 = __pa(domain->gcr3_tbl);
1830 u64 glx = domain->glx;
1831 u64 tmp;
1832
1833 pte_root |= DTE_FLAG_GV;
1834 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1835
1836 /* First mask out possible old values for GCR3 table */
1837 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1838 flags &= ~tmp;
1839
1840 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1841 flags &= ~tmp;
1842
1843 /* Encode GCR3 table into DTE */
1844 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1845 pte_root |= tmp;
1846
1847 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1848 flags |= tmp;
1849
1850 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1851 flags |= tmp;
1852 }
1853
Joerg Roedelee6c2862011-11-09 12:06:03 +01001854 flags &= ~(0xffffUL);
1855 flags |= domain->id;
1856
1857 amd_iommu_dev_table[devid].data[1] = flags;
1858 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001859}
1860
Joerg Roedel15898bb2009-11-24 15:39:42 +01001861static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001862{
Joerg Roedel355bf552008-12-08 12:02:41 +01001863 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001864 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1865 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001866
Joerg Roedelc5cca142009-10-09 18:31:20 +02001867 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001868}
1869
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001870static void do_attach(struct iommu_dev_data *dev_data,
1871 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001872{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001873 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001874 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001875 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001876
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001877 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001878 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001879 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001880
1881 /* Update data structures */
1882 dev_data->domain = domain;
1883 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001884
1885 /* Do reference counting */
1886 domain->dev_iommu[iommu->index] += 1;
1887 domain->dev_cnt += 1;
1888
Joerg Roedele25bfb52015-10-20 17:33:38 +02001889 /* Update device table */
1890 set_dte_entry(dev_data->devid, domain, ats);
1891 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08001892 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001893
Joerg Roedel6c542042011-06-09 17:07:31 +02001894 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001895}
1896
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001897static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001898{
Suravee Suthikulpanit2efa79e2019-01-24 04:16:45 +00001899 struct protection_domain *domain = dev_data->domain;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001900 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001901 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001902
Joerg Roedel5adad992015-10-09 16:23:33 +02001903 /*
1904 * First check if the device is still attached. It might already
1905 * be detached from its domain because the generic
1906 * iommu_detach_group code detached it and we try again here in
1907 * our alias handling.
1908 */
1909 if (!dev_data->domain)
1910 return;
1911
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001912 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001913 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02001914
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001915 /* Update data structures */
1916 dev_data->domain = NULL;
1917 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001918 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001919 if (alias != dev_data->devid)
1920 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001921
1922 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001923 device_flush_dte(dev_data);
Suravee Suthikulpanit2efa79e2019-01-24 04:16:45 +00001924
1925 /* Flush IOTLB */
1926 domain_flush_tlb_pde(domain);
1927
1928 /* Wait for the flushes to finish */
1929 domain_flush_complete(domain);
1930
1931 /* decrease reference counters - needs to happen after the flushes */
1932 domain->dev_iommu[iommu->index] -= 1;
1933 domain->dev_cnt -= 1;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001934}
1935
1936/*
1937 * If a device is not yet associated with a domain, this function does
1938 * assigns it visible for the hardware
1939 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001940static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001941 struct protection_domain *domain)
1942{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001943 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001944
Joerg Roedel272e4f92015-10-20 17:33:37 +02001945 /*
1946 * Must be called with IRQs disabled. Warn here to detect early
1947 * when its not.
1948 */
1949 WARN_ON(!irqs_disabled());
1950
Joerg Roedel15898bb2009-11-24 15:39:42 +01001951 /* lock domain */
1952 spin_lock(&domain->lock);
1953
Joerg Roedel397111a2014-08-05 17:31:51 +02001954 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02001955 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02001956 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01001957
Joerg Roedel397111a2014-08-05 17:31:51 +02001958 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02001959 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001960
Julia Lawall84fe6c12010-05-27 12:31:51 +02001961 ret = 0;
1962
1963out_unlock:
1964
Joerg Roedel355bf552008-12-08 12:02:41 +01001965 /* ready */
1966 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001967
Julia Lawall84fe6c12010-05-27 12:31:51 +02001968 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001969}
1970
Joerg Roedel52815b72011-11-17 17:24:28 +01001971
1972static void pdev_iommuv2_disable(struct pci_dev *pdev)
1973{
1974 pci_disable_ats(pdev);
1975 pci_disable_pri(pdev);
1976 pci_disable_pasid(pdev);
1977}
1978
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001979/* FIXME: Change generic reset-function to do the same */
1980static int pri_reset_while_enabled(struct pci_dev *pdev)
1981{
1982 u16 control;
1983 int pos;
1984
Joerg Roedel46277b72011-12-07 14:34:02 +01001985 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001986 if (!pos)
1987 return -EINVAL;
1988
Joerg Roedel46277b72011-12-07 14:34:02 +01001989 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1990 control |= PCI_PRI_CTRL_RESET;
1991 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001992
1993 return 0;
1994}
1995
Joerg Roedel52815b72011-11-17 17:24:28 +01001996static int pdev_iommuv2_enable(struct pci_dev *pdev)
1997{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001998 bool reset_enable;
1999 int reqs, ret;
2000
2001 /* FIXME: Hardcode number of outstanding requests for now */
2002 reqs = 32;
2003 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
2004 reqs = 1;
2005 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01002006
2007 /* Only allow access to user-accessible pages */
2008 ret = pci_enable_pasid(pdev, 0);
2009 if (ret)
2010 goto out_err;
2011
2012 /* First reset the PRI state of the device */
2013 ret = pci_reset_pri(pdev);
2014 if (ret)
2015 goto out_err;
2016
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002017 /* Enable PRI */
2018 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01002019 if (ret)
2020 goto out_err;
2021
Joerg Roedel6a113dd2011-12-01 12:04:58 +01002022 if (reset_enable) {
2023 ret = pri_reset_while_enabled(pdev);
2024 if (ret)
2025 goto out_err;
2026 }
2027
Joerg Roedel52815b72011-11-17 17:24:28 +01002028 ret = pci_enable_ats(pdev, PAGE_SHIFT);
2029 if (ret)
2030 goto out_err;
2031
2032 return 0;
2033
2034out_err:
2035 pci_disable_pri(pdev);
2036 pci_disable_pasid(pdev);
2037
2038 return ret;
2039}
2040
Joerg Roedelc99afa22011-11-21 18:19:25 +01002041/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02002042#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002043
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002044static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002045{
Joerg Roedela3b93122012-04-12 12:49:26 +02002046 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002047 int pos;
2048
Joerg Roedel46277b72011-12-07 14:34:02 +01002049 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002050 if (!pos)
2051 return false;
2052
Joerg Roedela3b93122012-04-12 12:49:26 +02002053 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002054
Joerg Roedela3b93122012-04-12 12:49:26 +02002055 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002056}
2057
Joerg Roedel15898bb2009-11-24 15:39:42 +01002058/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002059 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002060 * assigns it visible for the hardware
2061 */
2062static int attach_device(struct device *dev,
2063 struct protection_domain *domain)
2064{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002065 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002066 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002067 unsigned long flags;
2068 int ret;
2069
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002070 dev_data = get_dev_data(dev);
2071
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002072 if (!dev_is_pci(dev))
2073 goto skip_ats_check;
2074
2075 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002076 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002077 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002078 return -EINVAL;
2079
Joerg Roedel02ca2022015-07-28 16:58:49 +02002080 if (dev_data->iommu_v2) {
2081 if (pdev_iommuv2_enable(pdev) != 0)
2082 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002083
Joerg Roedel02ca2022015-07-28 16:58:49 +02002084 dev_data->ats.enabled = true;
2085 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2086 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2087 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002088 } else if (amd_iommu_iotlb_sup &&
2089 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002090 dev_data->ats.enabled = true;
2091 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2092 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002093
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002094skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01002095 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002096 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002097 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2098
2099 /*
2100 * We might boot into a crash-kernel here. The crashed kernel
2101 * left the caches in the IOMMU dirty. So we have to flush
2102 * here to evict all dirty stuff.
2103 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002104 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002105
2106 return ret;
2107}
2108
2109/*
2110 * Removes a device from a protection domain (unlocked)
2111 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002112static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002113{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002114 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002115
Joerg Roedel272e4f92015-10-20 17:33:37 +02002116 /*
2117 * Must be called with IRQs disabled. Warn here to detect early
2118 * when its not.
2119 */
2120 WARN_ON(!irqs_disabled());
2121
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002122 if (WARN_ON(!dev_data->domain))
2123 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002124
Joerg Roedel2ca76272010-01-22 16:45:31 +01002125 domain = dev_data->domain;
2126
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002127 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002128
Joerg Roedel150952f2015-10-20 17:33:35 +02002129 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002130
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002131 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002132}
2133
2134/*
2135 * Removes a device from a protection domain (with devtable_lock held)
2136 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002137static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002138{
Joerg Roedel52815b72011-11-17 17:24:28 +01002139 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002140 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002141 unsigned long flags;
2142
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002143 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002144 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002145
Joerg Roedel355bf552008-12-08 12:02:41 +01002146 /* lock device table */
2147 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002148 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002149 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002150
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002151 if (!dev_is_pci(dev))
2152 return;
2153
Joerg Roedel02ca2022015-07-28 16:58:49 +02002154 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002155 pdev_iommuv2_disable(to_pci_dev(dev));
2156 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002157 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002158
2159 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002160}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002161
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002162static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002163{
Joerg Roedel71f77582011-06-09 19:03:15 +02002164 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002165 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002166 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002167 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002168
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002169 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002170 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002171
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002172 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002173 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002174 return devid;
2175
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002176 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002177
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002178 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002179 if (ret) {
2180 if (ret != -ENOTSUPP)
2181 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2182 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002183
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002184 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002185 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002186 goto out;
2187 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002188 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002189
Joerg Roedel07ee8692015-05-28 18:41:42 +02002190 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002191
2192 BUG_ON(!dev_data);
2193
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002194 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002195 iommu_request_dm_for_dev(dev);
2196
2197 /* Domains are initialized for this device - have a look what we ended up with */
2198 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002199 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002200 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002201 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002202 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002203
2204out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002205 iommu_completion_wait(iommu);
2206
Joerg Roedele275a2a2008-12-10 18:27:25 +01002207 return 0;
2208}
2209
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002210static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002211{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002212 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002213 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002214
2215 if (!check_device(dev))
2216 return;
2217
2218 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002219 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002220 return;
2221
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002222 iommu = amd_iommu_rlookup_table[devid];
2223
2224 iommu_uninit_device(dev);
2225 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002226}
2227
Wan Zongshunb097d112016-04-01 09:06:04 -04002228static struct iommu_group *amd_iommu_device_group(struct device *dev)
2229{
2230 if (dev_is_pci(dev))
2231 return pci_device_group(dev);
2232
2233 return acpihid_device_group(dev);
2234}
2235
Joerg Roedel431b2a22008-07-11 17:14:22 +02002236/*****************************************************************************
2237 *
2238 * The next functions belong to the dma_ops mapping/unmapping code.
2239 *
2240 *****************************************************************************/
2241
Joerg Roedelb1516a12016-07-06 13:07:22 +02002242static void __queue_flush(struct flush_queue *queue)
2243{
2244 struct protection_domain *domain;
2245 unsigned long flags;
2246 int idx;
2247
2248 /* First flush TLB of all known domains */
2249 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
2250 list_for_each_entry(domain, &amd_iommu_pd_list, list)
2251 domain_flush_tlb(domain);
2252 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
2253
2254 /* Wait until flushes have completed */
2255 domain_flush_complete(NULL);
2256
2257 for (idx = 0; idx < queue->next; ++idx) {
2258 struct flush_queue_entry *entry;
2259
2260 entry = queue->entries + idx;
2261
2262 free_iova_fast(&entry->dma_dom->iovad,
2263 entry->iova_pfn,
2264 entry->pages);
2265
2266 /* Not really necessary, just to make sure we catch any bugs */
2267 entry->dma_dom = NULL;
2268 }
2269
2270 queue->next = 0;
2271}
2272
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002273static void queue_flush_all(void)
Joerg Roedelbb279472016-07-06 13:56:36 +02002274{
2275 int cpu;
2276
Joerg Roedelbb279472016-07-06 13:56:36 +02002277 for_each_possible_cpu(cpu) {
2278 struct flush_queue *queue;
2279 unsigned long flags;
2280
2281 queue = per_cpu_ptr(&flush_queue, cpu);
2282 spin_lock_irqsave(&queue->lock, flags);
2283 if (queue->next > 0)
2284 __queue_flush(queue);
2285 spin_unlock_irqrestore(&queue->lock, flags);
2286 }
2287}
2288
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002289static void queue_flush_timeout(unsigned long unsused)
2290{
2291 atomic_set(&queue_timer_on, 0);
2292 queue_flush_all();
2293}
2294
Joerg Roedelb1516a12016-07-06 13:07:22 +02002295static void queue_add(struct dma_ops_domain *dma_dom,
2296 unsigned long address, unsigned long pages)
2297{
2298 struct flush_queue_entry *entry;
2299 struct flush_queue *queue;
2300 unsigned long flags;
2301 int idx;
2302
2303 pages = __roundup_pow_of_two(pages);
2304 address >>= PAGE_SHIFT;
2305
2306 queue = get_cpu_ptr(&flush_queue);
2307 spin_lock_irqsave(&queue->lock, flags);
2308
2309 if (queue->next == FLUSH_QUEUE_SIZE)
2310 __queue_flush(queue);
2311
2312 idx = queue->next++;
2313 entry = queue->entries + idx;
2314
2315 entry->iova_pfn = address;
2316 entry->pages = pages;
2317 entry->dma_dom = dma_dom;
2318
2319 spin_unlock_irqrestore(&queue->lock, flags);
Joerg Roedelbb279472016-07-06 13:56:36 +02002320
2321 if (atomic_cmpxchg(&queue_timer_on, 0, 1) == 0)
2322 mod_timer(&queue_timer, jiffies + msecs_to_jiffies(10));
2323
Joerg Roedelb1516a12016-07-06 13:07:22 +02002324 put_cpu_ptr(&flush_queue);
2325}
2326
2327
Joerg Roedel431b2a22008-07-11 17:14:22 +02002328/*
2329 * In the dma_ops path we only have the struct device. This function
2330 * finds the corresponding IOMMU, the protection domain and the
2331 * requestor id for a given device.
2332 * If the device is not yet associated with a domain this is also done
2333 * in this function.
2334 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002335static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002336{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002337 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002338
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002339 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002340 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002341
Joerg Roedeld26592a2016-07-07 15:31:13 +02002342 domain = get_dev_data(dev)->domain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002343 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002344 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002345
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002346 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002347}
2348
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002349static void update_device_table(struct protection_domain *domain)
2350{
Joerg Roedel492667d2009-11-27 13:25:47 +01002351 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002352
Joerg Roedel3254de62016-07-26 15:18:54 +02002353 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002354 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel3254de62016-07-26 15:18:54 +02002355
2356 if (dev_data->devid == dev_data->alias)
2357 continue;
2358
2359 /* There is an alias, update device table entry for it */
2360 set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled);
2361 }
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002362}
2363
2364static void update_domain(struct protection_domain *domain)
2365{
2366 if (!domain->updated)
2367 return;
2368
2369 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002370
2371 domain_flush_devices(domain);
2372 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002373
2374 domain->updated = false;
2375}
2376
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002377static int dir2prot(enum dma_data_direction direction)
2378{
2379 if (direction == DMA_TO_DEVICE)
2380 return IOMMU_PROT_IR;
2381 else if (direction == DMA_FROM_DEVICE)
2382 return IOMMU_PROT_IW;
2383 else if (direction == DMA_BIDIRECTIONAL)
2384 return IOMMU_PROT_IW | IOMMU_PROT_IR;
2385 else
2386 return 0;
2387}
Joerg Roedel431b2a22008-07-11 17:14:22 +02002388/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002389 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002390 * contiguous memory region into DMA address space. It is used by all
2391 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002392 * Must be called with the domain lock held.
2393 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002394static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002395 struct dma_ops_domain *dma_dom,
2396 phys_addr_t paddr,
2397 size_t size,
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002398 enum dma_data_direction direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002399 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002400{
2401 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002402 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002403 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002404 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002405 int i;
2406
Joerg Roedele3c449f2008-10-15 22:02:11 -07002407 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002408 paddr &= PAGE_MASK;
2409
Joerg Roedel256e4622016-07-05 14:23:01 +02002410 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002411 if (address == DMA_ERROR_CODE)
2412 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002413
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002414 prot = dir2prot(direction);
Joerg Roedel518d9b42016-07-05 14:39:47 +02002415
Joerg Roedelcb76c322008-06-26 21:28:00 +02002416 start = address;
2417 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002418 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2419 PAGE_SIZE, prot, GFP_ATOMIC);
2420 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002421 goto out_unmap;
2422
Joerg Roedelcb76c322008-06-26 21:28:00 +02002423 paddr += PAGE_SIZE;
2424 start += PAGE_SIZE;
2425 }
2426 address += offset;
2427
Joerg Roedelab7032b2015-12-21 18:47:11 +01002428 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002429 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002430 domain_flush_complete(&dma_dom->domain);
2431 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002432
Joerg Roedelcb76c322008-06-26 21:28:00 +02002433out:
2434 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002435
2436out_unmap:
2437
2438 for (--i; i >= 0; --i) {
2439 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002440 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002441 }
2442
Joerg Roedel256e4622016-07-05 14:23:01 +02002443 domain_flush_tlb(&dma_dom->domain);
2444 domain_flush_complete(&dma_dom->domain);
2445
2446 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002447
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002448 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002449}
2450
Joerg Roedel431b2a22008-07-11 17:14:22 +02002451/*
2452 * Does the reverse of the __map_single function. Must be called with
2453 * the domain lock held too
2454 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002455static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002456 dma_addr_t dma_addr,
2457 size_t size,
2458 int dir)
2459{
Joerg Roedel04e04632010-09-23 16:12:48 +02002460 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002461 dma_addr_t i, start;
2462 unsigned int pages;
2463
Joerg Roedel04e04632010-09-23 16:12:48 +02002464 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002465 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002466 dma_addr &= PAGE_MASK;
2467 start = dma_addr;
2468
2469 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002470 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002471 start += PAGE_SIZE;
2472 }
2473
Joerg Roedelb1516a12016-07-06 13:07:22 +02002474 if (amd_iommu_unmap_flush) {
Joerg Roedelb1516a12016-07-06 13:07:22 +02002475 domain_flush_tlb(&dma_dom->domain);
2476 domain_flush_complete(&dma_dom->domain);
Zhen Lei5eb06bf2018-06-06 10:18:46 +08002477 dma_ops_free_iova(dma_dom, dma_addr, pages);
Joerg Roedelb1516a12016-07-06 13:07:22 +02002478 } else {
2479 queue_add(dma_dom, dma_addr, pages);
2480 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002481}
2482
Joerg Roedel431b2a22008-07-11 17:14:22 +02002483/*
2484 * The exported map_single function for dma_ops.
2485 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002486static dma_addr_t map_page(struct device *dev, struct page *page,
2487 unsigned long offset, size_t size,
2488 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002489 unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002490{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002491 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002492 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002493 struct dma_ops_domain *dma_dom;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002494 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002495
Joerg Roedel94f6d192009-11-24 16:40:02 +01002496 domain = get_domain(dev);
2497 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002498 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002499 else if (IS_ERR(domain))
2500 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002501
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002502 dma_mask = *dev->dma_mask;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002503 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002504
Joerg Roedelb3311b02016-07-08 13:31:31 +02002505 return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002506}
2507
Joerg Roedel431b2a22008-07-11 17:14:22 +02002508/*
2509 * The exported unmap_single function for dma_ops.
2510 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002511static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002512 enum dma_data_direction dir, unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002513{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002514 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002515 struct dma_ops_domain *dma_dom;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002516
Joerg Roedel94f6d192009-11-24 16:40:02 +01002517 domain = get_domain(dev);
2518 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002519 return;
2520
Joerg Roedelb3311b02016-07-08 13:31:31 +02002521 dma_dom = to_dma_ops_domain(domain);
2522
2523 __unmap_single(dma_dom, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002524}
2525
Joerg Roedel80187fd2016-07-06 17:20:54 +02002526static int sg_num_pages(struct device *dev,
2527 struct scatterlist *sglist,
2528 int nelems)
2529{
2530 unsigned long mask, boundary_size;
2531 struct scatterlist *s;
2532 int i, npages = 0;
2533
2534 mask = dma_get_seg_boundary(dev);
2535 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
2536 1UL << (BITS_PER_LONG - PAGE_SHIFT);
2537
2538 for_each_sg(sglist, s, nelems, i) {
2539 int p, n;
2540
2541 s->dma_address = npages << PAGE_SHIFT;
2542 p = npages % boundary_size;
2543 n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2544 if (p + n > boundary_size)
2545 npages += boundary_size - p;
2546 npages += n;
2547 }
2548
2549 return npages;
2550}
2551
Joerg Roedel431b2a22008-07-11 17:14:22 +02002552/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002553 * The exported map_sg function for dma_ops (handles scatter-gather
2554 * lists).
2555 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002556static int map_sg(struct device *dev, struct scatterlist *sglist,
Joerg Roedel80187fd2016-07-06 17:20:54 +02002557 int nelems, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002558 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002559{
Joerg Roedel80187fd2016-07-06 17:20:54 +02002560 int mapped_pages = 0, npages = 0, prot = 0, i;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002561 struct protection_domain *domain;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002562 struct dma_ops_domain *dma_dom;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002563 struct scatterlist *s;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002564 unsigned long address;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002565 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002566
Joerg Roedel94f6d192009-11-24 16:40:02 +01002567 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002568 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002569 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002570
Joerg Roedelb3311b02016-07-08 13:31:31 +02002571 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel832a90c2008-09-18 15:54:23 +02002572 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002573
Joerg Roedel80187fd2016-07-06 17:20:54 +02002574 npages = sg_num_pages(dev, sglist, nelems);
2575
2576 address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
2577 if (address == DMA_ERROR_CODE)
2578 goto out_err;
2579
2580 prot = dir2prot(direction);
2581
2582 /* Map all sg entries */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002583 for_each_sg(sglist, s, nelems, i) {
Joerg Roedel80187fd2016-07-06 17:20:54 +02002584 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002585
Joerg Roedel80187fd2016-07-06 17:20:54 +02002586 for (j = 0; j < pages; ++j) {
2587 unsigned long bus_addr, phys_addr;
2588 int ret;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002589
Joerg Roedel80187fd2016-07-06 17:20:54 +02002590 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2591 phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
2592 ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
2593 if (ret)
2594 goto out_unmap;
2595
2596 mapped_pages += 1;
2597 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002598 }
2599
Joerg Roedel80187fd2016-07-06 17:20:54 +02002600 /* Everything is mapped - write the right values into s->dma_address */
2601 for_each_sg(sglist, s, nelems, i) {
Stanislaw Gruszkacfa2d252019-03-13 10:03:17 +01002602 /*
2603 * Add in the remaining piece of the scatter-gather offset that
2604 * was masked out when we were determining the physical address
2605 * via (sg_phys(s) & PAGE_MASK) earlier.
2606 */
2607 s->dma_address += address + (s->offset & ~PAGE_MASK);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002608 s->dma_length = s->length;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002609 }
2610
Joerg Roedel80187fd2016-07-06 17:20:54 +02002611 return nelems;
2612
2613out_unmap:
2614 pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
2615 dev_name(dev), npages);
2616
2617 for_each_sg(sglist, s, nelems, i) {
2618 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2619
2620 for (j = 0; j < pages; ++j) {
2621 unsigned long bus_addr;
2622
2623 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2624 iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
2625
Jerry Snitselaar50c382e2019-01-19 10:38:05 -07002626 if (--mapped_pages == 0)
Joerg Roedel80187fd2016-07-06 17:20:54 +02002627 goto out_free_iova;
2628 }
2629 }
2630
2631out_free_iova:
Jerry Snitselaar8a6c9f62019-01-17 12:29:02 -07002632 free_iova_fast(&dma_dom->iovad, address >> PAGE_SHIFT, npages);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002633
2634out_err:
Joerg Roedel92d420e2015-12-21 19:31:33 +01002635 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002636}
2637
Joerg Roedel431b2a22008-07-11 17:14:22 +02002638/*
2639 * The exported map_sg function for dma_ops (handles scatter-gather
2640 * lists).
2641 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002642static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002643 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002644 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002645{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002646 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002647 struct dma_ops_domain *dma_dom;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002648 unsigned long startaddr;
2649 int npages = 2;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002650
Joerg Roedel94f6d192009-11-24 16:40:02 +01002651 domain = get_domain(dev);
2652 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002653 return;
2654
Joerg Roedel80187fd2016-07-06 17:20:54 +02002655 startaddr = sg_dma_address(sglist) & PAGE_MASK;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002656 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002657 npages = sg_num_pages(dev, sglist, nelems);
2658
Joerg Roedelb3311b02016-07-08 13:31:31 +02002659 __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002660}
2661
Joerg Roedel431b2a22008-07-11 17:14:22 +02002662/*
2663 * The exported alloc_coherent function for dma_ops.
2664 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002665static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002666 dma_addr_t *dma_addr, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002667 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002668{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002669 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002670 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002671 struct dma_ops_domain *dma_dom;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002672 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002673
Joerg Roedel94f6d192009-11-24 16:40:02 +01002674 domain = get_domain(dev);
2675 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002676 page = alloc_pages(flag, get_order(size));
2677 *dma_addr = page_to_phys(page);
2678 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002679 } else if (IS_ERR(domain))
2680 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002681
Joerg Roedelb3311b02016-07-08 13:31:31 +02002682 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel3b839a52015-04-01 14:58:47 +02002683 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002684 dma_mask = dev->coherent_dma_mask;
2685 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002686 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002687
Joerg Roedel3b839a52015-04-01 14:58:47 +02002688 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2689 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002690 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002691 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002692
Joerg Roedel3b839a52015-04-01 14:58:47 +02002693 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2694 get_order(size));
2695 if (!page)
2696 return NULL;
2697 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002698
Joerg Roedel832a90c2008-09-18 15:54:23 +02002699 if (!dma_mask)
2700 dma_mask = *dev->dma_mask;
2701
Joerg Roedelb3311b02016-07-08 13:31:31 +02002702 *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
Joerg Roedelbda350d2016-07-05 16:28:02 +02002703 size, DMA_BIDIRECTIONAL, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002704
Joerg Roedel92d420e2015-12-21 19:31:33 +01002705 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002706 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002707
Joerg Roedel3b839a52015-04-01 14:58:47 +02002708 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002709
2710out_free:
2711
Joerg Roedel3b839a52015-04-01 14:58:47 +02002712 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2713 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002714
2715 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002716}
2717
Joerg Roedel431b2a22008-07-11 17:14:22 +02002718/*
2719 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002720 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002721static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002722 void *virt_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002723 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002724{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002725 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002726 struct dma_ops_domain *dma_dom;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002727 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002728
Joerg Roedel3b839a52015-04-01 14:58:47 +02002729 page = virt_to_page(virt_addr);
2730 size = PAGE_ALIGN(size);
2731
Joerg Roedel94f6d192009-11-24 16:40:02 +01002732 domain = get_domain(dev);
2733 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002734 goto free_mem;
2735
Joerg Roedelb3311b02016-07-08 13:31:31 +02002736 dma_dom = to_dma_ops_domain(domain);
2737
2738 __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002739
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002740free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002741 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2742 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002743}
2744
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002745/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002746 * This function is called by the DMA layer to find out if we can handle a
2747 * particular device. It is part of the dma_ops.
2748 */
2749static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2750{
Joerg Roedel420aef82009-11-23 16:14:57 +01002751 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002752}
2753
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002754static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002755 .alloc = alloc_coherent,
2756 .free = free_coherent,
2757 .map_page = map_page,
2758 .unmap_page = unmap_page,
2759 .map_sg = map_sg,
2760 .unmap_sg = unmap_sg,
2761 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002762};
2763
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002764static int init_reserved_iova_ranges(void)
2765{
2766 struct pci_dev *pdev = NULL;
2767 struct iova *val;
2768
2769 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2770 IOVA_START_PFN, DMA_32BIT_PFN);
2771
2772 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2773 &reserved_rbtree_key);
2774
2775 /* MSI memory range */
2776 val = reserve_iova(&reserved_iova_ranges,
2777 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2778 if (!val) {
2779 pr_err("Reserving MSI range failed\n");
2780 return -ENOMEM;
2781 }
2782
2783 /* HT memory range */
2784 val = reserve_iova(&reserved_iova_ranges,
2785 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2786 if (!val) {
2787 pr_err("Reserving HT range failed\n");
2788 return -ENOMEM;
2789 }
2790
2791 /*
2792 * Memory used for PCI resources
2793 * FIXME: Check whether we can reserve the PCI-hole completly
2794 */
2795 for_each_pci_dev(pdev) {
2796 int i;
2797
2798 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2799 struct resource *r = &pdev->resource[i];
2800
2801 if (!(r->flags & IORESOURCE_MEM))
2802 continue;
2803
2804 val = reserve_iova(&reserved_iova_ranges,
2805 IOVA_PFN(r->start),
2806 IOVA_PFN(r->end));
2807 if (!val) {
2808 pr_err("Reserve pci-resource range failed\n");
2809 return -ENOMEM;
2810 }
2811 }
2812 }
2813
2814 return 0;
2815}
2816
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002817int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002818{
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002819 int ret, cpu, err = 0;
Joerg Roedel307d5852016-07-05 11:54:04 +02002820
2821 ret = iova_cache_get();
2822 if (ret)
2823 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002824
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002825 ret = init_reserved_iova_ranges();
2826 if (ret)
2827 return ret;
2828
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002829 for_each_possible_cpu(cpu) {
2830 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2831
2832 queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
2833 sizeof(*queue->entries),
2834 GFP_KERNEL);
2835 if (!queue->entries)
2836 goto out_put_iova;
2837
2838 spin_lock_init(&queue->lock);
2839 }
2840
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002841 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2842 if (err)
2843 return err;
2844#ifdef CONFIG_ARM_AMBA
2845 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2846 if (err)
2847 return err;
2848#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002849 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2850 if (err)
2851 return err;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002852 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002853
2854out_put_iova:
2855 for_each_possible_cpu(cpu) {
2856 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2857
2858 kfree(queue->entries);
2859 }
2860
2861 return -ENOMEM;
Joerg Roedelf5325092010-01-22 17:44:35 +01002862}
2863
Joerg Roedel6631ee92008-06-26 21:28:05 +02002864int __init amd_iommu_init_dma_ops(void)
2865{
Joerg Roedelbb279472016-07-06 13:56:36 +02002866 setup_timer(&queue_timer, queue_flush_timeout, 0);
2867 atomic_set(&queue_timer_on, 0);
2868
Joerg Roedel32302322015-07-28 16:58:50 +02002869 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002870 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002871
Joerg Roedel52717822015-07-28 16:58:51 +02002872 /*
2873 * In case we don't initialize SWIOTLB (actually the common case
2874 * when AMD IOMMU is enabled), make sure there are global
2875 * dma_ops set as a fall-back for devices not handled by this
2876 * driver (for example non-PCI devices).
2877 */
2878 if (!swiotlb)
2879 dma_ops = &nommu_dma_ops;
2880
Joerg Roedel62410ee2012-06-12 16:42:43 +02002881 if (amd_iommu_unmap_flush)
2882 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2883 else
2884 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2885
Joerg Roedel6631ee92008-06-26 21:28:05 +02002886 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002887
Joerg Roedel6631ee92008-06-26 21:28:05 +02002888}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002889
2890/*****************************************************************************
2891 *
2892 * The following functions belong to the exported interface of AMD IOMMU
2893 *
2894 * This interface allows access to lower level functions of the IOMMU
2895 * like protection domain handling and assignement of devices to domains
2896 * which is not possible with the dma_ops interface.
2897 *
2898 *****************************************************************************/
2899
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002900static void cleanup_domain(struct protection_domain *domain)
2901{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002902 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002903 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002904
2905 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2906
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002907 while (!list_empty(&domain->dev_list)) {
2908 entry = list_first_entry(&domain->dev_list,
2909 struct iommu_dev_data, list);
2910 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002911 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002912
2913 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2914}
2915
Joerg Roedel26508152009-08-26 16:52:40 +02002916static void protection_domain_free(struct protection_domain *domain)
2917{
2918 if (!domain)
2919 return;
2920
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002921 del_domain_from_list(domain);
2922
Joerg Roedel26508152009-08-26 16:52:40 +02002923 if (domain->id)
2924 domain_id_free(domain->id);
2925
2926 kfree(domain);
2927}
2928
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002929static int protection_domain_init(struct protection_domain *domain)
2930{
2931 spin_lock_init(&domain->lock);
2932 mutex_init(&domain->api_lock);
2933 domain->id = domain_id_alloc();
2934 if (!domain->id)
2935 return -ENOMEM;
2936 INIT_LIST_HEAD(&domain->dev_list);
2937
2938 return 0;
2939}
2940
Joerg Roedel26508152009-08-26 16:52:40 +02002941static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002942{
2943 struct protection_domain *domain;
2944
2945 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2946 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002947 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002948
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002949 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002950 goto out_err;
2951
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002952 add_domain_to_list(domain);
2953
Joerg Roedel26508152009-08-26 16:52:40 +02002954 return domain;
2955
2956out_err:
2957 kfree(domain);
2958
2959 return NULL;
2960}
2961
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002962static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2963{
2964 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002965 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002966
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002967 switch (type) {
2968 case IOMMU_DOMAIN_UNMANAGED:
2969 pdomain = protection_domain_alloc();
2970 if (!pdomain)
2971 return NULL;
2972
2973 pdomain->mode = PAGE_MODE_3_LEVEL;
2974 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2975 if (!pdomain->pt_root) {
2976 protection_domain_free(pdomain);
2977 return NULL;
2978 }
2979
2980 pdomain->domain.geometry.aperture_start = 0;
2981 pdomain->domain.geometry.aperture_end = ~0ULL;
2982 pdomain->domain.geometry.force_aperture = true;
2983
2984 break;
2985 case IOMMU_DOMAIN_DMA:
2986 dma_domain = dma_ops_domain_alloc();
2987 if (!dma_domain) {
2988 pr_err("AMD-Vi: Failed to allocate\n");
2989 return NULL;
2990 }
2991 pdomain = &dma_domain->domain;
2992 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002993 case IOMMU_DOMAIN_IDENTITY:
2994 pdomain = protection_domain_alloc();
2995 if (!pdomain)
2996 return NULL;
2997
2998 pdomain->mode = PAGE_MODE_NONE;
2999 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003000 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003001 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02003002 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003003
3004 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003005}
3006
3007static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02003008{
3009 struct protection_domain *domain;
Joerg Roedelcda70052016-07-07 15:57:04 +02003010 struct dma_ops_domain *dma_dom;
Joerg Roedel98383fc2008-12-02 18:34:12 +01003011
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003012 domain = to_pdomain(dom);
3013
Joerg Roedel98383fc2008-12-02 18:34:12 +01003014 if (domain->dev_cnt > 0)
3015 cleanup_domain(domain);
3016
3017 BUG_ON(domain->dev_cnt != 0);
3018
Joerg Roedelcda70052016-07-07 15:57:04 +02003019 if (!dom)
3020 return;
Joerg Roedel98383fc2008-12-02 18:34:12 +01003021
Joerg Roedelcda70052016-07-07 15:57:04 +02003022 switch (dom->type) {
3023 case IOMMU_DOMAIN_DMA:
Joerg Roedel281e8cc2016-07-07 16:12:02 +02003024 /*
3025 * First make sure the domain is no longer referenced from the
3026 * flush queue
3027 */
3028 queue_flush_all();
3029
3030 /* Now release the domain */
Joerg Roedelb3311b02016-07-08 13:31:31 +02003031 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelcda70052016-07-07 15:57:04 +02003032 dma_ops_domain_free(dma_dom);
3033 break;
3034 default:
3035 if (domain->mode != PAGE_MODE_NONE)
3036 free_pagetable(domain);
Joerg Roedel52815b72011-11-17 17:24:28 +01003037
Joerg Roedelcda70052016-07-07 15:57:04 +02003038 if (domain->flags & PD_IOMMUV2_MASK)
3039 free_gcr3_table(domain);
3040
3041 protection_domain_free(domain);
3042 break;
3043 }
Joerg Roedel98383fc2008-12-02 18:34:12 +01003044}
3045
Joerg Roedel684f2882008-12-08 12:07:44 +01003046static void amd_iommu_detach_device(struct iommu_domain *dom,
3047 struct device *dev)
3048{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003049 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003050 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003051 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01003052
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003053 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003054 return;
3055
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003056 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003057 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003058 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01003059
Joerg Roedel657cbb62009-11-23 15:26:46 +01003060 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003061 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003062
3063 iommu = amd_iommu_rlookup_table[devid];
3064 if (!iommu)
3065 return;
3066
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003067#ifdef CONFIG_IRQ_REMAP
3068 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
3069 (dom->type == IOMMU_DOMAIN_UNMANAGED))
3070 dev_data->use_vapic = 0;
3071#endif
3072
Joerg Roedel684f2882008-12-08 12:07:44 +01003073 iommu_completion_wait(iommu);
3074}
3075
Joerg Roedel01106062008-12-02 19:34:11 +01003076static int amd_iommu_attach_device(struct iommu_domain *dom,
3077 struct device *dev)
3078{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003079 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003080 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003081 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003082 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003083
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003084 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003085 return -EINVAL;
3086
Joerg Roedel657cbb62009-11-23 15:26:46 +01003087 dev_data = dev->archdata.iommu;
3088
Joerg Roedelf62dda62011-06-09 12:55:35 +02003089 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003090 if (!iommu)
3091 return -EINVAL;
3092
Joerg Roedel657cbb62009-11-23 15:26:46 +01003093 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003094 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003095
Joerg Roedel15898bb2009-11-24 15:39:42 +01003096 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003097
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003098#ifdef CONFIG_IRQ_REMAP
3099 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
3100 if (dom->type == IOMMU_DOMAIN_UNMANAGED)
3101 dev_data->use_vapic = 1;
3102 else
3103 dev_data->use_vapic = 0;
3104 }
3105#endif
3106
Joerg Roedel01106062008-12-02 19:34:11 +01003107 iommu_completion_wait(iommu);
3108
Joerg Roedel15898bb2009-11-24 15:39:42 +01003109 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003110}
3111
Joerg Roedel468e2362010-01-21 16:37:36 +01003112static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003113 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003114{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003115 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003116 int prot = 0;
3117 int ret;
3118
Joerg Roedel132bd682011-11-17 14:18:46 +01003119 if (domain->mode == PAGE_MODE_NONE)
3120 return -EINVAL;
3121
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003122 if (iommu_prot & IOMMU_READ)
3123 prot |= IOMMU_PROT_IR;
3124 if (iommu_prot & IOMMU_WRITE)
3125 prot |= IOMMU_PROT_IW;
3126
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003127 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02003128 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003129 mutex_unlock(&domain->api_lock);
3130
Joerg Roedel795e74f2010-05-11 17:40:57 +02003131 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003132}
3133
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003134static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3135 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003136{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003137 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003138 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003139
Joerg Roedel132bd682011-11-17 14:18:46 +01003140 if (domain->mode == PAGE_MODE_NONE)
3141 return -EINVAL;
3142
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003143 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003144 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f2010-05-11 17:40:57 +02003145 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003146
Joerg Roedel17b124b2011-04-06 18:01:35 +02003147 domain_flush_tlb_pde(domain);
Joerg Roedel3abebf02017-10-13 14:32:37 +02003148 domain_flush_complete(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003149
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003150 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003151}
3152
Joerg Roedel645c4c82008-12-02 20:05:50 +01003153static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303154 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003155{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003156 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003157 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003158 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003159
Joerg Roedel132bd682011-11-17 14:18:46 +01003160 if (domain->mode == PAGE_MODE_NONE)
3161 return iova;
3162
Joerg Roedel3039ca12015-04-01 14:58:48 +02003163 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003164
Joerg Roedela6d41a42009-09-02 17:08:55 +02003165 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003166 return 0;
3167
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003168 offset_mask = pte_pgsize - 1;
3169 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003170
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003171 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003172}
3173
Joerg Roedelab636482014-09-05 10:48:21 +02003174static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003175{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003176 switch (cap) {
3177 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003178 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003179 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003180 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003181 case IOMMU_CAP_NOEXEC:
3182 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003183 }
3184
Joerg Roedelab636482014-09-05 10:48:21 +02003185 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003186}
3187
Joerg Roedel35cf2482015-05-28 18:41:37 +02003188static void amd_iommu_get_dm_regions(struct device *dev,
3189 struct list_head *head)
3190{
3191 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003192 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003193
3194 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003195 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003196 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003197
3198 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3199 struct iommu_dm_region *region;
3200
3201 if (devid < entry->devid_start || devid > entry->devid_end)
3202 continue;
3203
3204 region = kzalloc(sizeof(*region), GFP_KERNEL);
3205 if (!region) {
3206 pr_err("Out of memory allocating dm-regions for %s\n",
3207 dev_name(dev));
3208 return;
3209 }
3210
3211 region->start = entry->address_start;
3212 region->length = entry->address_end - entry->address_start;
3213 if (entry->prot & IOMMU_PROT_IR)
3214 region->prot |= IOMMU_READ;
3215 if (entry->prot & IOMMU_PROT_IW)
3216 region->prot |= IOMMU_WRITE;
3217
3218 list_add_tail(&region->list, head);
3219 }
3220}
3221
3222static void amd_iommu_put_dm_regions(struct device *dev,
3223 struct list_head *head)
3224{
3225 struct iommu_dm_region *entry, *next;
3226
3227 list_for_each_entry_safe(entry, next, head, list)
3228 kfree(entry);
3229}
3230
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003231static void amd_iommu_apply_dm_region(struct device *dev,
3232 struct iommu_domain *domain,
3233 struct iommu_dm_region *region)
3234{
Joerg Roedelb3311b02016-07-08 13:31:31 +02003235 struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003236 unsigned long start, end;
3237
3238 start = IOVA_PFN(region->start);
Gary R Hook03bfadf2017-11-03 10:50:34 -06003239 end = IOVA_PFN(region->start + region->length - 1);
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003240
3241 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
3242}
3243
Thierry Redingb22f6432014-06-27 09:03:12 +02003244static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003245 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003246 .domain_alloc = amd_iommu_domain_alloc,
3247 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003248 .attach_dev = amd_iommu_attach_device,
3249 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003250 .map = amd_iommu_map,
3251 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003252 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003253 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003254 .add_device = amd_iommu_add_device,
3255 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04003256 .device_group = amd_iommu_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003257 .get_dm_regions = amd_iommu_get_dm_regions,
3258 .put_dm_regions = amd_iommu_put_dm_regions,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003259 .apply_dm_region = amd_iommu_apply_dm_region,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003260 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003261};
3262
Joerg Roedel0feae532009-08-26 15:26:30 +02003263/*****************************************************************************
3264 *
3265 * The next functions do a basic initialization of IOMMU for pass through
3266 * mode
3267 *
3268 * In passthrough mode the IOMMU is initialized and enabled but not used for
3269 * DMA-API translation.
3270 *
3271 *****************************************************************************/
3272
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003273/* IOMMUv2 specific functions */
3274int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3275{
3276 return atomic_notifier_chain_register(&ppr_notifier, nb);
3277}
3278EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3279
3280int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3281{
3282 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3283}
3284EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003285
3286void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3287{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003288 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003289 unsigned long flags;
3290
3291 spin_lock_irqsave(&domain->lock, flags);
3292
3293 /* Update data structure */
3294 domain->mode = PAGE_MODE_NONE;
3295 domain->updated = true;
3296
3297 /* Make changes visible to IOMMUs */
3298 update_domain(domain);
3299
3300 /* Page-table is not visible to IOMMU anymore, so free it */
3301 free_pagetable(domain);
3302
3303 spin_unlock_irqrestore(&domain->lock, flags);
3304}
3305EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003306
3307int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3308{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003309 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003310 unsigned long flags;
3311 int levels, ret;
3312
3313 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3314 return -EINVAL;
3315
3316 /* Number of GCR3 table levels required */
3317 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3318 levels += 1;
3319
3320 if (levels > amd_iommu_max_glx_val)
3321 return -EINVAL;
3322
3323 spin_lock_irqsave(&domain->lock, flags);
3324
3325 /*
3326 * Save us all sanity checks whether devices already in the
3327 * domain support IOMMUv2. Just force that the domain has no
3328 * devices attached when it is switched into IOMMUv2 mode.
3329 */
3330 ret = -EBUSY;
3331 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3332 goto out;
3333
3334 ret = -ENOMEM;
3335 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3336 if (domain->gcr3_tbl == NULL)
3337 goto out;
3338
3339 domain->glx = levels;
3340 domain->flags |= PD_IOMMUV2_MASK;
3341 domain->updated = true;
3342
3343 update_domain(domain);
3344
3345 ret = 0;
3346
3347out:
3348 spin_unlock_irqrestore(&domain->lock, flags);
3349
3350 return ret;
3351}
3352EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003353
3354static int __flush_pasid(struct protection_domain *domain, int pasid,
3355 u64 address, bool size)
3356{
3357 struct iommu_dev_data *dev_data;
3358 struct iommu_cmd cmd;
3359 int i, ret;
3360
3361 if (!(domain->flags & PD_IOMMUV2_MASK))
3362 return -EINVAL;
3363
3364 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3365
3366 /*
3367 * IOMMU TLB needs to be flushed before Device TLB to
3368 * prevent device TLB refill from IOMMU TLB
3369 */
3370 for (i = 0; i < amd_iommus_present; ++i) {
3371 if (domain->dev_iommu[i] == 0)
3372 continue;
3373
3374 ret = iommu_queue_command(amd_iommus[i], &cmd);
3375 if (ret != 0)
3376 goto out;
3377 }
3378
3379 /* Wait until IOMMU TLB flushes are complete */
3380 domain_flush_complete(domain);
3381
3382 /* Now flush device TLBs */
3383 list_for_each_entry(dev_data, &domain->dev_list, list) {
3384 struct amd_iommu *iommu;
3385 int qdep;
3386
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003387 /*
3388 There might be non-IOMMUv2 capable devices in an IOMMUv2
3389 * domain.
3390 */
3391 if (!dev_data->ats.enabled)
3392 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003393
3394 qdep = dev_data->ats.qdep;
3395 iommu = amd_iommu_rlookup_table[dev_data->devid];
3396
3397 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3398 qdep, address, size);
3399
3400 ret = iommu_queue_command(iommu, &cmd);
3401 if (ret != 0)
3402 goto out;
3403 }
3404
3405 /* Wait until all device TLBs are flushed */
3406 domain_flush_complete(domain);
3407
3408 ret = 0;
3409
3410out:
3411
3412 return ret;
3413}
3414
3415static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3416 u64 address)
3417{
3418 return __flush_pasid(domain, pasid, address, false);
3419}
3420
3421int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3422 u64 address)
3423{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003424 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003425 unsigned long flags;
3426 int ret;
3427
3428 spin_lock_irqsave(&domain->lock, flags);
3429 ret = __amd_iommu_flush_page(domain, pasid, address);
3430 spin_unlock_irqrestore(&domain->lock, flags);
3431
3432 return ret;
3433}
3434EXPORT_SYMBOL(amd_iommu_flush_page);
3435
3436static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3437{
3438 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3439 true);
3440}
3441
3442int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3443{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003444 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003445 unsigned long flags;
3446 int ret;
3447
3448 spin_lock_irqsave(&domain->lock, flags);
3449 ret = __amd_iommu_flush_tlb(domain, pasid);
3450 spin_unlock_irqrestore(&domain->lock, flags);
3451
3452 return ret;
3453}
3454EXPORT_SYMBOL(amd_iommu_flush_tlb);
3455
Joerg Roedelb16137b2011-11-21 16:50:23 +01003456static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3457{
3458 int index;
3459 u64 *pte;
3460
3461 while (true) {
3462
3463 index = (pasid >> (9 * level)) & 0x1ff;
3464 pte = &root[index];
3465
3466 if (level == 0)
3467 break;
3468
3469 if (!(*pte & GCR3_VALID)) {
3470 if (!alloc)
3471 return NULL;
3472
3473 root = (void *)get_zeroed_page(GFP_ATOMIC);
3474 if (root == NULL)
3475 return NULL;
3476
3477 *pte = __pa(root) | GCR3_VALID;
3478 }
3479
3480 root = __va(*pte & PAGE_MASK);
3481
3482 level -= 1;
3483 }
3484
3485 return pte;
3486}
3487
3488static int __set_gcr3(struct protection_domain *domain, int pasid,
3489 unsigned long cr3)
3490{
3491 u64 *pte;
3492
3493 if (domain->mode != PAGE_MODE_NONE)
3494 return -EINVAL;
3495
3496 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3497 if (pte == NULL)
3498 return -ENOMEM;
3499
3500 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3501
3502 return __amd_iommu_flush_tlb(domain, pasid);
3503}
3504
3505static int __clear_gcr3(struct protection_domain *domain, int pasid)
3506{
3507 u64 *pte;
3508
3509 if (domain->mode != PAGE_MODE_NONE)
3510 return -EINVAL;
3511
3512 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3513 if (pte == NULL)
3514 return 0;
3515
3516 *pte = 0;
3517
3518 return __amd_iommu_flush_tlb(domain, pasid);
3519}
3520
3521int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3522 unsigned long cr3)
3523{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003524 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003525 unsigned long flags;
3526 int ret;
3527
3528 spin_lock_irqsave(&domain->lock, flags);
3529 ret = __set_gcr3(domain, pasid, cr3);
3530 spin_unlock_irqrestore(&domain->lock, flags);
3531
3532 return ret;
3533}
3534EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3535
3536int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3537{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003538 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003539 unsigned long flags;
3540 int ret;
3541
3542 spin_lock_irqsave(&domain->lock, flags);
3543 ret = __clear_gcr3(domain, pasid);
3544 spin_unlock_irqrestore(&domain->lock, flags);
3545
3546 return ret;
3547}
3548EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003549
3550int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3551 int status, int tag)
3552{
3553 struct iommu_dev_data *dev_data;
3554 struct amd_iommu *iommu;
3555 struct iommu_cmd cmd;
3556
3557 dev_data = get_dev_data(&pdev->dev);
3558 iommu = amd_iommu_rlookup_table[dev_data->devid];
3559
3560 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3561 tag, dev_data->pri_tlp);
3562
3563 return iommu_queue_command(iommu, &cmd);
3564}
3565EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003566
3567struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3568{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003569 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003570
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003571 pdomain = get_domain(&pdev->dev);
3572 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003573 return NULL;
3574
3575 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003576 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003577 return NULL;
3578
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003579 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003580}
3581EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003582
3583void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3584{
3585 struct iommu_dev_data *dev_data;
3586
3587 if (!amd_iommu_v2_supported())
3588 return;
3589
3590 dev_data = get_dev_data(&pdev->dev);
3591 dev_data->errata |= (1 << erratum);
3592}
3593EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003594
3595int amd_iommu_device_info(struct pci_dev *pdev,
3596 struct amd_iommu_device_info *info)
3597{
3598 int max_pasids;
3599 int pos;
3600
3601 if (pdev == NULL || info == NULL)
3602 return -EINVAL;
3603
3604 if (!amd_iommu_v2_supported())
3605 return -EINVAL;
3606
3607 memset(info, 0, sizeof(*info));
3608
3609 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3610 if (pos)
3611 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3612
3613 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3614 if (pos)
3615 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3616
3617 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3618 if (pos) {
3619 int features;
3620
3621 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3622 max_pasids = min(max_pasids, (1 << 20));
3623
3624 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3625 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3626
3627 features = pci_pasid_features(pdev);
3628 if (features & PCI_PASID_CAP_EXEC)
3629 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3630 if (features & PCI_PASID_CAP_PRIV)
3631 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3632 }
3633
3634 return 0;
3635}
3636EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003637
3638#ifdef CONFIG_IRQ_REMAP
3639
3640/*****************************************************************************
3641 *
3642 * Interrupt Remapping Implementation
3643 *
3644 *****************************************************************************/
3645
Jiang Liu7c71d302015-04-13 14:11:33 +08003646static struct irq_chip amd_ir_chip;
3647
Joerg Roedel2b324502012-06-21 16:29:10 +02003648#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3649#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3650#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3651#define DTE_IRQ_REMAP_ENABLE 1ULL
3652
3653static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3654{
3655 u64 dte;
3656
3657 dte = amd_iommu_dev_table[devid].data[2];
3658 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3659 dte |= virt_to_phys(table->table);
3660 dte |= DTE_IRQ_REMAP_INTCTL;
3661 dte |= DTE_IRQ_TABLE_LEN;
3662 dte |= DTE_IRQ_REMAP_ENABLE;
3663
3664 amd_iommu_dev_table[devid].data[2] = dte;
3665}
3666
Joerg Roedel2b324502012-06-21 16:29:10 +02003667static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3668{
3669 struct irq_remap_table *table = NULL;
3670 struct amd_iommu *iommu;
3671 unsigned long flags;
3672 u16 alias;
3673
3674 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3675
3676 iommu = amd_iommu_rlookup_table[devid];
3677 if (!iommu)
3678 goto out_unlock;
3679
3680 table = irq_lookup_table[devid];
3681 if (table)
Baoquan He09284b92016-09-20 09:05:34 +08003682 goto out_unlock;
Joerg Roedel2b324502012-06-21 16:29:10 +02003683
3684 alias = amd_iommu_alias_table[devid];
3685 table = irq_lookup_table[alias];
3686 if (table) {
3687 irq_lookup_table[devid] = table;
3688 set_dte_irq_entry(devid, table);
3689 iommu_flush_dte(iommu, devid);
3690 goto out;
3691 }
3692
3693 /* Nothing there yet, allocate new irq remapping table */
3694 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3695 if (!table)
Baoquan He09284b92016-09-20 09:05:34 +08003696 goto out_unlock;
Joerg Roedel2b324502012-06-21 16:29:10 +02003697
Joerg Roedel197887f2013-04-09 21:14:08 +02003698 /* Initialize table spin-lock */
3699 spin_lock_init(&table->lock);
3700
Joerg Roedel2b324502012-06-21 16:29:10 +02003701 if (ioapic)
3702 /* Keep the first 32 indexes free for IOAPIC interrupts */
3703 table->min_index = 32;
3704
3705 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3706 if (!table->table) {
3707 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003708 table = NULL;
Baoquan He09284b92016-09-20 09:05:34 +08003709 goto out_unlock;
Joerg Roedel2b324502012-06-21 16:29:10 +02003710 }
3711
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003712 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3713 memset(table->table, 0,
3714 MAX_IRQS_PER_TABLE * sizeof(u32));
3715 else
3716 memset(table->table, 0,
3717 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
Joerg Roedel2b324502012-06-21 16:29:10 +02003718
3719 if (ioapic) {
3720 int i;
3721
3722 for (i = 0; i < 32; ++i)
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003723 iommu->irte_ops->set_allocated(table, i);
Joerg Roedel2b324502012-06-21 16:29:10 +02003724 }
3725
3726 irq_lookup_table[devid] = table;
3727 set_dte_irq_entry(devid, table);
3728 iommu_flush_dte(iommu, devid);
3729 if (devid != alias) {
3730 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003731 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003732 iommu_flush_dte(iommu, alias);
3733 }
3734
3735out:
3736 iommu_completion_wait(iommu);
3737
3738out_unlock:
3739 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3740
3741 return table;
3742}
3743
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003744static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003745{
3746 struct irq_remap_table *table;
3747 unsigned long flags;
3748 int index, c;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003749 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3750
3751 if (!iommu)
3752 return -ENODEV;
Joerg Roedel2b324502012-06-21 16:29:10 +02003753
3754 table = get_irq_table(devid, false);
3755 if (!table)
3756 return -ENODEV;
3757
3758 spin_lock_irqsave(&table->lock, flags);
3759
3760 /* Scan table for free entries */
3761 for (c = 0, index = table->min_index;
3762 index < MAX_IRQS_PER_TABLE;
3763 ++index) {
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003764 if (!iommu->irte_ops->is_allocated(table, index))
Joerg Roedel2b324502012-06-21 16:29:10 +02003765 c += 1;
3766 else
3767 c = 0;
3768
3769 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003770 for (; c != 0; --c)
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003771 iommu->irte_ops->set_allocated(table, index - c + 1);
Joerg Roedel2b324502012-06-21 16:29:10 +02003772
3773 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003774 goto out;
3775 }
3776 }
3777
3778 index = -ENOSPC;
3779
3780out:
3781 spin_unlock_irqrestore(&table->lock, flags);
3782
3783 return index;
3784}
3785
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003786static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
3787 struct amd_ir_data *data)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003788{
3789 struct irq_remap_table *table;
3790 struct amd_iommu *iommu;
3791 unsigned long flags;
3792 struct irte_ga *entry;
3793
3794 iommu = amd_iommu_rlookup_table[devid];
3795 if (iommu == NULL)
3796 return -EINVAL;
3797
3798 table = get_irq_table(devid, false);
3799 if (!table)
3800 return -ENOMEM;
3801
3802 spin_lock_irqsave(&table->lock, flags);
3803
3804 entry = (struct irte_ga *)table->table;
3805 entry = &entry[index];
3806 entry->lo.fields_remap.valid = 0;
3807 entry->hi.val = irte->hi.val;
3808 entry->lo.val = irte->lo.val;
3809 entry->lo.fields_remap.valid = 1;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003810 if (data)
3811 data->ref = entry;
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003812
3813 spin_unlock_irqrestore(&table->lock, flags);
3814
3815 iommu_flush_irt(iommu, devid);
3816 iommu_completion_wait(iommu);
3817
3818 return 0;
3819}
3820
3821static int modify_irte(u16 devid, int index, union irte *irte)
Joerg Roedel2b324502012-06-21 16:29:10 +02003822{
3823 struct irq_remap_table *table;
3824 struct amd_iommu *iommu;
3825 unsigned long flags;
3826
3827 iommu = amd_iommu_rlookup_table[devid];
3828 if (iommu == NULL)
3829 return -EINVAL;
3830
3831 table = get_irq_table(devid, false);
3832 if (!table)
3833 return -ENOMEM;
3834
3835 spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003836 table->table[index] = irte->val;
Joerg Roedel2b324502012-06-21 16:29:10 +02003837 spin_unlock_irqrestore(&table->lock, flags);
3838
3839 iommu_flush_irt(iommu, devid);
3840 iommu_completion_wait(iommu);
3841
3842 return 0;
3843}
3844
3845static void free_irte(u16 devid, int index)
3846{
3847 struct irq_remap_table *table;
3848 struct amd_iommu *iommu;
3849 unsigned long flags;
3850
3851 iommu = amd_iommu_rlookup_table[devid];
3852 if (iommu == NULL)
3853 return;
3854
3855 table = get_irq_table(devid, false);
3856 if (!table)
3857 return;
3858
3859 spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003860 iommu->irte_ops->clear_allocated(table, index);
Joerg Roedel2b324502012-06-21 16:29:10 +02003861 spin_unlock_irqrestore(&table->lock, flags);
3862
3863 iommu_flush_irt(iommu, devid);
3864 iommu_completion_wait(iommu);
3865}
3866
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003867static void irte_prepare(void *entry,
3868 u32 delivery_mode, u32 dest_mode,
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003869 u8 vector, u32 dest_apicid, int devid)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003870{
3871 union irte *irte = (union irte *) entry;
3872
3873 irte->val = 0;
3874 irte->fields.vector = vector;
3875 irte->fields.int_type = delivery_mode;
3876 irte->fields.destination = dest_apicid;
3877 irte->fields.dm = dest_mode;
3878 irte->fields.valid = 1;
3879}
3880
3881static void irte_ga_prepare(void *entry,
3882 u32 delivery_mode, u32 dest_mode,
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003883 u8 vector, u32 dest_apicid, int devid)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003884{
3885 struct irte_ga *irte = (struct irte_ga *) entry;
3886
3887 irte->lo.val = 0;
3888 irte->hi.val = 0;
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003889 irte->lo.fields_remap.int_type = delivery_mode;
3890 irte->lo.fields_remap.dm = dest_mode;
3891 irte->hi.fields.vector = vector;
3892 irte->lo.fields_remap.destination = dest_apicid;
3893 irte->lo.fields_remap.valid = 1;
3894}
3895
3896static void irte_activate(void *entry, u16 devid, u16 index)
3897{
3898 union irte *irte = (union irte *) entry;
3899
3900 irte->fields.valid = 1;
3901 modify_irte(devid, index, irte);
3902}
3903
3904static void irte_ga_activate(void *entry, u16 devid, u16 index)
3905{
3906 struct irte_ga *irte = (struct irte_ga *) entry;
3907
3908 irte->lo.fields_remap.valid = 1;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003909 modify_irte_ga(devid, index, irte, NULL);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003910}
3911
3912static void irte_deactivate(void *entry, u16 devid, u16 index)
3913{
3914 union irte *irte = (union irte *) entry;
3915
3916 irte->fields.valid = 0;
3917 modify_irte(devid, index, irte);
3918}
3919
3920static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
3921{
3922 struct irte_ga *irte = (struct irte_ga *) entry;
3923
3924 irte->lo.fields_remap.valid = 0;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05003925 modify_irte_ga(devid, index, irte, NULL);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003926}
3927
3928static void irte_set_affinity(void *entry, u16 devid, u16 index,
3929 u8 vector, u32 dest_apicid)
3930{
3931 union irte *irte = (union irte *) entry;
3932
3933 irte->fields.vector = vector;
3934 irte->fields.destination = dest_apicid;
3935 modify_irte(devid, index, irte);
3936}
3937
3938static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
3939 u8 vector, u32 dest_apicid)
3940{
3941 struct irte_ga *irte = (struct irte_ga *) entry;
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003942 struct iommu_dev_data *dev_data = search_dev_data(devid);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003943
Suravee Suthikulpanit1781a292017-06-26 04:28:04 -05003944 if (!dev_data || !dev_data->use_vapic ||
3945 !irte->lo.fields_remap.guest_mode) {
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003946 irte->hi.fields.vector = vector;
3947 irte->lo.fields_remap.destination = dest_apicid;
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05003948 modify_irte_ga(devid, index, irte, NULL);
3949 }
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003950}
3951
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003952#define IRTE_ALLOCATED (~1U)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003953static void irte_set_allocated(struct irq_remap_table *table, int index)
3954{
3955 table->table[index] = IRTE_ALLOCATED;
3956}
3957
3958static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3959{
3960 struct irte_ga *ptr = (struct irte_ga *)table->table;
3961 struct irte_ga *irte = &ptr[index];
3962
3963 memset(&irte->lo.val, 0, sizeof(u64));
3964 memset(&irte->hi.val, 0, sizeof(u64));
3965 irte->hi.fields.vector = 0xff;
3966}
3967
3968static bool irte_is_allocated(struct irq_remap_table *table, int index)
3969{
3970 union irte *ptr = (union irte *)table->table;
3971 union irte *irte = &ptr[index];
3972
3973 return irte->val != 0;
3974}
3975
3976static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3977{
3978 struct irte_ga *ptr = (struct irte_ga *)table->table;
3979 struct irte_ga *irte = &ptr[index];
3980
3981 return irte->hi.fields.vector != 0;
3982}
3983
3984static void irte_clear_allocated(struct irq_remap_table *table, int index)
3985{
3986 table->table[index] = 0;
3987}
3988
3989static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3990{
3991 struct irte_ga *ptr = (struct irte_ga *)table->table;
3992 struct irte_ga *irte = &ptr[index];
3993
3994 memset(&irte->lo.val, 0, sizeof(u64));
3995 memset(&irte->hi.val, 0, sizeof(u64));
3996}
3997
Jiang Liu7c71d302015-04-13 14:11:33 +08003998static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003999{
Jiang Liu7c71d302015-04-13 14:11:33 +08004000 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02004001
Jiang Liu7c71d302015-04-13 14:11:33 +08004002 switch (info->type) {
4003 case X86_IRQ_ALLOC_TYPE_IOAPIC:
4004 devid = get_ioapic_devid(info->ioapic_id);
4005 break;
4006 case X86_IRQ_ALLOC_TYPE_HPET:
4007 devid = get_hpet_devid(info->hpet_id);
4008 break;
4009 case X86_IRQ_ALLOC_TYPE_MSI:
4010 case X86_IRQ_ALLOC_TYPE_MSIX:
4011 devid = get_device_id(&info->msi_dev->dev);
4012 break;
4013 default:
4014 BUG_ON(1);
4015 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02004016 }
4017
Jiang Liu7c71d302015-04-13 14:11:33 +08004018 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02004019}
4020
Jiang Liu7c71d302015-04-13 14:11:33 +08004021static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02004022{
Jiang Liu7c71d302015-04-13 14:11:33 +08004023 struct amd_iommu *iommu;
4024 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02004025
Jiang Liu7c71d302015-04-13 14:11:33 +08004026 if (!info)
4027 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02004028
Jiang Liu7c71d302015-04-13 14:11:33 +08004029 devid = get_devid(info);
4030 if (devid >= 0) {
4031 iommu = amd_iommu_rlookup_table[devid];
4032 if (iommu)
4033 return iommu->ir_domain;
4034 }
Joerg Roedel5527de72012-06-26 11:17:32 +02004035
Jiang Liu7c71d302015-04-13 14:11:33 +08004036 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02004037}
4038
Jiang Liu7c71d302015-04-13 14:11:33 +08004039static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004040{
Jiang Liu7c71d302015-04-13 14:11:33 +08004041 struct amd_iommu *iommu;
4042 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004043
Jiang Liu7c71d302015-04-13 14:11:33 +08004044 if (!info)
4045 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004046
Jiang Liu7c71d302015-04-13 14:11:33 +08004047 switch (info->type) {
4048 case X86_IRQ_ALLOC_TYPE_MSI:
4049 case X86_IRQ_ALLOC_TYPE_MSIX:
4050 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02004051 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04004052 return NULL;
4053
Dan Carpenter1fb260b2016-01-07 12:36:06 +03004054 iommu = amd_iommu_rlookup_table[devid];
4055 if (iommu)
4056 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08004057 break;
4058 default:
4059 break;
4060 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02004061
Jiang Liu7c71d302015-04-13 14:11:33 +08004062 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02004063}
4064
Joerg Roedel6b474b82012-06-26 16:46:04 +02004065struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02004066 .prepare = amd_iommu_prepare,
4067 .enable = amd_iommu_enable,
4068 .disable = amd_iommu_disable,
4069 .reenable = amd_iommu_reenable,
4070 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08004071 .get_ir_irq_domain = get_ir_irq_domain,
4072 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02004073};
Jiang Liu7c71d302015-04-13 14:11:33 +08004074
4075static void irq_remapping_prepare_irte(struct amd_ir_data *data,
4076 struct irq_cfg *irq_cfg,
4077 struct irq_alloc_info *info,
4078 int devid, int index, int sub_handle)
4079{
4080 struct irq_2_irte *irte_info = &data->irq_2_irte;
4081 struct msi_msg *msg = &data->msi_entry;
Jiang Liu7c71d302015-04-13 14:11:33 +08004082 struct IO_APIC_route_entry *entry;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004083 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
4084
4085 if (!iommu)
4086 return;
Jiang Liu7c71d302015-04-13 14:11:33 +08004087
Jiang Liu7c71d302015-04-13 14:11:33 +08004088 data->irq_2_irte.devid = devid;
4089 data->irq_2_irte.index = index + sub_handle;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004090 iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
4091 apic->irq_dest_mode, irq_cfg->vector,
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05004092 irq_cfg->dest_apicid, devid);
Jiang Liu7c71d302015-04-13 14:11:33 +08004093
4094 switch (info->type) {
4095 case X86_IRQ_ALLOC_TYPE_IOAPIC:
4096 /* Setup IOAPIC entry */
4097 entry = info->ioapic_entry;
4098 info->ioapic_entry = NULL;
4099 memset(entry, 0, sizeof(*entry));
4100 entry->vector = index;
4101 entry->mask = 0;
4102 entry->trigger = info->ioapic_trigger;
4103 entry->polarity = info->ioapic_polarity;
4104 /* Mask level triggered irqs. */
4105 if (info->ioapic_trigger)
4106 entry->mask = 1;
4107 break;
4108
4109 case X86_IRQ_ALLOC_TYPE_HPET:
4110 case X86_IRQ_ALLOC_TYPE_MSI:
4111 case X86_IRQ_ALLOC_TYPE_MSIX:
4112 msg->address_hi = MSI_ADDR_BASE_HI;
4113 msg->address_lo = MSI_ADDR_BASE_LO;
4114 msg->data = irte_info->index;
4115 break;
4116
4117 default:
4118 BUG_ON(1);
4119 break;
4120 }
4121}
4122
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05004123struct amd_irte_ops irte_32_ops = {
4124 .prepare = irte_prepare,
4125 .activate = irte_activate,
4126 .deactivate = irte_deactivate,
4127 .set_affinity = irte_set_affinity,
4128 .set_allocated = irte_set_allocated,
4129 .is_allocated = irte_is_allocated,
4130 .clear_allocated = irte_clear_allocated,
4131};
4132
4133struct amd_irte_ops irte_128_ops = {
4134 .prepare = irte_ga_prepare,
4135 .activate = irte_ga_activate,
4136 .deactivate = irte_ga_deactivate,
4137 .set_affinity = irte_ga_set_affinity,
4138 .set_allocated = irte_ga_set_allocated,
4139 .is_allocated = irte_ga_is_allocated,
4140 .clear_allocated = irte_ga_clear_allocated,
4141};
4142
Jiang Liu7c71d302015-04-13 14:11:33 +08004143static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
4144 unsigned int nr_irqs, void *arg)
4145{
4146 struct irq_alloc_info *info = arg;
4147 struct irq_data *irq_data;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004148 struct amd_ir_data *data = NULL;
Jiang Liu7c71d302015-04-13 14:11:33 +08004149 struct irq_cfg *cfg;
4150 int i, ret, devid;
4151 int index = -1;
4152
4153 if (!info)
4154 return -EINVAL;
4155 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
4156 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
4157 return -EINVAL;
4158
4159 /*
4160 * With IRQ remapping enabled, don't need contiguous CPU vectors
4161 * to support multiple MSI interrupts.
4162 */
4163 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
4164 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
4165
4166 devid = get_devid(info);
4167 if (devid < 0)
4168 return -EINVAL;
4169
4170 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
4171 if (ret < 0)
4172 return ret;
4173
Jiang Liu7c71d302015-04-13 14:11:33 +08004174 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
4175 if (get_irq_table(devid, true))
4176 index = info->ioapic_pin;
4177 else
4178 ret = -ENOMEM;
4179 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08004180 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08004181 }
4182 if (index < 0) {
4183 pr_warn("Failed to allocate IRTE\n");
Wei Yongjun517abe42016-07-28 02:10:26 +00004184 ret = index;
Jiang Liu7c71d302015-04-13 14:11:33 +08004185 goto out_free_parent;
4186 }
4187
4188 for (i = 0; i < nr_irqs; i++) {
4189 irq_data = irq_domain_get_irq_data(domain, virq + i);
4190 cfg = irqd_cfg(irq_data);
4191 if (!irq_data || !cfg) {
4192 ret = -EINVAL;
4193 goto out_free_data;
4194 }
4195
Joerg Roedela130e692015-08-13 11:07:25 +02004196 ret = -ENOMEM;
4197 data = kzalloc(sizeof(*data), GFP_KERNEL);
4198 if (!data)
4199 goto out_free_data;
4200
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004201 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
4202 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
4203 else
4204 data->entry = kzalloc(sizeof(struct irte_ga),
4205 GFP_KERNEL);
4206 if (!data->entry) {
4207 kfree(data);
4208 goto out_free_data;
4209 }
4210
Jiang Liu7c71d302015-04-13 14:11:33 +08004211 irq_data->hwirq = (devid << 16) + i;
4212 irq_data->chip_data = data;
4213 irq_data->chip = &amd_ir_chip;
4214 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
4215 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
4216 }
Joerg Roedela130e692015-08-13 11:07:25 +02004217
Jiang Liu7c71d302015-04-13 14:11:33 +08004218 return 0;
4219
4220out_free_data:
4221 for (i--; i >= 0; i--) {
4222 irq_data = irq_domain_get_irq_data(domain, virq + i);
4223 if (irq_data)
4224 kfree(irq_data->chip_data);
4225 }
4226 for (i = 0; i < nr_irqs; i++)
4227 free_irte(devid, index + i);
4228out_free_parent:
4229 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4230 return ret;
4231}
4232
4233static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
4234 unsigned int nr_irqs)
4235{
4236 struct irq_2_irte *irte_info;
4237 struct irq_data *irq_data;
4238 struct amd_ir_data *data;
4239 int i;
4240
4241 for (i = 0; i < nr_irqs; i++) {
4242 irq_data = irq_domain_get_irq_data(domain, virq + i);
4243 if (irq_data && irq_data->chip_data) {
4244 data = irq_data->chip_data;
4245 irte_info = &data->irq_2_irte;
4246 free_irte(irte_info->devid, irte_info->index);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004247 kfree(data->entry);
Jiang Liu7c71d302015-04-13 14:11:33 +08004248 kfree(data);
4249 }
4250 }
4251 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4252}
4253
4254static void irq_remapping_activate(struct irq_domain *domain,
4255 struct irq_data *irq_data)
4256{
4257 struct amd_ir_data *data = irq_data->chip_data;
4258 struct irq_2_irte *irte_info = &data->irq_2_irte;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004259 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004260
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004261 if (iommu)
4262 iommu->irte_ops->activate(data->entry, irte_info->devid,
4263 irte_info->index);
Jiang Liu7c71d302015-04-13 14:11:33 +08004264}
4265
4266static void irq_remapping_deactivate(struct irq_domain *domain,
4267 struct irq_data *irq_data)
4268{
4269 struct amd_ir_data *data = irq_data->chip_data;
4270 struct irq_2_irte *irte_info = &data->irq_2_irte;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004271 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004272
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004273 if (iommu)
4274 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
4275 irte_info->index);
Jiang Liu7c71d302015-04-13 14:11:33 +08004276}
4277
4278static struct irq_domain_ops amd_ir_domain_ops = {
4279 .alloc = irq_remapping_alloc,
4280 .free = irq_remapping_free,
4281 .activate = irq_remapping_activate,
4282 .deactivate = irq_remapping_deactivate,
4283};
4284
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004285static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
4286{
4287 struct amd_iommu *iommu;
4288 struct amd_iommu_pi_data *pi_data = vcpu_info;
4289 struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
4290 struct amd_ir_data *ir_data = data->chip_data;
4291 struct irte_ga *irte = (struct irte_ga *) ir_data->entry;
4292 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05004293 struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
4294
4295 /* Note:
4296 * This device has never been set up for guest mode.
4297 * we should not modify the IRTE
4298 */
4299 if (!dev_data || !dev_data->use_vapic)
4300 return 0;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004301
4302 pi_data->ir_data = ir_data;
4303
4304 /* Note:
4305 * SVM tries to set up for VAPIC mode, but we are in
4306 * legacy mode. So, we force legacy mode instead.
4307 */
4308 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
4309 pr_debug("AMD-Vi: %s: Fall back to using intr legacy remap\n",
4310 __func__);
4311 pi_data->is_guest_mode = false;
4312 }
4313
4314 iommu = amd_iommu_rlookup_table[irte_info->devid];
4315 if (iommu == NULL)
4316 return -EINVAL;
4317
4318 pi_data->prev_ga_tag = ir_data->cached_ga_tag;
4319 if (pi_data->is_guest_mode) {
4320 /* Setting */
4321 irte->hi.fields.ga_root_ptr = (pi_data->base >> 12);
4322 irte->hi.fields.vector = vcpu_pi_info->vector;
Suravee Suthikulpanitbe5c6ef2017-07-05 21:29:59 -05004323 irte->lo.fields_vapic.ga_log_intr = 1;
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004324 irte->lo.fields_vapic.guest_mode = 1;
4325 irte->lo.fields_vapic.ga_tag = pi_data->ga_tag;
4326
4327 ir_data->cached_ga_tag = pi_data->ga_tag;
4328 } else {
4329 /* Un-Setting */
4330 struct irq_cfg *cfg = irqd_cfg(data);
4331
4332 irte->hi.val = 0;
4333 irte->lo.val = 0;
4334 irte->hi.fields.vector = cfg->vector;
4335 irte->lo.fields_remap.guest_mode = 0;
4336 irte->lo.fields_remap.destination = cfg->dest_apicid;
4337 irte->lo.fields_remap.int_type = apic->irq_delivery_mode;
4338 irte->lo.fields_remap.dm = apic->irq_dest_mode;
4339
4340 /*
4341 * This communicates the ga_tag back to the caller
4342 * so that it can do all the necessary clean up.
4343 */
4344 ir_data->cached_ga_tag = 0;
4345 }
4346
4347 return modify_irte_ga(irte_info->devid, irte_info->index, irte, ir_data);
4348}
4349
Jiang Liu7c71d302015-04-13 14:11:33 +08004350static int amd_ir_set_affinity(struct irq_data *data,
4351 const struct cpumask *mask, bool force)
4352{
4353 struct amd_ir_data *ir_data = data->chip_data;
4354 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4355 struct irq_cfg *cfg = irqd_cfg(data);
4356 struct irq_data *parent = data->parent_data;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004357 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004358 int ret;
4359
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004360 if (!iommu)
4361 return -ENODEV;
4362
Jiang Liu7c71d302015-04-13 14:11:33 +08004363 ret = parent->chip->irq_set_affinity(parent, mask, force);
4364 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4365 return ret;
4366
4367 /*
4368 * Atomically updates the IRTE with the new destination, vector
4369 * and flushes the interrupt entry cache.
4370 */
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004371 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
4372 irte_info->index, cfg->vector, cfg->dest_apicid);
Jiang Liu7c71d302015-04-13 14:11:33 +08004373
4374 /*
4375 * After this point, all the interrupts will start arriving
4376 * at the new destination. So, time to cleanup the previous
4377 * vector allocation.
4378 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004379 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004380
4381 return IRQ_SET_MASK_OK_DONE;
4382}
4383
4384static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4385{
4386 struct amd_ir_data *ir_data = irq_data->chip_data;
4387
4388 *msg = ir_data->msi_entry;
4389}
4390
4391static struct irq_chip amd_ir_chip = {
4392 .irq_ack = ir_ack_apic_edge,
4393 .irq_set_affinity = amd_ir_set_affinity,
Suravee Suthikulpanitb9fc6b52016-08-23 13:52:39 -05004394 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
Jiang Liu7c71d302015-04-13 14:11:33 +08004395 .irq_compose_msi_msg = ir_compose_msi_msg,
4396};
4397
4398int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4399{
4400 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4401 if (!iommu->ir_domain)
4402 return -ENOMEM;
4403
4404 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4405 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4406
4407 return 0;
4408}
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004409
4410int amd_iommu_update_ga(int cpu, bool is_run, void *data)
4411{
4412 unsigned long flags;
4413 struct amd_iommu *iommu;
4414 struct irq_remap_table *irt;
4415 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4416 int devid = ir_data->irq_2_irte.devid;
4417 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4418 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
4419
4420 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4421 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
4422 return 0;
4423
4424 iommu = amd_iommu_rlookup_table[devid];
4425 if (!iommu)
4426 return -ENODEV;
4427
4428 irt = get_irq_table(devid, false);
4429 if (!irt)
4430 return -ENODEV;
4431
4432 spin_lock_irqsave(&irt->lock, flags);
4433
4434 if (ref->lo.fields_vapic.guest_mode) {
4435 if (cpu >= 0)
4436 ref->lo.fields_vapic.destination = cpu;
4437 ref->lo.fields_vapic.is_run = is_run;
4438 barrier();
4439 }
4440
4441 spin_unlock_irqrestore(&irt->lock, flags);
4442
4443 iommu_flush_irt(iommu, devid);
4444 iommu_completion_wait(iommu);
4445 return 0;
4446}
4447EXPORT_SYMBOL(amd_iommu_update_ga);
Joerg Roedel2b324502012-06-21 16:29:10 +02004448#endif