blob: d218e35ed3e30056cef7687f37cb405b927e0727 [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 Roedel0feae532009-08-26 15:26:30 +020092/*
93 * Domain for untranslated devices - only allocated
94 * if iommu=pt passed on kernel cmd line.
95 */
Thierry Redingb22f6432014-06-27 09:03:12 +020096static const struct iommu_ops amd_iommu_ops;
Joerg Roedel26961ef2008-12-03 17:00:17 +010097
Joerg Roedel72e1dcc2011-11-10 19:13:51 +010098static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
Joerg Roedel52815b72011-11-17 17:24:28 +010099int amd_iommu_max_glx_val = -1;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100100
Joerg Roedelac1534a2012-06-21 14:52:40 +0200101static struct dma_map_ops amd_iommu_dma_ops;
102
Joerg Roedel431b2a22008-07-11 17:14:22 +0200103/*
Joerg Roedel50917e22014-08-05 16:38:38 +0200104 * This struct contains device specific data for the IOMMU
105 */
106struct iommu_dev_data {
107 struct list_head list; /* For domain->dev_list */
108 struct list_head dev_data_list; /* For global dev_data_list */
Joerg Roedel50917e22014-08-05 16:38:38 +0200109 struct protection_domain *domain; /* Domain the device is bound to */
Joerg Roedel50917e22014-08-05 16:38:38 +0200110 u16 devid; /* PCI Device ID */
Joerg Roedele3156042016-04-08 15:12:24 +0200111 u16 alias; /* Alias Device ID */
Joerg Roedel50917e22014-08-05 16:38:38 +0200112 bool iommu_v2; /* Device can make use of IOMMUv2 */
Joerg Roedel1e6a7b02015-07-28 16:58:48 +0200113 bool passthrough; /* Device is identity mapped */
Joerg Roedel50917e22014-08-05 16:38:38 +0200114 struct {
115 bool enabled;
116 int qdep;
117 } ats; /* ATS state */
118 bool pri_tlp; /* PASID TLB required for
119 PPR completions */
120 u32 errata; /* Bitmap for errata to apply */
121};
122
123/*
Joerg Roedel431b2a22008-07-11 17:14:22 +0200124 * general struct to manage commands send to an IOMMU
125 */
Joerg Roedeld6449532008-07-11 17:14:28 +0200126struct iommu_cmd {
Joerg Roedelb6c02712008-06-26 21:27:53 +0200127 u32 data[4];
128};
129
Joerg Roedel05152a02012-06-15 16:53:51 +0200130struct kmem_cache *amd_iommu_irq_cache;
131
Joerg Roedel04bfdd82009-09-02 16:00:23 +0200132static void update_domain(struct protection_domain *domain);
Joerg Roedel7a5a5662015-06-30 08:56:11 +0200133static int protection_domain_init(struct protection_domain *domain);
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100134static void detach_device(struct device *dev);
Chris Wrightc1eee672009-05-21 00:56:58 -0700135
Joerg Roedel007b74b2015-12-21 12:53:54 +0100136/*
Joerg Roedel007b74b2015-12-21 12:53:54 +0100137 * Data container for a dma_ops specific protection domain
138 */
139struct dma_ops_domain {
140 /* generic protection domain information */
141 struct protection_domain domain;
142
Joerg Roedel307d5852016-07-05 11:54:04 +0200143 /* IOVA RB-Tree */
144 struct iova_domain iovad;
Joerg Roedel007b74b2015-12-21 12:53:54 +0100145};
146
Joerg Roedel81cd07b2016-07-07 18:01:10 +0200147static struct iova_domain reserved_iova_ranges;
148static struct lock_class_key reserved_rbtree_key;
149
Joerg Roedel15898bb2009-11-24 15:39:42 +0100150/****************************************************************************
151 *
152 * Helper functions
153 *
154 ****************************************************************************/
155
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400156static inline int match_hid_uid(struct device *dev,
157 struct acpihid_map_entry *entry)
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100158{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400159 const char *hid, *uid;
160
161 hid = acpi_device_hid(ACPI_COMPANION(dev));
162 uid = acpi_device_uid(ACPI_COMPANION(dev));
163
164 if (!hid || !(*hid))
165 return -ENODEV;
166
167 if (!uid || !(*uid))
168 return strcmp(hid, entry->hid);
169
170 if (!(*entry->uid))
171 return strcmp(hid, entry->hid);
172
173 return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
Joerg Roedel3f4b87b2015-03-26 13:43:07 +0100174}
175
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400176static inline u16 get_pci_device_id(struct device *dev)
Joerg Roedele3156042016-04-08 15:12:24 +0200177{
178 struct pci_dev *pdev = to_pci_dev(dev);
179
180 return PCI_DEVID(pdev->bus->number, pdev->devfn);
181}
182
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400183static inline int get_acpihid_device_id(struct device *dev,
184 struct acpihid_map_entry **entry)
185{
186 struct acpihid_map_entry *p;
187
188 list_for_each_entry(p, &acpihid_map, list) {
189 if (!match_hid_uid(dev, p)) {
190 if (entry)
191 *entry = p;
192 return p->devid;
193 }
194 }
195 return -EINVAL;
196}
197
198static inline int get_device_id(struct device *dev)
199{
200 int devid;
201
202 if (dev_is_pci(dev))
203 devid = get_pci_device_id(dev);
204 else
205 devid = get_acpihid_device_id(dev, NULL);
206
207 return devid;
208}
209
Joerg Roedel15898bb2009-11-24 15:39:42 +0100210static struct protection_domain *to_pdomain(struct iommu_domain *dom)
211{
212 return container_of(dom, struct protection_domain, domain);
213}
214
Joerg Roedelf62dda62011-06-09 12:55:35 +0200215static struct iommu_dev_data *alloc_dev_data(u16 devid)
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200216{
217 struct iommu_dev_data *dev_data;
218 unsigned long flags;
219
220 dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
221 if (!dev_data)
222 return NULL;
223
Joerg Roedelf62dda62011-06-09 12:55:35 +0200224 dev_data->devid = devid;
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200225
226 spin_lock_irqsave(&dev_data_list_lock, flags);
227 list_add_tail(&dev_data->dev_data_list, &dev_data_list);
228 spin_unlock_irqrestore(&dev_data_list_lock, flags);
229
230 return dev_data;
231}
232
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200233static struct iommu_dev_data *search_dev_data(u16 devid)
234{
235 struct iommu_dev_data *dev_data;
236 unsigned long flags;
237
238 spin_lock_irqsave(&dev_data_list_lock, flags);
239 list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
240 if (dev_data->devid == devid)
241 goto out_unlock;
242 }
243
244 dev_data = NULL;
245
246out_unlock:
247 spin_unlock_irqrestore(&dev_data_list_lock, flags);
248
249 return dev_data;
250}
251
Joerg Roedele3156042016-04-08 15:12:24 +0200252static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
253{
254 *(u16 *)data = alias;
255 return 0;
256}
257
258static u16 get_alias(struct device *dev)
259{
260 struct pci_dev *pdev = to_pci_dev(dev);
261 u16 devid, ivrs_alias, pci_alias;
262
Joerg Roedel6c0b43d2016-05-09 19:39:17 +0200263 /* The callers make sure that get_device_id() does not fail here */
Joerg Roedele3156042016-04-08 15:12:24 +0200264 devid = get_device_id(dev);
265 ivrs_alias = amd_iommu_alias_table[devid];
266 pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
267
268 if (ivrs_alias == pci_alias)
269 return ivrs_alias;
270
271 /*
272 * DMA alias showdown
273 *
274 * The IVRS is fairly reliable in telling us about aliases, but it
275 * can't know about every screwy device. If we don't have an IVRS
276 * reported alias, use the PCI reported alias. In that case we may
277 * still need to initialize the rlookup and dev_table entries if the
278 * alias is to a non-existent device.
279 */
280 if (ivrs_alias == devid) {
281 if (!amd_iommu_rlookup_table[pci_alias]) {
282 amd_iommu_rlookup_table[pci_alias] =
283 amd_iommu_rlookup_table[devid];
284 memcpy(amd_iommu_dev_table[pci_alias].data,
285 amd_iommu_dev_table[devid].data,
286 sizeof(amd_iommu_dev_table[pci_alias].data));
287 }
288
289 return pci_alias;
290 }
291
292 pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
293 "for device %s[%04x:%04x], kernel reported alias "
294 "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
295 PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
296 PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
297 PCI_FUNC(pci_alias));
298
299 /*
300 * If we don't have a PCI DMA alias and the IVRS alias is on the same
301 * bus, then the IVRS table may know about a quirk that we don't.
302 */
303 if (pci_alias == devid &&
304 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
Linus Torvalds7afd16f2016-05-19 13:10:54 -0700305 pci_add_dma_alias(pdev, ivrs_alias & 0xff);
Joerg Roedele3156042016-04-08 15:12:24 +0200306 pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
307 PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
308 dev_name(dev));
309 }
310
311 return ivrs_alias;
312}
313
Joerg Roedel3b03bb72011-06-09 18:53:25 +0200314static struct iommu_dev_data *find_dev_data(u16 devid)
315{
316 struct iommu_dev_data *dev_data;
317
318 dev_data = search_dev_data(devid);
319
320 if (dev_data == NULL)
321 dev_data = alloc_dev_data(devid);
322
323 return dev_data;
324}
325
Joerg Roedel657cbb62009-11-23 15:26:46 +0100326static struct iommu_dev_data *get_dev_data(struct device *dev)
327{
328 return dev->archdata.iommu;
329}
330
Wan Zongshunb097d112016-04-01 09:06:04 -0400331/*
332* Find or create an IOMMU group for a acpihid device.
333*/
334static struct iommu_group *acpihid_device_group(struct device *dev)
335{
336 struct acpihid_map_entry *p, *entry = NULL;
Dan Carpenter2d8e1f02016-04-11 10:14:46 +0300337 int devid;
Wan Zongshunb097d112016-04-01 09:06:04 -0400338
339 devid = get_acpihid_device_id(dev, &entry);
340 if (devid < 0)
341 return ERR_PTR(devid);
342
343 list_for_each_entry(p, &acpihid_map, list) {
344 if ((devid == p->devid) && p->group)
345 entry->group = p->group;
346 }
347
348 if (!entry->group)
349 entry->group = generic_device_group(dev);
350
351 return entry->group;
352}
353
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100354static bool pci_iommuv2_capable(struct pci_dev *pdev)
355{
356 static const int caps[] = {
357 PCI_EXT_CAP_ID_ATS,
Joerg Roedel46277b72011-12-07 14:34:02 +0100358 PCI_EXT_CAP_ID_PRI,
359 PCI_EXT_CAP_ID_PASID,
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100360 };
361 int i, pos;
362
363 for (i = 0; i < 3; ++i) {
364 pos = pci_find_ext_capability(pdev, caps[i]);
365 if (pos == 0)
366 return false;
367 }
368
369 return true;
370}
371
Joerg Roedel6a113dd2011-12-01 12:04:58 +0100372static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
373{
374 struct iommu_dev_data *dev_data;
375
376 dev_data = get_dev_data(&pdev->dev);
377
378 return dev_data->errata & (1 << erratum) ? true : false;
379}
380
Joerg Roedel71c70982009-11-24 16:43:06 +0100381/*
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100382 * This function checks if the driver got a valid device from the caller to
383 * avoid dereferencing invalid pointers.
384 */
385static bool check_device(struct device *dev)
386{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400387 int devid;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100388
389 if (!dev || !dev->dma_mask)
390 return false;
391
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100392 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200393 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400394 return false;
Joerg Roedel98fc5a62009-11-24 17:19:23 +0100395
396 /* Out of our scope? */
397 if (devid > amd_iommu_last_bdf)
398 return false;
399
400 if (amd_iommu_rlookup_table[devid] == NULL)
401 return false;
402
403 return true;
404}
405
Alex Williamson25b11ce2014-09-19 10:03:13 -0600406static void init_iommu_group(struct device *dev)
Alex Williamson2851db22012-10-08 22:49:41 -0600407{
Alex Williamson2851db22012-10-08 22:49:41 -0600408 struct iommu_group *group;
Alex Williamson2851db22012-10-08 22:49:41 -0600409
Alex Williamson65d53522014-07-03 09:51:30 -0600410 group = iommu_group_get_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200411 if (IS_ERR(group))
412 return;
413
Joerg Roedel0bb6e242015-05-28 18:41:40 +0200414 iommu_group_put(group);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600415}
416
417static int iommu_init_device(struct device *dev)
418{
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600419 struct iommu_dev_data *dev_data;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400420 int devid;
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600421
422 if (dev->archdata.iommu)
423 return 0;
424
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400425 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200426 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400427 return devid;
428
429 dev_data = find_dev_data(devid);
Alex Williamsoneb9c9522012-10-08 22:49:35 -0600430 if (!dev_data)
431 return -ENOMEM;
432
Joerg Roedele3156042016-04-08 15:12:24 +0200433 dev_data->alias = get_alias(dev);
434
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400435 if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100436 struct amd_iommu *iommu;
437
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -0400438 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100439 dev_data->iommu_v2 = iommu->is_iommu_v2;
440 }
441
Joerg Roedel657cbb62009-11-23 15:26:46 +0100442 dev->archdata.iommu = dev_data;
443
Alex Williamson066f2e92014-06-12 16:12:37 -0600444 iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
445 dev);
446
Joerg Roedel657cbb62009-11-23 15:26:46 +0100447 return 0;
448}
449
Joerg Roedel26018872011-06-06 16:50:14 +0200450static void iommu_ignore_device(struct device *dev)
451{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400452 u16 alias;
453 int devid;
Joerg Roedel26018872011-06-06 16:50:14 +0200454
455 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200456 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400457 return;
458
Joerg Roedele3156042016-04-08 15:12:24 +0200459 alias = get_alias(dev);
Joerg Roedel26018872011-06-06 16:50:14 +0200460
461 memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
462 memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
463
464 amd_iommu_rlookup_table[devid] = NULL;
465 amd_iommu_rlookup_table[alias] = NULL;
466}
467
Joerg Roedel657cbb62009-11-23 15:26:46 +0100468static void iommu_uninit_device(struct device *dev)
469{
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400470 int devid;
471 struct iommu_dev_data *dev_data;
Alex Williamsonc1931092014-07-03 09:51:24 -0600472
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400473 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +0200474 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -0400475 return;
476
477 dev_data = search_dev_data(devid);
Alex Williamsonc1931092014-07-03 09:51:24 -0600478 if (!dev_data)
479 return;
480
Joerg Roedelb6809ee2016-02-26 16:48:59 +0100481 if (dev_data->domain)
482 detach_device(dev);
483
Alex Williamson066f2e92014-06-12 16:12:37 -0600484 iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
485 dev);
486
Alex Williamson9dcd6132012-05-30 14:19:07 -0600487 iommu_group_remove_device(dev);
488
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200489 /* Remove dma-ops */
490 dev->archdata.dma_ops = NULL;
491
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200492 /*
Alex Williamsonc1931092014-07-03 09:51:24 -0600493 * We keep dev_data around for unplugged devices and reuse it when the
494 * device is re-plugged - not doing so would introduce a ton of races.
Joerg Roedel8fa5f802011-06-09 12:24:45 +0200495 */
Joerg Roedel657cbb62009-11-23 15:26:46 +0100496}
Joerg Roedelb7cc9552009-12-10 11:03:39 +0100497
Joerg Roedel431b2a22008-07-11 17:14:22 +0200498/****************************************************************************
499 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200500 * Interrupt handling functions
501 *
502 ****************************************************************************/
503
Joerg Roedele3e59872009-09-03 14:02:10 +0200504static void dump_dte_entry(u16 devid)
505{
506 int i;
507
Joerg Roedelee6c2862011-11-09 12:06:03 +0100508 for (i = 0; i < 4; ++i)
509 pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
Joerg Roedele3e59872009-09-03 14:02:10 +0200510 amd_iommu_dev_table[devid].data[i]);
511}
512
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200513static void dump_command(unsigned long phys_addr)
514{
515 struct iommu_cmd *cmd = phys_to_virt(phys_addr);
516 int i;
517
518 for (i = 0; i < 4; ++i)
519 pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
520}
521
Joerg Roedela345b232009-09-03 15:01:43 +0200522static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Joerg Roedel90008ee2008-09-09 16:41:05 +0200523{
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200524 int type, devid, domid, flags;
525 volatile u32 *event = __evt;
526 int count = 0;
527 u64 address;
528
529retry:
530 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
531 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
532 domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
533 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
534 address = (u64)(((u64)event[3]) << 32) | event[2];
535
536 if (type == 0) {
537 /* Did we hit the erratum? */
538 if (++count == LOOP_TIMEOUT) {
539 pr_err("AMD-Vi: No event written to event log\n");
540 return;
541 }
542 udelay(1);
543 goto retry;
544 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200545
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200546 printk(KERN_ERR "AMD-Vi: Event logged [");
Joerg Roedel90008ee2008-09-09 16:41:05 +0200547
548 switch (type) {
549 case EVENT_TYPE_ILL_DEV:
550 printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
551 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700552 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200553 address, flags);
Joerg Roedele3e59872009-09-03 14:02:10 +0200554 dump_dte_entry(devid);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200555 break;
556 case EVENT_TYPE_IO_FAULT:
557 printk("IO_PAGE_FAULT device=%02x:%02x.%x "
558 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700559 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200560 domid, address, flags);
561 break;
562 case EVENT_TYPE_DEV_TAB_ERR:
563 printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
564 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700565 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200566 address, flags);
567 break;
568 case EVENT_TYPE_PAGE_TAB_ERR:
569 printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
570 "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700571 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200572 domid, address, flags);
573 break;
574 case EVENT_TYPE_ILL_CMD:
575 printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
Joerg Roedel945b4ac2009-09-03 14:25:02 +0200576 dump_command(address);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200577 break;
578 case EVENT_TYPE_CMD_HARD_ERR:
579 printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
580 "flags=0x%04x]\n", address, flags);
581 break;
582 case EVENT_TYPE_IOTLB_INV_TO:
583 printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
584 "address=0x%016llx]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700585 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200586 address);
587 break;
588 case EVENT_TYPE_INV_DEV_REQ:
589 printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
590 "address=0x%016llx flags=0x%04x]\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700591 PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
Joerg Roedel90008ee2008-09-09 16:41:05 +0200592 address, flags);
593 break;
594 default:
595 printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
596 }
Joerg Roedel3d06fca2012-04-12 14:12:00 +0200597
598 memset(__evt, 0, 4 * sizeof(u32));
Joerg Roedel90008ee2008-09-09 16:41:05 +0200599}
600
601static void iommu_poll_events(struct amd_iommu *iommu)
602{
603 u32 head, tail;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200604
605 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
606 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
607
608 while (head != tail) {
Joerg Roedela345b232009-09-03 15:01:43 +0200609 iommu_print_event(iommu, iommu->evt_buf + head);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200610 head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
Joerg Roedel90008ee2008-09-09 16:41:05 +0200611 }
612
613 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200614}
615
Joerg Roedeleee53532012-06-01 15:20:23 +0200616static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100617{
618 struct amd_iommu_fault fault;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100619
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100620 if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
621 pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
622 return;
623 }
624
625 fault.address = raw[1];
626 fault.pasid = PPR_PASID(raw[0]);
627 fault.device_id = PPR_DEVID(raw[0]);
628 fault.tag = PPR_TAG(raw[0]);
629 fault.flags = PPR_FLAGS(raw[0]);
630
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100631 atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
632}
633
634static void iommu_poll_ppr_log(struct amd_iommu *iommu)
635{
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100636 u32 head, tail;
637
638 if (iommu->ppr_log == NULL)
639 return;
640
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100641 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
642 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
643
644 while (head != tail) {
Joerg Roedeleee53532012-06-01 15:20:23 +0200645 volatile u64 *raw;
646 u64 entry[2];
647 int i;
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100648
Joerg Roedeleee53532012-06-01 15:20:23 +0200649 raw = (u64 *)(iommu->ppr_log + head);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100650
Joerg Roedeleee53532012-06-01 15:20:23 +0200651 /*
652 * Hardware bug: Interrupt may arrive before the entry is
653 * written to memory. If this happens we need to wait for the
654 * entry to arrive.
655 */
656 for (i = 0; i < LOOP_TIMEOUT; ++i) {
657 if (PPR_REQ_TYPE(raw[0]) != 0)
658 break;
659 udelay(1);
660 }
661
662 /* Avoid memcpy function-call overhead */
663 entry[0] = raw[0];
664 entry[1] = raw[1];
665
666 /*
667 * To detect the hardware bug we need to clear the entry
668 * back to zero.
669 */
670 raw[0] = raw[1] = 0UL;
671
672 /* Update head pointer of hardware ring-buffer */
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100673 head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
674 writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedeleee53532012-06-01 15:20:23 +0200675
Joerg Roedeleee53532012-06-01 15:20:23 +0200676 /* Handle PPR entry */
677 iommu_handle_ppr_entry(iommu, entry);
678
Joerg Roedeleee53532012-06-01 15:20:23 +0200679 /* Refresh ring-buffer information */
680 head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100681 tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
682 }
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100683}
684
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200685irqreturn_t amd_iommu_int_thread(int irq, void *data)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200686{
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500687 struct amd_iommu *iommu = (struct amd_iommu *) data;
688 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel90008ee2008-09-09 16:41:05 +0200689
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -0500690 while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) {
691 /* Enable EVT and PPR interrupts again */
692 writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK),
693 iommu->mmio_base + MMIO_STATUS_OFFSET);
694
695 if (status & MMIO_STATUS_EVT_INT_MASK) {
696 pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
697 iommu_poll_events(iommu);
698 }
699
700 if (status & MMIO_STATUS_PPR_INT_MASK) {
701 pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
702 iommu_poll_ppr_log(iommu);
703 }
704
705 /*
706 * Hardware bug: ERBT1312
707 * When re-enabling interrupt (by writing 1
708 * to clear the bit), the hardware might also try to set
709 * the interrupt bit in the event status register.
710 * In this scenario, the bit will be set, and disable
711 * subsequent interrupts.
712 *
713 * Workaround: The IOMMU driver should read back the
714 * status register and check if the interrupt bits are cleared.
715 * If not, driver will need to go through the interrupt handler
716 * again and re-clear the bits
717 */
718 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
Joerg Roedel72e1dcc2011-11-10 19:13:51 +0100719 }
Joerg Roedel90008ee2008-09-09 16:41:05 +0200720 return IRQ_HANDLED;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200721}
722
Joerg Roedel72fe00f2011-05-10 10:50:42 +0200723irqreturn_t amd_iommu_int_handler(int irq, void *data)
724{
725 return IRQ_WAKE_THREAD;
726}
727
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200728/****************************************************************************
729 *
Joerg Roedel431b2a22008-07-11 17:14:22 +0200730 * IOMMU command queuing functions
731 *
732 ****************************************************************************/
733
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200734static int wait_on_sem(volatile u64 *sem)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200735{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200736 int i = 0;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200737
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200738 while (*sem == 0 && i < LOOP_TIMEOUT) {
739 udelay(1);
740 i += 1;
741 }
742
743 if (i == LOOP_TIMEOUT) {
744 pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
745 return -EIO;
746 }
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200747
748 return 0;
749}
750
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200751static void copy_cmd_to_buffer(struct amd_iommu *iommu,
752 struct iommu_cmd *cmd,
753 u32 tail)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200754{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200755 u8 *target;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200756
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200757 target = iommu->cmd_buf + tail;
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200758 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200759
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200760 /* Copy command to buffer */
761 memcpy(target, cmd, sizeof(*cmd));
762
763 /* Tell the IOMMU about it */
764 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
765}
766
Joerg Roedel815b33f2011-04-06 17:26:49 +0200767static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
Joerg Roedelded46732011-04-06 10:53:48 +0200768{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200769 WARN_ON(address & 0x7ULL);
770
Joerg Roedelded46732011-04-06 10:53:48 +0200771 memset(cmd, 0, sizeof(*cmd));
Joerg Roedel815b33f2011-04-06 17:26:49 +0200772 cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
773 cmd->data[1] = upper_32_bits(__pa(address));
774 cmd->data[2] = 1;
Joerg Roedelded46732011-04-06 10:53:48 +0200775 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
776}
777
Joerg Roedel94fe79e2011-04-06 11:07:21 +0200778static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
779{
780 memset(cmd, 0, sizeof(*cmd));
781 cmd->data[0] = devid;
782 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
783}
784
Joerg Roedel11b64022011-04-06 11:49:28 +0200785static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
786 size_t size, u16 domid, int pde)
787{
788 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100789 bool s;
Joerg Roedel11b64022011-04-06 11:49:28 +0200790
791 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100792 s = false;
Joerg Roedel11b64022011-04-06 11:49:28 +0200793
794 if (pages > 1) {
795 /*
796 * If we have to flush more than one page, flush all
797 * TLB entries for this domain
798 */
799 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100800 s = true;
Joerg Roedel11b64022011-04-06 11:49:28 +0200801 }
802
803 address &= PAGE_MASK;
804
805 memset(cmd, 0, sizeof(*cmd));
806 cmd->data[1] |= domid;
807 cmd->data[2] = lower_32_bits(address);
808 cmd->data[3] = upper_32_bits(address);
809 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
810 if (s) /* size bit - we flush more than one 4kb page */
811 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
Frank Arnolddf805ab2012-08-27 19:21:04 +0200812 if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
Joerg Roedel11b64022011-04-06 11:49:28 +0200813 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
814}
815
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200816static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
817 u64 address, size_t size)
818{
819 u64 pages;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100820 bool s;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200821
822 pages = iommu_num_pages(address, size, PAGE_SIZE);
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100823 s = false;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200824
825 if (pages > 1) {
826 /*
827 * If we have to flush more than one page, flush all
828 * TLB entries for this domain
829 */
830 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
Quentin Lambertae0cbbb2015-02-04 11:40:07 +0100831 s = true;
Joerg Roedelcb41ed82011-04-05 11:00:53 +0200832 }
833
834 address &= PAGE_MASK;
835
836 memset(cmd, 0, sizeof(*cmd));
837 cmd->data[0] = devid;
838 cmd->data[0] |= (qdep & 0xff) << 24;
839 cmd->data[1] = devid;
840 cmd->data[2] = lower_32_bits(address);
841 cmd->data[3] = upper_32_bits(address);
842 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
843 if (s)
844 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
845}
846
Joerg Roedel22e266c2011-11-21 15:59:08 +0100847static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
848 u64 address, bool size)
849{
850 memset(cmd, 0, sizeof(*cmd));
851
852 address &= ~(0xfffULL);
853
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600854 cmd->data[0] = pasid;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100855 cmd->data[1] = domid;
856 cmd->data[2] = lower_32_bits(address);
857 cmd->data[3] = upper_32_bits(address);
858 cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
859 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
860 if (size)
861 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
862 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
863}
864
865static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
866 int qdep, u64 address, bool size)
867{
868 memset(cmd, 0, sizeof(*cmd));
869
870 address &= ~(0xfffULL);
871
872 cmd->data[0] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600873 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100874 cmd->data[0] |= (qdep & 0xff) << 24;
875 cmd->data[1] = devid;
Jay Cornwalle8d2d822014-02-26 15:49:31 -0600876 cmd->data[1] |= (pasid & 0xff) << 16;
Joerg Roedel22e266c2011-11-21 15:59:08 +0100877 cmd->data[2] = lower_32_bits(address);
878 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
879 cmd->data[3] = upper_32_bits(address);
880 if (size)
881 cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
882 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
883}
884
Joerg Roedelc99afa22011-11-21 18:19:25 +0100885static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
886 int status, int tag, bool gn)
887{
888 memset(cmd, 0, sizeof(*cmd));
889
890 cmd->data[0] = devid;
891 if (gn) {
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600892 cmd->data[1] = pasid;
Joerg Roedelc99afa22011-11-21 18:19:25 +0100893 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
894 }
895 cmd->data[3] = tag & 0x1ff;
896 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
897
898 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
899}
900
Joerg Roedel58fc7f12011-04-11 11:13:24 +0200901static void build_inv_all(struct iommu_cmd *cmd)
902{
903 memset(cmd, 0, sizeof(*cmd));
904 CMD_SET_TYPE(cmd, CMD_INV_ALL);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200905}
906
Joerg Roedel7ef27982012-06-21 16:46:04 +0200907static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
908{
909 memset(cmd, 0, sizeof(*cmd));
910 cmd->data[0] = devid;
911 CMD_SET_TYPE(cmd, CMD_INV_IRT);
912}
913
Joerg Roedel431b2a22008-07-11 17:14:22 +0200914/*
Joerg Roedelb6c02712008-06-26 21:27:53 +0200915 * Writes the command to the IOMMUs command buffer and informs the
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200916 * hardware about the new command.
Joerg Roedel431b2a22008-07-11 17:14:22 +0200917 */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200918static int iommu_queue_command_sync(struct amd_iommu *iommu,
919 struct iommu_cmd *cmd,
920 bool sync)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200921{
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200922 u32 left, tail, head, next_tail;
Joerg Roedel815b33f2011-04-06 17:26:49 +0200923 unsigned long flags;
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200924
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200925again:
Joerg Roedel815b33f2011-04-06 17:26:49 +0200926 spin_lock_irqsave(&iommu->lock, flags);
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200927
928 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
929 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200930 next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
931 left = (head - next_tail) % CMD_BUFFER_SIZE;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200932
933 if (left <= 2) {
934 struct iommu_cmd sync_cmd;
935 volatile u64 sem = 0;
936 int ret;
937
938 build_completion_wait(&sync_cmd, (u64)&sem);
939 copy_cmd_to_buffer(iommu, &sync_cmd, tail);
940
941 spin_unlock_irqrestore(&iommu->lock, flags);
942
943 if ((ret = wait_on_sem(&sem)) != 0)
944 return ret;
945
946 goto again;
Joerg Roedel136f78a2008-07-11 17:14:27 +0200947 }
948
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200949 copy_cmd_to_buffer(iommu, cmd, tail);
Joerg Roedel519c31b2008-08-14 19:55:15 +0200950
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200951 /* We need to sync now to make sure all commands are processed */
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200952 iommu->need_sync = sync;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200953
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200954 spin_unlock_irqrestore(&iommu->lock, flags);
955
Joerg Roedel815b33f2011-04-06 17:26:49 +0200956 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100957}
958
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200959static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
960{
961 return iommu_queue_command_sync(iommu, cmd, true);
962}
963
Joerg Roedel8d201962008-12-02 20:34:41 +0100964/*
965 * This function queues a completion wait command into the command
966 * buffer of an IOMMU
967 */
Joerg Roedel8d201962008-12-02 20:34:41 +0100968static int iommu_completion_wait(struct amd_iommu *iommu)
969{
Joerg Roedel815b33f2011-04-06 17:26:49 +0200970 struct iommu_cmd cmd;
971 volatile u64 sem = 0;
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200972 int ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100973
974 if (!iommu->need_sync)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200975 return 0;
Joerg Roedel8d201962008-12-02 20:34:41 +0100976
Joerg Roedel815b33f2011-04-06 17:26:49 +0200977 build_completion_wait(&cmd, (u64)&sem);
Joerg Roedel8d201962008-12-02 20:34:41 +0100978
Joerg Roedelf1ca1512011-09-02 14:10:32 +0200979 ret = iommu_queue_command_sync(iommu, &cmd, false);
Joerg Roedel8d201962008-12-02 20:34:41 +0100980 if (ret)
Joerg Roedel815b33f2011-04-06 17:26:49 +0200981 return ret;
Joerg Roedel8d201962008-12-02 20:34:41 +0100982
Joerg Roedelac0ea6e2011-04-06 18:38:20 +0200983 return wait_on_sem(&sem);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200984}
985
Joerg Roedeld8c13082011-04-06 18:51:26 +0200986static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200987{
988 struct iommu_cmd cmd;
989
Joerg Roedeld8c13082011-04-06 18:51:26 +0200990 build_inv_dte(&cmd, devid);
Joerg Roedela19ae1e2008-06-26 21:27:55 +0200991
Joerg Roedeld8c13082011-04-06 18:51:26 +0200992 return iommu_queue_command(iommu, &cmd);
993}
994
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +0200995static void iommu_flush_dte_all(struct amd_iommu *iommu)
996{
997 u32 devid;
998
999 for (devid = 0; devid <= 0xffff; ++devid)
1000 iommu_flush_dte(iommu, devid);
1001
1002 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001003}
1004
1005/*
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001006 * This function uses heavy locking and may disable irqs for some time. But
1007 * this is no issue because it is only called during resume.
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001008 */
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001009static void iommu_flush_tlb_all(struct amd_iommu *iommu)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001010{
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001011 u32 dom_id;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001012
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001013 for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
1014 struct iommu_cmd cmd;
1015 build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1016 dom_id, 1);
1017 iommu_queue_command(iommu, &cmd);
1018 }
Joerg Roedel431b2a22008-07-11 17:14:22 +02001019
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001020 iommu_completion_wait(iommu);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001021}
1022
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001023static void iommu_flush_all(struct amd_iommu *iommu)
1024{
1025 struct iommu_cmd cmd;
1026
1027 build_inv_all(&cmd);
1028
1029 iommu_queue_command(iommu, &cmd);
1030 iommu_completion_wait(iommu);
1031}
1032
Joerg Roedel7ef27982012-06-21 16:46:04 +02001033static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
1034{
1035 struct iommu_cmd cmd;
1036
1037 build_inv_irt(&cmd, devid);
1038
1039 iommu_queue_command(iommu, &cmd);
1040}
1041
1042static void iommu_flush_irt_all(struct amd_iommu *iommu)
1043{
1044 u32 devid;
1045
1046 for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
1047 iommu_flush_irt(iommu, devid);
1048
1049 iommu_completion_wait(iommu);
1050}
1051
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001052void iommu_flush_all_caches(struct amd_iommu *iommu)
1053{
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001054 if (iommu_feature(iommu, FEATURE_IA)) {
1055 iommu_flush_all(iommu);
1056 } else {
1057 iommu_flush_dte_all(iommu);
Joerg Roedel7ef27982012-06-21 16:46:04 +02001058 iommu_flush_irt_all(iommu);
Joerg Roedel58fc7f12011-04-11 11:13:24 +02001059 iommu_flush_tlb_all(iommu);
1060 }
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001061}
1062
Joerg Roedel431b2a22008-07-11 17:14:22 +02001063/*
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001064 * Command send function for flushing on-device TLB
1065 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001066static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1067 u64 address, size_t size)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001068{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001069 struct amd_iommu *iommu;
1070 struct iommu_cmd cmd;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001071 int qdep;
1072
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001073 qdep = dev_data->ats.qdep;
1074 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001075
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001076 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001077
1078 return iommu_queue_command(iommu, &cmd);
1079}
1080
1081/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001082 * Command send function for invalidating a device table entry
1083 */
Joerg Roedel6c542042011-06-09 17:07:31 +02001084static int device_flush_dte(struct iommu_dev_data *dev_data)
Joerg Roedel3fa43652009-11-26 15:04:38 +01001085{
1086 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001087 u16 alias;
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001088 int ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001089
Joerg Roedel6c542042011-06-09 17:07:31 +02001090 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001091 alias = dev_data->alias;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001092
Joerg Roedelf62dda62011-06-09 12:55:35 +02001093 ret = iommu_flush_dte(iommu, dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001094 if (!ret && alias != dev_data->devid)
1095 ret = iommu_flush_dte(iommu, alias);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001096 if (ret)
1097 return ret;
1098
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001099 if (dev_data->ats.enabled)
Joerg Roedel6c542042011-06-09 17:07:31 +02001100 ret = device_flush_iotlb(dev_data, 0, ~0UL);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001101
1102 return ret;
Joerg Roedel3fa43652009-11-26 15:04:38 +01001103}
1104
Joerg Roedel431b2a22008-07-11 17:14:22 +02001105/*
1106 * TLB invalidation function which is called from the mapping functions.
1107 * It invalidates a single PTE if the range to flush is within a single
1108 * page. Otherwise it flushes the whole TLB of the IOMMU.
1109 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001110static void __domain_flush_pages(struct protection_domain *domain,
1111 u64 address, size_t size, int pde)
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001112{
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001113 struct iommu_dev_data *dev_data;
Joerg Roedel11b64022011-04-06 11:49:28 +02001114 struct iommu_cmd cmd;
1115 int ret = 0, i;
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001116
Joerg Roedel11b64022011-04-06 11:49:28 +02001117 build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
Joerg Roedel999ba412008-07-03 19:35:08 +02001118
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001119 for (i = 0; i < amd_iommus_present; ++i) {
1120 if (!domain->dev_iommu[i])
1121 continue;
1122
1123 /*
1124 * Devices of this domain are behind this IOMMU
1125 * We need a TLB flush
1126 */
Joerg Roedel11b64022011-04-06 11:49:28 +02001127 ret |= iommu_queue_command(amd_iommus[i], &cmd);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001128 }
1129
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001130 list_for_each_entry(dev_data, &domain->dev_list, list) {
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001131
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001132 if (!dev_data->ats.enabled)
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001133 continue;
1134
Joerg Roedel6c542042011-06-09 17:07:31 +02001135 ret |= device_flush_iotlb(dev_data, address, size);
Joerg Roedelcb41ed82011-04-05 11:00:53 +02001136 }
1137
Joerg Roedel11b64022011-04-06 11:49:28 +02001138 WARN_ON(ret);
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001139}
1140
Joerg Roedel17b124b2011-04-06 18:01:35 +02001141static void domain_flush_pages(struct protection_domain *domain,
1142 u64 address, size_t size)
Joerg Roedel6de8ad92009-11-23 18:30:32 +01001143{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001144 __domain_flush_pages(domain, address, size, 0);
Joerg Roedela19ae1e2008-06-26 21:27:55 +02001145}
Joerg Roedelb6c02712008-06-26 21:27:53 +02001146
Joerg Roedel1c655772008-09-04 18:40:05 +02001147/* Flush the whole IO/TLB for a given protection domain */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001148static void domain_flush_tlb(struct protection_domain *domain)
Joerg Roedel1c655772008-09-04 18:40:05 +02001149{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001150 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
Joerg Roedel1c655772008-09-04 18:40:05 +02001151}
1152
Chris Wright42a49f92009-06-15 15:42:00 +02001153/* Flush the whole IO/TLB for a given protection domain - including PDE */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001154static void domain_flush_tlb_pde(struct protection_domain *domain)
Chris Wright42a49f92009-06-15 15:42:00 +02001155{
Joerg Roedel17b124b2011-04-06 18:01:35 +02001156 __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
1157}
1158
1159static void domain_flush_complete(struct protection_domain *domain)
Joerg Roedelb6c02712008-06-26 21:27:53 +02001160{
1161 int i;
1162
1163 for (i = 0; i < amd_iommus_present; ++i) {
1164 if (!domain->dev_iommu[i])
1165 continue;
1166
1167 /*
1168 * Devices of this domain are behind this IOMMU
1169 * We need to wait for completion of all commands.
1170 */
1171 iommu_completion_wait(amd_iommus[i]);
1172 }
1173}
1174
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001175
Joerg Roedel43f49602008-12-02 21:01:12 +01001176/*
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001177 * This function flushes the DTEs for all devices in domain
Joerg Roedel43f49602008-12-02 21:01:12 +01001178 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001179static void domain_flush_devices(struct protection_domain *domain)
Joerg Roedelbfd1be12009-05-05 15:33:57 +02001180{
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001181 struct iommu_dev_data *dev_data;
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001182
1183 list_for_each_entry(dev_data, &domain->dev_list, list)
Joerg Roedel6c542042011-06-09 17:07:31 +02001184 device_flush_dte(dev_data);
Joerg Roedelb00d3bc2009-11-26 15:35:33 +01001185}
1186
Joerg Roedel431b2a22008-07-11 17:14:22 +02001187/****************************************************************************
1188 *
1189 * The functions below are used the create the page table mappings for
1190 * unity mapped regions.
1191 *
1192 ****************************************************************************/
1193
1194/*
Joerg Roedel308973d2009-11-24 17:43:32 +01001195 * This function is used to add another level to an IO page table. Adding
1196 * another level increases the size of the address space by 9 bits to a size up
1197 * to 64 bits.
1198 */
1199static bool increase_address_space(struct protection_domain *domain,
1200 gfp_t gfp)
1201{
1202 u64 *pte;
1203
1204 if (domain->mode == PAGE_MODE_6_LEVEL)
1205 /* address space already 64 bit large */
1206 return false;
1207
1208 pte = (void *)get_zeroed_page(gfp);
1209 if (!pte)
1210 return false;
1211
1212 *pte = PM_LEVEL_PDE(domain->mode,
1213 virt_to_phys(domain->pt_root));
1214 domain->pt_root = pte;
1215 domain->mode += 1;
1216 domain->updated = true;
1217
1218 return true;
1219}
1220
1221static u64 *alloc_pte(struct protection_domain *domain,
1222 unsigned long address,
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001223 unsigned long page_size,
Joerg Roedel308973d2009-11-24 17:43:32 +01001224 u64 **pte_page,
1225 gfp_t gfp)
1226{
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001227 int level, end_lvl;
Joerg Roedel308973d2009-11-24 17:43:32 +01001228 u64 *pte, *page;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001229
1230 BUG_ON(!is_power_of_2(page_size));
Joerg Roedel308973d2009-11-24 17:43:32 +01001231
1232 while (address > PM_LEVEL_SIZE(domain->mode))
1233 increase_address_space(domain, gfp);
1234
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001235 level = domain->mode - 1;
1236 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1237 address = PAGE_SIZE_ALIGN(address, page_size);
1238 end_lvl = PAGE_SIZE_LEVEL(page_size);
Joerg Roedel308973d2009-11-24 17:43:32 +01001239
1240 while (level > end_lvl) {
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001241 u64 __pte, __npte;
1242
1243 __pte = *pte;
1244
1245 if (!IOMMU_PTE_PRESENT(__pte)) {
Joerg Roedel308973d2009-11-24 17:43:32 +01001246 page = (u64 *)get_zeroed_page(gfp);
1247 if (!page)
1248 return NULL;
Joerg Roedel7bfa5bd2015-12-21 19:07:50 +01001249
1250 __npte = PM_LEVEL_PDE(level, virt_to_phys(page));
1251
1252 if (cmpxchg64(pte, __pte, __npte)) {
1253 free_page((unsigned long)page);
1254 continue;
1255 }
Joerg Roedel308973d2009-11-24 17:43:32 +01001256 }
1257
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001258 /* No level skipping support yet */
1259 if (PM_PTE_LEVEL(*pte) != level)
1260 return NULL;
1261
Joerg Roedel308973d2009-11-24 17:43:32 +01001262 level -= 1;
1263
1264 pte = IOMMU_PTE_PAGE(*pte);
1265
1266 if (pte_page && level == end_lvl)
1267 *pte_page = pte;
1268
1269 pte = &pte[PM_LEVEL_INDEX(level, address)];
1270 }
1271
1272 return pte;
1273}
1274
1275/*
1276 * This function checks if there is a PTE for a given dma address. If
1277 * there is one, it returns the pointer to it.
1278 */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001279static u64 *fetch_pte(struct protection_domain *domain,
1280 unsigned long address,
1281 unsigned long *page_size)
Joerg Roedel308973d2009-11-24 17:43:32 +01001282{
1283 int level;
1284 u64 *pte;
1285
Joerg Roedel24cd7722010-01-19 17:27:39 +01001286 if (address > PM_LEVEL_SIZE(domain->mode))
1287 return NULL;
Joerg Roedel308973d2009-11-24 17:43:32 +01001288
Joerg Roedel3039ca12015-04-01 14:58:48 +02001289 level = domain->mode - 1;
1290 pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
1291 *page_size = PTE_LEVEL_PAGE_SIZE(level);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001292
1293 while (level > 0) {
1294
1295 /* Not Present */
Joerg Roedel308973d2009-11-24 17:43:32 +01001296 if (!IOMMU_PTE_PRESENT(*pte))
1297 return NULL;
1298
Joerg Roedel24cd7722010-01-19 17:27:39 +01001299 /* Large PTE */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001300 if (PM_PTE_LEVEL(*pte) == 7 ||
1301 PM_PTE_LEVEL(*pte) == 0)
1302 break;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001303
1304 /* No level skipping support yet */
1305 if (PM_PTE_LEVEL(*pte) != level)
1306 return NULL;
1307
Joerg Roedel308973d2009-11-24 17:43:32 +01001308 level -= 1;
1309
Joerg Roedel24cd7722010-01-19 17:27:39 +01001310 /* Walk to the next level */
Joerg Roedel3039ca12015-04-01 14:58:48 +02001311 pte = IOMMU_PTE_PAGE(*pte);
1312 pte = &pte[PM_LEVEL_INDEX(level, address)];
1313 *page_size = PTE_LEVEL_PAGE_SIZE(level);
1314 }
1315
1316 if (PM_PTE_LEVEL(*pte) == 0x07) {
1317 unsigned long pte_mask;
1318
1319 /*
1320 * If we have a series of large PTEs, make
1321 * sure to return a pointer to the first one.
1322 */
1323 *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
1324 pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
1325 pte = (u64 *)(((unsigned long)pte) & pte_mask);
Joerg Roedel308973d2009-11-24 17:43:32 +01001326 }
1327
1328 return pte;
1329}
1330
1331/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02001332 * Generic mapping functions. It maps a physical address into a DMA
1333 * address space. It allocates the page table pages if necessary.
1334 * In the future it can be extended to a generic mapping function
1335 * supporting all features of AMD IOMMU page tables like level skipping
1336 * and full 64 bit address spaces.
1337 */
Joerg Roedel38e817f2008-12-02 17:27:52 +01001338static int iommu_map_page(struct protection_domain *dom,
1339 unsigned long bus_addr,
1340 unsigned long phys_addr,
Joerg Roedelb911b892016-07-05 14:29:11 +02001341 unsigned long page_size,
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001342 int prot,
Joerg Roedelb911b892016-07-05 14:29:11 +02001343 gfp_t gfp)
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001344{
Joerg Roedel8bda3092009-05-12 12:02:46 +02001345 u64 __pte, *pte;
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001346 int i, count;
Joerg Roedelabdc5eb2009-09-03 11:33:51 +02001347
Joerg Roedeld4b03662015-04-01 14:58:52 +02001348 BUG_ON(!IS_ALIGNED(bus_addr, page_size));
1349 BUG_ON(!IS_ALIGNED(phys_addr, page_size));
1350
Joerg Roedelbad1cac2009-09-02 16:52:23 +02001351 if (!(prot & IOMMU_PROT_MASK))
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001352 return -EINVAL;
1353
Joerg Roedeld4b03662015-04-01 14:58:52 +02001354 count = PAGE_SIZE_PTE_COUNT(page_size);
Joerg Roedelb911b892016-07-05 14:29:11 +02001355 pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001356
Maurizio Lombardi63eaa752014-09-11 12:28:03 +02001357 if (!pte)
1358 return -ENOMEM;
1359
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001360 for (i = 0; i < count; ++i)
1361 if (IOMMU_PTE_PRESENT(pte[i]))
1362 return -EBUSY;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001363
Joerg Roedeld4b03662015-04-01 14:58:52 +02001364 if (count > 1) {
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001365 __pte = PAGE_SIZE_PTE(phys_addr, page_size);
1366 __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
1367 } else
1368 __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
1369
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001370 if (prot & IOMMU_PROT_IR)
1371 __pte |= IOMMU_PTE_IR;
1372 if (prot & IOMMU_PROT_IW)
1373 __pte |= IOMMU_PTE_IW;
1374
Joerg Roedelcbb9d722010-01-15 14:41:15 +01001375 for (i = 0; i < count; ++i)
1376 pte[i] = __pte;
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001377
Joerg Roedel04bfdd82009-09-02 16:00:23 +02001378 update_domain(dom);
1379
Joerg Roedelbd0e5212008-06-26 21:27:56 +02001380 return 0;
1381}
1382
Joerg Roedel24cd7722010-01-19 17:27:39 +01001383static unsigned long iommu_unmap_page(struct protection_domain *dom,
1384 unsigned long bus_addr,
1385 unsigned long page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001386{
Joerg Roedel71b390e2015-04-01 14:58:49 +02001387 unsigned long long unmapped;
1388 unsigned long unmap_size;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001389 u64 *pte;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001390
Joerg Roedel24cd7722010-01-19 17:27:39 +01001391 BUG_ON(!is_power_of_2(page_size));
1392
1393 unmapped = 0;
1394
1395 while (unmapped < page_size) {
1396
Joerg Roedel71b390e2015-04-01 14:58:49 +02001397 pte = fetch_pte(dom, bus_addr, &unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001398
Joerg Roedel71b390e2015-04-01 14:58:49 +02001399 if (pte) {
1400 int i, count;
Joerg Roedel24cd7722010-01-19 17:27:39 +01001401
Joerg Roedel71b390e2015-04-01 14:58:49 +02001402 count = PAGE_SIZE_PTE_COUNT(unmap_size);
Joerg Roedel24cd7722010-01-19 17:27:39 +01001403 for (i = 0; i < count; i++)
1404 pte[i] = 0ULL;
1405 }
1406
1407 bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
1408 unmapped += unmap_size;
1409 }
1410
Alex Williamson60d0ca32013-06-21 14:33:19 -06001411 BUG_ON(unmapped && !is_power_of_2(unmapped));
Joerg Roedel24cd7722010-01-19 17:27:39 +01001412
1413 return unmapped;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001414}
Joerg Roedeleb74ff62008-12-02 19:59:10 +01001415
Joerg Roedel431b2a22008-07-11 17:14:22 +02001416/****************************************************************************
1417 *
1418 * The next functions belong to the address allocator for the dma_ops
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001419 * interface functions.
Joerg Roedel431b2a22008-07-11 17:14:22 +02001420 *
1421 ****************************************************************************/
Joerg Roedeld3086442008-06-26 21:27:57 +02001422
Joerg Roedel9cabe892009-05-18 16:38:55 +02001423
Joerg Roedel256e4622016-07-05 14:23:01 +02001424static unsigned long dma_ops_alloc_iova(struct device *dev,
1425 struct dma_ops_domain *dma_dom,
1426 unsigned int pages, u64 dma_mask)
Joerg Roedela0f51442015-12-21 16:20:09 +01001427{
Joerg Roedel256e4622016-07-05 14:23:01 +02001428 unsigned long pfn = 0;
Joerg Roedela0f51442015-12-21 16:20:09 +01001429
Joerg Roedel256e4622016-07-05 14:23:01 +02001430 pages = __roundup_pow_of_two(pages);
Joerg Roedela0f51442015-12-21 16:20:09 +01001431
Joerg Roedel256e4622016-07-05 14:23:01 +02001432 if (dma_mask > DMA_BIT_MASK(32))
1433 pfn = alloc_iova_fast(&dma_dom->iovad, pages,
1434 IOVA_PFN(DMA_BIT_MASK(32)));
Joerg Roedel7b5e25b2015-12-22 13:38:12 +01001435
Joerg Roedel256e4622016-07-05 14:23:01 +02001436 if (!pfn)
1437 pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask));
Joerg Roedel60e6a7c2015-12-21 16:53:17 +01001438
Joerg Roedel256e4622016-07-05 14:23:01 +02001439 return (pfn << PAGE_SHIFT);
Joerg Roedela0f51442015-12-21 16:20:09 +01001440}
1441
Joerg Roedel256e4622016-07-05 14:23:01 +02001442static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
1443 unsigned long address,
1444 unsigned int pages)
Joerg Roedel384de722009-05-15 12:30:05 +02001445{
Joerg Roedel256e4622016-07-05 14:23:01 +02001446 pages = __roundup_pow_of_two(pages);
1447 address >>= PAGE_SHIFT;
Joerg Roedel5f6bed52015-12-22 13:34:22 +01001448
Joerg Roedel256e4622016-07-05 14:23:01 +02001449 free_iova_fast(&dma_dom->iovad, address, pages);
Joerg Roedeld3086442008-06-26 21:27:57 +02001450}
1451
Joerg Roedel431b2a22008-07-11 17:14:22 +02001452/****************************************************************************
1453 *
1454 * The next functions belong to the domain allocation. A domain is
1455 * allocated for every IOMMU as the default domain. If device isolation
1456 * is enabled, every device get its own domain. The most important thing
1457 * about domains is the page table mapping the DMA address space they
1458 * contain.
1459 *
1460 ****************************************************************************/
1461
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001462/*
1463 * This function adds a protection domain to the global protection domain list
1464 */
1465static void add_domain_to_list(struct protection_domain *domain)
1466{
1467 unsigned long flags;
1468
1469 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1470 list_add(&domain->list, &amd_iommu_pd_list);
1471 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1472}
1473
1474/*
1475 * This function removes a protection domain to the global
1476 * protection domain list
1477 */
1478static void del_domain_from_list(struct protection_domain *domain)
1479{
1480 unsigned long flags;
1481
1482 spin_lock_irqsave(&amd_iommu_pd_lock, flags);
1483 list_del(&domain->list);
1484 spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
1485}
1486
Joerg Roedelec487d12008-06-26 21:27:58 +02001487static u16 domain_id_alloc(void)
1488{
1489 unsigned long flags;
1490 int id;
1491
1492 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1493 id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
1494 BUG_ON(id == 0);
1495 if (id > 0 && id < MAX_DOMAIN_ID)
1496 __set_bit(id, amd_iommu_pd_alloc_bitmap);
1497 else
1498 id = 0;
1499 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1500
1501 return id;
1502}
1503
Joerg Roedela2acfb72008-12-02 18:28:53 +01001504static void domain_id_free(int id)
1505{
1506 unsigned long flags;
1507
1508 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
1509 if (id > 0 && id < MAX_DOMAIN_ID)
1510 __clear_bit(id, amd_iommu_pd_alloc_bitmap);
1511 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1512}
Joerg Roedela2acfb72008-12-02 18:28:53 +01001513
Joerg Roedel5c34c402013-06-20 20:22:58 +02001514#define DEFINE_FREE_PT_FN(LVL, FN) \
1515static void free_pt_##LVL (unsigned long __pt) \
1516{ \
1517 unsigned long p; \
1518 u64 *pt; \
1519 int i; \
1520 \
1521 pt = (u64 *)__pt; \
1522 \
1523 for (i = 0; i < 512; ++i) { \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001524 /* PTE present? */ \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001525 if (!IOMMU_PTE_PRESENT(pt[i])) \
1526 continue; \
1527 \
Joerg Roedel0b3fff542015-06-18 10:48:34 +02001528 /* Large PTE? */ \
1529 if (PM_PTE_LEVEL(pt[i]) == 0 || \
1530 PM_PTE_LEVEL(pt[i]) == 7) \
1531 continue; \
1532 \
Joerg Roedel5c34c402013-06-20 20:22:58 +02001533 p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
1534 FN(p); \
1535 } \
1536 free_page((unsigned long)pt); \
1537}
1538
1539DEFINE_FREE_PT_FN(l2, free_page)
1540DEFINE_FREE_PT_FN(l3, free_pt_l2)
1541DEFINE_FREE_PT_FN(l4, free_pt_l3)
1542DEFINE_FREE_PT_FN(l5, free_pt_l4)
1543DEFINE_FREE_PT_FN(l6, free_pt_l5)
1544
Joerg Roedel86db2e52008-12-02 18:20:21 +01001545static void free_pagetable(struct protection_domain *domain)
Joerg Roedelec487d12008-06-26 21:27:58 +02001546{
Joerg Roedel5c34c402013-06-20 20:22:58 +02001547 unsigned long root = (unsigned long)domain->pt_root;
Joerg Roedelec487d12008-06-26 21:27:58 +02001548
Joerg Roedel5c34c402013-06-20 20:22:58 +02001549 switch (domain->mode) {
1550 case PAGE_MODE_NONE:
1551 break;
1552 case PAGE_MODE_1_LEVEL:
1553 free_page(root);
1554 break;
1555 case PAGE_MODE_2_LEVEL:
1556 free_pt_l2(root);
1557 break;
1558 case PAGE_MODE_3_LEVEL:
1559 free_pt_l3(root);
1560 break;
1561 case PAGE_MODE_4_LEVEL:
1562 free_pt_l4(root);
1563 break;
1564 case PAGE_MODE_5_LEVEL:
1565 free_pt_l5(root);
1566 break;
1567 case PAGE_MODE_6_LEVEL:
1568 free_pt_l6(root);
1569 break;
1570 default:
1571 BUG();
Joerg Roedelec487d12008-06-26 21:27:58 +02001572 }
Joerg Roedelec487d12008-06-26 21:27:58 +02001573}
1574
Joerg Roedelb16137b2011-11-21 16:50:23 +01001575static void free_gcr3_tbl_level1(u64 *tbl)
1576{
1577 u64 *ptr;
1578 int i;
1579
1580 for (i = 0; i < 512; ++i) {
1581 if (!(tbl[i] & GCR3_VALID))
1582 continue;
1583
1584 ptr = __va(tbl[i] & PAGE_MASK);
1585
1586 free_page((unsigned long)ptr);
1587 }
1588}
1589
1590static void free_gcr3_tbl_level2(u64 *tbl)
1591{
1592 u64 *ptr;
1593 int i;
1594
1595 for (i = 0; i < 512; ++i) {
1596 if (!(tbl[i] & GCR3_VALID))
1597 continue;
1598
1599 ptr = __va(tbl[i] & PAGE_MASK);
1600
1601 free_gcr3_tbl_level1(ptr);
1602 }
1603}
1604
Joerg Roedel52815b72011-11-17 17:24:28 +01001605static void free_gcr3_table(struct protection_domain *domain)
1606{
Joerg Roedelb16137b2011-11-21 16:50:23 +01001607 if (domain->glx == 2)
1608 free_gcr3_tbl_level2(domain->gcr3_tbl);
1609 else if (domain->glx == 1)
1610 free_gcr3_tbl_level1(domain->gcr3_tbl);
Joerg Roedel23d3a982015-08-13 11:15:13 +02001611 else
1612 BUG_ON(domain->glx != 0);
Joerg Roedelb16137b2011-11-21 16:50:23 +01001613
Joerg Roedel52815b72011-11-17 17:24:28 +01001614 free_page((unsigned long)domain->gcr3_tbl);
1615}
1616
Joerg Roedel431b2a22008-07-11 17:14:22 +02001617/*
1618 * Free a domain, only used if something went wrong in the
1619 * allocation path and we need to free an already allocated page table
1620 */
Joerg Roedelec487d12008-06-26 21:27:58 +02001621static void dma_ops_domain_free(struct dma_ops_domain *dom)
1622{
1623 if (!dom)
1624 return;
1625
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001626 del_domain_from_list(&dom->domain);
1627
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001628 put_iova_domain(&dom->iovad);
1629
Joerg Roedel86db2e52008-12-02 18:20:21 +01001630 free_pagetable(&dom->domain);
Joerg Roedelec487d12008-06-26 21:27:58 +02001631
Joerg Roedelec487d12008-06-26 21:27:58 +02001632 kfree(dom);
1633}
1634
Joerg Roedel431b2a22008-07-11 17:14:22 +02001635/*
1636 * Allocates a new protection domain usable for the dma_ops functions.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001637 * It also initializes the page table and the address allocator data
Joerg Roedel431b2a22008-07-11 17:14:22 +02001638 * structures required for the dma_ops interface
1639 */
Joerg Roedel87a64d52009-11-24 17:26:43 +01001640static struct dma_ops_domain *dma_ops_domain_alloc(void)
Joerg Roedelec487d12008-06-26 21:27:58 +02001641{
1642 struct dma_ops_domain *dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001643
1644 dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
1645 if (!dma_dom)
1646 return NULL;
1647
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001648 if (protection_domain_init(&dma_dom->domain))
Joerg Roedelec487d12008-06-26 21:27:58 +02001649 goto free_dma_dom;
Joerg Roedel7a5a5662015-06-30 08:56:11 +02001650
Joerg Roedel8f7a0172009-09-02 16:55:24 +02001651 dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
Joerg Roedelec487d12008-06-26 21:27:58 +02001652 dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
Joerg Roedel9fdb19d2008-12-02 17:46:25 +01001653 dma_dom->domain.flags = PD_DMA_OPS_MASK;
Joerg Roedelec487d12008-06-26 21:27:58 +02001654 dma_dom->domain.priv = dma_dom;
1655 if (!dma_dom->domain.pt_root)
1656 goto free_dma_dom;
Joerg Roedelec487d12008-06-26 21:27:58 +02001657
Joerg Roedel307d5852016-07-05 11:54:04 +02001658 init_iova_domain(&dma_dom->iovad, PAGE_SIZE,
1659 IOVA_START_PFN, DMA_32BIT_PFN);
1660
Joerg Roedel81cd07b2016-07-07 18:01:10 +02001661 /* Initialize reserved ranges */
1662 copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
1663
Joerg Roedel2d4c5152016-07-05 16:21:32 +02001664 add_domain_to_list(&dma_dom->domain);
1665
Joerg Roedelec487d12008-06-26 21:27:58 +02001666 return dma_dom;
1667
1668free_dma_dom:
1669 dma_ops_domain_free(dma_dom);
1670
1671 return NULL;
1672}
1673
Joerg Roedel431b2a22008-07-11 17:14:22 +02001674/*
Joerg Roedel5b28df62008-12-02 17:49:42 +01001675 * little helper function to check whether a given protection domain is a
1676 * dma_ops domain
1677 */
1678static bool dma_ops_domain(struct protection_domain *domain)
1679{
1680 return domain->flags & PD_DMA_OPS_MASK;
1681}
1682
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001683static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001684{
Joerg Roedel132bd682011-11-17 14:18:46 +01001685 u64 pte_root = 0;
Joerg Roedelee6c2862011-11-09 12:06:03 +01001686 u64 flags = 0;
Joerg Roedel863c74e2008-12-02 17:56:36 +01001687
Joerg Roedel132bd682011-11-17 14:18:46 +01001688 if (domain->mode != PAGE_MODE_NONE)
1689 pte_root = virt_to_phys(domain->pt_root);
1690
Joerg Roedel38ddf412008-09-11 10:38:32 +02001691 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
1692 << DEV_ENTRY_MODE_SHIFT;
1693 pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001694
Joerg Roedelee6c2862011-11-09 12:06:03 +01001695 flags = amd_iommu_dev_table[devid].data[1];
1696
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001697 if (ats)
1698 flags |= DTE_FLAG_IOTLB;
1699
Joerg Roedel52815b72011-11-17 17:24:28 +01001700 if (domain->flags & PD_IOMMUV2_MASK) {
1701 u64 gcr3 = __pa(domain->gcr3_tbl);
1702 u64 glx = domain->glx;
1703 u64 tmp;
1704
1705 pte_root |= DTE_FLAG_GV;
1706 pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
1707
1708 /* First mask out possible old values for GCR3 table */
1709 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1710 flags &= ~tmp;
1711
1712 tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1713 flags &= ~tmp;
1714
1715 /* Encode GCR3 table into DTE */
1716 tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
1717 pte_root |= tmp;
1718
1719 tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
1720 flags |= tmp;
1721
1722 tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
1723 flags |= tmp;
1724 }
1725
Joerg Roedelee6c2862011-11-09 12:06:03 +01001726 flags &= ~(0xffffUL);
1727 flags |= domain->id;
1728
1729 amd_iommu_dev_table[devid].data[1] = flags;
1730 amd_iommu_dev_table[devid].data[0] = pte_root;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02001731}
1732
Joerg Roedel15898bb2009-11-24 15:39:42 +01001733static void clear_dte_entry(u16 devid)
Joerg Roedel355bf552008-12-08 12:02:41 +01001734{
Joerg Roedel355bf552008-12-08 12:02:41 +01001735 /* remove entry from the device table seen by the hardware */
Joerg Roedelcbf3ccd2015-10-20 14:59:36 +02001736 amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
1737 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
Joerg Roedel355bf552008-12-08 12:02:41 +01001738
Joerg Roedelc5cca142009-10-09 18:31:20 +02001739 amd_iommu_apply_erratum_63(devid);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001740}
1741
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001742static void do_attach(struct iommu_dev_data *dev_data,
1743 struct protection_domain *domain)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001744{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001745 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001746 u16 alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001747 bool ats;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001748
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001749 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001750 alias = dev_data->alias;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001751 ats = dev_data->ats.enabled;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001752
1753 /* Update data structures */
1754 dev_data->domain = domain;
1755 list_add(&dev_data->list, &domain->dev_list);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001756
1757 /* Do reference counting */
1758 domain->dev_iommu[iommu->index] += 1;
1759 domain->dev_cnt += 1;
1760
Joerg Roedele25bfb52015-10-20 17:33:38 +02001761 /* Update device table */
1762 set_dte_entry(dev_data->devid, domain, ats);
1763 if (alias != dev_data->devid)
Baoquan He9b1a12d2016-01-20 22:01:19 +08001764 set_dte_entry(alias, domain, ats);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001765
Joerg Roedel6c542042011-06-09 17:07:31 +02001766 device_flush_dte(dev_data);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001767}
1768
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001769static void do_detach(struct iommu_dev_data *dev_data)
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001770{
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001771 struct amd_iommu *iommu;
Joerg Roedele25bfb52015-10-20 17:33:38 +02001772 u16 alias;
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001773
Joerg Roedel5adad992015-10-09 16:23:33 +02001774 /*
1775 * First check if the device is still attached. It might already
1776 * be detached from its domain because the generic
1777 * iommu_detach_group code detached it and we try again here in
1778 * our alias handling.
1779 */
1780 if (!dev_data->domain)
1781 return;
1782
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001783 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedele3156042016-04-08 15:12:24 +02001784 alias = dev_data->alias;
Joerg Roedelc5cca142009-10-09 18:31:20 +02001785
Joerg Roedelc4596112009-11-20 14:57:32 +01001786 /* decrease reference counters */
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001787 dev_data->domain->dev_iommu[iommu->index] -= 1;
1788 dev_data->domain->dev_cnt -= 1;
Joerg Roedel355bf552008-12-08 12:02:41 +01001789
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001790 /* Update data structures */
1791 dev_data->domain = NULL;
1792 list_del(&dev_data->list);
Joerg Roedelf62dda62011-06-09 12:55:35 +02001793 clear_dte_entry(dev_data->devid);
Joerg Roedele25bfb52015-10-20 17:33:38 +02001794 if (alias != dev_data->devid)
1795 clear_dte_entry(alias);
Joerg Roedel7f760dd2009-11-26 14:49:59 +01001796
1797 /* Flush the DTE entry */
Joerg Roedel6c542042011-06-09 17:07:31 +02001798 device_flush_dte(dev_data);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001799}
1800
1801/*
1802 * If a device is not yet associated with a domain, this function does
1803 * assigns it visible for the hardware
1804 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001805static int __attach_device(struct iommu_dev_data *dev_data,
Joerg Roedel15898bb2009-11-24 15:39:42 +01001806 struct protection_domain *domain)
1807{
Julia Lawall84fe6c12010-05-27 12:31:51 +02001808 int ret;
Joerg Roedel657cbb62009-11-23 15:26:46 +01001809
Joerg Roedel272e4f92015-10-20 17:33:37 +02001810 /*
1811 * Must be called with IRQs disabled. Warn here to detect early
1812 * when its not.
1813 */
1814 WARN_ON(!irqs_disabled());
1815
Joerg Roedel15898bb2009-11-24 15:39:42 +01001816 /* lock domain */
1817 spin_lock(&domain->lock);
1818
Joerg Roedel397111a2014-08-05 17:31:51 +02001819 ret = -EBUSY;
Joerg Roedel150952f2015-10-20 17:33:35 +02001820 if (dev_data->domain != NULL)
Joerg Roedel397111a2014-08-05 17:31:51 +02001821 goto out_unlock;
Joerg Roedel24100052009-11-25 15:59:57 +01001822
Joerg Roedel397111a2014-08-05 17:31:51 +02001823 /* Attach alias group root */
Joerg Roedel150952f2015-10-20 17:33:35 +02001824 do_attach(dev_data, domain);
Joerg Roedel24100052009-11-25 15:59:57 +01001825
Julia Lawall84fe6c12010-05-27 12:31:51 +02001826 ret = 0;
1827
1828out_unlock:
1829
Joerg Roedel355bf552008-12-08 12:02:41 +01001830 /* ready */
1831 spin_unlock(&domain->lock);
Joerg Roedel21129f72009-09-01 11:59:42 +02001832
Julia Lawall84fe6c12010-05-27 12:31:51 +02001833 return ret;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001834}
1835
Joerg Roedel52815b72011-11-17 17:24:28 +01001836
1837static void pdev_iommuv2_disable(struct pci_dev *pdev)
1838{
1839 pci_disable_ats(pdev);
1840 pci_disable_pri(pdev);
1841 pci_disable_pasid(pdev);
1842}
1843
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001844/* FIXME: Change generic reset-function to do the same */
1845static int pri_reset_while_enabled(struct pci_dev *pdev)
1846{
1847 u16 control;
1848 int pos;
1849
Joerg Roedel46277b72011-12-07 14:34:02 +01001850 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001851 if (!pos)
1852 return -EINVAL;
1853
Joerg Roedel46277b72011-12-07 14:34:02 +01001854 pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
1855 control |= PCI_PRI_CTRL_RESET;
1856 pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001857
1858 return 0;
1859}
1860
Joerg Roedel52815b72011-11-17 17:24:28 +01001861static int pdev_iommuv2_enable(struct pci_dev *pdev)
1862{
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001863 bool reset_enable;
1864 int reqs, ret;
1865
1866 /* FIXME: Hardcode number of outstanding requests for now */
1867 reqs = 32;
1868 if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
1869 reqs = 1;
1870 reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
Joerg Roedel52815b72011-11-17 17:24:28 +01001871
1872 /* Only allow access to user-accessible pages */
1873 ret = pci_enable_pasid(pdev, 0);
1874 if (ret)
1875 goto out_err;
1876
1877 /* First reset the PRI state of the device */
1878 ret = pci_reset_pri(pdev);
1879 if (ret)
1880 goto out_err;
1881
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001882 /* Enable PRI */
1883 ret = pci_enable_pri(pdev, reqs);
Joerg Roedel52815b72011-11-17 17:24:28 +01001884 if (ret)
1885 goto out_err;
1886
Joerg Roedel6a113dd2011-12-01 12:04:58 +01001887 if (reset_enable) {
1888 ret = pri_reset_while_enabled(pdev);
1889 if (ret)
1890 goto out_err;
1891 }
1892
Joerg Roedel52815b72011-11-17 17:24:28 +01001893 ret = pci_enable_ats(pdev, PAGE_SHIFT);
1894 if (ret)
1895 goto out_err;
1896
1897 return 0;
1898
1899out_err:
1900 pci_disable_pri(pdev);
1901 pci_disable_pasid(pdev);
1902
1903 return ret;
1904}
1905
Joerg Roedelc99afa22011-11-21 18:19:25 +01001906/* FIXME: Move this to PCI code */
Joerg Roedela3b93122012-04-12 12:49:26 +02001907#define PCI_PRI_TLP_OFF (1 << 15)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001908
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001909static bool pci_pri_tlp_required(struct pci_dev *pdev)
Joerg Roedelc99afa22011-11-21 18:19:25 +01001910{
Joerg Roedela3b93122012-04-12 12:49:26 +02001911 u16 status;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001912 int pos;
1913
Joerg Roedel46277b72011-12-07 14:34:02 +01001914 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001915 if (!pos)
1916 return false;
1917
Joerg Roedela3b93122012-04-12 12:49:26 +02001918 pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
Joerg Roedelc99afa22011-11-21 18:19:25 +01001919
Joerg Roedela3b93122012-04-12 12:49:26 +02001920 return (status & PCI_PRI_TLP_OFF) ? true : false;
Joerg Roedelc99afa22011-11-21 18:19:25 +01001921}
1922
Joerg Roedel15898bb2009-11-24 15:39:42 +01001923/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001924 * If a device is not yet associated with a domain, this function
Joerg Roedel15898bb2009-11-24 15:39:42 +01001925 * assigns it visible for the hardware
1926 */
1927static int attach_device(struct device *dev,
1928 struct protection_domain *domain)
1929{
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001930 struct pci_dev *pdev;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001931 struct iommu_dev_data *dev_data;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001932 unsigned long flags;
1933 int ret;
1934
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001935 dev_data = get_dev_data(dev);
1936
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001937 if (!dev_is_pci(dev))
1938 goto skip_ats_check;
1939
1940 pdev = to_pci_dev(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01001941 if (domain->flags & PD_IOMMUV2_MASK) {
Joerg Roedel02ca2022015-07-28 16:58:49 +02001942 if (!dev_data->passthrough)
Joerg Roedel52815b72011-11-17 17:24:28 +01001943 return -EINVAL;
1944
Joerg Roedel02ca2022015-07-28 16:58:49 +02001945 if (dev_data->iommu_v2) {
1946 if (pdev_iommuv2_enable(pdev) != 0)
1947 return -EINVAL;
Joerg Roedel52815b72011-11-17 17:24:28 +01001948
Joerg Roedel02ca2022015-07-28 16:58:49 +02001949 dev_data->ats.enabled = true;
1950 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1951 dev_data->pri_tlp = pci_pri_tlp_required(pdev);
1952 }
Joerg Roedel52815b72011-11-17 17:24:28 +01001953 } else if (amd_iommu_iotlb_sup &&
1954 pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
Joerg Roedelea61cdd2011-06-09 12:56:30 +02001955 dev_data->ats.enabled = true;
1956 dev_data->ats.qdep = pci_ats_queue_depth(pdev);
1957 }
Joerg Roedelfd7b5532011-04-05 15:31:08 +02001958
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04001959skip_ats_check:
Joerg Roedel15898bb2009-11-24 15:39:42 +01001960 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001961 ret = __attach_device(dev_data, domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001962 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
1963
1964 /*
1965 * We might boot into a crash-kernel here. The crashed kernel
1966 * left the caches in the IOMMU dirty. So we have to flush
1967 * here to evict all dirty stuff.
1968 */
Joerg Roedel17b124b2011-04-06 18:01:35 +02001969 domain_flush_tlb_pde(domain);
Joerg Roedel15898bb2009-11-24 15:39:42 +01001970
1971 return ret;
1972}
1973
1974/*
1975 * Removes a device from a protection domain (unlocked)
1976 */
Joerg Roedelec9e79e2011-06-09 17:25:50 +02001977static void __detach_device(struct iommu_dev_data *dev_data)
Joerg Roedel15898bb2009-11-24 15:39:42 +01001978{
Joerg Roedel2ca76272010-01-22 16:45:31 +01001979 struct protection_domain *domain;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001980
Joerg Roedel272e4f92015-10-20 17:33:37 +02001981 /*
1982 * Must be called with IRQs disabled. Warn here to detect early
1983 * when its not.
1984 */
1985 WARN_ON(!irqs_disabled());
1986
Joerg Roedelf34c73f2015-10-20 17:33:34 +02001987 if (WARN_ON(!dev_data->domain))
1988 return;
Joerg Roedel15898bb2009-11-24 15:39:42 +01001989
Joerg Roedel2ca76272010-01-22 16:45:31 +01001990 domain = dev_data->domain;
1991
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02001992 spin_lock(&domain->lock);
Joerg Roedel24100052009-11-25 15:59:57 +01001993
Joerg Roedel150952f2015-10-20 17:33:35 +02001994 do_detach(dev_data);
Joerg Roedel71f77582011-06-09 19:03:15 +02001995
Joerg Roedelf1dd0a82015-10-20 17:33:36 +02001996 spin_unlock(&domain->lock);
Joerg Roedel355bf552008-12-08 12:02:41 +01001997}
1998
1999/*
2000 * Removes a device from a protection domain (with devtable_lock held)
2001 */
Joerg Roedel15898bb2009-11-24 15:39:42 +01002002static void detach_device(struct device *dev)
Joerg Roedel355bf552008-12-08 12:02:41 +01002003{
Joerg Roedel52815b72011-11-17 17:24:28 +01002004 struct protection_domain *domain;
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002005 struct iommu_dev_data *dev_data;
Joerg Roedel355bf552008-12-08 12:02:41 +01002006 unsigned long flags;
2007
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002008 dev_data = get_dev_data(dev);
Joerg Roedel52815b72011-11-17 17:24:28 +01002009 domain = dev_data->domain;
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002010
Joerg Roedel355bf552008-12-08 12:02:41 +01002011 /* lock device table */
2012 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
Joerg Roedelec9e79e2011-06-09 17:25:50 +02002013 __detach_device(dev_data);
Joerg Roedel355bf552008-12-08 12:02:41 +01002014 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
Joerg Roedelfd7b5532011-04-05 15:31:08 +02002015
Wan Zongshun2bf9a0a2016-04-01 09:06:03 -04002016 if (!dev_is_pci(dev))
2017 return;
2018
Joerg Roedel02ca2022015-07-28 16:58:49 +02002019 if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
Joerg Roedel52815b72011-11-17 17:24:28 +01002020 pdev_iommuv2_disable(to_pci_dev(dev));
2021 else if (dev_data->ats.enabled)
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002022 pci_disable_ats(to_pci_dev(dev));
Joerg Roedel52815b72011-11-17 17:24:28 +01002023
2024 dev_data->ats.enabled = false;
Joerg Roedel355bf552008-12-08 12:02:41 +01002025}
Joerg Roedele275a2a2008-12-10 18:27:25 +01002026
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002027static int amd_iommu_add_device(struct device *dev)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002028{
Joerg Roedel71f77582011-06-09 19:03:15 +02002029 struct iommu_dev_data *dev_data;
Joerg Roedel07ee8692015-05-28 18:41:42 +02002030 struct iommu_domain *domain;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002031 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002032 int ret, devid;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002033
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002034 if (!check_device(dev) || get_dev_data(dev))
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002035 return 0;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002036
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002037 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002038 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002039 return devid;
2040
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002041 iommu = amd_iommu_rlookup_table[devid];
Joerg Roedele275a2a2008-12-10 18:27:25 +01002042
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002043 ret = iommu_init_device(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002044 if (ret) {
2045 if (ret != -ENOTSUPP)
2046 pr_err("Failed to initialize device %s - trying to proceed anyway\n",
2047 dev_name(dev));
Joerg Roedel657cbb62009-11-23 15:26:46 +01002048
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002049 iommu_ignore_device(dev);
Joerg Roedel343e9ca2015-05-28 18:41:43 +02002050 dev->archdata.dma_ops = &nommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002051 goto out;
2052 }
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002053 init_iommu_group(dev);
Joerg Roedele275a2a2008-12-10 18:27:25 +01002054
Joerg Roedel07ee8692015-05-28 18:41:42 +02002055 dev_data = get_dev_data(dev);
Joerg Roedel4d58b8a2015-06-11 09:21:39 +02002056
2057 BUG_ON(!dev_data);
2058
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002059 if (iommu_pass_through || dev_data->iommu_v2)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002060 iommu_request_dm_for_dev(dev);
2061
2062 /* Domains are initialized for this device - have a look what we ended up with */
2063 domain = iommu_get_domain_for_dev(dev);
Joerg Roedel32302322015-07-28 16:58:50 +02002064 if (domain->type == IOMMU_DOMAIN_IDENTITY)
Joerg Roedel07ee8692015-05-28 18:41:42 +02002065 dev_data->passthrough = true;
Joerg Roedel32302322015-07-28 16:58:50 +02002066 else
Joerg Roedel07ee8692015-05-28 18:41:42 +02002067 dev->archdata.dma_ops = &amd_iommu_dma_ops;
Joerg Roedele275a2a2008-12-10 18:27:25 +01002068
2069out:
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002070 iommu_completion_wait(iommu);
2071
Joerg Roedele275a2a2008-12-10 18:27:25 +01002072 return 0;
2073}
2074
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002075static void amd_iommu_remove_device(struct device *dev)
Joerg Roedel8638c492009-12-10 11:12:25 +01002076{
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002077 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002078 int devid;
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002079
2080 if (!check_device(dev))
2081 return;
2082
2083 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002084 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002085 return;
2086
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002087 iommu = amd_iommu_rlookup_table[devid];
2088
2089 iommu_uninit_device(dev);
2090 iommu_completion_wait(iommu);
Joerg Roedel8638c492009-12-10 11:12:25 +01002091}
2092
Wan Zongshunb097d112016-04-01 09:06:04 -04002093static struct iommu_group *amd_iommu_device_group(struct device *dev)
2094{
2095 if (dev_is_pci(dev))
2096 return pci_device_group(dev);
2097
2098 return acpihid_device_group(dev);
2099}
2100
Joerg Roedel431b2a22008-07-11 17:14:22 +02002101/*****************************************************************************
2102 *
2103 * The next functions belong to the dma_ops mapping/unmapping code.
2104 *
2105 *****************************************************************************/
2106
2107/*
2108 * In the dma_ops path we only have the struct device. This function
2109 * finds the corresponding IOMMU, the protection domain and the
2110 * requestor id for a given device.
2111 * If the device is not yet associated with a domain this is also done
2112 * in this function.
2113 */
Joerg Roedel94f6d192009-11-24 16:40:02 +01002114static struct protection_domain *get_domain(struct device *dev)
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002115{
Joerg Roedel94f6d192009-11-24 16:40:02 +01002116 struct protection_domain *domain;
Joerg Roedel063071d2015-05-28 18:41:38 +02002117 struct iommu_domain *io_domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002118
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002119 if (!check_device(dev))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002120 return ERR_PTR(-EINVAL);
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002121
Joerg Roedel063071d2015-05-28 18:41:38 +02002122 io_domain = iommu_get_domain_for_dev(dev);
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002123 if (!io_domain)
2124 return NULL;
Joerg Roedel063071d2015-05-28 18:41:38 +02002125
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002126 domain = to_pdomain(io_domain);
2127 if (!dma_ops_domain(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002128 return ERR_PTR(-EBUSY);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002129
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002130 return domain;
Joerg Roedelb20ac0d2008-06-26 21:27:59 +02002131}
2132
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002133static void update_device_table(struct protection_domain *domain)
2134{
Joerg Roedel492667d2009-11-27 13:25:47 +01002135 struct iommu_dev_data *dev_data;
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002136
Joerg Roedelea61cdd2011-06-09 12:56:30 +02002137 list_for_each_entry(dev_data, &domain->dev_list, list)
2138 set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002139}
2140
2141static void update_domain(struct protection_domain *domain)
2142{
2143 if (!domain->updated)
2144 return;
2145
2146 update_device_table(domain);
Joerg Roedel17b124b2011-04-06 18:01:35 +02002147
2148 domain_flush_devices(domain);
2149 domain_flush_tlb_pde(domain);
Joerg Roedel04bfdd82009-09-02 16:00:23 +02002150
2151 domain->updated = false;
2152}
2153
Joerg Roedel431b2a22008-07-11 17:14:22 +02002154/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002155 * This function contains common code for mapping of a physically
Joerg Roedel24f81162008-12-08 14:25:39 +01002156 * contiguous memory region into DMA address space. It is used by all
2157 * mapping functions provided with this IOMMU driver.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002158 * Must be called with the domain lock held.
2159 */
Joerg Roedelcb76c322008-06-26 21:28:00 +02002160static dma_addr_t __map_single(struct device *dev,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002161 struct dma_ops_domain *dma_dom,
2162 phys_addr_t paddr,
2163 size_t size,
Joerg Roedel518d9b42016-07-05 14:39:47 +02002164 int direction,
Joerg Roedel832a90c2008-09-18 15:54:23 +02002165 u64 dma_mask)
Joerg Roedelcb76c322008-06-26 21:28:00 +02002166{
2167 dma_addr_t offset = paddr & ~PAGE_MASK;
Joerg Roedel53812c12009-05-12 12:17:38 +02002168 dma_addr_t address, start, ret;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002169 unsigned int pages;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002170 int prot = 0;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002171 int i;
2172
Joerg Roedele3c449f2008-10-15 22:02:11 -07002173 pages = iommu_num_pages(paddr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002174 paddr &= PAGE_MASK;
2175
Joerg Roedel256e4622016-07-05 14:23:01 +02002176 address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
Joerg Roedel266a3bd2015-12-21 18:54:24 +01002177 if (address == DMA_ERROR_CODE)
2178 goto out;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002179
Joerg Roedel518d9b42016-07-05 14:39:47 +02002180 if (direction == DMA_TO_DEVICE)
2181 prot = IOMMU_PROT_IR;
2182 else if (direction == DMA_FROM_DEVICE)
2183 prot = IOMMU_PROT_IW;
2184 else if (direction == DMA_BIDIRECTIONAL)
2185 prot = IOMMU_PROT_IW | IOMMU_PROT_IR;
2186
Joerg Roedelcb76c322008-06-26 21:28:00 +02002187 start = address;
2188 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002189 ret = iommu_map_page(&dma_dom->domain, start, paddr,
2190 PAGE_SIZE, prot, GFP_ATOMIC);
2191 if (ret)
Joerg Roedel53812c12009-05-12 12:17:38 +02002192 goto out_unmap;
2193
Joerg Roedelcb76c322008-06-26 21:28:00 +02002194 paddr += PAGE_SIZE;
2195 start += PAGE_SIZE;
2196 }
2197 address += offset;
2198
Joerg Roedelab7032b2015-12-21 18:47:11 +01002199 if (unlikely(amd_iommu_np_cache)) {
Joerg Roedel17b124b2011-04-06 18:01:35 +02002200 domain_flush_pages(&dma_dom->domain, address, size);
Joerg Roedelab7032b2015-12-21 18:47:11 +01002201 domain_flush_complete(&dma_dom->domain);
2202 }
Joerg Roedel270cab242008-09-04 15:49:46 +02002203
Joerg Roedelcb76c322008-06-26 21:28:00 +02002204out:
2205 return address;
Joerg Roedel53812c12009-05-12 12:17:38 +02002206
2207out_unmap:
2208
2209 for (--i; i >= 0; --i) {
2210 start -= PAGE_SIZE;
Joerg Roedel518d9b42016-07-05 14:39:47 +02002211 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedel53812c12009-05-12 12:17:38 +02002212 }
2213
Joerg Roedel256e4622016-07-05 14:23:01 +02002214 domain_flush_tlb(&dma_dom->domain);
2215 domain_flush_complete(&dma_dom->domain);
2216
2217 dma_ops_free_iova(dma_dom, address, pages);
Joerg Roedel53812c12009-05-12 12:17:38 +02002218
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +09002219 return DMA_ERROR_CODE;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002220}
2221
Joerg Roedel431b2a22008-07-11 17:14:22 +02002222/*
2223 * Does the reverse of the __map_single function. Must be called with
2224 * the domain lock held too
2225 */
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002226static void __unmap_single(struct dma_ops_domain *dma_dom,
Joerg Roedelcb76c322008-06-26 21:28:00 +02002227 dma_addr_t dma_addr,
2228 size_t size,
2229 int dir)
2230{
Joerg Roedel04e04632010-09-23 16:12:48 +02002231 dma_addr_t flush_addr;
Joerg Roedelcb76c322008-06-26 21:28:00 +02002232 dma_addr_t i, start;
2233 unsigned int pages;
2234
Joerg Roedel04e04632010-09-23 16:12:48 +02002235 flush_addr = dma_addr;
Joerg Roedele3c449f2008-10-15 22:02:11 -07002236 pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002237 dma_addr &= PAGE_MASK;
2238 start = dma_addr;
2239
2240 for (i = 0; i < pages; ++i) {
Joerg Roedel518d9b42016-07-05 14:39:47 +02002241 iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002242 start += PAGE_SIZE;
2243 }
2244
Joerg Roedel256e4622016-07-05 14:23:01 +02002245 domain_flush_tlb(&dma_dom->domain);
2246 domain_flush_complete(&dma_dom->domain);
2247
2248 dma_ops_free_iova(dma_dom, dma_addr, pages);
Joerg Roedelcb76c322008-06-26 21:28:00 +02002249}
2250
Joerg Roedel431b2a22008-07-11 17:14:22 +02002251/*
2252 * The exported map_single function for dma_ops.
2253 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002254static dma_addr_t map_page(struct device *dev, struct page *page,
2255 unsigned long offset, size_t size,
2256 enum dma_data_direction dir,
2257 struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002258{
FUJITA Tomonori51491362009-01-05 23:47:25 +09002259 phys_addr_t paddr = page_to_phys(page) + offset;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002260 struct protection_domain *domain;
2261 u64 dma_mask;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002262
Joerg Roedel94f6d192009-11-24 16:40:02 +01002263 domain = get_domain(dev);
2264 if (PTR_ERR(domain) == -EINVAL)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002265 return (dma_addr_t)paddr;
Joerg Roedel94f6d192009-11-24 16:40:02 +01002266 else if (IS_ERR(domain))
2267 return DMA_ERROR_CODE;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002268
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002269 dma_mask = *dev->dma_mask;
2270
Joerg Roedelbda350d2016-07-05 16:28:02 +02002271 return __map_single(dev, domain->priv, paddr, size, dir, dma_mask);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002272}
2273
Joerg Roedel431b2a22008-07-11 17:14:22 +02002274/*
2275 * The exported unmap_single function for dma_ops.
2276 */
FUJITA Tomonori51491362009-01-05 23:47:25 +09002277static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
2278 enum dma_data_direction dir, struct dma_attrs *attrs)
Joerg Roedel4da70b92008-06-26 21:28:01 +02002279{
Joerg Roedel4da70b92008-06-26 21:28:01 +02002280 struct protection_domain *domain;
Joerg Roedel4da70b92008-06-26 21:28:01 +02002281
Joerg Roedel94f6d192009-11-24 16:40:02 +01002282 domain = get_domain(dev);
2283 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002284 return;
2285
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002286 __unmap_single(domain->priv, dma_addr, size, dir);
Joerg Roedel4da70b92008-06-26 21:28:01 +02002287}
2288
Joerg Roedel431b2a22008-07-11 17:14:22 +02002289/*
Joerg Roedel431b2a22008-07-11 17:14:22 +02002290 * The exported map_sg function for dma_ops (handles scatter-gather
2291 * lists).
2292 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002293static int map_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002294 int nelems, enum dma_data_direction dir,
2295 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002296{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002297 struct protection_domain *domain;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002298 int i;
2299 struct scatterlist *s;
2300 phys_addr_t paddr;
2301 int mapped_elems = 0;
Joerg Roedel832a90c2008-09-18 15:54:23 +02002302 u64 dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002303
Joerg Roedel94f6d192009-11-24 16:40:02 +01002304 domain = get_domain(dev);
Joerg Roedela0e191b2013-04-09 15:04:36 +02002305 if (IS_ERR(domain))
Joerg Roedel94f6d192009-11-24 16:40:02 +01002306 return 0;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002307
Joerg Roedel832a90c2008-09-18 15:54:23 +02002308 dma_mask = *dev->dma_mask;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002309
Joerg Roedel65b050a2008-06-26 21:28:02 +02002310 for_each_sg(sglist, s, nelems, i) {
2311 paddr = sg_phys(s);
2312
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002313 s->dma_address = __map_single(dev, domain->priv,
Joerg Roedelbda350d2016-07-05 16:28:02 +02002314 paddr, s->length, dir, dma_mask);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002315
2316 if (s->dma_address) {
2317 s->dma_length = s->length;
2318 mapped_elems++;
2319 } else
2320 goto unmap;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002321 }
2322
Joerg Roedel65b050a2008-06-26 21:28:02 +02002323 return mapped_elems;
Joerg Roedel92d420e2015-12-21 19:31:33 +01002324
Joerg Roedel65b050a2008-06-26 21:28:02 +02002325unmap:
2326 for_each_sg(sglist, s, mapped_elems, i) {
2327 if (s->dma_address)
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002328 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002329 s->dma_length, dir);
2330 s->dma_address = s->dma_length = 0;
2331 }
2332
Joerg Roedel92d420e2015-12-21 19:31:33 +01002333 return 0;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002334}
2335
Joerg Roedel431b2a22008-07-11 17:14:22 +02002336/*
2337 * The exported map_sg function for dma_ops (handles scatter-gather
2338 * lists).
2339 */
Joerg Roedel65b050a2008-06-26 21:28:02 +02002340static void unmap_sg(struct device *dev, struct scatterlist *sglist,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002341 int nelems, enum dma_data_direction dir,
2342 struct dma_attrs *attrs)
Joerg Roedel65b050a2008-06-26 21:28:02 +02002343{
Joerg Roedel65b050a2008-06-26 21:28:02 +02002344 struct protection_domain *domain;
2345 struct scatterlist *s;
Joerg Roedel65b050a2008-06-26 21:28:02 +02002346 int i;
2347
Joerg Roedel94f6d192009-11-24 16:40:02 +01002348 domain = get_domain(dev);
2349 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002350 return;
2351
Joerg Roedel65b050a2008-06-26 21:28:02 +02002352 for_each_sg(sglist, s, nelems, i) {
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002353 __unmap_single(domain->priv, s->dma_address,
Joerg Roedel65b050a2008-06-26 21:28:02 +02002354 s->dma_length, dir);
Joerg Roedel65b050a2008-06-26 21:28:02 +02002355 s->dma_address = s->dma_length = 0;
2356 }
Joerg Roedel65b050a2008-06-26 21:28:02 +02002357}
2358
Joerg Roedel431b2a22008-07-11 17:14:22 +02002359/*
2360 * The exported alloc_coherent function for dma_ops.
2361 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002362static void *alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002363 dma_addr_t *dma_addr, gfp_t flag,
2364 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002365{
Joerg Roedel832a90c2008-09-18 15:54:23 +02002366 u64 dma_mask = dev->coherent_dma_mask;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002367 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002368 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002369
Joerg Roedel94f6d192009-11-24 16:40:02 +01002370 domain = get_domain(dev);
2371 if (PTR_ERR(domain) == -EINVAL) {
Joerg Roedel3b839a52015-04-01 14:58:47 +02002372 page = alloc_pages(flag, get_order(size));
2373 *dma_addr = page_to_phys(page);
2374 return page_address(page);
Joerg Roedel94f6d192009-11-24 16:40:02 +01002375 } else if (IS_ERR(domain))
2376 return NULL;
Joerg Roedeldbcc1122008-09-04 15:04:26 +02002377
Joerg Roedel3b839a52015-04-01 14:58:47 +02002378 size = PAGE_ALIGN(size);
Joerg Roedelf99c0f12009-11-23 16:52:56 +01002379 dma_mask = dev->coherent_dma_mask;
2380 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
Joerg Roedel2d0ec7a2015-06-01 17:30:57 +02002381 flag |= __GFP_ZERO;
FUJITA Tomonori13d9fea2008-09-10 20:19:40 +09002382
Joerg Roedel3b839a52015-04-01 14:58:47 +02002383 page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
2384 if (!page) {
Mel Gormand0164ad2015-11-06 16:28:21 -08002385 if (!gfpflags_allow_blocking(flag))
Joerg Roedel3b839a52015-04-01 14:58:47 +02002386 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002387
Joerg Roedel3b839a52015-04-01 14:58:47 +02002388 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
2389 get_order(size));
2390 if (!page)
2391 return NULL;
2392 }
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002393
Joerg Roedel832a90c2008-09-18 15:54:23 +02002394 if (!dma_mask)
2395 dma_mask = *dev->dma_mask;
2396
Joerg Roedel3b839a52015-04-01 14:58:47 +02002397 *dma_addr = __map_single(dev, domain->priv, page_to_phys(page),
Joerg Roedelbda350d2016-07-05 16:28:02 +02002398 size, DMA_BIDIRECTIONAL, dma_mask);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002399
Joerg Roedel92d420e2015-12-21 19:31:33 +01002400 if (*dma_addr == DMA_ERROR_CODE)
Joerg Roedel5b28df62008-12-02 17:49:42 +01002401 goto out_free;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002402
Joerg Roedel3b839a52015-04-01 14:58:47 +02002403 return page_address(page);
Joerg Roedel5b28df62008-12-02 17:49:42 +01002404
2405out_free:
2406
Joerg Roedel3b839a52015-04-01 14:58:47 +02002407 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2408 __free_pages(page, get_order(size));
Joerg Roedel5b28df62008-12-02 17:49:42 +01002409
2410 return NULL;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002411}
2412
Joerg Roedel431b2a22008-07-11 17:14:22 +02002413/*
2414 * The exported free_coherent function for dma_ops.
Joerg Roedel431b2a22008-07-11 17:14:22 +02002415 */
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002416static void free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +02002417 void *virt_addr, dma_addr_t dma_addr,
2418 struct dma_attrs *attrs)
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002419{
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002420 struct protection_domain *domain;
Joerg Roedel3b839a52015-04-01 14:58:47 +02002421 struct page *page;
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002422
Joerg Roedel3b839a52015-04-01 14:58:47 +02002423 page = virt_to_page(virt_addr);
2424 size = PAGE_ALIGN(size);
2425
Joerg Roedel94f6d192009-11-24 16:40:02 +01002426 domain = get_domain(dev);
2427 if (IS_ERR(domain))
Joerg Roedel5b28df62008-12-02 17:49:42 +01002428 goto free_mem;
2429
Joerg Roedelcd8c82e2009-11-23 19:33:56 +01002430 __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002431
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002432free_mem:
Joerg Roedel3b839a52015-04-01 14:58:47 +02002433 if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
2434 __free_pages(page, get_order(size));
Joerg Roedel5d8b53c2008-06-26 21:28:03 +02002435}
2436
Joerg Roedelc432f3d2008-06-26 21:28:04 +02002437/*
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002438 * This function is called by the DMA layer to find out if we can handle a
2439 * particular device. It is part of the dma_ops.
2440 */
2441static int amd_iommu_dma_supported(struct device *dev, u64 mask)
2442{
Joerg Roedel420aef82009-11-23 16:14:57 +01002443 return check_device(dev);
Joerg Roedelb39ba6a2008-09-09 18:40:46 +02002444}
2445
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002446static struct dma_map_ops amd_iommu_dma_ops = {
Joerg Roedela639a8e2015-12-22 16:06:49 +01002447 .alloc = alloc_coherent,
2448 .free = free_coherent,
2449 .map_page = map_page,
2450 .unmap_page = unmap_page,
2451 .map_sg = map_sg,
2452 .unmap_sg = unmap_sg,
2453 .dma_supported = amd_iommu_dma_supported,
Joerg Roedel6631ee92008-06-26 21:28:05 +02002454};
2455
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002456static int init_reserved_iova_ranges(void)
2457{
2458 struct pci_dev *pdev = NULL;
2459 struct iova *val;
2460
2461 init_iova_domain(&reserved_iova_ranges, PAGE_SIZE,
2462 IOVA_START_PFN, DMA_32BIT_PFN);
2463
2464 lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
2465 &reserved_rbtree_key);
2466
2467 /* MSI memory range */
2468 val = reserve_iova(&reserved_iova_ranges,
2469 IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
2470 if (!val) {
2471 pr_err("Reserving MSI range failed\n");
2472 return -ENOMEM;
2473 }
2474
2475 /* HT memory range */
2476 val = reserve_iova(&reserved_iova_ranges,
2477 IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
2478 if (!val) {
2479 pr_err("Reserving HT range failed\n");
2480 return -ENOMEM;
2481 }
2482
2483 /*
2484 * Memory used for PCI resources
2485 * FIXME: Check whether we can reserve the PCI-hole completly
2486 */
2487 for_each_pci_dev(pdev) {
2488 int i;
2489
2490 for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
2491 struct resource *r = &pdev->resource[i];
2492
2493 if (!(r->flags & IORESOURCE_MEM))
2494 continue;
2495
2496 val = reserve_iova(&reserved_iova_ranges,
2497 IOVA_PFN(r->start),
2498 IOVA_PFN(r->end));
2499 if (!val) {
2500 pr_err("Reserve pci-resource range failed\n");
2501 return -ENOMEM;
2502 }
2503 }
2504 }
2505
2506 return 0;
2507}
2508
Joerg Roedel3a18404c2015-05-28 18:41:45 +02002509int __init amd_iommu_init_api(void)
Joerg Roedel27c21272011-05-30 15:56:24 +02002510{
Joerg Roedel307d5852016-07-05 11:54:04 +02002511 int ret, err = 0;
2512
2513 ret = iova_cache_get();
2514 if (ret)
2515 return ret;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002516
Joerg Roedel81cd07b2016-07-07 18:01:10 +02002517 ret = init_reserved_iova_ranges();
2518 if (ret)
2519 return ret;
2520
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002521 err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
2522 if (err)
2523 return err;
2524#ifdef CONFIG_ARM_AMBA
2525 err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
2526 if (err)
2527 return err;
2528#endif
Wan Zongshun0076cd32016-05-10 09:21:01 -04002529 err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
2530 if (err)
2531 return err;
Wan Zongshun9a4d3bf52016-04-01 09:06:05 -04002532 return 0;
Joerg Roedelf5325092010-01-22 17:44:35 +01002533}
2534
Joerg Roedel6631ee92008-06-26 21:28:05 +02002535int __init amd_iommu_init_dma_ops(void)
2536{
Joerg Roedel32302322015-07-28 16:58:50 +02002537 swiotlb = iommu_pass_through ? 1 : 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002538 iommu_detected = 1;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002539
Joerg Roedel52717822015-07-28 16:58:51 +02002540 /*
2541 * In case we don't initialize SWIOTLB (actually the common case
2542 * when AMD IOMMU is enabled), make sure there are global
2543 * dma_ops set as a fall-back for devices not handled by this
2544 * driver (for example non-PCI devices).
2545 */
2546 if (!swiotlb)
2547 dma_ops = &nommu_dma_ops;
2548
Joerg Roedel62410ee2012-06-12 16:42:43 +02002549 if (amd_iommu_unmap_flush)
2550 pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
2551 else
2552 pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
2553
Joerg Roedel6631ee92008-06-26 21:28:05 +02002554 return 0;
Joerg Roedel6631ee92008-06-26 21:28:05 +02002555}
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002556
2557/*****************************************************************************
2558 *
2559 * The following functions belong to the exported interface of AMD IOMMU
2560 *
2561 * This interface allows access to lower level functions of the IOMMU
2562 * like protection domain handling and assignement of devices to domains
2563 * which is not possible with the dma_ops interface.
2564 *
2565 *****************************************************************************/
2566
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002567static void cleanup_domain(struct protection_domain *domain)
2568{
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002569 struct iommu_dev_data *entry;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002570 unsigned long flags;
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002571
2572 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
2573
Joerg Roedel9b29d3c2014-08-05 17:50:15 +02002574 while (!list_empty(&domain->dev_list)) {
2575 entry = list_first_entry(&domain->dev_list,
2576 struct iommu_dev_data, list);
2577 __detach_device(entry);
Joerg Roedel492667d2009-11-27 13:25:47 +01002578 }
Joerg Roedel6d98cd82008-12-08 12:05:55 +01002579
2580 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
2581}
2582
Joerg Roedel26508152009-08-26 16:52:40 +02002583static void protection_domain_free(struct protection_domain *domain)
2584{
2585 if (!domain)
2586 return;
2587
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002588 del_domain_from_list(domain);
2589
Joerg Roedel26508152009-08-26 16:52:40 +02002590 if (domain->id)
2591 domain_id_free(domain->id);
2592
2593 kfree(domain);
2594}
2595
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002596static int protection_domain_init(struct protection_domain *domain)
2597{
2598 spin_lock_init(&domain->lock);
2599 mutex_init(&domain->api_lock);
2600 domain->id = domain_id_alloc();
2601 if (!domain->id)
2602 return -ENOMEM;
2603 INIT_LIST_HEAD(&domain->dev_list);
2604
2605 return 0;
2606}
2607
Joerg Roedel26508152009-08-26 16:52:40 +02002608static struct protection_domain *protection_domain_alloc(void)
Joerg Roedelc156e342008-12-02 18:13:27 +01002609{
2610 struct protection_domain *domain;
2611
2612 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2613 if (!domain)
Joerg Roedel26508152009-08-26 16:52:40 +02002614 return NULL;
Joerg Roedelc156e342008-12-02 18:13:27 +01002615
Joerg Roedel7a5a5662015-06-30 08:56:11 +02002616 if (protection_domain_init(domain))
Joerg Roedel26508152009-08-26 16:52:40 +02002617 goto out_err;
2618
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002619 add_domain_to_list(domain);
2620
Joerg Roedel26508152009-08-26 16:52:40 +02002621 return domain;
2622
2623out_err:
2624 kfree(domain);
2625
2626 return NULL;
2627}
2628
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002629static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2630{
2631 struct protection_domain *pdomain;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002632 struct dma_ops_domain *dma_domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002633
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002634 switch (type) {
2635 case IOMMU_DOMAIN_UNMANAGED:
2636 pdomain = protection_domain_alloc();
2637 if (!pdomain)
2638 return NULL;
2639
2640 pdomain->mode = PAGE_MODE_3_LEVEL;
2641 pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
2642 if (!pdomain->pt_root) {
2643 protection_domain_free(pdomain);
2644 return NULL;
2645 }
2646
2647 pdomain->domain.geometry.aperture_start = 0;
2648 pdomain->domain.geometry.aperture_end = ~0ULL;
2649 pdomain->domain.geometry.force_aperture = true;
2650
2651 break;
2652 case IOMMU_DOMAIN_DMA:
2653 dma_domain = dma_ops_domain_alloc();
2654 if (!dma_domain) {
2655 pr_err("AMD-Vi: Failed to allocate\n");
2656 return NULL;
2657 }
2658 pdomain = &dma_domain->domain;
2659 break;
Joerg Roedel07f643a2015-05-28 18:41:41 +02002660 case IOMMU_DOMAIN_IDENTITY:
2661 pdomain = protection_domain_alloc();
2662 if (!pdomain)
2663 return NULL;
2664
2665 pdomain->mode = PAGE_MODE_NONE;
2666 break;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002667 default:
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002668 return NULL;
Joerg Roedel0bb6e242015-05-28 18:41:40 +02002669 }
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002670
2671 return &pdomain->domain;
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002672}
2673
2674static void amd_iommu_domain_free(struct iommu_domain *dom)
Joerg Roedel26508152009-08-26 16:52:40 +02002675{
2676 struct protection_domain *domain;
2677
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002678 if (!dom)
Joerg Roedel98383fc2008-12-02 18:34:12 +01002679 return;
2680
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002681 domain = to_pdomain(dom);
2682
Joerg Roedel98383fc2008-12-02 18:34:12 +01002683 if (domain->dev_cnt > 0)
2684 cleanup_domain(domain);
2685
2686 BUG_ON(domain->dev_cnt != 0);
2687
Joerg Roedel132bd682011-11-17 14:18:46 +01002688 if (domain->mode != PAGE_MODE_NONE)
2689 free_pagetable(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002690
Joerg Roedel52815b72011-11-17 17:24:28 +01002691 if (domain->flags & PD_IOMMUV2_MASK)
2692 free_gcr3_table(domain);
2693
Joerg Roedel8b408fe2010-03-08 14:20:07 +01002694 protection_domain_free(domain);
Joerg Roedel98383fc2008-12-02 18:34:12 +01002695}
2696
Joerg Roedel684f2882008-12-08 12:07:44 +01002697static void amd_iommu_detach_device(struct iommu_domain *dom,
2698 struct device *dev)
2699{
Joerg Roedel657cbb62009-11-23 15:26:46 +01002700 struct iommu_dev_data *dev_data = dev->archdata.iommu;
Joerg Roedel684f2882008-12-08 12:07:44 +01002701 struct amd_iommu *iommu;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002702 int devid;
Joerg Roedel684f2882008-12-08 12:07:44 +01002703
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002704 if (!check_device(dev))
Joerg Roedel684f2882008-12-08 12:07:44 +01002705 return;
2706
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002707 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002708 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002709 return;
Joerg Roedel684f2882008-12-08 12:07:44 +01002710
Joerg Roedel657cbb62009-11-23 15:26:46 +01002711 if (dev_data->domain != NULL)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002712 detach_device(dev);
Joerg Roedel684f2882008-12-08 12:07:44 +01002713
2714 iommu = amd_iommu_rlookup_table[devid];
2715 if (!iommu)
2716 return;
2717
Joerg Roedel684f2882008-12-08 12:07:44 +01002718 iommu_completion_wait(iommu);
2719}
2720
Joerg Roedel01106062008-12-02 19:34:11 +01002721static int amd_iommu_attach_device(struct iommu_domain *dom,
2722 struct device *dev)
2723{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002724 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel657cbb62009-11-23 15:26:46 +01002725 struct iommu_dev_data *dev_data;
Joerg Roedel01106062008-12-02 19:34:11 +01002726 struct amd_iommu *iommu;
Joerg Roedel15898bb2009-11-24 15:39:42 +01002727 int ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002728
Joerg Roedel98fc5a62009-11-24 17:19:23 +01002729 if (!check_device(dev))
Joerg Roedel01106062008-12-02 19:34:11 +01002730 return -EINVAL;
2731
Joerg Roedel657cbb62009-11-23 15:26:46 +01002732 dev_data = dev->archdata.iommu;
2733
Joerg Roedelf62dda62011-06-09 12:55:35 +02002734 iommu = amd_iommu_rlookup_table[dev_data->devid];
Joerg Roedel01106062008-12-02 19:34:11 +01002735 if (!iommu)
2736 return -EINVAL;
2737
Joerg Roedel657cbb62009-11-23 15:26:46 +01002738 if (dev_data->domain)
Joerg Roedel15898bb2009-11-24 15:39:42 +01002739 detach_device(dev);
Joerg Roedel01106062008-12-02 19:34:11 +01002740
Joerg Roedel15898bb2009-11-24 15:39:42 +01002741 ret = attach_device(dev, domain);
Joerg Roedel01106062008-12-02 19:34:11 +01002742
2743 iommu_completion_wait(iommu);
2744
Joerg Roedel15898bb2009-11-24 15:39:42 +01002745 return ret;
Joerg Roedel01106062008-12-02 19:34:11 +01002746}
2747
Joerg Roedel468e2362010-01-21 16:37:36 +01002748static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002749 phys_addr_t paddr, size_t page_size, int iommu_prot)
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002750{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002751 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002752 int prot = 0;
2753 int ret;
2754
Joerg Roedel132bd682011-11-17 14:18:46 +01002755 if (domain->mode == PAGE_MODE_NONE)
2756 return -EINVAL;
2757
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002758 if (iommu_prot & IOMMU_READ)
2759 prot |= IOMMU_PROT_IR;
2760 if (iommu_prot & IOMMU_WRITE)
2761 prot |= IOMMU_PROT_IW;
2762
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002763 mutex_lock(&domain->api_lock);
Joerg Roedelb911b892016-07-05 14:29:11 +02002764 ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002765 mutex_unlock(&domain->api_lock);
2766
Joerg Roedel795e74f72010-05-11 17:40:57 +02002767 return ret;
Joerg Roedelc6229ca2008-12-02 19:48:43 +01002768}
2769
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002770static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
2771 size_t page_size)
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002772{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002773 struct protection_domain *domain = to_pdomain(dom);
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002774 size_t unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002775
Joerg Roedel132bd682011-11-17 14:18:46 +01002776 if (domain->mode == PAGE_MODE_NONE)
2777 return -EINVAL;
2778
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002779 mutex_lock(&domain->api_lock);
Joerg Roedel468e2362010-01-21 16:37:36 +01002780 unmap_size = iommu_unmap_page(domain, iova, page_size);
Joerg Roedel795e74f72010-05-11 17:40:57 +02002781 mutex_unlock(&domain->api_lock);
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002782
Joerg Roedel17b124b2011-04-06 18:01:35 +02002783 domain_flush_tlb_pde(domain);
Joerg Roedel5d214fe2010-02-08 14:44:49 +01002784
Ohad Ben-Cohen50090652011-11-10 11:32:25 +02002785 return unmap_size;
Joerg Roedeleb74ff62008-12-02 19:59:10 +01002786}
2787
Joerg Roedel645c4c82008-12-02 20:05:50 +01002788static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
Varun Sethibb5547ac2013-03-29 01:23:58 +05302789 dma_addr_t iova)
Joerg Roedel645c4c82008-12-02 20:05:50 +01002790{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002791 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel3039ca12015-04-01 14:58:48 +02002792 unsigned long offset_mask, pte_pgsize;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002793 u64 *pte, __pte;
Joerg Roedel645c4c82008-12-02 20:05:50 +01002794
Joerg Roedel132bd682011-11-17 14:18:46 +01002795 if (domain->mode == PAGE_MODE_NONE)
2796 return iova;
2797
Joerg Roedel3039ca12015-04-01 14:58:48 +02002798 pte = fetch_pte(domain, iova, &pte_pgsize);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002799
Joerg Roedela6d41a42009-09-02 17:08:55 +02002800 if (!pte || !IOMMU_PTE_PRESENT(*pte))
Joerg Roedel645c4c82008-12-02 20:05:50 +01002801 return 0;
2802
Joerg Roedelb24b1b62015-04-01 14:58:51 +02002803 offset_mask = pte_pgsize - 1;
2804 __pte = *pte & PM_ADDR_MASK;
Joerg Roedelf03152b2010-01-21 16:15:24 +01002805
Joerg Roedelb24b1b62015-04-01 14:58:51 +02002806 return (__pte & ~offset_mask) | (iova & offset_mask);
Joerg Roedel645c4c82008-12-02 20:05:50 +01002807}
2808
Joerg Roedelab636482014-09-05 10:48:21 +02002809static bool amd_iommu_capable(enum iommu_cap cap)
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002810{
Joerg Roedel80a506b2010-07-27 17:14:24 +02002811 switch (cap) {
2812 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedelab636482014-09-05 10:48:21 +02002813 return true;
Joerg Roedelbdddadc2012-07-02 18:38:13 +02002814 case IOMMU_CAP_INTR_REMAP:
Joerg Roedelab636482014-09-05 10:48:21 +02002815 return (irq_remapping_enabled == 1);
Will Deaconcfdeec22014-10-27 11:24:48 +00002816 case IOMMU_CAP_NOEXEC:
2817 return false;
Joerg Roedel80a506b2010-07-27 17:14:24 +02002818 }
2819
Joerg Roedelab636482014-09-05 10:48:21 +02002820 return false;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08002821}
2822
Joerg Roedel35cf2482015-05-28 18:41:37 +02002823static void amd_iommu_get_dm_regions(struct device *dev,
2824 struct list_head *head)
2825{
2826 struct unity_map_entry *entry;
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002827 int devid;
Joerg Roedel35cf2482015-05-28 18:41:37 +02002828
2829 devid = get_device_id(dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02002830 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04002831 return;
Joerg Roedel35cf2482015-05-28 18:41:37 +02002832
2833 list_for_each_entry(entry, &amd_iommu_unity_map, list) {
2834 struct iommu_dm_region *region;
2835
2836 if (devid < entry->devid_start || devid > entry->devid_end)
2837 continue;
2838
2839 region = kzalloc(sizeof(*region), GFP_KERNEL);
2840 if (!region) {
2841 pr_err("Out of memory allocating dm-regions for %s\n",
2842 dev_name(dev));
2843 return;
2844 }
2845
2846 region->start = entry->address_start;
2847 region->length = entry->address_end - entry->address_start;
2848 if (entry->prot & IOMMU_PROT_IR)
2849 region->prot |= IOMMU_READ;
2850 if (entry->prot & IOMMU_PROT_IW)
2851 region->prot |= IOMMU_WRITE;
2852
2853 list_add_tail(&region->list, head);
2854 }
2855}
2856
2857static void amd_iommu_put_dm_regions(struct device *dev,
2858 struct list_head *head)
2859{
2860 struct iommu_dm_region *entry, *next;
2861
2862 list_for_each_entry_safe(entry, next, head, list)
2863 kfree(entry);
2864}
2865
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02002866static void amd_iommu_apply_dm_region(struct device *dev,
2867 struct iommu_domain *domain,
2868 struct iommu_dm_region *region)
2869{
2870 struct protection_domain *pdomain = to_pdomain(domain);
2871 struct dma_ops_domain *dma_dom = pdomain->priv;
2872 unsigned long start, end;
2873
2874 start = IOVA_PFN(region->start);
2875 end = IOVA_PFN(region->start + region->length);
2876
2877 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
2878}
2879
Thierry Redingb22f6432014-06-27 09:03:12 +02002880static const struct iommu_ops amd_iommu_ops = {
Joerg Roedelab636482014-09-05 10:48:21 +02002881 .capable = amd_iommu_capable,
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002882 .domain_alloc = amd_iommu_domain_alloc,
2883 .domain_free = amd_iommu_domain_free,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002884 .attach_dev = amd_iommu_attach_device,
2885 .detach_dev = amd_iommu_detach_device,
Joerg Roedel468e2362010-01-21 16:37:36 +01002886 .map = amd_iommu_map,
2887 .unmap = amd_iommu_unmap,
Olav Haugan315786e2014-10-25 09:55:16 -07002888 .map_sg = default_iommu_map_sg,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002889 .iova_to_phys = amd_iommu_iova_to_phys,
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02002890 .add_device = amd_iommu_add_device,
2891 .remove_device = amd_iommu_remove_device,
Wan Zongshunb097d112016-04-01 09:06:04 -04002892 .device_group = amd_iommu_device_group,
Joerg Roedel35cf2482015-05-28 18:41:37 +02002893 .get_dm_regions = amd_iommu_get_dm_regions,
2894 .put_dm_regions = amd_iommu_put_dm_regions,
Joerg Roedel8d54d6c2016-07-05 13:32:20 +02002895 .apply_dm_region = amd_iommu_apply_dm_region,
Ohad Ben-Cohenaa3de9c2011-11-10 11:32:29 +02002896 .pgsize_bitmap = AMD_IOMMU_PGSIZES,
Joerg Roedel26961ef2008-12-03 17:00:17 +01002897};
2898
Joerg Roedel0feae532009-08-26 15:26:30 +02002899/*****************************************************************************
2900 *
2901 * The next functions do a basic initialization of IOMMU for pass through
2902 * mode
2903 *
2904 * In passthrough mode the IOMMU is initialized and enabled but not used for
2905 * DMA-API translation.
2906 *
2907 *****************************************************************************/
2908
Joerg Roedel72e1dcc2011-11-10 19:13:51 +01002909/* IOMMUv2 specific functions */
2910int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
2911{
2912 return atomic_notifier_chain_register(&ppr_notifier, nb);
2913}
2914EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
2915
2916int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
2917{
2918 return atomic_notifier_chain_unregister(&ppr_notifier, nb);
2919}
2920EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
Joerg Roedel132bd682011-11-17 14:18:46 +01002921
2922void amd_iommu_domain_direct_map(struct iommu_domain *dom)
2923{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002924 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel132bd682011-11-17 14:18:46 +01002925 unsigned long flags;
2926
2927 spin_lock_irqsave(&domain->lock, flags);
2928
2929 /* Update data structure */
2930 domain->mode = PAGE_MODE_NONE;
2931 domain->updated = true;
2932
2933 /* Make changes visible to IOMMUs */
2934 update_domain(domain);
2935
2936 /* Page-table is not visible to IOMMU anymore, so free it */
2937 free_pagetable(domain);
2938
2939 spin_unlock_irqrestore(&domain->lock, flags);
2940}
2941EXPORT_SYMBOL(amd_iommu_domain_direct_map);
Joerg Roedel52815b72011-11-17 17:24:28 +01002942
2943int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
2944{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01002945 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel52815b72011-11-17 17:24:28 +01002946 unsigned long flags;
2947 int levels, ret;
2948
2949 if (pasids <= 0 || pasids > (PASID_MASK + 1))
2950 return -EINVAL;
2951
2952 /* Number of GCR3 table levels required */
2953 for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
2954 levels += 1;
2955
2956 if (levels > amd_iommu_max_glx_val)
2957 return -EINVAL;
2958
2959 spin_lock_irqsave(&domain->lock, flags);
2960
2961 /*
2962 * Save us all sanity checks whether devices already in the
2963 * domain support IOMMUv2. Just force that the domain has no
2964 * devices attached when it is switched into IOMMUv2 mode.
2965 */
2966 ret = -EBUSY;
2967 if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
2968 goto out;
2969
2970 ret = -ENOMEM;
2971 domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
2972 if (domain->gcr3_tbl == NULL)
2973 goto out;
2974
2975 domain->glx = levels;
2976 domain->flags |= PD_IOMMUV2_MASK;
2977 domain->updated = true;
2978
2979 update_domain(domain);
2980
2981 ret = 0;
2982
2983out:
2984 spin_unlock_irqrestore(&domain->lock, flags);
2985
2986 return ret;
2987}
2988EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
Joerg Roedel22e266c2011-11-21 15:59:08 +01002989
2990static int __flush_pasid(struct protection_domain *domain, int pasid,
2991 u64 address, bool size)
2992{
2993 struct iommu_dev_data *dev_data;
2994 struct iommu_cmd cmd;
2995 int i, ret;
2996
2997 if (!(domain->flags & PD_IOMMUV2_MASK))
2998 return -EINVAL;
2999
3000 build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
3001
3002 /*
3003 * IOMMU TLB needs to be flushed before Device TLB to
3004 * prevent device TLB refill from IOMMU TLB
3005 */
3006 for (i = 0; i < amd_iommus_present; ++i) {
3007 if (domain->dev_iommu[i] == 0)
3008 continue;
3009
3010 ret = iommu_queue_command(amd_iommus[i], &cmd);
3011 if (ret != 0)
3012 goto out;
3013 }
3014
3015 /* Wait until IOMMU TLB flushes are complete */
3016 domain_flush_complete(domain);
3017
3018 /* Now flush device TLBs */
3019 list_for_each_entry(dev_data, &domain->dev_list, list) {
3020 struct amd_iommu *iommu;
3021 int qdep;
3022
Joerg Roedel1c1cc452015-07-30 11:24:45 +02003023 /*
3024 There might be non-IOMMUv2 capable devices in an IOMMUv2
3025 * domain.
3026 */
3027 if (!dev_data->ats.enabled)
3028 continue;
Joerg Roedel22e266c2011-11-21 15:59:08 +01003029
3030 qdep = dev_data->ats.qdep;
3031 iommu = amd_iommu_rlookup_table[dev_data->devid];
3032
3033 build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
3034 qdep, address, size);
3035
3036 ret = iommu_queue_command(iommu, &cmd);
3037 if (ret != 0)
3038 goto out;
3039 }
3040
3041 /* Wait until all device TLBs are flushed */
3042 domain_flush_complete(domain);
3043
3044 ret = 0;
3045
3046out:
3047
3048 return ret;
3049}
3050
3051static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
3052 u64 address)
3053{
3054 return __flush_pasid(domain, pasid, address, false);
3055}
3056
3057int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
3058 u64 address)
3059{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003060 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003061 unsigned long flags;
3062 int ret;
3063
3064 spin_lock_irqsave(&domain->lock, flags);
3065 ret = __amd_iommu_flush_page(domain, pasid, address);
3066 spin_unlock_irqrestore(&domain->lock, flags);
3067
3068 return ret;
3069}
3070EXPORT_SYMBOL(amd_iommu_flush_page);
3071
3072static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
3073{
3074 return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
3075 true);
3076}
3077
3078int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
3079{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003080 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedel22e266c2011-11-21 15:59:08 +01003081 unsigned long flags;
3082 int ret;
3083
3084 spin_lock_irqsave(&domain->lock, flags);
3085 ret = __amd_iommu_flush_tlb(domain, pasid);
3086 spin_unlock_irqrestore(&domain->lock, flags);
3087
3088 return ret;
3089}
3090EXPORT_SYMBOL(amd_iommu_flush_tlb);
3091
Joerg Roedelb16137b2011-11-21 16:50:23 +01003092static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
3093{
3094 int index;
3095 u64 *pte;
3096
3097 while (true) {
3098
3099 index = (pasid >> (9 * level)) & 0x1ff;
3100 pte = &root[index];
3101
3102 if (level == 0)
3103 break;
3104
3105 if (!(*pte & GCR3_VALID)) {
3106 if (!alloc)
3107 return NULL;
3108
3109 root = (void *)get_zeroed_page(GFP_ATOMIC);
3110 if (root == NULL)
3111 return NULL;
3112
3113 *pte = __pa(root) | GCR3_VALID;
3114 }
3115
3116 root = __va(*pte & PAGE_MASK);
3117
3118 level -= 1;
3119 }
3120
3121 return pte;
3122}
3123
3124static int __set_gcr3(struct protection_domain *domain, int pasid,
3125 unsigned long cr3)
3126{
3127 u64 *pte;
3128
3129 if (domain->mode != PAGE_MODE_NONE)
3130 return -EINVAL;
3131
3132 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
3133 if (pte == NULL)
3134 return -ENOMEM;
3135
3136 *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
3137
3138 return __amd_iommu_flush_tlb(domain, pasid);
3139}
3140
3141static int __clear_gcr3(struct protection_domain *domain, int pasid)
3142{
3143 u64 *pte;
3144
3145 if (domain->mode != PAGE_MODE_NONE)
3146 return -EINVAL;
3147
3148 pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
3149 if (pte == NULL)
3150 return 0;
3151
3152 *pte = 0;
3153
3154 return __amd_iommu_flush_tlb(domain, pasid);
3155}
3156
3157int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
3158 unsigned long cr3)
3159{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003160 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003161 unsigned long flags;
3162 int ret;
3163
3164 spin_lock_irqsave(&domain->lock, flags);
3165 ret = __set_gcr3(domain, pasid, cr3);
3166 spin_unlock_irqrestore(&domain->lock, flags);
3167
3168 return ret;
3169}
3170EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
3171
3172int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
3173{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003174 struct protection_domain *domain = to_pdomain(dom);
Joerg Roedelb16137b2011-11-21 16:50:23 +01003175 unsigned long flags;
3176 int ret;
3177
3178 spin_lock_irqsave(&domain->lock, flags);
3179 ret = __clear_gcr3(domain, pasid);
3180 spin_unlock_irqrestore(&domain->lock, flags);
3181
3182 return ret;
3183}
3184EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
Joerg Roedelc99afa22011-11-21 18:19:25 +01003185
3186int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
3187 int status, int tag)
3188{
3189 struct iommu_dev_data *dev_data;
3190 struct amd_iommu *iommu;
3191 struct iommu_cmd cmd;
3192
3193 dev_data = get_dev_data(&pdev->dev);
3194 iommu = amd_iommu_rlookup_table[dev_data->devid];
3195
3196 build_complete_ppr(&cmd, dev_data->devid, pasid, status,
3197 tag, dev_data->pri_tlp);
3198
3199 return iommu_queue_command(iommu, &cmd);
3200}
3201EXPORT_SYMBOL(amd_iommu_complete_ppr);
Joerg Roedelf3572db2011-11-23 12:36:25 +01003202
3203struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
3204{
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003205 struct protection_domain *pdomain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003206
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003207 pdomain = get_domain(&pdev->dev);
3208 if (IS_ERR(pdomain))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003209 return NULL;
3210
3211 /* Only return IOMMUv2 domains */
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003212 if (!(pdomain->flags & PD_IOMMUV2_MASK))
Joerg Roedelf3572db2011-11-23 12:36:25 +01003213 return NULL;
3214
Joerg Roedel3f4b87b2015-03-26 13:43:07 +01003215 return &pdomain->domain;
Joerg Roedelf3572db2011-11-23 12:36:25 +01003216}
3217EXPORT_SYMBOL(amd_iommu_get_v2_domain);
Joerg Roedel6a113dd2011-12-01 12:04:58 +01003218
3219void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
3220{
3221 struct iommu_dev_data *dev_data;
3222
3223 if (!amd_iommu_v2_supported())
3224 return;
3225
3226 dev_data = get_dev_data(&pdev->dev);
3227 dev_data->errata |= (1 << erratum);
3228}
3229EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
Joerg Roedel52efdb82011-12-07 12:01:36 +01003230
3231int amd_iommu_device_info(struct pci_dev *pdev,
3232 struct amd_iommu_device_info *info)
3233{
3234 int max_pasids;
3235 int pos;
3236
3237 if (pdev == NULL || info == NULL)
3238 return -EINVAL;
3239
3240 if (!amd_iommu_v2_supported())
3241 return -EINVAL;
3242
3243 memset(info, 0, sizeof(*info));
3244
3245 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
3246 if (pos)
3247 info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
3248
3249 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
3250 if (pos)
3251 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
3252
3253 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
3254 if (pos) {
3255 int features;
3256
3257 max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
3258 max_pasids = min(max_pasids, (1 << 20));
3259
3260 info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
3261 info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
3262
3263 features = pci_pasid_features(pdev);
3264 if (features & PCI_PASID_CAP_EXEC)
3265 info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
3266 if (features & PCI_PASID_CAP_PRIV)
3267 info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
3268 }
3269
3270 return 0;
3271}
3272EXPORT_SYMBOL(amd_iommu_device_info);
Joerg Roedel2b324502012-06-21 16:29:10 +02003273
3274#ifdef CONFIG_IRQ_REMAP
3275
3276/*****************************************************************************
3277 *
3278 * Interrupt Remapping Implementation
3279 *
3280 *****************************************************************************/
3281
3282union irte {
3283 u32 val;
3284 struct {
3285 u32 valid : 1,
3286 no_fault : 1,
3287 int_type : 3,
3288 rq_eoi : 1,
3289 dm : 1,
3290 rsvd_1 : 1,
3291 destination : 8,
3292 vector : 8,
3293 rsvd_2 : 8;
3294 } fields;
3295};
3296
Jiang Liu9c724962015-04-14 10:29:52 +08003297struct irq_2_irte {
3298 u16 devid; /* Device ID for IRTE table */
3299 u16 index; /* Index into IRTE table*/
3300};
3301
Jiang Liu7c71d302015-04-13 14:11:33 +08003302struct amd_ir_data {
3303 struct irq_2_irte irq_2_irte;
3304 union irte irte_entry;
3305 union {
3306 struct msi_msg msi_entry;
3307 };
3308};
3309
3310static struct irq_chip amd_ir_chip;
3311
Joerg Roedel2b324502012-06-21 16:29:10 +02003312#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
3313#define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
3314#define DTE_IRQ_TABLE_LEN (8ULL << 1)
3315#define DTE_IRQ_REMAP_ENABLE 1ULL
3316
3317static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
3318{
3319 u64 dte;
3320
3321 dte = amd_iommu_dev_table[devid].data[2];
3322 dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
3323 dte |= virt_to_phys(table->table);
3324 dte |= DTE_IRQ_REMAP_INTCTL;
3325 dte |= DTE_IRQ_TABLE_LEN;
3326 dte |= DTE_IRQ_REMAP_ENABLE;
3327
3328 amd_iommu_dev_table[devid].data[2] = dte;
3329}
3330
3331#define IRTE_ALLOCATED (~1U)
3332
3333static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic)
3334{
3335 struct irq_remap_table *table = NULL;
3336 struct amd_iommu *iommu;
3337 unsigned long flags;
3338 u16 alias;
3339
3340 write_lock_irqsave(&amd_iommu_devtable_lock, flags);
3341
3342 iommu = amd_iommu_rlookup_table[devid];
3343 if (!iommu)
3344 goto out_unlock;
3345
3346 table = irq_lookup_table[devid];
3347 if (table)
3348 goto out;
3349
3350 alias = amd_iommu_alias_table[devid];
3351 table = irq_lookup_table[alias];
3352 if (table) {
3353 irq_lookup_table[devid] = table;
3354 set_dte_irq_entry(devid, table);
3355 iommu_flush_dte(iommu, devid);
3356 goto out;
3357 }
3358
3359 /* Nothing there yet, allocate new irq remapping table */
3360 table = kzalloc(sizeof(*table), GFP_ATOMIC);
3361 if (!table)
3362 goto out;
3363
Joerg Roedel197887f2013-04-09 21:14:08 +02003364 /* Initialize table spin-lock */
3365 spin_lock_init(&table->lock);
3366
Joerg Roedel2b324502012-06-21 16:29:10 +02003367 if (ioapic)
3368 /* Keep the first 32 indexes free for IOAPIC interrupts */
3369 table->min_index = 32;
3370
3371 table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC);
3372 if (!table->table) {
3373 kfree(table);
Dan Carpenter821f0f62012-10-02 11:34:40 +03003374 table = NULL;
Joerg Roedel2b324502012-06-21 16:29:10 +02003375 goto out;
3376 }
3377
3378 memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32));
3379
3380 if (ioapic) {
3381 int i;
3382
3383 for (i = 0; i < 32; ++i)
3384 table->table[i] = IRTE_ALLOCATED;
3385 }
3386
3387 irq_lookup_table[devid] = table;
3388 set_dte_irq_entry(devid, table);
3389 iommu_flush_dte(iommu, devid);
3390 if (devid != alias) {
3391 irq_lookup_table[alias] = table;
Alex Williamsone028a9e2014-04-22 10:08:40 -06003392 set_dte_irq_entry(alias, table);
Joerg Roedel2b324502012-06-21 16:29:10 +02003393 iommu_flush_dte(iommu, alias);
3394 }
3395
3396out:
3397 iommu_completion_wait(iommu);
3398
3399out_unlock:
3400 write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
3401
3402 return table;
3403}
3404
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003405static int alloc_irq_index(u16 devid, int count)
Joerg Roedel2b324502012-06-21 16:29:10 +02003406{
3407 struct irq_remap_table *table;
3408 unsigned long flags;
3409 int index, c;
3410
3411 table = get_irq_table(devid, false);
3412 if (!table)
3413 return -ENODEV;
3414
3415 spin_lock_irqsave(&table->lock, flags);
3416
3417 /* Scan table for free entries */
3418 for (c = 0, index = table->min_index;
3419 index < MAX_IRQS_PER_TABLE;
3420 ++index) {
3421 if (table->table[index] == 0)
3422 c += 1;
3423 else
3424 c = 0;
3425
3426 if (c == count) {
Joerg Roedel2b324502012-06-21 16:29:10 +02003427 for (; c != 0; --c)
3428 table->table[index - c + 1] = IRTE_ALLOCATED;
3429
3430 index -= count - 1;
Joerg Roedel2b324502012-06-21 16:29:10 +02003431 goto out;
3432 }
3433 }
3434
3435 index = -ENOSPC;
3436
3437out:
3438 spin_unlock_irqrestore(&table->lock, flags);
3439
3440 return index;
3441}
3442
Joerg Roedel2b324502012-06-21 16:29:10 +02003443static int modify_irte(u16 devid, int index, union irte irte)
3444{
3445 struct irq_remap_table *table;
3446 struct amd_iommu *iommu;
3447 unsigned long flags;
3448
3449 iommu = amd_iommu_rlookup_table[devid];
3450 if (iommu == NULL)
3451 return -EINVAL;
3452
3453 table = get_irq_table(devid, false);
3454 if (!table)
3455 return -ENOMEM;
3456
3457 spin_lock_irqsave(&table->lock, flags);
3458 table->table[index] = irte.val;
3459 spin_unlock_irqrestore(&table->lock, flags);
3460
3461 iommu_flush_irt(iommu, devid);
3462 iommu_completion_wait(iommu);
3463
3464 return 0;
3465}
3466
3467static void free_irte(u16 devid, int index)
3468{
3469 struct irq_remap_table *table;
3470 struct amd_iommu *iommu;
3471 unsigned long flags;
3472
3473 iommu = amd_iommu_rlookup_table[devid];
3474 if (iommu == NULL)
3475 return;
3476
3477 table = get_irq_table(devid, false);
3478 if (!table)
3479 return;
3480
3481 spin_lock_irqsave(&table->lock, flags);
3482 table->table[index] = 0;
3483 spin_unlock_irqrestore(&table->lock, flags);
3484
3485 iommu_flush_irt(iommu, devid);
3486 iommu_completion_wait(iommu);
3487}
3488
Jiang Liu7c71d302015-04-13 14:11:33 +08003489static int get_devid(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003490{
Jiang Liu7c71d302015-04-13 14:11:33 +08003491 int devid = -1;
Joerg Roedel5527de72012-06-26 11:17:32 +02003492
Jiang Liu7c71d302015-04-13 14:11:33 +08003493 switch (info->type) {
3494 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3495 devid = get_ioapic_devid(info->ioapic_id);
3496 break;
3497 case X86_IRQ_ALLOC_TYPE_HPET:
3498 devid = get_hpet_devid(info->hpet_id);
3499 break;
3500 case X86_IRQ_ALLOC_TYPE_MSI:
3501 case X86_IRQ_ALLOC_TYPE_MSIX:
3502 devid = get_device_id(&info->msi_dev->dev);
3503 break;
3504 default:
3505 BUG_ON(1);
3506 break;
Joerg Roedel5527de72012-06-26 11:17:32 +02003507 }
3508
Jiang Liu7c71d302015-04-13 14:11:33 +08003509 return devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003510}
3511
Jiang Liu7c71d302015-04-13 14:11:33 +08003512static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
Joerg Roedel5527de72012-06-26 11:17:32 +02003513{
Jiang Liu7c71d302015-04-13 14:11:33 +08003514 struct amd_iommu *iommu;
3515 int devid;
Joerg Roedel5527de72012-06-26 11:17:32 +02003516
Jiang Liu7c71d302015-04-13 14:11:33 +08003517 if (!info)
3518 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003519
Jiang Liu7c71d302015-04-13 14:11:33 +08003520 devid = get_devid(info);
3521 if (devid >= 0) {
3522 iommu = amd_iommu_rlookup_table[devid];
3523 if (iommu)
3524 return iommu->ir_domain;
3525 }
Joerg Roedel5527de72012-06-26 11:17:32 +02003526
Jiang Liu7c71d302015-04-13 14:11:33 +08003527 return NULL;
Joerg Roedel5527de72012-06-26 11:17:32 +02003528}
3529
Jiang Liu7c71d302015-04-13 14:11:33 +08003530static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003531{
Jiang Liu7c71d302015-04-13 14:11:33 +08003532 struct amd_iommu *iommu;
3533 int devid;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003534
Jiang Liu7c71d302015-04-13 14:11:33 +08003535 if (!info)
3536 return NULL;
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003537
Jiang Liu7c71d302015-04-13 14:11:33 +08003538 switch (info->type) {
3539 case X86_IRQ_ALLOC_TYPE_MSI:
3540 case X86_IRQ_ALLOC_TYPE_MSIX:
3541 devid = get_device_id(&info->msi_dev->dev);
Joerg Roedel9ee35e42016-04-21 18:21:31 +02003542 if (devid < 0)
Wan Zongshun7aba6cb2016-04-01 09:06:02 -04003543 return NULL;
3544
Dan Carpenter1fb260b2016-01-07 12:36:06 +03003545 iommu = amd_iommu_rlookup_table[devid];
3546 if (iommu)
3547 return iommu->msi_domain;
Jiang Liu7c71d302015-04-13 14:11:33 +08003548 break;
3549 default:
3550 break;
3551 }
Joerg Roedel0b4d48c2012-06-26 14:54:17 +02003552
Jiang Liu7c71d302015-04-13 14:11:33 +08003553 return NULL;
Joerg Roedeld9761952012-06-26 16:00:08 +02003554}
3555
Joerg Roedel6b474b82012-06-26 16:46:04 +02003556struct irq_remap_ops amd_iommu_irq_ops = {
Joerg Roedel6b474b82012-06-26 16:46:04 +02003557 .prepare = amd_iommu_prepare,
3558 .enable = amd_iommu_enable,
3559 .disable = amd_iommu_disable,
3560 .reenable = amd_iommu_reenable,
3561 .enable_faulting = amd_iommu_enable_faulting,
Jiang Liu7c71d302015-04-13 14:11:33 +08003562 .get_ir_irq_domain = get_ir_irq_domain,
3563 .get_irq_domain = get_irq_domain,
Joerg Roedel6b474b82012-06-26 16:46:04 +02003564};
Jiang Liu7c71d302015-04-13 14:11:33 +08003565
3566static void irq_remapping_prepare_irte(struct amd_ir_data *data,
3567 struct irq_cfg *irq_cfg,
3568 struct irq_alloc_info *info,
3569 int devid, int index, int sub_handle)
3570{
3571 struct irq_2_irte *irte_info = &data->irq_2_irte;
3572 struct msi_msg *msg = &data->msi_entry;
3573 union irte *irte = &data->irte_entry;
3574 struct IO_APIC_route_entry *entry;
3575
Jiang Liu7c71d302015-04-13 14:11:33 +08003576 data->irq_2_irte.devid = devid;
3577 data->irq_2_irte.index = index + sub_handle;
3578
3579 /* Setup IRTE for IOMMU */
3580 irte->val = 0;
3581 irte->fields.vector = irq_cfg->vector;
3582 irte->fields.int_type = apic->irq_delivery_mode;
3583 irte->fields.destination = irq_cfg->dest_apicid;
3584 irte->fields.dm = apic->irq_dest_mode;
3585 irte->fields.valid = 1;
3586
3587 switch (info->type) {
3588 case X86_IRQ_ALLOC_TYPE_IOAPIC:
3589 /* Setup IOAPIC entry */
3590 entry = info->ioapic_entry;
3591 info->ioapic_entry = NULL;
3592 memset(entry, 0, sizeof(*entry));
3593 entry->vector = index;
3594 entry->mask = 0;
3595 entry->trigger = info->ioapic_trigger;
3596 entry->polarity = info->ioapic_polarity;
3597 /* Mask level triggered irqs. */
3598 if (info->ioapic_trigger)
3599 entry->mask = 1;
3600 break;
3601
3602 case X86_IRQ_ALLOC_TYPE_HPET:
3603 case X86_IRQ_ALLOC_TYPE_MSI:
3604 case X86_IRQ_ALLOC_TYPE_MSIX:
3605 msg->address_hi = MSI_ADDR_BASE_HI;
3606 msg->address_lo = MSI_ADDR_BASE_LO;
3607 msg->data = irte_info->index;
3608 break;
3609
3610 default:
3611 BUG_ON(1);
3612 break;
3613 }
3614}
3615
3616static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
3617 unsigned int nr_irqs, void *arg)
3618{
3619 struct irq_alloc_info *info = arg;
3620 struct irq_data *irq_data;
3621 struct amd_ir_data *data;
3622 struct irq_cfg *cfg;
3623 int i, ret, devid;
3624 int index = -1;
3625
3626 if (!info)
3627 return -EINVAL;
3628 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
3629 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
3630 return -EINVAL;
3631
3632 /*
3633 * With IRQ remapping enabled, don't need contiguous CPU vectors
3634 * to support multiple MSI interrupts.
3635 */
3636 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
3637 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
3638
3639 devid = get_devid(info);
3640 if (devid < 0)
3641 return -EINVAL;
3642
3643 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
3644 if (ret < 0)
3645 return ret;
3646
Jiang Liu7c71d302015-04-13 14:11:33 +08003647 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
3648 if (get_irq_table(devid, true))
3649 index = info->ioapic_pin;
3650 else
3651 ret = -ENOMEM;
3652 } else {
Jiang Liu3c3d4f92015-04-13 14:11:38 +08003653 index = alloc_irq_index(devid, nr_irqs);
Jiang Liu7c71d302015-04-13 14:11:33 +08003654 }
3655 if (index < 0) {
3656 pr_warn("Failed to allocate IRTE\n");
Jiang Liu7c71d302015-04-13 14:11:33 +08003657 goto out_free_parent;
3658 }
3659
3660 for (i = 0; i < nr_irqs; i++) {
3661 irq_data = irq_domain_get_irq_data(domain, virq + i);
3662 cfg = irqd_cfg(irq_data);
3663 if (!irq_data || !cfg) {
3664 ret = -EINVAL;
3665 goto out_free_data;
3666 }
3667
Joerg Roedela130e692015-08-13 11:07:25 +02003668 ret = -ENOMEM;
3669 data = kzalloc(sizeof(*data), GFP_KERNEL);
3670 if (!data)
3671 goto out_free_data;
3672
Jiang Liu7c71d302015-04-13 14:11:33 +08003673 irq_data->hwirq = (devid << 16) + i;
3674 irq_data->chip_data = data;
3675 irq_data->chip = &amd_ir_chip;
3676 irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
3677 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
3678 }
Joerg Roedela130e692015-08-13 11:07:25 +02003679
Jiang Liu7c71d302015-04-13 14:11:33 +08003680 return 0;
3681
3682out_free_data:
3683 for (i--; i >= 0; i--) {
3684 irq_data = irq_domain_get_irq_data(domain, virq + i);
3685 if (irq_data)
3686 kfree(irq_data->chip_data);
3687 }
3688 for (i = 0; i < nr_irqs; i++)
3689 free_irte(devid, index + i);
3690out_free_parent:
3691 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3692 return ret;
3693}
3694
3695static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
3696 unsigned int nr_irqs)
3697{
3698 struct irq_2_irte *irte_info;
3699 struct irq_data *irq_data;
3700 struct amd_ir_data *data;
3701 int i;
3702
3703 for (i = 0; i < nr_irqs; i++) {
3704 irq_data = irq_domain_get_irq_data(domain, virq + i);
3705 if (irq_data && irq_data->chip_data) {
3706 data = irq_data->chip_data;
3707 irte_info = &data->irq_2_irte;
3708 free_irte(irte_info->devid, irte_info->index);
3709 kfree(data);
3710 }
3711 }
3712 irq_domain_free_irqs_common(domain, virq, nr_irqs);
3713}
3714
3715static void irq_remapping_activate(struct irq_domain *domain,
3716 struct irq_data *irq_data)
3717{
3718 struct amd_ir_data *data = irq_data->chip_data;
3719 struct irq_2_irte *irte_info = &data->irq_2_irte;
3720
3721 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3722}
3723
3724static void irq_remapping_deactivate(struct irq_domain *domain,
3725 struct irq_data *irq_data)
3726{
3727 struct amd_ir_data *data = irq_data->chip_data;
3728 struct irq_2_irte *irte_info = &data->irq_2_irte;
3729 union irte entry;
3730
3731 entry.val = 0;
3732 modify_irte(irte_info->devid, irte_info->index, data->irte_entry);
3733}
3734
3735static struct irq_domain_ops amd_ir_domain_ops = {
3736 .alloc = irq_remapping_alloc,
3737 .free = irq_remapping_free,
3738 .activate = irq_remapping_activate,
3739 .deactivate = irq_remapping_deactivate,
3740};
3741
3742static int amd_ir_set_affinity(struct irq_data *data,
3743 const struct cpumask *mask, bool force)
3744{
3745 struct amd_ir_data *ir_data = data->chip_data;
3746 struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
3747 struct irq_cfg *cfg = irqd_cfg(data);
3748 struct irq_data *parent = data->parent_data;
3749 int ret;
3750
3751 ret = parent->chip->irq_set_affinity(parent, mask, force);
3752 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
3753 return ret;
3754
3755 /*
3756 * Atomically updates the IRTE with the new destination, vector
3757 * and flushes the interrupt entry cache.
3758 */
3759 ir_data->irte_entry.fields.vector = cfg->vector;
3760 ir_data->irte_entry.fields.destination = cfg->dest_apicid;
3761 modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry);
3762
3763 /*
3764 * After this point, all the interrupts will start arriving
3765 * at the new destination. So, time to cleanup the previous
3766 * vector allocation.
3767 */
Jiang Liuc6c20022015-04-14 10:30:02 +08003768 send_cleanup_vector(cfg);
Jiang Liu7c71d302015-04-13 14:11:33 +08003769
3770 return IRQ_SET_MASK_OK_DONE;
3771}
3772
3773static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
3774{
3775 struct amd_ir_data *ir_data = irq_data->chip_data;
3776
3777 *msg = ir_data->msi_entry;
3778}
3779
3780static struct irq_chip amd_ir_chip = {
3781 .irq_ack = ir_ack_apic_edge,
3782 .irq_set_affinity = amd_ir_set_affinity,
3783 .irq_compose_msi_msg = ir_compose_msi_msg,
3784};
3785
3786int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
3787{
3788 iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu);
3789 if (!iommu->ir_domain)
3790 return -ENOMEM;
3791
3792 iommu->ir_domain->parent = arch_get_ir_parent_domain();
3793 iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
3794
3795 return 0;
3796}
Joerg Roedel2b324502012-06-21 16:29:10 +02003797#endif