blob: 089e1ed4deec1bdc8bd90d1177336c1a520c90a0 [file] [log] [blame]
Joerg Roedelb6c02712008-06-26 21:27:53 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelb6c02712008-06-26 21:27:53 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010020#include <linux/ratelimit.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020021#include <linux/pci.h>
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -040022#include <linux/acpi.h>
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -040023#include <linux/amba/bus.h>
Wan Zongshun0076cd32016-05-10 09:21:01 -040024#include <linux/platform_device.h>
Joerg Roedelcb41ed82011-04-05 11:00:53 +020025#include <linux/pci-ats.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080026#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Joerg Roedel7f265082008-12-12 13:50:21 +010028#include <linux/debugfs.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020029#include <linux/scatterlist.h>
FUJITA Tomonori51491362009-01-05 23:47:25 +090030#include <linux/dma-mapping.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020031#include <linux/iommu-helper.h>
Joerg Roedelc156e342008-12-02 18:13:27 +010032#include <linux/iommu.h>
Joerg Roedel815b33f2011-04-06 17:26:49 +020033#include <linux/delay.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020034#include <linux/amd-iommu.h>
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010035#include <linux/notifier.h>
36#include <linux/export.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020037#include <linux/irq.h>
38#include <linux/msi.h>
Joerg Roedel3b839a52015-04-01 14:58:47 +020039#include <linux/dma-contiguous.h>
Jiang Liu7c71d302015-04-13 14:11:33 +080040#include <linux/irqdomain.h>
Joerg Roedel5f6bed52015-12-22 13:34:22 +010041#include <linux/percpu.h>
Joerg Roedel307d5852016-07-05 11:54:04 +020042#include <linux/iova.h>
Joerg Roedel2b324502012-06-21 16:29:10 +020043#include <asm/irq_remapping.h>
44#include <asm/io_apic.h>
45#include <asm/apic.h>
46#include <asm/hw_irq.h>
Joerg Roedel17f5b562011-07-06 17:14:44 +020047#include <asm/msidef.h>
Joerg Roedelb6c02712008-06-26 21:27:53 +020048#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090049#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010050#include <asm/gart.h>
Joerg Roedel27c21272011-05-30 15:56:24 +020051#include <asm/dma.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020052
53#include "amd_iommu_proto.h"
54#include "amd_iommu_types.h"
Joerg Roedel6b474b82012-06-26 16:46:04 +020055#include "irq_remapping.h"
Joerg Roedelb6c02712008-06-26 21:27:53 +020056
57#define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
58
Joerg Roedel815b33f2011-04-06 17:26:49 +020059#define LOOP_TIMEOUT 100000
Joerg Roedel136f78a2008-07-11 17:14:27 +020060
Joerg Roedel307d5852016-07-05 11:54:04 +020061/* IO virtual address start page frame number */
62#define IOVA_START_PFN (1)
63#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
64#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
65
Joerg Roedel81cd07b2016-07-07 18:01:10 +020066/* Reserved IOVA ranges */
67#define MSI_RANGE_START (0xfee00000)
68#define MSI_RANGE_END (0xfeefffff)
69#define HT_RANGE_START (0xfd00000000ULL)
70#define HT_RANGE_END (0xffffffffffULL)
71
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020072/*
73 * This bitmap is used to advertise the page sizes our hardware support
74 * to the IOMMU core, which will then use this information to split
75 * physically contiguous memory regions it is mapping into page sizes
76 * that we support.
77 *
Joerg Roedel954e3dd2012-12-02 15:35:37 +010078 * 512GB Pages are not supported due to a hardware bug
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020079 */
Joerg Roedel954e3dd2012-12-02 15:35:37 +010080#define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +020081
Joerg Roedelb6c02712008-06-26 21:27:53 +020082static DEFINE_RWLOCK(amd_iommu_devtable_lock);
83
Joerg Roedel8fa5f802011-06-09 12:24:45 +020084/* List of all available dev_data structures */
85static LIST_HEAD(dev_data_list);
86static DEFINE_SPINLOCK(dev_data_list_lock);
87
Joerg Roedel6efed632012-06-14 15:52:58 +020088LIST_HEAD(ioapic_map);
89LIST_HEAD(hpet_map);
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040090LIST_HEAD(acpihid_map);
Joerg Roedel6efed632012-06-14 15:52:58 +020091
Joerg Roedelc5b5da92016-07-06 11:55:37 +020092#define FLUSH_QUEUE_SIZE 256
93
94struct flush_queue_entry {
95 unsigned long iova_pfn;
96 unsigned long pages;
97 struct dma_ops_domain *dma_dom;
98};
99
100struct flush_queue {
101 spinlock_t lock;
102 unsigned next;
103 struct flush_queue_entry *entries;
104};
105
106DEFINE_PER_CPU(struct flush_queue, flush_queue);
107
Joerg Roedelbb279472016-07-06 13:56:36 +0200108static atomic_t queue_timer_on;
109static struct timer_list queue_timer;
110
Joerg Roedel0feae532009-08-26 15:26:30 +0200111/*
112 * Domain for untranslated devices - only allocated
113 * if iommu=pt passed on kernel cmd line.
114 */
Thierry Redingb22f6432014-06-27 09:03:12 +0200115static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +0100116
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100117static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +0100118int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100119
Joerg Roedelac1534a2012-06-21 14:52:40 +0200120static struct dma_map_ops amd_iommu_dma_ops;
121
Joerg Roedel431b2a22008-07-11 17:14:22 +0200122/*
Joerg Roedel50917e22014-08-05 16:38:38 +0200123 * This struct contains device specific data for the IOMMU
124 */
125struct iommu_dev_data {
126 struct list_head list; /* For domain->dev_list */
127 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +0200128 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +0200129 u16 devid; /* PCI Device ID */
Joerg Roedele3156042016-04-08 15:12:24 +0200130 u16 alias; /* Alias Device ID */
Joerg Roedel50917e22014-08-05 16:38:38 +0200131 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +0200132 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +0200133 struct {
134 bool enabled;
135 int qdep;
136 } ats; /* ATS state */
137 bool pri_tlp; /* PASID TLB required for
138 PPR completions */
139 u32 errata; /* Bitmap for errata to apply */
140};
141
142/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200143 * general struct to manage commands send to an IOMMU
144 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200145struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200146 u32 data[4];
147};
148
Joerg Roedel05152a02012-06-15 16:53:51 +0200149struct kmem_cache *amd_iommu_irq_cache;
150
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200151static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200152static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100153static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700154
Joerg Roedel007b74b2015-12-21 12:53:54 +0100155/*
Joerg Roedel007b74b2015-12-21 12:53:54 +0100156 * Data container for a dma_ops specific protection domain
157 */
158struct dma_ops_domain {
159 /* generic protection domain information */
160 struct protection_domain domain;
161
Joerg Roedel307d5852016-07-05 11:54:04 +0200162 /* IOVA RB-Tree */
163 struct iova_domain iovad;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100164};
165
Joerg Roedel81cd07b2016-07-07 18:01:10 +0200166static struct iova_domain reserved_iova_ranges;
167static struct lock_class_key reserved_rbtree_key;
168
Joerg Roedel15898bb2009-11-24 15:39:42 +0100169/****************************************************************************
170 *
171 * Helper functions
172 *
173 ****************************************************************************/
174
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400175static inline int match_hid_uid(struct device *dev,
176 struct acpihid_map_entry *entry)
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100177{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400178 const char *hid, *uid;
179
180 hid = acpi_device_hid(ACPI_COMPANION(dev));
181 uid = acpi_device_uid(ACPI_COMPANION(dev));
182
183 if (!hid || !(*hid))
184 return -ENODEV;
185
186 if (!uid || !(*uid))
187 return strcmp(hid, entry->hid);
188
189 if (!(*entry->uid))
190 return strcmp(hid, entry->hid);
191
192 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100193}
194
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400195static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedele3156042016-04-08 15:12:24 +0200196{
197 struct pci_dev *pdev = to_pci_dev(dev);
198
199 return PCI_DEVID(pdev->bus->number, pdev->devfn);
200}
201
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400202static inline int get_acpihid_device_id(struct device *dev,
203 struct acpihid_map_entry **entry)
204{
205 struct acpihid_map_entry *p;
206
207 list_for_each_entry(p, &acpihid_map, list) {
208 if (!match_hid_uid(dev, p)) {
209 if (entry)
210 *entry = p;
211 return p->devid;
212 }
213 }
214 return -EINVAL;
215}
216
217static inline int get_device_id(struct device *dev)
218{
219 int devid;
220
221 if (dev_is_pci(dev))
222 devid = get_pci_device_id(dev);
223 else
224 devid = get_acpihid_device_id(dev, NULL);
225
226 return devid;
227}
228
Joerg Roedel15898bb2009-11-24 15:39:42 +0100229static struct protection_domain *to_pdomain(struct iommu_domain *dom)
230{
231 return container_of(dom, struct protection_domain, domain);
232}
233
Joerg Roedelb3311b02016-07-08 13:31:31 +0200234static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
235{
236 BUG_ON(domain->flags != PD_DMA_OPS_MASK);
237 return container_of(domain, struct dma_ops_domain, domain);
238}
239
Joerg Roedelf62dda62011-06-09 12:55:35 +0200240static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200241{
242 struct iommu_dev_data *dev_data;
243 unsigned long flags;
244
245 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
246 if (!dev_data)
247 return NULL;
248
Joerg Roedelf62dda62011-06-09 12:55:35 +0200249 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200250
251 spin_lock_irqsave(&dev_data_list_lock, flags);
252 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
253 spin_unlock_irqrestore(&dev_data_list_lock, flags);
254
255 return dev_data;
256}
257
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200258static struct iommu_dev_data *search_dev_data(u16 devid)
259{
260 struct iommu_dev_data *dev_data;
261 unsigned long flags;
262
263 spin_lock_irqsave(&dev_data_list_lock, flags);
264 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
265 if (dev_data->devid == devid)
266 goto out_unlock;
267 }
268
269 dev_data = NULL;
270
271out_unlock:
272 spin_unlock_irqrestore(&dev_data_list_lock, flags);
273
274 return dev_data;
275}
276
Joerg Roedele3156042016-04-08 15:12:24 +0200277static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
278{
279 *(u16 *)data = alias;
280 return 0;
281}
282
283static u16 get_alias(struct device *dev)
284{
285 struct pci_dev *pdev = to_pci_dev(dev);
286 u16 devid, ivrs_alias, pci_alias;
287
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200288 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200289 devid = get_device_id(dev);
290 ivrs_alias = amd_iommu_alias_table[devid];
291 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
292
293 if (ivrs_alias == pci_alias)
294 return ivrs_alias;
295
296 /*
297 * DMA alias showdown
298 *
299 * The IVRS is fairly reliable in telling us about aliases, but it
300 * can't know about every screwy device. If we don't have an IVRS
301 * reported alias, use the PCI reported alias. In that case we may
302 * still need to initialize the rlookup and dev_table entries if the
303 * alias is to a non-existent device.
304 */
305 if (ivrs_alias == devid) {
306 if (!amd_iommu_rlookup_table[pci_alias]) {
307 amd_iommu_rlookup_table[pci_alias] =
308 amd_iommu_rlookup_table[devid];
309 memcpy(amd_iommu_dev_table[pci_alias].data,
310 amd_iommu_dev_table[devid].data,
311 sizeof(amd_iommu_dev_table[pci_alias].data));
312 }
313
314 return pci_alias;
315 }
316
317 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
318 "for device %s[%04x:%04x], kernel reported alias "
319 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
320 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
321 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
322 PCI_FUNC(pci_alias));
323
324 /*
325 * If we don't have a PCI DMA alias and the IVRS alias is on the same
326 * bus, then the IVRS table may know about a quirk that we don't.
327 */
328 if (pci_alias == devid &&
329 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700330 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Joerg Roedele3156042016-04-08 15:12:24 +0200331 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
332 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
333 dev_name(dev));
334 }
335
336 return ivrs_alias;
337}
338
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200339static struct iommu_dev_data *find_dev_data(u16 devid)
340{
341 struct iommu_dev_data *dev_data;
342
343 dev_data = search_dev_data(devid);
344
345 if (dev_data == NULL)
346 dev_data = alloc_dev_data(devid);
347
348 return dev_data;
349}
350
Joerg Roedel657cbb62009-11-23 15:26:46 +0100351static struct iommu_dev_data *get_dev_data(struct device *dev)
352{
353 return dev->archdata.iommu;
354}
355
Wan Zongshunb097d112016-04-01 09:06:04 -0400356/*
357* Find or create an IOMMU group for a acpihid device.
358*/
359static struct iommu_group *acpihid_device_group(struct device *dev)
360{
361 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300362 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400363
364 devid = get_acpihid_device_id(dev, &entry);
365 if (devid < 0)
366 return ERR_PTR(devid);
367
368 list_for_each_entry(p, &acpihid_map, list) {
369 if ((devid == p->devid) && p->group)
370 entry->group = p->group;
371 }
372
373 if (!entry->group)
374 entry->group = generic_device_group(dev);
375
376 return entry->group;
377}
378
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100379static bool pci_iommuv2_capable(struct pci_dev *pdev)
380{
381 static const int caps[] = {
382 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100383 PCI_EXT_CAP_ID_PRI,
384 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100385 };
386 int i, pos;
387
388 for (i = 0; i < 3; ++i) {
389 pos = pci_find_ext_capability(pdev, caps[i]);
390 if (pos == 0)
391 return false;
392 }
393
394 return true;
395}
396
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100397static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
398{
399 struct iommu_dev_data *dev_data;
400
401 dev_data = get_dev_data(&pdev->dev);
402
403 return dev_data->errata & (1 << erratum) ? true : false;
404}
405
Joerg Roedel71c70982009-11-24 16:43:06 +0100406/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100407 * This function checks if the driver got a valid device from the caller to
408 * avoid dereferencing invalid pointers.
409 */
410static bool check_device(struct device *dev)
411{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400412 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100413
414 if (!dev || !dev->dma_mask)
415 return false;
416
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100417 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200418 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400419 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100420
421 /* Out of our scope? */
422 if (devid > amd_iommu_last_bdf)
423 return false;
424
425 if (amd_iommu_rlookup_table[devid] == NULL)
426 return false;
427
428 return true;
429}
430
Alex Williamson25b11ce2014-09-19 10:03:13 -0600431static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600432{
Alex Williamson2851db22012-10-08 22:49:41 -0600433 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600434
Alex Williamson65d53522014-07-03 09:51:30 -0600435 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200436 if (IS_ERR(group))
437 return;
438
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200439 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600440}
441
442static int iommu_init_device(struct device *dev)
443{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600444 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400445 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600446
447 if (dev->archdata.iommu)
448 return 0;
449
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400450 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200451 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400452 return devid;
453
454 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600455 if (!dev_data)
456 return -ENOMEM;
457
Joerg Roedele3156042016-04-08 15:12:24 +0200458 dev_data->alias = get_alias(dev);
459
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400460 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100461 struct amd_iommu *iommu;
462
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400463 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100464 dev_data->iommu_v2 = iommu->is_iommu_v2;
465 }
466
Joerg Roedel657cbb62009-11-23 15:26:46 +0100467 dev->archdata.iommu = dev_data;
468
Alex Williamson066f2e92014-06-12 16:12:37 -0600469 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
470 dev);
471
Joerg Roedel657cbb62009-11-23 15:26:46 +0100472 return 0;
473}
474
Joerg Roedel26018872011-06-06 16:50:14 +0200475static void iommu_ignore_device(struct device *dev)
476{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400477 u16 alias;
478 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200479
480 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200481 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400482 return;
483
Joerg Roedele3156042016-04-08 15:12:24 +0200484 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200485
486 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
487 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
488
489 amd_iommu_rlookup_table[devid] = NULL;
490 amd_iommu_rlookup_table[alias] = NULL;
491}
492
Joerg Roedel657cbb62009-11-23 15:26:46 +0100493static void iommu_uninit_device(struct device *dev)
494{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400495 int devid;
496 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600497
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400498 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200499 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400500 return;
501
502 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600503 if (!dev_data)
504 return;
505
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100506 if (dev_data->domain)
507 detach_device(dev);
508
Alex Williamson066f2e92014-06-12 16:12:37 -0600509 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
510 dev);
511
Alex Williamson9dcd6132012-05-30 14:19:07 -0600512 iommu_group_remove_device(dev);
513
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200514 /* Remove dma-ops */
515 dev->archdata.dma_ops = NULL;
516
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200517 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600518 * We keep dev_data around for unplugged devices and reuse it when the
519 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200520 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100521}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100522
Joerg Roedel431b2a22008-07-11 17:14:22 +0200523/****************************************************************************
524 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200525 * Interrupt handling functions
526 *
527 ****************************************************************************/
528
Joerg Roedele3e59872009-09-03 14:02:10 +0200529static void dump_dte_entry(u16 devid)
530{
531 int i;
532
Joerg Roedelee6c2862011-11-09 12:06:03 +0100533 for (i = 0; i < 4; ++i)
534 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200535 amd_iommu_dev_table[devid].data[i]);
536}
537
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200538static void dump_command(unsigned long phys_addr)
539{
540 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
541 int i;
542
543 for (i = 0; i < 4; ++i)
544 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
545}
546
Joerg Roedela345b232009-09-03 15:01:43 +0200547static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200548{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200549 int type, devid, domid, flags;
550 volatile u32 *event = __evt;
551 int count = 0;
552 u64 address;
553
554retry:
555 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
556 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
557 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
558 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
559 address = (u64)(((u64)event[3]) << 32) | event[2];
560
561 if (type == 0) {
562 /* Did we hit the erratum? */
563 if (++count == LOOP_TIMEOUT) {
564 pr_err("AMD-Vi: No event written to event log\n");
565 return;
566 }
567 udelay(1);
568 goto retry;
569 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200570
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200571 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200572
573 switch (type) {
574 case EVENT_TYPE_ILL_DEV:
575 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
576 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700577 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200578 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200579 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200580 break;
581 case EVENT_TYPE_IO_FAULT:
582 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
583 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700584 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200585 domid, address, flags);
586 break;
587 case EVENT_TYPE_DEV_TAB_ERR:
588 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
589 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700590 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200591 address, flags);
592 break;
593 case EVENT_TYPE_PAGE_TAB_ERR:
594 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
595 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700596 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200597 domid, address, flags);
598 break;
599 case EVENT_TYPE_ILL_CMD:
600 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200601 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200602 break;
603 case EVENT_TYPE_CMD_HARD_ERR:
604 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
605 "flags=0x%04x]\n", address, flags);
606 break;
607 case EVENT_TYPE_IOTLB_INV_TO:
608 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
609 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700610 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611 address);
612 break;
613 case EVENT_TYPE_INV_DEV_REQ:
614 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
615 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700616 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200617 address, flags);
618 break;
619 default:
620 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
621 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200622
623 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200624}
625
626static void iommu_poll_events(struct amd_iommu *iommu)
627{
628 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200629
630 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
631 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
632
633 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200634 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200635 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200636 }
637
638 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200639}
640
Joerg Roedeleee53532012-06-01 15:20:23 +0200641static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100642{
643 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100644
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100645 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
646 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
647 return;
648 }
649
650 fault.address = raw[1];
651 fault.pasid = PPR_PASID(raw[0]);
652 fault.device_id = PPR_DEVID(raw[0]);
653 fault.tag = PPR_TAG(raw[0]);
654 fault.flags = PPR_FLAGS(raw[0]);
655
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100656 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
657}
658
659static void iommu_poll_ppr_log(struct amd_iommu *iommu)
660{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100661 u32 head, tail;
662
663 if (iommu->ppr_log == NULL)
664 return;
665
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100666 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
667 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
668
669 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200670 volatile u64 *raw;
671 u64 entry[2];
672 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100673
Joerg Roedeleee53532012-06-01 15:20:23 +0200674 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100675
Joerg Roedeleee53532012-06-01 15:20:23 +0200676 /*
677 * Hardware bug: Interrupt may arrive before the entry is
678 * written to memory. If this happens we need to wait for the
679 * entry to arrive.
680 */
681 for (i = 0; i < LOOP_TIMEOUT; ++i) {
682 if (PPR_REQ_TYPE(raw[0]) != 0)
683 break;
684 udelay(1);
685 }
686
687 /* Avoid memcpy function-call overhead */
688 entry[0] = raw[0];
689 entry[1] = raw[1];
690
691 /*
692 * To detect the hardware bug we need to clear the entry
693 * back to zero.
694 */
695 raw[0] = raw[1] = 0UL;
696
697 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100698 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
699 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200700
Joerg Roedeleee53532012-06-01 15:20:23 +0200701 /* Handle PPR entry */
702 iommu_handle_ppr_entry(iommu, entry);
703
Joerg Roedeleee53532012-06-01 15:20:23 +0200704 /* Refresh ring-buffer information */
705 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100706 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
707 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100708}
709
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500710#ifdef CONFIG_IRQ_REMAP
711static int (*iommu_ga_log_notifier)(u32);
712
713int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
714{
715 iommu_ga_log_notifier = notifier;
716
717 return 0;
718}
719EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
720
721static void iommu_poll_ga_log(struct amd_iommu *iommu)
722{
723 u32 head, tail, cnt = 0;
724
725 if (iommu->ga_log == NULL)
726 return;
727
728 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
729 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
730
731 while (head != tail) {
732 volatile u64 *raw;
733 u64 log_entry;
734
735 raw = (u64 *)(iommu->ga_log + head);
736 cnt++;
737
738 /* Avoid memcpy function-call overhead */
739 log_entry = *raw;
740
741 /* Update head pointer of hardware ring-buffer */
742 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
743 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
744
745 /* Handle GA entry */
746 switch (GA_REQ_TYPE(log_entry)) {
747 case GA_GUEST_NR:
748 if (!iommu_ga_log_notifier)
749 break;
750
751 pr_debug("AMD-Vi: %s: devid=%#x, ga_tag=%#x\n",
752 __func__, GA_DEVID(log_entry),
753 GA_TAG(log_entry));
754
755 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
756 pr_err("AMD-Vi: GA log notifier failed.\n");
757 break;
758 default:
759 break;
760 }
761 }
762}
763#endif /* CONFIG_IRQ_REMAP */
764
765#define AMD_IOMMU_INT_MASK \
766 (MMIO_STATUS_EVT_INT_MASK | \
767 MMIO_STATUS_PPR_INT_MASK | \
768 MMIO_STATUS_GALOG_INT_MASK)
769
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200770irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200771{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500772 struct amd_iommu *iommu = (struct amd_iommu *) data;
773 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200774
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500775 while (status & AMD_IOMMU_INT_MASK) {
776 /* Enable EVT and PPR and GA interrupts again */
777 writel(AMD_IOMMU_INT_MASK,
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500778 iommu->mmio_base + MMIO_STATUS_OFFSET);
779
780 if (status & MMIO_STATUS_EVT_INT_MASK) {
781 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
782 iommu_poll_events(iommu);
783 }
784
785 if (status & MMIO_STATUS_PPR_INT_MASK) {
786 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
787 iommu_poll_ppr_log(iommu);
788 }
789
Suravee Suthikulpanitbd6fcef2016-08-23 13:52:37 -0500790#ifdef CONFIG_IRQ_REMAP
791 if (status & MMIO_STATUS_GALOG_INT_MASK) {
792 pr_devel("AMD-Vi: Processing IOMMU GA Log\n");
793 iommu_poll_ga_log(iommu);
794 }
795#endif
796
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500797 /*
798 * Hardware bug: ERBT1312
799 * When re-enabling interrupt (by writing 1
800 * to clear the bit), the hardware might also try to set
801 * the interrupt bit in the event status register.
802 * In this scenario, the bit will be set, and disable
803 * subsequent interrupts.
804 *
805 * Workaround: The IOMMU driver should read back the
806 * status register and check if the interrupt bits are cleared.
807 * If not, driver will need to go through the interrupt handler
808 * again and re-clear the bits
809 */
810 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100811 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200812 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200813}
814
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200815irqreturn_t amd_iommu_int_handler(int irq, void *data)
816{
817 return IRQ_WAKE_THREAD;
818}
819
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200820/****************************************************************************
821 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200822 * IOMMU command queuing functions
823 *
824 ****************************************************************************/
825
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200826static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200827{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200828 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200829
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200830 while (*sem == 0 && i < LOOP_TIMEOUT) {
831 udelay(1);
832 i += 1;
833 }
834
835 if (i == LOOP_TIMEOUT) {
836 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
837 return -EIO;
838 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200839
840 return 0;
841}
842
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200843static void copy_cmd_to_buffer(struct amd_iommu *iommu,
844 struct iommu_cmd *cmd,
845 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200846{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200847 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200848
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200849 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200850 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200851
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200852 /* Copy command to buffer */
853 memcpy(target, cmd, sizeof(*cmd));
854
855 /* Tell the IOMMU about it */
856 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
857}
858
Joerg Roedel815b33f2011-04-06 17:26:49 +0200859static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200860{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200861 WARN_ON(address & 0x7ULL);
862
Joerg Roedelded46732011-04-06 10:53:48 +0200863 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200864 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
865 cmd->data[1] = upper_32_bits(__pa(address));
866 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200867 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
868}
869
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200870static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
871{
872 memset(cmd, 0, sizeof(*cmd));
873 cmd->data[0] = devid;
874 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
875}
876
Joerg Roedel11b64022011-04-06 11:49:28 +0200877static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
878 size_t size, u16 domid, int pde)
879{
880 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100881 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200882
883 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100884 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200885
886 if (pages > 1) {
887 /*
888 * If we have to flush more than one page, flush all
889 * TLB entries for this domain
890 */
891 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100892 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200893 }
894
895 address &= PAGE_MASK;
896
897 memset(cmd, 0, sizeof(*cmd));
898 cmd->data[1] |= domid;
899 cmd->data[2] = lower_32_bits(address);
900 cmd->data[3] = upper_32_bits(address);
901 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
902 if (s) /* size bit - we flush more than one 4kb page */
903 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200904 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200905 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
906}
907
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200908static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
909 u64 address, size_t size)
910{
911 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100912 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200913
914 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100915 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200916
917 if (pages > 1) {
918 /*
919 * If we have to flush more than one page, flush all
920 * TLB entries for this domain
921 */
922 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100923 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200924 }
925
926 address &= PAGE_MASK;
927
928 memset(cmd, 0, sizeof(*cmd));
929 cmd->data[0] = devid;
930 cmd->data[0] |= (qdep & 0xff) << 24;
931 cmd->data[1] = devid;
932 cmd->data[2] = lower_32_bits(address);
933 cmd->data[3] = upper_32_bits(address);
934 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
935 if (s)
936 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
937}
938
Joerg Roedel22e266c2011-11-21 15:59:08 +0100939static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
940 u64 address, bool size)
941{
942 memset(cmd, 0, sizeof(*cmd));
943
944 address &= ~(0xfffULL);
945
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600946 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100947 cmd->data[1] = domid;
948 cmd->data[2] = lower_32_bits(address);
949 cmd->data[3] = upper_32_bits(address);
950 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
951 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
952 if (size)
953 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
954 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
955}
956
957static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
958 int qdep, u64 address, bool size)
959{
960 memset(cmd, 0, sizeof(*cmd));
961
962 address &= ~(0xfffULL);
963
964 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600965 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100966 cmd->data[0] |= (qdep & 0xff) << 24;
967 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600968 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100969 cmd->data[2] = lower_32_bits(address);
970 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
971 cmd->data[3] = upper_32_bits(address);
972 if (size)
973 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
974 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
975}
976
Joerg Roedelc99afa22011-11-21 18:19:25 +0100977static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
978 int status, int tag, bool gn)
979{
980 memset(cmd, 0, sizeof(*cmd));
981
982 cmd->data[0] = devid;
983 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600984 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100985 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
986 }
987 cmd->data[3] = tag & 0x1ff;
988 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
989
990 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
991}
992
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200993static void build_inv_all(struct iommu_cmd *cmd)
994{
995 memset(cmd, 0, sizeof(*cmd));
996 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200997}
998
Joerg Roedel7ef27982012-06-21 16:46:04 +0200999static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
1000{
1001 memset(cmd, 0, sizeof(*cmd));
1002 cmd->data[0] = devid;
1003 CMD_SET_TYPE(cmd, CMD_INV_IRT);
1004}
1005
Joerg Roedel431b2a22008-07-11 17:14:22 +02001006/*
Joerg Roedelb6c02712008-06-26 21:27:53 +02001007 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001008 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001009 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001010static int iommu_queue_command_sync(struct amd_iommu *iommu,
1011 struct iommu_cmd *cmd,
1012 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001013{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001014 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +02001015 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001016
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001017again:
Joerg Roedel815b33f2011-04-06 17:26:49 +02001018 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001019
1020 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
1021 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +02001022 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
1023 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001024
1025 if (left <= 2) {
1026 struct iommu_cmd sync_cmd;
1027 volatile u64 sem = 0;
1028 int ret;
1029
1030 build_completion_wait(&sync_cmd, (u64)&sem);
1031 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
1032
1033 spin_unlock_irqrestore(&iommu->lock, flags);
1034
1035 if ((ret = wait_on_sem(&sem)) != 0)
1036 return ret;
1037
1038 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +02001039 }
1040
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001041 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +02001042
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001043 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001044 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001045
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001046 spin_unlock_irqrestore(&iommu->lock, flags);
1047
Joerg Roedel815b33f2011-04-06 17:26:49 +02001048 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001049}
1050
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001051static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
1052{
1053 return iommu_queue_command_sync(iommu, cmd, true);
1054}
1055
Joerg Roedel8d201962008-12-02 20:34:41 +01001056/*
1057 * This function queues a completion wait command into the command
1058 * buffer of an IOMMU
1059 */
Joerg Roedel8d201962008-12-02 20:34:41 +01001060static int iommu_completion_wait(struct amd_iommu *iommu)
1061{
Joerg Roedel815b33f2011-04-06 17:26:49 +02001062 struct iommu_cmd cmd;
1063 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001064 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001065
1066 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001067 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +01001068
Joerg Roedel815b33f2011-04-06 17:26:49 +02001069 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +01001070
Joerg Roedelf1ca1512011-09-02 14:10:32 +02001071 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +01001072 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +02001073 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +01001074
Joerg Roedelac0ea6e2011-04-06 18:38:20 +02001075 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001076}
1077
Joerg Roedeld8c13082011-04-06 18:51:26 +02001078static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001079{
1080 struct iommu_cmd cmd;
1081
Joerg Roedeld8c13082011-04-06 18:51:26 +02001082 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001083
Joerg Roedeld8c13082011-04-06 18:51:26 +02001084 return iommu_queue_command(iommu, &cmd);
1085}
1086
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001087static void iommu_flush_dte_all(struct amd_iommu *iommu)
1088{
1089 u32 devid;
1090
1091 for (devid = 0; devid <= 0xffff; ++devid)
1092 iommu_flush_dte(iommu, devid);
1093
1094 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001095}
1096
1097/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001098 * This function uses heavy locking and may disable irqs for some time. But
1099 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001100 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001101static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001102{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001103 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001104
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001105 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1106 struct iommu_cmd cmd;
1107 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1108 dom_id, 1);
1109 iommu_queue_command(iommu, &cmd);
1110 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001111
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001112 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001113}
1114
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001115static void iommu_flush_all(struct amd_iommu *iommu)
1116{
1117 struct iommu_cmd cmd;
1118
1119 build_inv_all(&cmd);
1120
1121 iommu_queue_command(iommu, &cmd);
1122 iommu_completion_wait(iommu);
1123}
1124
Joerg Roedel7ef27982012-06-21 16:46:04 +02001125static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1126{
1127 struct iommu_cmd cmd;
1128
1129 build_inv_irt(&cmd, devid);
1130
1131 iommu_queue_command(iommu, &cmd);
1132}
1133
1134static void iommu_flush_irt_all(struct amd_iommu *iommu)
1135{
1136 u32 devid;
1137
1138 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1139 iommu_flush_irt(iommu, devid);
1140
1141 iommu_completion_wait(iommu);
1142}
1143
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001144void iommu_flush_all_caches(struct amd_iommu *iommu)
1145{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001146 if (iommu_feature(iommu, FEATURE_IA)) {
1147 iommu_flush_all(iommu);
1148 } else {
1149 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001150 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001151 iommu_flush_tlb_all(iommu);
1152 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001153}
1154
Joerg Roedel431b2a22008-07-11 17:14:22 +02001155/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001156 * Command send function for flushing on-device TLB
1157 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001158static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1159 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001160{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001161 struct amd_iommu *iommu;
1162 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001163 int qdep;
1164
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001165 qdep = dev_data->ats.qdep;
1166 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001167
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001168 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001169
1170 return iommu_queue_command(iommu, &cmd);
1171}
1172
1173/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001174 * Command send function for invalidating a device table entry
1175 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001176static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001177{
1178 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001179 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001180 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001181
Joerg Roedel6c542042011-06-09 17:07:31 +02001182 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001183 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001184
Joerg Roedelf62dda62011-06-09 12:55:35 +02001185 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001186 if (!ret && alias != dev_data->devid)
1187 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001188 if (ret)
1189 return ret;
1190
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001191 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001192 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001193
1194 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001195}
1196
Joerg Roedel431b2a22008-07-11 17:14:22 +02001197/*
1198 * TLB invalidation function which is called from the mapping functions.
1199 * It invalidates a single PTE if the range to flush is within a single
1200 * page. Otherwise it flushes the whole TLB of the IOMMU.
1201 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001202static void __domain_flush_pages(struct protection_domain *domain,
1203 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001204{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001205 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001206 struct iommu_cmd cmd;
1207 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001208
Joerg Roedel11b64022011-04-06 11:49:28 +02001209 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001210
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001211 for (i = 0; i < amd_iommus_present; ++i) {
1212 if (!domain->dev_iommu[i])
1213 continue;
1214
1215 /*
1216 * Devices of this domain are behind this IOMMU
1217 * We need a TLB flush
1218 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001219 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001220 }
1221
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001222 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001223
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001224 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001225 continue;
1226
Joerg Roedel6c542042011-06-09 17:07:31 +02001227 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001228 }
1229
Joerg Roedel11b64022011-04-06 11:49:28 +02001230 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001231}
1232
Joerg Roedel17b124b2011-04-06 18:01:35 +02001233static void domain_flush_pages(struct protection_domain *domain,
1234 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001235{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001236 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001237}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001238
Joerg Roedel1c655772008-09-04 18:40:05 +02001239/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001240static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001241{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001242 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001243}
1244
Chris Wright42a49f92009-06-15 15:42:00 +02001245/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001246static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001247{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001248 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1249}
1250
1251static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001252{
1253 int i;
1254
1255 for (i = 0; i < amd_iommus_present; ++i) {
Joerg Roedelf1eae7c2016-07-06 12:50:35 +02001256 if (domain && !domain->dev_iommu[i])
Joerg Roedelb6c02712008-06-26 21:27:53 +02001257 continue;
1258
1259 /*
1260 * Devices of this domain are behind this IOMMU
1261 * We need to wait for completion of all commands.
1262 */
1263 iommu_completion_wait(amd_iommus[i]);
1264 }
1265}
1266
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001267
Joerg Roedel43f49602008-12-02 21:01:12 +01001268/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001269 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001270 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001271static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001272{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001273 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001274
1275 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001276 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001277}
1278
Joerg Roedel431b2a22008-07-11 17:14:22 +02001279/****************************************************************************
1280 *
1281 * The functions below are used the create the page table mappings for
1282 * unity mapped regions.
1283 *
1284 ****************************************************************************/
1285
1286/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001287 * This function is used to add another level to an IO page table. Adding
1288 * another level increases the size of the address space by 9 bits to a size up
1289 * to 64 bits.
1290 */
1291static bool increase_address_space(struct protection_domain *domain,
1292 gfp_t gfp)
1293{
1294 u64 *pte;
1295
1296 if (domain->mode == PAGE_MODE_6_LEVEL)
1297 /* address space already 64 bit large */
1298 return false;
1299
1300 pte = (void *)get_zeroed_page(gfp);
1301 if (!pte)
1302 return false;
1303
1304 *pte = PM_LEVEL_PDE(domain->mode,
1305 virt_to_phys(domain->pt_root));
1306 domain->pt_root = pte;
1307 domain->mode += 1;
1308 domain->updated = true;
1309
1310 return true;
1311}
1312
1313static u64 *alloc_pte(struct protection_domain *domain,
1314 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001315 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001316 u64 **pte_page,
1317 gfp_t gfp)
1318{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001319 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001320 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001321
1322 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001323
1324 while (address > PM_LEVEL_SIZE(domain->mode))
1325 increase_address_space(domain, gfp);
1326
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001327 level = domain->mode - 1;
1328 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1329 address = PAGE_SIZE_ALIGN(address, page_size);
1330 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001331
1332 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001333 u64 __pte, __npte;
1334
1335 __pte = *pte;
1336
1337 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001338 page = (u64 *)get_zeroed_page(gfp);
1339 if (!page)
1340 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001341
1342 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1343
1344 if (cmpxchg64(pte, __pte, __npte)) {
1345 free_page((unsigned long)page);
1346 continue;
1347 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001348 }
1349
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001350 /* No level skipping support yet */
1351 if (PM_PTE_LEVEL(*pte) != level)
1352 return NULL;
1353
Joerg Roedel308973d2009-11-24 17:43:32 +01001354 level -= 1;
1355
1356 pte = IOMMU_PTE_PAGE(*pte);
1357
1358 if (pte_page && level == end_lvl)
1359 *pte_page = pte;
1360
1361 pte = &pte[PM_LEVEL_INDEX(level, address)];
1362 }
1363
1364 return pte;
1365}
1366
1367/*
1368 * This function checks if there is a PTE for a given dma address. If
1369 * there is one, it returns the pointer to it.
1370 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001371static u64 *fetch_pte(struct protection_domain *domain,
1372 unsigned long address,
1373 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001374{
1375 int level;
1376 u64 *pte;
1377
Joerg Roedel24cd7722010-01-19 17:27:39 +01001378 if (address > PM_LEVEL_SIZE(domain->mode))
1379 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001380
Joerg Roedel3039ca12015-04-01 14:58:48 +02001381 level = domain->mode - 1;
1382 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1383 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001384
1385 while (level > 0) {
1386
1387 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001388 if (!IOMMU_PTE_PRESENT(*pte))
1389 return NULL;
1390
Joerg Roedel24cd7722010-01-19 17:27:39 +01001391 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001392 if (PM_PTE_LEVEL(*pte) == 7 ||
1393 PM_PTE_LEVEL(*pte) == 0)
1394 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001395
1396 /* No level skipping support yet */
1397 if (PM_PTE_LEVEL(*pte) != level)
1398 return NULL;
1399
Joerg Roedel308973d2009-11-24 17:43:32 +01001400 level -= 1;
1401
Joerg Roedel24cd7722010-01-19 17:27:39 +01001402 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001403 pte = IOMMU_PTE_PAGE(*pte);
1404 pte = &pte[PM_LEVEL_INDEX(level, address)];
1405 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1406 }
1407
1408 if (PM_PTE_LEVEL(*pte) == 0x07) {
1409 unsigned long pte_mask;
1410
1411 /*
1412 * If we have a series of large PTEs, make
1413 * sure to return a pointer to the first one.
1414 */
1415 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1416 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1417 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001418 }
1419
1420 return pte;
1421}
1422
1423/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001424 * Generic mapping functions. It maps a physical address into a DMA
1425 * address space. It allocates the page table pages if necessary.
1426 * In the future it can be extended to a generic mapping function
1427 * supporting all features of AMD IOMMU page tables like level skipping
1428 * and full 64 bit address spaces.
1429 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001430static int iommu_map_page(struct protection_domain *dom,
1431 unsigned long bus_addr,
1432 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001433 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001434 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001435 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001436{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001437 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001438 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001439
Joerg Roedeld4b03662015-04-01 14:58:52 +02001440 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1441 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1442
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001443 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001444 return -EINVAL;
1445
Joerg Roedeld4b03662015-04-01 14:58:52 +02001446 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001447 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001448
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001449 if (!pte)
1450 return -ENOMEM;
1451
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001452 for (i = 0; i < count; ++i)
1453 if (IOMMU_PTE_PRESENT(pte[i]))
1454 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001455
Joerg Roedeld4b03662015-04-01 14:58:52 +02001456 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001457 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1458 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1459 } else
1460 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1461
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001462 if (prot & IOMMU_PROT_IR)
1463 __pte |= IOMMU_PTE_IR;
1464 if (prot & IOMMU_PROT_IW)
1465 __pte |= IOMMU_PTE_IW;
1466
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001467 for (i = 0; i < count; ++i)
1468 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001469
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001470 update_domain(dom);
1471
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001472 return 0;
1473}
1474
Joerg Roedel24cd7722010-01-19 17:27:39 +01001475static unsigned long iommu_unmap_page(struct protection_domain *dom,
1476 unsigned long bus_addr,
1477 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001478{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001479 unsigned long long unmapped;
1480 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001481 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001482
Joerg Roedel24cd7722010-01-19 17:27:39 +01001483 BUG_ON(!is_power_of_2(page_size));
1484
1485 unmapped = 0;
1486
1487 while (unmapped < page_size) {
1488
Joerg Roedel71b390e2015-04-01 14:58:49 +02001489 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001490
Joerg Roedel71b390e2015-04-01 14:58:49 +02001491 if (pte) {
1492 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001493
Joerg Roedel71b390e2015-04-01 14:58:49 +02001494 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001495 for (i = 0; i < count; i++)
1496 pte[i] = 0ULL;
1497 }
1498
1499 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1500 unmapped += unmap_size;
1501 }
1502
Alex Williamson60d0ca32013-06-21 14:33:19 -06001503 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001504
1505 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001506}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001507
Joerg Roedel431b2a22008-07-11 17:14:22 +02001508/****************************************************************************
1509 *
1510 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001511 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001512 *
1513 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001514
Joerg Roedel9cabe892009-05-18 16:38:55 +02001515
Joerg Roedel256e4622016-07-05 14:23:01 +02001516static unsigned long dma_ops_alloc_iova(struct device *dev,
1517 struct dma_ops_domain *dma_dom,
1518 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001519{
Joerg Roedel256e4622016-07-05 14:23:01 +02001520 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001521
Joerg Roedel256e4622016-07-05 14:23:01 +02001522 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001523
Joerg Roedel256e4622016-07-05 14:23:01 +02001524 if (dma_mask > DMA_BIT_MASK(32))
1525 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1526 IOVA_PFN(DMA_BIT_MASK(32)));
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001527
Joerg Roedel256e4622016-07-05 14:23:01 +02001528 if (!pfn)
1529 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001530
Joerg Roedel256e4622016-07-05 14:23:01 +02001531 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001532}
1533
Joerg Roedel256e4622016-07-05 14:23:01 +02001534static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1535 unsigned long address,
1536 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001537{
Joerg Roedel256e4622016-07-05 14:23:01 +02001538 pages = __roundup_pow_of_two(pages);
1539 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001540
Joerg Roedel256e4622016-07-05 14:23:01 +02001541 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001542}
1543
Joerg Roedel431b2a22008-07-11 17:14:22 +02001544/****************************************************************************
1545 *
1546 * The next functions belong to the domain allocation. A domain is
1547 * allocated for every IOMMU as the default domain. If device isolation
1548 * is enabled, every device get its own domain. The most important thing
1549 * about domains is the page table mapping the DMA address space they
1550 * contain.
1551 *
1552 ****************************************************************************/
1553
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001554/*
1555 * This function adds a protection domain to the global protection domain list
1556 */
1557static void add_domain_to_list(struct protection_domain *domain)
1558{
1559 unsigned long flags;
1560
1561 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1562 list_add(&domain->list, &amd_iommu_pd_list);
1563 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1564}
1565
1566/*
1567 * This function removes a protection domain to the global
1568 * protection domain list
1569 */
1570static void del_domain_from_list(struct protection_domain *domain)
1571{
1572 unsigned long flags;
1573
1574 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1575 list_del(&domain->list);
1576 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1577}
1578
Joerg Roedelec487d12008-06-26 21:27:58 +02001579static u16 domain_id_alloc(void)
1580{
1581 unsigned long flags;
1582 int id;
1583
1584 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1585 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1586 BUG_ON(id == 0);
1587 if (id > 0 && id < MAX_DOMAIN_ID)
1588 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1589 else
1590 id = 0;
1591 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1592
1593 return id;
1594}
1595
Joerg Roedela2acfb72008-12-02 18:28:53 +01001596static void domain_id_free(int id)
1597{
1598 unsigned long flags;
1599
1600 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1601 if (id > 0 && id < MAX_DOMAIN_ID)
1602 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1603 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1604}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001605
Joerg Roedel5c34c402013-06-20 20:22:58 +02001606#define DEFINE_FREE_PT_FN(LVL, FN) \
1607static void free_pt_##LVL (unsigned long __pt) \
1608{ \
1609 unsigned long p; \
1610 u64 *pt; \
1611 int i; \
1612 \
1613 pt = (u64 *)__pt; \
1614 \
1615 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001616 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001617 if (!IOMMU_PTE_PRESENT(pt[i])) \
1618 continue; \
1619 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001620 /* Large PTE? */ \
1621 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1622 PM_PTE_LEVEL(pt[i]) == 7) \
1623 continue; \
1624 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001625 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1626 FN(p); \
1627 } \
1628 free_page((unsigned long)pt); \
1629}
1630
1631DEFINE_FREE_PT_FN(l2, free_page)
1632DEFINE_FREE_PT_FN(l3, free_pt_l2)
1633DEFINE_FREE_PT_FN(l4, free_pt_l3)
1634DEFINE_FREE_PT_FN(l5, free_pt_l4)
1635DEFINE_FREE_PT_FN(l6, free_pt_l5)
1636
Joerg Roedel86db2e52008-12-02 18:20:21 +01001637static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001638{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001639 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001640
Joerg Roedel5c34c402013-06-20 20:22:58 +02001641 switch (domain->mode) {
1642 case PAGE_MODE_NONE:
1643 break;
1644 case PAGE_MODE_1_LEVEL:
1645 free_page(root);
1646 break;
1647 case PAGE_MODE_2_LEVEL:
1648 free_pt_l2(root);
1649 break;
1650 case PAGE_MODE_3_LEVEL:
1651 free_pt_l3(root);
1652 break;
1653 case PAGE_MODE_4_LEVEL:
1654 free_pt_l4(root);
1655 break;
1656 case PAGE_MODE_5_LEVEL:
1657 free_pt_l5(root);
1658 break;
1659 case PAGE_MODE_6_LEVEL:
1660 free_pt_l6(root);
1661 break;
1662 default:
1663 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001664 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001665}
1666
Joerg Roedelb16137b2011-11-21 16:50:23 +01001667static void free_gcr3_tbl_level1(u64 *tbl)
1668{
1669 u64 *ptr;
1670 int i;
1671
1672 for (i = 0; i < 512; ++i) {
1673 if (!(tbl[i] & GCR3_VALID))
1674 continue;
1675
1676 ptr = __va(tbl[i] & PAGE_MASK);
1677
1678 free_page((unsigned long)ptr);
1679 }
1680}
1681
1682static void free_gcr3_tbl_level2(u64 *tbl)
1683{
1684 u64 *ptr;
1685 int i;
1686
1687 for (i = 0; i < 512; ++i) {
1688 if (!(tbl[i] & GCR3_VALID))
1689 continue;
1690
1691 ptr = __va(tbl[i] & PAGE_MASK);
1692
1693 free_gcr3_tbl_level1(ptr);
1694 }
1695}
1696
Joerg Roedel52815b72011-11-17 17:24:28 +01001697static void free_gcr3_table(struct protection_domain *domain)
1698{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001699 if (domain->glx == 2)
1700 free_gcr3_tbl_level2(domain->gcr3_tbl);
1701 else if (domain->glx == 1)
1702 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001703 else
1704 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001705
Joerg Roedel52815b72011-11-17 17:24:28 +01001706 free_page((unsigned long)domain->gcr3_tbl);
1707}
1708
Joerg Roedel431b2a22008-07-11 17:14:22 +02001709/*
1710 * Free a domain, only used if something went wrong in the
1711 * allocation path and we need to free an already allocated page table
1712 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001713static void dma_ops_domain_free(struct dma_ops_domain *dom)
1714{
1715 if (!dom)
1716 return;
1717
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001718 del_domain_from_list(&dom->domain);
1719
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001720 put_iova_domain(&dom->iovad);
1721
Joerg Roedel86db2e52008-12-02 18:20:21 +01001722 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001723
Joerg Roedelec487d12008-06-26 21:27:58 +02001724 kfree(dom);
1725}
1726
Joerg Roedel431b2a22008-07-11 17:14:22 +02001727/*
1728 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001729 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001730 * structures required for the dma_ops interface
1731 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001732static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001733{
1734 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001735
1736 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1737 if (!dma_dom)
1738 return NULL;
1739
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001740 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001741 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001742
Joerg Roedelffec2192016-07-26 15:31:23 +02001743 dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001744 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001745 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001746 if (!dma_dom->domain.pt_root)
1747 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001748
Joerg Roedel307d5852016-07-05 11:54:04 +02001749 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1750 IOVA_START_PFN, DMA_32BIT_PFN);
1751
Joerg Roedel81cd07b2016-07-07 18:01:10 +02001752 /* Initialize reserved ranges */
1753 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1754
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001755 add_domain_to_list(&dma_dom->domain);
1756
Joerg Roedelec487d12008-06-26 21:27:58 +02001757 return dma_dom;
1758
1759free_dma_dom:
1760 dma_ops_domain_free(dma_dom);
1761
1762 return NULL;
1763}
1764
Joerg Roedel431b2a22008-07-11 17:14:22 +02001765/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001766 * little helper function to check whether a given protection domain is a
1767 * dma_ops domain
1768 */
1769static bool dma_ops_domain(struct protection_domain *domain)
1770{
1771 return domain->flags & PD_DMA_OPS_MASK;
1772}
1773
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001774static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001775{
Joerg Roedel132bd682011-11-17 14:18:46 +01001776 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001777 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001778
Joerg Roedel132bd682011-11-17 14:18:46 +01001779 if (domain->mode != PAGE_MODE_NONE)
1780 pte_root = virt_to_phys(domain->pt_root);
1781
Joerg Roedel38ddf412008-09-11 10:38:32 +02001782 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1783 << DEV_ENTRY_MODE_SHIFT;
1784 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001785
Joerg Roedelee6c2862011-11-09 12:06:03 +01001786 flags = amd_iommu_dev_table[devid].data[1];
1787
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001788 if (ats)
1789 flags |= DTE_FLAG_IOTLB;
1790
Joerg Roedel52815b72011-11-17 17:24:28 +01001791 if (domain->flags & PD_IOMMUV2_MASK) {
1792 u64 gcr3 = __pa(domain->gcr3_tbl);
1793 u64 glx = domain->glx;
1794 u64 tmp;
1795
1796 pte_root |= DTE_FLAG_GV;
1797 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1798
1799 /* First mask out possible old values for GCR3 table */
1800 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1801 flags &= ~tmp;
1802
1803 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1804 flags &= ~tmp;
1805
1806 /* Encode GCR3 table into DTE */
1807 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1808 pte_root |= tmp;
1809
1810 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1811 flags |= tmp;
1812
1813 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1814 flags |= tmp;
1815 }
1816
Joerg Roedelee6c2862011-11-09 12:06:03 +01001817 flags &= ~(0xffffUL);
1818 flags |= domain->id;
1819
1820 amd_iommu_dev_table[devid].data[1] = flags;
1821 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001822}
1823
Joerg Roedel15898bb2009-11-24 15:39:42 +01001824static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001825{
Joerg Roedel355bf552008-12-08 12:02:41 +01001826 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001827 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1828 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001829
Joerg Roedelc5cca142009-10-09 18:31:20 +02001830 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001831}
1832
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001833static void do_attach(struct iommu_dev_data *dev_data,
1834 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001835{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001836 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001837 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001838 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001839
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001840 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001841 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001842 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001843
1844 /* Update data structures */
1845 dev_data->domain = domain;
1846 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001847
1848 /* Do reference counting */
1849 domain->dev_iommu[iommu->index] += 1;
1850 domain->dev_cnt += 1;
1851
Joerg Roedele25bfb52015-10-20 17:33:38 +02001852 /* Update device table */
1853 set_dte_entry(dev_data->devid, domain, ats);
1854 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08001855 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001856
Joerg Roedel6c542042011-06-09 17:07:31 +02001857 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001858}
1859
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001860static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001861{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001862 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001863 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001864
Joerg Roedel5adad992015-10-09 16:23:33 +02001865 /*
1866 * First check if the device is still attached. It might already
1867 * be detached from its domain because the generic
1868 * iommu_detach_group code detached it and we try again here in
1869 * our alias handling.
1870 */
1871 if (!dev_data->domain)
1872 return;
1873
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001874 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001875 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02001876
Joerg Roedelc4596112009-11-20 14:57:32 +01001877 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001878 dev_data->domain->dev_iommu[iommu->index] -= 1;
1879 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001880
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001881 /* Update data structures */
1882 dev_data->domain = NULL;
1883 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001884 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001885 if (alias != dev_data->devid)
1886 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001887
1888 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001889 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001890}
1891
1892/*
1893 * If a device is not yet associated with a domain, this function does
1894 * assigns it visible for the hardware
1895 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001896static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001897 struct protection_domain *domain)
1898{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001899 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001900
Joerg Roedel272e4f92015-10-20 17:33:37 +02001901 /*
1902 * Must be called with IRQs disabled. Warn here to detect early
1903 * when its not.
1904 */
1905 WARN_ON(!irqs_disabled());
1906
Joerg Roedel15898bb2009-11-24 15:39:42 +01001907 /* lock domain */
1908 spin_lock(&domain->lock);
1909
Joerg Roedel397111a2014-08-05 17:31:51 +02001910 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02001911 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02001912 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01001913
Joerg Roedel397111a2014-08-05 17:31:51 +02001914 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02001915 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001916
Julia Lawall84fe6c12010-05-27 12:31:51 +02001917 ret = 0;
1918
1919out_unlock:
1920
Joerg Roedel355bf552008-12-08 12:02:41 +01001921 /* ready */
1922 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001923
Julia Lawall84fe6c12010-05-27 12:31:51 +02001924 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001925}
1926
Joerg Roedel52815b72011-11-17 17:24:28 +01001927
1928static void pdev_iommuv2_disable(struct pci_dev *pdev)
1929{
1930 pci_disable_ats(pdev);
1931 pci_disable_pri(pdev);
1932 pci_disable_pasid(pdev);
1933}
1934
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001935/* FIXME: Change generic reset-function to do the same */
1936static int pri_reset_while_enabled(struct pci_dev *pdev)
1937{
1938 u16 control;
1939 int pos;
1940
Joerg Roedel46277b72011-12-07 14:34:02 +01001941 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001942 if (!pos)
1943 return -EINVAL;
1944
Joerg Roedel46277b72011-12-07 14:34:02 +01001945 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1946 control |= PCI_PRI_CTRL_RESET;
1947 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001948
1949 return 0;
1950}
1951
Joerg Roedel52815b72011-11-17 17:24:28 +01001952static int pdev_iommuv2_enable(struct pci_dev *pdev)
1953{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001954 bool reset_enable;
1955 int reqs, ret;
1956
1957 /* FIXME: Hardcode number of outstanding requests for now */
1958 reqs = 32;
1959 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1960 reqs = 1;
1961 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01001962
1963 /* Only allow access to user-accessible pages */
1964 ret = pci_enable_pasid(pdev, 0);
1965 if (ret)
1966 goto out_err;
1967
1968 /* First reset the PRI state of the device */
1969 ret = pci_reset_pri(pdev);
1970 if (ret)
1971 goto out_err;
1972
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001973 /* Enable PRI */
1974 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01001975 if (ret)
1976 goto out_err;
1977
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001978 if (reset_enable) {
1979 ret = pri_reset_while_enabled(pdev);
1980 if (ret)
1981 goto out_err;
1982 }
1983
Joerg Roedel52815b72011-11-17 17:24:28 +01001984 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1985 if (ret)
1986 goto out_err;
1987
1988 return 0;
1989
1990out_err:
1991 pci_disable_pri(pdev);
1992 pci_disable_pasid(pdev);
1993
1994 return ret;
1995}
1996
Joerg Roedelc99afa22011-11-21 18:19:25 +01001997/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02001998#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001999
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002000static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01002001{
Joerg Roedela3b93122012-04-12 12:49:26 +02002002 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002003 int pos;
2004
Joerg Roedel46277b72011-12-07 14:34:02 +01002005 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002006 if (!pos)
2007 return false;
2008
Joerg Roedela3b93122012-04-12 12:49:26 +02002009 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01002010
Joerg Roedela3b93122012-04-12 12:49:26 +02002011 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01002012}
2013
Joerg Roedel15898bb2009-11-24 15:39:42 +01002014/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02002015 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01002016 * assigns it visible for the hardware
2017 */
2018static int attach_device(struct device *dev,
2019 struct protection_domain *domain)
2020{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002021 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002022 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002023 unsigned long flags;
2024 int ret;
2025
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002026 dev_data = get_dev_data(dev);
2027
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002028 if (!dev_is_pci(dev))
2029 goto skip_ats_check;
2030
2031 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002032 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02002033 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01002034 return -EINVAL;
2035
Joerg Roedel02ca2022015-07-28 16:58:49 +02002036 if (dev_data->iommu_v2) {
2037 if (pdev_iommuv2_enable(pdev) != 0)
2038 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01002039
Joerg Roedel02ca2022015-07-28 16:58:49 +02002040 dev_data->ats.enabled = true;
2041 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2042 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
2043 }
Joerg Roedel52815b72011-11-17 17:24:28 +01002044 } else if (amd_iommu_iotlb_sup &&
2045 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002046 dev_data->ats.enabled = true;
2047 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
2048 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002049
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002050skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01002051 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002052 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002053 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2054
2055 /*
2056 * We might boot into a crash-kernel here. The crashed kernel
2057 * left the caches in the IOMMU dirty. So we have to flush
2058 * here to evict all dirty stuff.
2059 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02002060 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01002061
2062 return ret;
2063}
2064
2065/*
2066 * Removes a device from a protection domain (unlocked)
2067 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002068static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002069{
Joerg Roedel2ca76272010-01-22 16:45:31 +01002070 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002071
Joerg Roedel272e4f92015-10-20 17:33:37 +02002072 /*
2073 * Must be called with IRQs disabled. Warn here to detect early
2074 * when its not.
2075 */
2076 WARN_ON(!irqs_disabled());
2077
Joerg Roedelf34c73f2015-10-20 17:33:34 +02002078 if (WARN_ON(!dev_data->domain))
2079 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002080
Joerg Roedel2ca76272010-01-22 16:45:31 +01002081 domain = dev_data->domain;
2082
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002083 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01002084
Joerg Roedel150952f2015-10-20 17:33:35 +02002085 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02002086
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02002087 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01002088}
2089
2090/*
2091 * Removes a device from a protection domain (with devtable_lock held)
2092 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002093static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002094{
Joerg Roedel52815b72011-11-17 17:24:28 +01002095 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002096 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002097 unsigned long flags;
2098
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002099 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002100 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002101
Joerg Roedel355bf552008-12-08 12:02:41 +01002102 /* lock device table */
2103 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002104 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002105 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002106
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002107 if (!dev_is_pci(dev))
2108 return;
2109
Joerg Roedel02ca2022015-07-28 16:58:49 +02002110 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002111 pdev_iommuv2_disable(to_pci_dev(dev));
2112 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002113 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002114
2115 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002116}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002117
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002118static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002119{
Joerg Roedel71f77582011-06-09 19:03:15 +02002120 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002121 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002122 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002123 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002124
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002125 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002126 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002127
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002128 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002129 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002130 return devid;
2131
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002132 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002133
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002134 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002135 if (ret) {
2136 if (ret != -ENOTSUPP)
2137 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2138 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002139
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002140 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002141 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002142 goto out;
2143 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002144 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002145
Joerg Roedel07ee8692015-05-28 18:41:42 +02002146 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002147
2148 BUG_ON(!dev_data);
2149
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002150 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002151 iommu_request_dm_for_dev(dev);
2152
2153 /* Domains are initialized for this device - have a look what we ended up with */
2154 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002155 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002156 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002157 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002158 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002159
2160out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002161 iommu_completion_wait(iommu);
2162
Joerg Roedele275a2a2008-12-10 18:27:25 +01002163 return 0;
2164}
2165
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002166static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002167{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002168 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002169 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002170
2171 if (!check_device(dev))
2172 return;
2173
2174 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002175 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002176 return;
2177
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002178 iommu = amd_iommu_rlookup_table[devid];
2179
2180 iommu_uninit_device(dev);
2181 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002182}
2183
Wan Zongshunb097d112016-04-01 09:06:04 -04002184static struct iommu_group *amd_iommu_device_group(struct device *dev)
2185{
2186 if (dev_is_pci(dev))
2187 return pci_device_group(dev);
2188
2189 return acpihid_device_group(dev);
2190}
2191
Joerg Roedel431b2a22008-07-11 17:14:22 +02002192/*****************************************************************************
2193 *
2194 * The next functions belong to the dma_ops mapping/unmapping code.
2195 *
2196 *****************************************************************************/
2197
Joerg Roedelb1516a12016-07-06 13:07:22 +02002198static void __queue_flush(struct flush_queue *queue)
2199{
2200 struct protection_domain *domain;
2201 unsigned long flags;
2202 int idx;
2203
2204 /* First flush TLB of all known domains */
2205 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
2206 list_for_each_entry(domain, &amd_iommu_pd_list, list)
2207 domain_flush_tlb(domain);
2208 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
2209
2210 /* Wait until flushes have completed */
2211 domain_flush_complete(NULL);
2212
2213 for (idx = 0; idx < queue->next; ++idx) {
2214 struct flush_queue_entry *entry;
2215
2216 entry = queue->entries + idx;
2217
2218 free_iova_fast(&entry->dma_dom->iovad,
2219 entry->iova_pfn,
2220 entry->pages);
2221
2222 /* Not really necessary, just to make sure we catch any bugs */
2223 entry->dma_dom = NULL;
2224 }
2225
2226 queue->next = 0;
2227}
2228
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002229static void queue_flush_all(void)
Joerg Roedelbb279472016-07-06 13:56:36 +02002230{
2231 int cpu;
2232
Joerg Roedelbb279472016-07-06 13:56:36 +02002233 for_each_possible_cpu(cpu) {
2234 struct flush_queue *queue;
2235 unsigned long flags;
2236
2237 queue = per_cpu_ptr(&flush_queue, cpu);
2238 spin_lock_irqsave(&queue->lock, flags);
2239 if (queue->next > 0)
2240 __queue_flush(queue);
2241 spin_unlock_irqrestore(&queue->lock, flags);
2242 }
2243}
2244
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002245static void queue_flush_timeout(unsigned long unsused)
2246{
2247 atomic_set(&queue_timer_on, 0);
2248 queue_flush_all();
2249}
2250
Joerg Roedelb1516a12016-07-06 13:07:22 +02002251static void queue_add(struct dma_ops_domain *dma_dom,
2252 unsigned long address, unsigned long pages)
2253{
2254 struct flush_queue_entry *entry;
2255 struct flush_queue *queue;
2256 unsigned long flags;
2257 int idx;
2258
2259 pages = __roundup_pow_of_two(pages);
2260 address >>= PAGE_SHIFT;
2261
2262 queue = get_cpu_ptr(&flush_queue);
2263 spin_lock_irqsave(&queue->lock, flags);
2264
2265 if (queue->next == FLUSH_QUEUE_SIZE)
2266 __queue_flush(queue);
2267
2268 idx = queue->next++;
2269 entry = queue->entries + idx;
2270
2271 entry->iova_pfn = address;
2272 entry->pages = pages;
2273 entry->dma_dom = dma_dom;
2274
2275 spin_unlock_irqrestore(&queue->lock, flags);
Joerg Roedelbb279472016-07-06 13:56:36 +02002276
2277 if (atomic_cmpxchg(&queue_timer_on, 0, 1) == 0)
2278 mod_timer(&queue_timer, jiffies + msecs_to_jiffies(10));
2279
Joerg Roedelb1516a12016-07-06 13:07:22 +02002280 put_cpu_ptr(&flush_queue);
2281}
2282
2283
Joerg Roedel431b2a22008-07-11 17:14:22 +02002284/*
2285 * In the dma_ops path we only have the struct device. This function
2286 * finds the corresponding IOMMU, the protection domain and the
2287 * requestor id for a given device.
2288 * If the device is not yet associated with a domain this is also done
2289 * in this function.
2290 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002291static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002292{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002293 struct protection_domain *domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002294
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002295 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002296 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002297
Joerg Roedeld26592a2016-07-07 15:31:13 +02002298 domain = get_dev_data(dev)->domain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002299 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002300 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002301
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002302 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002303}
2304
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002305static void update_device_table(struct protection_domain *domain)
2306{
Joerg Roedel492667d2009-11-27 13:25:47 +01002307 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002308
Joerg Roedel3254de62016-07-26 15:18:54 +02002309 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002310 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel3254de62016-07-26 15:18:54 +02002311
2312 if (dev_data->devid == dev_data->alias)
2313 continue;
2314
2315 /* There is an alias, update device table entry for it */
2316 set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled);
2317 }
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002318}
2319
2320static void update_domain(struct protection_domain *domain)
2321{
2322 if (!domain->updated)
2323 return;
2324
2325 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002326
2327 domain_flush_devices(domain);
2328 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002329
2330 domain->updated = false;
2331}
2332
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002333static int dir2prot(enum dma_data_direction direction)
2334{
2335 if (direction == DMA_TO_DEVICE)
2336 return IOMMU_PROT_IR;
2337 else if (direction == DMA_FROM_DEVICE)
2338 return IOMMU_PROT_IW;
2339 else if (direction == DMA_BIDIRECTIONAL)
2340 return IOMMU_PROT_IW | IOMMU_PROT_IR;
2341 else
2342 return 0;
2343}
Joerg Roedel431b2a22008-07-11 17:14:22 +02002344/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002345 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002346 * contiguous memory region into DMA address space. It is used by all
2347 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002348 * Must be called with the domain lock held.
2349 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002350static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002351 struct dma_ops_domain *dma_dom,
2352 phys_addr_t paddr,
2353 size_t size,
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002354 enum dma_data_direction direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002355 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002356{
2357 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002358 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002359 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002360 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002361 int i;
2362
Joerg Roedele3c449f2008-10-15 22:02:11 -07002363 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002364 paddr &= PAGE_MASK;
2365
Joerg Roedel256e4622016-07-05 14:23:01 +02002366 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002367 if (address == DMA_ERROR_CODE)
2368 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002369
Joerg Roedelf37f7f32016-07-08 11:47:22 +02002370 prot = dir2prot(direction);
Joerg Roedel518d9b42016-07-05 14:39:47 +02002371
Joerg Roedelcb76c322008-06-26 21:28:00 +02002372 start = address;
2373 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002374 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2375 PAGE_SIZE, prot, GFP_ATOMIC);
2376 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002377 goto out_unmap;
2378
Joerg Roedelcb76c322008-06-26 21:28:00 +02002379 paddr += PAGE_SIZE;
2380 start += PAGE_SIZE;
2381 }
2382 address += offset;
2383
Joerg Roedelab7032b2015-12-21 18:47:11 +01002384 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002385 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002386 domain_flush_complete(&dma_dom->domain);
2387 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002388
Joerg Roedelcb76c322008-06-26 21:28:00 +02002389out:
2390 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002391
2392out_unmap:
2393
2394 for (--i; i >= 0; --i) {
2395 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002396 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002397 }
2398
Joerg Roedel256e4622016-07-05 14:23:01 +02002399 domain_flush_tlb(&dma_dom->domain);
2400 domain_flush_complete(&dma_dom->domain);
2401
2402 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002403
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002404 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002405}
2406
Joerg Roedel431b2a22008-07-11 17:14:22 +02002407/*
2408 * Does the reverse of the __map_single function. Must be called with
2409 * the domain lock held too
2410 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002411static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002412 dma_addr_t dma_addr,
2413 size_t size,
2414 int dir)
2415{
Joerg Roedel04e04632010-09-23 16:12:48 +02002416 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002417 dma_addr_t i, start;
2418 unsigned int pages;
2419
Joerg Roedel04e04632010-09-23 16:12:48 +02002420 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002421 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002422 dma_addr &= PAGE_MASK;
2423 start = dma_addr;
2424
2425 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002426 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002427 start += PAGE_SIZE;
2428 }
2429
Joerg Roedelb1516a12016-07-06 13:07:22 +02002430 if (amd_iommu_unmap_flush) {
2431 dma_ops_free_iova(dma_dom, dma_addr, pages);
2432 domain_flush_tlb(&dma_dom->domain);
2433 domain_flush_complete(&dma_dom->domain);
2434 } else {
2435 queue_add(dma_dom, dma_addr, pages);
2436 }
Joerg Roedelcb76c322008-06-26 21:28:00 +02002437}
2438
Joerg Roedel431b2a22008-07-11 17:14:22 +02002439/*
2440 * The exported map_single function for dma_ops.
2441 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002442static dma_addr_t map_page(struct device *dev, struct page *page,
2443 unsigned long offset, size_t size,
2444 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002445 unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002446{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002447 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002448 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002449 struct dma_ops_domain *dma_dom;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002450 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002451
Joerg Roedel94f6d192009-11-24 16:40:02 +01002452 domain = get_domain(dev);
2453 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002454 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002455 else if (IS_ERR(domain))
2456 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002457
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002458 dma_mask = *dev->dma_mask;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002459 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002460
Joerg Roedelb3311b02016-07-08 13:31:31 +02002461 return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002462}
2463
Joerg Roedel431b2a22008-07-11 17:14:22 +02002464/*
2465 * The exported unmap_single function for dma_ops.
2466 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002467static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002468 enum dma_data_direction dir, unsigned long attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002469{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002470 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002471 struct dma_ops_domain *dma_dom;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002472
Joerg Roedel94f6d192009-11-24 16:40:02 +01002473 domain = get_domain(dev);
2474 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002475 return;
2476
Joerg Roedelb3311b02016-07-08 13:31:31 +02002477 dma_dom = to_dma_ops_domain(domain);
2478
2479 __unmap_single(dma_dom, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002480}
2481
Joerg Roedel80187fd2016-07-06 17:20:54 +02002482static int sg_num_pages(struct device *dev,
2483 struct scatterlist *sglist,
2484 int nelems)
2485{
2486 unsigned long mask, boundary_size;
2487 struct scatterlist *s;
2488 int i, npages = 0;
2489
2490 mask = dma_get_seg_boundary(dev);
2491 boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
2492 1UL << (BITS_PER_LONG - PAGE_SHIFT);
2493
2494 for_each_sg(sglist, s, nelems, i) {
2495 int p, n;
2496
2497 s->dma_address = npages << PAGE_SHIFT;
2498 p = npages % boundary_size;
2499 n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2500 if (p + n > boundary_size)
2501 npages += boundary_size - p;
2502 npages += n;
2503 }
2504
2505 return npages;
2506}
2507
Joerg Roedel431b2a22008-07-11 17:14:22 +02002508/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002509 * The exported map_sg function for dma_ops (handles scatter-gather
2510 * lists).
2511 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002512static int map_sg(struct device *dev, struct scatterlist *sglist,
Joerg Roedel80187fd2016-07-06 17:20:54 +02002513 int nelems, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002514 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002515{
Joerg Roedel80187fd2016-07-06 17:20:54 +02002516 int mapped_pages = 0, npages = 0, prot = 0, i;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002517 struct protection_domain *domain;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002518 struct dma_ops_domain *dma_dom;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002519 struct scatterlist *s;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002520 unsigned long address;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002521 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002522
Joerg Roedel94f6d192009-11-24 16:40:02 +01002523 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002524 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002525 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002526
Joerg Roedelb3311b02016-07-08 13:31:31 +02002527 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel832a90c2008-09-18 15:54:23 +02002528 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002529
Joerg Roedel80187fd2016-07-06 17:20:54 +02002530 npages = sg_num_pages(dev, sglist, nelems);
2531
2532 address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
2533 if (address == DMA_ERROR_CODE)
2534 goto out_err;
2535
2536 prot = dir2prot(direction);
2537
2538 /* Map all sg entries */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002539 for_each_sg(sglist, s, nelems, i) {
Joerg Roedel80187fd2016-07-06 17:20:54 +02002540 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002541
Joerg Roedel80187fd2016-07-06 17:20:54 +02002542 for (j = 0; j < pages; ++j) {
2543 unsigned long bus_addr, phys_addr;
2544 int ret;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002545
Joerg Roedel80187fd2016-07-06 17:20:54 +02002546 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2547 phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
2548 ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
2549 if (ret)
2550 goto out_unmap;
2551
2552 mapped_pages += 1;
2553 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002554 }
2555
Joerg Roedel80187fd2016-07-06 17:20:54 +02002556 /* Everything is mapped - write the right values into s->dma_address */
2557 for_each_sg(sglist, s, nelems, i) {
2558 s->dma_address += address + s->offset;
2559 s->dma_length = s->length;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002560 }
2561
Joerg Roedel80187fd2016-07-06 17:20:54 +02002562 return nelems;
2563
2564out_unmap:
2565 pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
2566 dev_name(dev), npages);
2567
2568 for_each_sg(sglist, s, nelems, i) {
2569 int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
2570
2571 for (j = 0; j < pages; ++j) {
2572 unsigned long bus_addr;
2573
2574 bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
2575 iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
2576
2577 if (--mapped_pages)
2578 goto out_free_iova;
2579 }
2580 }
2581
2582out_free_iova:
2583 free_iova_fast(&dma_dom->iovad, address, npages);
2584
2585out_err:
Joerg Roedel92d420e2015-12-21 19:31:33 +01002586 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002587}
2588
Joerg Roedel431b2a22008-07-11 17:14:22 +02002589/*
2590 * The exported map_sg function for dma_ops (handles scatter-gather
2591 * lists).
2592 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002593static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002594 int nelems, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002595 unsigned long attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002596{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002597 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002598 struct dma_ops_domain *dma_dom;
Joerg Roedel80187fd2016-07-06 17:20:54 +02002599 unsigned long startaddr;
2600 int npages = 2;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002601
Joerg Roedel94f6d192009-11-24 16:40:02 +01002602 domain = get_domain(dev);
2603 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002604 return;
2605
Joerg Roedel80187fd2016-07-06 17:20:54 +02002606 startaddr = sg_dma_address(sglist) & PAGE_MASK;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002607 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel80187fd2016-07-06 17:20:54 +02002608 npages = sg_num_pages(dev, sglist, nelems);
2609
Joerg Roedelb3311b02016-07-08 13:31:31 +02002610 __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002611}
2612
Joerg Roedel431b2a22008-07-11 17:14:22 +02002613/*
2614 * The exported alloc_coherent function for dma_ops.
2615 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002616static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002617 dma_addr_t *dma_addr, gfp_t flag,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002618 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002619{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002620 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002621 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002622 struct dma_ops_domain *dma_dom;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002623 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002624
Joerg Roedel94f6d192009-11-24 16:40:02 +01002625 domain = get_domain(dev);
2626 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002627 page = alloc_pages(flag, get_order(size));
2628 *dma_addr = page_to_phys(page);
2629 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002630 } else if (IS_ERR(domain))
2631 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002632
Joerg Roedelb3311b02016-07-08 13:31:31 +02002633 dma_dom = to_dma_ops_domain(domain);
Joerg Roedel3b839a52015-04-01 14:58:47 +02002634 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002635 dma_mask = dev->coherent_dma_mask;
2636 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002637 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002638
Joerg Roedel3b839a52015-04-01 14:58:47 +02002639 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2640 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002641 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002642 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002643
Joerg Roedel3b839a52015-04-01 14:58:47 +02002644 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2645 get_order(size));
2646 if (!page)
2647 return NULL;
2648 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002649
Joerg Roedel832a90c2008-09-18 15:54:23 +02002650 if (!dma_mask)
2651 dma_mask = *dev->dma_mask;
2652
Joerg Roedelb3311b02016-07-08 13:31:31 +02002653 *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
Joerg Roedelbda350d2016-07-05 16:28:02 +02002654 size, DMA_BIDIRECTIONAL, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002655
Joerg Roedel92d420e2015-12-21 19:31:33 +01002656 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002657 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002658
Joerg Roedel3b839a52015-04-01 14:58:47 +02002659 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002660
2661out_free:
2662
Joerg Roedel3b839a52015-04-01 14:58:47 +02002663 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2664 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002665
2666 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002667}
2668
Joerg Roedel431b2a22008-07-11 17:14:22 +02002669/*
2670 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002671 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002672static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002673 void *virt_addr, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -07002674 unsigned long attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002675{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002676 struct protection_domain *domain;
Joerg Roedelb3311b02016-07-08 13:31:31 +02002677 struct dma_ops_domain *dma_dom;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002678 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002679
Joerg Roedel3b839a52015-04-01 14:58:47 +02002680 page = virt_to_page(virt_addr);
2681 size = PAGE_ALIGN(size);
2682
Joerg Roedel94f6d192009-11-24 16:40:02 +01002683 domain = get_domain(dev);
2684 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002685 goto free_mem;
2686
Joerg Roedelb3311b02016-07-08 13:31:31 +02002687 dma_dom = to_dma_ops_domain(domain);
2688
2689 __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002690
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002691free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002692 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2693 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002694}
2695
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002696/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002697 * This function is called by the DMA layer to find out if we can handle a
2698 * particular device. It is part of the dma_ops.
2699 */
2700static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2701{
Joerg Roedel420aef82009-11-23 16:14:57 +01002702 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002703}
2704
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002705static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002706 .alloc = alloc_coherent,
2707 .free = free_coherent,
2708 .map_page = map_page,
2709 .unmap_page = unmap_page,
2710 .map_sg = map_sg,
2711 .unmap_sg = unmap_sg,
2712 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002713};
2714
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002715static int init_reserved_iova_ranges(void)
2716{
2717 struct pci_dev *pdev = NULL;
2718 struct iova *val;
2719
2720 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2721 IOVA_START_PFN, DMA_32BIT_PFN);
2722
2723 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2724 &reserved_rbtree_key);
2725
2726 /* MSI memory range */
2727 val = reserve_iova(&reserved_iova_ranges,
2728 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2729 if (!val) {
2730 pr_err("Reserving MSI range failed\n");
2731 return -ENOMEM;
2732 }
2733
2734 /* HT memory range */
2735 val = reserve_iova(&reserved_iova_ranges,
2736 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2737 if (!val) {
2738 pr_err("Reserving HT range failed\n");
2739 return -ENOMEM;
2740 }
2741
2742 /*
2743 * Memory used for PCI resources
2744 * FIXME: Check whether we can reserve the PCI-hole completly
2745 */
2746 for_each_pci_dev(pdev) {
2747 int i;
2748
2749 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2750 struct resource *r = &pdev->resource[i];
2751
2752 if (!(r->flags & IORESOURCE_MEM))
2753 continue;
2754
2755 val = reserve_iova(&reserved_iova_ranges,
2756 IOVA_PFN(r->start),
2757 IOVA_PFN(r->end));
2758 if (!val) {
2759 pr_err("Reserve pci-resource range failed\n");
2760 return -ENOMEM;
2761 }
2762 }
2763 }
2764
2765 return 0;
2766}
2767
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002768int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002769{
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002770 int ret, cpu, err = 0;
Joerg Roedel307d5852016-07-05 11:54:04 +02002771
2772 ret = iova_cache_get();
2773 if (ret)
2774 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002775
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002776 ret = init_reserved_iova_ranges();
2777 if (ret)
2778 return ret;
2779
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002780 for_each_possible_cpu(cpu) {
2781 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2782
2783 queue->entries = kzalloc(FLUSH_QUEUE_SIZE *
2784 sizeof(*queue->entries),
2785 GFP_KERNEL);
2786 if (!queue->entries)
2787 goto out_put_iova;
2788
2789 spin_lock_init(&queue->lock);
2790 }
2791
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002792 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2793 if (err)
2794 return err;
2795#ifdef CONFIG_ARM_AMBA
2796 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2797 if (err)
2798 return err;
2799#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002800 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2801 if (err)
2802 return err;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002803 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002804
2805out_put_iova:
2806 for_each_possible_cpu(cpu) {
2807 struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu);
2808
2809 kfree(queue->entries);
2810 }
2811
2812 return -ENOMEM;
Joerg Roedelf5325092010-01-22 17:44:35 +01002813}
2814
Joerg Roedel6631ee92008-06-26 21:28:05 +02002815int __init amd_iommu_init_dma_ops(void)
2816{
Joerg Roedelbb279472016-07-06 13:56:36 +02002817 setup_timer(&queue_timer, queue_flush_timeout, 0);
2818 atomic_set(&queue_timer_on, 0);
2819
Joerg Roedel32302322015-07-28 16:58:50 +02002820 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002821 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002822
Joerg Roedel52717822015-07-28 16:58:51 +02002823 /*
2824 * In case we don't initialize SWIOTLB (actually the common case
2825 * when AMD IOMMU is enabled), make sure there are global
2826 * dma_ops set as a fall-back for devices not handled by this
2827 * driver (for example non-PCI devices).
2828 */
2829 if (!swiotlb)
2830 dma_ops = &nommu_dma_ops;
2831
Joerg Roedel62410ee2012-06-12 16:42:43 +02002832 if (amd_iommu_unmap_flush)
2833 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2834 else
2835 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2836
Joerg Roedel6631ee92008-06-26 21:28:05 +02002837 return 0;
Joerg Roedelc5b5da92016-07-06 11:55:37 +02002838
Joerg Roedel6631ee92008-06-26 21:28:05 +02002839}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002840
2841/*****************************************************************************
2842 *
2843 * The following functions belong to the exported interface of AMD IOMMU
2844 *
2845 * This interface allows access to lower level functions of the IOMMU
2846 * like protection domain handling and assignement of devices to domains
2847 * which is not possible with the dma_ops interface.
2848 *
2849 *****************************************************************************/
2850
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002851static void cleanup_domain(struct protection_domain *domain)
2852{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002853 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002854 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002855
2856 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2857
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002858 while (!list_empty(&domain->dev_list)) {
2859 entry = list_first_entry(&domain->dev_list,
2860 struct iommu_dev_data, list);
2861 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002862 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002863
2864 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2865}
2866
Joerg Roedel26508152009-08-26 16:52:40 +02002867static void protection_domain_free(struct protection_domain *domain)
2868{
2869 if (!domain)
2870 return;
2871
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002872 del_domain_from_list(domain);
2873
Joerg Roedel26508152009-08-26 16:52:40 +02002874 if (domain->id)
2875 domain_id_free(domain->id);
2876
2877 kfree(domain);
2878}
2879
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002880static int protection_domain_init(struct protection_domain *domain)
2881{
2882 spin_lock_init(&domain->lock);
2883 mutex_init(&domain->api_lock);
2884 domain->id = domain_id_alloc();
2885 if (!domain->id)
2886 return -ENOMEM;
2887 INIT_LIST_HEAD(&domain->dev_list);
2888
2889 return 0;
2890}
2891
Joerg Roedel26508152009-08-26 16:52:40 +02002892static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002893{
2894 struct protection_domain *domain;
2895
2896 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2897 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002898 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002899
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002900 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002901 goto out_err;
2902
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002903 add_domain_to_list(domain);
2904
Joerg Roedel26508152009-08-26 16:52:40 +02002905 return domain;
2906
2907out_err:
2908 kfree(domain);
2909
2910 return NULL;
2911}
2912
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002913static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2914{
2915 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002916 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002917
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002918 switch (type) {
2919 case IOMMU_DOMAIN_UNMANAGED:
2920 pdomain = protection_domain_alloc();
2921 if (!pdomain)
2922 return NULL;
2923
2924 pdomain->mode = PAGE_MODE_3_LEVEL;
2925 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2926 if (!pdomain->pt_root) {
2927 protection_domain_free(pdomain);
2928 return NULL;
2929 }
2930
2931 pdomain->domain.geometry.aperture_start = 0;
2932 pdomain->domain.geometry.aperture_end = ~0ULL;
2933 pdomain->domain.geometry.force_aperture = true;
2934
2935 break;
2936 case IOMMU_DOMAIN_DMA:
2937 dma_domain = dma_ops_domain_alloc();
2938 if (!dma_domain) {
2939 pr_err("AMD-Vi: Failed to allocate\n");
2940 return NULL;
2941 }
2942 pdomain = &dma_domain->domain;
2943 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002944 case IOMMU_DOMAIN_IDENTITY:
2945 pdomain = protection_domain_alloc();
2946 if (!pdomain)
2947 return NULL;
2948
2949 pdomain->mode = PAGE_MODE_NONE;
2950 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002951 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002952 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002953 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002954
2955 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002956}
2957
2958static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002959{
2960 struct protection_domain *domain;
Joerg Roedelcda70052016-07-07 15:57:04 +02002961 struct dma_ops_domain *dma_dom;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002962
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002963 domain = to_pdomain(dom);
2964
Joerg Roedel98383fc2008-12-02 18:34:12 +01002965 if (domain->dev_cnt > 0)
2966 cleanup_domain(domain);
2967
2968 BUG_ON(domain->dev_cnt != 0);
2969
Joerg Roedelcda70052016-07-07 15:57:04 +02002970 if (!dom)
2971 return;
Joerg Roedel98383fc2008-12-02 18:34:12 +01002972
Joerg Roedelcda70052016-07-07 15:57:04 +02002973 switch (dom->type) {
2974 case IOMMU_DOMAIN_DMA:
Joerg Roedel281e8cc2016-07-07 16:12:02 +02002975 /*
2976 * First make sure the domain is no longer referenced from the
2977 * flush queue
2978 */
2979 queue_flush_all();
2980
2981 /* Now release the domain */
Joerg Roedelb3311b02016-07-08 13:31:31 +02002982 dma_dom = to_dma_ops_domain(domain);
Joerg Roedelcda70052016-07-07 15:57:04 +02002983 dma_ops_domain_free(dma_dom);
2984 break;
2985 default:
2986 if (domain->mode != PAGE_MODE_NONE)
2987 free_pagetable(domain);
Joerg Roedel52815b72011-11-17 17:24:28 +01002988
Joerg Roedelcda70052016-07-07 15:57:04 +02002989 if (domain->flags & PD_IOMMUV2_MASK)
2990 free_gcr3_table(domain);
2991
2992 protection_domain_free(domain);
2993 break;
2994 }
Joerg Roedel98383fc2008-12-02 18:34:12 +01002995}
2996
Joerg Roedel684f2882008-12-08 12:07:44 +01002997static void amd_iommu_detach_device(struct iommu_domain *dom,
2998 struct device *dev)
2999{
Joerg Roedel657cbb62009-11-23 15:26:46 +01003000 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01003001 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003002 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01003003
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003004 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01003005 return;
3006
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003007 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003008 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003009 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01003010
Joerg Roedel657cbb62009-11-23 15:26:46 +01003011 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003012 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01003013
3014 iommu = amd_iommu_rlookup_table[devid];
3015 if (!iommu)
3016 return;
3017
Joerg Roedel684f2882008-12-08 12:07:44 +01003018 iommu_completion_wait(iommu);
3019}
3020
Joerg Roedel01106062008-12-02 19:34:11 +01003021static int amd_iommu_attach_device(struct iommu_domain *dom,
3022 struct device *dev)
3023{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003024 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01003025 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01003026 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01003027 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003028
Joerg Roedel98fc5a62009-11-24 17:19:23 +01003029 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01003030 return -EINVAL;
3031
Joerg Roedel657cbb62009-11-23 15:26:46 +01003032 dev_data = dev->archdata.iommu;
3033
Joerg Roedelf62dda62011-06-09 12:55:35 +02003034 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01003035 if (!iommu)
3036 return -EINVAL;
3037
Joerg Roedel657cbb62009-11-23 15:26:46 +01003038 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01003039 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01003040
Joerg Roedel15898bb2009-11-24 15:39:42 +01003041 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01003042
3043 iommu_completion_wait(iommu);
3044
Joerg Roedel15898bb2009-11-24 15:39:42 +01003045 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01003046}
3047
Joerg Roedel468e2362010-01-21 16:37:36 +01003048static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003049 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003050{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003051 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003052 int prot = 0;
3053 int ret;
3054
Joerg Roedel132bd682011-11-17 14:18:46 +01003055 if (domain->mode == PAGE_MODE_NONE)
3056 return -EINVAL;
3057
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003058 if (iommu_prot & IOMMU_READ)
3059 prot |= IOMMU_PROT_IR;
3060 if (iommu_prot & IOMMU_WRITE)
3061 prot |= IOMMU_PROT_IW;
3062
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003063 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02003064 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003065 mutex_unlock(&domain->api_lock);
3066
Joerg Roedel795e74f72010-05-11 17:40:57 +02003067 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01003068}
3069
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003070static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
3071 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003072{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003073 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003074 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003075
Joerg Roedel132bd682011-11-17 14:18:46 +01003076 if (domain->mode == PAGE_MODE_NONE)
3077 return -EINVAL;
3078
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003079 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01003080 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02003081 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003082
Joerg Roedel17b124b2011-04-06 18:01:35 +02003083 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01003084
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02003085 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01003086}
3087
Joerg Roedel645c4c82008-12-02 20:05:50 +01003088static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05303089 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01003090{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003091 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02003092 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003093 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01003094
Joerg Roedel132bd682011-11-17 14:18:46 +01003095 if (domain->mode == PAGE_MODE_NONE)
3096 return iova;
3097
Joerg Roedel3039ca12015-04-01 14:58:48 +02003098 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003099
Joerg Roedela6d41a42009-09-02 17:08:55 +02003100 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01003101 return 0;
3102
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003103 offset_mask = pte_pgsize - 1;
3104 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01003105
Joerg Roedelb24b1b62015-04-01 14:58:51 +02003106 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01003107}
3108
Joerg Roedelab636482014-09-05 10:48:21 +02003109static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003110{
Joerg Roedel80a506b2010-07-27 17:14:24 +02003111 switch (cap) {
3112 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02003113 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02003114 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02003115 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00003116 case IOMMU_CAP_NOEXEC:
3117 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02003118 }
3119
Joerg Roedelab636482014-09-05 10:48:21 +02003120 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003121}
3122
Joerg Roedel35cf2482015-05-28 18:41:37 +02003123static void amd_iommu_get_dm_regions(struct device *dev,
3124 struct list_head *head)
3125{
3126 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003127 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003128
3129 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003130 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003131 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02003132
3133 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
3134 struct iommu_dm_region *region;
3135
3136 if (devid < entry->devid_start || devid > entry->devid_end)
3137 continue;
3138
3139 region = kzalloc(sizeof(*region), GFP_KERNEL);
3140 if (!region) {
3141 pr_err("Out of memory allocating dm-regions for %s\n",
3142 dev_name(dev));
3143 return;
3144 }
3145
3146 region->start = entry->address_start;
3147 region->length = entry->address_end - entry->address_start;
3148 if (entry->prot & IOMMU_PROT_IR)
3149 region->prot |= IOMMU_READ;
3150 if (entry->prot & IOMMU_PROT_IW)
3151 region->prot |= IOMMU_WRITE;
3152
3153 list_add_tail(&region->list, head);
3154 }
3155}
3156
3157static void amd_iommu_put_dm_regions(struct device *dev,
3158 struct list_head *head)
3159{
3160 struct iommu_dm_region *entry, *next;
3161
3162 list_for_each_entry_safe(entry, next, head, list)
3163 kfree(entry);
3164}
3165
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003166static void amd_iommu_apply_dm_region(struct device *dev,
3167 struct iommu_domain *domain,
3168 struct iommu_dm_region *region)
3169{
Joerg Roedelb3311b02016-07-08 13:31:31 +02003170 struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003171 unsigned long start, end;
3172
3173 start = IOVA_PFN(region->start);
3174 end = IOVA_PFN(region->start + region->length);
3175
3176 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
3177}
3178
Thierry Redingb22f6432014-06-27 09:03:12 +02003179static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02003180 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003181 .domain_alloc = amd_iommu_domain_alloc,
3182 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003183 .attach_dev = amd_iommu_attach_device,
3184 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01003185 .map = amd_iommu_map,
3186 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07003187 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003188 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02003189 .add_device = amd_iommu_add_device,
3190 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04003191 .device_group = amd_iommu_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02003192 .get_dm_regions = amd_iommu_get_dm_regions,
3193 .put_dm_regions = amd_iommu_put_dm_regions,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02003194 .apply_dm_region = amd_iommu_apply_dm_region,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02003195 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01003196};
3197
Joerg Roedel0feae532009-08-26 15:26:30 +02003198/*****************************************************************************
3199 *
3200 * The next functions do a basic initialization of IOMMU for pass through
3201 * mode
3202 *
3203 * In passthrough mode the IOMMU is initialized and enabled but not used for
3204 * DMA-API translation.
3205 *
3206 *****************************************************************************/
3207
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01003208/* IOMMUv2 specific functions */
3209int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
3210{
3211 return atomic_notifier_chain_register(&ppr_notifier, nb);
3212}
3213EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
3214
3215int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
3216{
3217 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
3218}
3219EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01003220
3221void amd_iommu_domain_direct_map(struct iommu_domain *dom)
3222{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003223 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01003224 unsigned long flags;
3225
3226 spin_lock_irqsave(&domain->lock, flags);
3227
3228 /* Update data structure */
3229 domain->mode = PAGE_MODE_NONE;
3230 domain->updated = true;
3231
3232 /* Make changes visible to IOMMUs */
3233 update_domain(domain);
3234
3235 /* Page-table is not visible to IOMMU anymore, so free it */
3236 free_pagetable(domain);
3237
3238 spin_unlock_irqrestore(&domain->lock, flags);
3239}
3240EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01003241
3242int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
3243{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003244 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01003245 unsigned long flags;
3246 int levels, ret;
3247
3248 if (pasids <= 0 || pasids > (PASID_MASK + 1))
3249 return -EINVAL;
3250
3251 /* Number of GCR3 table levels required */
3252 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
3253 levels += 1;
3254
3255 if (levels > amd_iommu_max_glx_val)
3256 return -EINVAL;
3257
3258 spin_lock_irqsave(&domain->lock, flags);
3259
3260 /*
3261 * Save us all sanity checks whether devices already in the
3262 * domain support IOMMUv2. Just force that the domain has no
3263 * devices attached when it is switched into IOMMUv2 mode.
3264 */
3265 ret = -EBUSY;
3266 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
3267 goto out;
3268
3269 ret = -ENOMEM;
3270 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
3271 if (domain->gcr3_tbl == NULL)
3272 goto out;
3273
3274 domain->glx = levels;
3275 domain->flags |= PD_IOMMUV2_MASK;
3276 domain->updated = true;
3277
3278 update_domain(domain);
3279
3280 ret = 0;
3281
3282out:
3283 spin_unlock_irqrestore(&domain->lock, flags);
3284
3285 return ret;
3286}
3287EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003288
3289static int __flush_pasid(struct protection_domain *domain, int pasid,
3290 u64 address, bool size)
3291{
3292 struct iommu_dev_data *dev_data;
3293 struct iommu_cmd cmd;
3294 int i, ret;
3295
3296 if (!(domain->flags & PD_IOMMUV2_MASK))
3297 return -EINVAL;
3298
3299 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3300
3301 /*
3302 * IOMMU TLB needs to be flushed before Device TLB to
3303 * prevent device TLB refill from IOMMU TLB
3304 */
3305 for (i = 0; i < amd_iommus_present; ++i) {
3306 if (domain->dev_iommu[i] == 0)
3307 continue;
3308
3309 ret = iommu_queue_command(amd_iommus[i], &cmd);
3310 if (ret != 0)
3311 goto out;
3312 }
3313
3314 /* Wait until IOMMU TLB flushes are complete */
3315 domain_flush_complete(domain);
3316
3317 /* Now flush device TLBs */
3318 list_for_each_entry(dev_data, &domain->dev_list, list) {
3319 struct amd_iommu *iommu;
3320 int qdep;
3321
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003322 /*
3323 There might be non-IOMMUv2 capable devices in an IOMMUv2
3324 * domain.
3325 */
3326 if (!dev_data->ats.enabled)
3327 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003328
3329 qdep = dev_data->ats.qdep;
3330 iommu = amd_iommu_rlookup_table[dev_data->devid];
3331
3332 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3333 qdep, address, size);
3334
3335 ret = iommu_queue_command(iommu, &cmd);
3336 if (ret != 0)
3337 goto out;
3338 }
3339
3340 /* Wait until all device TLBs are flushed */
3341 domain_flush_complete(domain);
3342
3343 ret = 0;
3344
3345out:
3346
3347 return ret;
3348}
3349
3350static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3351 u64 address)
3352{
3353 return __flush_pasid(domain, pasid, address, false);
3354}
3355
3356int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3357 u64 address)
3358{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003359 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003360 unsigned long flags;
3361 int ret;
3362
3363 spin_lock_irqsave(&domain->lock, flags);
3364 ret = __amd_iommu_flush_page(domain, pasid, address);
3365 spin_unlock_irqrestore(&domain->lock, flags);
3366
3367 return ret;
3368}
3369EXPORT_SYMBOL(amd_iommu_flush_page);
3370
3371static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3372{
3373 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3374 true);
3375}
3376
3377int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3378{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003379 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003380 unsigned long flags;
3381 int ret;
3382
3383 spin_lock_irqsave(&domain->lock, flags);
3384 ret = __amd_iommu_flush_tlb(domain, pasid);
3385 spin_unlock_irqrestore(&domain->lock, flags);
3386
3387 return ret;
3388}
3389EXPORT_SYMBOL(amd_iommu_flush_tlb);
3390
Joerg Roedelb16137b2011-11-21 16:50:23 +01003391static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3392{
3393 int index;
3394 u64 *pte;
3395
3396 while (true) {
3397
3398 index = (pasid >> (9 * level)) & 0x1ff;
3399 pte = &root[index];
3400
3401 if (level == 0)
3402 break;
3403
3404 if (!(*pte & GCR3_VALID)) {
3405 if (!alloc)
3406 return NULL;
3407
3408 root = (void *)get_zeroed_page(GFP_ATOMIC);
3409 if (root == NULL)
3410 return NULL;
3411
3412 *pte = __pa(root) | GCR3_VALID;
3413 }
3414
3415 root = __va(*pte & PAGE_MASK);
3416
3417 level -= 1;
3418 }
3419
3420 return pte;
3421}
3422
3423static int __set_gcr3(struct protection_domain *domain, int pasid,
3424 unsigned long cr3)
3425{
3426 u64 *pte;
3427
3428 if (domain->mode != PAGE_MODE_NONE)
3429 return -EINVAL;
3430
3431 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3432 if (pte == NULL)
3433 return -ENOMEM;
3434
3435 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3436
3437 return __amd_iommu_flush_tlb(domain, pasid);
3438}
3439
3440static int __clear_gcr3(struct protection_domain *domain, int pasid)
3441{
3442 u64 *pte;
3443
3444 if (domain->mode != PAGE_MODE_NONE)
3445 return -EINVAL;
3446
3447 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3448 if (pte == NULL)
3449 return 0;
3450
3451 *pte = 0;
3452
3453 return __amd_iommu_flush_tlb(domain, pasid);
3454}
3455
3456int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3457 unsigned long cr3)
3458{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003459 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003460 unsigned long flags;
3461 int ret;
3462
3463 spin_lock_irqsave(&domain->lock, flags);
3464 ret = __set_gcr3(domain, pasid, cr3);
3465 spin_unlock_irqrestore(&domain->lock, flags);
3466
3467 return ret;
3468}
3469EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3470
3471int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3472{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003473 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003474 unsigned long flags;
3475 int ret;
3476
3477 spin_lock_irqsave(&domain->lock, flags);
3478 ret = __clear_gcr3(domain, pasid);
3479 spin_unlock_irqrestore(&domain->lock, flags);
3480
3481 return ret;
3482}
3483EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003484
3485int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3486 int status, int tag)
3487{
3488 struct iommu_dev_data *dev_data;
3489 struct amd_iommu *iommu;
3490 struct iommu_cmd cmd;
3491
3492 dev_data = get_dev_data(&pdev->dev);
3493 iommu = amd_iommu_rlookup_table[dev_data->devid];
3494
3495 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3496 tag, dev_data->pri_tlp);
3497
3498 return iommu_queue_command(iommu, &cmd);
3499}
3500EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003501
3502struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3503{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003504 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003505
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003506 pdomain = get_domain(&pdev->dev);
3507 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003508 return NULL;
3509
3510 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003511 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003512 return NULL;
3513
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003514 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003515}
3516EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003517
3518void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3519{
3520 struct iommu_dev_data *dev_data;
3521
3522 if (!amd_iommu_v2_supported())
3523 return;
3524
3525 dev_data = get_dev_data(&pdev->dev);
3526 dev_data->errata |= (1 << erratum);
3527}
3528EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003529
3530int amd_iommu_device_info(struct pci_dev *pdev,
3531 struct amd_iommu_device_info *info)
3532{
3533 int max_pasids;
3534 int pos;
3535
3536 if (pdev == NULL || info == NULL)
3537 return -EINVAL;
3538
3539 if (!amd_iommu_v2_supported())
3540 return -EINVAL;
3541
3542 memset(info, 0, sizeof(*info));
3543
3544 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3545 if (pos)
3546 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3547
3548 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3549 if (pos)
3550 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3551
3552 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3553 if (pos) {
3554 int features;
3555
3556 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3557 max_pasids = min(max_pasids, (1 << 20));
3558
3559 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3560 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3561
3562 features = pci_pasid_features(pdev);
3563 if (features & PCI_PASID_CAP_EXEC)
3564 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3565 if (features & PCI_PASID_CAP_PRIV)
3566 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3567 }
3568
3569 return 0;
3570}
3571EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003572
3573#ifdef CONFIG_IRQ_REMAP
3574
3575/*****************************************************************************
3576 *
3577 * Interrupt Remapping Implementation
3578 *
3579 *****************************************************************************/
3580
Jiang Liu7c71d302015-04-13 14:11:33 +08003581static struct irq_chip amd_ir_chip;
3582
Joerg Roedel2b324502012-06-21 16:29:10 +02003583#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3584#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3585#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3586#define DTE_IRQ_REMAP_ENABLE 1ULL
3587
3588static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3589{
3590 u64 dte;
3591
3592 dte = amd_iommu_dev_table[devid].data[2];
3593 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3594 dte |= virt_to_phys(table->table);
3595 dte |= DTE_IRQ_REMAP_INTCTL;
3596 dte |= DTE_IRQ_TABLE_LEN;
3597 dte |= DTE_IRQ_REMAP_ENABLE;
3598
3599 amd_iommu_dev_table[devid].data[2] = dte;
3600}
3601
Joerg Roedel2b324502012-06-21 16:29:10 +02003602static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3603{
3604 struct irq_remap_table *table = NULL;
3605 struct amd_iommu *iommu;
3606 unsigned long flags;
3607 u16 alias;
3608
3609 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3610
3611 iommu = amd_iommu_rlookup_table[devid];
3612 if (!iommu)
3613 goto out_unlock;
3614
3615 table = irq_lookup_table[devid];
3616 if (table)
3617 goto out;
3618
3619 alias = amd_iommu_alias_table[devid];
3620 table = irq_lookup_table[alias];
3621 if (table) {
3622 irq_lookup_table[devid] = table;
3623 set_dte_irq_entry(devid, table);
3624 iommu_flush_dte(iommu, devid);
3625 goto out;
3626 }
3627
3628 /* Nothing there yet, allocate new irq remapping table */
3629 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3630 if (!table)
3631 goto out;
3632
Joerg Roedel197887f2013-04-09 21:14:08 +02003633 /* Initialize table spin-lock */
3634 spin_lock_init(&table->lock);
3635
Joerg Roedel2b324502012-06-21 16:29:10 +02003636 if (ioapic)
3637 /* Keep the first 32 indexes free for IOAPIC interrupts */
3638 table->min_index = 32;
3639
3640 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3641 if (!table->table) {
3642 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003643 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003644 goto out;
3645 }
3646
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003647 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3648 memset(table->table, 0,
3649 MAX_IRQS_PER_TABLE * sizeof(u32));
3650 else
3651 memset(table->table, 0,
3652 (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
Joerg Roedel2b324502012-06-21 16:29:10 +02003653
3654 if (ioapic) {
3655 int i;
3656
3657 for (i = 0; i < 32; ++i)
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003658 iommu->irte_ops->set_allocated(table, i);
Joerg Roedel2b324502012-06-21 16:29:10 +02003659 }
3660
3661 irq_lookup_table[devid] = table;
3662 set_dte_irq_entry(devid, table);
3663 iommu_flush_dte(iommu, devid);
3664 if (devid != alias) {
3665 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003666 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003667 iommu_flush_dte(iommu, alias);
3668 }
3669
3670out:
3671 iommu_completion_wait(iommu);
3672
3673out_unlock:
3674 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3675
3676 return table;
3677}
3678
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003679static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003680{
3681 struct irq_remap_table *table;
3682 unsigned long flags;
3683 int index, c;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003684 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
3685
3686 if (!iommu)
3687 return -ENODEV;
Joerg Roedel2b324502012-06-21 16:29:10 +02003688
3689 table = get_irq_table(devid, false);
3690 if (!table)
3691 return -ENODEV;
3692
3693 spin_lock_irqsave(&table->lock, flags);
3694
3695 /* Scan table for free entries */
3696 for (c = 0, index = table->min_index;
3697 index < MAX_IRQS_PER_TABLE;
3698 ++index) {
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003699 if (!iommu->irte_ops->is_allocated(table, index))
Joerg Roedel2b324502012-06-21 16:29:10 +02003700 c += 1;
3701 else
3702 c = 0;
3703
3704 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003705 for (; c != 0; --c)
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003706 iommu->irte_ops->set_allocated(table, index - c + 1);
Joerg Roedel2b324502012-06-21 16:29:10 +02003707
3708 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003709 goto out;
3710 }
3711 }
3712
3713 index = -ENOSPC;
3714
3715out:
3716 spin_unlock_irqrestore(&table->lock, flags);
3717
3718 return index;
3719}
3720
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003721static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte)
3722{
3723 struct irq_remap_table *table;
3724 struct amd_iommu *iommu;
3725 unsigned long flags;
3726 struct irte_ga *entry;
3727
3728 iommu = amd_iommu_rlookup_table[devid];
3729 if (iommu == NULL)
3730 return -EINVAL;
3731
3732 table = get_irq_table(devid, false);
3733 if (!table)
3734 return -ENOMEM;
3735
3736 spin_lock_irqsave(&table->lock, flags);
3737
3738 entry = (struct irte_ga *)table->table;
3739 entry = &entry[index];
3740 entry->lo.fields_remap.valid = 0;
3741 entry->hi.val = irte->hi.val;
3742 entry->lo.val = irte->lo.val;
3743 entry->lo.fields_remap.valid = 1;
3744
3745 spin_unlock_irqrestore(&table->lock, flags);
3746
3747 iommu_flush_irt(iommu, devid);
3748 iommu_completion_wait(iommu);
3749
3750 return 0;
3751}
3752
3753static int modify_irte(u16 devid, int index, union irte *irte)
Joerg Roedel2b324502012-06-21 16:29:10 +02003754{
3755 struct irq_remap_table *table;
3756 struct amd_iommu *iommu;
3757 unsigned long flags;
3758
3759 iommu = amd_iommu_rlookup_table[devid];
3760 if (iommu == NULL)
3761 return -EINVAL;
3762
3763 table = get_irq_table(devid, false);
3764 if (!table)
3765 return -ENOMEM;
3766
3767 spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003768 table->table[index] = irte->val;
Joerg Roedel2b324502012-06-21 16:29:10 +02003769 spin_unlock_irqrestore(&table->lock, flags);
3770
3771 iommu_flush_irt(iommu, devid);
3772 iommu_completion_wait(iommu);
3773
3774 return 0;
3775}
3776
3777static void free_irte(u16 devid, int index)
3778{
3779 struct irq_remap_table *table;
3780 struct amd_iommu *iommu;
3781 unsigned long flags;
3782
3783 iommu = amd_iommu_rlookup_table[devid];
3784 if (iommu == NULL)
3785 return;
3786
3787 table = get_irq_table(devid, false);
3788 if (!table)
3789 return;
3790
3791 spin_lock_irqsave(&table->lock, flags);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003792 iommu->irte_ops->clear_allocated(table, index);
Joerg Roedel2b324502012-06-21 16:29:10 +02003793 spin_unlock_irqrestore(&table->lock, flags);
3794
3795 iommu_flush_irt(iommu, devid);
3796 iommu_completion_wait(iommu);
3797}
3798
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003799static void irte_prepare(void *entry,
3800 u32 delivery_mode, u32 dest_mode,
3801 u8 vector, u32 dest_apicid)
3802{
3803 union irte *irte = (union irte *) entry;
3804
3805 irte->val = 0;
3806 irte->fields.vector = vector;
3807 irte->fields.int_type = delivery_mode;
3808 irte->fields.destination = dest_apicid;
3809 irte->fields.dm = dest_mode;
3810 irte->fields.valid = 1;
3811}
3812
3813static void irte_ga_prepare(void *entry,
3814 u32 delivery_mode, u32 dest_mode,
3815 u8 vector, u32 dest_apicid)
3816{
3817 struct irte_ga *irte = (struct irte_ga *) entry;
3818
3819 irte->lo.val = 0;
3820 irte->hi.val = 0;
3821 irte->lo.fields_remap.guest_mode = 0;
3822 irte->lo.fields_remap.int_type = delivery_mode;
3823 irte->lo.fields_remap.dm = dest_mode;
3824 irte->hi.fields.vector = vector;
3825 irte->lo.fields_remap.destination = dest_apicid;
3826 irte->lo.fields_remap.valid = 1;
3827}
3828
3829static void irte_activate(void *entry, u16 devid, u16 index)
3830{
3831 union irte *irte = (union irte *) entry;
3832
3833 irte->fields.valid = 1;
3834 modify_irte(devid, index, irte);
3835}
3836
3837static void irte_ga_activate(void *entry, u16 devid, u16 index)
3838{
3839 struct irte_ga *irte = (struct irte_ga *) entry;
3840
3841 irte->lo.fields_remap.valid = 1;
3842 modify_irte_ga(devid, index, irte);
3843}
3844
3845static void irte_deactivate(void *entry, u16 devid, u16 index)
3846{
3847 union irte *irte = (union irte *) entry;
3848
3849 irte->fields.valid = 0;
3850 modify_irte(devid, index, irte);
3851}
3852
3853static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
3854{
3855 struct irte_ga *irte = (struct irte_ga *) entry;
3856
3857 irte->lo.fields_remap.valid = 0;
3858 modify_irte_ga(devid, index, irte);
3859}
3860
3861static void irte_set_affinity(void *entry, u16 devid, u16 index,
3862 u8 vector, u32 dest_apicid)
3863{
3864 union irte *irte = (union irte *) entry;
3865
3866 irte->fields.vector = vector;
3867 irte->fields.destination = dest_apicid;
3868 modify_irte(devid, index, irte);
3869}
3870
3871static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
3872 u8 vector, u32 dest_apicid)
3873{
3874 struct irte_ga *irte = (struct irte_ga *) entry;
3875
3876 irte->hi.fields.vector = vector;
3877 irte->lo.fields_remap.destination = dest_apicid;
3878 irte->lo.fields_remap.guest_mode = 0;
3879 modify_irte_ga(devid, index, irte);
3880}
3881
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05003882#define IRTE_ALLOCATED (~1U)
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05003883static void irte_set_allocated(struct irq_remap_table *table, int index)
3884{
3885 table->table[index] = IRTE_ALLOCATED;
3886}
3887
3888static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
3889{
3890 struct irte_ga *ptr = (struct irte_ga *)table->table;
3891 struct irte_ga *irte = &ptr[index];
3892
3893 memset(&irte->lo.val, 0, sizeof(u64));
3894 memset(&irte->hi.val, 0, sizeof(u64));
3895 irte->hi.fields.vector = 0xff;
3896}
3897
3898static bool irte_is_allocated(struct irq_remap_table *table, int index)
3899{
3900 union irte *ptr = (union irte *)table->table;
3901 union irte *irte = &ptr[index];
3902
3903 return irte->val != 0;
3904}
3905
3906static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
3907{
3908 struct irte_ga *ptr = (struct irte_ga *)table->table;
3909 struct irte_ga *irte = &ptr[index];
3910
3911 return irte->hi.fields.vector != 0;
3912}
3913
3914static void irte_clear_allocated(struct irq_remap_table *table, int index)
3915{
3916 table->table[index] = 0;
3917}
3918
3919static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
3920{
3921 struct irte_ga *ptr = (struct irte_ga *)table->table;
3922 struct irte_ga *irte = &ptr[index];
3923
3924 memset(&irte->lo.val, 0, sizeof(u64));
3925 memset(&irte->hi.val, 0, sizeof(u64));
3926}
3927
Jiang Liu7c71d302015-04-13 14:11:33 +08003928static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003929{
Jiang Liu7c71d302015-04-13 14:11:33 +08003930 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003931
Jiang Liu7c71d302015-04-13 14:11:33 +08003932 switch (info->type) {
3933 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3934 devid = get_ioapic_devid(info->ioapic_id);
3935 break;
3936 case X86_IRQ_ALLOC_TYPE_HPET:
3937 devid = get_hpet_devid(info->hpet_id);
3938 break;
3939 case X86_IRQ_ALLOC_TYPE_MSI:
3940 case X86_IRQ_ALLOC_TYPE_MSIX:
3941 devid = get_device_id(&info->msi_dev->dev);
3942 break;
3943 default:
3944 BUG_ON(1);
3945 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003946 }
3947
Jiang Liu7c71d302015-04-13 14:11:33 +08003948 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003949}
3950
Jiang Liu7c71d302015-04-13 14:11:33 +08003951static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003952{
Jiang Liu7c71d302015-04-13 14:11:33 +08003953 struct amd_iommu *iommu;
3954 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003955
Jiang Liu7c71d302015-04-13 14:11:33 +08003956 if (!info)
3957 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003958
Jiang Liu7c71d302015-04-13 14:11:33 +08003959 devid = get_devid(info);
3960 if (devid >= 0) {
3961 iommu = amd_iommu_rlookup_table[devid];
3962 if (iommu)
3963 return iommu->ir_domain;
3964 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003965
Jiang Liu7c71d302015-04-13 14:11:33 +08003966 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003967}
3968
Jiang Liu7c71d302015-04-13 14:11:33 +08003969static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003970{
Jiang Liu7c71d302015-04-13 14:11:33 +08003971 struct amd_iommu *iommu;
3972 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003973
Jiang Liu7c71d302015-04-13 14:11:33 +08003974 if (!info)
3975 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003976
Jiang Liu7c71d302015-04-13 14:11:33 +08003977 switch (info->type) {
3978 case X86_IRQ_ALLOC_TYPE_MSI:
3979 case X86_IRQ_ALLOC_TYPE_MSIX:
3980 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003981 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003982 return NULL;
3983
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003984 iommu = amd_iommu_rlookup_table[devid];
3985 if (iommu)
3986 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003987 break;
3988 default:
3989 break;
3990 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003991
Jiang Liu7c71d302015-04-13 14:11:33 +08003992 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003993}
3994
Joerg Roedel6b474b82012-06-26 16:46:04 +02003995struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003996 .prepare = amd_iommu_prepare,
3997 .enable = amd_iommu_enable,
3998 .disable = amd_iommu_disable,
3999 .reenable = amd_iommu_reenable,
4000 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08004001 .get_ir_irq_domain = get_ir_irq_domain,
4002 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02004003};
Jiang Liu7c71d302015-04-13 14:11:33 +08004004
4005static void irq_remapping_prepare_irte(struct amd_ir_data *data,
4006 struct irq_cfg *irq_cfg,
4007 struct irq_alloc_info *info,
4008 int devid, int index, int sub_handle)
4009{
4010 struct irq_2_irte *irte_info = &data->irq_2_irte;
4011 struct msi_msg *msg = &data->msi_entry;
Jiang Liu7c71d302015-04-13 14:11:33 +08004012 struct IO_APIC_route_entry *entry;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004013 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
4014
4015 if (!iommu)
4016 return;
Jiang Liu7c71d302015-04-13 14:11:33 +08004017
Jiang Liu7c71d302015-04-13 14:11:33 +08004018 data->irq_2_irte.devid = devid;
4019 data->irq_2_irte.index = index + sub_handle;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004020 iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
4021 apic->irq_dest_mode, irq_cfg->vector,
4022 irq_cfg->dest_apicid);
Jiang Liu7c71d302015-04-13 14:11:33 +08004023
4024 switch (info->type) {
4025 case X86_IRQ_ALLOC_TYPE_IOAPIC:
4026 /* Setup IOAPIC entry */
4027 entry = info->ioapic_entry;
4028 info->ioapic_entry = NULL;
4029 memset(entry, 0, sizeof(*entry));
4030 entry->vector = index;
4031 entry->mask = 0;
4032 entry->trigger = info->ioapic_trigger;
4033 entry->polarity = info->ioapic_polarity;
4034 /* Mask level triggered irqs. */
4035 if (info->ioapic_trigger)
4036 entry->mask = 1;
4037 break;
4038
4039 case X86_IRQ_ALLOC_TYPE_HPET:
4040 case X86_IRQ_ALLOC_TYPE_MSI:
4041 case X86_IRQ_ALLOC_TYPE_MSIX:
4042 msg->address_hi = MSI_ADDR_BASE_HI;
4043 msg->address_lo = MSI_ADDR_BASE_LO;
4044 msg->data = irte_info->index;
4045 break;
4046
4047 default:
4048 BUG_ON(1);
4049 break;
4050 }
4051}
4052
Suravee Suthikulpanit880ac602016-08-23 13:52:34 -05004053struct amd_irte_ops irte_32_ops = {
4054 .prepare = irte_prepare,
4055 .activate = irte_activate,
4056 .deactivate = irte_deactivate,
4057 .set_affinity = irte_set_affinity,
4058 .set_allocated = irte_set_allocated,
4059 .is_allocated = irte_is_allocated,
4060 .clear_allocated = irte_clear_allocated,
4061};
4062
4063struct amd_irte_ops irte_128_ops = {
4064 .prepare = irte_ga_prepare,
4065 .activate = irte_ga_activate,
4066 .deactivate = irte_ga_deactivate,
4067 .set_affinity = irte_ga_set_affinity,
4068 .set_allocated = irte_ga_set_allocated,
4069 .is_allocated = irte_ga_is_allocated,
4070 .clear_allocated = irte_ga_clear_allocated,
4071};
4072
Jiang Liu7c71d302015-04-13 14:11:33 +08004073static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
4074 unsigned int nr_irqs, void *arg)
4075{
4076 struct irq_alloc_info *info = arg;
4077 struct irq_data *irq_data;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004078 struct amd_ir_data *data = NULL;
Jiang Liu7c71d302015-04-13 14:11:33 +08004079 struct irq_cfg *cfg;
4080 int i, ret, devid;
4081 int index = -1;
4082
4083 if (!info)
4084 return -EINVAL;
4085 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
4086 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
4087 return -EINVAL;
4088
4089 /*
4090 * With IRQ remapping enabled, don't need contiguous CPU vectors
4091 * to support multiple MSI interrupts.
4092 */
4093 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
4094 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
4095
4096 devid = get_devid(info);
4097 if (devid < 0)
4098 return -EINVAL;
4099
4100 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
4101 if (ret < 0)
4102 return ret;
4103
Jiang Liu7c71d302015-04-13 14:11:33 +08004104 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
4105 if (get_irq_table(devid, true))
4106 index = info->ioapic_pin;
4107 else
4108 ret = -ENOMEM;
4109 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08004110 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08004111 }
4112 if (index < 0) {
4113 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08004114 goto out_free_parent;
4115 }
4116
4117 for (i = 0; i < nr_irqs; i++) {
4118 irq_data = irq_domain_get_irq_data(domain, virq + i);
4119 cfg = irqd_cfg(irq_data);
4120 if (!irq_data || !cfg) {
4121 ret = -EINVAL;
4122 goto out_free_data;
4123 }
4124
Joerg Roedela130e692015-08-13 11:07:25 +02004125 ret = -ENOMEM;
4126 data = kzalloc(sizeof(*data), GFP_KERNEL);
4127 if (!data)
4128 goto out_free_data;
4129
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004130 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
4131 data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
4132 else
4133 data->entry = kzalloc(sizeof(struct irte_ga),
4134 GFP_KERNEL);
4135 if (!data->entry) {
4136 kfree(data);
4137 goto out_free_data;
4138 }
4139
Jiang Liu7c71d302015-04-13 14:11:33 +08004140 irq_data->hwirq = (devid << 16) + i;
4141 irq_data->chip_data = data;
4142 irq_data->chip = &amd_ir_chip;
4143 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
4144 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
4145 }
Joerg Roedela130e692015-08-13 11:07:25 +02004146
Jiang Liu7c71d302015-04-13 14:11:33 +08004147 return 0;
4148
4149out_free_data:
4150 for (i--; i >= 0; i--) {
4151 irq_data = irq_domain_get_irq_data(domain, virq + i);
4152 if (irq_data)
4153 kfree(irq_data->chip_data);
4154 }
4155 for (i = 0; i < nr_irqs; i++)
4156 free_irte(devid, index + i);
4157out_free_parent:
4158 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4159 return ret;
4160}
4161
4162static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
4163 unsigned int nr_irqs)
4164{
4165 struct irq_2_irte *irte_info;
4166 struct irq_data *irq_data;
4167 struct amd_ir_data *data;
4168 int i;
4169
4170 for (i = 0; i < nr_irqs; i++) {
4171 irq_data = irq_domain_get_irq_data(domain, virq + i);
4172 if (irq_data && irq_data->chip_data) {
4173 data = irq_data->chip_data;
4174 irte_info = &data->irq_2_irte;
4175 free_irte(irte_info->devid, irte_info->index);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004176 kfree(data->entry);
Jiang Liu7c71d302015-04-13 14:11:33 +08004177 kfree(data);
4178 }
4179 }
4180 irq_domain_free_irqs_common(domain, virq, nr_irqs);
4181}
4182
4183static void irq_remapping_activate(struct irq_domain *domain,
4184 struct irq_data *irq_data)
4185{
4186 struct amd_ir_data *data = irq_data->chip_data;
4187 struct irq_2_irte *irte_info = &data->irq_2_irte;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004188 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004189
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004190 if (iommu)
4191 iommu->irte_ops->activate(data->entry, irte_info->devid,
4192 irte_info->index);
Jiang Liu7c71d302015-04-13 14:11:33 +08004193}
4194
4195static void irq_remapping_deactivate(struct irq_domain *domain,
4196 struct irq_data *irq_data)
4197{
4198 struct amd_ir_data *data = irq_data->chip_data;
4199 struct irq_2_irte *irte_info = &data->irq_2_irte;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004200 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004201
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004202 if (iommu)
4203 iommu->irte_ops->deactivate(data->entry, irte_info->devid,
4204 irte_info->index);
Jiang Liu7c71d302015-04-13 14:11:33 +08004205}
4206
4207static struct irq_domain_ops amd_ir_domain_ops = {
4208 .alloc = irq_remapping_alloc,
4209 .free = irq_remapping_free,
4210 .activate = irq_remapping_activate,
4211 .deactivate = irq_remapping_deactivate,
4212};
4213
4214static int amd_ir_set_affinity(struct irq_data *data,
4215 const struct cpumask *mask, bool force)
4216{
4217 struct amd_ir_data *ir_data = data->chip_data;
4218 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
4219 struct irq_cfg *cfg = irqd_cfg(data);
4220 struct irq_data *parent = data->parent_data;
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004221 struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
Jiang Liu7c71d302015-04-13 14:11:33 +08004222 int ret;
4223
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004224 if (!iommu)
4225 return -ENODEV;
4226
Jiang Liu7c71d302015-04-13 14:11:33 +08004227 ret = parent->chip->irq_set_affinity(parent, mask, force);
4228 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
4229 return ret;
4230
4231 /*
4232 * Atomically updates the IRTE with the new destination, vector
4233 * and flushes the interrupt entry cache.
4234 */
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05004235 iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
4236 irte_info->index, cfg->vector, cfg->dest_apicid);
Jiang Liu7c71d302015-04-13 14:11:33 +08004237
4238 /*
4239 * After this point, all the interrupts will start arriving
4240 * at the new destination. So, time to cleanup the previous
4241 * vector allocation.
4242 */
Jiang Liuc6c20022015-04-14 10:30:02 +08004243 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08004244
4245 return IRQ_SET_MASK_OK_DONE;
4246}
4247
4248static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
4249{
4250 struct amd_ir_data *ir_data = irq_data->chip_data;
4251
4252 *msg = ir_data->msi_entry;
4253}
4254
4255static struct irq_chip amd_ir_chip = {
4256 .irq_ack = ir_ack_apic_edge,
4257 .irq_set_affinity = amd_ir_set_affinity,
4258 .irq_compose_msi_msg = ir_compose_msi_msg,
4259};
4260
4261int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
4262{
4263 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
4264 if (!iommu->ir_domain)
4265 return -ENOMEM;
4266
4267 iommu->ir_domain->parent = arch_get_ir_parent_domain();
4268 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
4269
4270 return 0;
4271}
Suravee Suthikulpanit8dbea3f2016-08-23 13:52:38 -05004272
4273int amd_iommu_update_ga(int cpu, bool is_run, void *data)
4274{
4275 unsigned long flags;
4276 struct amd_iommu *iommu;
4277 struct irq_remap_table *irt;
4278 struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
4279 int devid = ir_data->irq_2_irte.devid;
4280 struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
4281 struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
4282
4283 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
4284 !ref || !entry || !entry->lo.fields_vapic.guest_mode)
4285 return 0;
4286
4287 iommu = amd_iommu_rlookup_table[devid];
4288 if (!iommu)
4289 return -ENODEV;
4290
4291 irt = get_irq_table(devid, false);
4292 if (!irt)
4293 return -ENODEV;
4294
4295 spin_lock_irqsave(&irt->lock, flags);
4296
4297 if (ref->lo.fields_vapic.guest_mode) {
4298 if (cpu >= 0)
4299 ref->lo.fields_vapic.destination = cpu;
4300 ref->lo.fields_vapic.is_run = is_run;
4301 barrier();
4302 }
4303
4304 spin_unlock_irqrestore(&irt->lock, flags);
4305
4306 iommu_flush_irt(iommu, devid);
4307 iommu_completion_wait(iommu);
4308 return 0;
4309}
4310EXPORT_SYMBOL(amd_iommu_update_ga);
Joerg Roedel2b324502012-06-21 16:29:10 +02004311#endif