blob: d13a18633dce965c68ad941d8b98ee73fb048b4b [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 Roedelf62dda62011-06-09 12:55:35 +0200234static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200235{
236 struct iommu_dev_data *dev_data;
237 unsigned long flags;
238
239 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
240 if (!dev_data)
241 return NULL;
242
Joerg Roedelf62dda62011-06-09 12:55:35 +0200243 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200244
245 spin_lock_irqsave(&dev_data_list_lock, flags);
246 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
247 spin_unlock_irqrestore(&dev_data_list_lock, flags);
248
249 return dev_data;
250}
251
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200252static struct iommu_dev_data *search_dev_data(u16 devid)
253{
254 struct iommu_dev_data *dev_data;
255 unsigned long flags;
256
257 spin_lock_irqsave(&dev_data_list_lock, flags);
258 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
259 if (dev_data->devid == devid)
260 goto out_unlock;
261 }
262
263 dev_data = NULL;
264
265out_unlock:
266 spin_unlock_irqrestore(&dev_data_list_lock, flags);
267
268 return dev_data;
269}
270
Joerg Roedele3156042016-04-08 15:12:24 +0200271static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
272{
273 *(u16 *)data = alias;
274 return 0;
275}
276
277static u16 get_alias(struct device *dev)
278{
279 struct pci_dev *pdev = to_pci_dev(dev);
280 u16 devid, ivrs_alias, pci_alias;
281
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200282 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200283 devid = get_device_id(dev);
284 ivrs_alias = amd_iommu_alias_table[devid];
285 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
286
287 if (ivrs_alias == pci_alias)
288 return ivrs_alias;
289
290 /*
291 * DMA alias showdown
292 *
293 * The IVRS is fairly reliable in telling us about aliases, but it
294 * can't know about every screwy device. If we don't have an IVRS
295 * reported alias, use the PCI reported alias. In that case we may
296 * still need to initialize the rlookup and dev_table entries if the
297 * alias is to a non-existent device.
298 */
299 if (ivrs_alias == devid) {
300 if (!amd_iommu_rlookup_table[pci_alias]) {
301 amd_iommu_rlookup_table[pci_alias] =
302 amd_iommu_rlookup_table[devid];
303 memcpy(amd_iommu_dev_table[pci_alias].data,
304 amd_iommu_dev_table[devid].data,
305 sizeof(amd_iommu_dev_table[pci_alias].data));
306 }
307
308 return pci_alias;
309 }
310
311 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
312 "for device %s[%04x:%04x], kernel reported alias "
313 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
314 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
315 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
316 PCI_FUNC(pci_alias));
317
318 /*
319 * If we don't have a PCI DMA alias and the IVRS alias is on the same
320 * bus, then the IVRS table may know about a quirk that we don't.
321 */
322 if (pci_alias == devid &&
323 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700324 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Joerg Roedele3156042016-04-08 15:12:24 +0200325 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
326 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
327 dev_name(dev));
328 }
329
330 return ivrs_alias;
331}
332
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200333static struct iommu_dev_data *find_dev_data(u16 devid)
334{
335 struct iommu_dev_data *dev_data;
336
337 dev_data = search_dev_data(devid);
338
339 if (dev_data == NULL)
340 dev_data = alloc_dev_data(devid);
341
342 return dev_data;
343}
344
Joerg Roedel657cbb62009-11-23 15:26:46 +0100345static struct iommu_dev_data *get_dev_data(struct device *dev)
346{
347 return dev->archdata.iommu;
348}
349
Wan Zongshunb097d112016-04-01 09:06:04 -0400350/*
351* Find or create an IOMMU group for a acpihid device.
352*/
353static struct iommu_group *acpihid_device_group(struct device *dev)
354{
355 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300356 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400357
358 devid = get_acpihid_device_id(dev, &entry);
359 if (devid < 0)
360 return ERR_PTR(devid);
361
362 list_for_each_entry(p, &acpihid_map, list) {
363 if ((devid == p->devid) && p->group)
364 entry->group = p->group;
365 }
366
367 if (!entry->group)
368 entry->group = generic_device_group(dev);
369
370 return entry->group;
371}
372
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100373static bool pci_iommuv2_capable(struct pci_dev *pdev)
374{
375 static const int caps[] = {
376 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100377 PCI_EXT_CAP_ID_PRI,
378 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100379 };
380 int i, pos;
381
382 for (i = 0; i < 3; ++i) {
383 pos = pci_find_ext_capability(pdev, caps[i]);
384 if (pos == 0)
385 return false;
386 }
387
388 return true;
389}
390
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100391static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
392{
393 struct iommu_dev_data *dev_data;
394
395 dev_data = get_dev_data(&pdev->dev);
396
397 return dev_data->errata & (1 << erratum) ? true : false;
398}
399
Joerg Roedel71c70982009-11-24 16:43:06 +0100400/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100401 * This function checks if the driver got a valid device from the caller to
402 * avoid dereferencing invalid pointers.
403 */
404static bool check_device(struct device *dev)
405{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400406 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100407
408 if (!dev || !dev->dma_mask)
409 return false;
410
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100411 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200412 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400413 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100414
415 /* Out of our scope? */
416 if (devid > amd_iommu_last_bdf)
417 return false;
418
419 if (amd_iommu_rlookup_table[devid] == NULL)
420 return false;
421
422 return true;
423}
424
Alex Williamson25b11ce2014-09-19 10:03:13 -0600425static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600426{
Alex Williamson2851db22012-10-08 22:49:41 -0600427 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600428
Alex Williamson65d53522014-07-03 09:51:30 -0600429 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200430 if (IS_ERR(group))
431 return;
432
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200433 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600434}
435
436static int iommu_init_device(struct device *dev)
437{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600438 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400439 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600440
441 if (dev->archdata.iommu)
442 return 0;
443
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400444 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200445 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400446 return devid;
447
448 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600449 if (!dev_data)
450 return -ENOMEM;
451
Joerg Roedele3156042016-04-08 15:12:24 +0200452 dev_data->alias = get_alias(dev);
453
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400454 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100455 struct amd_iommu *iommu;
456
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400457 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100458 dev_data->iommu_v2 = iommu->is_iommu_v2;
459 }
460
Joerg Roedel657cbb62009-11-23 15:26:46 +0100461 dev->archdata.iommu = dev_data;
462
Alex Williamson066f2e92014-06-12 16:12:37 -0600463 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
464 dev);
465
Joerg Roedel657cbb62009-11-23 15:26:46 +0100466 return 0;
467}
468
Joerg Roedel26018872011-06-06 16:50:14 +0200469static void iommu_ignore_device(struct device *dev)
470{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400471 u16 alias;
472 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200473
474 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200475 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400476 return;
477
Joerg Roedele3156042016-04-08 15:12:24 +0200478 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200479
480 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
481 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
482
483 amd_iommu_rlookup_table[devid] = NULL;
484 amd_iommu_rlookup_table[alias] = NULL;
485}
486
Joerg Roedel657cbb62009-11-23 15:26:46 +0100487static void iommu_uninit_device(struct device *dev)
488{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400489 int devid;
490 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600491
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400492 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200493 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400494 return;
495
496 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600497 if (!dev_data)
498 return;
499
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100500 if (dev_data->domain)
501 detach_device(dev);
502
Alex Williamson066f2e92014-06-12 16:12:37 -0600503 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
504 dev);
505
Alex Williamson9dcd6132012-05-30 14:19:07 -0600506 iommu_group_remove_device(dev);
507
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200508 /* Remove dma-ops */
509 dev->archdata.dma_ops = NULL;
510
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200511 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600512 * We keep dev_data around for unplugged devices and reuse it when the
513 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200514 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100515}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100516
Joerg Roedel431b2a22008-07-11 17:14:22 +0200517/****************************************************************************
518 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200519 * Interrupt handling functions
520 *
521 ****************************************************************************/
522
Joerg Roedele3e59872009-09-03 14:02:10 +0200523static void dump_dte_entry(u16 devid)
524{
525 int i;
526
Joerg Roedelee6c2862011-11-09 12:06:03 +0100527 for (i = 0; i < 4; ++i)
528 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200529 amd_iommu_dev_table[devid].data[i]);
530}
531
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200532static void dump_command(unsigned long phys_addr)
533{
534 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
535 int i;
536
537 for (i = 0; i < 4; ++i)
538 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
539}
540
Joerg Roedela345b232009-09-03 15:01:43 +0200541static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200542{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200543 int type, devid, domid, flags;
544 volatile u32 *event = __evt;
545 int count = 0;
546 u64 address;
547
548retry:
549 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
550 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
551 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
552 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
553 address = (u64)(((u64)event[3]) << 32) | event[2];
554
555 if (type == 0) {
556 /* Did we hit the erratum? */
557 if (++count == LOOP_TIMEOUT) {
558 pr_err("AMD-Vi: No event written to event log\n");
559 return;
560 }
561 udelay(1);
562 goto retry;
563 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200564
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200565 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200566
567 switch (type) {
568 case EVENT_TYPE_ILL_DEV:
569 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
570 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700571 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200572 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200573 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200574 break;
575 case EVENT_TYPE_IO_FAULT:
576 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
577 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700578 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200579 domid, address, flags);
580 break;
581 case EVENT_TYPE_DEV_TAB_ERR:
582 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
583 "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 address, flags);
586 break;
587 case EVENT_TYPE_PAGE_TAB_ERR:
588 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
589 "domain=0x%04x 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 domid, address, flags);
592 break;
593 case EVENT_TYPE_ILL_CMD:
594 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200595 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200596 break;
597 case EVENT_TYPE_CMD_HARD_ERR:
598 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
599 "flags=0x%04x]\n", address, flags);
600 break;
601 case EVENT_TYPE_IOTLB_INV_TO:
602 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
603 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700604 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200605 address);
606 break;
607 case EVENT_TYPE_INV_DEV_REQ:
608 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
609 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700610 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611 address, flags);
612 break;
613 default:
614 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
615 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200616
617 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200618}
619
620static void iommu_poll_events(struct amd_iommu *iommu)
621{
622 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200623
624 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
625 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
626
627 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200628 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200629 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200630 }
631
632 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200633}
634
Joerg Roedeleee53532012-06-01 15:20:23 +0200635static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100636{
637 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100638
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100639 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
640 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
641 return;
642 }
643
644 fault.address = raw[1];
645 fault.pasid = PPR_PASID(raw[0]);
646 fault.device_id = PPR_DEVID(raw[0]);
647 fault.tag = PPR_TAG(raw[0]);
648 fault.flags = PPR_FLAGS(raw[0]);
649
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100650 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
651}
652
653static void iommu_poll_ppr_log(struct amd_iommu *iommu)
654{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100655 u32 head, tail;
656
657 if (iommu->ppr_log == NULL)
658 return;
659
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100660 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
661 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
662
663 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200664 volatile u64 *raw;
665 u64 entry[2];
666 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100667
Joerg Roedeleee53532012-06-01 15:20:23 +0200668 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100669
Joerg Roedeleee53532012-06-01 15:20:23 +0200670 /*
671 * Hardware bug: Interrupt may arrive before the entry is
672 * written to memory. If this happens we need to wait for the
673 * entry to arrive.
674 */
675 for (i = 0; i < LOOP_TIMEOUT; ++i) {
676 if (PPR_REQ_TYPE(raw[0]) != 0)
677 break;
678 udelay(1);
679 }
680
681 /* Avoid memcpy function-call overhead */
682 entry[0] = raw[0];
683 entry[1] = raw[1];
684
685 /*
686 * To detect the hardware bug we need to clear the entry
687 * back to zero.
688 */
689 raw[0] = raw[1] = 0UL;
690
691 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100692 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
693 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200694
Joerg Roedeleee53532012-06-01 15:20:23 +0200695 /* Handle PPR entry */
696 iommu_handle_ppr_entry(iommu, entry);
697
Joerg Roedeleee53532012-06-01 15:20:23 +0200698 /* Refresh ring-buffer information */
699 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100700 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
701 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100702}
703
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200704irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200705{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500706 struct amd_iommu *iommu = (struct amd_iommu *) data;
707 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200708
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500709 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
710 /* Enable EVT and PPR interrupts again */
711 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
712 iommu->mmio_base + MMIO_STATUS_OFFSET);
713
714 if (status & MMIO_STATUS_EVT_INT_MASK) {
715 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
716 iommu_poll_events(iommu);
717 }
718
719 if (status & MMIO_STATUS_PPR_INT_MASK) {
720 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
721 iommu_poll_ppr_log(iommu);
722 }
723
724 /*
725 * Hardware bug: ERBT1312
726 * When re-enabling interrupt (by writing 1
727 * to clear the bit), the hardware might also try to set
728 * the interrupt bit in the event status register.
729 * In this scenario, the bit will be set, and disable
730 * subsequent interrupts.
731 *
732 * Workaround: The IOMMU driver should read back the
733 * status register and check if the interrupt bits are cleared.
734 * If not, driver will need to go through the interrupt handler
735 * again and re-clear the bits
736 */
737 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100738 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200739 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200740}
741
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200742irqreturn_t amd_iommu_int_handler(int irq, void *data)
743{
744 return IRQ_WAKE_THREAD;
745}
746
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200747/****************************************************************************
748 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200749 * IOMMU command queuing functions
750 *
751 ****************************************************************************/
752
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200753static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200754{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200755 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200756
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200757 while (*sem == 0 && i < LOOP_TIMEOUT) {
758 udelay(1);
759 i += 1;
760 }
761
762 if (i == LOOP_TIMEOUT) {
763 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
764 return -EIO;
765 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200766
767 return 0;
768}
769
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200770static void copy_cmd_to_buffer(struct amd_iommu *iommu,
771 struct iommu_cmd *cmd,
772 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200773{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200774 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200775
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200776 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200777 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200778
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200779 /* Copy command to buffer */
780 memcpy(target, cmd, sizeof(*cmd));
781
782 /* Tell the IOMMU about it */
783 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
784}
785
Joerg Roedel815b33f2011-04-06 17:26:49 +0200786static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200787{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200788 WARN_ON(address & 0x7ULL);
789
Joerg Roedelded46732011-04-06 10:53:48 +0200790 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200791 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
792 cmd->data[1] = upper_32_bits(__pa(address));
793 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200794 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
795}
796
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200797static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
798{
799 memset(cmd, 0, sizeof(*cmd));
800 cmd->data[0] = devid;
801 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
802}
803
Joerg Roedel11b64022011-04-06 11:49:28 +0200804static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
805 size_t size, u16 domid, int pde)
806{
807 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100808 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200809
810 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100811 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200812
813 if (pages > 1) {
814 /*
815 * If we have to flush more than one page, flush all
816 * TLB entries for this domain
817 */
818 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100819 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200820 }
821
822 address &= PAGE_MASK;
823
824 memset(cmd, 0, sizeof(*cmd));
825 cmd->data[1] |= domid;
826 cmd->data[2] = lower_32_bits(address);
827 cmd->data[3] = upper_32_bits(address);
828 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
829 if (s) /* size bit - we flush more than one 4kb page */
830 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200831 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200832 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
833}
834
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200835static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
836 u64 address, size_t size)
837{
838 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100839 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200840
841 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100842 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200843
844 if (pages > 1) {
845 /*
846 * If we have to flush more than one page, flush all
847 * TLB entries for this domain
848 */
849 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100850 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200851 }
852
853 address &= PAGE_MASK;
854
855 memset(cmd, 0, sizeof(*cmd));
856 cmd->data[0] = devid;
857 cmd->data[0] |= (qdep & 0xff) << 24;
858 cmd->data[1] = devid;
859 cmd->data[2] = lower_32_bits(address);
860 cmd->data[3] = upper_32_bits(address);
861 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
862 if (s)
863 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
864}
865
Joerg Roedel22e266c2011-11-21 15:59:08 +0100866static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
867 u64 address, bool size)
868{
869 memset(cmd, 0, sizeof(*cmd));
870
871 address &= ~(0xfffULL);
872
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600873 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100874 cmd->data[1] = domid;
875 cmd->data[2] = lower_32_bits(address);
876 cmd->data[3] = upper_32_bits(address);
877 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
878 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
879 if (size)
880 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
881 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
882}
883
884static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
885 int qdep, u64 address, bool size)
886{
887 memset(cmd, 0, sizeof(*cmd));
888
889 address &= ~(0xfffULL);
890
891 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600892 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100893 cmd->data[0] |= (qdep & 0xff) << 24;
894 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600895 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100896 cmd->data[2] = lower_32_bits(address);
897 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
898 cmd->data[3] = upper_32_bits(address);
899 if (size)
900 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
901 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
902}
903
Joerg Roedelc99afa22011-11-21 18:19:25 +0100904static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
905 int status, int tag, bool gn)
906{
907 memset(cmd, 0, sizeof(*cmd));
908
909 cmd->data[0] = devid;
910 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600911 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100912 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
913 }
914 cmd->data[3] = tag & 0x1ff;
915 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
916
917 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
918}
919
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200920static void build_inv_all(struct iommu_cmd *cmd)
921{
922 memset(cmd, 0, sizeof(*cmd));
923 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200924}
925
Joerg Roedel7ef27982012-06-21 16:46:04 +0200926static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
927{
928 memset(cmd, 0, sizeof(*cmd));
929 cmd->data[0] = devid;
930 CMD_SET_TYPE(cmd, CMD_INV_IRT);
931}
932
Joerg Roedel431b2a22008-07-11 17:14:22 +0200933/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200934 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200935 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200936 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200937static int iommu_queue_command_sync(struct amd_iommu *iommu,
938 struct iommu_cmd *cmd,
939 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200940{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200941 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200942 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200943
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200944again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200945 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200946
947 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
948 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200949 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
950 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200951
952 if (left <= 2) {
953 struct iommu_cmd sync_cmd;
954 volatile u64 sem = 0;
955 int ret;
956
957 build_completion_wait(&sync_cmd, (u64)&sem);
958 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
959
960 spin_unlock_irqrestore(&iommu->lock, flags);
961
962 if ((ret = wait_on_sem(&sem)) != 0)
963 return ret;
964
965 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200966 }
967
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200968 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200969
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200970 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200971 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200972
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200973 spin_unlock_irqrestore(&iommu->lock, flags);
974
Joerg Roedel815b33f2011-04-06 17:26:49 +0200975 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100976}
977
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200978static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
979{
980 return iommu_queue_command_sync(iommu, cmd, true);
981}
982
Joerg Roedel8d201962008-12-02 20:34:41 +0100983/*
984 * This function queues a completion wait command into the command
985 * buffer of an IOMMU
986 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100987static int iommu_completion_wait(struct amd_iommu *iommu)
988{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200989 struct iommu_cmd cmd;
990 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200991 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100992
993 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200994 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100995
Joerg Roedel815b33f2011-04-06 17:26:49 +0200996 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100997
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200998 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100999 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001000 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001001
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001002 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001003}
1004
Joerg Roedeld8c13082011-04-06 18:51:26 +02001005static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001006{
1007 struct iommu_cmd cmd;
1008
Joerg Roedeld8c13082011-04-06 18:51:26 +02001009 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001010
Joerg Roedeld8c13082011-04-06 18:51:26 +02001011 return iommu_queue_command(iommu, &cmd);
1012}
1013
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001014static void iommu_flush_dte_all(struct amd_iommu *iommu)
1015{
1016 u32 devid;
1017
1018 for (devid = 0; devid <= 0xffff; ++devid)
1019 iommu_flush_dte(iommu, devid);
1020
1021 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001022}
1023
1024/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001025 * This function uses heavy locking and may disable irqs for some time. But
1026 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001027 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001028static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001029{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001030 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001031
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001032 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1033 struct iommu_cmd cmd;
1034 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1035 dom_id, 1);
1036 iommu_queue_command(iommu, &cmd);
1037 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001038
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001039 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001040}
1041
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001042static void iommu_flush_all(struct amd_iommu *iommu)
1043{
1044 struct iommu_cmd cmd;
1045
1046 build_inv_all(&cmd);
1047
1048 iommu_queue_command(iommu, &cmd);
1049 iommu_completion_wait(iommu);
1050}
1051
Joerg Roedel7ef27982012-06-21 16:46:04 +02001052static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1053{
1054 struct iommu_cmd cmd;
1055
1056 build_inv_irt(&cmd, devid);
1057
1058 iommu_queue_command(iommu, &cmd);
1059}
1060
1061static void iommu_flush_irt_all(struct amd_iommu *iommu)
1062{
1063 u32 devid;
1064
1065 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1066 iommu_flush_irt(iommu, devid);
1067
1068 iommu_completion_wait(iommu);
1069}
1070
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001071void iommu_flush_all_caches(struct amd_iommu *iommu)
1072{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001073 if (iommu_feature(iommu, FEATURE_IA)) {
1074 iommu_flush_all(iommu);
1075 } else {
1076 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001077 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001078 iommu_flush_tlb_all(iommu);
1079 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001080}
1081
Joerg Roedel431b2a22008-07-11 17:14:22 +02001082/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001083 * Command send function for flushing on-device TLB
1084 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001085static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1086 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001087{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001088 struct amd_iommu *iommu;
1089 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001090 int qdep;
1091
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001092 qdep = dev_data->ats.qdep;
1093 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001094
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001095 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001096
1097 return iommu_queue_command(iommu, &cmd);
1098}
1099
1100/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001101 * Command send function for invalidating a device table entry
1102 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001103static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001104{
1105 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001106 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001107 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001108
Joerg Roedel6c542042011-06-09 17:07:31 +02001109 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001110 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001111
Joerg Roedelf62dda62011-06-09 12:55:35 +02001112 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001113 if (!ret && alias != dev_data->devid)
1114 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001115 if (ret)
1116 return ret;
1117
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001118 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001119 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001120
1121 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001122}
1123
Joerg Roedel431b2a22008-07-11 17:14:22 +02001124/*
1125 * TLB invalidation function which is called from the mapping functions.
1126 * It invalidates a single PTE if the range to flush is within a single
1127 * page. Otherwise it flushes the whole TLB of the IOMMU.
1128 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001129static void __domain_flush_pages(struct protection_domain *domain,
1130 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001131{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001132 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001133 struct iommu_cmd cmd;
1134 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001135
Joerg Roedel11b64022011-04-06 11:49:28 +02001136 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001137
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001138 for (i = 0; i < amd_iommus_present; ++i) {
1139 if (!domain->dev_iommu[i])
1140 continue;
1141
1142 /*
1143 * Devices of this domain are behind this IOMMU
1144 * We need a TLB flush
1145 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001146 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001147 }
1148
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001149 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001150
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001151 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001152 continue;
1153
Joerg Roedel6c542042011-06-09 17:07:31 +02001154 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001155 }
1156
Joerg Roedel11b64022011-04-06 11:49:28 +02001157 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001158}
1159
Joerg Roedel17b124b2011-04-06 18:01:35 +02001160static void domain_flush_pages(struct protection_domain *domain,
1161 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001162{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001163 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001164}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001165
Joerg Roedel1c655772008-09-04 18:40:05 +02001166/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001167static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001168{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001169 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001170}
1171
Chris Wright42a49f92009-06-15 15:42:00 +02001172/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001173static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001174{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001175 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1176}
1177
1178static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001179{
1180 int i;
1181
1182 for (i = 0; i < amd_iommus_present; ++i) {
Joerg Roedelf1eae7c2016-07-06 12:50:35 +02001183 if (domain && !domain->dev_iommu[i])
Joerg Roedelb6c02712008-06-26 21:27:53 +02001184 continue;
1185
1186 /*
1187 * Devices of this domain are behind this IOMMU
1188 * We need to wait for completion of all commands.
1189 */
1190 iommu_completion_wait(amd_iommus[i]);
1191 }
1192}
1193
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001194
Joerg Roedel43f49602008-12-02 21:01:12 +01001195/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001196 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001197 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001198static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001199{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001200 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001201
1202 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001203 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001204}
1205
Joerg Roedel431b2a22008-07-11 17:14:22 +02001206/****************************************************************************
1207 *
1208 * The functions below are used the create the page table mappings for
1209 * unity mapped regions.
1210 *
1211 ****************************************************************************/
1212
1213/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001214 * This function is used to add another level to an IO page table. Adding
1215 * another level increases the size of the address space by 9 bits to a size up
1216 * to 64 bits.
1217 */
1218static bool increase_address_space(struct protection_domain *domain,
1219 gfp_t gfp)
1220{
1221 u64 *pte;
1222
1223 if (domain->mode == PAGE_MODE_6_LEVEL)
1224 /* address space already 64 bit large */
1225 return false;
1226
1227 pte = (void *)get_zeroed_page(gfp);
1228 if (!pte)
1229 return false;
1230
1231 *pte = PM_LEVEL_PDE(domain->mode,
1232 virt_to_phys(domain->pt_root));
1233 domain->pt_root = pte;
1234 domain->mode += 1;
1235 domain->updated = true;
1236
1237 return true;
1238}
1239
1240static u64 *alloc_pte(struct protection_domain *domain,
1241 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001242 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001243 u64 **pte_page,
1244 gfp_t gfp)
1245{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001246 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001247 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001248
1249 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001250
1251 while (address > PM_LEVEL_SIZE(domain->mode))
1252 increase_address_space(domain, gfp);
1253
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001254 level = domain->mode - 1;
1255 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1256 address = PAGE_SIZE_ALIGN(address, page_size);
1257 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001258
1259 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001260 u64 __pte, __npte;
1261
1262 __pte = *pte;
1263
1264 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001265 page = (u64 *)get_zeroed_page(gfp);
1266 if (!page)
1267 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001268
1269 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1270
1271 if (cmpxchg64(pte, __pte, __npte)) {
1272 free_page((unsigned long)page);
1273 continue;
1274 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001275 }
1276
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001277 /* No level skipping support yet */
1278 if (PM_PTE_LEVEL(*pte) != level)
1279 return NULL;
1280
Joerg Roedel308973d2009-11-24 17:43:32 +01001281 level -= 1;
1282
1283 pte = IOMMU_PTE_PAGE(*pte);
1284
1285 if (pte_page && level == end_lvl)
1286 *pte_page = pte;
1287
1288 pte = &pte[PM_LEVEL_INDEX(level, address)];
1289 }
1290
1291 return pte;
1292}
1293
1294/*
1295 * This function checks if there is a PTE for a given dma address. If
1296 * there is one, it returns the pointer to it.
1297 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001298static u64 *fetch_pte(struct protection_domain *domain,
1299 unsigned long address,
1300 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001301{
1302 int level;
1303 u64 *pte;
1304
Joerg Roedel24cd7722010-01-19 17:27:39 +01001305 if (address > PM_LEVEL_SIZE(domain->mode))
1306 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001307
Joerg Roedel3039ca12015-04-01 14:58:48 +02001308 level = domain->mode - 1;
1309 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1310 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001311
1312 while (level > 0) {
1313
1314 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001315 if (!IOMMU_PTE_PRESENT(*pte))
1316 return NULL;
1317
Joerg Roedel24cd7722010-01-19 17:27:39 +01001318 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001319 if (PM_PTE_LEVEL(*pte) == 7 ||
1320 PM_PTE_LEVEL(*pte) == 0)
1321 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001322
1323 /* No level skipping support yet */
1324 if (PM_PTE_LEVEL(*pte) != level)
1325 return NULL;
1326
Joerg Roedel308973d2009-11-24 17:43:32 +01001327 level -= 1;
1328
Joerg Roedel24cd7722010-01-19 17:27:39 +01001329 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001330 pte = IOMMU_PTE_PAGE(*pte);
1331 pte = &pte[PM_LEVEL_INDEX(level, address)];
1332 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1333 }
1334
1335 if (PM_PTE_LEVEL(*pte) == 0x07) {
1336 unsigned long pte_mask;
1337
1338 /*
1339 * If we have a series of large PTEs, make
1340 * sure to return a pointer to the first one.
1341 */
1342 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1343 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1344 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001345 }
1346
1347 return pte;
1348}
1349
1350/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001351 * Generic mapping functions. It maps a physical address into a DMA
1352 * address space. It allocates the page table pages if necessary.
1353 * In the future it can be extended to a generic mapping function
1354 * supporting all features of AMD IOMMU page tables like level skipping
1355 * and full 64 bit address spaces.
1356 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001357static int iommu_map_page(struct protection_domain *dom,
1358 unsigned long bus_addr,
1359 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001360 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001361 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001362 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001363{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001364 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001365 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001366
Joerg Roedeld4b03662015-04-01 14:58:52 +02001367 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1368 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1369
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001370 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001371 return -EINVAL;
1372
Joerg Roedeld4b03662015-04-01 14:58:52 +02001373 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001374 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001375
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001376 if (!pte)
1377 return -ENOMEM;
1378
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001379 for (i = 0; i < count; ++i)
1380 if (IOMMU_PTE_PRESENT(pte[i]))
1381 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001382
Joerg Roedeld4b03662015-04-01 14:58:52 +02001383 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001384 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1385 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1386 } else
1387 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1388
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001389 if (prot & IOMMU_PROT_IR)
1390 __pte |= IOMMU_PTE_IR;
1391 if (prot & IOMMU_PROT_IW)
1392 __pte |= IOMMU_PTE_IW;
1393
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001394 for (i = 0; i < count; ++i)
1395 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001396
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001397 update_domain(dom);
1398
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001399 return 0;
1400}
1401
Joerg Roedel24cd7722010-01-19 17:27:39 +01001402static unsigned long iommu_unmap_page(struct protection_domain *dom,
1403 unsigned long bus_addr,
1404 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001405{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001406 unsigned long long unmapped;
1407 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001408 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001409
Joerg Roedel24cd7722010-01-19 17:27:39 +01001410 BUG_ON(!is_power_of_2(page_size));
1411
1412 unmapped = 0;
1413
1414 while (unmapped < page_size) {
1415
Joerg Roedel71b390e2015-04-01 14:58:49 +02001416 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001417
Joerg Roedel71b390e2015-04-01 14:58:49 +02001418 if (pte) {
1419 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001420
Joerg Roedel71b390e2015-04-01 14:58:49 +02001421 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001422 for (i = 0; i < count; i++)
1423 pte[i] = 0ULL;
1424 }
1425
1426 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1427 unmapped += unmap_size;
1428 }
1429
Alex Williamson60d0ca32013-06-21 14:33:19 -06001430 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001431
1432 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001433}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001434
Joerg Roedel431b2a22008-07-11 17:14:22 +02001435/****************************************************************************
1436 *
1437 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001438 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001439 *
1440 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001441
Joerg Roedel9cabe892009-05-18 16:38:55 +02001442
Joerg Roedel256e4622016-07-05 14:23:01 +02001443static unsigned long dma_ops_alloc_iova(struct device *dev,
1444 struct dma_ops_domain *dma_dom,
1445 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001446{
Joerg Roedel256e4622016-07-05 14:23:01 +02001447 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001448
Joerg Roedel256e4622016-07-05 14:23:01 +02001449 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001450
Joerg Roedel256e4622016-07-05 14:23:01 +02001451 if (dma_mask > DMA_BIT_MASK(32))
1452 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1453 IOVA_PFN(DMA_BIT_MASK(32)));
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001454
Joerg Roedel256e4622016-07-05 14:23:01 +02001455 if (!pfn)
1456 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001457
Joerg Roedel256e4622016-07-05 14:23:01 +02001458 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001459}
1460
Joerg Roedel256e4622016-07-05 14:23:01 +02001461static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1462 unsigned long address,
1463 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001464{
Joerg Roedel256e4622016-07-05 14:23:01 +02001465 pages = __roundup_pow_of_two(pages);
1466 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001467
Joerg Roedel256e4622016-07-05 14:23:01 +02001468 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001469}
1470
Joerg Roedel431b2a22008-07-11 17:14:22 +02001471/****************************************************************************
1472 *
1473 * The next functions belong to the domain allocation. A domain is
1474 * allocated for every IOMMU as the default domain. If device isolation
1475 * is enabled, every device get its own domain. The most important thing
1476 * about domains is the page table mapping the DMA address space they
1477 * contain.
1478 *
1479 ****************************************************************************/
1480
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001481/*
1482 * This function adds a protection domain to the global protection domain list
1483 */
1484static void add_domain_to_list(struct protection_domain *domain)
1485{
1486 unsigned long flags;
1487
1488 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1489 list_add(&domain->list, &amd_iommu_pd_list);
1490 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1491}
1492
1493/*
1494 * This function removes a protection domain to the global
1495 * protection domain list
1496 */
1497static void del_domain_from_list(struct protection_domain *domain)
1498{
1499 unsigned long flags;
1500
1501 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1502 list_del(&domain->list);
1503 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1504}
1505
Joerg Roedelec487d12008-06-26 21:27:58 +02001506static u16 domain_id_alloc(void)
1507{
1508 unsigned long flags;
1509 int id;
1510
1511 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1512 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1513 BUG_ON(id == 0);
1514 if (id > 0 && id < MAX_DOMAIN_ID)
1515 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1516 else
1517 id = 0;
1518 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1519
1520 return id;
1521}
1522
Joerg Roedela2acfb72008-12-02 18:28:53 +01001523static void domain_id_free(int id)
1524{
1525 unsigned long flags;
1526
1527 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1528 if (id > 0 && id < MAX_DOMAIN_ID)
1529 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1530 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1531}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001532
Joerg Roedel5c34c402013-06-20 20:22:58 +02001533#define DEFINE_FREE_PT_FN(LVL, FN) \
1534static void free_pt_##LVL (unsigned long __pt) \
1535{ \
1536 unsigned long p; \
1537 u64 *pt; \
1538 int i; \
1539 \
1540 pt = (u64 *)__pt; \
1541 \
1542 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001543 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001544 if (!IOMMU_PTE_PRESENT(pt[i])) \
1545 continue; \
1546 \
Joerg Roedel0b3fff52015-06-18 10:48:34 +02001547 /* Large PTE? */ \
1548 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1549 PM_PTE_LEVEL(pt[i]) == 7) \
1550 continue; \
1551 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001552 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1553 FN(p); \
1554 } \
1555 free_page((unsigned long)pt); \
1556}
1557
1558DEFINE_FREE_PT_FN(l2, free_page)
1559DEFINE_FREE_PT_FN(l3, free_pt_l2)
1560DEFINE_FREE_PT_FN(l4, free_pt_l3)
1561DEFINE_FREE_PT_FN(l5, free_pt_l4)
1562DEFINE_FREE_PT_FN(l6, free_pt_l5)
1563
Joerg Roedel86db2e52008-12-02 18:20:21 +01001564static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001565{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001566 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001567
Joerg Roedel5c34c402013-06-20 20:22:58 +02001568 switch (domain->mode) {
1569 case PAGE_MODE_NONE:
1570 break;
1571 case PAGE_MODE_1_LEVEL:
1572 free_page(root);
1573 break;
1574 case PAGE_MODE_2_LEVEL:
1575 free_pt_l2(root);
1576 break;
1577 case PAGE_MODE_3_LEVEL:
1578 free_pt_l3(root);
1579 break;
1580 case PAGE_MODE_4_LEVEL:
1581 free_pt_l4(root);
1582 break;
1583 case PAGE_MODE_5_LEVEL:
1584 free_pt_l5(root);
1585 break;
1586 case PAGE_MODE_6_LEVEL:
1587 free_pt_l6(root);
1588 break;
1589 default:
1590 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001591 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001592}
1593
Joerg Roedelb16137b2011-11-21 16:50:23 +01001594static void free_gcr3_tbl_level1(u64 *tbl)
1595{
1596 u64 *ptr;
1597 int i;
1598
1599 for (i = 0; i < 512; ++i) {
1600 if (!(tbl[i] & GCR3_VALID))
1601 continue;
1602
1603 ptr = __va(tbl[i] & PAGE_MASK);
1604
1605 free_page((unsigned long)ptr);
1606 }
1607}
1608
1609static void free_gcr3_tbl_level2(u64 *tbl)
1610{
1611 u64 *ptr;
1612 int i;
1613
1614 for (i = 0; i < 512; ++i) {
1615 if (!(tbl[i] & GCR3_VALID))
1616 continue;
1617
1618 ptr = __va(tbl[i] & PAGE_MASK);
1619
1620 free_gcr3_tbl_level1(ptr);
1621 }
1622}
1623
Joerg Roedel52815b72011-11-17 17:24:28 +01001624static void free_gcr3_table(struct protection_domain *domain)
1625{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001626 if (domain->glx == 2)
1627 free_gcr3_tbl_level2(domain->gcr3_tbl);
1628 else if (domain->glx == 1)
1629 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001630 else
1631 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001632
Joerg Roedel52815b72011-11-17 17:24:28 +01001633 free_page((unsigned long)domain->gcr3_tbl);
1634}
1635
Joerg Roedel431b2a22008-07-11 17:14:22 +02001636/*
1637 * Free a domain, only used if something went wrong in the
1638 * allocation path and we need to free an already allocated page table
1639 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001640static void dma_ops_domain_free(struct dma_ops_domain *dom)
1641{
1642 if (!dom)
1643 return;
1644
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001645 del_domain_from_list(&dom->domain);
1646
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001647 put_iova_domain(&dom->iovad);
1648
Joerg Roedel86db2e52008-12-02 18:20:21 +01001649 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001650
Joerg Roedelec487d12008-06-26 21:27:58 +02001651 kfree(dom);
1652}
1653
Joerg Roedel431b2a22008-07-11 17:14:22 +02001654/*
1655 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001656 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001657 * structures required for the dma_ops interface
1658 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001659static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001660{
1661 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001662
1663 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1664 if (!dma_dom)
1665 return NULL;
1666
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001667 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001668 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001669
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001670 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001671 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001672 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001673 dma_dom->domain.priv = dma_dom;
1674 if (!dma_dom->domain.pt_root)
1675 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001676
Joerg Roedel307d5852016-07-05 11:54:04 +02001677 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1678 IOVA_START_PFN, DMA_32BIT_PFN);
1679
Joerg Roedel81cd07b2016-07-07 18:01:10 +02001680 /* Initialize reserved ranges */
1681 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1682
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001683 add_domain_to_list(&dma_dom->domain);
1684
Joerg Roedelec487d12008-06-26 21:27:58 +02001685 return dma_dom;
1686
1687free_dma_dom:
1688 dma_ops_domain_free(dma_dom);
1689
1690 return NULL;
1691}
1692
Joerg Roedel431b2a22008-07-11 17:14:22 +02001693/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001694 * little helper function to check whether a given protection domain is a
1695 * dma_ops domain
1696 */
1697static bool dma_ops_domain(struct protection_domain *domain)
1698{
1699 return domain->flags & PD_DMA_OPS_MASK;
1700}
1701
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001702static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001703{
Joerg Roedel132bd682011-11-17 14:18:46 +01001704 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001705 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001706
Joerg Roedel132bd682011-11-17 14:18:46 +01001707 if (domain->mode != PAGE_MODE_NONE)
1708 pte_root = virt_to_phys(domain->pt_root);
1709
Joerg Roedel38ddf412008-09-11 10:38:32 +02001710 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1711 << DEV_ENTRY_MODE_SHIFT;
1712 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001713
Joerg Roedelee6c2862011-11-09 12:06:03 +01001714 flags = amd_iommu_dev_table[devid].data[1];
1715
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001716 if (ats)
1717 flags |= DTE_FLAG_IOTLB;
1718
Joerg Roedel52815b72011-11-17 17:24:28 +01001719 if (domain->flags & PD_IOMMUV2_MASK) {
1720 u64 gcr3 = __pa(domain->gcr3_tbl);
1721 u64 glx = domain->glx;
1722 u64 tmp;
1723
1724 pte_root |= DTE_FLAG_GV;
1725 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1726
1727 /* First mask out possible old values for GCR3 table */
1728 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1729 flags &= ~tmp;
1730
1731 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1732 flags &= ~tmp;
1733
1734 /* Encode GCR3 table into DTE */
1735 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1736 pte_root |= tmp;
1737
1738 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1739 flags |= tmp;
1740
1741 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1742 flags |= tmp;
1743 }
1744
Joerg Roedelee6c2862011-11-09 12:06:03 +01001745 flags &= ~(0xffffUL);
1746 flags |= domain->id;
1747
1748 amd_iommu_dev_table[devid].data[1] = flags;
1749 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001750}
1751
Joerg Roedel15898bb2009-11-24 15:39:42 +01001752static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001753{
Joerg Roedel355bf552008-12-08 12:02:41 +01001754 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001755 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1756 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001757
Joerg Roedelc5cca142009-10-09 18:31:20 +02001758 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001759}
1760
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001761static void do_attach(struct iommu_dev_data *dev_data,
1762 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001763{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001764 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001765 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001766 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001767
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001768 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001769 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001770 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001771
1772 /* Update data structures */
1773 dev_data->domain = domain;
1774 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001775
1776 /* Do reference counting */
1777 domain->dev_iommu[iommu->index] += 1;
1778 domain->dev_cnt += 1;
1779
Joerg Roedele25bfb52015-10-20 17:33:38 +02001780 /* Update device table */
1781 set_dte_entry(dev_data->devid, domain, ats);
1782 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08001783 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001784
Joerg Roedel6c542042011-06-09 17:07:31 +02001785 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001786}
1787
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001788static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001789{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001790 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001791 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001792
Joerg Roedel5adad992015-10-09 16:23:33 +02001793 /*
1794 * First check if the device is still attached. It might already
1795 * be detached from its domain because the generic
1796 * iommu_detach_group code detached it and we try again here in
1797 * our alias handling.
1798 */
1799 if (!dev_data->domain)
1800 return;
1801
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001802 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001803 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02001804
Joerg Roedelc4596112009-11-20 14:57:32 +01001805 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001806 dev_data->domain->dev_iommu[iommu->index] -= 1;
1807 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001808
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001809 /* Update data structures */
1810 dev_data->domain = NULL;
1811 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001812 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001813 if (alias != dev_data->devid)
1814 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001815
1816 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001817 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001818}
1819
1820/*
1821 * If a device is not yet associated with a domain, this function does
1822 * assigns it visible for the hardware
1823 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001824static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001825 struct protection_domain *domain)
1826{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001827 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001828
Joerg Roedel272e4f92015-10-20 17:33:37 +02001829 /*
1830 * Must be called with IRQs disabled. Warn here to detect early
1831 * when its not.
1832 */
1833 WARN_ON(!irqs_disabled());
1834
Joerg Roedel15898bb2009-11-24 15:39:42 +01001835 /* lock domain */
1836 spin_lock(&domain->lock);
1837
Joerg Roedel397111a2014-08-05 17:31:51 +02001838 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02001839 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02001840 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01001841
Joerg Roedel397111a2014-08-05 17:31:51 +02001842 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02001843 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001844
Julia Lawall84fe6c12010-05-27 12:31:51 +02001845 ret = 0;
1846
1847out_unlock:
1848
Joerg Roedel355bf552008-12-08 12:02:41 +01001849 /* ready */
1850 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001851
Julia Lawall84fe6c12010-05-27 12:31:51 +02001852 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001853}
1854
Joerg Roedel52815b72011-11-17 17:24:28 +01001855
1856static void pdev_iommuv2_disable(struct pci_dev *pdev)
1857{
1858 pci_disable_ats(pdev);
1859 pci_disable_pri(pdev);
1860 pci_disable_pasid(pdev);
1861}
1862
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001863/* FIXME: Change generic reset-function to do the same */
1864static int pri_reset_while_enabled(struct pci_dev *pdev)
1865{
1866 u16 control;
1867 int pos;
1868
Joerg Roedel46277b72011-12-07 14:34:02 +01001869 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001870 if (!pos)
1871 return -EINVAL;
1872
Joerg Roedel46277b72011-12-07 14:34:02 +01001873 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1874 control |= PCI_PRI_CTRL_RESET;
1875 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001876
1877 return 0;
1878}
1879
Joerg Roedel52815b72011-11-17 17:24:28 +01001880static int pdev_iommuv2_enable(struct pci_dev *pdev)
1881{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001882 bool reset_enable;
1883 int reqs, ret;
1884
1885 /* FIXME: Hardcode number of outstanding requests for now */
1886 reqs = 32;
1887 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1888 reqs = 1;
1889 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01001890
1891 /* Only allow access to user-accessible pages */
1892 ret = pci_enable_pasid(pdev, 0);
1893 if (ret)
1894 goto out_err;
1895
1896 /* First reset the PRI state of the device */
1897 ret = pci_reset_pri(pdev);
1898 if (ret)
1899 goto out_err;
1900
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001901 /* Enable PRI */
1902 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01001903 if (ret)
1904 goto out_err;
1905
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001906 if (reset_enable) {
1907 ret = pri_reset_while_enabled(pdev);
1908 if (ret)
1909 goto out_err;
1910 }
1911
Joerg Roedel52815b72011-11-17 17:24:28 +01001912 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1913 if (ret)
1914 goto out_err;
1915
1916 return 0;
1917
1918out_err:
1919 pci_disable_pri(pdev);
1920 pci_disable_pasid(pdev);
1921
1922 return ret;
1923}
1924
Joerg Roedelc99afa22011-11-21 18:19:25 +01001925/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02001926#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001927
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001928static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001929{
Joerg Roedela3b93122012-04-12 12:49:26 +02001930 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001931 int pos;
1932
Joerg Roedel46277b72011-12-07 14:34:02 +01001933 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001934 if (!pos)
1935 return false;
1936
Joerg Roedela3b93122012-04-12 12:49:26 +02001937 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001938
Joerg Roedela3b93122012-04-12 12:49:26 +02001939 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001940}
1941
Joerg Roedel15898bb2009-11-24 15:39:42 +01001942/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001943 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01001944 * assigns it visible for the hardware
1945 */
1946static int attach_device(struct device *dev,
1947 struct protection_domain *domain)
1948{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001949 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001950 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001951 unsigned long flags;
1952 int ret;
1953
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001954 dev_data = get_dev_data(dev);
1955
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001956 if (!dev_is_pci(dev))
1957 goto skip_ats_check;
1958
1959 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01001960 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02001961 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01001962 return -EINVAL;
1963
Joerg Roedel02ca2022015-07-28 16:58:49 +02001964 if (dev_data->iommu_v2) {
1965 if (pdev_iommuv2_enable(pdev) != 0)
1966 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01001967
Joerg Roedel02ca2022015-07-28 16:58:49 +02001968 dev_data->ats.enabled = true;
1969 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1970 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
1971 }
Joerg Roedel52815b72011-11-17 17:24:28 +01001972 } else if (amd_iommu_iotlb_sup &&
1973 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001974 dev_data->ats.enabled = true;
1975 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1976 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001977
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001978skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01001979 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001980 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001981 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1982
1983 /*
1984 * We might boot into a crash-kernel here. The crashed kernel
1985 * left the caches in the IOMMU dirty. So we have to flush
1986 * here to evict all dirty stuff.
1987 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001988 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001989
1990 return ret;
1991}
1992
1993/*
1994 * Removes a device from a protection domain (unlocked)
1995 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001996static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001997{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001998 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001999
Joerg Roedel272e4f92015-10-20 17:33:37 +02002000 /*
2001 * Must be called with IRQs disabled. Warn here to detect early
2002 * when its not.
2003 */
2004 WARN_ON(!irqs_disabled());
2005
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002006 if (WARN_ON(!dev_data->domain))
2007 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002008
Joerg Roedel2ca76272010-01-22 16:45:31 +01002009 domain = dev_data->domain;
2010
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002011 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002012
Joerg Roedel150952f2015-10-20 17:33:35 +02002013 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002014
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002015 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002016}
2017
2018/*
2019 * Removes a device from a protection domain (with devtable_lock held)
2020 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002021static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002022{
Joerg Roedel52815b72011-11-17 17:24:28 +01002023 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002024 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002025 unsigned long flags;
2026
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002027 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002028 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002029
Joerg Roedel355bf552008-12-08 12:02:41 +01002030 /* lock device table */
2031 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002032 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002033 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002034
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002035 if (!dev_is_pci(dev))
2036 return;
2037
Joerg Roedel02ca2022015-07-28 16:58:49 +02002038 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002039 pdev_iommuv2_disable(to_pci_dev(dev));
2040 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002041 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002042
2043 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002044}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002045
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002046static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002047{
Joerg Roedel71f77582011-06-09 19:03:15 +02002048 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002049 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002050 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002051 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002052
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002053 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002054 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002055
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002056 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002057 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002058 return devid;
2059
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002060 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002061
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002062 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002063 if (ret) {
2064 if (ret != -ENOTSUPP)
2065 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2066 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002067
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002068 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002069 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002070 goto out;
2071 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002072 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002073
Joerg Roedel07ee8692015-05-28 18:41:42 +02002074 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002075
2076 BUG_ON(!dev_data);
2077
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002078 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002079 iommu_request_dm_for_dev(dev);
2080
2081 /* Domains are initialized for this device - have a look what we ended up with */
2082 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002083 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002084 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002085 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002086 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002087
2088out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002089 iommu_completion_wait(iommu);
2090
Joerg Roedele275a2a2008-12-10 18:27:25 +01002091 return 0;
2092}
2093
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002094static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002095{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002096 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002097 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002098
2099 if (!check_device(dev))
2100 return;
2101
2102 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002103 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002104 return;
2105
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002106 iommu = amd_iommu_rlookup_table[devid];
2107
2108 iommu_uninit_device(dev);
2109 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002110}
2111
Wan Zongshunb097d112016-04-01 09:06:04 -04002112static struct iommu_group *amd_iommu_device_group(struct device *dev)
2113{
2114 if (dev_is_pci(dev))
2115 return pci_device_group(dev);
2116
2117 return acpihid_device_group(dev);
2118}
2119
Joerg Roedel431b2a22008-07-11 17:14:22 +02002120/*****************************************************************************
2121 *
2122 * The next functions belong to the dma_ops mapping/unmapping code.
2123 *
2124 *****************************************************************************/
2125
Joerg Roedelb1516a12016-07-06 13:07:22 +02002126static void __queue_flush(struct flush_queue *queue)
2127{
2128 struct protection_domain *domain;
2129 unsigned long flags;
2130 int idx;
2131
2132 /* First flush TLB of all known domains */
2133 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
2134 list_for_each_entry(domain, &amd_iommu_pd_list, list)
2135 domain_flush_tlb(domain);
2136 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
2137
2138 /* Wait until flushes have completed */
2139 domain_flush_complete(NULL);
2140
2141 for (idx = 0; idx < queue->next; ++idx) {
2142 struct flush_queue_entry *entry;
2143
2144 entry = queue->entries + idx;
2145
2146 free_iova_fast(&entry->dma_dom->iovad,
2147 entry->iova_pfn,
2148 entry->pages);
2149
2150 /* Not really necessary, just to make sure we catch any bugs */
2151 entry->dma_dom = NULL;
2152 }
2153
2154 queue->next = 0;
2155}
2156
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002157static void queue_flush_all(void)
Joerg Roedelbb279472016-07-06 13:56:36 +02002158{
2159 int cpu;
2160
Joerg Roedelbb279472016-07-06 13:56:36 +02002161 for_each_possible_cpu(cpu) {
2162 struct flush_queue *queue;
2163 unsigned long flags;
2164
2165 queue = per_cpu_ptr(&flush_queue, cpu);
2166 spin_lock_irqsave(&queue->lock, flags);
2167 if (queue->next > 0)
2168 __queue_flush(queue);
2169 spin_unlock_irqrestore(&queue->lock, flags);
2170 }
2171}
2172
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002173static void queue_flush_timeout(unsigned long unsused)
2174{
2175 atomic_set(&queue_timer_on, 0);
2176 queue_flush_all();
2177}
2178
Joerg Roedelb1516a12016-07-06 13:07:22 +02002179static void queue_add(struct dma_ops_domain *dma_dom,
2180 unsigned long address, unsigned long pages)
2181{
2182 struct flush_queue_entry *entry;
2183 struct flush_queue *queue;
2184 unsigned long flags;
2185 int idx;
2186
2187 pages = __roundup_pow_of_two(pages);
2188 address >>= PAGE_SHIFT;
2189
2190 queue = get_cpu_ptr(&flush_queue);
2191 spin_lock_irqsave(&queue->lock, flags);
2192
2193 if (queue->next == FLUSH_QUEUE_SIZE)
2194 __queue_flush(queue);
2195
2196 idx = queue->next++;
2197 entry = queue->entries + idx;
2198
2199 entry->iova_pfn = address;
2200 entry->pages = pages;
2201 entry->dma_dom = dma_dom;
2202
2203 spin_unlock_irqrestore(&queue->lock, flags);
Joerg Roedelbb279472016-07-06 13:56:36 +02002204
2205 if (atomic_cmpxchg(&queue_timer_on, 0, 1) == 0)
2206 mod_timer(&queue_timer, jiffies + msecs_to_jiffies(10));
2207
Joerg Roedelb1516a12016-07-06 13:07:22 +02002208 put_cpu_ptr(&flush_queue);
2209}
2210
2211
Joerg Roedel431b2a22008-07-11 17:14:22 +02002212/*
2213 * In the dma_ops path we only have the struct device. This function
2214 * finds the corresponding IOMMU, the protection domain and the
2215 * requestor id for a given device.
2216 * If the device is not yet associated with a domain this is also done
2217 * in this function.
2218 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002219static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002220{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002221 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002222
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002223 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002224 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002225
Joerg Roedeld26592a2016-07-07 15:31:13 +02002226 domain = get_dev_data(dev)->domain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002227 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002228 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002229
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002230 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002231}
2232
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002233static void update_device_table(struct protection_domain *domain)
2234{
Joerg Roedel492667d2009-11-27 13:25:47 +01002235 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002236
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002237 list_for_each_entry(dev_data, &domain->dev_list, list)
2238 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002239}
2240
2241static void update_domain(struct protection_domain *domain)
2242{
2243 if (!domain->updated)
2244 return;
2245
2246 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002247
2248 domain_flush_devices(domain);
2249 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002250
2251 domain->updated = false;
2252}
2253
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002254static int dir2prot(enum dma_data_direction direction)
2255{
2256 if (direction == DMA_TO_DEVICE)
2257 return IOMMU_PROT_IR;
2258 else if (direction == DMA_FROM_DEVICE)
2259 return IOMMU_PROT_IW;
2260 else if (direction == DMA_BIDIRECTIONAL)
2261 return IOMMU_PROT_IW | IOMMU_PROT_IR;
2262 else
2263 return 0;
2264}
Joerg Roedel431b2a22008-07-11 17:14:22 +02002265/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002266 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002267 * contiguous memory region into DMA address space. It is used by all
2268 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002269 * Must be called with the domain lock held.
2270 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002271static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002272 struct dma_ops_domain *dma_dom,
2273 phys_addr_t paddr,
2274 size_t size,
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002275 enum dma_data_direction direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002276 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002277{
2278 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002279 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002280 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002281 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002282 int i;
2283
Joerg Roedele3c449f2008-10-15 22:02:11 -07002284 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002285 paddr &= PAGE_MASK;
2286
Joerg Roedel256e4622016-07-05 14:23:01 +02002287 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002288 if (address == DMA_ERROR_CODE)
2289 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002290
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002291 prot = dir2prot(direction);
Joerg Roedel518d9b42016-07-05 14:39:47 +02002292
Joerg Roedelcb76c322008-06-26 21:28:00 +02002293 start = address;
2294 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002295 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2296 PAGE_SIZE, prot, GFP_ATOMIC);
2297 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002298 goto out_unmap;
2299
Joerg Roedelcb76c322008-06-26 21:28:00 +02002300 paddr += PAGE_SIZE;
2301 start += PAGE_SIZE;
2302 }
2303 address += offset;
2304
Joerg Roedelab7032b2015-12-21 18:47:11 +01002305 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002306 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002307 domain_flush_complete(&dma_dom->domain);
2308 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002309
Joerg Roedelcb76c322008-06-26 21:28:00 +02002310out:
2311 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002312
2313out_unmap:
2314
2315 for (--i; i >= 0; --i) {
2316 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002317 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002318 }
2319
Joerg Roedel256e4622016-07-05 14:23:01 +02002320 domain_flush_tlb(&dma_dom->domain);
2321 domain_flush_complete(&dma_dom->domain);
2322
2323 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002324
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002325 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002326}
2327
Joerg Roedel431b2a22008-07-11 17:14:22 +02002328/*
2329 * Does the reverse of the __map_single function. Must be called with
2330 * the domain lock held too
2331 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002332static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002333 dma_addr_t dma_addr,
2334 size_t size,
2335 int dir)
2336{
Joerg Roedel04e04632010-09-23 16:12:48 +02002337 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002338 dma_addr_t i, start;
2339 unsigned int pages;
2340
Joerg Roedel04e04632010-09-23 16:12:48 +02002341 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002342 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002343 dma_addr &= PAGE_MASK;
2344 start = dma_addr;
2345
2346 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002347 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002348 start += PAGE_SIZE;
2349 }
2350
Joerg Roedelb1516a12016-07-06 13:07:22 +02002351 if (amd_iommu_unmap_flush) {
2352 dma_ops_free_iova(dma_dom, dma_addr, pages);
2353 domain_flush_tlb(&dma_dom->domain);
2354 domain_flush_complete(&dma_dom->domain);
2355 } else {
2356 queue_add(dma_dom, dma_addr, pages);
2357 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002358}
2359
Joerg Roedel431b2a22008-07-11 17:14:22 +02002360/*
2361 * The exported map_single function for dma_ops.
2362 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002363static dma_addr_t map_page(struct device *dev, struct page *page,
2364 unsigned long offset, size_t size,
2365 enum dma_data_direction dir,
2366 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002367{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002368 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002369 struct protection_domain *domain;
2370 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002371
Joerg Roedel94f6d192009-11-24 16:40:02 +01002372 domain = get_domain(dev);
2373 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002374 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002375 else if (IS_ERR(domain))
2376 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002377
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002378 dma_mask = *dev->dma_mask;
2379
Joerg Roedelbda350d2016-07-05 16:28:02 +02002380 return __map_single(dev, domain->priv, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002381}
2382
Joerg Roedel431b2a22008-07-11 17:14:22 +02002383/*
2384 * The exported unmap_single function for dma_ops.
2385 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002386static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2387 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002388{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002389 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002390
Joerg Roedel94f6d192009-11-24 16:40:02 +01002391 domain = get_domain(dev);
2392 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002393 return;
2394
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002395 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002396}
2397
Joerg Roedel80187fd2016-07-06 17:20:54 +02002398static int sg_num_pages(struct device *dev,
2399 struct scatterlist *sglist,
2400 int nelems)
2401{
2402 unsigned long mask, boundary_size;
2403 struct scatterlist *s;
2404 int i, npages = 0;
2405
2406 mask = dma_get_seg_boundary(dev);
2407 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
2408 1UL << (BITS_PER_LONG - PAGE_SHIFT);
2409
2410 for_each_sg(sglist, s, nelems, i) {
2411 int p, n;
2412
2413 s->dma_address = npages << PAGE_SHIFT;
2414 p = npages % boundary_size;
2415 n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2416 if (p + n > boundary_size)
2417 npages += boundary_size - p;
2418 npages += n;
2419 }
2420
2421 return npages;
2422}
2423
Joerg Roedel431b2a22008-07-11 17:14:22 +02002424/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002425 * The exported map_sg function for dma_ops (handles scatter-gather
2426 * lists).
2427 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002428static int map_sg(struct device *dev, struct scatterlist *sglist,
Joerg Roedel80187fd2016-07-06 17:20:54 +02002429 int nelems, enum dma_data_direction direction,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002430 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002431{
Joerg Roedel80187fd2016-07-06 17:20:54 +02002432 int mapped_pages = 0, npages = 0, prot = 0, i;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002433 struct protection_domain *domain;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002434 struct dma_ops_domain *dma_dom;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002435 struct scatterlist *s;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002436 unsigned long address;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002437 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002438
Joerg Roedel94f6d192009-11-24 16:40:02 +01002439 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002440 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002441 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002442
Joerg Roedel80187fd2016-07-06 17:20:54 +02002443 dma_dom = domain->priv;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002444 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002445
Joerg Roedel80187fd2016-07-06 17:20:54 +02002446 npages = sg_num_pages(dev, sglist, nelems);
2447
2448 address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
2449 if (address == DMA_ERROR_CODE)
2450 goto out_err;
2451
2452 prot = dir2prot(direction);
2453
2454 /* Map all sg entries */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002455 for_each_sg(sglist, s, nelems, i) {
Joerg Roedel80187fd2016-07-06 17:20:54 +02002456 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002457
Joerg Roedel80187fd2016-07-06 17:20:54 +02002458 for (j = 0; j < pages; ++j) {
2459 unsigned long bus_addr, phys_addr;
2460 int ret;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002461
Joerg Roedel80187fd2016-07-06 17:20:54 +02002462 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2463 phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
2464 ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
2465 if (ret)
2466 goto out_unmap;
2467
2468 mapped_pages += 1;
2469 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002470 }
2471
Joerg Roedel80187fd2016-07-06 17:20:54 +02002472 /* Everything is mapped - write the right values into s->dma_address */
2473 for_each_sg(sglist, s, nelems, i) {
2474 s->dma_address += address + s->offset;
2475 s->dma_length = s->length;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002476 }
2477
Joerg Roedel80187fd2016-07-06 17:20:54 +02002478 return nelems;
2479
2480out_unmap:
2481 pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
2482 dev_name(dev), npages);
2483
2484 for_each_sg(sglist, s, nelems, i) {
2485 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2486
2487 for (j = 0; j < pages; ++j) {
2488 unsigned long bus_addr;
2489
2490 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2491 iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
2492
2493 if (--mapped_pages)
2494 goto out_free_iova;
2495 }
2496 }
2497
2498out_free_iova:
2499 free_iova_fast(&dma_dom->iovad, address, npages);
2500
2501out_err:
Joerg Roedel92d420e2015-12-21 19:31:33 +01002502 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002503}
2504
Joerg Roedel431b2a22008-07-11 17:14:22 +02002505/*
2506 * The exported map_sg function for dma_ops (handles scatter-gather
2507 * lists).
2508 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002509static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002510 int nelems, enum dma_data_direction dir,
2511 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002512{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002513 struct protection_domain *domain;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002514 unsigned long startaddr;
2515 int npages = 2;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002516
Joerg Roedel94f6d192009-11-24 16:40:02 +01002517 domain = get_domain(dev);
2518 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002519 return;
2520
Joerg Roedel80187fd2016-07-06 17:20:54 +02002521 startaddr = sg_dma_address(sglist) & PAGE_MASK;
2522 npages = sg_num_pages(dev, sglist, nelems);
2523
2524 __unmap_single(domain->priv, startaddr, npages << PAGE_SHIFT, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002525}
2526
Joerg Roedel431b2a22008-07-11 17:14:22 +02002527/*
2528 * The exported alloc_coherent function for dma_ops.
2529 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002530static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002531 dma_addr_t *dma_addr, gfp_t flag,
2532 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002533{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002534 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002535 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002536 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002537
Joerg Roedel94f6d192009-11-24 16:40:02 +01002538 domain = get_domain(dev);
2539 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002540 page = alloc_pages(flag, get_order(size));
2541 *dma_addr = page_to_phys(page);
2542 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002543 } else if (IS_ERR(domain))
2544 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002545
Joerg Roedel3b839a52015-04-01 14:58:47 +02002546 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002547 dma_mask = dev->coherent_dma_mask;
2548 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002549 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002550
Joerg Roedel3b839a52015-04-01 14:58:47 +02002551 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2552 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002553 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002554 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002555
Joerg Roedel3b839a52015-04-01 14:58:47 +02002556 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2557 get_order(size));
2558 if (!page)
2559 return NULL;
2560 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002561
Joerg Roedel832a90c2008-09-18 15:54:23 +02002562 if (!dma_mask)
2563 dma_mask = *dev->dma_mask;
2564
Joerg Roedel3b839a52015-04-01 14:58:47 +02002565 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedelbda350d2016-07-05 16:28:02 +02002566 size, DMA_BIDIRECTIONAL, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002567
Joerg Roedel92d420e2015-12-21 19:31:33 +01002568 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002569 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002570
Joerg Roedel3b839a52015-04-01 14:58:47 +02002571 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002572
2573out_free:
2574
Joerg Roedel3b839a52015-04-01 14:58:47 +02002575 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2576 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002577
2578 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002579}
2580
Joerg Roedel431b2a22008-07-11 17:14:22 +02002581/*
2582 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002583 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002584static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002585 void *virt_addr, dma_addr_t dma_addr,
2586 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002587{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002588 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002589 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002590
Joerg Roedel3b839a52015-04-01 14:58:47 +02002591 page = virt_to_page(virt_addr);
2592 size = PAGE_ALIGN(size);
2593
Joerg Roedel94f6d192009-11-24 16:40:02 +01002594 domain = get_domain(dev);
2595 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002596 goto free_mem;
2597
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002598 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002599
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002600free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002601 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2602 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002603}
2604
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002605/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002606 * This function is called by the DMA layer to find out if we can handle a
2607 * particular device. It is part of the dma_ops.
2608 */
2609static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2610{
Joerg Roedel420aef82009-11-23 16:14:57 +01002611 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002612}
2613
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002614static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002615 .alloc = alloc_coherent,
2616 .free = free_coherent,
2617 .map_page = map_page,
2618 .unmap_page = unmap_page,
2619 .map_sg = map_sg,
2620 .unmap_sg = unmap_sg,
2621 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002622};
2623
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002624static int init_reserved_iova_ranges(void)
2625{
2626 struct pci_dev *pdev = NULL;
2627 struct iova *val;
2628
2629 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2630 IOVA_START_PFN, DMA_32BIT_PFN);
2631
2632 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2633 &reserved_rbtree_key);
2634
2635 /* MSI memory range */
2636 val = reserve_iova(&reserved_iova_ranges,
2637 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2638 if (!val) {
2639 pr_err("Reserving MSI range failed\n");
2640 return -ENOMEM;
2641 }
2642
2643 /* HT memory range */
2644 val = reserve_iova(&reserved_iova_ranges,
2645 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2646 if (!val) {
2647 pr_err("Reserving HT range failed\n");
2648 return -ENOMEM;
2649 }
2650
2651 /*
2652 * Memory used for PCI resources
2653 * FIXME: Check whether we can reserve the PCI-hole completly
2654 */
2655 for_each_pci_dev(pdev) {
2656 int i;
2657
2658 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2659 struct resource *r = &pdev->resource[i];
2660
2661 if (!(r->flags & IORESOURCE_MEM))
2662 continue;
2663
2664 val = reserve_iova(&reserved_iova_ranges,
2665 IOVA_PFN(r->start),
2666 IOVA_PFN(r->end));
2667 if (!val) {
2668 pr_err("Reserve pci-resource range failed\n");
2669 return -ENOMEM;
2670 }
2671 }
2672 }
2673
2674 return 0;
2675}
2676
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002677int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002678{
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002679 int ret, cpu, err = 0;
Joerg Roedel307d5852016-07-05 11:54:04 +02002680
2681 ret = iova_cache_get();
2682 if (ret)
2683 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002684
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002685 ret = init_reserved_iova_ranges();
2686 if (ret)
2687 return ret;
2688
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002689 for_each_possible_cpu(cpu) {
2690 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2691
2692 queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
2693 sizeof(*queue->entries),
2694 GFP_KERNEL);
2695 if (!queue->entries)
2696 goto out_put_iova;
2697
2698 spin_lock_init(&queue->lock);
2699 }
2700
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002701 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2702 if (err)
2703 return err;
2704#ifdef CONFIG_ARM_AMBA
2705 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2706 if (err)
2707 return err;
2708#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002709 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2710 if (err)
2711 return err;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002712 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002713
2714out_put_iova:
2715 for_each_possible_cpu(cpu) {
2716 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2717
2718 kfree(queue->entries);
2719 }
2720
2721 return -ENOMEM;
Joerg Roedelf5325092010-01-22 17:44:35 +01002722}
2723
Joerg Roedel6631ee92008-06-26 21:28:05 +02002724int __init amd_iommu_init_dma_ops(void)
2725{
Joerg Roedelbb279472016-07-06 13:56:36 +02002726 setup_timer(&queue_timer, queue_flush_timeout, 0);
2727 atomic_set(&queue_timer_on, 0);
2728
Joerg Roedel32302322015-07-28 16:58:50 +02002729 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002730 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002731
Joerg Roedel52717822015-07-28 16:58:51 +02002732 /*
2733 * In case we don't initialize SWIOTLB (actually the common case
2734 * when AMD IOMMU is enabled), make sure there are global
2735 * dma_ops set as a fall-back for devices not handled by this
2736 * driver (for example non-PCI devices).
2737 */
2738 if (!swiotlb)
2739 dma_ops = &nommu_dma_ops;
2740
Joerg Roedel62410ee2012-06-12 16:42:43 +02002741 if (amd_iommu_unmap_flush)
2742 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2743 else
2744 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2745
Joerg Roedel6631ee92008-06-26 21:28:05 +02002746 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002747
Joerg Roedel6631ee92008-06-26 21:28:05 +02002748}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002749
2750/*****************************************************************************
2751 *
2752 * The following functions belong to the exported interface of AMD IOMMU
2753 *
2754 * This interface allows access to lower level functions of the IOMMU
2755 * like protection domain handling and assignement of devices to domains
2756 * which is not possible with the dma_ops interface.
2757 *
2758 *****************************************************************************/
2759
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002760static void cleanup_domain(struct protection_domain *domain)
2761{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002762 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002763 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002764
2765 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2766
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002767 while (!list_empty(&domain->dev_list)) {
2768 entry = list_first_entry(&domain->dev_list,
2769 struct iommu_dev_data, list);
2770 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002771 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002772
2773 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2774}
2775
Joerg Roedel26508152009-08-26 16:52:40 +02002776static void protection_domain_free(struct protection_domain *domain)
2777{
2778 if (!domain)
2779 return;
2780
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002781 del_domain_from_list(domain);
2782
Joerg Roedel26508152009-08-26 16:52:40 +02002783 if (domain->id)
2784 domain_id_free(domain->id);
2785
2786 kfree(domain);
2787}
2788
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002789static int protection_domain_init(struct protection_domain *domain)
2790{
2791 spin_lock_init(&domain->lock);
2792 mutex_init(&domain->api_lock);
2793 domain->id = domain_id_alloc();
2794 if (!domain->id)
2795 return -ENOMEM;
2796 INIT_LIST_HEAD(&domain->dev_list);
2797
2798 return 0;
2799}
2800
Joerg Roedel26508152009-08-26 16:52:40 +02002801static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002802{
2803 struct protection_domain *domain;
2804
2805 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2806 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002807 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002808
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002809 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002810 goto out_err;
2811
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002812 add_domain_to_list(domain);
2813
Joerg Roedel26508152009-08-26 16:52:40 +02002814 return domain;
2815
2816out_err:
2817 kfree(domain);
2818
2819 return NULL;
2820}
2821
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002822static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2823{
2824 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002825 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002826
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002827 switch (type) {
2828 case IOMMU_DOMAIN_UNMANAGED:
2829 pdomain = protection_domain_alloc();
2830 if (!pdomain)
2831 return NULL;
2832
2833 pdomain->mode = PAGE_MODE_3_LEVEL;
2834 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2835 if (!pdomain->pt_root) {
2836 protection_domain_free(pdomain);
2837 return NULL;
2838 }
2839
2840 pdomain->domain.geometry.aperture_start = 0;
2841 pdomain->domain.geometry.aperture_end = ~0ULL;
2842 pdomain->domain.geometry.force_aperture = true;
2843
2844 break;
2845 case IOMMU_DOMAIN_DMA:
2846 dma_domain = dma_ops_domain_alloc();
2847 if (!dma_domain) {
2848 pr_err("AMD-Vi: Failed to allocate\n");
2849 return NULL;
2850 }
2851 pdomain = &dma_domain->domain;
2852 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002853 case IOMMU_DOMAIN_IDENTITY:
2854 pdomain = protection_domain_alloc();
2855 if (!pdomain)
2856 return NULL;
2857
2858 pdomain->mode = PAGE_MODE_NONE;
2859 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002860 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002861 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002862 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002863
2864 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002865}
2866
2867static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002868{
2869 struct protection_domain *domain;
Joerg Roedelcda70052016-07-07 15:57:04 +02002870 struct dma_ops_domain *dma_dom;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002871
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002872 domain = to_pdomain(dom);
2873
Joerg Roedel98383fc2008-12-02 18:34:12 +01002874 if (domain->dev_cnt > 0)
2875 cleanup_domain(domain);
2876
2877 BUG_ON(domain->dev_cnt != 0);
2878
Joerg Roedelcda70052016-07-07 15:57:04 +02002879 if (!dom)
2880 return;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002881
Joerg Roedelcda70052016-07-07 15:57:04 +02002882 switch (dom->type) {
2883 case IOMMU_DOMAIN_DMA:
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002884 /*
2885 * First make sure the domain is no longer referenced from the
2886 * flush queue
2887 */
2888 queue_flush_all();
2889
2890 /* Now release the domain */
Joerg Roedelcda70052016-07-07 15:57:04 +02002891 dma_dom = domain->priv;
2892 dma_ops_domain_free(dma_dom);
2893 break;
2894 default:
2895 if (domain->mode != PAGE_MODE_NONE)
2896 free_pagetable(domain);
Joerg Roedel52815b72011-11-17 17:24:28 +01002897
Joerg Roedelcda70052016-07-07 15:57:04 +02002898 if (domain->flags & PD_IOMMUV2_MASK)
2899 free_gcr3_table(domain);
2900
2901 protection_domain_free(domain);
2902 break;
2903 }
Joerg Roedel98383fc2008-12-02 18:34:12 +01002904}
2905
Joerg Roedel684f2882008-12-08 12:07:44 +01002906static void amd_iommu_detach_device(struct iommu_domain *dom,
2907 struct device *dev)
2908{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002909 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002910 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002911 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01002912
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002913 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002914 return;
2915
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002916 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002917 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002918 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01002919
Joerg Roedel657cbb62009-11-23 15:26:46 +01002920 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002921 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002922
2923 iommu = amd_iommu_rlookup_table[devid];
2924 if (!iommu)
2925 return;
2926
Joerg Roedel684f2882008-12-08 12:07:44 +01002927 iommu_completion_wait(iommu);
2928}
2929
Joerg Roedel01106062008-12-02 19:34:11 +01002930static int amd_iommu_attach_device(struct iommu_domain *dom,
2931 struct device *dev)
2932{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002933 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002934 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002935 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002936 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002937
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002938 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002939 return -EINVAL;
2940
Joerg Roedel657cbb62009-11-23 15:26:46 +01002941 dev_data = dev->archdata.iommu;
2942
Joerg Roedelf62dda62011-06-09 12:55:35 +02002943 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002944 if (!iommu)
2945 return -EINVAL;
2946
Joerg Roedel657cbb62009-11-23 15:26:46 +01002947 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002948 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002949
Joerg Roedel15898bb2009-11-24 15:39:42 +01002950 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002951
2952 iommu_completion_wait(iommu);
2953
Joerg Roedel15898bb2009-11-24 15:39:42 +01002954 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002955}
2956
Joerg Roedel468e2362010-01-21 16:37:36 +01002957static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002958 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002959{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002960 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002961 int prot = 0;
2962 int ret;
2963
Joerg Roedel132bd682011-11-17 14:18:46 +01002964 if (domain->mode == PAGE_MODE_NONE)
2965 return -EINVAL;
2966
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002967 if (iommu_prot & IOMMU_READ)
2968 prot |= IOMMU_PROT_IR;
2969 if (iommu_prot & IOMMU_WRITE)
2970 prot |= IOMMU_PROT_IW;
2971
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002972 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02002973 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002974 mutex_unlock(&domain->api_lock);
2975
Joerg Roedel795e74f72010-05-11 17:40:57 +02002976 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002977}
2978
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002979static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2980 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002981{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002982 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002983 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002984
Joerg Roedel132bd682011-11-17 14:18:46 +01002985 if (domain->mode == PAGE_MODE_NONE)
2986 return -EINVAL;
2987
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002988 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002989 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002990 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002991
Joerg Roedel17b124b2011-04-06 18:01:35 +02002992 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002993
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002994 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002995}
2996
Joerg Roedel645c4c82008-12-02 20:05:50 +01002997static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547a2013-03-29 01:23:58 +05302998 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01002999{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003000 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003001 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003002 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003003
Joerg Roedel132bd682011-11-17 14:18:46 +01003004 if (domain->mode == PAGE_MODE_NONE)
3005 return iova;
3006
Joerg Roedel3039ca12015-04-01 14:58:48 +02003007 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003008
Joerg Roedela6d41a42009-09-02 17:08:55 +02003009 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003010 return 0;
3011
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003012 offset_mask = pte_pgsize - 1;
3013 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003014
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003015 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003016}
3017
Joerg Roedelab636482014-09-05 10:48:21 +02003018static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003019{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003020 switch (cap) {
3021 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003022 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003023 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003024 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003025 case IOMMU_CAP_NOEXEC:
3026 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003027 }
3028
Joerg Roedelab636482014-09-05 10:48:21 +02003029 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003030}
3031
Joerg Roedel35cf2482015-05-28 18:41:37 +02003032static void amd_iommu_get_dm_regions(struct device *dev,
3033 struct list_head *head)
3034{
3035 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003036 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003037
3038 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003039 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003040 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003041
3042 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3043 struct iommu_dm_region *region;
3044
3045 if (devid < entry->devid_start || devid > entry->devid_end)
3046 continue;
3047
3048 region = kzalloc(sizeof(*region), GFP_KERNEL);
3049 if (!region) {
3050 pr_err("Out of memory allocating dm-regions for %s\n",
3051 dev_name(dev));
3052 return;
3053 }
3054
3055 region->start = entry->address_start;
3056 region->length = entry->address_end - entry->address_start;
3057 if (entry->prot & IOMMU_PROT_IR)
3058 region->prot |= IOMMU_READ;
3059 if (entry->prot & IOMMU_PROT_IW)
3060 region->prot |= IOMMU_WRITE;
3061
3062 list_add_tail(&region->list, head);
3063 }
3064}
3065
3066static void amd_iommu_put_dm_regions(struct device *dev,
3067 struct list_head *head)
3068{
3069 struct iommu_dm_region *entry, *next;
3070
3071 list_for_each_entry_safe(entry, next, head, list)
3072 kfree(entry);
3073}
3074
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003075static void amd_iommu_apply_dm_region(struct device *dev,
3076 struct iommu_domain *domain,
3077 struct iommu_dm_region *region)
3078{
3079 struct protection_domain *pdomain = to_pdomain(domain);
3080 struct dma_ops_domain *dma_dom = pdomain->priv;
3081 unsigned long start, end;
3082
3083 start = IOVA_PFN(region->start);
3084 end = IOVA_PFN(region->start + region->length);
3085
3086 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
3087}
3088
Thierry Redingb22f6432014-06-27 09:03:12 +02003089static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003090 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003091 .domain_alloc = amd_iommu_domain_alloc,
3092 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003093 .attach_dev = amd_iommu_attach_device,
3094 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003095 .map = amd_iommu_map,
3096 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003097 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003098 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003099 .add_device = amd_iommu_add_device,
3100 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04003101 .device_group = amd_iommu_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003102 .get_dm_regions = amd_iommu_get_dm_regions,
3103 .put_dm_regions = amd_iommu_put_dm_regions,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003104 .apply_dm_region = amd_iommu_apply_dm_region,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003105 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003106};
3107
Joerg Roedel0feae532009-08-26 15:26:30 +02003108/*****************************************************************************
3109 *
3110 * The next functions do a basic initialization of IOMMU for pass through
3111 * mode
3112 *
3113 * In passthrough mode the IOMMU is initialized and enabled but not used for
3114 * DMA-API translation.
3115 *
3116 *****************************************************************************/
3117
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003118/* IOMMUv2 specific functions */
3119int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3120{
3121 return atomic_notifier_chain_register(&ppr_notifier, nb);
3122}
3123EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3124
3125int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3126{
3127 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3128}
3129EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003130
3131void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3132{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003133 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003134 unsigned long flags;
3135
3136 spin_lock_irqsave(&domain->lock, flags);
3137
3138 /* Update data structure */
3139 domain->mode = PAGE_MODE_NONE;
3140 domain->updated = true;
3141
3142 /* Make changes visible to IOMMUs */
3143 update_domain(domain);
3144
3145 /* Page-table is not visible to IOMMU anymore, so free it */
3146 free_pagetable(domain);
3147
3148 spin_unlock_irqrestore(&domain->lock, flags);
3149}
3150EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003151
3152int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3153{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003154 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003155 unsigned long flags;
3156 int levels, ret;
3157
3158 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3159 return -EINVAL;
3160
3161 /* Number of GCR3 table levels required */
3162 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3163 levels += 1;
3164
3165 if (levels > amd_iommu_max_glx_val)
3166 return -EINVAL;
3167
3168 spin_lock_irqsave(&domain->lock, flags);
3169
3170 /*
3171 * Save us all sanity checks whether devices already in the
3172 * domain support IOMMUv2. Just force that the domain has no
3173 * devices attached when it is switched into IOMMUv2 mode.
3174 */
3175 ret = -EBUSY;
3176 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3177 goto out;
3178
3179 ret = -ENOMEM;
3180 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3181 if (domain->gcr3_tbl == NULL)
3182 goto out;
3183
3184 domain->glx = levels;
3185 domain->flags |= PD_IOMMUV2_MASK;
3186 domain->updated = true;
3187
3188 update_domain(domain);
3189
3190 ret = 0;
3191
3192out:
3193 spin_unlock_irqrestore(&domain->lock, flags);
3194
3195 return ret;
3196}
3197EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003198
3199static int __flush_pasid(struct protection_domain *domain, int pasid,
3200 u64 address, bool size)
3201{
3202 struct iommu_dev_data *dev_data;
3203 struct iommu_cmd cmd;
3204 int i, ret;
3205
3206 if (!(domain->flags & PD_IOMMUV2_MASK))
3207 return -EINVAL;
3208
3209 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3210
3211 /*
3212 * IOMMU TLB needs to be flushed before Device TLB to
3213 * prevent device TLB refill from IOMMU TLB
3214 */
3215 for (i = 0; i < amd_iommus_present; ++i) {
3216 if (domain->dev_iommu[i] == 0)
3217 continue;
3218
3219 ret = iommu_queue_command(amd_iommus[i], &cmd);
3220 if (ret != 0)
3221 goto out;
3222 }
3223
3224 /* Wait until IOMMU TLB flushes are complete */
3225 domain_flush_complete(domain);
3226
3227 /* Now flush device TLBs */
3228 list_for_each_entry(dev_data, &domain->dev_list, list) {
3229 struct amd_iommu *iommu;
3230 int qdep;
3231
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003232 /*
3233 There might be non-IOMMUv2 capable devices in an IOMMUv2
3234 * domain.
3235 */
3236 if (!dev_data->ats.enabled)
3237 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003238
3239 qdep = dev_data->ats.qdep;
3240 iommu = amd_iommu_rlookup_table[dev_data->devid];
3241
3242 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3243 qdep, address, size);
3244
3245 ret = iommu_queue_command(iommu, &cmd);
3246 if (ret != 0)
3247 goto out;
3248 }
3249
3250 /* Wait until all device TLBs are flushed */
3251 domain_flush_complete(domain);
3252
3253 ret = 0;
3254
3255out:
3256
3257 return ret;
3258}
3259
3260static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3261 u64 address)
3262{
3263 return __flush_pasid(domain, pasid, address, false);
3264}
3265
3266int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3267 u64 address)
3268{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003269 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003270 unsigned long flags;
3271 int ret;
3272
3273 spin_lock_irqsave(&domain->lock, flags);
3274 ret = __amd_iommu_flush_page(domain, pasid, address);
3275 spin_unlock_irqrestore(&domain->lock, flags);
3276
3277 return ret;
3278}
3279EXPORT_SYMBOL(amd_iommu_flush_page);
3280
3281static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3282{
3283 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3284 true);
3285}
3286
3287int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3288{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003289 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003290 unsigned long flags;
3291 int ret;
3292
3293 spin_lock_irqsave(&domain->lock, flags);
3294 ret = __amd_iommu_flush_tlb(domain, pasid);
3295 spin_unlock_irqrestore(&domain->lock, flags);
3296
3297 return ret;
3298}
3299EXPORT_SYMBOL(amd_iommu_flush_tlb);
3300
Joerg Roedelb16137b2011-11-21 16:50:23 +01003301static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3302{
3303 int index;
3304 u64 *pte;
3305
3306 while (true) {
3307
3308 index = (pasid >> (9 * level)) & 0x1ff;
3309 pte = &root[index];
3310
3311 if (level == 0)
3312 break;
3313
3314 if (!(*pte & GCR3_VALID)) {
3315 if (!alloc)
3316 return NULL;
3317
3318 root = (void *)get_zeroed_page(GFP_ATOMIC);
3319 if (root == NULL)
3320 return NULL;
3321
3322 *pte = __pa(root) | GCR3_VALID;
3323 }
3324
3325 root = __va(*pte & PAGE_MASK);
3326
3327 level -= 1;
3328 }
3329
3330 return pte;
3331}
3332
3333static int __set_gcr3(struct protection_domain *domain, int pasid,
3334 unsigned long cr3)
3335{
3336 u64 *pte;
3337
3338 if (domain->mode != PAGE_MODE_NONE)
3339 return -EINVAL;
3340
3341 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3342 if (pte == NULL)
3343 return -ENOMEM;
3344
3345 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3346
3347 return __amd_iommu_flush_tlb(domain, pasid);
3348}
3349
3350static int __clear_gcr3(struct protection_domain *domain, int pasid)
3351{
3352 u64 *pte;
3353
3354 if (domain->mode != PAGE_MODE_NONE)
3355 return -EINVAL;
3356
3357 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3358 if (pte == NULL)
3359 return 0;
3360
3361 *pte = 0;
3362
3363 return __amd_iommu_flush_tlb(domain, pasid);
3364}
3365
3366int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3367 unsigned long cr3)
3368{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003369 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003370 unsigned long flags;
3371 int ret;
3372
3373 spin_lock_irqsave(&domain->lock, flags);
3374 ret = __set_gcr3(domain, pasid, cr3);
3375 spin_unlock_irqrestore(&domain->lock, flags);
3376
3377 return ret;
3378}
3379EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3380
3381int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3382{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003383 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003384 unsigned long flags;
3385 int ret;
3386
3387 spin_lock_irqsave(&domain->lock, flags);
3388 ret = __clear_gcr3(domain, pasid);
3389 spin_unlock_irqrestore(&domain->lock, flags);
3390
3391 return ret;
3392}
3393EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003394
3395int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3396 int status, int tag)
3397{
3398 struct iommu_dev_data *dev_data;
3399 struct amd_iommu *iommu;
3400 struct iommu_cmd cmd;
3401
3402 dev_data = get_dev_data(&pdev->dev);
3403 iommu = amd_iommu_rlookup_table[dev_data->devid];
3404
3405 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3406 tag, dev_data->pri_tlp);
3407
3408 return iommu_queue_command(iommu, &cmd);
3409}
3410EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003411
3412struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3413{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003414 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003415
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003416 pdomain = get_domain(&pdev->dev);
3417 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003418 return NULL;
3419
3420 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003421 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003422 return NULL;
3423
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003424 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003425}
3426EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003427
3428void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3429{
3430 struct iommu_dev_data *dev_data;
3431
3432 if (!amd_iommu_v2_supported())
3433 return;
3434
3435 dev_data = get_dev_data(&pdev->dev);
3436 dev_data->errata |= (1 << erratum);
3437}
3438EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003439
3440int amd_iommu_device_info(struct pci_dev *pdev,
3441 struct amd_iommu_device_info *info)
3442{
3443 int max_pasids;
3444 int pos;
3445
3446 if (pdev == NULL || info == NULL)
3447 return -EINVAL;
3448
3449 if (!amd_iommu_v2_supported())
3450 return -EINVAL;
3451
3452 memset(info, 0, sizeof(*info));
3453
3454 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3455 if (pos)
3456 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3457
3458 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3459 if (pos)
3460 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3461
3462 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3463 if (pos) {
3464 int features;
3465
3466 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3467 max_pasids = min(max_pasids, (1 << 20));
3468
3469 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3470 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3471
3472 features = pci_pasid_features(pdev);
3473 if (features & PCI_PASID_CAP_EXEC)
3474 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3475 if (features & PCI_PASID_CAP_PRIV)
3476 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3477 }
3478
3479 return 0;
3480}
3481EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003482
3483#ifdef CONFIG_IRQ_REMAP
3484
3485/*****************************************************************************
3486 *
3487 * Interrupt Remapping Implementation
3488 *
3489 *****************************************************************************/
3490
3491union irte {
3492 u32 val;
3493 struct {
3494 u32 valid : 1,
3495 no_fault : 1,
3496 int_type : 3,
3497 rq_eoi : 1,
3498 dm : 1,
3499 rsvd_1 : 1,
3500 destination : 8,
3501 vector : 8,
3502 rsvd_2 : 8;
3503 } fields;
3504};
3505
Jiang Liu9c724962015-04-14 10:29:52 +08003506struct irq_2_irte {
3507 u16 devid; /* Device ID for IRTE table */
3508 u16 index; /* Index into IRTE table*/
3509};
3510
Jiang Liu7c71d302015-04-13 14:11:33 +08003511struct amd_ir_data {
3512 struct irq_2_irte irq_2_irte;
3513 union irte irte_entry;
3514 union {
3515 struct msi_msg msi_entry;
3516 };
3517};
3518
3519static struct irq_chip amd_ir_chip;
3520
Joerg Roedel2b324502012-06-21 16:29:10 +02003521#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3522#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3523#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3524#define DTE_IRQ_REMAP_ENABLE 1ULL
3525
3526static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3527{
3528 u64 dte;
3529
3530 dte = amd_iommu_dev_table[devid].data[2];
3531 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3532 dte |= virt_to_phys(table->table);
3533 dte |= DTE_IRQ_REMAP_INTCTL;
3534 dte |= DTE_IRQ_TABLE_LEN;
3535 dte |= DTE_IRQ_REMAP_ENABLE;
3536
3537 amd_iommu_dev_table[devid].data[2] = dte;
3538}
3539
3540#define IRTE_ALLOCATED (~1U)
3541
3542static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3543{
3544 struct irq_remap_table *table = NULL;
3545 struct amd_iommu *iommu;
3546 unsigned long flags;
3547 u16 alias;
3548
3549 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3550
3551 iommu = amd_iommu_rlookup_table[devid];
3552 if (!iommu)
3553 goto out_unlock;
3554
3555 table = irq_lookup_table[devid];
3556 if (table)
3557 goto out;
3558
3559 alias = amd_iommu_alias_table[devid];
3560 table = irq_lookup_table[alias];
3561 if (table) {
3562 irq_lookup_table[devid] = table;
3563 set_dte_irq_entry(devid, table);
3564 iommu_flush_dte(iommu, devid);
3565 goto out;
3566 }
3567
3568 /* Nothing there yet, allocate new irq remapping table */
3569 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3570 if (!table)
3571 goto out;
3572
Joerg Roedel197887f2013-04-09 21:14:08 +02003573 /* Initialize table spin-lock */
3574 spin_lock_init(&table->lock);
3575
Joerg Roedel2b324502012-06-21 16:29:10 +02003576 if (ioapic)
3577 /* Keep the first 32 indexes free for IOAPIC interrupts */
3578 table->min_index = 32;
3579
3580 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3581 if (!table->table) {
3582 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003583 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003584 goto out;
3585 }
3586
3587 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3588
3589 if (ioapic) {
3590 int i;
3591
3592 for (i = 0; i < 32; ++i)
3593 table->table[i] = IRTE_ALLOCATED;
3594 }
3595
3596 irq_lookup_table[devid] = table;
3597 set_dte_irq_entry(devid, table);
3598 iommu_flush_dte(iommu, devid);
3599 if (devid != alias) {
3600 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003601 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003602 iommu_flush_dte(iommu, alias);
3603 }
3604
3605out:
3606 iommu_completion_wait(iommu);
3607
3608out_unlock:
3609 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3610
3611 return table;
3612}
3613
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003614static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003615{
3616 struct irq_remap_table *table;
3617 unsigned long flags;
3618 int index, c;
3619
3620 table = get_irq_table(devid, false);
3621 if (!table)
3622 return -ENODEV;
3623
3624 spin_lock_irqsave(&table->lock, flags);
3625
3626 /* Scan table for free entries */
3627 for (c = 0, index = table->min_index;
3628 index < MAX_IRQS_PER_TABLE;
3629 ++index) {
3630 if (table->table[index] == 0)
3631 c += 1;
3632 else
3633 c = 0;
3634
3635 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003636 for (; c != 0; --c)
3637 table->table[index - c + 1] = IRTE_ALLOCATED;
3638
3639 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003640 goto out;
3641 }
3642 }
3643
3644 index = -ENOSPC;
3645
3646out:
3647 spin_unlock_irqrestore(&table->lock, flags);
3648
3649 return index;
3650}
3651
Joerg Roedel2b324502012-06-21 16:29:10 +02003652static int modify_irte(u16 devid, int index, union irte irte)
3653{
3654 struct irq_remap_table *table;
3655 struct amd_iommu *iommu;
3656 unsigned long flags;
3657
3658 iommu = amd_iommu_rlookup_table[devid];
3659 if (iommu == NULL)
3660 return -EINVAL;
3661
3662 table = get_irq_table(devid, false);
3663 if (!table)
3664 return -ENOMEM;
3665
3666 spin_lock_irqsave(&table->lock, flags);
3667 table->table[index] = irte.val;
3668 spin_unlock_irqrestore(&table->lock, flags);
3669
3670 iommu_flush_irt(iommu, devid);
3671 iommu_completion_wait(iommu);
3672
3673 return 0;
3674}
3675
3676static void free_irte(u16 devid, int index)
3677{
3678 struct irq_remap_table *table;
3679 struct amd_iommu *iommu;
3680 unsigned long flags;
3681
3682 iommu = amd_iommu_rlookup_table[devid];
3683 if (iommu == NULL)
3684 return;
3685
3686 table = get_irq_table(devid, false);
3687 if (!table)
3688 return;
3689
3690 spin_lock_irqsave(&table->lock, flags);
3691 table->table[index] = 0;
3692 spin_unlock_irqrestore(&table->lock, flags);
3693
3694 iommu_flush_irt(iommu, devid);
3695 iommu_completion_wait(iommu);
3696}
3697
Jiang Liu7c71d302015-04-13 14:11:33 +08003698static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003699{
Jiang Liu7c71d302015-04-13 14:11:33 +08003700 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003701
Jiang Liu7c71d302015-04-13 14:11:33 +08003702 switch (info->type) {
3703 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3704 devid = get_ioapic_devid(info->ioapic_id);
3705 break;
3706 case X86_IRQ_ALLOC_TYPE_HPET:
3707 devid = get_hpet_devid(info->hpet_id);
3708 break;
3709 case X86_IRQ_ALLOC_TYPE_MSI:
3710 case X86_IRQ_ALLOC_TYPE_MSIX:
3711 devid = get_device_id(&info->msi_dev->dev);
3712 break;
3713 default:
3714 BUG_ON(1);
3715 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003716 }
3717
Jiang Liu7c71d302015-04-13 14:11:33 +08003718 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003719}
3720
Jiang Liu7c71d302015-04-13 14:11:33 +08003721static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003722{
Jiang Liu7c71d302015-04-13 14:11:33 +08003723 struct amd_iommu *iommu;
3724 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003725
Jiang Liu7c71d302015-04-13 14:11:33 +08003726 if (!info)
3727 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003728
Jiang Liu7c71d302015-04-13 14:11:33 +08003729 devid = get_devid(info);
3730 if (devid >= 0) {
3731 iommu = amd_iommu_rlookup_table[devid];
3732 if (iommu)
3733 return iommu->ir_domain;
3734 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003735
Jiang Liu7c71d302015-04-13 14:11:33 +08003736 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003737}
3738
Jiang Liu7c71d302015-04-13 14:11:33 +08003739static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003740{
Jiang Liu7c71d302015-04-13 14:11:33 +08003741 struct amd_iommu *iommu;
3742 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003743
Jiang Liu7c71d302015-04-13 14:11:33 +08003744 if (!info)
3745 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003746
Jiang Liu7c71d302015-04-13 14:11:33 +08003747 switch (info->type) {
3748 case X86_IRQ_ALLOC_TYPE_MSI:
3749 case X86_IRQ_ALLOC_TYPE_MSIX:
3750 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003751 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003752 return NULL;
3753
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003754 iommu = amd_iommu_rlookup_table[devid];
3755 if (iommu)
3756 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003757 break;
3758 default:
3759 break;
3760 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003761
Jiang Liu7c71d302015-04-13 14:11:33 +08003762 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003763}
3764
Joerg Roedel6b474b82012-06-26 16:46:04 +02003765struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003766 .prepare = amd_iommu_prepare,
3767 .enable = amd_iommu_enable,
3768 .disable = amd_iommu_disable,
3769 .reenable = amd_iommu_reenable,
3770 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003771 .get_ir_irq_domain = get_ir_irq_domain,
3772 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003773};
Jiang Liu7c71d302015-04-13 14:11:33 +08003774
3775static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3776 struct irq_cfg *irq_cfg,
3777 struct irq_alloc_info *info,
3778 int devid, int index, int sub_handle)
3779{
3780 struct irq_2_irte *irte_info = &data->irq_2_irte;
3781 struct msi_msg *msg = &data->msi_entry;
3782 union irte *irte = &data->irte_entry;
3783 struct IO_APIC_route_entry *entry;
3784
Jiang Liu7c71d302015-04-13 14:11:33 +08003785 data->irq_2_irte.devid = devid;
3786 data->irq_2_irte.index = index + sub_handle;
3787
3788 /* Setup IRTE for IOMMU */
3789 irte->val = 0;
3790 irte->fields.vector = irq_cfg->vector;
3791 irte->fields.int_type = apic->irq_delivery_mode;
3792 irte->fields.destination = irq_cfg->dest_apicid;
3793 irte->fields.dm = apic->irq_dest_mode;
3794 irte->fields.valid = 1;
3795
3796 switch (info->type) {
3797 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3798 /* Setup IOAPIC entry */
3799 entry = info->ioapic_entry;
3800 info->ioapic_entry = NULL;
3801 memset(entry, 0, sizeof(*entry));
3802 entry->vector = index;
3803 entry->mask = 0;
3804 entry->trigger = info->ioapic_trigger;
3805 entry->polarity = info->ioapic_polarity;
3806 /* Mask level triggered irqs. */
3807 if (info->ioapic_trigger)
3808 entry->mask = 1;
3809 break;
3810
3811 case X86_IRQ_ALLOC_TYPE_HPET:
3812 case X86_IRQ_ALLOC_TYPE_MSI:
3813 case X86_IRQ_ALLOC_TYPE_MSIX:
3814 msg->address_hi = MSI_ADDR_BASE_HI;
3815 msg->address_lo = MSI_ADDR_BASE_LO;
3816 msg->data = irte_info->index;
3817 break;
3818
3819 default:
3820 BUG_ON(1);
3821 break;
3822 }
3823}
3824
3825static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3826 unsigned int nr_irqs, void *arg)
3827{
3828 struct irq_alloc_info *info = arg;
3829 struct irq_data *irq_data;
3830 struct amd_ir_data *data;
3831 struct irq_cfg *cfg;
3832 int i, ret, devid;
3833 int index = -1;
3834
3835 if (!info)
3836 return -EINVAL;
3837 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3838 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3839 return -EINVAL;
3840
3841 /*
3842 * With IRQ remapping enabled, don't need contiguous CPU vectors
3843 * to support multiple MSI interrupts.
3844 */
3845 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3846 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3847
3848 devid = get_devid(info);
3849 if (devid < 0)
3850 return -EINVAL;
3851
3852 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3853 if (ret < 0)
3854 return ret;
3855
Jiang Liu7c71d302015-04-13 14:11:33 +08003856 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3857 if (get_irq_table(devid, true))
3858 index = info->ioapic_pin;
3859 else
3860 ret = -ENOMEM;
3861 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003862 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003863 }
3864 if (index < 0) {
3865 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003866 goto out_free_parent;
3867 }
3868
3869 for (i = 0; i < nr_irqs; i++) {
3870 irq_data = irq_domain_get_irq_data(domain, virq + i);
3871 cfg = irqd_cfg(irq_data);
3872 if (!irq_data || !cfg) {
3873 ret = -EINVAL;
3874 goto out_free_data;
3875 }
3876
Joerg Roedela130e692015-08-13 11:07:25 +02003877 ret = -ENOMEM;
3878 data = kzalloc(sizeof(*data), GFP_KERNEL);
3879 if (!data)
3880 goto out_free_data;
3881
Jiang Liu7c71d302015-04-13 14:11:33 +08003882 irq_data->hwirq = (devid << 16) + i;
3883 irq_data->chip_data = data;
3884 irq_data->chip = &amd_ir_chip;
3885 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3886 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3887 }
Joerg Roedela130e692015-08-13 11:07:25 +02003888
Jiang Liu7c71d302015-04-13 14:11:33 +08003889 return 0;
3890
3891out_free_data:
3892 for (i--; i >= 0; i--) {
3893 irq_data = irq_domain_get_irq_data(domain, virq + i);
3894 if (irq_data)
3895 kfree(irq_data->chip_data);
3896 }
3897 for (i = 0; i < nr_irqs; i++)
3898 free_irte(devid, index + i);
3899out_free_parent:
3900 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3901 return ret;
3902}
3903
3904static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3905 unsigned int nr_irqs)
3906{
3907 struct irq_2_irte *irte_info;
3908 struct irq_data *irq_data;
3909 struct amd_ir_data *data;
3910 int i;
3911
3912 for (i = 0; i < nr_irqs; i++) {
3913 irq_data = irq_domain_get_irq_data(domain, virq + i);
3914 if (irq_data && irq_data->chip_data) {
3915 data = irq_data->chip_data;
3916 irte_info = &data->irq_2_irte;
3917 free_irte(irte_info->devid, irte_info->index);
3918 kfree(data);
3919 }
3920 }
3921 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3922}
3923
3924static void irq_remapping_activate(struct irq_domain *domain,
3925 struct irq_data *irq_data)
3926{
3927 struct amd_ir_data *data = irq_data->chip_data;
3928 struct irq_2_irte *irte_info = &data->irq_2_irte;
3929
3930 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3931}
3932
3933static void irq_remapping_deactivate(struct irq_domain *domain,
3934 struct irq_data *irq_data)
3935{
3936 struct amd_ir_data *data = irq_data->chip_data;
3937 struct irq_2_irte *irte_info = &data->irq_2_irte;
3938 union irte entry;
3939
3940 entry.val = 0;
3941 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3942}
3943
3944static struct irq_domain_ops amd_ir_domain_ops = {
3945 .alloc = irq_remapping_alloc,
3946 .free = irq_remapping_free,
3947 .activate = irq_remapping_activate,
3948 .deactivate = irq_remapping_deactivate,
3949};
3950
3951static int amd_ir_set_affinity(struct irq_data *data,
3952 const struct cpumask *mask, bool force)
3953{
3954 struct amd_ir_data *ir_data = data->chip_data;
3955 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3956 struct irq_cfg *cfg = irqd_cfg(data);
3957 struct irq_data *parent = data->parent_data;
3958 int ret;
3959
3960 ret = parent->chip->irq_set_affinity(parent, mask, force);
3961 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3962 return ret;
3963
3964 /*
3965 * Atomically updates the IRTE with the new destination, vector
3966 * and flushes the interrupt entry cache.
3967 */
3968 ir_data->irte_entry.fields.vector = cfg->vector;
3969 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
3970 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
3971
3972 /*
3973 * After this point, all the interrupts will start arriving
3974 * at the new destination. So, time to cleanup the previous
3975 * vector allocation.
3976 */
Jiang Liuc6c20022015-04-14 10:30:02 +08003977 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08003978
3979 return IRQ_SET_MASK_OK_DONE;
3980}
3981
3982static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3983{
3984 struct amd_ir_data *ir_data = irq_data->chip_data;
3985
3986 *msg = ir_data->msi_entry;
3987}
3988
3989static struct irq_chip amd_ir_chip = {
3990 .irq_ack = ir_ack_apic_edge,
3991 .irq_set_affinity = amd_ir_set_affinity,
3992 .irq_compose_msi_msg = ir_compose_msi_msg,
3993};
3994
3995int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3996{
3997 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
3998 if (!iommu->ir_domain)
3999 return -ENOMEM;
4000
4001 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4002 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4003
4004 return 0;
4005}
Joerg Roedel2b324502012-06-21 16:29:10 +02004006#endif