blob: 4025291ea0ae9c57370cdb283ae0b9959585f9a3 [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
106DEFINE_PER_CPU(struct flush_queue, flush_queue);
107
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 */
140};
141
142/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200143 * general struct to manage commands send to an IOMMU
144 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200145struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200146 u32 data[4];
147};
148
Joerg Roedel05152a02012-06-15 16:53:51 +0200149struct kmem_cache *amd_iommu_irq_cache;
150
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200151static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200152static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100153static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700154
Joerg Roedel007b74b2015-12-21 12:53:54 +0100155/*
Joerg Roedel007b74b2015-12-21 12:53:54 +0100156 * Data container for a dma_ops specific protection domain
157 */
158struct dma_ops_domain {
159 /* generic protection domain information */
160 struct protection_domain domain;
161
Joerg Roedel307d5852016-07-05 11:54:04 +0200162 /* IOVA RB-Tree */
163 struct iova_domain iovad;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100164};
165
Joerg Roedel81cd07b2016-07-07 18:01:10 +0200166static struct iova_domain reserved_iova_ranges;
167static struct lock_class_key reserved_rbtree_key;
168
Joerg Roedel15898bb2009-11-24 15:39:42 +0100169/****************************************************************************
170 *
171 * Helper functions
172 *
173 ****************************************************************************/
174
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400175static inline int match_hid_uid(struct device *dev,
176 struct acpihid_map_entry *entry)
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100177{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400178 const char *hid, *uid;
179
180 hid = acpi_device_hid(ACPI_COMPANION(dev));
181 uid = acpi_device_uid(ACPI_COMPANION(dev));
182
183 if (!hid || !(*hid))
184 return -ENODEV;
185
186 if (!uid || !(*uid))
187 return strcmp(hid, entry->hid);
188
189 if (!(*entry->uid))
190 return strcmp(hid, entry->hid);
191
192 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100193}
194
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400195static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedele3156042016-04-08 15:12:24 +0200196{
197 struct pci_dev *pdev = to_pci_dev(dev);
198
199 return PCI_DEVID(pdev->bus->number, pdev->devfn);
200}
201
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400202static inline int get_acpihid_device_id(struct device *dev,
203 struct acpihid_map_entry **entry)
204{
205 struct acpihid_map_entry *p;
206
207 list_for_each_entry(p, &acpihid_map, list) {
208 if (!match_hid_uid(dev, p)) {
209 if (entry)
210 *entry = p;
211 return p->devid;
212 }
213 }
214 return -EINVAL;
215}
216
217static inline int get_device_id(struct device *dev)
218{
219 int devid;
220
221 if (dev_is_pci(dev))
222 devid = get_pci_device_id(dev);
223 else
224 devid = get_acpihid_device_id(dev, NULL);
225
226 return devid;
227}
228
Joerg Roedel15898bb2009-11-24 15:39:42 +0100229static struct protection_domain *to_pdomain(struct iommu_domain *dom)
230{
231 return container_of(dom, struct protection_domain, domain);
232}
233
Joerg Roedelb3311b02016-07-08 13:31:31 +0200234static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
235{
236 BUG_ON(domain->flags != PD_DMA_OPS_MASK);
237 return container_of(domain, struct dma_ops_domain, domain);
238}
239
Joerg Roedelf62dda62011-06-09 12:55:35 +0200240static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200241{
242 struct iommu_dev_data *dev_data;
243 unsigned long flags;
244
245 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
246 if (!dev_data)
247 return NULL;
248
Joerg Roedelf62dda62011-06-09 12:55:35 +0200249 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200250
251 spin_lock_irqsave(&dev_data_list_lock, flags);
252 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
253 spin_unlock_irqrestore(&dev_data_list_lock, flags);
254
255 return dev_data;
256}
257
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200258static struct iommu_dev_data *search_dev_data(u16 devid)
259{
260 struct iommu_dev_data *dev_data;
261 unsigned long flags;
262
263 spin_lock_irqsave(&dev_data_list_lock, flags);
264 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
265 if (dev_data->devid == devid)
266 goto out_unlock;
267 }
268
269 dev_data = NULL;
270
271out_unlock:
272 spin_unlock_irqrestore(&dev_data_list_lock, flags);
273
274 return dev_data;
275}
276
Joerg Roedele3156042016-04-08 15:12:24 +0200277static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
278{
279 *(u16 *)data = alias;
280 return 0;
281}
282
283static u16 get_alias(struct device *dev)
284{
285 struct pci_dev *pdev = to_pci_dev(dev);
286 u16 devid, ivrs_alias, pci_alias;
287
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200288 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200289 devid = get_device_id(dev);
290 ivrs_alias = amd_iommu_alias_table[devid];
291 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
292
293 if (ivrs_alias == pci_alias)
294 return ivrs_alias;
295
296 /*
297 * DMA alias showdown
298 *
299 * The IVRS is fairly reliable in telling us about aliases, but it
300 * can't know about every screwy device. If we don't have an IVRS
301 * reported alias, use the PCI reported alias. In that case we may
302 * still need to initialize the rlookup and dev_table entries if the
303 * alias is to a non-existent device.
304 */
305 if (ivrs_alias == devid) {
306 if (!amd_iommu_rlookup_table[pci_alias]) {
307 amd_iommu_rlookup_table[pci_alias] =
308 amd_iommu_rlookup_table[devid];
309 memcpy(amd_iommu_dev_table[pci_alias].data,
310 amd_iommu_dev_table[devid].data,
311 sizeof(amd_iommu_dev_table[pci_alias].data));
312 }
313
314 return pci_alias;
315 }
316
317 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
318 "for device %s[%04x:%04x], kernel reported alias "
319 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
320 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
321 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
322 PCI_FUNC(pci_alias));
323
324 /*
325 * If we don't have a PCI DMA alias and the IVRS alias is on the same
326 * bus, then the IVRS table may know about a quirk that we don't.
327 */
328 if (pci_alias == devid &&
329 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700330 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Joerg Roedele3156042016-04-08 15:12:24 +0200331 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
332 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
333 dev_name(dev));
334 }
335
336 return ivrs_alias;
337}
338
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200339static struct iommu_dev_data *find_dev_data(u16 devid)
340{
341 struct iommu_dev_data *dev_data;
342
343 dev_data = search_dev_data(devid);
344
345 if (dev_data == NULL)
346 dev_data = alloc_dev_data(devid);
347
348 return dev_data;
349}
350
Joerg Roedel657cbb62009-11-23 15:26:46 +0100351static struct iommu_dev_data *get_dev_data(struct device *dev)
352{
353 return dev->archdata.iommu;
354}
355
Wan Zongshunb097d112016-04-01 09:06:04 -0400356/*
357* Find or create an IOMMU group for a acpihid device.
358*/
359static struct iommu_group *acpihid_device_group(struct device *dev)
360{
361 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300362 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400363
364 devid = get_acpihid_device_id(dev, &entry);
365 if (devid < 0)
366 return ERR_PTR(devid);
367
368 list_for_each_entry(p, &acpihid_map, list) {
369 if ((devid == p->devid) && p->group)
370 entry->group = p->group;
371 }
372
373 if (!entry->group)
374 entry->group = generic_device_group(dev);
375
376 return entry->group;
377}
378
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100379static bool pci_iommuv2_capable(struct pci_dev *pdev)
380{
381 static const int caps[] = {
382 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100383 PCI_EXT_CAP_ID_PRI,
384 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100385 };
386 int i, pos;
387
388 for (i = 0; i < 3; ++i) {
389 pos = pci_find_ext_capability(pdev, caps[i]);
390 if (pos == 0)
391 return false;
392 }
393
394 return true;
395}
396
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100397static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
398{
399 struct iommu_dev_data *dev_data;
400
401 dev_data = get_dev_data(&pdev->dev);
402
403 return dev_data->errata & (1 << erratum) ? true : false;
404}
405
Joerg Roedel71c70982009-11-24 16:43:06 +0100406/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100407 * This function checks if the driver got a valid device from the caller to
408 * avoid dereferencing invalid pointers.
409 */
410static bool check_device(struct device *dev)
411{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400412 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100413
414 if (!dev || !dev->dma_mask)
415 return false;
416
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100417 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200418 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400419 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100420
421 /* Out of our scope? */
422 if (devid > amd_iommu_last_bdf)
423 return false;
424
425 if (amd_iommu_rlookup_table[devid] == NULL)
426 return false;
427
428 return true;
429}
430
Alex Williamson25b11ce2014-09-19 10:03:13 -0600431static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600432{
Alex Williamson2851db22012-10-08 22:49:41 -0600433 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600434
Alex Williamson65d53522014-07-03 09:51:30 -0600435 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200436 if (IS_ERR(group))
437 return;
438
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200439 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600440}
441
442static int iommu_init_device(struct device *dev)
443{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600444 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400445 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600446
447 if (dev->archdata.iommu)
448 return 0;
449
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400450 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200451 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400452 return devid;
453
454 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600455 if (!dev_data)
456 return -ENOMEM;
457
Joerg Roedele3156042016-04-08 15:12:24 +0200458 dev_data->alias = get_alias(dev);
459
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400460 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100461 struct amd_iommu *iommu;
462
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400463 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100464 dev_data->iommu_v2 = iommu->is_iommu_v2;
465 }
466
Joerg Roedel657cbb62009-11-23 15:26:46 +0100467 dev->archdata.iommu = dev_data;
468
Alex Williamson066f2e92014-06-12 16:12:37 -0600469 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
470 dev);
471
Joerg Roedel657cbb62009-11-23 15:26:46 +0100472 return 0;
473}
474
Joerg Roedel26018872011-06-06 16:50:14 +0200475static void iommu_ignore_device(struct device *dev)
476{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400477 u16 alias;
478 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200479
480 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200481 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400482 return;
483
Joerg Roedele3156042016-04-08 15:12:24 +0200484 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200485
486 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
487 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
488
489 amd_iommu_rlookup_table[devid] = NULL;
490 amd_iommu_rlookup_table[alias] = NULL;
491}
492
Joerg Roedel657cbb62009-11-23 15:26:46 +0100493static void iommu_uninit_device(struct device *dev)
494{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400495 int devid;
496 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600497
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400498 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200499 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400500 return;
501
502 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600503 if (!dev_data)
504 return;
505
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100506 if (dev_data->domain)
507 detach_device(dev);
508
Alex Williamson066f2e92014-06-12 16:12:37 -0600509 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
510 dev);
511
Alex Williamson9dcd6132012-05-30 14:19:07 -0600512 iommu_group_remove_device(dev);
513
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200514 /* Remove dma-ops */
515 dev->archdata.dma_ops = NULL;
516
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200517 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600518 * We keep dev_data around for unplugged devices and reuse it when the
519 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200520 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100521}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100522
Joerg Roedel431b2a22008-07-11 17:14:22 +0200523/****************************************************************************
524 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200525 * Interrupt handling functions
526 *
527 ****************************************************************************/
528
Joerg Roedele3e59872009-09-03 14:02:10 +0200529static void dump_dte_entry(u16 devid)
530{
531 int i;
532
Joerg Roedelee6c2862011-11-09 12:06:03 +0100533 for (i = 0; i < 4; ++i)
534 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200535 amd_iommu_dev_table[devid].data[i]);
536}
537
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200538static void dump_command(unsigned long phys_addr)
539{
540 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
541 int i;
542
543 for (i = 0; i < 4; ++i)
544 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
545}
546
Joerg Roedela345b232009-09-03 15:01:43 +0200547static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200548{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200549 int type, devid, domid, flags;
550 volatile u32 *event = __evt;
551 int count = 0;
552 u64 address;
553
554retry:
555 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
556 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
557 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
558 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
559 address = (u64)(((u64)event[3]) << 32) | event[2];
560
561 if (type == 0) {
562 /* Did we hit the erratum? */
563 if (++count == LOOP_TIMEOUT) {
564 pr_err("AMD-Vi: No event written to event log\n");
565 return;
566 }
567 udelay(1);
568 goto retry;
569 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200570
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200571 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200572
573 switch (type) {
574 case EVENT_TYPE_ILL_DEV:
575 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
576 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700577 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200578 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200579 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200580 break;
581 case EVENT_TYPE_IO_FAULT:
582 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
583 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700584 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200585 domid, address, flags);
586 break;
587 case EVENT_TYPE_DEV_TAB_ERR:
588 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
589 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700590 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200591 address, flags);
592 break;
593 case EVENT_TYPE_PAGE_TAB_ERR:
594 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
595 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700596 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200597 domid, address, flags);
598 break;
599 case EVENT_TYPE_ILL_CMD:
600 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200601 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200602 break;
603 case EVENT_TYPE_CMD_HARD_ERR:
604 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
605 "flags=0x%04x]\n", address, flags);
606 break;
607 case EVENT_TYPE_IOTLB_INV_TO:
608 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
609 "address=0x%016llx]\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 address);
612 break;
613 case EVENT_TYPE_INV_DEV_REQ:
614 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
615 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700616 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200617 address, flags);
618 break;
619 default:
620 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
621 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200622
623 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200624}
625
626static void iommu_poll_events(struct amd_iommu *iommu)
627{
628 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200629
630 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
631 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
632
633 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200634 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200635 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200636 }
637
638 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200639}
640
Joerg Roedeleee53532012-06-01 15:20:23 +0200641static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100642{
643 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100644
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100645 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
646 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
647 return;
648 }
649
650 fault.address = raw[1];
651 fault.pasid = PPR_PASID(raw[0]);
652 fault.device_id = PPR_DEVID(raw[0]);
653 fault.tag = PPR_TAG(raw[0]);
654 fault.flags = PPR_FLAGS(raw[0]);
655
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100656 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
657}
658
659static void iommu_poll_ppr_log(struct amd_iommu *iommu)
660{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100661 u32 head, tail;
662
663 if (iommu->ppr_log == NULL)
664 return;
665
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100666 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
667 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
668
669 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200670 volatile u64 *raw;
671 u64 entry[2];
672 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100673
Joerg Roedeleee53532012-06-01 15:20:23 +0200674 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100675
Joerg Roedeleee53532012-06-01 15:20:23 +0200676 /*
677 * Hardware bug: Interrupt may arrive before the entry is
678 * written to memory. If this happens we need to wait for the
679 * entry to arrive.
680 */
681 for (i = 0; i < LOOP_TIMEOUT; ++i) {
682 if (PPR_REQ_TYPE(raw[0]) != 0)
683 break;
684 udelay(1);
685 }
686
687 /* Avoid memcpy function-call overhead */
688 entry[0] = raw[0];
689 entry[1] = raw[1];
690
691 /*
692 * To detect the hardware bug we need to clear the entry
693 * back to zero.
694 */
695 raw[0] = raw[1] = 0UL;
696
697 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100698 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
699 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200700
Joerg Roedeleee53532012-06-01 15:20:23 +0200701 /* Handle PPR entry */
702 iommu_handle_ppr_entry(iommu, entry);
703
Joerg Roedeleee53532012-06-01 15:20:23 +0200704 /* Refresh ring-buffer information */
705 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100706 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
707 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100708}
709
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200710irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200711{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500712 struct amd_iommu *iommu = (struct amd_iommu *) data;
713 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200714
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500715 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
716 /* Enable EVT and PPR interrupts again */
717 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
718 iommu->mmio_base + MMIO_STATUS_OFFSET);
719
720 if (status & MMIO_STATUS_EVT_INT_MASK) {
721 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
722 iommu_poll_events(iommu);
723 }
724
725 if (status & MMIO_STATUS_PPR_INT_MASK) {
726 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
727 iommu_poll_ppr_log(iommu);
728 }
729
730 /*
731 * Hardware bug: ERBT1312
732 * When re-enabling interrupt (by writing 1
733 * to clear the bit), the hardware might also try to set
734 * the interrupt bit in the event status register.
735 * In this scenario, the bit will be set, and disable
736 * subsequent interrupts.
737 *
738 * Workaround: The IOMMU driver should read back the
739 * status register and check if the interrupt bits are cleared.
740 * If not, driver will need to go through the interrupt handler
741 * again and re-clear the bits
742 */
743 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100744 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200745 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200746}
747
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200748irqreturn_t amd_iommu_int_handler(int irq, void *data)
749{
750 return IRQ_WAKE_THREAD;
751}
752
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200753/****************************************************************************
754 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200755 * IOMMU command queuing functions
756 *
757 ****************************************************************************/
758
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200759static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200760{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200761 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200762
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200763 while (*sem == 0 && i < LOOP_TIMEOUT) {
764 udelay(1);
765 i += 1;
766 }
767
768 if (i == LOOP_TIMEOUT) {
769 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
770 return -EIO;
771 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200772
773 return 0;
774}
775
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200776static void copy_cmd_to_buffer(struct amd_iommu *iommu,
777 struct iommu_cmd *cmd,
778 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200779{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200780 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200781
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200782 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200783 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200784
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200785 /* Copy command to buffer */
786 memcpy(target, cmd, sizeof(*cmd));
787
788 /* Tell the IOMMU about it */
789 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
790}
791
Joerg Roedel815b33f2011-04-06 17:26:49 +0200792static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200793{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200794 WARN_ON(address & 0x7ULL);
795
Joerg Roedelded46732011-04-06 10:53:48 +0200796 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200797 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
798 cmd->data[1] = upper_32_bits(__pa(address));
799 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200800 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
801}
802
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200803static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
804{
805 memset(cmd, 0, sizeof(*cmd));
806 cmd->data[0] = devid;
807 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
808}
809
Joerg Roedel11b64022011-04-06 11:49:28 +0200810static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
811 size_t size, u16 domid, int pde)
812{
813 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100814 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200815
816 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100817 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200818
819 if (pages > 1) {
820 /*
821 * If we have to flush more than one page, flush all
822 * TLB entries for this domain
823 */
824 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100825 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200826 }
827
828 address &= PAGE_MASK;
829
830 memset(cmd, 0, sizeof(*cmd));
831 cmd->data[1] |= domid;
832 cmd->data[2] = lower_32_bits(address);
833 cmd->data[3] = upper_32_bits(address);
834 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
835 if (s) /* size bit - we flush more than one 4kb page */
836 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200837 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200838 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
839}
840
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200841static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
842 u64 address, size_t size)
843{
844 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100845 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200846
847 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100848 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200849
850 if (pages > 1) {
851 /*
852 * If we have to flush more than one page, flush all
853 * TLB entries for this domain
854 */
855 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100856 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200857 }
858
859 address &= PAGE_MASK;
860
861 memset(cmd, 0, sizeof(*cmd));
862 cmd->data[0] = devid;
863 cmd->data[0] |= (qdep & 0xff) << 24;
864 cmd->data[1] = devid;
865 cmd->data[2] = lower_32_bits(address);
866 cmd->data[3] = upper_32_bits(address);
867 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
868 if (s)
869 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
870}
871
Joerg Roedel22e266c2011-11-21 15:59:08 +0100872static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
873 u64 address, bool size)
874{
875 memset(cmd, 0, sizeof(*cmd));
876
877 address &= ~(0xfffULL);
878
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600879 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100880 cmd->data[1] = domid;
881 cmd->data[2] = lower_32_bits(address);
882 cmd->data[3] = upper_32_bits(address);
883 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
884 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
885 if (size)
886 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
887 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
888}
889
890static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
891 int qdep, u64 address, bool size)
892{
893 memset(cmd, 0, sizeof(*cmd));
894
895 address &= ~(0xfffULL);
896
897 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600898 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100899 cmd->data[0] |= (qdep & 0xff) << 24;
900 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600901 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100902 cmd->data[2] = lower_32_bits(address);
903 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
904 cmd->data[3] = upper_32_bits(address);
905 if (size)
906 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
907 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
908}
909
Joerg Roedelc99afa22011-11-21 18:19:25 +0100910static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
911 int status, int tag, bool gn)
912{
913 memset(cmd, 0, sizeof(*cmd));
914
915 cmd->data[0] = devid;
916 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600917 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100918 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
919 }
920 cmd->data[3] = tag & 0x1ff;
921 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
922
923 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
924}
925
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200926static void build_inv_all(struct iommu_cmd *cmd)
927{
928 memset(cmd, 0, sizeof(*cmd));
929 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200930}
931
Joerg Roedel7ef27982012-06-21 16:46:04 +0200932static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
933{
934 memset(cmd, 0, sizeof(*cmd));
935 cmd->data[0] = devid;
936 CMD_SET_TYPE(cmd, CMD_INV_IRT);
937}
938
Joerg Roedel431b2a22008-07-11 17:14:22 +0200939/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200940 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200941 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200942 */
Joerg Roedel4bf5bee2016-09-14 11:41:59 +0200943static int __iommu_queue_command_sync(struct amd_iommu *iommu,
944 struct iommu_cmd *cmd,
945 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200946{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200947 u32 left, tail, head, next_tail;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200948
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200949again:
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200950
951 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
952 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200953 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
954 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200955
956 if (left <= 2) {
957 struct iommu_cmd sync_cmd;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200958 int ret;
959
Joerg Roedel4bf5bee2016-09-14 11:41:59 +0200960 iommu->cmd_sem = 0;
961
962 build_completion_wait(&sync_cmd, (u64)&iommu->cmd_sem);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200963 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
964
Joerg Roedel4bf5bee2016-09-14 11:41:59 +0200965 if ((ret = wait_on_sem(&iommu->cmd_sem)) != 0)
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200966 return ret;
967
968 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200969 }
970
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200971 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200972
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200973 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200974 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200975
Joerg Roedel4bf5bee2016-09-14 11:41:59 +0200976 return 0;
977}
978
979static int iommu_queue_command_sync(struct amd_iommu *iommu,
980 struct iommu_cmd *cmd,
981 bool sync)
982{
983 unsigned long flags;
984 int ret;
985
986 spin_lock_irqsave(&iommu->lock, flags);
987 ret = __iommu_queue_command_sync(iommu, cmd, sync);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200988 spin_unlock_irqrestore(&iommu->lock, flags);
989
Joerg Roedel4bf5bee2016-09-14 11:41:59 +0200990 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100991}
992
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200993static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
994{
995 return iommu_queue_command_sync(iommu, cmd, true);
996}
997
Joerg Roedel8d201962008-12-02 20:34:41 +0100998/*
999 * This function queues a completion wait command into the command
1000 * buffer of an IOMMU
1001 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001002static int iommu_completion_wait(struct amd_iommu *iommu)
1003{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001004 struct iommu_cmd cmd;
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001005 unsigned long flags;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001006 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001007
1008 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001009 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001010
Joerg Roedel8d201962008-12-02 20:34:41 +01001011
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001012 build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
1013
1014 spin_lock_irqsave(&iommu->lock, flags);
1015
1016 iommu->cmd_sem = 0;
1017
1018 ret = __iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001019 if (ret)
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001020 goto out_unlock;
Joerg Roedel8d201962008-12-02 20:34:41 +01001021
Joerg Roedel4bf5bee2016-09-14 11:41:59 +02001022 ret = wait_on_sem(&iommu->cmd_sem);
1023
1024out_unlock:
1025 spin_unlock_irqrestore(&iommu->lock, flags);
1026
1027 return ret;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001028}
1029
Joerg Roedeld8c13082011-04-06 18:51:26 +02001030static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001031{
1032 struct iommu_cmd cmd;
1033
Joerg Roedeld8c13082011-04-06 18:51:26 +02001034 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001035
Joerg Roedeld8c13082011-04-06 18:51:26 +02001036 return iommu_queue_command(iommu, &cmd);
1037}
1038
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001039static void iommu_flush_dte_all(struct amd_iommu *iommu)
1040{
1041 u32 devid;
1042
1043 for (devid = 0; devid <= 0xffff; ++devid)
1044 iommu_flush_dte(iommu, devid);
1045
1046 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001047}
1048
1049/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001050 * This function uses heavy locking and may disable irqs for some time. But
1051 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001052 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001053static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001054{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001055 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001056
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001057 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1058 struct iommu_cmd cmd;
1059 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1060 dom_id, 1);
1061 iommu_queue_command(iommu, &cmd);
1062 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001063
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001064 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001065}
1066
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001067static void iommu_flush_all(struct amd_iommu *iommu)
1068{
1069 struct iommu_cmd cmd;
1070
1071 build_inv_all(&cmd);
1072
1073 iommu_queue_command(iommu, &cmd);
1074 iommu_completion_wait(iommu);
1075}
1076
Joerg Roedel7ef27982012-06-21 16:46:04 +02001077static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1078{
1079 struct iommu_cmd cmd;
1080
1081 build_inv_irt(&cmd, devid);
1082
1083 iommu_queue_command(iommu, &cmd);
1084}
1085
1086static void iommu_flush_irt_all(struct amd_iommu *iommu)
1087{
1088 u32 devid;
1089
1090 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1091 iommu_flush_irt(iommu, devid);
1092
1093 iommu_completion_wait(iommu);
1094}
1095
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001096void iommu_flush_all_caches(struct amd_iommu *iommu)
1097{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001098 if (iommu_feature(iommu, FEATURE_IA)) {
1099 iommu_flush_all(iommu);
1100 } else {
1101 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001102 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001103 iommu_flush_tlb_all(iommu);
1104 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001105}
1106
Joerg Roedel431b2a22008-07-11 17:14:22 +02001107/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001108 * Command send function for flushing on-device TLB
1109 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001110static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1111 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001112{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001113 struct amd_iommu *iommu;
1114 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001115 int qdep;
1116
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001117 qdep = dev_data->ats.qdep;
1118 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001119
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001120 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001121
1122 return iommu_queue_command(iommu, &cmd);
1123}
1124
1125/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001126 * Command send function for invalidating a device table entry
1127 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001128static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001129{
1130 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001131 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001132 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001133
Joerg Roedel6c542042011-06-09 17:07:31 +02001134 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001135 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001136
Joerg Roedelf62dda62011-06-09 12:55:35 +02001137 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001138 if (!ret && alias != dev_data->devid)
1139 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001140 if (ret)
1141 return ret;
1142
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001143 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001144 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001145
1146 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001147}
1148
Joerg Roedel431b2a22008-07-11 17:14:22 +02001149/*
1150 * TLB invalidation function which is called from the mapping functions.
1151 * It invalidates a single PTE if the range to flush is within a single
1152 * page. Otherwise it flushes the whole TLB of the IOMMU.
1153 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001154static void __domain_flush_pages(struct protection_domain *domain,
1155 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001156{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001157 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001158 struct iommu_cmd cmd;
1159 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001160
Joerg Roedel11b64022011-04-06 11:49:28 +02001161 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001162
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001163 for (i = 0; i < amd_iommus_present; ++i) {
1164 if (!domain->dev_iommu[i])
1165 continue;
1166
1167 /*
1168 * Devices of this domain are behind this IOMMU
1169 * We need a TLB flush
1170 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001171 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001172 }
1173
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001174 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001175
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001176 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001177 continue;
1178
Joerg Roedel6c542042011-06-09 17:07:31 +02001179 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001180 }
1181
Joerg Roedel11b64022011-04-06 11:49:28 +02001182 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001183}
1184
Joerg Roedel17b124b2011-04-06 18:01:35 +02001185static void domain_flush_pages(struct protection_domain *domain,
1186 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001187{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001188 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001189}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001190
Joerg Roedel1c655772008-09-04 18:40:05 +02001191/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001192static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001193{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001194 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001195}
1196
Chris Wright42a49f92009-06-15 15:42:00 +02001197/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001198static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001199{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001200 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1201}
1202
1203static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001204{
1205 int i;
1206
1207 for (i = 0; i < amd_iommus_present; ++i) {
Joerg Roedelf1eae7c2016-07-06 12:50:35 +02001208 if (domain && !domain->dev_iommu[i])
Joerg Roedelb6c02712008-06-26 21:27:53 +02001209 continue;
1210
1211 /*
1212 * Devices of this domain are behind this IOMMU
1213 * We need to wait for completion of all commands.
1214 */
1215 iommu_completion_wait(amd_iommus[i]);
1216 }
1217}
1218
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001219
Joerg Roedel43f49602008-12-02 21:01:12 +01001220/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001221 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001222 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001223static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001224{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001225 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001226
1227 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001228 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001229}
1230
Joerg Roedel431b2a22008-07-11 17:14:22 +02001231/****************************************************************************
1232 *
1233 * The functions below are used the create the page table mappings for
1234 * unity mapped regions.
1235 *
1236 ****************************************************************************/
1237
1238/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001239 * This function is used to add another level to an IO page table. Adding
1240 * another level increases the size of the address space by 9 bits to a size up
1241 * to 64 bits.
1242 */
1243static bool increase_address_space(struct protection_domain *domain,
1244 gfp_t gfp)
1245{
1246 u64 *pte;
1247
1248 if (domain->mode == PAGE_MODE_6_LEVEL)
1249 /* address space already 64 bit large */
1250 return false;
1251
1252 pte = (void *)get_zeroed_page(gfp);
1253 if (!pte)
1254 return false;
1255
1256 *pte = PM_LEVEL_PDE(domain->mode,
1257 virt_to_phys(domain->pt_root));
1258 domain->pt_root = pte;
1259 domain->mode += 1;
1260 domain->updated = true;
1261
1262 return true;
1263}
1264
1265static u64 *alloc_pte(struct protection_domain *domain,
1266 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001267 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001268 u64 **pte_page,
1269 gfp_t gfp)
1270{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001271 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001272 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001273
1274 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001275
1276 while (address > PM_LEVEL_SIZE(domain->mode))
1277 increase_address_space(domain, gfp);
1278
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001279 level = domain->mode - 1;
1280 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1281 address = PAGE_SIZE_ALIGN(address, page_size);
1282 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001283
1284 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001285 u64 __pte, __npte;
1286
1287 __pte = *pte;
1288
1289 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001290 page = (u64 *)get_zeroed_page(gfp);
1291 if (!page)
1292 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001293
1294 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1295
1296 if (cmpxchg64(pte, __pte, __npte)) {
1297 free_page((unsigned long)page);
1298 continue;
1299 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001300 }
1301
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001302 /* No level skipping support yet */
1303 if (PM_PTE_LEVEL(*pte) != level)
1304 return NULL;
1305
Joerg Roedel308973d2009-11-24 17:43:32 +01001306 level -= 1;
1307
1308 pte = IOMMU_PTE_PAGE(*pte);
1309
1310 if (pte_page && level == end_lvl)
1311 *pte_page = pte;
1312
1313 pte = &pte[PM_LEVEL_INDEX(level, address)];
1314 }
1315
1316 return pte;
1317}
1318
1319/*
1320 * This function checks if there is a PTE for a given dma address. If
1321 * there is one, it returns the pointer to it.
1322 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001323static u64 *fetch_pte(struct protection_domain *domain,
1324 unsigned long address,
1325 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001326{
1327 int level;
1328 u64 *pte;
1329
Joerg Roedel24cd7722010-01-19 17:27:39 +01001330 if (address > PM_LEVEL_SIZE(domain->mode))
1331 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001332
Joerg Roedel3039ca12015-04-01 14:58:48 +02001333 level = domain->mode - 1;
1334 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1335 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001336
1337 while (level > 0) {
1338
1339 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001340 if (!IOMMU_PTE_PRESENT(*pte))
1341 return NULL;
1342
Joerg Roedel24cd7722010-01-19 17:27:39 +01001343 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001344 if (PM_PTE_LEVEL(*pte) == 7 ||
1345 PM_PTE_LEVEL(*pte) == 0)
1346 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001347
1348 /* No level skipping support yet */
1349 if (PM_PTE_LEVEL(*pte) != level)
1350 return NULL;
1351
Joerg Roedel308973d2009-11-24 17:43:32 +01001352 level -= 1;
1353
Joerg Roedel24cd7722010-01-19 17:27:39 +01001354 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001355 pte = IOMMU_PTE_PAGE(*pte);
1356 pte = &pte[PM_LEVEL_INDEX(level, address)];
1357 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1358 }
1359
1360 if (PM_PTE_LEVEL(*pte) == 0x07) {
1361 unsigned long pte_mask;
1362
1363 /*
1364 * If we have a series of large PTEs, make
1365 * sure to return a pointer to the first one.
1366 */
1367 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1368 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1369 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001370 }
1371
1372 return pte;
1373}
1374
1375/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001376 * Generic mapping functions. It maps a physical address into a DMA
1377 * address space. It allocates the page table pages if necessary.
1378 * In the future it can be extended to a generic mapping function
1379 * supporting all features of AMD IOMMU page tables like level skipping
1380 * and full 64 bit address spaces.
1381 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001382static int iommu_map_page(struct protection_domain *dom,
1383 unsigned long bus_addr,
1384 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001385 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001386 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001387 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001388{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001389 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001390 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001391
Joerg Roedeld4b03662015-04-01 14:58:52 +02001392 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1393 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1394
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001395 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001396 return -EINVAL;
1397
Joerg Roedeld4b03662015-04-01 14:58:52 +02001398 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001399 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001400
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001401 if (!pte)
1402 return -ENOMEM;
1403
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001404 for (i = 0; i < count; ++i)
1405 if (IOMMU_PTE_PRESENT(pte[i]))
1406 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001407
Joerg Roedeld4b03662015-04-01 14:58:52 +02001408 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001409 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1410 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1411 } else
1412 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1413
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001414 if (prot & IOMMU_PROT_IR)
1415 __pte |= IOMMU_PTE_IR;
1416 if (prot & IOMMU_PROT_IW)
1417 __pte |= IOMMU_PTE_IW;
1418
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001419 for (i = 0; i < count; ++i)
1420 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001421
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001422 update_domain(dom);
1423
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001424 return 0;
1425}
1426
Joerg Roedel24cd7722010-01-19 17:27:39 +01001427static unsigned long iommu_unmap_page(struct protection_domain *dom,
1428 unsigned long bus_addr,
1429 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001430{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001431 unsigned long long unmapped;
1432 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001433 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001434
Joerg Roedel24cd7722010-01-19 17:27:39 +01001435 BUG_ON(!is_power_of_2(page_size));
1436
1437 unmapped = 0;
1438
1439 while (unmapped < page_size) {
1440
Joerg Roedel71b390e2015-04-01 14:58:49 +02001441 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001442
Joerg Roedel71b390e2015-04-01 14:58:49 +02001443 if (pte) {
1444 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001445
Joerg Roedel71b390e2015-04-01 14:58:49 +02001446 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001447 for (i = 0; i < count; i++)
1448 pte[i] = 0ULL;
1449 }
1450
1451 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1452 unmapped += unmap_size;
1453 }
1454
Alex Williamson60d0ca32013-06-21 14:33:19 -06001455 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001456
1457 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001458}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001459
Joerg Roedel431b2a22008-07-11 17:14:22 +02001460/****************************************************************************
1461 *
1462 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001463 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001464 *
1465 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001466
Joerg Roedel9cabe892009-05-18 16:38:55 +02001467
Joerg Roedel256e4622016-07-05 14:23:01 +02001468static unsigned long dma_ops_alloc_iova(struct device *dev,
1469 struct dma_ops_domain *dma_dom,
1470 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001471{
Joerg Roedel256e4622016-07-05 14:23:01 +02001472 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001473
Joerg Roedel256e4622016-07-05 14:23:01 +02001474 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001475
Joerg Roedel256e4622016-07-05 14:23:01 +02001476 if (dma_mask > DMA_BIT_MASK(32))
1477 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1478 IOVA_PFN(DMA_BIT_MASK(32)));
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001479
Joerg Roedel256e4622016-07-05 14:23:01 +02001480 if (!pfn)
1481 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001482
Joerg Roedel256e4622016-07-05 14:23:01 +02001483 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001484}
1485
Joerg Roedel256e4622016-07-05 14:23:01 +02001486static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1487 unsigned long address,
1488 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001489{
Joerg Roedel256e4622016-07-05 14:23:01 +02001490 pages = __roundup_pow_of_two(pages);
1491 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001492
Joerg Roedel256e4622016-07-05 14:23:01 +02001493 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001494}
1495
Joerg Roedel431b2a22008-07-11 17:14:22 +02001496/****************************************************************************
1497 *
1498 * The next functions belong to the domain allocation. A domain is
1499 * allocated for every IOMMU as the default domain. If device isolation
1500 * is enabled, every device get its own domain. The most important thing
1501 * about domains is the page table mapping the DMA address space they
1502 * contain.
1503 *
1504 ****************************************************************************/
1505
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001506/*
1507 * This function adds a protection domain to the global protection domain list
1508 */
1509static void add_domain_to_list(struct protection_domain *domain)
1510{
1511 unsigned long flags;
1512
1513 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1514 list_add(&domain->list, &amd_iommu_pd_list);
1515 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1516}
1517
1518/*
1519 * This function removes a protection domain to the global
1520 * protection domain list
1521 */
1522static void del_domain_from_list(struct protection_domain *domain)
1523{
1524 unsigned long flags;
1525
1526 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1527 list_del(&domain->list);
1528 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1529}
1530
Joerg Roedelec487d12008-06-26 21:27:58 +02001531static u16 domain_id_alloc(void)
1532{
1533 unsigned long flags;
1534 int id;
1535
1536 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1537 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1538 BUG_ON(id == 0);
1539 if (id > 0 && id < MAX_DOMAIN_ID)
1540 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1541 else
1542 id = 0;
1543 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1544
1545 return id;
1546}
1547
Joerg Roedela2acfb72008-12-02 18:28:53 +01001548static void domain_id_free(int id)
1549{
1550 unsigned long flags;
1551
1552 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1553 if (id > 0 && id < MAX_DOMAIN_ID)
1554 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1555 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1556}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001557
Joerg Roedel5c34c402013-06-20 20:22:58 +02001558#define DEFINE_FREE_PT_FN(LVL, FN) \
1559static void free_pt_##LVL (unsigned long __pt) \
1560{ \
1561 unsigned long p; \
1562 u64 *pt; \
1563 int i; \
1564 \
1565 pt = (u64 *)__pt; \
1566 \
1567 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001568 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001569 if (!IOMMU_PTE_PRESENT(pt[i])) \
1570 continue; \
1571 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001572 /* Large PTE? */ \
1573 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1574 PM_PTE_LEVEL(pt[i]) == 7) \
1575 continue; \
1576 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001577 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1578 FN(p); \
1579 } \
1580 free_page((unsigned long)pt); \
1581}
1582
1583DEFINE_FREE_PT_FN(l2, free_page)
1584DEFINE_FREE_PT_FN(l3, free_pt_l2)
1585DEFINE_FREE_PT_FN(l4, free_pt_l3)
1586DEFINE_FREE_PT_FN(l5, free_pt_l4)
1587DEFINE_FREE_PT_FN(l6, free_pt_l5)
1588
Joerg Roedel86db2e52008-12-02 18:20:21 +01001589static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001590{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001591 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001592
Joerg Roedel5c34c402013-06-20 20:22:58 +02001593 switch (domain->mode) {
1594 case PAGE_MODE_NONE:
1595 break;
1596 case PAGE_MODE_1_LEVEL:
1597 free_page(root);
1598 break;
1599 case PAGE_MODE_2_LEVEL:
1600 free_pt_l2(root);
1601 break;
1602 case PAGE_MODE_3_LEVEL:
1603 free_pt_l3(root);
1604 break;
1605 case PAGE_MODE_4_LEVEL:
1606 free_pt_l4(root);
1607 break;
1608 case PAGE_MODE_5_LEVEL:
1609 free_pt_l5(root);
1610 break;
1611 case PAGE_MODE_6_LEVEL:
1612 free_pt_l6(root);
1613 break;
1614 default:
1615 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001616 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001617}
1618
Joerg Roedelb16137b2011-11-21 16:50:23 +01001619static void free_gcr3_tbl_level1(u64 *tbl)
1620{
1621 u64 *ptr;
1622 int i;
1623
1624 for (i = 0; i < 512; ++i) {
1625 if (!(tbl[i] & GCR3_VALID))
1626 continue;
1627
1628 ptr = __va(tbl[i] & PAGE_MASK);
1629
1630 free_page((unsigned long)ptr);
1631 }
1632}
1633
1634static void free_gcr3_tbl_level2(u64 *tbl)
1635{
1636 u64 *ptr;
1637 int i;
1638
1639 for (i = 0; i < 512; ++i) {
1640 if (!(tbl[i] & GCR3_VALID))
1641 continue;
1642
1643 ptr = __va(tbl[i] & PAGE_MASK);
1644
1645 free_gcr3_tbl_level1(ptr);
1646 }
1647}
1648
Joerg Roedel52815b72011-11-17 17:24:28 +01001649static void free_gcr3_table(struct protection_domain *domain)
1650{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001651 if (domain->glx == 2)
1652 free_gcr3_tbl_level2(domain->gcr3_tbl);
1653 else if (domain->glx == 1)
1654 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001655 else
1656 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001657
Joerg Roedel52815b72011-11-17 17:24:28 +01001658 free_page((unsigned long)domain->gcr3_tbl);
1659}
1660
Joerg Roedel431b2a22008-07-11 17:14:22 +02001661/*
1662 * Free a domain, only used if something went wrong in the
1663 * allocation path and we need to free an already allocated page table
1664 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001665static void dma_ops_domain_free(struct dma_ops_domain *dom)
1666{
1667 if (!dom)
1668 return;
1669
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001670 del_domain_from_list(&dom->domain);
1671
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001672 put_iova_domain(&dom->iovad);
1673
Joerg Roedel86db2e52008-12-02 18:20:21 +01001674 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001675
Joerg Roedelec487d12008-06-26 21:27:58 +02001676 kfree(dom);
1677}
1678
Joerg Roedel431b2a22008-07-11 17:14:22 +02001679/*
1680 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001681 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001682 * structures required for the dma_ops interface
1683 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001684static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001685{
1686 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001687
1688 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1689 if (!dma_dom)
1690 return NULL;
1691
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001692 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001693 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001694
Joerg Roedelffec2192016-07-26 15:31:23 +02001695 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001696 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001697 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001698 if (!dma_dom->domain.pt_root)
1699 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001700
Joerg Roedel307d5852016-07-05 11:54:04 +02001701 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1702 IOVA_START_PFN, DMA_32BIT_PFN);
1703
Joerg Roedel81cd07b2016-07-07 18:01:10 +02001704 /* Initialize reserved ranges */
1705 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1706
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001707 add_domain_to_list(&dma_dom->domain);
1708
Joerg Roedelec487d12008-06-26 21:27:58 +02001709 return dma_dom;
1710
1711free_dma_dom:
1712 dma_ops_domain_free(dma_dom);
1713
1714 return NULL;
1715}
1716
Joerg Roedel431b2a22008-07-11 17:14:22 +02001717/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001718 * little helper function to check whether a given protection domain is a
1719 * dma_ops domain
1720 */
1721static bool dma_ops_domain(struct protection_domain *domain)
1722{
1723 return domain->flags & PD_DMA_OPS_MASK;
1724}
1725
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001726static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001727{
Joerg Roedel132bd682011-11-17 14:18:46 +01001728 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001729 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001730
Joerg Roedel132bd682011-11-17 14:18:46 +01001731 if (domain->mode != PAGE_MODE_NONE)
1732 pte_root = virt_to_phys(domain->pt_root);
1733
Joerg Roedel38ddf412008-09-11 10:38:32 +02001734 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1735 << DEV_ENTRY_MODE_SHIFT;
1736 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001737
Joerg Roedelee6c2862011-11-09 12:06:03 +01001738 flags = amd_iommu_dev_table[devid].data[1];
1739
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001740 if (ats)
1741 flags |= DTE_FLAG_IOTLB;
1742
Joerg Roedel52815b72011-11-17 17:24:28 +01001743 if (domain->flags & PD_IOMMUV2_MASK) {
1744 u64 gcr3 = __pa(domain->gcr3_tbl);
1745 u64 glx = domain->glx;
1746 u64 tmp;
1747
1748 pte_root |= DTE_FLAG_GV;
1749 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1750
1751 /* First mask out possible old values for GCR3 table */
1752 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1753 flags &= ~tmp;
1754
1755 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1756 flags &= ~tmp;
1757
1758 /* Encode GCR3 table into DTE */
1759 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1760 pte_root |= tmp;
1761
1762 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1763 flags |= tmp;
1764
1765 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1766 flags |= tmp;
1767 }
1768
Joerg Roedelee6c2862011-11-09 12:06:03 +01001769 flags &= ~(0xffffUL);
1770 flags |= domain->id;
1771
1772 amd_iommu_dev_table[devid].data[1] = flags;
1773 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001774}
1775
Joerg Roedel15898bb2009-11-24 15:39:42 +01001776static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001777{
Joerg Roedel355bf552008-12-08 12:02:41 +01001778 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001779 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1780 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001781
Joerg Roedelc5cca142009-10-09 18:31:20 +02001782 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001783}
1784
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001785static void do_attach(struct iommu_dev_data *dev_data,
1786 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001787{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001788 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001789 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001790 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001791
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001792 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001793 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001794 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001795
1796 /* Update data structures */
1797 dev_data->domain = domain;
1798 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001799
1800 /* Do reference counting */
1801 domain->dev_iommu[iommu->index] += 1;
1802 domain->dev_cnt += 1;
1803
Joerg Roedele25bfb52015-10-20 17:33:38 +02001804 /* Update device table */
1805 set_dte_entry(dev_data->devid, domain, ats);
1806 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08001807 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001808
Joerg Roedel6c542042011-06-09 17:07:31 +02001809 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001810}
1811
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001812static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001813{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001814 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001815 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001816
Joerg Roedel5adad992015-10-09 16:23:33 +02001817 /*
1818 * First check if the device is still attached. It might already
1819 * be detached from its domain because the generic
1820 * iommu_detach_group code detached it and we try again here in
1821 * our alias handling.
1822 */
1823 if (!dev_data->domain)
1824 return;
1825
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001826 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001827 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02001828
Joerg Roedelc4596112009-11-20 14:57:32 +01001829 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001830 dev_data->domain->dev_iommu[iommu->index] -= 1;
1831 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001832
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001833 /* Update data structures */
1834 dev_data->domain = NULL;
1835 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001836 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001837 if (alias != dev_data->devid)
1838 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001839
1840 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001841 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001842}
1843
1844/*
1845 * If a device is not yet associated with a domain, this function does
1846 * assigns it visible for the hardware
1847 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001848static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001849 struct protection_domain *domain)
1850{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001851 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001852
Joerg Roedel272e4f92015-10-20 17:33:37 +02001853 /*
1854 * Must be called with IRQs disabled. Warn here to detect early
1855 * when its not.
1856 */
1857 WARN_ON(!irqs_disabled());
1858
Joerg Roedel15898bb2009-11-24 15:39:42 +01001859 /* lock domain */
1860 spin_lock(&domain->lock);
1861
Joerg Roedel397111a2014-08-05 17:31:51 +02001862 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02001863 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02001864 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01001865
Joerg Roedel397111a2014-08-05 17:31:51 +02001866 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02001867 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001868
Julia Lawall84fe6c12010-05-27 12:31:51 +02001869 ret = 0;
1870
1871out_unlock:
1872
Joerg Roedel355bf552008-12-08 12:02:41 +01001873 /* ready */
1874 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001875
Julia Lawall84fe6c12010-05-27 12:31:51 +02001876 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001877}
1878
Joerg Roedel52815b72011-11-17 17:24:28 +01001879
1880static void pdev_iommuv2_disable(struct pci_dev *pdev)
1881{
1882 pci_disable_ats(pdev);
1883 pci_disable_pri(pdev);
1884 pci_disable_pasid(pdev);
1885}
1886
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001887/* FIXME: Change generic reset-function to do the same */
1888static int pri_reset_while_enabled(struct pci_dev *pdev)
1889{
1890 u16 control;
1891 int pos;
1892
Joerg Roedel46277b72011-12-07 14:34:02 +01001893 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001894 if (!pos)
1895 return -EINVAL;
1896
Joerg Roedel46277b72011-12-07 14:34:02 +01001897 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1898 control |= PCI_PRI_CTRL_RESET;
1899 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001900
1901 return 0;
1902}
1903
Joerg Roedel52815b72011-11-17 17:24:28 +01001904static int pdev_iommuv2_enable(struct pci_dev *pdev)
1905{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001906 bool reset_enable;
1907 int reqs, ret;
1908
1909 /* FIXME: Hardcode number of outstanding requests for now */
1910 reqs = 32;
1911 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1912 reqs = 1;
1913 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01001914
1915 /* Only allow access to user-accessible pages */
1916 ret = pci_enable_pasid(pdev, 0);
1917 if (ret)
1918 goto out_err;
1919
1920 /* First reset the PRI state of the device */
1921 ret = pci_reset_pri(pdev);
1922 if (ret)
1923 goto out_err;
1924
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001925 /* Enable PRI */
1926 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01001927 if (ret)
1928 goto out_err;
1929
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001930 if (reset_enable) {
1931 ret = pri_reset_while_enabled(pdev);
1932 if (ret)
1933 goto out_err;
1934 }
1935
Joerg Roedel52815b72011-11-17 17:24:28 +01001936 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1937 if (ret)
1938 goto out_err;
1939
1940 return 0;
1941
1942out_err:
1943 pci_disable_pri(pdev);
1944 pci_disable_pasid(pdev);
1945
1946 return ret;
1947}
1948
Joerg Roedelc99afa22011-11-21 18:19:25 +01001949/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02001950#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001951
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001952static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001953{
Joerg Roedela3b93122012-04-12 12:49:26 +02001954 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001955 int pos;
1956
Joerg Roedel46277b72011-12-07 14:34:02 +01001957 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001958 if (!pos)
1959 return false;
1960
Joerg Roedela3b93122012-04-12 12:49:26 +02001961 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001962
Joerg Roedela3b93122012-04-12 12:49:26 +02001963 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001964}
1965
Joerg Roedel15898bb2009-11-24 15:39:42 +01001966/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001967 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01001968 * assigns it visible for the hardware
1969 */
1970static int attach_device(struct device *dev,
1971 struct protection_domain *domain)
1972{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001973 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001974 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001975 unsigned long flags;
1976 int ret;
1977
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001978 dev_data = get_dev_data(dev);
1979
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001980 if (!dev_is_pci(dev))
1981 goto skip_ats_check;
1982
1983 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01001984 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02001985 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01001986 return -EINVAL;
1987
Joerg Roedel02ca2022015-07-28 16:58:49 +02001988 if (dev_data->iommu_v2) {
1989 if (pdev_iommuv2_enable(pdev) != 0)
1990 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01001991
Joerg Roedel02ca2022015-07-28 16:58:49 +02001992 dev_data->ats.enabled = true;
1993 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1994 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
1995 }
Joerg Roedel52815b72011-11-17 17:24:28 +01001996 } else if (amd_iommu_iotlb_sup &&
1997 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001998 dev_data->ats.enabled = true;
1999 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2000 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002001
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002002skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01002003 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002004 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002005 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2006
2007 /*
2008 * We might boot into a crash-kernel here. The crashed kernel
2009 * left the caches in the IOMMU dirty. So we have to flush
2010 * here to evict all dirty stuff.
2011 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002012 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002013
2014 return ret;
2015}
2016
2017/*
2018 * Removes a device from a protection domain (unlocked)
2019 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002020static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002021{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002022 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002023
Joerg Roedel272e4f92015-10-20 17:33:37 +02002024 /*
2025 * Must be called with IRQs disabled. Warn here to detect early
2026 * when its not.
2027 */
2028 WARN_ON(!irqs_disabled());
2029
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002030 if (WARN_ON(!dev_data->domain))
2031 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002032
Joerg Roedel2ca76272010-01-22 16:45:31 +01002033 domain = dev_data->domain;
2034
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002035 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002036
Joerg Roedel150952f2015-10-20 17:33:35 +02002037 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002038
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002039 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002040}
2041
2042/*
2043 * Removes a device from a protection domain (with devtable_lock held)
2044 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002045static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002046{
Joerg Roedel52815b72011-11-17 17:24:28 +01002047 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002048 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002049 unsigned long flags;
2050
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002051 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002052 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002053
Joerg Roedel355bf552008-12-08 12:02:41 +01002054 /* lock device table */
2055 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002056 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002057 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002058
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002059 if (!dev_is_pci(dev))
2060 return;
2061
Joerg Roedel02ca2022015-07-28 16:58:49 +02002062 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002063 pdev_iommuv2_disable(to_pci_dev(dev));
2064 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002065 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002066
2067 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002068}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002069
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002070static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002071{
Joerg Roedel71f77582011-06-09 19:03:15 +02002072 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002073 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002074 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002075 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002076
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002077 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002078 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002079
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002080 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002081 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002082 return devid;
2083
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002084 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002085
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002086 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002087 if (ret) {
2088 if (ret != -ENOTSUPP)
2089 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2090 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002091
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002092 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002093 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002094 goto out;
2095 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002096 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002097
Joerg Roedel07ee8692015-05-28 18:41:42 +02002098 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002099
2100 BUG_ON(!dev_data);
2101
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002102 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002103 iommu_request_dm_for_dev(dev);
2104
2105 /* Domains are initialized for this device - have a look what we ended up with */
2106 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002107 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002108 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002109 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002110 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002111
2112out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002113 iommu_completion_wait(iommu);
2114
Joerg Roedele275a2a2008-12-10 18:27:25 +01002115 return 0;
2116}
2117
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002118static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002119{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002120 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002121 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002122
2123 if (!check_device(dev))
2124 return;
2125
2126 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002127 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002128 return;
2129
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002130 iommu = amd_iommu_rlookup_table[devid];
2131
2132 iommu_uninit_device(dev);
2133 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002134}
2135
Wan Zongshunb097d112016-04-01 09:06:04 -04002136static struct iommu_group *amd_iommu_device_group(struct device *dev)
2137{
2138 if (dev_is_pci(dev))
2139 return pci_device_group(dev);
2140
2141 return acpihid_device_group(dev);
2142}
2143
Joerg Roedel431b2a22008-07-11 17:14:22 +02002144/*****************************************************************************
2145 *
2146 * The next functions belong to the dma_ops mapping/unmapping code.
2147 *
2148 *****************************************************************************/
2149
Joerg Roedelb1516a12016-07-06 13:07:22 +02002150static void __queue_flush(struct flush_queue *queue)
2151{
2152 struct protection_domain *domain;
2153 unsigned long flags;
2154 int idx;
2155
2156 /* First flush TLB of all known domains */
2157 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
2158 list_for_each_entry(domain, &amd_iommu_pd_list, list)
2159 domain_flush_tlb(domain);
2160 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
2161
2162 /* Wait until flushes have completed */
2163 domain_flush_complete(NULL);
2164
2165 for (idx = 0; idx < queue->next; ++idx) {
2166 struct flush_queue_entry *entry;
2167
2168 entry = queue->entries + idx;
2169
2170 free_iova_fast(&entry->dma_dom->iovad,
2171 entry->iova_pfn,
2172 entry->pages);
2173
2174 /* Not really necessary, just to make sure we catch any bugs */
2175 entry->dma_dom = NULL;
2176 }
2177
2178 queue->next = 0;
2179}
2180
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002181static void queue_flush_all(void)
Joerg Roedelbb279472016-07-06 13:56:36 +02002182{
2183 int cpu;
2184
Joerg Roedelbb279472016-07-06 13:56:36 +02002185 for_each_possible_cpu(cpu) {
2186 struct flush_queue *queue;
2187 unsigned long flags;
2188
2189 queue = per_cpu_ptr(&flush_queue, cpu);
2190 spin_lock_irqsave(&queue->lock, flags);
2191 if (queue->next > 0)
2192 __queue_flush(queue);
2193 spin_unlock_irqrestore(&queue->lock, flags);
2194 }
2195}
2196
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002197static void queue_flush_timeout(unsigned long unsused)
2198{
2199 atomic_set(&queue_timer_on, 0);
2200 queue_flush_all();
2201}
2202
Joerg Roedelb1516a12016-07-06 13:07:22 +02002203static void queue_add(struct dma_ops_domain *dma_dom,
2204 unsigned long address, unsigned long pages)
2205{
2206 struct flush_queue_entry *entry;
2207 struct flush_queue *queue;
2208 unsigned long flags;
2209 int idx;
2210
2211 pages = __roundup_pow_of_two(pages);
2212 address >>= PAGE_SHIFT;
2213
2214 queue = get_cpu_ptr(&flush_queue);
2215 spin_lock_irqsave(&queue->lock, flags);
2216
2217 if (queue->next == FLUSH_QUEUE_SIZE)
2218 __queue_flush(queue);
2219
2220 idx = queue->next++;
2221 entry = queue->entries + idx;
2222
2223 entry->iova_pfn = address;
2224 entry->pages = pages;
2225 entry->dma_dom = dma_dom;
2226
2227 spin_unlock_irqrestore(&queue->lock, flags);
Joerg Roedelbb279472016-07-06 13:56:36 +02002228
2229 if (atomic_cmpxchg(&queue_timer_on, 0, 1) == 0)
2230 mod_timer(&queue_timer, jiffies + msecs_to_jiffies(10));
2231
Joerg Roedelb1516a12016-07-06 13:07:22 +02002232 put_cpu_ptr(&flush_queue);
2233}
2234
2235
Joerg Roedel431b2a22008-07-11 17:14:22 +02002236/*
2237 * In the dma_ops path we only have the struct device. This function
2238 * finds the corresponding IOMMU, the protection domain and the
2239 * requestor id for a given device.
2240 * If the device is not yet associated with a domain this is also done
2241 * in this function.
2242 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002243static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002244{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002245 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002246
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002247 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002248 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002249
Joerg Roedeld26592a2016-07-07 15:31:13 +02002250 domain = get_dev_data(dev)->domain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002251 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002252 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002253
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002254 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002255}
2256
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002257static void update_device_table(struct protection_domain *domain)
2258{
Joerg Roedel492667d2009-11-27 13:25:47 +01002259 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002260
Joerg Roedel3254de62016-07-26 15:18:54 +02002261 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002262 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel3254de62016-07-26 15:18:54 +02002263
2264 if (dev_data->devid == dev_data->alias)
2265 continue;
2266
2267 /* There is an alias, update device table entry for it */
2268 set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled);
2269 }
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002270}
2271
2272static void update_domain(struct protection_domain *domain)
2273{
2274 if (!domain->updated)
2275 return;
2276
2277 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002278
2279 domain_flush_devices(domain);
2280 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002281
2282 domain->updated = false;
2283}
2284
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002285static int dir2prot(enum dma_data_direction direction)
2286{
2287 if (direction == DMA_TO_DEVICE)
2288 return IOMMU_PROT_IR;
2289 else if (direction == DMA_FROM_DEVICE)
2290 return IOMMU_PROT_IW;
2291 else if (direction == DMA_BIDIRECTIONAL)
2292 return IOMMU_PROT_IW | IOMMU_PROT_IR;
2293 else
2294 return 0;
2295}
Joerg Roedel431b2a22008-07-11 17:14:22 +02002296/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002297 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002298 * contiguous memory region into DMA address space. It is used by all
2299 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002300 * Must be called with the domain lock held.
2301 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002302static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002303 struct dma_ops_domain *dma_dom,
2304 phys_addr_t paddr,
2305 size_t size,
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002306 enum dma_data_direction direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002307 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002308{
2309 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002310 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002311 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002312 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002313 int i;
2314
Joerg Roedele3c449f2008-10-15 22:02:11 -07002315 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002316 paddr &= PAGE_MASK;
2317
Joerg Roedel256e4622016-07-05 14:23:01 +02002318 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002319 if (address == DMA_ERROR_CODE)
2320 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002321
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002322 prot = dir2prot(direction);
Joerg Roedel518d9b42016-07-05 14:39:47 +02002323
Joerg Roedelcb76c322008-06-26 21:28:00 +02002324 start = address;
2325 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002326 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2327 PAGE_SIZE, prot, GFP_ATOMIC);
2328 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002329 goto out_unmap;
2330
Joerg Roedelcb76c322008-06-26 21:28:00 +02002331 paddr += PAGE_SIZE;
2332 start += PAGE_SIZE;
2333 }
2334 address += offset;
2335
Joerg Roedelab7032b2015-12-21 18:47:11 +01002336 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002337 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002338 domain_flush_complete(&dma_dom->domain);
2339 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002340
Joerg Roedelcb76c322008-06-26 21:28:00 +02002341out:
2342 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002343
2344out_unmap:
2345
2346 for (--i; i >= 0; --i) {
2347 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002348 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002349 }
2350
Joerg Roedel256e4622016-07-05 14:23:01 +02002351 domain_flush_tlb(&dma_dom->domain);
2352 domain_flush_complete(&dma_dom->domain);
2353
2354 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002355
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002356 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002357}
2358
Joerg Roedel431b2a22008-07-11 17:14:22 +02002359/*
2360 * Does the reverse of the __map_single function. Must be called with
2361 * the domain lock held too
2362 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002363static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002364 dma_addr_t dma_addr,
2365 size_t size,
2366 int dir)
2367{
Joerg Roedel04e04632010-09-23 16:12:48 +02002368 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002369 dma_addr_t i, start;
2370 unsigned int pages;
2371
Joerg Roedel04e04632010-09-23 16:12:48 +02002372 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002373 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002374 dma_addr &= PAGE_MASK;
2375 start = dma_addr;
2376
2377 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002378 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002379 start += PAGE_SIZE;
2380 }
2381
Joerg Roedelb1516a12016-07-06 13:07:22 +02002382 if (amd_iommu_unmap_flush) {
2383 dma_ops_free_iova(dma_dom, dma_addr, pages);
2384 domain_flush_tlb(&dma_dom->domain);
2385 domain_flush_complete(&dma_dom->domain);
2386 } else {
2387 queue_add(dma_dom, dma_addr, pages);
2388 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002389}
2390
Joerg Roedel431b2a22008-07-11 17:14:22 +02002391/*
2392 * The exported map_single function for dma_ops.
2393 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002394static dma_addr_t map_page(struct device *dev, struct page *page,
2395 unsigned long offset, size_t size,
2396 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002397 unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002398{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002399 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002400 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002401 struct dma_ops_domain *dma_dom;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002402 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002403
Joerg Roedel94f6d192009-11-24 16:40:02 +01002404 domain = get_domain(dev);
2405 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002406 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002407 else if (IS_ERR(domain))
2408 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002409
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002410 dma_mask = *dev->dma_mask;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002411 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002412
Joerg Roedelb3311b02016-07-08 13:31:31 +02002413 return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002414}
2415
Joerg Roedel431b2a22008-07-11 17:14:22 +02002416/*
2417 * The exported unmap_single function for dma_ops.
2418 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002419static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002420 enum dma_data_direction dir, unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002421{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002422 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002423 struct dma_ops_domain *dma_dom;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002424
Joerg Roedel94f6d192009-11-24 16:40:02 +01002425 domain = get_domain(dev);
2426 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002427 return;
2428
Joerg Roedelb3311b02016-07-08 13:31:31 +02002429 dma_dom = to_dma_ops_domain(domain);
2430
2431 __unmap_single(dma_dom, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002432}
2433
Joerg Roedel80187fd2016-07-06 17:20:54 +02002434static int sg_num_pages(struct device *dev,
2435 struct scatterlist *sglist,
2436 int nelems)
2437{
2438 unsigned long mask, boundary_size;
2439 struct scatterlist *s;
2440 int i, npages = 0;
2441
2442 mask = dma_get_seg_boundary(dev);
2443 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
2444 1UL << (BITS_PER_LONG - PAGE_SHIFT);
2445
2446 for_each_sg(sglist, s, nelems, i) {
2447 int p, n;
2448
2449 s->dma_address = npages << PAGE_SHIFT;
2450 p = npages % boundary_size;
2451 n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2452 if (p + n > boundary_size)
2453 npages += boundary_size - p;
2454 npages += n;
2455 }
2456
2457 return npages;
2458}
2459
Joerg Roedel431b2a22008-07-11 17:14:22 +02002460/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002461 * The exported map_sg function for dma_ops (handles scatter-gather
2462 * lists).
2463 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002464static int map_sg(struct device *dev, struct scatterlist *sglist,
Joerg Roedel80187fd2016-07-06 17:20:54 +02002465 int nelems, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002466 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002467{
Joerg Roedel80187fd2016-07-06 17:20:54 +02002468 int mapped_pages = 0, npages = 0, prot = 0, i;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002469 struct protection_domain *domain;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002470 struct dma_ops_domain *dma_dom;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002471 struct scatterlist *s;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002472 unsigned long address;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002473 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002474
Joerg Roedel94f6d192009-11-24 16:40:02 +01002475 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002476 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002477 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002478
Joerg Roedelb3311b02016-07-08 13:31:31 +02002479 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel832a90c2008-09-18 15:54:23 +02002480 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002481
Joerg Roedel80187fd2016-07-06 17:20:54 +02002482 npages = sg_num_pages(dev, sglist, nelems);
2483
2484 address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
2485 if (address == DMA_ERROR_CODE)
2486 goto out_err;
2487
2488 prot = dir2prot(direction);
2489
2490 /* Map all sg entries */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002491 for_each_sg(sglist, s, nelems, i) {
Joerg Roedel80187fd2016-07-06 17:20:54 +02002492 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002493
Joerg Roedel80187fd2016-07-06 17:20:54 +02002494 for (j = 0; j < pages; ++j) {
2495 unsigned long bus_addr, phys_addr;
2496 int ret;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002497
Joerg Roedel80187fd2016-07-06 17:20:54 +02002498 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2499 phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
2500 ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
2501 if (ret)
2502 goto out_unmap;
2503
2504 mapped_pages += 1;
2505 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002506 }
2507
Joerg Roedel80187fd2016-07-06 17:20:54 +02002508 /* Everything is mapped - write the right values into s->dma_address */
2509 for_each_sg(sglist, s, nelems, i) {
2510 s->dma_address += address + s->offset;
2511 s->dma_length = s->length;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002512 }
2513
Joerg Roedel80187fd2016-07-06 17:20:54 +02002514 return nelems;
2515
2516out_unmap:
2517 pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
2518 dev_name(dev), npages);
2519
2520 for_each_sg(sglist, s, nelems, i) {
2521 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2522
2523 for (j = 0; j < pages; ++j) {
2524 unsigned long bus_addr;
2525
2526 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2527 iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
2528
2529 if (--mapped_pages)
2530 goto out_free_iova;
2531 }
2532 }
2533
2534out_free_iova:
2535 free_iova_fast(&dma_dom->iovad, address, npages);
2536
2537out_err:
Joerg Roedel92d420e2015-12-21 19:31:33 +01002538 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002539}
2540
Joerg Roedel431b2a22008-07-11 17:14:22 +02002541/*
2542 * The exported map_sg function for dma_ops (handles scatter-gather
2543 * lists).
2544 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002545static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002546 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002547 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002548{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002549 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002550 struct dma_ops_domain *dma_dom;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002551 unsigned long startaddr;
2552 int npages = 2;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002553
Joerg Roedel94f6d192009-11-24 16:40:02 +01002554 domain = get_domain(dev);
2555 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002556 return;
2557
Joerg Roedel80187fd2016-07-06 17:20:54 +02002558 startaddr = sg_dma_address(sglist) & PAGE_MASK;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002559 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002560 npages = sg_num_pages(dev, sglist, nelems);
2561
Joerg Roedelb3311b02016-07-08 13:31:31 +02002562 __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002563}
2564
Joerg Roedel431b2a22008-07-11 17:14:22 +02002565/*
2566 * The exported alloc_coherent function for dma_ops.
2567 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002568static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002569 dma_addr_t *dma_addr, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002570 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002571{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002572 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002573 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002574 struct dma_ops_domain *dma_dom;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002575 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002576
Joerg Roedel94f6d192009-11-24 16:40:02 +01002577 domain = get_domain(dev);
2578 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002579 page = alloc_pages(flag, get_order(size));
2580 *dma_addr = page_to_phys(page);
2581 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002582 } else if (IS_ERR(domain))
2583 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002584
Joerg Roedelb3311b02016-07-08 13:31:31 +02002585 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel3b839a52015-04-01 14:58:47 +02002586 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002587 dma_mask = dev->coherent_dma_mask;
2588 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002589 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002590
Joerg Roedel3b839a52015-04-01 14:58:47 +02002591 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2592 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002593 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002594 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002595
Joerg Roedel3b839a52015-04-01 14:58:47 +02002596 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2597 get_order(size));
2598 if (!page)
2599 return NULL;
2600 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002601
Joerg Roedel832a90c2008-09-18 15:54:23 +02002602 if (!dma_mask)
2603 dma_mask = *dev->dma_mask;
2604
Joerg Roedelb3311b02016-07-08 13:31:31 +02002605 *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
Joerg Roedelbda350d2016-07-05 16:28:02 +02002606 size, DMA_BIDIRECTIONAL, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002607
Joerg Roedel92d420e2015-12-21 19:31:33 +01002608 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002609 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002610
Joerg Roedel3b839a52015-04-01 14:58:47 +02002611 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002612
2613out_free:
2614
Joerg Roedel3b839a52015-04-01 14:58:47 +02002615 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2616 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002617
2618 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002619}
2620
Joerg Roedel431b2a22008-07-11 17:14:22 +02002621/*
2622 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002623 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002624static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002625 void *virt_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002626 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002627{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002628 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002629 struct dma_ops_domain *dma_dom;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002630 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002631
Joerg Roedel3b839a52015-04-01 14:58:47 +02002632 page = virt_to_page(virt_addr);
2633 size = PAGE_ALIGN(size);
2634
Joerg Roedel94f6d192009-11-24 16:40:02 +01002635 domain = get_domain(dev);
2636 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002637 goto free_mem;
2638
Joerg Roedelb3311b02016-07-08 13:31:31 +02002639 dma_dom = to_dma_ops_domain(domain);
2640
2641 __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002642
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002643free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002644 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2645 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002646}
2647
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002648/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002649 * This function is called by the DMA layer to find out if we can handle a
2650 * particular device. It is part of the dma_ops.
2651 */
2652static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2653{
Joerg Roedel420aef82009-11-23 16:14:57 +01002654 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002655}
2656
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002657static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002658 .alloc = alloc_coherent,
2659 .free = free_coherent,
2660 .map_page = map_page,
2661 .unmap_page = unmap_page,
2662 .map_sg = map_sg,
2663 .unmap_sg = unmap_sg,
2664 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002665};
2666
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002667static int init_reserved_iova_ranges(void)
2668{
2669 struct pci_dev *pdev = NULL;
2670 struct iova *val;
2671
2672 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2673 IOVA_START_PFN, DMA_32BIT_PFN);
2674
2675 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2676 &reserved_rbtree_key);
2677
2678 /* MSI memory range */
2679 val = reserve_iova(&reserved_iova_ranges,
2680 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2681 if (!val) {
2682 pr_err("Reserving MSI range failed\n");
2683 return -ENOMEM;
2684 }
2685
2686 /* HT memory range */
2687 val = reserve_iova(&reserved_iova_ranges,
2688 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2689 if (!val) {
2690 pr_err("Reserving HT range failed\n");
2691 return -ENOMEM;
2692 }
2693
2694 /*
2695 * Memory used for PCI resources
2696 * FIXME: Check whether we can reserve the PCI-hole completly
2697 */
2698 for_each_pci_dev(pdev) {
2699 int i;
2700
2701 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2702 struct resource *r = &pdev->resource[i];
2703
2704 if (!(r->flags & IORESOURCE_MEM))
2705 continue;
2706
2707 val = reserve_iova(&reserved_iova_ranges,
2708 IOVA_PFN(r->start),
2709 IOVA_PFN(r->end));
2710 if (!val) {
2711 pr_err("Reserve pci-resource range failed\n");
2712 return -ENOMEM;
2713 }
2714 }
2715 }
2716
2717 return 0;
2718}
2719
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002720int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002721{
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002722 int ret, cpu, err = 0;
Joerg Roedel307d5852016-07-05 11:54:04 +02002723
2724 ret = iova_cache_get();
2725 if (ret)
2726 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002727
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002728 ret = init_reserved_iova_ranges();
2729 if (ret)
2730 return ret;
2731
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002732 for_each_possible_cpu(cpu) {
2733 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2734
2735 queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
2736 sizeof(*queue->entries),
2737 GFP_KERNEL);
2738 if (!queue->entries)
2739 goto out_put_iova;
2740
2741 spin_lock_init(&queue->lock);
2742 }
2743
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002744 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2745 if (err)
2746 return err;
2747#ifdef CONFIG_ARM_AMBA
2748 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2749 if (err)
2750 return err;
2751#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002752 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2753 if (err)
2754 return err;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002755 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002756
2757out_put_iova:
2758 for_each_possible_cpu(cpu) {
2759 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2760
2761 kfree(queue->entries);
2762 }
2763
2764 return -ENOMEM;
Joerg Roedelf5325092010-01-22 17:44:35 +01002765}
2766
Joerg Roedel6631ee92008-06-26 21:28:05 +02002767int __init amd_iommu_init_dma_ops(void)
2768{
Joerg Roedelbb279472016-07-06 13:56:36 +02002769 setup_timer(&queue_timer, queue_flush_timeout, 0);
2770 atomic_set(&queue_timer_on, 0);
2771
Joerg Roedel32302322015-07-28 16:58:50 +02002772 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002773 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002774
Joerg Roedel52717822015-07-28 16:58:51 +02002775 /*
2776 * In case we don't initialize SWIOTLB (actually the common case
2777 * when AMD IOMMU is enabled), make sure there are global
2778 * dma_ops set as a fall-back for devices not handled by this
2779 * driver (for example non-PCI devices).
2780 */
2781 if (!swiotlb)
2782 dma_ops = &nommu_dma_ops;
2783
Joerg Roedel62410ee2012-06-12 16:42:43 +02002784 if (amd_iommu_unmap_flush)
2785 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2786 else
2787 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2788
Joerg Roedel6631ee92008-06-26 21:28:05 +02002789 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002790
Joerg Roedel6631ee92008-06-26 21:28:05 +02002791}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002792
2793/*****************************************************************************
2794 *
2795 * The following functions belong to the exported interface of AMD IOMMU
2796 *
2797 * This interface allows access to lower level functions of the IOMMU
2798 * like protection domain handling and assignement of devices to domains
2799 * which is not possible with the dma_ops interface.
2800 *
2801 *****************************************************************************/
2802
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002803static void cleanup_domain(struct protection_domain *domain)
2804{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002805 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002806 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002807
2808 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2809
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002810 while (!list_empty(&domain->dev_list)) {
2811 entry = list_first_entry(&domain->dev_list,
2812 struct iommu_dev_data, list);
2813 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002814 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002815
2816 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2817}
2818
Joerg Roedel26508152009-08-26 16:52:40 +02002819static void protection_domain_free(struct protection_domain *domain)
2820{
2821 if (!domain)
2822 return;
2823
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002824 del_domain_from_list(domain);
2825
Joerg Roedel26508152009-08-26 16:52:40 +02002826 if (domain->id)
2827 domain_id_free(domain->id);
2828
2829 kfree(domain);
2830}
2831
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002832static int protection_domain_init(struct protection_domain *domain)
2833{
2834 spin_lock_init(&domain->lock);
2835 mutex_init(&domain->api_lock);
2836 domain->id = domain_id_alloc();
2837 if (!domain->id)
2838 return -ENOMEM;
2839 INIT_LIST_HEAD(&domain->dev_list);
2840
2841 return 0;
2842}
2843
Joerg Roedel26508152009-08-26 16:52:40 +02002844static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002845{
2846 struct protection_domain *domain;
2847
2848 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2849 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002850 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002851
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002852 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002853 goto out_err;
2854
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002855 add_domain_to_list(domain);
2856
Joerg Roedel26508152009-08-26 16:52:40 +02002857 return domain;
2858
2859out_err:
2860 kfree(domain);
2861
2862 return NULL;
2863}
2864
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002865static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2866{
2867 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002868 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002869
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002870 switch (type) {
2871 case IOMMU_DOMAIN_UNMANAGED:
2872 pdomain = protection_domain_alloc();
2873 if (!pdomain)
2874 return NULL;
2875
2876 pdomain->mode = PAGE_MODE_3_LEVEL;
2877 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2878 if (!pdomain->pt_root) {
2879 protection_domain_free(pdomain);
2880 return NULL;
2881 }
2882
2883 pdomain->domain.geometry.aperture_start = 0;
2884 pdomain->domain.geometry.aperture_end = ~0ULL;
2885 pdomain->domain.geometry.force_aperture = true;
2886
2887 break;
2888 case IOMMU_DOMAIN_DMA:
2889 dma_domain = dma_ops_domain_alloc();
2890 if (!dma_domain) {
2891 pr_err("AMD-Vi: Failed to allocate\n");
2892 return NULL;
2893 }
2894 pdomain = &dma_domain->domain;
2895 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002896 case IOMMU_DOMAIN_IDENTITY:
2897 pdomain = protection_domain_alloc();
2898 if (!pdomain)
2899 return NULL;
2900
2901 pdomain->mode = PAGE_MODE_NONE;
2902 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002903 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002904 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002905 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002906
2907 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002908}
2909
2910static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002911{
2912 struct protection_domain *domain;
Joerg Roedelcda70052016-07-07 15:57:04 +02002913 struct dma_ops_domain *dma_dom;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002914
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002915 domain = to_pdomain(dom);
2916
Joerg Roedel98383fc2008-12-02 18:34:12 +01002917 if (domain->dev_cnt > 0)
2918 cleanup_domain(domain);
2919
2920 BUG_ON(domain->dev_cnt != 0);
2921
Joerg Roedelcda70052016-07-07 15:57:04 +02002922 if (!dom)
2923 return;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002924
Joerg Roedelcda70052016-07-07 15:57:04 +02002925 switch (dom->type) {
2926 case IOMMU_DOMAIN_DMA:
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002927 /*
2928 * First make sure the domain is no longer referenced from the
2929 * flush queue
2930 */
2931 queue_flush_all();
2932
2933 /* Now release the domain */
Joerg Roedelb3311b02016-07-08 13:31:31 +02002934 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelcda70052016-07-07 15:57:04 +02002935 dma_ops_domain_free(dma_dom);
2936 break;
2937 default:
2938 if (domain->mode != PAGE_MODE_NONE)
2939 free_pagetable(domain);
Joerg Roedel52815b72011-11-17 17:24:28 +01002940
Joerg Roedelcda70052016-07-07 15:57:04 +02002941 if (domain->flags & PD_IOMMUV2_MASK)
2942 free_gcr3_table(domain);
2943
2944 protection_domain_free(domain);
2945 break;
2946 }
Joerg Roedel98383fc2008-12-02 18:34:12 +01002947}
2948
Joerg Roedel684f2882008-12-08 12:07:44 +01002949static void amd_iommu_detach_device(struct iommu_domain *dom,
2950 struct device *dev)
2951{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002952 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002953 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002954 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01002955
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002956 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002957 return;
2958
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002959 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002960 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002961 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01002962
Joerg Roedel657cbb62009-11-23 15:26:46 +01002963 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002964 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002965
2966 iommu = amd_iommu_rlookup_table[devid];
2967 if (!iommu)
2968 return;
2969
Joerg Roedel684f2882008-12-08 12:07:44 +01002970 iommu_completion_wait(iommu);
2971}
2972
Joerg Roedel01106062008-12-02 19:34:11 +01002973static int amd_iommu_attach_device(struct iommu_domain *dom,
2974 struct device *dev)
2975{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002976 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002977 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002978 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002979 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002980
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002981 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002982 return -EINVAL;
2983
Joerg Roedel657cbb62009-11-23 15:26:46 +01002984 dev_data = dev->archdata.iommu;
2985
Joerg Roedelf62dda62011-06-09 12:55:35 +02002986 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002987 if (!iommu)
2988 return -EINVAL;
2989
Joerg Roedel657cbb62009-11-23 15:26:46 +01002990 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002991 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002992
Joerg Roedel15898bb2009-11-24 15:39:42 +01002993 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002994
2995 iommu_completion_wait(iommu);
2996
Joerg Roedel15898bb2009-11-24 15:39:42 +01002997 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002998}
2999
Joerg Roedel468e2362010-01-21 16:37:36 +01003000static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003001 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003002{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003003 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003004 int prot = 0;
3005 int ret;
3006
Joerg Roedel132bd682011-11-17 14:18:46 +01003007 if (domain->mode == PAGE_MODE_NONE)
3008 return -EINVAL;
3009
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003010 if (iommu_prot & IOMMU_READ)
3011 prot |= IOMMU_PROT_IR;
3012 if (iommu_prot & IOMMU_WRITE)
3013 prot |= IOMMU_PROT_IW;
3014
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003015 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02003016 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003017 mutex_unlock(&domain->api_lock);
3018
Joerg Roedel795e74f72010-05-11 17:40:57 +02003019 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003020}
3021
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003022static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3023 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003024{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003025 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003026 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003027
Joerg Roedel132bd682011-11-17 14:18:46 +01003028 if (domain->mode == PAGE_MODE_NONE)
3029 return -EINVAL;
3030
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003031 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003032 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003033 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003034
Joerg Roedel17b124b2011-04-06 18:01:35 +02003035 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003036
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003037 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003038}
3039
Joerg Roedel645c4c82008-12-02 20:05:50 +01003040static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303041 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003042{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003043 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003044 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003045 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003046
Joerg Roedel132bd682011-11-17 14:18:46 +01003047 if (domain->mode == PAGE_MODE_NONE)
3048 return iova;
3049
Joerg Roedel3039ca12015-04-01 14:58:48 +02003050 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003051
Joerg Roedela6d41a42009-09-02 17:08:55 +02003052 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003053 return 0;
3054
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003055 offset_mask = pte_pgsize - 1;
3056 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003057
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003058 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003059}
3060
Joerg Roedelab636482014-09-05 10:48:21 +02003061static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003062{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003063 switch (cap) {
3064 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003065 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003066 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003067 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003068 case IOMMU_CAP_NOEXEC:
3069 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003070 }
3071
Joerg Roedelab636482014-09-05 10:48:21 +02003072 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003073}
3074
Joerg Roedel35cf2482015-05-28 18:41:37 +02003075static void amd_iommu_get_dm_regions(struct device *dev,
3076 struct list_head *head)
3077{
3078 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003079 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003080
3081 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003082 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003083 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003084
3085 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3086 struct iommu_dm_region *region;
3087
3088 if (devid < entry->devid_start || devid > entry->devid_end)
3089 continue;
3090
3091 region = kzalloc(sizeof(*region), GFP_KERNEL);
3092 if (!region) {
3093 pr_err("Out of memory allocating dm-regions for %s\n",
3094 dev_name(dev));
3095 return;
3096 }
3097
3098 region->start = entry->address_start;
3099 region->length = entry->address_end - entry->address_start;
3100 if (entry->prot & IOMMU_PROT_IR)
3101 region->prot |= IOMMU_READ;
3102 if (entry->prot & IOMMU_PROT_IW)
3103 region->prot |= IOMMU_WRITE;
3104
3105 list_add_tail(&region->list, head);
3106 }
3107}
3108
3109static void amd_iommu_put_dm_regions(struct device *dev,
3110 struct list_head *head)
3111{
3112 struct iommu_dm_region *entry, *next;
3113
3114 list_for_each_entry_safe(entry, next, head, list)
3115 kfree(entry);
3116}
3117
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003118static void amd_iommu_apply_dm_region(struct device *dev,
3119 struct iommu_domain *domain,
3120 struct iommu_dm_region *region)
3121{
Joerg Roedelb3311b02016-07-08 13:31:31 +02003122 struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003123 unsigned long start, end;
3124
3125 start = IOVA_PFN(region->start);
3126 end = IOVA_PFN(region->start + region->length);
3127
3128 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
3129}
3130
Thierry Redingb22f6432014-06-27 09:03:12 +02003131static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003132 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003133 .domain_alloc = amd_iommu_domain_alloc,
3134 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003135 .attach_dev = amd_iommu_attach_device,
3136 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003137 .map = amd_iommu_map,
3138 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003139 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003140 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003141 .add_device = amd_iommu_add_device,
3142 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04003143 .device_group = amd_iommu_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003144 .get_dm_regions = amd_iommu_get_dm_regions,
3145 .put_dm_regions = amd_iommu_put_dm_regions,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003146 .apply_dm_region = amd_iommu_apply_dm_region,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003147 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003148};
3149
Joerg Roedel0feae532009-08-26 15:26:30 +02003150/*****************************************************************************
3151 *
3152 * The next functions do a basic initialization of IOMMU for pass through
3153 * mode
3154 *
3155 * In passthrough mode the IOMMU is initialized and enabled but not used for
3156 * DMA-API translation.
3157 *
3158 *****************************************************************************/
3159
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003160/* IOMMUv2 specific functions */
3161int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3162{
3163 return atomic_notifier_chain_register(&ppr_notifier, nb);
3164}
3165EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3166
3167int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3168{
3169 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3170}
3171EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003172
3173void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3174{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003175 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003176 unsigned long flags;
3177
3178 spin_lock_irqsave(&domain->lock, flags);
3179
3180 /* Update data structure */
3181 domain->mode = PAGE_MODE_NONE;
3182 domain->updated = true;
3183
3184 /* Make changes visible to IOMMUs */
3185 update_domain(domain);
3186
3187 /* Page-table is not visible to IOMMU anymore, so free it */
3188 free_pagetable(domain);
3189
3190 spin_unlock_irqrestore(&domain->lock, flags);
3191}
3192EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003193
3194int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3195{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003196 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003197 unsigned long flags;
3198 int levels, ret;
3199
3200 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3201 return -EINVAL;
3202
3203 /* Number of GCR3 table levels required */
3204 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3205 levels += 1;
3206
3207 if (levels > amd_iommu_max_glx_val)
3208 return -EINVAL;
3209
3210 spin_lock_irqsave(&domain->lock, flags);
3211
3212 /*
3213 * Save us all sanity checks whether devices already in the
3214 * domain support IOMMUv2. Just force that the domain has no
3215 * devices attached when it is switched into IOMMUv2 mode.
3216 */
3217 ret = -EBUSY;
3218 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3219 goto out;
3220
3221 ret = -ENOMEM;
3222 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3223 if (domain->gcr3_tbl == NULL)
3224 goto out;
3225
3226 domain->glx = levels;
3227 domain->flags |= PD_IOMMUV2_MASK;
3228 domain->updated = true;
3229
3230 update_domain(domain);
3231
3232 ret = 0;
3233
3234out:
3235 spin_unlock_irqrestore(&domain->lock, flags);
3236
3237 return ret;
3238}
3239EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003240
3241static int __flush_pasid(struct protection_domain *domain, int pasid,
3242 u64 address, bool size)
3243{
3244 struct iommu_dev_data *dev_data;
3245 struct iommu_cmd cmd;
3246 int i, ret;
3247
3248 if (!(domain->flags & PD_IOMMUV2_MASK))
3249 return -EINVAL;
3250
3251 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3252
3253 /*
3254 * IOMMU TLB needs to be flushed before Device TLB to
3255 * prevent device TLB refill from IOMMU TLB
3256 */
3257 for (i = 0; i < amd_iommus_present; ++i) {
3258 if (domain->dev_iommu[i] == 0)
3259 continue;
3260
3261 ret = iommu_queue_command(amd_iommus[i], &cmd);
3262 if (ret != 0)
3263 goto out;
3264 }
3265
3266 /* Wait until IOMMU TLB flushes are complete */
3267 domain_flush_complete(domain);
3268
3269 /* Now flush device TLBs */
3270 list_for_each_entry(dev_data, &domain->dev_list, list) {
3271 struct amd_iommu *iommu;
3272 int qdep;
3273
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003274 /*
3275 There might be non-IOMMUv2 capable devices in an IOMMUv2
3276 * domain.
3277 */
3278 if (!dev_data->ats.enabled)
3279 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003280
3281 qdep = dev_data->ats.qdep;
3282 iommu = amd_iommu_rlookup_table[dev_data->devid];
3283
3284 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3285 qdep, address, size);
3286
3287 ret = iommu_queue_command(iommu, &cmd);
3288 if (ret != 0)
3289 goto out;
3290 }
3291
3292 /* Wait until all device TLBs are flushed */
3293 domain_flush_complete(domain);
3294
3295 ret = 0;
3296
3297out:
3298
3299 return ret;
3300}
3301
3302static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3303 u64 address)
3304{
3305 return __flush_pasid(domain, pasid, address, false);
3306}
3307
3308int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3309 u64 address)
3310{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003311 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003312 unsigned long flags;
3313 int ret;
3314
3315 spin_lock_irqsave(&domain->lock, flags);
3316 ret = __amd_iommu_flush_page(domain, pasid, address);
3317 spin_unlock_irqrestore(&domain->lock, flags);
3318
3319 return ret;
3320}
3321EXPORT_SYMBOL(amd_iommu_flush_page);
3322
3323static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3324{
3325 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3326 true);
3327}
3328
3329int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3330{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003331 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003332 unsigned long flags;
3333 int ret;
3334
3335 spin_lock_irqsave(&domain->lock, flags);
3336 ret = __amd_iommu_flush_tlb(domain, pasid);
3337 spin_unlock_irqrestore(&domain->lock, flags);
3338
3339 return ret;
3340}
3341EXPORT_SYMBOL(amd_iommu_flush_tlb);
3342
Joerg Roedelb16137b2011-11-21 16:50:23 +01003343static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3344{
3345 int index;
3346 u64 *pte;
3347
3348 while (true) {
3349
3350 index = (pasid >> (9 * level)) & 0x1ff;
3351 pte = &root[index];
3352
3353 if (level == 0)
3354 break;
3355
3356 if (!(*pte & GCR3_VALID)) {
3357 if (!alloc)
3358 return NULL;
3359
3360 root = (void *)get_zeroed_page(GFP_ATOMIC);
3361 if (root == NULL)
3362 return NULL;
3363
3364 *pte = __pa(root) | GCR3_VALID;
3365 }
3366
3367 root = __va(*pte & PAGE_MASK);
3368
3369 level -= 1;
3370 }
3371
3372 return pte;
3373}
3374
3375static int __set_gcr3(struct protection_domain *domain, int pasid,
3376 unsigned long cr3)
3377{
3378 u64 *pte;
3379
3380 if (domain->mode != PAGE_MODE_NONE)
3381 return -EINVAL;
3382
3383 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3384 if (pte == NULL)
3385 return -ENOMEM;
3386
3387 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3388
3389 return __amd_iommu_flush_tlb(domain, pasid);
3390}
3391
3392static int __clear_gcr3(struct protection_domain *domain, int pasid)
3393{
3394 u64 *pte;
3395
3396 if (domain->mode != PAGE_MODE_NONE)
3397 return -EINVAL;
3398
3399 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3400 if (pte == NULL)
3401 return 0;
3402
3403 *pte = 0;
3404
3405 return __amd_iommu_flush_tlb(domain, pasid);
3406}
3407
3408int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3409 unsigned long cr3)
3410{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003411 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003412 unsigned long flags;
3413 int ret;
3414
3415 spin_lock_irqsave(&domain->lock, flags);
3416 ret = __set_gcr3(domain, pasid, cr3);
3417 spin_unlock_irqrestore(&domain->lock, flags);
3418
3419 return ret;
3420}
3421EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3422
3423int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3424{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003425 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003426 unsigned long flags;
3427 int ret;
3428
3429 spin_lock_irqsave(&domain->lock, flags);
3430 ret = __clear_gcr3(domain, pasid);
3431 spin_unlock_irqrestore(&domain->lock, flags);
3432
3433 return ret;
3434}
3435EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003436
3437int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3438 int status, int tag)
3439{
3440 struct iommu_dev_data *dev_data;
3441 struct amd_iommu *iommu;
3442 struct iommu_cmd cmd;
3443
3444 dev_data = get_dev_data(&pdev->dev);
3445 iommu = amd_iommu_rlookup_table[dev_data->devid];
3446
3447 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3448 tag, dev_data->pri_tlp);
3449
3450 return iommu_queue_command(iommu, &cmd);
3451}
3452EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003453
3454struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3455{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003456 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003457
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003458 pdomain = get_domain(&pdev->dev);
3459 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003460 return NULL;
3461
3462 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003463 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003464 return NULL;
3465
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003466 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003467}
3468EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003469
3470void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3471{
3472 struct iommu_dev_data *dev_data;
3473
3474 if (!amd_iommu_v2_supported())
3475 return;
3476
3477 dev_data = get_dev_data(&pdev->dev);
3478 dev_data->errata |= (1 << erratum);
3479}
3480EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003481
3482int amd_iommu_device_info(struct pci_dev *pdev,
3483 struct amd_iommu_device_info *info)
3484{
3485 int max_pasids;
3486 int pos;
3487
3488 if (pdev == NULL || info == NULL)
3489 return -EINVAL;
3490
3491 if (!amd_iommu_v2_supported())
3492 return -EINVAL;
3493
3494 memset(info, 0, sizeof(*info));
3495
3496 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3497 if (pos)
3498 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3499
3500 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3501 if (pos)
3502 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3503
3504 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3505 if (pos) {
3506 int features;
3507
3508 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3509 max_pasids = min(max_pasids, (1 << 20));
3510
3511 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3512 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3513
3514 features = pci_pasid_features(pdev);
3515 if (features & PCI_PASID_CAP_EXEC)
3516 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3517 if (features & PCI_PASID_CAP_PRIV)
3518 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3519 }
3520
3521 return 0;
3522}
3523EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003524
3525#ifdef CONFIG_IRQ_REMAP
3526
3527/*****************************************************************************
3528 *
3529 * Interrupt Remapping Implementation
3530 *
3531 *****************************************************************************/
3532
3533union irte {
3534 u32 val;
3535 struct {
3536 u32 valid : 1,
3537 no_fault : 1,
3538 int_type : 3,
3539 rq_eoi : 1,
3540 dm : 1,
3541 rsvd_1 : 1,
3542 destination : 8,
3543 vector : 8,
3544 rsvd_2 : 8;
3545 } fields;
3546};
3547
Jiang Liu9c724962015-04-14 10:29:52 +08003548struct irq_2_irte {
3549 u16 devid; /* Device ID for IRTE table */
3550 u16 index; /* Index into IRTE table*/
3551};
3552
Jiang Liu7c71d302015-04-13 14:11:33 +08003553struct amd_ir_data {
3554 struct irq_2_irte irq_2_irte;
3555 union irte irte_entry;
3556 union {
3557 struct msi_msg msi_entry;
3558 };
3559};
3560
3561static struct irq_chip amd_ir_chip;
3562
Joerg Roedel2b324502012-06-21 16:29:10 +02003563#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3564#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3565#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3566#define DTE_IRQ_REMAP_ENABLE 1ULL
3567
3568static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3569{
3570 u64 dte;
3571
3572 dte = amd_iommu_dev_table[devid].data[2];
3573 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3574 dte |= virt_to_phys(table->table);
3575 dte |= DTE_IRQ_REMAP_INTCTL;
3576 dte |= DTE_IRQ_TABLE_LEN;
3577 dte |= DTE_IRQ_REMAP_ENABLE;
3578
3579 amd_iommu_dev_table[devid].data[2] = dte;
3580}
3581
3582#define IRTE_ALLOCATED (~1U)
3583
3584static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3585{
3586 struct irq_remap_table *table = NULL;
3587 struct amd_iommu *iommu;
3588 unsigned long flags;
3589 u16 alias;
3590
3591 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3592
3593 iommu = amd_iommu_rlookup_table[devid];
3594 if (!iommu)
3595 goto out_unlock;
3596
3597 table = irq_lookup_table[devid];
3598 if (table)
3599 goto out;
3600
3601 alias = amd_iommu_alias_table[devid];
3602 table = irq_lookup_table[alias];
3603 if (table) {
3604 irq_lookup_table[devid] = table;
3605 set_dte_irq_entry(devid, table);
3606 iommu_flush_dte(iommu, devid);
3607 goto out;
3608 }
3609
3610 /* Nothing there yet, allocate new irq remapping table */
3611 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3612 if (!table)
3613 goto out;
3614
Joerg Roedel197887f2013-04-09 21:14:08 +02003615 /* Initialize table spin-lock */
3616 spin_lock_init(&table->lock);
3617
Joerg Roedel2b324502012-06-21 16:29:10 +02003618 if (ioapic)
3619 /* Keep the first 32 indexes free for IOAPIC interrupts */
3620 table->min_index = 32;
3621
3622 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3623 if (!table->table) {
3624 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003625 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003626 goto out;
3627 }
3628
3629 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3630
3631 if (ioapic) {
3632 int i;
3633
3634 for (i = 0; i < 32; ++i)
3635 table->table[i] = IRTE_ALLOCATED;
3636 }
3637
3638 irq_lookup_table[devid] = table;
3639 set_dte_irq_entry(devid, table);
3640 iommu_flush_dte(iommu, devid);
3641 if (devid != alias) {
3642 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003643 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003644 iommu_flush_dte(iommu, alias);
3645 }
3646
3647out:
3648 iommu_completion_wait(iommu);
3649
3650out_unlock:
3651 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3652
3653 return table;
3654}
3655
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003656static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003657{
3658 struct irq_remap_table *table;
3659 unsigned long flags;
3660 int index, c;
3661
3662 table = get_irq_table(devid, false);
3663 if (!table)
3664 return -ENODEV;
3665
3666 spin_lock_irqsave(&table->lock, flags);
3667
3668 /* Scan table for free entries */
3669 for (c = 0, index = table->min_index;
3670 index < MAX_IRQS_PER_TABLE;
3671 ++index) {
3672 if (table->table[index] == 0)
3673 c += 1;
3674 else
3675 c = 0;
3676
3677 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003678 for (; c != 0; --c)
3679 table->table[index - c + 1] = IRTE_ALLOCATED;
3680
3681 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003682 goto out;
3683 }
3684 }
3685
3686 index = -ENOSPC;
3687
3688out:
3689 spin_unlock_irqrestore(&table->lock, flags);
3690
3691 return index;
3692}
3693
Joerg Roedel2b324502012-06-21 16:29:10 +02003694static int modify_irte(u16 devid, int index, union irte irte)
3695{
3696 struct irq_remap_table *table;
3697 struct amd_iommu *iommu;
3698 unsigned long flags;
3699
3700 iommu = amd_iommu_rlookup_table[devid];
3701 if (iommu == NULL)
3702 return -EINVAL;
3703
3704 table = get_irq_table(devid, false);
3705 if (!table)
3706 return -ENOMEM;
3707
3708 spin_lock_irqsave(&table->lock, flags);
3709 table->table[index] = irte.val;
3710 spin_unlock_irqrestore(&table->lock, flags);
3711
3712 iommu_flush_irt(iommu, devid);
3713 iommu_completion_wait(iommu);
3714
3715 return 0;
3716}
3717
3718static void free_irte(u16 devid, int index)
3719{
3720 struct irq_remap_table *table;
3721 struct amd_iommu *iommu;
3722 unsigned long flags;
3723
3724 iommu = amd_iommu_rlookup_table[devid];
3725 if (iommu == NULL)
3726 return;
3727
3728 table = get_irq_table(devid, false);
3729 if (!table)
3730 return;
3731
3732 spin_lock_irqsave(&table->lock, flags);
3733 table->table[index] = 0;
3734 spin_unlock_irqrestore(&table->lock, flags);
3735
3736 iommu_flush_irt(iommu, devid);
3737 iommu_completion_wait(iommu);
3738}
3739
Jiang Liu7c71d302015-04-13 14:11:33 +08003740static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003741{
Jiang Liu7c71d302015-04-13 14:11:33 +08003742 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003743
Jiang Liu7c71d302015-04-13 14:11:33 +08003744 switch (info->type) {
3745 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3746 devid = get_ioapic_devid(info->ioapic_id);
3747 break;
3748 case X86_IRQ_ALLOC_TYPE_HPET:
3749 devid = get_hpet_devid(info->hpet_id);
3750 break;
3751 case X86_IRQ_ALLOC_TYPE_MSI:
3752 case X86_IRQ_ALLOC_TYPE_MSIX:
3753 devid = get_device_id(&info->msi_dev->dev);
3754 break;
3755 default:
3756 BUG_ON(1);
3757 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003758 }
3759
Jiang Liu7c71d302015-04-13 14:11:33 +08003760 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003761}
3762
Jiang Liu7c71d302015-04-13 14:11:33 +08003763static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003764{
Jiang Liu7c71d302015-04-13 14:11:33 +08003765 struct amd_iommu *iommu;
3766 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003767
Jiang Liu7c71d302015-04-13 14:11:33 +08003768 if (!info)
3769 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003770
Jiang Liu7c71d302015-04-13 14:11:33 +08003771 devid = get_devid(info);
3772 if (devid >= 0) {
3773 iommu = amd_iommu_rlookup_table[devid];
3774 if (iommu)
3775 return iommu->ir_domain;
3776 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003777
Jiang Liu7c71d302015-04-13 14:11:33 +08003778 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003779}
3780
Jiang Liu7c71d302015-04-13 14:11:33 +08003781static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003782{
Jiang Liu7c71d302015-04-13 14:11:33 +08003783 struct amd_iommu *iommu;
3784 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003785
Jiang Liu7c71d302015-04-13 14:11:33 +08003786 if (!info)
3787 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003788
Jiang Liu7c71d302015-04-13 14:11:33 +08003789 switch (info->type) {
3790 case X86_IRQ_ALLOC_TYPE_MSI:
3791 case X86_IRQ_ALLOC_TYPE_MSIX:
3792 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003793 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003794 return NULL;
3795
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003796 iommu = amd_iommu_rlookup_table[devid];
3797 if (iommu)
3798 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003799 break;
3800 default:
3801 break;
3802 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003803
Jiang Liu7c71d302015-04-13 14:11:33 +08003804 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003805}
3806
Joerg Roedel6b474b82012-06-26 16:46:04 +02003807struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003808 .prepare = amd_iommu_prepare,
3809 .enable = amd_iommu_enable,
3810 .disable = amd_iommu_disable,
3811 .reenable = amd_iommu_reenable,
3812 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003813 .get_ir_irq_domain = get_ir_irq_domain,
3814 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003815};
Jiang Liu7c71d302015-04-13 14:11:33 +08003816
3817static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3818 struct irq_cfg *irq_cfg,
3819 struct irq_alloc_info *info,
3820 int devid, int index, int sub_handle)
3821{
3822 struct irq_2_irte *irte_info = &data->irq_2_irte;
3823 struct msi_msg *msg = &data->msi_entry;
3824 union irte *irte = &data->irte_entry;
3825 struct IO_APIC_route_entry *entry;
3826
Jiang Liu7c71d302015-04-13 14:11:33 +08003827 data->irq_2_irte.devid = devid;
3828 data->irq_2_irte.index = index + sub_handle;
3829
3830 /* Setup IRTE for IOMMU */
3831 irte->val = 0;
3832 irte->fields.vector = irq_cfg->vector;
3833 irte->fields.int_type = apic->irq_delivery_mode;
3834 irte->fields.destination = irq_cfg->dest_apicid;
3835 irte->fields.dm = apic->irq_dest_mode;
3836 irte->fields.valid = 1;
3837
3838 switch (info->type) {
3839 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3840 /* Setup IOAPIC entry */
3841 entry = info->ioapic_entry;
3842 info->ioapic_entry = NULL;
3843 memset(entry, 0, sizeof(*entry));
3844 entry->vector = index;
3845 entry->mask = 0;
3846 entry->trigger = info->ioapic_trigger;
3847 entry->polarity = info->ioapic_polarity;
3848 /* Mask level triggered irqs. */
3849 if (info->ioapic_trigger)
3850 entry->mask = 1;
3851 break;
3852
3853 case X86_IRQ_ALLOC_TYPE_HPET:
3854 case X86_IRQ_ALLOC_TYPE_MSI:
3855 case X86_IRQ_ALLOC_TYPE_MSIX:
3856 msg->address_hi = MSI_ADDR_BASE_HI;
3857 msg->address_lo = MSI_ADDR_BASE_LO;
3858 msg->data = irte_info->index;
3859 break;
3860
3861 default:
3862 BUG_ON(1);
3863 break;
3864 }
3865}
3866
3867static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3868 unsigned int nr_irqs, void *arg)
3869{
3870 struct irq_alloc_info *info = arg;
3871 struct irq_data *irq_data;
3872 struct amd_ir_data *data;
3873 struct irq_cfg *cfg;
3874 int i, ret, devid;
3875 int index = -1;
3876
3877 if (!info)
3878 return -EINVAL;
3879 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3880 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3881 return -EINVAL;
3882
3883 /*
3884 * With IRQ remapping enabled, don't need contiguous CPU vectors
3885 * to support multiple MSI interrupts.
3886 */
3887 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3888 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3889
3890 devid = get_devid(info);
3891 if (devid < 0)
3892 return -EINVAL;
3893
3894 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3895 if (ret < 0)
3896 return ret;
3897
Jiang Liu7c71d302015-04-13 14:11:33 +08003898 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3899 if (get_irq_table(devid, true))
3900 index = info->ioapic_pin;
3901 else
3902 ret = -ENOMEM;
3903 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003904 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003905 }
3906 if (index < 0) {
3907 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003908 goto out_free_parent;
3909 }
3910
3911 for (i = 0; i < nr_irqs; i++) {
3912 irq_data = irq_domain_get_irq_data(domain, virq + i);
3913 cfg = irqd_cfg(irq_data);
3914 if (!irq_data || !cfg) {
3915 ret = -EINVAL;
3916 goto out_free_data;
3917 }
3918
Joerg Roedela130e692015-08-13 11:07:25 +02003919 ret = -ENOMEM;
3920 data = kzalloc(sizeof(*data), GFP_KERNEL);
3921 if (!data)
3922 goto out_free_data;
3923
Jiang Liu7c71d302015-04-13 14:11:33 +08003924 irq_data->hwirq = (devid << 16) + i;
3925 irq_data->chip_data = data;
3926 irq_data->chip = &amd_ir_chip;
3927 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3928 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3929 }
Joerg Roedela130e692015-08-13 11:07:25 +02003930
Jiang Liu7c71d302015-04-13 14:11:33 +08003931 return 0;
3932
3933out_free_data:
3934 for (i--; i >= 0; i--) {
3935 irq_data = irq_domain_get_irq_data(domain, virq + i);
3936 if (irq_data)
3937 kfree(irq_data->chip_data);
3938 }
3939 for (i = 0; i < nr_irqs; i++)
3940 free_irte(devid, index + i);
3941out_free_parent:
3942 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3943 return ret;
3944}
3945
3946static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3947 unsigned int nr_irqs)
3948{
3949 struct irq_2_irte *irte_info;
3950 struct irq_data *irq_data;
3951 struct amd_ir_data *data;
3952 int i;
3953
3954 for (i = 0; i < nr_irqs; i++) {
3955 irq_data = irq_domain_get_irq_data(domain, virq + i);
3956 if (irq_data && irq_data->chip_data) {
3957 data = irq_data->chip_data;
3958 irte_info = &data->irq_2_irte;
3959 free_irte(irte_info->devid, irte_info->index);
3960 kfree(data);
3961 }
3962 }
3963 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3964}
3965
3966static void irq_remapping_activate(struct irq_domain *domain,
3967 struct irq_data *irq_data)
3968{
3969 struct amd_ir_data *data = irq_data->chip_data;
3970 struct irq_2_irte *irte_info = &data->irq_2_irte;
3971
3972 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3973}
3974
3975static void irq_remapping_deactivate(struct irq_domain *domain,
3976 struct irq_data *irq_data)
3977{
3978 struct amd_ir_data *data = irq_data->chip_data;
3979 struct irq_2_irte *irte_info = &data->irq_2_irte;
3980 union irte entry;
3981
3982 entry.val = 0;
3983 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3984}
3985
3986static struct irq_domain_ops amd_ir_domain_ops = {
3987 .alloc = irq_remapping_alloc,
3988 .free = irq_remapping_free,
3989 .activate = irq_remapping_activate,
3990 .deactivate = irq_remapping_deactivate,
3991};
3992
3993static int amd_ir_set_affinity(struct irq_data *data,
3994 const struct cpumask *mask, bool force)
3995{
3996 struct amd_ir_data *ir_data = data->chip_data;
3997 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3998 struct irq_cfg *cfg = irqd_cfg(data);
3999 struct irq_data *parent = data->parent_data;
4000 int ret;
4001
4002 ret = parent->chip->irq_set_affinity(parent, mask, force);
4003 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4004 return ret;
4005
4006 /*
4007 * Atomically updates the IRTE with the new destination, vector
4008 * and flushes the interrupt entry cache.
4009 */
4010 ir_data->irte_entry.fields.vector = cfg->vector;
4011 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
4012 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
4013
4014 /*
4015 * After this point, all the interrupts will start arriving
4016 * at the new destination. So, time to cleanup the previous
4017 * vector allocation.
4018 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004019 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004020
4021 return IRQ_SET_MASK_OK_DONE;
4022}
4023
4024static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4025{
4026 struct amd_ir_data *ir_data = irq_data->chip_data;
4027
4028 *msg = ir_data->msi_entry;
4029}
4030
4031static struct irq_chip amd_ir_chip = {
4032 .irq_ack = ir_ack_apic_edge,
4033 .irq_set_affinity = amd_ir_set_affinity,
4034 .irq_compose_msi_msg = ir_compose_msi_msg,
4035};
4036
4037int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4038{
4039 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4040 if (!iommu->ir_domain)
4041 return -ENOMEM;
4042
4043 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4044 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4045
4046 return 0;
4047}
Joerg Roedel2b324502012-06-21 16:29:10 +02004048#endif